From 516dff5b097756173af216b7e6488a8818da0973 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Thu, 28 Apr 2022 08:18:03 +0200 Subject: [PATCH] RTC Module support for T-Echo and others with PCF8563 --- src/configuration.h | 3 +++ src/debug/i2cScan.h | 6 ++++++ src/gps/RTC.cpp | 30 ++++++++++++++++++++++++++++++ variants/t-echo/platformio.ini | 1 + variants/t-echo/variant.h | 3 +++ 5 files changed, 43 insertions(+) diff --git a/src/configuration.h b/src/configuration.h index 60cf7512..3e1e680f 100644 --- a/src/configuration.h +++ b/src/configuration.h @@ -28,6 +28,9 @@ along with this program. If not, see . #ifdef RV3028_RTC #include "Melopero_RV3028.h" #endif +#ifdef PCF8563_RTC + #include "pcf8563.h" +#endif // ----------------------------------------------------------------------------- // Version diff --git a/src/debug/i2cScan.h b/src/debug/i2cScan.h index ae6384fb..4bfd2adf 100644 --- a/src/debug/i2cScan.h +++ b/src/debug/i2cScan.h @@ -63,6 +63,12 @@ void scanI2Cdevice(void) rtc.writeToRegister(0x35,0x07); // no Clkout rtc.writeToRegister(0x37,0xB4); } +#endif +#ifdef PCF8563_RTC + if (addr == PCF8563_RTC){ + rtc_found = addr; + DEBUG_MSG("PCF8563 RTC found\n"); + } #endif if (addr == CARDKB_ADDR) { cardkb_found = addr; diff --git a/src/gps/RTC.cpp b/src/gps/RTC.cpp index e717f97b..335dbe37 100644 --- a/src/gps/RTC.cpp +++ b/src/gps/RTC.cpp @@ -40,6 +40,28 @@ void readFromRTC() currentQuality = RTCQualityDevice; } } +#elif defined(PCF8563_RTC) + if(rtc_found == PCF8563_RTC) { + uint32_t now = millis(); + PCF8563_Class rtc; + rtc.begin(); + auto tc = rtc.getDateTime(); + tm t; + t.tm_year = tc.year; + t.tm_mon = tc.month; + t.tm_mday = tc.day; + t.tm_hour = tc.hour; + t.tm_min = tc.minute; + t.tm_sec = tc.second; + tv.tv_sec = mktime(&t); + tv.tv_usec = 0; + DEBUG_MSG("Read RTC time from PCF8563 as %ld\n", tv.tv_sec); + timeStartMsec = now; + zeroOffsetSecs = tv.tv_sec; + if (currentQuality == RTCQualityNone) { + currentQuality = RTCQualityDevice; + } + } #else if (!gettimeofday(&tv, NULL)) { uint32_t now = millis(); @@ -85,6 +107,14 @@ bool perhapsSetRTC(RTCQuality q, const struct timeval *tv) rtc.setTime(t->tm_year + 1900, t->tm_mon + 1, t->tm_wday, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec); DEBUG_MSG("RV3028_RTC setTime %02d-%02d-%02d %02d:%02d:%02d %ld\n", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, tv->tv_sec); } +#elif defined(PCF8563_RTC) + if(rtc_found == PCF8563_RTC) { + PCF8563_Class rtc; + rtc.begin(); + tm *t = localtime(&tv->tv_sec); + rtc.setDateTime(t->tm_year + 1900, t->tm_mon + 1, t->tm_wday, t->tm_hour, t->tm_min, t->tm_sec); + DEBUG_MSG("PCF8563_RTC setDateTime %02d-%02d-%02d %02d:%02d:%02d %ld\n", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, tv->tv_sec); + } #elif !defined(NO_ESP32) settimeofday(tv, NULL); #endif diff --git a/variants/t-echo/platformio.ini b/variants/t-echo/platformio.ini index 9714b026..fca61584 100644 --- a/variants/t-echo/platformio.ini +++ b/variants/t-echo/platformio.ini @@ -14,4 +14,5 @@ lib_deps = ${nrf52840_base.lib_deps} https://github.com/meshtastic/GxEPD2 adafruit/Adafruit BusIO + lewisxhe/PCF8563_Library@^0.0.1 ;upload_protocol = fs diff --git a/variants/t-echo/variant.h b/variants/t-echo/variant.h index 4f8946e9..094894c7 100644 --- a/variants/t-echo/variant.h +++ b/variants/t-echo/variant.h @@ -182,6 +182,9 @@ External serial flash WP25R1635FZUIL0 #define PIN_SERIAL1_RX PIN_GPS_TX #define PIN_SERIAL1_TX PIN_GPS_RX +// PCF8563 RTC Module +#define PCF8563_RTC 0x51 + /* * SPI Interfaces */