Skip to content

Commit 49fe6dc

Browse files
committed
stmhal: Add config option to use LSE/LSI for RTC.
Most boards (except the pyboard) don't have a 32kHz crystal so they should use the LSI for the RTC.
1 parent 3cb7663 commit 49fe6dc

4 files changed

Lines changed: 21 additions & 1 deletion

File tree

stmhal/boards/PYBV10/mpconfigboard.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
#define MICROPY_HW_ENABLE_SPI3 (0)
2121
#define MICROPY_HW_ENABLE_CAN (1)
2222

23+
// The pyboard has a 32kHz crystal for the RTC
24+
#define MICROPY_HW_RTC_USE_LSE (1)
25+
2326
// USRSW has no pullup or pulldown, and pressing the switch makes the input go low
2427
#define MICROPY_HW_USRSW_PIN (pin_B3)
2528
#define MICROPY_HW_USRSW_PULL (GPIO_PULLUP)

stmhal/boards/PYBV3/mpconfigboard.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
#define MICROPY_HW_ENABLE_SPI3 (0)
2020
#define MICROPY_HW_ENABLE_CAN (1)
2121

22+
// The pyboard has a 32kHz crystal for the RTC
23+
#define MICROPY_HW_RTC_USE_LSE (1)
24+
2225
// USRSW has no pullup or pulldown, and pressing the switch makes the input go low
2326
#define MICROPY_HW_USRSW_PIN (pin_A13)
2427
#define MICROPY_HW_USRSW_PULL (GPIO_PULLUP)

stmhal/boards/PYBV4/mpconfigboard.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
#define MICROPY_HW_ENABLE_SPI3 (0)
2020
#define MICROPY_HW_ENABLE_CAN (1)
2121

22+
// The pyboard has a 32kHz crystal for the RTC
23+
#define MICROPY_HW_RTC_USE_LSE (1)
24+
2225
// USRSW has no pullup or pulldown, and pressing the switch makes the input go low
2326
#define MICROPY_HW_USRSW_PIN (pin_B3)
2427
#define MICROPY_HW_USRSW_PULL (GPIO_PULLUP)

stmhal/rtc.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,18 +256,29 @@ void HAL_RTC_MspInit(RTC_HandleTypeDef *hrtc) {
256256
__HAL_RCC_BACKUPRESET_RELEASE().
257257
- Configure the needed RTc clock source */
258258

259-
// set LSE as RTC clock source
259+
// RTC clock source uses LSE (external crystal) only if relevant
260+
// configuration variable is set. Otherwise it uses LSI (internal osc).
261+
260262
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE;
261263
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
264+
#if defined(MICROPY_HW_RTC_USE_LSE) && MICROPY_HW_RTC_USE_LSE
262265
RCC_OscInitStruct.LSEState = RCC_LSE_ON;
263266
RCC_OscInitStruct.LSIState = RCC_LSI_OFF;
267+
#else
268+
RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
269+
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
270+
#endif
264271
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
265272
//Error_Handler();
266273
return;
267274
}
268275

269276
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
277+
#if defined(MICROPY_HW_RTC_USE_LSE) && MICROPY_HW_RTC_USE_LSE
270278
PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
279+
#else
280+
PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
281+
#endif
271282
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
272283
//Error_Handler();
273284
return;

0 commit comments

Comments
 (0)