Skip to content

Commit f00ce59

Browse files
authored
Merge pull request #14243 from boraozgen/feature/stm32-rtc-hse
STM32: Add HSE support to RTC
2 parents 83adad9 + 257e466 commit f00ce59

File tree

4 files changed

+48
-5
lines changed

4 files changed

+48
-5
lines changed

targets/TARGET_STM/mbed_overrides.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "cmsis.h"
1717
#include "objects.h"
1818
#include "platform/mbed_error.h"
19+
#include "rtc_api_hal.h"
1920

2021
int mbed_sdk_inited = 0;
2122
extern void SetSysClock(void);
@@ -285,7 +286,15 @@ void mbed_sdk_init()
285286

286287
/* Start LSI clock for RTC */
287288
#if DEVICE_RTC
288-
#if !MBED_CONF_TARGET_LSE_AVAILABLE
289+
#if (MBED_CONF_TARGET_RTC_CLOCK_SOURCE == USE_RTC_CLK_HSE)
290+
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
291+
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
292+
PeriphClkInitStruct.RTCClockSelection = (RCC_RTCCLKSOURCE_HSE_DIVX | RTC_HSE_DIV << 16);
293+
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
294+
{
295+
error("PeriphClkInitStruct RTC failed with HSE\n");
296+
}
297+
#elif ((MBED_CONF_TARGET_RTC_CLOCK_SOURCE == USE_RTC_CLK_LSE_OR_LSI) && !MBED_CONF_TARGET_LSE_AVAILABLE) || (MBED_CONF_TARGET_RTC_CLOCK_SOURCE == USE_RTC_CLK_LSI)
289298
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
290299

291300
if (__HAL_RCC_GET_RTC_SOURCE() != RCC_RTCCLKSOURCE_NO_CLK) {

targets/TARGET_STM/rtc_api.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,14 @@ void rtc_init(void)
6262
while (LL_HSEM_1StepLock(HSEM, CFG_HW_RCC_SEMID)) {
6363
}
6464
#endif /* DUAL_CORE */
65-
#if MBED_CONF_TARGET_LSE_AVAILABLE
65+
#if (MBED_CONF_TARGET_RTC_CLOCK_SOURCE == USE_RTC_CLK_HSE)
66+
(void)RCC_OscInitStruct;
67+
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
68+
PeriphClkInitStruct.RTCClockSelection = (RCC_RTCCLKSOURCE_HSE_DIVX | RTC_HSE_DIV << 16);
69+
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
70+
error("PeriphClkInitStruct RTC failed with HSE\n");
71+
}
72+
#elif (MBED_CONF_TARGET_RTC_CLOCK_SOURCE == USE_RTC_CLK_LSE_OR_LSI) && MBED_CONF_TARGET_LSE_AVAILABLE
6673
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
6774
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
6875
#if MBED_CONF_TARGET_LSE_BYPASS
@@ -82,7 +89,7 @@ void rtc_init(void)
8289
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
8390
error("PeriphClkInitStruct RTC failed with LSE\n");
8491
}
85-
#else /* MBED_CONF_TARGET_LSE_AVAILABLE */
92+
#else /* Fallback to LSI */
8693
#if TARGET_STM32WB
8794
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI1;
8895
#else
@@ -101,7 +108,7 @@ void rtc_init(void)
101108
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
102109
error("PeriphClkInitStruct RTC failed with LSI\n");
103110
}
104-
#endif /* MBED_CONF_TARGET_LSE_AVAILABLE */
111+
#endif /* MBED_CONF_TARGET_RTC_CLOCK_SOURCE */
105112
#if defined(DUAL_CORE) && (TARGET_STM32H7)
106113
LL_HSEM_ReleaseLock(HSEM, CFG_HW_RCC_SEMID, HSEM_CR_COREID_CURRENT);
107114
#endif /* DUAL_CORE */

targets/TARGET_STM/rtc_api_hal.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,26 @@
3838
extern "C" {
3939
#endif
4040

41-
#if MBED_CONF_TARGET_LSE_AVAILABLE
41+
// Possible choices of the RTC_CLOCK_SOURCE configuration set in json file
42+
#define USE_RTC_CLK_LSE_OR_LSI 1
43+
#define USE_RTC_CLK_LSI 2
44+
#define USE_RTC_CLK_HSE 3
45+
46+
#if !((MBED_CONF_TARGET_RTC_CLOCK_SOURCE == USE_RTC_CLK_LSE_OR_LSI) || (MBED_CONF_TARGET_RTC_CLOCK_SOURCE == USE_RTC_CLK_LSI) || (MBED_CONF_TARGET_RTC_CLOCK_SOURCE == USE_RTC_CLK_HSE))
47+
#error "RTC clock configuration is invalid!"
48+
#endif
49+
50+
#if (MBED_CONF_TARGET_RTC_CLOCK_SOURCE == USE_RTC_CLK_HSE) && !(TARGET_STM32F2 || TARGET_STM32F3 || TARGET_STM32F4)
51+
#error "RTC from HSE not supported for this target"
52+
#endif
53+
54+
#if (MBED_CONF_TARGET_RTC_CLOCK_SOURCE == USE_RTC_CLK_HSE)
55+
#define RTC_CLOCK 1000000U
56+
#define RTC_HSE_DIV (HSE_VALUE / RTC_CLOCK)
57+
#if RTC_HSE_DIV > 31
58+
#error "HSE value too high for RTC"
59+
#endif
60+
#elif (MBED_CONF_TARGET_RTC_CLOCK_SOURCE == USE_RTC_CLK_LSE_OR_LSI) && MBED_CONF_TARGET_LSE_AVAILABLE
4261
#define RTC_CLOCK LSE_VALUE
4362
#else
4463
#define RTC_CLOCK LSI_VALUE
@@ -47,7 +66,11 @@ extern "C" {
4766
#if DEVICE_LPTICKER && !MBED_CONF_TARGET_LPTICKER_LPTIM
4867
/* PREDIV_A : 7-bit asynchronous prescaler */
4968
/* PREDIV_A is set to set LPTICKER frequency to RTC_CLOCK/4 */
69+
#if (MBED_CONF_TARGET_RTC_CLOCK_SOURCE == USE_RTC_CLK_HSE)
70+
#define PREDIV_A_VALUE 124
71+
#else
5072
#define PREDIV_A_VALUE 3
73+
#endif
5174

5275
/** Read RTC counter with sub second precision
5376
*

targets/targets.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,6 +1220,10 @@
12201220
"help": "Define if a Low Speed External xtal (LSE) is available on the board (0 = No, 1 = Yes). If Yes, the LSE will be used to clock the RTC, LPUART, ... otherwise the Low Speed Internal clock (LSI) will be used",
12211221
"value": "1"
12221222
},
1223+
"rtc_clock_source": {
1224+
"help": "Define the RTC clock source. USE_RTC_CLK_LSE_OR_LSI, USE_RTC_CLK_LSI, USE_RTC_CLK_HSE. LPTICKER is not available for HSE and should be removed from the target configuration.",
1225+
"value": "USE_RTC_CLK_LSE_OR_LSI"
1226+
},
12231227
"lpuart_clock_source": {
12241228
"help": "Define the LPUART clock source. Mask values: USE_LPUART_CLK_LSE, USE_LPUART_CLK_PCLK1, USE_LPUART_CLK_HSI",
12251229
"value": "USE_LPUART_CLK_LSE|USE_LPUART_CLK_PCLK1"

0 commit comments

Comments
 (0)