Skip to content

Commit 86aa16b

Browse files
T Sdpgeorge
authored andcommitted
stmhal: Implement delayed RTC initialization with LSI fallback.
If RTC is already running at boot then it's left alone. Otherwise, RTC is started at boot but startup function returns straight away. RTC startup is then finished the first time it is used. Fallback to LSI if LSE fails to start in a certain time. Also included: MICROPY_HW_CLK_LAST_FREQ hold pyb.freq() parameters in RTC backup reg MICROPY_HW_RTC_USE_US option to present datetime sub-seconds in microseconds MICROPY_HW_RTC_USE_CALOUT option to enable RTC calibration output CLK_LAST_FREQ and RTC_USE_CALOUT are enabled for PYBv1.0.
1 parent 4dea24e commit 86aa16b

File tree

8 files changed

+358
-168
lines changed

8 files changed

+358
-168
lines changed

stmhal/boards/PYBV10/mpconfigboard.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@
2222
#define MICROPY_HW_CLK_PLLN (336)
2323
#define MICROPY_HW_CLK_PLLP (RCC_PLLP_DIV2)
2424
#define MICROPY_HW_CLK_PLLQ (7)
25+
#define MICROPY_HW_CLK_LAST_FREQ (1)
2526

2627
// The pyboard has a 32kHz crystal for the RTC
2728
#define MICROPY_HW_RTC_USE_LSE (1)
29+
#define MICROPY_HW_RTC_USE_US (0)
30+
#define MICROPY_HW_RTC_USE_CALOUT (1)
2831

2932
// UART config
3033
#define MICROPY_HW_UART1_NAME "XB"

stmhal/diskio.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ DWORD get_fattime (
279279
void
280280
)
281281
{
282+
rtc_init_finalise();
282283
RTC_TimeTypeDef time;
283284
RTC_DateTypeDef date;
284285
HAL_RTC_GetTime(&RTCHandle, &time, FORMAT_BIN);

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();
425+
rtc_init_start();
426426
}
427427
#endif
428428

stmhal/modmachine.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "pin.h"
4040
#include "timer.h"
4141
#include "usb.h"
42+
#include "rtc.h"
4243
#include "i2c.h"
4344
#include "spi.h"
4445

@@ -281,6 +282,9 @@ STATIC mp_obj_t machine_freq(mp_uint_t n_args, const mp_obj_t *args) {
281282
} else {
282283
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
283284
}
285+
uint32_t h = RCC_ClkInitStruct.AHBCLKDivider >> 4;
286+
uint32_t b1 = RCC_ClkInitStruct.APB1CLKDivider >> 10;
287+
uint32_t b2 = RCC_ClkInitStruct.APB2CLKDivider >> 10;
284288
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) {
285289
goto fail;
286290
}
@@ -312,6 +316,23 @@ STATIC mp_obj_t machine_freq(mp_uint_t n_args, const mp_obj_t *args) {
312316
// re-init TIM3 for USB CDC rate
313317
timer_tim3_init();
314318

319+
#if defined(MICROPY_HW_CLK_LAST_FREQ) && MICROPY_HW_CLK_LAST_FREQ
320+
#if defined(MCU_SERIES_F7)
321+
#define FREQ_BKP BKP31R
322+
#else
323+
#define FREQ_BKP BKP19R
324+
#endif
325+
// qqqqqqqq pppppppp nnnnnnnn nnmmmmmm
326+
// qqqqQQQQ ppppppPP nNNNNNNN NNMMMMMM
327+
// 222111HH HHQQQQPP nNNNNNNN NNMMMMMM
328+
p = (p / 2) - 1;
329+
RTC->FREQ_BKP = m
330+
| (n << 6) | (p << 16) | (q << 18)
331+
| (h << 22)
332+
| (b1 << 26)
333+
| (b2 << 29);
334+
#endif
335+
315336
return mp_const_none;
316337

317338
fail:;
@@ -349,6 +370,8 @@ STATIC mp_obj_t machine_sleep(void) {
349370
MP_DEFINE_CONST_FUN_OBJ_0(machine_sleep_obj, machine_sleep);
350371

351372
STATIC mp_obj_t machine_deepsleep(void) {
373+
rtc_init_finalise();
374+
352375
#if defined(MCU_SERIES_F7)
353376
printf("machine.deepsleep not supported yet\n");
354377
#else

stmhal/modutime.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ STATIC mp_obj_t time_localtime(mp_uint_t n_args, const mp_obj_t *args) {
5757
if (n_args == 0 || args[0] == mp_const_none) {
5858
// get current date and time
5959
// note: need to call get time then get date to correctly access the registers
60+
rtc_init_finalise();
6061
RTC_DateTypeDef date;
6162
RTC_TimeTypeDef time;
6263
HAL_RTC_GetTime(&RTCHandle, &time, FORMAT_BIN);
@@ -119,6 +120,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(time_mktime_obj, time_mktime);
119120
STATIC mp_obj_t time_time(void) {
120121
// get date and time
121122
// note: need to call get time then get date to correctly access the registers
123+
rtc_init_finalise();
122124
RTC_DateTypeDef date;
123125
RTC_TimeTypeDef time;
124126
HAL_RTC_GetTime(&RTCHandle, &time, FORMAT_BIN);

0 commit comments

Comments
 (0)