Skip to content

Commit c5c7f3c

Browse files
committed
[NANO130] Fix RTC incorrect time issue after system reset
1 parent e1af465 commit c5c7f3c

File tree

1 file changed

+14
-10
lines changed
  • targets/TARGET_NUVOTON/TARGET_NANO100

1 file changed

+14
-10
lines changed

targets/TARGET_NUVOTON/TARGET_NANO100/rtc_api.c

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

2626
#define YEAR0 1900
2727

28-
static int rtc_inited = 0;
2928

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

3231
void rtc_init(void)
3332
{
34-
if (rtc_inited) {
33+
if (rtc_isenabled()) {
3534
return;
3635
}
37-
rtc_inited = 1;
38-
39-
// Enable IP clock
40-
CLK_EnableModuleClock(rtc_modinit.clkidx);
4136

4237
RTC_Open(NULL);
4338
}
4439

4540
void rtc_free(void)
4641
{
47-
// FIXME
42+
// N/A
4843
}
4944

5045
int rtc_isenabled(void)
5146
{
52-
return rtc_inited;
47+
// NOTE: To access (RTC) registers, clock must be enabled first.
48+
if (! (CLK->APBCLK & CLK_APBCLK_RTC_EN_Msk)) {
49+
// Enable IP clock
50+
CLK_EnableModuleClock(rtc_modinit.clkidx);
51+
}
52+
53+
// NOTE: Check RTC Init Active flag to support crossing reset cycle.
54+
return !! (RTC->INIR & RTC_INIR_ACTIVE_Msk);
5355
}
5456

5557
/*
@@ -67,7 +69,9 @@ int rtc_isenabled(void)
6769

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

@@ -93,7 +97,7 @@ time_t rtc_read(void)
9397

9498
void rtc_write(time_t t)
9599
{
96-
if (! rtc_inited) {
100+
if (! rtc_isenabled()) {
97101
rtc_init();
98102
}
99103

0 commit comments

Comments
 (0)