Skip to content
This repository was archived by the owner on Jan 17, 2024. It is now read-only.

Commit b3ab0f9

Browse files
committed
Add private and arch specific functions to init/get/set RTC
1 parent 86520bf commit b3ab0f9

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed

src/utility/time/TimeService.cpp

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,36 @@ RTCZero rtc;
3939

4040
time_t cvt_time(char const * time);
4141

42+
#ifdef ARDUINO_ARCH_SAMD
43+
void samd_initRTC();
44+
void samd_setRTC(unsigned long time);
45+
unsigned long samd_getRTC();
46+
#endif
47+
48+
#ifdef ARDUINO_NANO_RP2040_CONNECT
49+
void rp2040_connect_initRTC();
50+
void rp2040_connect_setRTC(unsigned long time);
51+
unsigned long rp2040_connect_getRTC();
52+
#endif
53+
54+
#ifdef BOARD_STM32H7
55+
void stm32h7_initRTC();
56+
void stm32h7_setRTC(unsigned long time);
57+
unsigned long stm32h7_getRTC();
58+
#endif
59+
60+
#ifdef ARDUINO_ARCH_ESP32
61+
void esp32_initRTC();
62+
void esp32_setRTC(unsigned long time);
63+
unsigned long esp32_getRTC();
64+
#endif
65+
66+
#ifdef ARDUINO_ARCH_ESP8266
67+
void esp8266_initRTC();
68+
void esp8266_setRTC(unsigned long time);
69+
unsigned long esp8266_getRTC();
70+
#endif
71+
4272
/**************************************************************************************
4373
* CONSTANTS
4474
**************************************************************************************/
@@ -277,6 +307,57 @@ bool TimeService::isTimeValid(unsigned long const time)
277307
return (time >= EPOCH_AT_COMPILE_TIME);
278308
}
279309

310+
void TimeService::initRTC()
311+
{
312+
#if defined (ARDUINO_ARCH_SAMD)
313+
samd_initRTC();
314+
#elif defined (ARDUINO_NANO_RP2040_CONNECT)
315+
rp2040_connect_initRTC();
316+
#elif defined (BOARD_STM32H7)
317+
stm32h7_initRTC();
318+
#elif defined (ARDUINO_ARCH_ESP32)
319+
esp32_initRTC();
320+
#elif ARDUINO_ARCH_ESP8266
321+
esp8266_initRTC();
322+
#else
323+
#error "RTC not available for this architecture"
324+
#endif
325+
}
326+
327+
void TimeService::setRTC(unsigned long time)
328+
{
329+
#if defined (ARDUINO_ARCH_SAMD)
330+
samd_setRTC(time);
331+
#elif defined (ARDUINO_NANO_RP2040_CONNECT)
332+
rp2040_connect_setRTC(time);
333+
#elif defined (BOARD_STM32H7)
334+
stm32h7_setRTC(time);
335+
#elif defined (ARDUINO_ARCH_ESP32)
336+
esp32_setRTC(time);
337+
#elif ARDUINO_ARCH_ESP8266
338+
esp8266_setRTC(time);
339+
#else
340+
#error "RTC not available for this architecture"
341+
#endif
342+
}
343+
344+
unsigned long TimeService::getRTC()
345+
{
346+
#if defined (ARDUINO_ARCH_SAMD)
347+
return samd_getRTC();
348+
#elif defined (ARDUINO_NANO_RP2040_CONNECT)
349+
return rp2040_connect_getRTC();
350+
#elif defined (BOARD_STM32H7)
351+
return stm32h7_getRTC();
352+
#elif defined (ARDUINO_ARCH_ESP32)
353+
return esp32_getRTC();
354+
#elif ARDUINO_ARCH_ESP8266
355+
return esp8266_getRTC();
356+
#else
357+
#error "RTC not available for this architecture"
358+
#endif
359+
}
360+
280361
/**************************************************************************************
281362
* INTERNAL FUNCTION DEFINITION
282363
**************************************************************************************/
@@ -311,6 +392,92 @@ time_t cvt_time(char const * time)
311392
return mktime(&t);
312393
}
313394

395+
#ifdef ARDUINO_ARCH_SAMD
396+
void samd_initRTC()
397+
{
398+
rtc.begin();
399+
}
400+
401+
void samd_setRTC(unsigned long time)
402+
{
403+
rtc.setEpoch(time);
404+
}
405+
406+
unsigned long samd_getRTC()
407+
{
408+
return rtc.getEpoch();
409+
}
410+
#endif
411+
412+
#ifdef ARDUINO_NANO_RP2040_CONNECT
413+
void rp2040_connect_initRTC()
414+
{
415+
/* Nothing to do */
416+
}
417+
418+
void rp2040_connect_setRTC(unsigned long time)
419+
{
420+
set_time(time);
421+
}
422+
423+
unsigned long rp2040_connect_getRTC()
424+
{
425+
return time(NULL);
426+
}
427+
#endif
428+
429+
#ifdef BOARD_STM32H7
430+
void stm32h7_initRTC()
431+
{
432+
/* Nothing to do */
433+
}
434+
435+
void stm32h7_setRTC(unsigned long time)
436+
{
437+
set_time(time);
438+
}
439+
440+
unsigned long stm32h7_getRTC()
441+
{
442+
return time(NULL);
443+
}
444+
#endif
445+
446+
#ifdef ARDUINO_ARCH_ESP32
447+
void esp32_initRTC()
448+
{
449+
//configTime(0, 0, "time.arduino.cc", "pool.ntp.org", "time.nist.gov");
450+
}
451+
452+
void esp32_setRTC(unsigned long time)
453+
{
454+
const timeval epoch = {(time_t)time, 0};
455+
settimeofday(&epoch, 0);
456+
}
457+
458+
unsigned long esp32_getRTC()
459+
{
460+
return time(NULL);
461+
}
462+
#endif
463+
464+
#ifdef ARDUINO_ARCH_ESP8266
465+
void esp8266_initRTC()
466+
{
467+
/* Nothing to do */
468+
}
469+
470+
void esp8266_setRTC(unsigned long time)
471+
{
472+
/* TODO */
473+
}
474+
475+
unsigned long esp8266_getRTC()
476+
{
477+
/* TODO */
478+
}
479+
#endif
480+
314481
TimeService & ArduinoIoTCloudTimeService() {
315482
static TimeService _timeService_instance;
316483
return _timeService_instance;

src/utility/time/TimeService.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ class TimeService
6969
unsigned long getRemoteTime();
7070
bool connected();
7171
static bool isTimeValid(unsigned long const time);
72+
void initRTC();
73+
void setRTC(unsigned long time);
74+
unsigned long getRTC();
7275

7376
};
7477

0 commit comments

Comments
 (0)