Skip to content

Commit

Permalink
Fix DS3231 driver
Browse files Browse the repository at this point in the history
Fix DS3231 driver breaking function chain (#4759)
  • Loading branch information
arendst committed Dec 30, 2018
1 parent e5c38da commit fb4fe0f
Showing 1 changed file with 10 additions and 20 deletions.
30 changes: 10 additions & 20 deletions sonoff/xsns_33_ds3231.ino
Original file line number Diff line number Diff line change
Expand Up @@ -62,27 +62,23 @@
#define HR1224 6 //Hours register 12 or 24 hour mode (24 hour mode==0)
#define CENTURY 7 //Century bit in Month register
#define DYDT 6 //Day/Date flag bit in alarm Day/Date registers
boolean ds3231ReadStatus = false , ds3231WriteStatus = false; //flag, we want to wriet/write to DS3231 onlu once
boolean DS3231chipDetected;

boolean ds3231ReadStatus = false;
boolean ds3231WriteStatus = false; //flag, we want to wriet/write to DS3231 onlu once
boolean DS3231chipDetected = false;

/*----------------------------------------------------------------------*
Detect the DS3231 Chip
----------------------------------------------------------------------*/
boolean DS3231Detect(void)
void DS3231Detect(void)
{
if (I2cValidRead(USE_RTC_ADDR, RTC_STATUS, 1))
{
DS3231chipDetected = false;
if (I2cValidRead(USE_RTC_ADDR, RTC_STATUS, 1)) {
snprintf_P(log_data, sizeof(log_data), S_LOG_I2C_FOUND_AT, "DS3231", USE_RTC_ADDR);
AddLog(LOG_LEVEL_INFO);
return true;
}
else
{
DS3231chipDetected = true;
} else {
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_I2C "%s *NOT* " D_FOUND_AT " 0x%x"), "DS3231", USE_RTC_ADDR);
AddLog(LOG_LEVEL_INFO);
return false;
}
AddLog(LOG_LEVEL_INFO);
}

/*----------------------------------------------------------------------*
Expand Down Expand Up @@ -143,10 +139,8 @@ boolean Xsns33(byte function)
if (i2c_flg) {
switch (function) {
case FUNC_INIT:
DS3231chipDetected = DS3231Detect();
result = DS3231chipDetected;
DS3231Detect();
break;

case FUNC_EVERY_SECOND:
TIME_T tmpTime;
if (!ds3231ReadStatus && DS3231chipDetected && utc_time < 1451602800 ) { // We still did not sync with NTP (time not valid) , so, read time from DS3231
Expand All @@ -169,7 +163,6 @@ boolean Xsns33(byte function)
} else {
rules_flag.time_set = 1;
}
result = true;
}
else if (!ds3231WriteStatus && DS3231chipDetected && utc_time > 1451602800 && abs(utc_time - ReadFromDS3231()) > 60) {//if time is valid and is drift from RTC in more that 60 second
snprintf_P(log_data, sizeof(log_data), PSTR("Write Time TO DS3231 from NTP (" D_UTC_TIME ") %s, (" D_DST_TIME ") %s, (" D_STD_TIME ") %s"),
Expand All @@ -178,9 +171,6 @@ boolean Xsns33(byte function)
SetDS3231Time (utc_time); //update the DS3231 time
ds3231WriteStatus = true;
}
else {
result = false;
}
break;
}
}
Expand Down

0 comments on commit fb4fe0f

Please sign in to comment.