Skip to content

Commit f7697ff

Browse files
committed
stmhal: Add rtc.init() method to force RTC to re-initialise.
1 parent f4c1737 commit f7697ff

3 files changed

Lines changed: 35 additions & 22 deletions

File tree

stmhal/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ int main(void) {
422422

423423
#if MICROPY_HW_ENABLE_RTC
424424
if (first_soft_reset) {
425-
rtc_init_start();
425+
rtc_init_start(false);
426426
}
427427
#endif
428428

stmhal/rtc.c

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ STATIC bool lse_magic(void) {
105105
#endif
106106
}
107107

108-
void rtc_init_start(void) {
108+
void rtc_init_start(bool force_init) {
109109
RTCHandle.Instance = RTC;
110110

111111
/* Configure RTC prescaler and RTC data registers */
@@ -124,26 +124,30 @@ void rtc_init_start(void) {
124124
RTCHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
125125

126126
rtc_need_init_finalise = false;
127-
if ((RCC->BDCR & (RCC_BDCR_LSEON | RCC_BDCR_LSERDY)) == (RCC_BDCR_LSEON | RCC_BDCR_LSERDY)) {
128-
// LSE is enabled & ready --> no need to (re-)init RTC
129-
// remove Backup Domain write protection
130-
HAL_PWR_EnableBkUpAccess();
131-
// Clear source Reset Flag
132-
__HAL_RCC_CLEAR_RESET_FLAGS();
133-
// provide some status information
134-
rtc_info |= 0x40000 | (RCC->BDCR & 7) | (RCC->CSR & 3) << 8;
135-
return;
136-
} else if (((RCC->BDCR & RCC_BDCR_RTCSEL) == RCC_BDCR_RTCSEL_1) && ((RCC->CSR & 3) == 3)) {
137-
// LSI configured & enabled & ready --> no need to (re-)init RTC
138-
// remove Backup Domain write protection
139-
HAL_PWR_EnableBkUpAccess();
140-
// Clear source Reset Flag
141-
__HAL_RCC_CLEAR_RESET_FLAGS();
142-
RCC->CSR |= 1;
143-
// provide some status information
144-
rtc_info |= 0x80000 | (RCC->BDCR & 7) | (RCC->CSR & 3) << 8;
145-
return;
127+
128+
if (!force_init) {
129+
if ((RCC->BDCR & (RCC_BDCR_LSEON | RCC_BDCR_LSERDY)) == (RCC_BDCR_LSEON | RCC_BDCR_LSERDY)) {
130+
// LSE is enabled & ready --> no need to (re-)init RTC
131+
// remove Backup Domain write protection
132+
HAL_PWR_EnableBkUpAccess();
133+
// Clear source Reset Flag
134+
__HAL_RCC_CLEAR_RESET_FLAGS();
135+
// provide some status information
136+
rtc_info |= 0x40000 | (RCC->BDCR & 7) | (RCC->CSR & 3) << 8;
137+
return;
138+
} else if (((RCC->BDCR & RCC_BDCR_RTCSEL) == RCC_BDCR_RTCSEL_1) && ((RCC->CSR & 3) == 3)) {
139+
// LSI configured & enabled & ready --> no need to (re-)init RTC
140+
// remove Backup Domain write protection
141+
HAL_PWR_EnableBkUpAccess();
142+
// Clear source Reset Flag
143+
__HAL_RCC_CLEAR_RESET_FLAGS();
144+
RCC->CSR |= 1;
145+
// provide some status information
146+
rtc_info |= 0x80000 | (RCC->BDCR & 7) | (RCC->CSR & 3) << 8;
147+
return;
148+
}
146149
}
150+
147151
rtc_startup_tick = HAL_GetTick();
148152
rtc_info = 0x3f000000 | (rtc_startup_tick & 0xffffff);
149153
if (rtc_use_lse) {
@@ -422,6 +426,14 @@ STATIC mp_obj_t pyb_rtc_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n
422426
return (mp_obj_t)&pyb_rtc_obj;
423427
}
424428

429+
// force rtc to re-initialise
430+
mp_obj_t pyb_rtc_init(mp_obj_t self_in) {
431+
rtc_init_start(true);
432+
rtc_init_finalise();
433+
return mp_const_none;
434+
}
435+
MP_DEFINE_CONST_FUN_OBJ_1(pyb_rtc_init_obj, pyb_rtc_init);
436+
425437
/// \method info()
426438
/// Get information about the startup time and reset source.
427439
///
@@ -687,6 +699,7 @@ mp_obj_t pyb_rtc_calibration(mp_uint_t n_args, const mp_obj_t *args) {
687699
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_rtc_calibration_obj, 1, 2, pyb_rtc_calibration);
688700

689701
STATIC const mp_map_elem_t pyb_rtc_locals_dict_table[] = {
702+
{ MP_OBJ_NEW_QSTR(MP_QSTR_init), (mp_obj_t)&pyb_rtc_init_obj },
690703
{ MP_OBJ_NEW_QSTR(MP_QSTR_info), (mp_obj_t)&pyb_rtc_info_obj },
691704
{ MP_OBJ_NEW_QSTR(MP_QSTR_datetime), (mp_obj_t)&pyb_rtc_datetime_obj },
692705
{ MP_OBJ_NEW_QSTR(MP_QSTR_wakeup), (mp_obj_t)&pyb_rtc_wakeup_obj },

stmhal/rtc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@
2727
extern RTC_HandleTypeDef RTCHandle;
2828
extern const mp_obj_type_t pyb_rtc_type;
2929

30-
void rtc_init_start(void);
30+
void rtc_init_start(bool force_init);
3131
void rtc_init_finalise(void);

0 commit comments

Comments
 (0)