From 654b7052476263c8a944cebad7a253c0ef38cee5 Mon Sep 17 00:00:00 2001 From: pennam Date: Mon, 9 Jun 2025 11:02:25 +0200 Subject: [PATCH 1/3] TimeService: RTC automatically fallback on software implementation --- src/AIoTC_Config.h | 6 + src/utility/time/RTCMillis.cpp | 4 +- src/utility/time/RTCMillis.h | 4 +- src/utility/time/TimeService.cpp | 308 ++++++++----------------------- 4 files changed, 91 insertions(+), 231 deletions(-) diff --git a/src/AIoTC_Config.h b/src/AIoTC_Config.h index a25b81080..9127676c3 100644 --- a/src/AIoTC_Config.h +++ b/src/AIoTC_Config.h @@ -142,6 +142,12 @@ #define BOARD_HAS_SECURE_ELEMENT #endif +#if (defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_MBED) ||\ + defined(ARDUINO_ARCH_RENESAS) || defined(ARDUINO_ARCH_ESP32)) &&\ + !defined(ARDUINO_ARCH_ZEPHYR) + #define BOARD_HAS_HW_RTC +#endif + #endif // HAS_NOTECARD #if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION) || defined(ARDUINO_OPTA) || defined(ARDUINO_GIGA) diff --git a/src/utility/time/RTCMillis.cpp b/src/utility/time/RTCMillis.cpp index 7ceb27436..0dc970e8d 100644 --- a/src/utility/time/RTCMillis.cpp +++ b/src/utility/time/RTCMillis.cpp @@ -21,7 +21,7 @@ #include "AIoTC_Config.h" -#if defined(HAS_NOTECARD) || defined(ARDUINO_ARCH_ESP8266) || defined (ARDUINO_RASPBERRY_PI_PICO_W) +#if !defined(BOARD_HAS_HW_RTC) #include #include "RTCMillis.h" @@ -61,4 +61,4 @@ unsigned long RTCMillis::get() return _last_rtc_update_value; } -#endif /* HAS_NOTECARD || ARDUINO_ARCH_ESP8266 || ARDUINO_RASPBERRY_PI_PICO_W */ +#endif /* BOARD_HAS_HW_RTC */ diff --git a/src/utility/time/RTCMillis.h b/src/utility/time/RTCMillis.h index 03d352941..543988071 100644 --- a/src/utility/time/RTCMillis.h +++ b/src/utility/time/RTCMillis.h @@ -18,7 +18,7 @@ #ifndef ARDUINO_IOT_CLOUD_RTC_MILLIS_H_ #define ARDUINO_IOT_CLOUD_RTC_MILLIS_H_ -#if defined(HAS_NOTECARD) || defined(ARDUINO_ARCH_ESP8266) || defined (ARDUINO_RASPBERRY_PI_PICO_W) +#if !defined(BOARD_HAS_HW_RTC) /************************************************************************************** * INCLUDE @@ -45,6 +45,6 @@ class RTCMillis }; -#endif /* HAS_NOTECARD || ARDUINO_ARCH_ESP8266 || ARDUINO_RASPBERRY_PI_PICO_W */ +#endif /* BOARD_HAS_HW_RTC */ #endif /* ARDUINO_IOT_CLOUD_RTC_MILLIS_H_ */ diff --git a/src/utility/time/TimeService.cpp b/src/utility/time/TimeService.cpp index fb7272fc4..577d4b86a 100644 --- a/src/utility/time/TimeService.cpp +++ b/src/utility/time/TimeService.cpp @@ -19,7 +19,6 @@ * INCLUDE **************************************************************************************/ - #include #include "AIoTC_Config.h" @@ -27,23 +26,29 @@ #include "NTPUtils.h" #include "TimeService.h" -#if defined(HAS_NOTECARD) || defined(ARDUINO_ARCH_ESP8266) || defined (ARDUINO_RASPBERRY_PI_PICO_W) +#if defined(BOARD_HAS_HW_RTC) + #if defined(ARDUINO_ARCH_SAMD) + #include + #endif + #if defined(ARDUINO_ARCH_MBED) + #include + #endif + #if defined(ARDUINO_ARCH_RENESAS) + #include + #endif +#else #include "RTCMillis.h" -#elif defined(ARDUINO_ARCH_SAMD) - #include -#elif defined(ARDUINO_ARCH_MBED) - #include -#elif defined(ARDUINO_ARCH_RENESAS) - #include "RTC.h" #endif /************************************************************************************** * GLOBAL VARIABLES **************************************************************************************/ -#if defined(HAS_NOTECARD) || defined(ARDUINO_ARCH_ESP8266) || defined (ARDUINO_RASPBERRY_PI_PICO_W) +#if !defined(BOARD_HAS_HW_RTC) RTCMillis rtc; -#elif defined(ARDUINO_ARCH_SAMD) +#endif + +#if defined(BOARD_HAS_HW_RTC) && defined(ARDUINO_ARCH_SAMD) RTCZero rtc; #endif @@ -53,50 +58,72 @@ RTCZero rtc; time_t cvt_time(char const * time); -#if defined(HAS_NOTECARD) -void notecard_initRTC(); -void notecard_setRTC(unsigned long time); -unsigned long notecard_getRTC(); -#else - -#ifdef ARDUINO_ARCH_SAMD -void samd_initRTC(); -void samd_setRTC(unsigned long time); -unsigned long samd_getRTC(); -#endif - -#ifdef ARDUINO_ARCH_MBED -void mbed_initRTC(); -void mbed_setRTC(unsigned long time); -unsigned long mbed_getRTC(); -#endif - -#ifdef ARDUINO_ARCH_ESP32 -void esp32_initRTC(); -void esp32_setRTC(unsigned long time); -unsigned long esp32_getRTC(); -#endif - -#ifdef ARDUINO_ARCH_ESP8266 -void esp8266_initRTC(); -void esp8266_setRTC(unsigned long time); -unsigned long esp8266_getRTC(); -#endif - -#ifdef ARDUINO_ARCH_RENESAS -void renesas_initRTC(); -void renesas_setRTC(unsigned long time); -unsigned long renesas_getRTC(); -#endif +/************************************************************************************** + * RTC PRIVATE FUNCTION DEFINITION + **************************************************************************************/ -#ifdef ARDUINO_RASPBERRY_PI_PICO_W -void pico_w_initRTC(); -void pico_w_setRTC(unsigned long time); -unsigned long pico_w_getRTC(); +#if defined(BOARD_HAS_HW_RTC) + #if defined(ARDUINO_ARCH_SAMD) +static inline void _initRTC() { + rtc.begin(); +} +static inline void _setRTC(unsigned long time) { + rtc.setEpoch(time); +} +static inline unsigned long _getRTC() { + return rtc.getEpoch(); +} + #endif + #if defined(ARDUINO_ARCH_MBED) +static inline void _initRTC() { + /* Nothing to do */ +} +static inline void _setRTC(unsigned long time) { + set_time(time); +} +static inline unsigned long _getRTC() { + return time(NULL); +} + #endif + #if defined(ARDUINO_ARCH_RENESAS) +static inline void _initRTC() { + RTC.begin(); +} +static inline void _setRTC(unsigned long time) { + RTCTime t(time); + RTC.setTime(t); +} +static inline unsigned long _getRTC() { + RTCTime t; + RTC.getTime(t); + return t.getUnixTime(); +} + #endif + #if defined(ARDUINO_ARCH_ESP32) +static inline void _initRTC() { + //configTime(0, 0, "time.arduino.cc", "pool.ntp.org", "time.nist.gov"); +} +static inline void _setRTC(unsigned long time) { + const timeval epoch = {(time_t)time, 0}; + settimeofday(&epoch, 0); +} +static inline unsigned long _getRTC() { + return time(NULL); +} + #endif +#else /* !BOARD_HAS_HW_RTC */ + #pragma message "No hardware RTC implementation found, using soft RTC" +static inline void _initRTC() { + rtc.begin(); +} +static inline void _setRTC(unsigned long time) { + rtc.set(time); +} +static inline unsigned long _getRTC() { + return rtc.get(); +} #endif -#endif /* HAS_NOTECARD */ - /************************************************************************************** * DEFINES **************************************************************************************/ @@ -349,65 +376,17 @@ bool TimeServiceClass::isTimeZoneOffsetValid(long const offset) void TimeServiceClass::initRTC() { -#if defined (HAS_NOTECARD) - notecard_initRTC(); -#elif defined (ARDUINO_ARCH_SAMD) - samd_initRTC(); -#elif defined (ARDUINO_ARCH_MBED) - mbed_initRTC(); -#elif defined (ARDUINO_ARCH_ESP32) - esp32_initRTC(); -#elif defined (ARDUINO_ARCH_ESP8266) - esp8266_initRTC(); -#elif defined (ARDUINO_ARCH_RENESAS) - renesas_initRTC(); -#elif defined (ARDUINO_RASPBERRY_PI_PICO_W) - pico_w_initRTC(); -#else - #error "RTC not available for this architecture" -#endif + _initRTC(); } void TimeServiceClass::setRTC(unsigned long time) { -#if defined (HAS_NOTECARD) - notecard_setRTC(time); -#elif defined (ARDUINO_ARCH_SAMD) - samd_setRTC(time); -#elif defined (ARDUINO_ARCH_MBED) - mbed_setRTC(time); -#elif defined (ARDUINO_ARCH_ESP32) - esp32_setRTC(time); -#elif defined (ARDUINO_ARCH_ESP8266) - esp8266_setRTC(time); -#elif defined (ARDUINO_ARCH_RENESAS) - renesas_setRTC(time); -#elif defined (ARDUINO_RASPBERRY_PI_PICO_W) - pico_w_setRTC(time); -#else - #error "RTC not available for this architecture" -#endif + _setRTC(time); } unsigned long TimeServiceClass::getRTC() { -#if defined (HAS_NOTECARD) - return notecard_getRTC(); -#elif defined (ARDUINO_ARCH_SAMD) - return samd_getRTC(); -#elif defined (ARDUINO_ARCH_MBED) - return mbed_getRTC(); -#elif defined (ARDUINO_ARCH_ESP32) - return esp32_getRTC(); -#elif defined (ARDUINO_ARCH_ESP8266) - return esp8266_getRTC(); -#elif defined (ARDUINO_ARCH_RENESAS) - return renesas_getRTC(); -#elif defined (ARDUINO_RASPBERRY_PI_PICO_W) - return pico_w_getRTC(); -#else - #error "RTC not available for this architecture" -#endif + return _getRTC(); } /************************************************************************************** @@ -450,131 +429,6 @@ time_t cvt_time(char const * time) return build_time; } -#ifdef HAS_NOTECARD -void notecard_initRTC() -{ - rtc.begin(); -} - -void notecard_setRTC(unsigned long time) -{ - rtc.set(time); -} - -unsigned long notecard_getRTC() -{ - return rtc.get(); -} -#else - -#ifdef ARDUINO_ARCH_SAMD -void samd_initRTC() -{ - rtc.begin(); -} - -void samd_setRTC(unsigned long time) -{ - rtc.setEpoch(time); -} - -unsigned long samd_getRTC() -{ - return rtc.getEpoch(); -} -#endif - -#ifdef ARDUINO_ARCH_MBED -void mbed_initRTC() -{ - /* Nothing to do */ -} - -void mbed_setRTC(unsigned long time) -{ - set_time(time); -} - -unsigned long mbed_getRTC() -{ - return time(NULL); -} -#endif - -#ifdef ARDUINO_ARCH_ESP32 -void esp32_initRTC() -{ - //configTime(0, 0, "time.arduino.cc", "pool.ntp.org", "time.nist.gov"); -} - -void esp32_setRTC(unsigned long time) -{ - const timeval epoch = {(time_t)time, 0}; - settimeofday(&epoch, 0); -} - -unsigned long esp32_getRTC() -{ - return time(NULL); -} -#endif - -#ifdef ARDUINO_ARCH_ESP8266 -void esp8266_initRTC() -{ - rtc.begin(); -} - -void esp8266_setRTC(unsigned long time) -{ - rtc.set(time); -} - -unsigned long esp8266_getRTC() -{ - return rtc.get(); -} -#endif - -#ifdef ARDUINO_ARCH_RENESAS -void renesas_initRTC() -{ - RTC.begin(); -} - -void renesas_setRTC(unsigned long time) -{ - RTCTime t(time); - RTC.setTime(t); -} - -unsigned long renesas_getRTC() -{ - RTCTime t; - RTC.getTime(t); - return t.getUnixTime(); -} -#endif - -#ifdef ARDUINO_RASPBERRY_PI_PICO_W -void pico_w_initRTC() -{ - rtc.begin(); -} - -void pico_w_setRTC(unsigned long time) -{ - rtc.set(time); -} - -unsigned long pico_w_getRTC() -{ - return rtc.get(); -} -#endif - -#endif /* HAS_NOTECARD */ - /****************************************************************************** * EXTERN DEFINITION ******************************************************************************/ From 79a2e5a5803867afcb0467ef045030ce1493fe9b Mon Sep 17 00:00:00 2001 From: pennam Date: Mon, 9 Jun 2025 14:40:49 +0200 Subject: [PATCH 2/3] TimeService: re-licensing --- src/utility/time/TimeService.cpp | 17 +++++------------ src/utility/time/TimeService.h | 17 +++++------------ 2 files changed, 10 insertions(+), 24 deletions(-) diff --git a/src/utility/time/TimeService.cpp b/src/utility/time/TimeService.cpp index 577d4b86a..fa822d068 100644 --- a/src/utility/time/TimeService.cpp +++ b/src/utility/time/TimeService.cpp @@ -1,18 +1,11 @@ /* - This file is part of ArduinoIoTCloud. + This file is part of the ArduinoIoTCloud library. - Copyright 2020 ARDUINO SA (http://www.arduino.cc/) + Copyright (c) 2020 Arduino SA - This software is released under the GNU General Public License version 3, - which covers the main part of arduino-cli. - The terms of this license can be found at: - https://www.gnu.org/licenses/gpl-3.0.en.html - - You can be released from the requirements of the above licenses by purchasing - a commercial license. Buying such a license is mandatory if you want to modify or - otherwise use the software for commercial activities involving the Arduino - software without disclosing the source code of your own applications. To purchase - a commercial license, send an email to license@arduino.cc. + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. */ /************************************************************************************** diff --git a/src/utility/time/TimeService.h b/src/utility/time/TimeService.h index 71656d948..1f58bd781 100644 --- a/src/utility/time/TimeService.h +++ b/src/utility/time/TimeService.h @@ -1,18 +1,11 @@ /* - This file is part of ArduinoIoTCloud. + This file is part of the ArduinoIoTCloud library. - Copyright 2020 ARDUINO SA (http://www.arduino.cc/) + Copyright (c) 2020 Arduino SA - This software is released under the GNU General Public License version 3, - which covers the main part of arduino-cli. - The terms of this license can be found at: - https://www.gnu.org/licenses/gpl-3.0.en.html - - You can be released from the requirements of the above licenses by purchasing - a commercial license. Buying such a license is mandatory if you want to modify or - otherwise use the software for commercial activities involving the Arduino - software without disclosing the source code of your own applications. To purchase - a commercial license, send an email to license@arduino.cc. + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #ifndef ARDUINO_IOT_CLOUD_TIME_SERVICE_H_ From 239cd744bbd555037ad4036924b78da6fd164790 Mon Sep 17 00:00:00 2001 From: pennam Date: Mon, 9 Jun 2025 14:51:24 +0200 Subject: [PATCH 3/3] TimeService: cosmetic changes --- src/utility/time/TimeService.cpp | 40 ++++++++++++++++---------------- src/utility/time/TimeService.h | 8 +++---- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/utility/time/TimeService.cpp b/src/utility/time/TimeService.cpp index fa822d068..9d88551df 100644 --- a/src/utility/time/TimeService.cpp +++ b/src/utility/time/TimeService.cpp @@ -8,9 +8,9 @@ file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -/************************************************************************************** +/****************************************************************************** * INCLUDE - **************************************************************************************/ + ******************************************************************************/ #include @@ -33,9 +33,9 @@ #include "RTCMillis.h" #endif -/************************************************************************************** +/****************************************************************************** * GLOBAL VARIABLES - **************************************************************************************/ + ******************************************************************************/ #if !defined(BOARD_HAS_HW_RTC) RTCMillis rtc; @@ -45,15 +45,15 @@ RTCMillis rtc; RTCZero rtc; #endif -/************************************************************************************** +/****************************************************************************** * INTERNAL FUNCTION DECLARATION - **************************************************************************************/ + ******************************************************************************/ time_t cvt_time(char const * time); -/************************************************************************************** +/****************************************************************************** * RTC PRIVATE FUNCTION DEFINITION - **************************************************************************************/ + ******************************************************************************/ #if defined(BOARD_HAS_HW_RTC) #if defined(ARDUINO_ARCH_SAMD) @@ -117,23 +117,23 @@ static inline unsigned long _getRTC() { } #endif -/************************************************************************************** +/****************************************************************************** * DEFINES - **************************************************************************************/ + ******************************************************************************/ #define EPOCH_AT_COMPILE_TIME cvt_time(__DATE__) -/************************************************************************************** +/****************************************************************************** * CONSTANTS - **************************************************************************************/ + ******************************************************************************/ /* Default NTP synch is scheduled each 24 hours from startup */ static time_t const TIMESERVICE_NTP_SYNC_TIMEOUT_ms = DAYS * 1000; static time_t const EPOCH = 0; -/************************************************************************************** +/****************************************************************************** * CTOR/DTOR - **************************************************************************************/ + ******************************************************************************/ TimeServiceClass::TimeServiceClass() : _con_hdl(nullptr) @@ -148,9 +148,9 @@ TimeServiceClass::TimeServiceClass() } -/************************************************************************************** +/****************************************************************************** * PUBLIC MEMBER FUNCTIONS - **************************************************************************************/ + ******************************************************************************/ void TimeServiceClass::begin(ConnectionHandler * con_hdl) { @@ -306,9 +306,9 @@ unsigned long TimeServiceClass::getTimeFromString(const String& input) return mktime(&t); } -/************************************************************************************** +/****************************************************************************** * PRIVATE MEMBER FUNCTIONS - **************************************************************************************/ + ******************************************************************************/ #if defined(HAS_NOTECARD) || defined(HAS_TCP) bool TimeServiceClass::connected() @@ -382,9 +382,9 @@ unsigned long TimeServiceClass::getRTC() return _getRTC(); } -/************************************************************************************** +/****************************************************************************** * INTERNAL FUNCTION DEFINITION - **************************************************************************************/ + ******************************************************************************/ time_t cvt_time(char const * time) { diff --git a/src/utility/time/TimeService.h b/src/utility/time/TimeService.h index 1f58bd781..d4a455cef 100644 --- a/src/utility/time/TimeService.h +++ b/src/utility/time/TimeService.h @@ -11,9 +11,9 @@ #ifndef ARDUINO_IOT_CLOUD_TIME_SERVICE_H_ #define ARDUINO_IOT_CLOUD_TIME_SERVICE_H_ -/************************************************************************************** +/****************************************************************************** * INCLUDE - **************************************************************************************/ + ******************************************************************************/ #include #include @@ -24,9 +24,9 @@ typedef unsigned long(*syncTimeFunctionPtr)(void); -/************************************************************************************** +/****************************************************************************** * CLASS DECLARATION - **************************************************************************************/ + ******************************************************************************/ class TimeServiceClass {