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
- spi_device_interface_config_t.spics_io_num=-1
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:
- #include "stdio.h"
- #include "stdbool.h"
- #include "freertos/FreeRTOS.h"
- #include "esp_system.h"
- #include "freertos/task.h"
- #include "driver/gpio.h"
- #include "driver/spi_master.h"
- #include "string.h"
- #include "time.h"
- #define MISO 12
- #define MOSI 13
- #define CLK 14
- #define CS 15
- #define PIN_NUM_MISO 25
- #define PIN_NUM_MOSI 23
- #define PIN_NUM_CLK 19
- #define PIN_NUM_CS 22
- #define SQW_1Hz 0b00000000;
- #define WRITE_CONTROL_REG 0x8E
- #define READ_CONTROL_REG 0x0E
- #define WRITE_TIME_REG 0x80
- #define READ_TIME_REG 0x00
- spi_device_handle_t spi;
- spi_bus_config_t busCfg;
- spi_device_interface_config_t devCfg;
- uint8_t bcdToInt(uint8_t bcd){
- return bcd-6*(bcd>>4);
- }
- uint8_t intToBcd(uint8_t dec){
- return dec+6*(dec/10);
- }
- uint8_t bcdTo24Hour(uint8_t bcdHour){
- uint8_t hour;
- if(bcdHour&0x40){
- bool isPm = ((bcdHour& 0x20)!=0);
- hour=bcdToInt(bcdHour& 0x1f);
- if(isPm){
- hour+=12;
- }
- }else{
- hour= bcdToInt(bcdHour);
- }
- return hour;
- }
- void spiInit(){
- busCfg.miso_io_num=MISO;
- busCfg.mosi_io_num=MOSI;
- busCfg.sclk_io_num=CLK;
- // busCfg.miso_io_num=PIN_NUM_MISO;
- // busCfg.mosi_io_num=PIN_NUM_MOSI;
- // busCfg.sclk_io_num=PIN_NUM_CLK;
- busCfg.quadhd_io_num=-1;
- busCfg.quadwp_io_num=-1;
- spi_bus_initialize(HSPI_HOST,&busCfg,2);
- vTaskDelay(100/portTICK_PERIOD_MS);
- devCfg.mode=1;
- devCfg.clock_speed_hz=1*1000*1000;
- devCfg.spics_io_num=CS;
- devCfg.queue_size=7;
- spi_bus_add_device(HSPI_HOST,&devCfg,&spi);
- vTaskDelay(100/portTICK_PERIOD_MS);
- // gpio_pad_select_gpio(PIN_NUM_CS);
- // gpio_set_direction(PIN_NUM_CS,GPIO_MODE_OUTPUT);
- // gpio_set_level(PIN_NUM_CS,1);
- // gpio_pad_select_gpio(CS);
- // gpio_set_direction(CS,GPIO_MODE_OUTPUT);
- // gpio_set_level(CS,1);
- }
- void setCS(){
- gpio_set_level(CS,0);
- }
- void unsetCS(){
- gpio_set_level(CS,1);
- }
- void setControlReg(const uint8_t cmd){
- esp_err_t ret;
- spi_transaction_t t;
- memset(&t,0,sizeof(t));
- t.length=8;
- t.tx_buffer=&cmd;
- ret=spi_device_transmit(spi,&t);
- assert(ret==ESP_OK);
- }
- void setTime(const uint8_t *data,uint8_t cmd,int len){
- //setCS();
- setControlReg(cmd);
- esp_err_t ret;
- spi_transaction_t t;
- memset(&t,0,sizeof(t));
- t.length=len*8;
- t.tx_buffer=&data;
- ret=spi_device_transmit(spi,&t);
- assert(ret==ESP_OK);
- //unsetCS();
- }
- uint8_t* readData(const uint8_t cmd,int len,uint8_t *data){
- //setCS();
- setControlReg(cmd);
- esp_err_t ret;
- spi_transaction_t t;
- memset(&t,0,sizeof(t));
- t.length=len*8;
- t.flags=SPI_TRANS_USE_RXDATA;
- ret=spi_device_transmit(spi,&t);
- //printf("Get Data result");
- assert(ret==ESP_OK);
- //unsetCS();
- data=t.rx_data;
- return data;
- }
- void print_time(struct tm tm){
- 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);
- }
- uint8_t* timeToBuffer(uint8_t *data,struct tm tm){
- printf("Time to buffer");
- print_time(tm);
- data[0]=intToBcd(tm.tm_sec);
- data[1]=intToBcd(tm.tm_min);
- data[2]=intToBcd(tm.tm_hour);
- data[3]=intToBcd(tm.tm_wday);
- data[4]=intToBcd(tm.tm_mday);
- data[5]=intToBcd(tm.tm_mon);
- data[6]=intToBcd(tm.tm_year);
- return data;
- }
- void bufferToTime(struct tm *tm,uint8_t *buffer){
- tm->tm_sec=bcdToInt(buffer[0]);
- tm->tm_min=bcdToInt(buffer[1]);
- tm->tm_hour=bcdTo24Hour(buffer[2]);
- tm->tm_wday=bcdToInt(buffer[3]);
- tm->tm_mday=bcdToInt(buffer[4]);
- tm->tm_mon=bcdToInt(buffer[5]);
- tm->tm_year=bcdToInt(buffer[6]);
- }
- void app_main(void)
- {
- printf("Initializing SPI\n");
- spiInit();
- printf("Set Time\n");
- uint8_t bufferData[7];
- memset(bufferData,0,sizeof(bufferData));
- time_t curTime;
- time(&curTime);
- printf("%s\n",ctime(&curTime));
- struct tm *myTime=localtime(&curTime);
- setTime(timeToBuffer(bufferData,*myTime),WRITE_TIME_REG,7);
- memset(bufferData,0,sizeof(bufferData));
- printf("Start Reading\n");
- while (true) {
- readData(READ_TIME_REG,7,bufferData);
- bufferToTime(myTime,bufferData);
- print_time(*myTime);
- vTaskDelay(1000/portTICK_PERIOD_MS);
- }
- }
Output:
- Initializing SPI
- Set Time
- Thu Jan 1 00:00:00 1970
- Time to bufferTime: 0:0:0 4 Day 2070-1-1
- Start Reading
- Time: 0:0:0 0 Day 2000-1-0
- Time: 0:0:0 0 Day 2000-1-0
- Time: 0:0:0 0 Day 2000-1-0