Skip to content

Commit 0531ad4

Browse files
committed
Merge pull request #926 from Sissors/lookmartinImadeanewbranch
Add option to modify RTC source for time functions (using func pointers)
2 parents 2313b82 + 4a184d9 commit 0531ad4

File tree

2 files changed

+48
-11
lines changed

2 files changed

+48
-11
lines changed

libraries/mbed/api/rtc_time.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ 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+
* Do not call this function from an interrupt while an RTC read/write operation may be occurring
75+
*
76+
* @param read_rtc pointer to function which returns current UNIX timestamp
77+
* @param write_rtc pointer to function which sets current UNIX timestamp, can be NULL
78+
* @param init_rtc pointer to funtion which initializes RTC, can be NULL
79+
* @param isenabled_rtc pointer to function wich returns if the rtc is enabled, can be NULL
80+
*/
81+
void attach_rtc(time_t (*read_rtc)(void), void (*write_rtc)(time_t), void (*init_rtc)(void), int (*isenabled_rtc)(void));
82+
7283
#ifdef __cplusplus
7384
}
7485
#endif

libraries/mbed/common/rtc_time.c

Lines changed: 37 additions & 11 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,16 @@ 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+
}
3548
}
36-
time_t t = rtc_read();
37-
38-
#else
49+
3950
time_t t = 0;
40-
#endif
51+
if (_rtc_read != NULL) {
52+
t = _rtc_read();
53+
}
4154

4255
if (timer != NULL) {
4356
*timer = t;
@@ -46,10 +59,12 @@ time_t time(time_t *timer)
4659
}
4760

4861
void set_time(time_t t) {
49-
#if DEVICE_RTC
50-
rtc_init();
51-
rtc_write(t);
52-
#endif
62+
if (_rtc_init != NULL) {
63+
_rtc_init();
64+
}
65+
if (_rtc_write != NULL) {
66+
_rtc_write(t);
67+
}
5368
}
5469

5570
clock_t clock() {
@@ -58,6 +73,17 @@ clock_t clock() {
5873
return t;
5974
}
6075

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

0 commit comments

Comments
 (0)