DS3234 rtc SPI communication problem

kyrpav
Posts: 37
Joined: Wed Jan 29, 2020 8:27 am

DS3234 rtc SPI communication problem

Postby kyrpav » Sun Mar 01, 2020 11:35 pm

Hello, i am trying to learn protocols and ESP32. I am a Arduino user, but i want to move to ESP32 with eclipse , not using arduino ide at all.

I am trying to user DS3234 rtc with SPI communication but i can not make it work.

First i have set the RTC with Arduino and everything worked perfect also the 1Hz pulse was fine (blinking led). Then i tried first to read only from ESP32 but i can not make it work.

I tried pins from SPI2 (HSPI_HOST) from API reference site and also the pinout from the spi_master template, in mode 1 and 3 based on what mode is used in Arduino .ide (i also tried mode 0) , I tried freq of 1,4,10,16,80MHz for Spi_freq. Arduino SPI frequency is 4MHz.

From API reference in Espressif site:
GPIO Number
CS0* 15
SCLK 14
MISO 12
MOSI 13

I also tried to set manually CS to low and high at the beginning and end of transmission also by setting
  1. spi_device_interface_config_t.spics_io_num=-1
. I am giving you the main.c file and the output that i have.

I am using time.h and time() function to get system time but as you will see in the output this also does not give correct hour, also even trying to set 0:00:00 of Tue 1 Jan of 1970 to rtc does not work cause you will see that the return of Read function is 0:00:00 1 as day 2000-1-1 which is not correct and also there is no change in secs.

May main concern is if there is a problem in my spi setup about ,bus ,dev, initialization etc. If anyone can help it will be great. Thanks.

Main.c:
  1. #include "stdio.h"
  2. #include "stdbool.h"
  3. #include "freertos/FreeRTOS.h"
  4. #include "esp_system.h"
  5. #include "freertos/task.h"
  6. #include "driver/gpio.h"
  7. #include "driver/spi_master.h"
  8. #include "string.h"
  9. #include "time.h"
  10.  
  11. #define MISO 12
  12. #define MOSI 13
  13. #define CLK 14
  14. #define CS 15
  15.  
  16. #define PIN_NUM_MISO 25
  17. #define PIN_NUM_MOSI 23
  18. #define PIN_NUM_CLK  19
  19. #define PIN_NUM_CS   22
  20.  
  21. #define SQW_1Hz 0b00000000;
  22. #define WRITE_CONTROL_REG 0x8E
  23. #define READ_CONTROL_REG 0x0E
  24. #define WRITE_TIME_REG 0x80
  25. #define READ_TIME_REG 0x00
  26.  
  27.  
  28. spi_device_handle_t spi;
  29. spi_bus_config_t busCfg;
  30. spi_device_interface_config_t devCfg;
  31.  
  32.  
  33. uint8_t bcdToInt(uint8_t bcd){
  34.     return bcd-6*(bcd>>4);
  35. }
  36.  
  37. uint8_t intToBcd(uint8_t dec){
  38.     return dec+6*(dec/10);
  39. }
  40.  
  41. uint8_t bcdTo24Hour(uint8_t bcdHour){
  42.     uint8_t hour;
  43.     if(bcdHour&0x40){
  44.         bool isPm = ((bcdHour& 0x20)!=0);
  45.         hour=bcdToInt(bcdHour& 0x1f);
  46.         if(isPm){
  47.             hour+=12;
  48.         }
  49.     }else{
  50.         hour= bcdToInt(bcdHour);
  51.     }
  52.     return hour;
  53. }
  54.  
  55.  
  56. void spiInit(){
  57.  
  58.     busCfg.miso_io_num=MISO;
  59.     busCfg.mosi_io_num=MOSI;
  60.     busCfg.sclk_io_num=CLK;
  61.  
  62. //  busCfg.miso_io_num=PIN_NUM_MISO;
  63. //  busCfg.mosi_io_num=PIN_NUM_MOSI;
  64. //  busCfg.sclk_io_num=PIN_NUM_CLK;
  65.     busCfg.quadhd_io_num=-1;
  66.     busCfg.quadwp_io_num=-1;
  67.  
  68.     spi_bus_initialize(HSPI_HOST,&busCfg,2);
  69.  
  70.     vTaskDelay(100/portTICK_PERIOD_MS);
  71.  
  72.     devCfg.mode=1;
  73.     devCfg.clock_speed_hz=1*1000*1000;
  74.     devCfg.spics_io_num=CS;
  75.     devCfg.queue_size=7;
  76.  
  77.     spi_bus_add_device(HSPI_HOST,&devCfg,&spi);
  78.  
  79.     vTaskDelay(100/portTICK_PERIOD_MS);
  80.  
  81. //  gpio_pad_select_gpio(PIN_NUM_CS);
  82. //  gpio_set_direction(PIN_NUM_CS,GPIO_MODE_OUTPUT);
  83. //  gpio_set_level(PIN_NUM_CS,1);
  84.  
  85. //  gpio_pad_select_gpio(CS);
  86. //  gpio_set_direction(CS,GPIO_MODE_OUTPUT);
  87. //  gpio_set_level(CS,1);
  88.  
  89. }
  90.  
  91. void setCS(){
  92.  
  93.     gpio_set_level(CS,0);
  94. }
  95.  
  96. void unsetCS(){
  97.     gpio_set_level(CS,1);
  98. }
  99.  
  100. void setControlReg(const uint8_t cmd){
  101.  
  102.  
  103.     esp_err_t ret;
  104.     spi_transaction_t t;
  105.     memset(&t,0,sizeof(t));
  106.     t.length=8;
  107.     t.tx_buffer=&cmd;
  108.     ret=spi_device_transmit(spi,&t);
  109.  
  110.     assert(ret==ESP_OK);
  111. }
  112. void setTime(const uint8_t *data,uint8_t cmd,int len){
  113.  
  114.     //setCS();
  115.     setControlReg(cmd);
  116.  
  117.     esp_err_t ret;
  118.     spi_transaction_t t;
  119.     memset(&t,0,sizeof(t));
  120.     t.length=len*8;
  121.     t.tx_buffer=&data;
  122.     ret=spi_device_transmit(spi,&t);
  123.     assert(ret==ESP_OK);
  124.     //unsetCS();
  125. }
  126.  
  127. uint8_t* readData(const uint8_t cmd,int len,uint8_t *data){
  128.  
  129.     //setCS();
  130.     setControlReg(cmd);
  131.  
  132.     esp_err_t ret;
  133.     spi_transaction_t t;
  134.     memset(&t,0,sizeof(t));
  135.     t.length=len*8;
  136.     t.flags=SPI_TRANS_USE_RXDATA;
  137.     ret=spi_device_transmit(spi,&t);
  138.     //printf("Get Data result");
  139.     assert(ret==ESP_OK);
  140.  
  141.     //unsetCS();
  142.  
  143.     data=t.rx_data;
  144.  
  145.     return data;
  146.  
  147. }
  148.  
  149. void print_time(struct tm tm){
  150.  
  151.     printf("Time: %i:%i:%i %i Day %i-%i-%i\n",tm.tm_hour,tm.tm_min,tm.tm_sec,tm.tm_wday,tm.tm_year+2000,tm.tm_mon+1,tm.tm_mday);
  152. }
  153.  
  154. uint8_t* timeToBuffer(uint8_t *data,struct tm tm){
  155.     printf("Time to buffer");
  156.     print_time(tm);
  157.     data[0]=intToBcd(tm.tm_sec);
  158.     data[1]=intToBcd(tm.tm_min);
  159.     data[2]=intToBcd(tm.tm_hour);
  160.     data[3]=intToBcd(tm.tm_wday);
  161.     data[4]=intToBcd(tm.tm_mday);
  162.     data[5]=intToBcd(tm.tm_mon);
  163.     data[6]=intToBcd(tm.tm_year);
  164.  
  165.     return data;
  166.  
  167. }
  168.  
  169. void bufferToTime(struct tm *tm,uint8_t *buffer){
  170.  
  171.  
  172.     tm->tm_sec=bcdToInt(buffer[0]);
  173.     tm->tm_min=bcdToInt(buffer[1]);
  174.     tm->tm_hour=bcdTo24Hour(buffer[2]);
  175.     tm->tm_wday=bcdToInt(buffer[3]);
  176.     tm->tm_mday=bcdToInt(buffer[4]);
  177.     tm->tm_mon=bcdToInt(buffer[5]);
  178.     tm->tm_year=bcdToInt(buffer[6]);
  179.  
  180. }
  181.  
  182.  
  183. void app_main(void)
  184. {
  185.     printf("Initializing SPI\n");
  186.     spiInit();
  187.  
  188.     printf("Set Time\n");
  189.  
  190.     uint8_t bufferData[7];
  191.     memset(bufferData,0,sizeof(bufferData));
  192.  
  193.     time_t curTime;
  194.     time(&curTime);
  195.     printf("%s\n",ctime(&curTime));
  196.     struct tm *myTime=localtime(&curTime);
  197.  
  198.  
  199.     setTime(timeToBuffer(bufferData,*myTime),WRITE_TIME_REG,7);
  200.     memset(bufferData,0,sizeof(bufferData));
  201.  
  202.     printf("Start Reading\n");
  203.     while (true) {
  204.         readData(READ_TIME_REG,7,bufferData);
  205.         bufferToTime(myTime,bufferData);
  206.         print_time(*myTime);
  207.         vTaskDelay(1000/portTICK_PERIOD_MS);
  208.     }
  209. }
  210.  

Output:
  1. Initializing SPI
  2. Set Time
  3. Thu Jan  1 00:00:00 1970
  4.  
  5. Time to bufferTime: 0:0:0 4 Day 2070-1-1
  6. Start Reading
  7. Time: 0:0:0 0 Day 2000-1-0
  8. Time: 0:0:0 0 Day 2000-1-0
  9. Time: 0:0:0 0 Day 2000-1-0
Attachments
main.c
(3.85 KiB) Downloaded 354 times

kyrpav
Posts: 37
Joined: Wed Jan 29, 2020 8:27 am

Re: DS3234 rtc SPI communication problem

Postby kyrpav » Mon Mar 02, 2020 9:45 pm

I found the problem.

Set of time is working properly. On read i had to comment out the flag and set the t.rx_buffer to the data variable. SO the code in this function works like. And CS has to be handled manually.
  1. uint8_t* readData(const uint8_t cmd,int len,uint8_t *data){
  2.  
  3.     setCS();
  4.     setControlReg(cmd);
  5.  
  6.     esp_err_t ret;
  7.     spi_transaction_t t;
  8.     memset(&t,0,sizeof(t));
  9.     t.length=len*8;
  10.     //t.flags=SPI_TRANS_USE_RXDATA;
  11.     //t.tx_buffer=data;
  12.     t.rx_buffer=data;
  13.     ret=spi_device_transmit(spi,&t);
  14.     //printf("Get Data result");
  15.     assert(ret==ESP_OK);
  16.  
  17.     unsetCS();
  18.  
  19.     //data=t.rx_data;
  20.  
  21.     return data;
  22.  
  23. }

Who is online

Users browsing this forum: Baidu [Spider] and 117 guests