|
| 1 | +From 98eef4b565d5701b55e58388b14d45f1776e0430 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Martino Facchin < [email protected]> |
| 3 | +Date: Wed, 28 Sep 2022 17:28:59 +0200 |
| 4 | +Subject: [PATCH 179/179] STM32: RTC: allow runtime clock source selection |
| 5 | + |
| 6 | +--- |
| 7 | + targets/TARGET_STM/rtc_api.c | 80 +++++++++++++++++++++--------------- |
| 8 | + 1 file changed, 46 insertions(+), 34 deletions(-) |
| 9 | + |
| 10 | +diff --git a/targets/TARGET_STM/rtc_api.c b/targets/TARGET_STM/rtc_api.c |
| 11 | +index 3fb6cc7320..4b0d386e39 100644 |
| 12 | +--- a/targets/TARGET_STM/rtc_api.c |
| 13 | ++++ b/targets/TARGET_STM/rtc_api.c |
| 14 | +@@ -44,6 +44,14 @@ static int RTC_inited = 0; |
| 15 | + |
| 16 | + static RTC_HandleTypeDef RtcHandle; |
| 17 | + |
| 18 | ++MBED_WEAK bool isLSEAvailableAndPrecise() { |
| 19 | ++#if MBED_CONF_TARGET_LSE_AVAILABLE |
| 20 | ++ return true; |
| 21 | ++#else |
| 22 | ++ return false; |
| 23 | ++#endif |
| 24 | ++} |
| 25 | ++ |
| 26 | + void rtc_init(void) |
| 27 | + { |
| 28 | + RCC_OscInitTypeDef RCC_OscInitStruct = {0}; |
| 29 | +@@ -73,44 +81,48 @@ void rtc_init(void) |
| 30 | + if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) { |
| 31 | + error("PeriphClkInitStruct RTC failed with HSE\n"); |
| 32 | + } |
| 33 | +-#elif (MBED_CONF_TARGET_RTC_CLOCK_SOURCE == USE_RTC_CLK_LSE_OR_LSI) && MBED_CONF_TARGET_LSE_AVAILABLE |
| 34 | +- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE; |
| 35 | +- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; |
| 36 | +-#if MBED_CONF_TARGET_LSE_BYPASS |
| 37 | +- RCC_OscInitStruct.LSEState = RCC_LSE_BYPASS; |
| 38 | +-#else |
| 39 | +- RCC_OscInitStruct.LSEState = RCC_LSE_ON; |
| 40 | +-#endif |
| 41 | +- |
| 42 | +- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
| 43 | +- error("Cannot initialize RTC with LSE\n"); |
| 44 | +- } |
| 45 | ++#elif (MBED_CONF_TARGET_RTC_CLOCK_SOURCE == USE_RTC_CLK_LSE_OR_LSI) |
| 46 | ++ |
| 47 | ++ // Request if LSE is precise (fallback to WEAK implementation in case) |
| 48 | ++ if (isLSEAvailableAndPrecise()) { |
| 49 | ++ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE; |
| 50 | ++ RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; |
| 51 | ++ #if MBED_CONF_TARGET_LSE_BYPASS |
| 52 | ++ RCC_OscInitStruct.LSEState = RCC_LSE_BYPASS; |
| 53 | ++ #else |
| 54 | ++ RCC_OscInitStruct.LSEState = RCC_LSE_ON; |
| 55 | ++ #endif |
| 56 | ++ |
| 57 | ++ if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
| 58 | ++ error("Cannot initialize RTC with LSE\n"); |
| 59 | ++ } |
| 60 | + |
| 61 | +- __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE); |
| 62 | ++ __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE); |
| 63 | + |
| 64 | +- PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; |
| 65 | +- PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE; |
| 66 | +- if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) { |
| 67 | +- error("PeriphClkInitStruct RTC failed with LSE\n"); |
| 68 | +- } |
| 69 | +-#else /* Fallback to LSI */ |
| 70 | +-#if TARGET_STM32WB |
| 71 | +- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI1; |
| 72 | +-#else |
| 73 | +- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI; |
| 74 | +-#endif |
| 75 | +- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; |
| 76 | +- RCC_OscInitStruct.LSIState = RCC_LSI_ON; |
| 77 | +- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
| 78 | +- error("Cannot initialize RTC with LSI\n"); |
| 79 | +- } |
| 80 | ++ PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; |
| 81 | ++ PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE; |
| 82 | ++ if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) { |
| 83 | ++ error("PeriphClkInitStruct RTC failed with LSE\n"); |
| 84 | ++ } |
| 85 | ++ } else { |
| 86 | ++ #if TARGET_STM32WB |
| 87 | ++ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI1; |
| 88 | ++ #else |
| 89 | ++ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI; |
| 90 | ++ #endif |
| 91 | ++ RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; |
| 92 | ++ RCC_OscInitStruct.LSIState = RCC_LSI_ON; |
| 93 | ++ if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
| 94 | ++ error("Cannot initialize RTC with LSI\n"); |
| 95 | ++ } |
| 96 | + |
| 97 | +- __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI); |
| 98 | ++ __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI); |
| 99 | + |
| 100 | +- PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; |
| 101 | +- PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI; |
| 102 | +- if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) { |
| 103 | +- error("PeriphClkInitStruct RTC failed with LSI\n"); |
| 104 | ++ PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; |
| 105 | ++ PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI; |
| 106 | ++ if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) { |
| 107 | ++ error("PeriphClkInitStruct RTC failed with LSI\n"); |
| 108 | ++ } |
| 109 | + } |
| 110 | + #endif /* MBED_CONF_TARGET_RTC_CLOCK_SOURCE */ |
| 111 | + #if defined(DUAL_CORE) && (TARGET_STM32H7) |
| 112 | +-- |
| 113 | +2.37.3 |
| 114 | + |
0 commit comments