From 0fecc56e84f8f53fba028945fd505aae5373e796 Mon Sep 17 00:00:00 2001 From: jeromecoutant Date: Tue, 11 Sep 2018 14:04:03 +0200 Subject: [PATCH 1/3] STM32 RTC : Start LSI clock asap --- targets/TARGET_STM/mbed_overrides.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/targets/TARGET_STM/mbed_overrides.c b/targets/TARGET_STM/mbed_overrides.c index 9f794ed339..4166cef34e 100644 --- a/targets/TARGET_STM/mbed_overrides.c +++ b/targets/TARGET_STM/mbed_overrides.c @@ -54,5 +54,21 @@ void mbed_sdk_init() SetSysClock(); SystemCoreClockUpdate(); + /* Start LSI clock for RTC */ +#if DEVICE_RTC +#if !MBED_CONF_TARGET_LSE_AVAILABLE + RCC_OscInitTypeDef RCC_OscInitStruct = {0}; + + if (__HAL_RCC_GET_RTC_SOURCE() != RCC_RTCCLKSOURCE_NO_CLK) { + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI; + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; + RCC_OscInitStruct.LSIState = RCC_LSI_ON; + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { + error("Init : cannot initialize LSI\n"); + } + } +#endif /* ! MBED_CONF_TARGET_LSE_AVAILABLE */ +#endif /* DEVICE_RTC */ + mbed_sdk_inited = 1; } From 5c46bec4d4821d195c3172b1ad5d1cf9ebc639cb Mon Sep 17 00:00:00 2001 From: jeromecoutant Date: Tue, 11 Sep 2018 14:04:49 +0200 Subject: [PATCH 2/3] STM32 RTC with LSI : stop reset registers during init --- targets/TARGET_STM/rtc_api.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/targets/TARGET_STM/rtc_api.c b/targets/TARGET_STM/rtc_api.c index e88e9cd28d..66a22abd22 100644 --- a/targets/TARGET_STM/rtc_api.c +++ b/targets/TARGET_STM/rtc_api.c @@ -76,10 +76,6 @@ void rtc_init(void) error("PeriphClkInitStruct RTC failed with LSE\n"); } #else /* MBED_CONF_TARGET_LSE_AVAILABLE */ - // Reset Backup domain - __HAL_RCC_BACKUPRESET_FORCE(); - __HAL_RCC_BACKUPRESET_RELEASE(); - // Enable LSI clock RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured! From 827c8bd486aecdbf64ddc21a7533ecb90f5d52f7 Mon Sep 17 00:00:00 2001 From: jeromecoutant Date: Tue, 11 Sep 2018 14:12:33 +0200 Subject: [PATCH 3/3] STM32 RTC : remove not necessary macro __HAL_RCC_RTC_CLKPRESCALER is called in __HAL_RCC_RTC_CONFIG --- targets/TARGET_STM/TARGET_STM32F0/common_objects.h | 1 - targets/TARGET_STM/TARGET_STM32F1/common_objects.h | 4 ---- targets/TARGET_STM/TARGET_STM32F3/common_objects.h | 4 ---- targets/TARGET_STM/TARGET_STM32L4/common_objects.h | 4 ---- targets/TARGET_STM/rtc_api.c | 12 ++++-------- 5 files changed, 4 insertions(+), 21 deletions(-) diff --git a/targets/TARGET_STM/TARGET_STM32F0/common_objects.h b/targets/TARGET_STM/TARGET_STM32F0/common_objects.h index fee640796f..89b076ef07 100644 --- a/targets/TARGET_STM/TARGET_STM32F0/common_objects.h +++ b/targets/TARGET_STM/TARGET_STM32F0/common_objects.h @@ -148,7 +148,6 @@ struct flash_s { #endif /* STM32F0 HAL doesn't provide this API called in rtc_api.c */ -#define __HAL_RCC_RTC_CLKPRESCALER(__RTCCLKSource__) #define RTC_WKUP_IRQn RTC_IRQn #endif diff --git a/targets/TARGET_STM/TARGET_STM32F1/common_objects.h b/targets/TARGET_STM/TARGET_STM32F1/common_objects.h index d57c6128f9..b22a153b0c 100644 --- a/targets/TARGET_STM/TARGET_STM32F1/common_objects.h +++ b/targets/TARGET_STM/TARGET_STM32F1/common_objects.h @@ -137,8 +137,4 @@ struct flash_s { } #endif -/* STM32F1 HAL doesn't provide this API called in rtc_api.c */ -#define __HAL_RCC_RTC_CLKPRESCALER(__RTCCLKSource__) - #endif - diff --git a/targets/TARGET_STM/TARGET_STM32F3/common_objects.h b/targets/TARGET_STM/TARGET_STM32F3/common_objects.h index 590b60d94c..b1d82d34e7 100644 --- a/targets/TARGET_STM/TARGET_STM32F3/common_objects.h +++ b/targets/TARGET_STM/TARGET_STM32F3/common_objects.h @@ -145,8 +145,4 @@ struct flash_s { } #endif -/* STM32F3 HAL doesn't provide this API called in rtc_api.c */ -#define __HAL_RCC_RTC_CLKPRESCALER(__RTCCLKSource__) - #endif - diff --git a/targets/TARGET_STM/TARGET_STM32L4/common_objects.h b/targets/TARGET_STM/TARGET_STM32L4/common_objects.h index 9ee16729ea..043a2474ef 100644 --- a/targets/TARGET_STM/TARGET_STM32L4/common_objects.h +++ b/targets/TARGET_STM/TARGET_STM32L4/common_objects.h @@ -143,8 +143,4 @@ struct can_s { }; #endif -/* STM32L4 HAL doesn't provide this API called in rtc_api.c */ -#define __HAL_RCC_RTC_CLKPRESCALER(__RTCCLKSource__) - #endif - diff --git a/targets/TARGET_STM/rtc_api.c b/targets/TARGET_STM/rtc_api.c index 66a22abd22..a5da831712 100644 --- a/targets/TARGET_STM/rtc_api.c +++ b/targets/TARGET_STM/rtc_api.c @@ -60,14 +60,12 @@ void rtc_init(void) #if MBED_CONF_TARGET_LSE_AVAILABLE RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured! + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; RCC_OscInitStruct.LSEState = RCC_LSE_ON; - if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { error("Cannot initialize RTC with LSE\n"); } - __HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSE); __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE); PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; @@ -76,15 +74,13 @@ void rtc_init(void) error("PeriphClkInitStruct RTC failed with LSE\n"); } #else /* MBED_CONF_TARGET_LSE_AVAILABLE */ - // Enable LSI clock RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured! + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; RCC_OscInitStruct.LSIState = RCC_LSI_ON; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { error("Cannot initialize RTC with LSI\n"); } - __HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSI); __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI); PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; @@ -112,14 +108,14 @@ void rtc_init(void) #endif /* TARGET_STM32F1 */ if (HAL_RTC_Init(&RtcHandle) != HAL_OK) { - error("RTC initialization failed"); + error("RTC initialization failed\n"); } #if !(TARGET_STM32F1) && !(TARGET_STM32F2) /* STM32F1 : there are no shadow registers */ /* STM32F2 : shadow registers can not be bypassed */ if (HAL_RTCEx_EnableBypassShadow(&RtcHandle) != HAL_OK) { - error("EnableBypassShadow error"); + error("EnableBypassShadow error\n"); } #endif /* TARGET_STM32F1 || TARGET_STM32F2 */ }