Skip to content

Commit 257e466

Browse files
committed
Add RTC clock source selection to targets.json
1 parent 25a0aee commit 257e466

File tree

4 files changed

+35
-13
lines changed

4 files changed

+35
-13
lines changed

targets/TARGET_STM/mbed_overrides.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "cmsis.h"
2929
#include "objects.h"
3030
#include "platform/mbed_error.h"
31+
#include "rtc_api_hal.h"
3132

3233
int mbed_sdk_inited = 0;
3334
extern void SetSysClock(void);
@@ -295,7 +296,15 @@ void mbed_sdk_init()
295296

296297
/* Start LSI clock for RTC */
297298
#if DEVICE_RTC
298-
#if !MBED_CONF_TARGET_LSE_AVAILABLE
299+
#if (MBED_CONF_TARGET_RTC_CLOCK_SOURCE == USE_RTC_CLK_HSE)
300+
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
301+
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
302+
PeriphClkInitStruct.RTCClockSelection = (RCC_RTCCLKSOURCE_HSE_DIVX | RTC_HSE_DIV << 16);
303+
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
304+
{
305+
error("PeriphClkInitStruct RTC failed with HSE\n");
306+
}
307+
#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)
299308
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
300309

301310
if (__HAL_RCC_GET_RTC_SOURCE() != RCC_RTCCLKSOURCE_NO_CLK) {

targets/TARGET_STM/rtc_api.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,14 @@ void rtc_init(void)
6262
while (LL_HSEM_1StepLock(HSEM, CFG_HW_RCC_SEMID)) {
6363
}
6464
#endif /* DUAL_CORE */
65-
#if RTC_FROM_HSE
66-
#define RTC_HSE_DIV (HSE_VALUE / RTC_CLOCK)
67-
#if RTC_HSE_DIV > 31
68-
#error "HSE value too high for RTC"
69-
#endif
65+
#if (MBED_CONF_TARGET_RTC_CLOCK_SOURCE == USE_RTC_CLK_HSE)
7066
(void)RCC_OscInitStruct;
7167
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
7268
PeriphClkInitStruct.RTCClockSelection = (RCC_RTCCLKSOURCE_HSE_DIVX | RTC_HSE_DIV << 16);
7369
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
7470
error("PeriphClkInitStruct RTC failed with HSE\n");
7571
}
76-
#elif MBED_CONF_TARGET_LSE_AVAILABLE
72+
#elif (MBED_CONF_TARGET_RTC_CLOCK_SOURCE == USE_RTC_CLK_LSE_OR_LSI) && MBED_CONF_TARGET_LSE_AVAILABLE
7773
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
7874
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
7975
#if MBED_CONF_TARGET_LSE_BYPASS
@@ -93,7 +89,7 @@ void rtc_init(void)
9389
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
9490
error("PeriphClkInitStruct RTC failed with LSE\n");
9591
}
96-
#else /* MBED_CONF_TARGET_LSE_AVAILABLE */
92+
#else /* Fallback to LSI */
9793
#if TARGET_STM32WB
9894
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI1;
9995
#else
@@ -112,7 +108,7 @@ void rtc_init(void)
112108
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
113109
error("PeriphClkInitStruct RTC failed with LSI\n");
114110
}
115-
#endif /* MBED_CONF_TARGET_LSE_AVAILABLE */
111+
#endif /* MBED_CONF_TARGET_RTC_CLOCK_SOURCE */
116112
#if defined(DUAL_CORE) && (TARGET_STM32H7)
117113
LL_HSEM_ReleaseLock(HSEM, CFG_HW_RCC_SEMID, HSEM_CR_COREID_CURRENT);
118114
#endif /* DUAL_CORE */

targets/TARGET_STM/rtc_api_hal.h

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,26 @@
3838
extern "C" {
3939
#endif
4040

41-
#if RTC_FROM_HSE && !(TARGET_STM32F2 || TARGET_STM32F3 || TARGET_STM32F4)
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)
4251
#error "RTC from HSE not supported for this target"
4352
#endif
4453

45-
#if RTC_FROM_HSE
54+
#if (MBED_CONF_TARGET_RTC_CLOCK_SOURCE == USE_RTC_CLK_HSE)
4655
#define RTC_CLOCK 1000000U
47-
#elif MBED_CONF_TARGET_LSE_AVAILABLE
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
4861
#define RTC_CLOCK LSE_VALUE
4962
#else
5063
#define RTC_CLOCK LSI_VALUE
@@ -53,7 +66,7 @@ extern "C" {
5366
#if DEVICE_LPTICKER && !MBED_CONF_TARGET_LPTICKER_LPTIM
5467
/* PREDIV_A : 7-bit asynchronous prescaler */
5568
/* PREDIV_A is set to set LPTICKER frequency to RTC_CLOCK/4 */
56-
#if RTC_FROM_HSE
69+
#if (MBED_CONF_TARGET_RTC_CLOCK_SOURCE == USE_RTC_CLK_HSE)
5770
#define PREDIV_A_VALUE 124
5871
#else
5972
#define PREDIV_A_VALUE 3

targets/targets.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,6 +1139,10 @@
11391139
"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",
11401140
"value": "1"
11411141
},
1142+
"rtc_clock_source": {
1143+
"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.",
1144+
"value": "USE_RTC_CLK_LSE_OR_LSI"
1145+
},
11421146
"lpuart_clock_source": {
11431147
"help": "Define the LPUART clock source. Mask values: USE_LPUART_CLK_LSE, USE_LPUART_CLK_PCLK1, USE_LPUART_CLK_HSI",
11441148
"value": "USE_LPUART_CLK_LSE|USE_LPUART_CLK_PCLK1"

0 commit comments

Comments
 (0)