I have MAX31341 RTC connected to ESP32 over I2C. Connection works, I can set and get time, but for unknown reason, hour register counts hours from 0 to 31:
E (3888) main: RTC time: 2022-08-22 23:59:12
E (63888) main: RTC time: 2022-08-22 24:00:12
E (123888) main: RTC time: 2022-08-22 24:01:12
E (183888) main: RTC time: 2022-08-22 24:02:11
E (243888) main: RTC time: 2022-08-22 24:03:11
This is my first RTC integration, so it is possible that I'm doing something wrong, but after a lot of tests I have no idea what.
MAX31341 rtc(I2C_NUM_0,MAX31341_I2C_ADDRESS);
void setupRTC()
{
esp_err_t rc;
struct timeval tv;
i2c_config_t conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = 21,
.scl_io_num = 22,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master = {
.clk_speed = 400000
},
.clk_flags = I2C_SCLK_SRC_FLAG_FOR_NOMAL
};
rc = i2c_param_config(I2C_NUM_0, &conf);
if( rc != ESP_OK )
ESP_LOGE(TAG,"i2c_param_config failed, rc=%d", rc);
rc = i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0);
if( rc != ESP_OK )
ESP_LOGE(TAG,"i2c_driver_install failed, rc=%d", rc);
rtc.begin();
}
int main()
{
// rtc.set_time( '2022-08-22 23:59:12' );
while(1)
{
int ret = rtc.get_time(&rtc_ctime);
if (ret)
ESP_LOGE( TAG, "get_time failed!");
else
{
char buf[64];
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &rtc_ctime);
ESP_LOGE( TAG, "RTC time: %s", buf );
}
sleep(60);
}
return 0;
}
MAX31341 code, comes from Arduino MaxEssentialToolkit v1.0.1 with read and write register functions, replaced with esp-idf substitutes.