diff --git a/targets/TARGET_Silicon_Labs/TARGET_EFM32/rtcc.c b/targets/TARGET_Silicon_Labs/TARGET_EFM32/rtcc.c index 5efdc57b73f..108802182a3 100644 --- a/targets/TARGET_Silicon_Labs/TARGET_EFM32/rtcc.c +++ b/targets/TARGET_Silicon_Labs/TARGET_EFM32/rtcc.c @@ -51,12 +51,25 @@ void rtc_init(void) RMU_ResetControl(rmuResetPin, rmuResetModeFull); /* Set up the RTCC and let it run, Forrest, run */ + + /* Save time if it has been set */ + time_t t = 0; + if (RTCC->RET[1].REG == 0) { + t = rtc_read(); + } + RTCC_Reset(); RTCC_Init_TypeDef rtcc_init = RTCC_INIT_DEFAULT; rtcc_init.presc = rtccCntPresc_32768; RTCC_Init(&rtcc_init); RTCC_Enable(true); - RTCC->RET[0].REG = 0; + + /* Update time */ + if (RTCC->RET[1].REG == 0) { + rtc_write(t); + } else { + RTCC->RET[0].REG = 0; + } } void rtc_free(void) @@ -79,6 +92,9 @@ void rtc_write(time_t t) { core_util_critical_section_enter(); RTCC->RET[0].REG = t - RTCC_CounterGet(); + + /* Record that the time has been set */ + RTCC->RET[1].REG = 0; core_util_critical_section_exit(); }