Skip to content

Commit f0d04f5

Browse files
committed
[API] Added option to modify RTC source
Initially from Karls question here: http://developer.mbed.org/questions/6337/setTime-and-time-exported-with-__weak-fo/#answer6545?compage=1#c15936. Currently the C time functions only work with internal RTC. Sometimes you either want to use an external one, or your target does not even have an internal RTC. I added function attach_rtc(...) where the default functions can be overridden at runtime.
1 parent ac0504d commit f0d04f5

File tree

2 files changed

+47
-12
lines changed

2 files changed

+47
-12
lines changed

libraries/mbed/api/rtc_time.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ extern "C" {
6969
*/
7070
void set_time(time_t t);
7171

72+
/** Attach an external RTC to be used for the C time functions
73+
*
74+
* @param read_rtc pointer to function which returns current UNIX timestamp
75+
* @param write_rtc pointer to function which sets current UNIX timestamp, can be NULL
76+
* @param init_rtc pointer to funtion which initializes RTC, can be NULL
77+
* @param isenabled_rtc pointer to function wich returns if the rtc is enabled, can be NULL
78+
*/
79+
void attach_rtc(time_t (*read_rtc)(void), void (*write_rtc)(time_t), void (*init_rtc)(void), int (*isenabled_rtc)(void));
80+
7281
#ifdef __cplusplus
7382
}
7483
#endif

libraries/mbed/common/rtc_time.c

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@
1919
#include "rtc_time.h"
2020
#include "us_ticker_api.h"
2121

22+
#if DEVICE_RTC
23+
static void (*_rtc_init)(void) = rtc_init;
24+
static int (*_rtc_isenabled)(void) = rtc_isenabled;
25+
static time_t (*_rtc_read)(void) = rtc_read;
26+
static void (*_rtc_write)(time_t t) = rtc_write;
27+
#else
28+
static void (*_rtc_init)(void) = NULL;
29+
static int (*_rtc_isenabled)(void) = NULL;
30+
static time_t (*_rtc_read)(void) = NULL;
31+
static void (*_rtc_write)(time_t t) = NULL;
32+
#endif
33+
2234
#ifdef __cplusplus
2335
extern "C" {
2436
#endif
@@ -29,15 +41,18 @@ time_t time(time_t *timer)
2941
#endif
3042

3143
{
32-
#if DEVICE_RTC
33-
if (!(rtc_isenabled())) {
34-
set_time(0);
44+
if (_rtc_isenabled != NULL) {
45+
if (!(_rtc_isenabled())) {
46+
set_time(0);
47+
}
48+
}
49+
50+
time_t t;
51+
if (_rtc_read != NULL) {
52+
t = _rtc_read();
53+
} else {
54+
t = 0;
3555
}
36-
time_t t = rtc_read();
37-
38-
#else
39-
time_t t = 0;
40-
#endif
4156

4257
if (timer != NULL) {
4358
*timer = t;
@@ -46,10 +61,12 @@ time_t time(time_t *timer)
4661
}
4762

4863
void set_time(time_t t) {
49-
#if DEVICE_RTC
50-
rtc_init();
51-
rtc_write(t);
52-
#endif
64+
if (_rtc_init != NULL) {
65+
_rtc_init();
66+
}
67+
if (_rtc_write != NULL) {
68+
_rtc_write(t);
69+
}
5370
}
5471

5572
clock_t clock() {
@@ -58,6 +75,15 @@ clock_t clock() {
5875
return t;
5976
}
6077

78+
void attach_rtc(time_t (*read_rtc)(void), void (*write_rtc)(time_t), void (*init_rtc)(void), int (*isenabled_rtc)(void)) {
79+
_rtc_read = read_rtc;
80+
_rtc_write = write_rtc;
81+
_rtc_init = init_rtc;
82+
_rtc_isenabled = isenabled_rtc;
83+
}
84+
85+
86+
6187
#ifdef __cplusplus
6288
}
6389
#endif

0 commit comments

Comments
 (0)