cpu/stm32f1: make RTC Y2038 safe

Use rtc_* functions with custom EPOCH.
This buys us about 118 years.
This commit is contained in:
Benjamin Valentin 2020-02-04 16:24:30 +01:00 committed by Benjamin Valentin
parent 6dd69e7134
commit 33d03e7b3f

View File

@ -115,12 +115,12 @@ static void _rtc_config(void)
NVIC_EnableIRQ(RTC_Alarm_IRQn);
}
static time_t _rtc_get_time(void)
static uint32_t _rtc_get_time(void)
{
return (RTC->CNTH << 16) | RTC->CNTL;
}
static void _rtc_set_time(time_t counter_val)
static void _rtc_set_time(uint32_t counter_val)
{
_rtc_enter_config_mode();
RTC->CNTH = (counter_val & 0xffff0000) >> 16;
@ -132,7 +132,7 @@ void rtc_init(void)
{
/* save current time if RTC already works */
bool is_rtc_enable = _is_rtc_enable();
time_t cur_time = 0;
uint32_t cur_time = 0;
if (is_rtc_enable) {
cur_time = _rtc_get_time();
}
@ -148,26 +148,21 @@ void rtc_init(void)
int rtc_set_time(struct tm *time)
{
time_t timestamp = mktime(time);
if (timestamp == -1) {
return -1;
}
uint32_t timestamp = rtc_mktime(time);
_rtc_set_time(timestamp);
DEBUG("%s timestamp=%"PRIu32"\n", __func__, (uint32_t)timestamp);
DEBUG("%s timestamp=%"PRIu32"\n", __func__, timestamp);
return 0;
}
int rtc_get_time(struct tm *time)
{
time_t timestamp = _rtc_get_time();
localtime_r(&timestamp, time);
DEBUG("%s timestamp=%"PRIu32"\n", __func__, (uint32_t)timestamp);
uint32_t timestamp = _rtc_get_time();
rtc_localtime(timestamp, time);
DEBUG("%s timestamp=%"PRIu32"\n", __func__, timestamp);
return 0;
}
@ -188,12 +183,12 @@ static void _rtc_disable_alarm(void)
_rtc_exit_config_mode();
}
static time_t _rtc_get_alarm_time(void)
static uint32_t _rtc_get_alarm_time(void)
{
return (RTC->ALRH << 16) | RTC->ALRL;
}
static void _rtc_set_alarm_time(time_t alarm_time)
static void _rtc_set_alarm_time(uint32_t alarm_time)
{
_rtc_enter_config_mode();
RTC->ALRL = (alarm_time & 0x0000ffff);
@ -203,11 +198,7 @@ static void _rtc_set_alarm_time(time_t alarm_time)
int rtc_set_alarm(struct tm *time, rtc_alarm_cb_t cb, void *arg)
{
time_t timestamp = mktime(time);
if (timestamp == -1) {
return -2;
}
uint32_t timestamp = rtc_mktime(time);
/* disable existing alarm (if enabled) */
rtc_clear_alarm();
@ -222,17 +213,17 @@ int rtc_set_alarm(struct tm *time, rtc_alarm_cb_t cb, void *arg)
/* enable Alarm */
_rtc_enable_alarm();
DEBUG("%s timestamp=%"PRIu32"\n", __func__, (uint32_t)timestamp);
DEBUG("%s timestamp=%"PRIu32"\n", __func__, timestamp);
return 0;
}
int rtc_get_alarm(struct tm *time)
{
time_t timestamp = _rtc_get_alarm_time();
localtime_r(&timestamp, time);
uint32_t timestamp = _rtc_get_alarm_time();
rtc_localtime(timestamp, time);
DEBUG("%s timestamp=%"PRIu32"\n", __func__, (uint32_t)timestamp);
DEBUG("%s timestamp=%"PRIu32"\n", __func__, timestamp);
return 0;
}