Skip to content

Commit 2844be6

Browse files
committed
[NUC472/M453] Fix RTC time doesn't continue across reset cycle
1 parent c4df35d commit 2844be6

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

targets/TARGET_NUVOTON/TARGET_M451/rtc_api.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,33 @@
2626

2727
#define YEAR0 1900
2828
//#define EPOCH_YR 1970
29-
static int rtc_inited = 0;
3029

3130
static const struct nu_modinit_s rtc_modinit = {RTC_0, RTC_MODULE, 0, 0, 0, RTC_IRQn, NULL};
3231

3332
void rtc_init(void)
3433
{
35-
if (rtc_inited) {
34+
if (rtc_isenabled()) {
3635
return;
3736
}
38-
rtc_inited = 1;
39-
40-
// Enable IP clock
41-
CLK_EnableModuleClock(rtc_modinit.clkidx);
4237

4338
RTC_Open(NULL);
4439
}
4540

4641
void rtc_free(void)
4742
{
48-
// FIXME
43+
// N/A
4944
}
5045

5146
int rtc_isenabled(void)
5247
{
53-
return rtc_inited;
48+
// NOTE: To access (RTC) registers, clock must be enabled first.
49+
if (! (CLK->APBCLK0 & CLK_APBCLK0_RTCCKEN_Msk)) {
50+
// Enable IP clock
51+
CLK_EnableModuleClock(rtc_modinit.clkidx);
52+
}
53+
54+
// NOTE: Check RTC Init Active flag to support crossing reset cycle.
55+
return !! (RTC->INIT & RTC_INIT_ACTIVE_Msk);
5456
}
5557

5658
/*
@@ -68,7 +70,9 @@ int rtc_isenabled(void)
6870

6971
time_t rtc_read(void)
7072
{
71-
if (! rtc_inited) {
73+
// NOTE: After boot, RTC time registers are not synced immediately, about 1 sec latency.
74+
// RTC time got (through RTC_GetDateAndTime()) in this sec would be last-synced and incorrect.
75+
if (! rtc_isenabled()) {
7276
rtc_init();
7377
}
7478

@@ -94,7 +98,7 @@ time_t rtc_read(void)
9498

9599
void rtc_write(time_t t)
96100
{
97-
if (! rtc_inited) {
101+
if (! rtc_isenabled()) {
98102
rtc_init();
99103
}
100104

targets/TARGET_NUVOTON/TARGET_NUC472/rtc_api.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,33 @@
2626

2727
#define YEAR0 1900
2828
//#define EPOCH_YR 1970
29-
static int rtc_inited = 0;
3029

3130
static const struct nu_modinit_s rtc_modinit = {RTC_0, RTC_MODULE, 0, 0, 0, RTC_IRQn, NULL};
3231

3332
void rtc_init(void)
3433
{
35-
if (rtc_inited) {
34+
if (rtc_isenabled()) {
3635
return;
3736
}
38-
rtc_inited = 1;
39-
40-
// Enable IP clock
41-
CLK_EnableModuleClock(rtc_modinit.clkidx);
4237

4338
RTC_Open(NULL);
4439
}
4540

4641
void rtc_free(void)
4742
{
48-
// FIXME
43+
// N/A
4944
}
5045

5146
int rtc_isenabled(void)
5247
{
53-
return rtc_inited;
48+
// NOTE: To access (RTC) registers, clock must be enabled first.
49+
if (! (CLK->APBCLK0 & CLK_APBCLK0_RTCCKEN_Msk)) {
50+
// Enable IP clock
51+
CLK_EnableModuleClock(rtc_modinit.clkidx);
52+
}
53+
54+
// NOTE: Check RTC Init Active flag to support crossing reset cycle.
55+
return !! (RTC->INIT & RTC_INIT_INIT_Active_Msk);
5456
}
5557

5658
/*
@@ -68,7 +70,9 @@ int rtc_isenabled(void)
6870

6971
time_t rtc_read(void)
7072
{
71-
if (! rtc_inited) {
73+
// NOTE: After boot, RTC time registers are not synced immediately, about 1 sec latency.
74+
// RTC time got (through RTC_GetDateAndTime()) in this sec would be last-synced and incorrect.
75+
if (! rtc_isenabled()) {
7276
rtc_init();
7377
}
7478

@@ -94,7 +98,7 @@ time_t rtc_read(void)
9498

9599
void rtc_write(time_t t)
96100
{
97-
if (! rtc_inited) {
101+
if (! rtc_isenabled()) {
98102
rtc_init();
99103
}
100104

0 commit comments

Comments
 (0)