Refactor BM8563 driver

This commit is contained in:
Theo Arends 2021-11-02 14:54:32 +01:00
parent b36a531fb7
commit 903c5c1de7
1 changed files with 45 additions and 52 deletions

View File

@ -1,7 +1,7 @@
/* /*
xdrv_52_9_berry.ino - Berry scripting language xdrv_56_BM8563_RTC.ino - BM8563 RTC
Copyright (C) 2021 Stephan Hadinger, Berry language by Guan Wenliang https://github.com/Skiars/berry Copyright (C) 2021 Stephan Hadinger and Theo Arends
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
@ -17,9 +17,13 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifdef USE_I2C #ifdef USE_I2C
#ifdef USE_BM8563 #ifdef USE_BM8563
/*********************************************************************************************\
* BM8563 - Real Time Clock
*
* I2C Address: 0x51 (Fixed in library as BM8563_ADRESS)
\*********************************************************************************************/
#define XDRV_56 56 #define XDRV_56 56
#define XI2C_59 59 // See I2CDEVICES.md #define XI2C_59 59 // See I2CDEVICES.md
@ -32,31 +36,7 @@ struct {
bool ntp_time_ok = false; bool ntp_time_ok = false;
} bm8563_driver; } bm8563_driver;
/*********************************************************************************************\
*
*
\*********************************************************************************************/
void BM8563Detect(void) {
#ifdef ESP32
if (!I2cSetDevice(BM8563_ADRESS, 0)) {
if (!I2cSetDevice(BM8563_ADRESS, 1)) { return; } // check on bus 1
bm8563_driver.Rtc.setBus(1); // switch to bus 1
I2cSetActiveFound(BM8563_ADRESS, "BM8563", 1);
} else {
I2cSetActiveFound(BM8563_ADRESS, "BM8563", 0);
}
#else
if (!I2cSetDevice(BM8563_ADRESS)) { return; }
I2cSetActiveFound(BM8563_ADRESS, "BM8563");
#endif
bm8563_driver.Rtc.begin();
bm8563_driver.rtc_ready = true;
}
uint32_t BM8563GetUtc(void) { uint32_t BM8563GetUtc(void) {
if (!bm8563_driver.rtc_ready) return 0;
RTC_TimeTypeDef RTCtime; RTC_TimeTypeDef RTCtime;
// 1. read has errors ??? // 1. read has errors ???
bm8563_driver.Rtc.GetTime(&RTCtime); bm8563_driver.Rtc.GetTime(&RTCtime);
@ -75,7 +55,6 @@ uint32_t BM8563GetUtc(void) {
} }
void BM8563SetUtc(uint32_t epoch_time) { void BM8563SetUtc(uint32_t epoch_time) {
if (!bm8563_driver.rtc_ready) return;
TIME_T tm; TIME_T tm;
BreakTime(epoch_time, tm); BreakTime(epoch_time, tm);
RTC_TimeTypeDef RTCtime; RTC_TimeTypeDef RTCtime;
@ -92,7 +71,7 @@ void BM8563SetUtc(uint32_t epoch_time) {
} }
void InitTimeFromRTC(void) { void InitTimeFromRTC(void) {
if (bm8563_driver.rtc_ready && Rtc.utc_time < START_VALID_TIME) { if (Rtc.utc_time < START_VALID_TIME) {
// set rtc from chip // set rtc from chip
Rtc.utc_time = BM8563GetUtc(); Rtc.utc_time = BM8563GetUtc();
@ -111,39 +90,53 @@ void InitTimeFromRTC(void) {
} }
} }
void BM8563Detect(void) {
#ifdef ESP32
if (!I2cSetDevice(BM8563_ADRESS, 0)) {
if (!I2cSetDevice(BM8563_ADRESS, 1)) { return; } // check on bus 1
bm8563_driver.Rtc.setBus(1); // switch to bus 1
I2cSetActiveFound(BM8563_ADRESS, "BM8563", 1);
} else {
I2cSetActiveFound(BM8563_ADRESS, "BM8563", 0);
}
#else
if (!I2cSetDevice(BM8563_ADRESS)) { return; }
I2cSetActiveFound(BM8563_ADRESS, "BM8563");
#endif
bm8563_driver.Rtc.begin();
bm8563_driver.rtc_ready = true;
InitTimeFromRTC();
}
void BM8563EverySecond(void) { void BM8563EverySecond(void) {
if (bm8563_driver.rtc_ready) { if (!bm8563_driver.ntp_time_ok && (Rtc.utc_time > START_VALID_TIME) && abs((int32_t)Rtc.utc_time - (int32_t)BM8563GetUtc()) > 3) {
if (!bm8563_driver.ntp_time_ok && Rtc.utc_time > START_VALID_TIME && abs((int32_t)Rtc.utc_time - (int32_t)BM8563GetUtc()) > 3) {
BM8563SetUtc(Rtc.utc_time); BM8563SetUtc(Rtc.utc_time);
AddLog(LOG_LEVEL_INFO, PSTR("I2C: Write Time TO BM8563 from NTP (" D_UTC_TIME ") %s, (" D_DST_TIME ") %s, (" D_STD_TIME ") %s"), AddLog(LOG_LEVEL_INFO, PSTR("I2C: Write time to BM8563 from NTP (" D_UTC_TIME ") %s, (" D_DST_TIME ") %s, (" D_STD_TIME ") %s"),
GetDateAndTime(DT_UTC).c_str(), GetDateAndTime(DT_DST).c_str(), GetDateAndTime(DT_STD).c_str()); GetDateAndTime(DT_UTC).c_str(), GetDateAndTime(DT_DST).c_str(), GetDateAndTime(DT_STD).c_str());
bm8563_driver.ntp_time_ok = true; bm8563_driver.ntp_time_ok = true;
} }
} }
}
/*********************************************************************************************\ /*********************************************************************************************\
* Interface * Interface
\*********************************************************************************************/ \*********************************************************************************************/
bool Xdrv56(uint8_t function)
{ bool Xdrv56(uint8_t function) {
bool result = false;
if (!I2cEnabled(XI2C_59)) { return false; } if (!I2cEnabled(XI2C_59)) { return false; }
switch (function) { bool result = false;
// case FUNC_PRE_INIT: // we start Berry in pre_init so that other modules can call Berry in their init methods
case FUNC_INIT:
BM8563Detect();
InitTimeFromRTC();
break;
if (FUNC_INIT == function) {
BM8563Detect();
}
else if (bm8563_driver.rtc_ready) {
switch (function) {
case FUNC_EVERY_SECOND: case FUNC_EVERY_SECOND:
BM8563EverySecond(); BM8563EverySecond();
break; break;
}
case FUNC_SAVE_BEFORE_RESTART:
break;
} }
return result; return result;
} }