Skip to content

Commit ac92764

Browse files
committed
Add low power timer fallback for platforms without RTC
Low power timer will be used as RTC for platforms that don't have HW RTC capabilities.
1 parent 2104d8a commit ac92764

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

platform/mbed_rtc_time.cpp

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,53 @@
2323
static SingletonPtr<PlatformMutex> _mutex;
2424

2525
#if DEVICE_RTC
26+
2627
static void (*_rtc_init)(void) = rtc_init;
2728
static int (*_rtc_isenabled)(void) = rtc_isenabled;
2829
static time_t (*_rtc_read)(void) = rtc_read;
2930
static void (*_rtc_write)(time_t t) = rtc_write;
30-
#else
31+
32+
#elif DEVICE_LOWPOWERTIMER
33+
34+
#include "drivers/LowPowerTimer.h"
35+
36+
static SingletonPtr<mbed::LowPowerTimer> _rtc_lp_timer;
37+
static uint64_t _rtc_lp_base;
38+
static bool _rtc_enabled;
39+
40+
static void _rtc_lpticker_init(void)
41+
{
42+
_rtc_lp_timer->start();
43+
_rtc_enabled = true;
44+
}
45+
46+
static int _rtc_lpticker_isenabled(void)
47+
{
48+
return (_rtc_enabled == true);
49+
}
50+
51+
static time_t _rtc_lpticker_read(void)
52+
{
53+
return (uint64_t)_rtc_lp_timer->read() + _rtc_lp_base;
54+
}
55+
56+
static void _rtc_lpticker_write(time_t t)
57+
{
58+
_rtc_lp_base = t;
59+
}
60+
61+
static void (*_rtc_init)(void) = _rtc_lpticker_init;
62+
static int (*_rtc_isenabled)(void) = _rtc_lpticker_isenabled;
63+
static time_t (*_rtc_read)(void) = _rtc_lpticker_read;
64+
static void (*_rtc_write)(time_t t) = _rtc_lpticker_write;
65+
66+
#else /* DEVICE_LOWPOWERTIMER */
67+
3168
static void (*_rtc_init)(void) = NULL;
3269
static int (*_rtc_isenabled)(void) = NULL;
3370
static time_t (*_rtc_read)(void) = NULL;
3471
static void (*_rtc_write)(time_t t) = NULL;
35-
#endif
72+
#endif /* DEVICE_LOWPOWERTIMER */
3673

3774
#ifdef __cplusplus
3875
extern "C" {

0 commit comments

Comments
 (0)