|
| 1 | +From ff38953e8e678c697b52ddbe62bc99fe445a1c74 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Martino Facchin < [email protected]> |
| 3 | +Date: Thu, 7 Oct 2021 17:00:27 +0200 |
| 4 | +Subject: [PATCH 175/176] STM32: lpticker: allow dynamic configuration |
| 5 | + |
| 6 | +Step1: allow automatic fallback to LSI if LSE is not functional |
| 7 | +Step2: expose two reconfiguration APIs, so the user can check if LSE is precise enough and eventually revert to LSI |
| 8 | +--- |
| 9 | + targets/TARGET_STM/lp_ticker.c | 121 ++++++++++++++++++++++----------- |
| 10 | + 1 file changed, 83 insertions(+), 38 deletions(-) |
| 11 | + |
| 12 | +diff --git a/targets/TARGET_STM/lp_ticker.c b/targets/TARGET_STM/lp_ticker.c |
| 13 | +index d5292566e5..6dc806ccf6 100644 |
| 14 | +--- a/targets/TARGET_STM/lp_ticker.c |
| 15 | ++++ b/targets/TARGET_STM/lp_ticker.c |
| 16 | +@@ -126,20 +126,35 @@ |
| 17 | + |
| 18 | + |
| 19 | + LPTIM_HandleTypeDef LptimHandle; |
| 20 | ++static uint8_t using_lse = MBED_CONF_TARGET_LSE_AVAILABLE; |
| 21 | + |
| 22 | +-const ticker_info_t *lp_ticker_get_info() |
| 23 | ++static const ticker_info_t *lp_ticker_get_info_lse() |
| 24 | + { |
| 25 | +- static const ticker_info_t info = { |
| 26 | +-#if MBED_CONF_TARGET_LSE_AVAILABLE |
| 27 | ++ const static ticker_info_t info = { |
| 28 | + LSE_VALUE / MBED_CONF_TARGET_LPTICKER_LPTIM_CLOCK, |
| 29 | +-#else |
| 30 | ++ 16 |
| 31 | ++ }; |
| 32 | ++ return &info; |
| 33 | ++} |
| 34 | ++ |
| 35 | ++static const ticker_info_t *lp_ticker_get_info_lsi() |
| 36 | ++{ |
| 37 | ++ const static ticker_info_t info = { |
| 38 | + LSI_VALUE / MBED_CONF_TARGET_LPTICKER_LPTIM_CLOCK, |
| 39 | +-#endif |
| 40 | + 16 |
| 41 | + }; |
| 42 | + return &info; |
| 43 | + } |
| 44 | + |
| 45 | ++const ticker_info_t *lp_ticker_get_info() |
| 46 | ++{ |
| 47 | ++ if (using_lse) { |
| 48 | ++ return lp_ticker_get_info_lse(); |
| 49 | ++ } else { |
| 50 | ++ return lp_ticker_get_info_lsi(); |
| 51 | ++ } |
| 52 | ++} |
| 53 | ++ |
| 54 | + volatile uint8_t lp_Fired = 0; |
| 55 | + /* Flag and stored counter to handle delayed programing at low level */ |
| 56 | + volatile bool lp_delayed_prog = false; |
| 57 | +@@ -154,71 +169,101 @@ volatile bool sleep_manager_locked = false; |
| 58 | + static int LPTICKER_inited = 0; |
| 59 | + static void LPTIM_IRQHandler(void); |
| 60 | + |
| 61 | +-void lp_ticker_init(void) |
| 62 | +-{ |
| 63 | +- /* Check if LPTIM is already configured */ |
| 64 | +- if (LPTICKER_inited) { |
| 65 | +- lp_ticker_disable_interrupt(); |
| 66 | +- return; |
| 67 | +- } |
| 68 | +- LPTICKER_inited = 1; |
| 69 | +- |
| 70 | +- RCC_PeriphCLKInitTypeDef RCC_PeriphCLKInitStruct = {0}; |
| 71 | +- RCC_OscInitTypeDef RCC_OscInitStruct = {0}; |
| 72 | +- |
| 73 | +-#if MBED_CONF_TARGET_LSE_AVAILABLE |
| 74 | ++static void configureClocksLSE(RCC_PeriphCLKInitTypeDef* RCC_PeriphCLKInitStruct, |
| 75 | ++ RCC_OscInitTypeDef* RCC_OscInitStruct){ |
| 76 | + |
| 77 | + /* Enable LSE clock */ |
| 78 | +- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE; |
| 79 | ++ RCC_OscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_LSE; |
| 80 | + #if MBED_CONF_TARGET_LSE_BYPASS |
| 81 | +- RCC_OscInitStruct.LSEState = RCC_LSE_BYPASS; |
| 82 | ++ RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS; |
| 83 | + #else |
| 84 | +- RCC_OscInitStruct.LSEState = RCC_LSE_ON; |
| 85 | ++ RCC_OscInitStruct->LSEState = RCC_LSE_ON; |
| 86 | + #endif |
| 87 | +- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; |
| 88 | ++ RCC_OscInitStruct->PLL.PLLState = RCC_PLL_NONE; |
| 89 | + |
| 90 | + /* Select the LSE clock as LPTIM peripheral clock */ |
| 91 | +- RCC_PeriphCLKInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LPTIM; |
| 92 | ++ RCC_PeriphCLKInitStruct->PeriphClockSelection = RCC_PERIPHCLK_LPTIM; |
| 93 | + #if (TARGET_STM32L0) |
| 94 | +- RCC_PeriphCLKInitStruct.LptimClockSelection = RCC_LPTIMCLKSOURCE_LSE; |
| 95 | ++ RCC_PeriphCLKInitStruct->LptimClockSelection = RCC_LPTIMCLKSOURCE_LSE; |
| 96 | + #else |
| 97 | + #if (LPTIM_MST_BASE == LPTIM1_BASE) |
| 98 | +- RCC_PeriphCLKInitStruct.Lptim1ClockSelection = RCC_LPTIMCLKSOURCE_LSE; |
| 99 | ++ RCC_PeriphCLKInitStruct->Lptim1ClockSelection = RCC_LPTIMCLKSOURCE_LSE; |
| 100 | + #elif (LPTIM_MST_BASE == LPTIM3_BASE) || (LPTIM_MST_BASE == LPTIM4_BASE) || (LPTIM_MST_BASE == LPTIM5_BASE) |
| 101 | +- RCC_PeriphCLKInitStruct.Lptim345ClockSelection = RCC_LPTIMCLKSOURCE_LSE; |
| 102 | ++ RCC_PeriphCLKInitStruct->Lptim345ClockSelection = RCC_LPTIMCLKSOURCE_LSE; |
| 103 | + #endif /* LPTIM_MST_BASE == LPTIM1 */ |
| 104 | + #endif /* TARGET_STM32L0 */ |
| 105 | +-#else /* MBED_CONF_TARGET_LSE_AVAILABLE */ |
| 106 | ++} |
| 107 | ++ |
| 108 | ++static void configureClocksLSI(RCC_PeriphCLKInitTypeDef* RCC_PeriphCLKInitStruct, |
| 109 | ++ RCC_OscInitTypeDef* RCC_OscInitStruct){ |
| 110 | + |
| 111 | + /* Enable LSI clock */ |
| 112 | + #if TARGET_STM32WB |
| 113 | +- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI1; |
| 114 | ++ RCC_OscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_LSI1; |
| 115 | + #else |
| 116 | +- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI; |
| 117 | ++ RCC_OscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_LSI; |
| 118 | + #endif |
| 119 | +- RCC_OscInitStruct.LSIState = RCC_LSI_ON; |
| 120 | +- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; |
| 121 | ++ RCC_OscInitStruct->LSIState = RCC_LSI_ON; |
| 122 | ++ RCC_OscInitStruct->PLL.PLLState = RCC_PLL_NONE; |
| 123 | + |
| 124 | + /* Select the LSI clock as LPTIM peripheral clock */ |
| 125 | +- RCC_PeriphCLKInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LPTIM; |
| 126 | ++ RCC_PeriphCLKInitStruct->PeriphClockSelection = RCC_PERIPHCLK_LPTIM; |
| 127 | + #if (TARGET_STM32L0) |
| 128 | +- RCC_PeriphCLKInitStruct.LptimClockSelection = RCC_LPTIMCLKSOURCE_LSI; |
| 129 | ++ RCC_PeriphCLKInitStruct->LptimClockSelection = RCC_LPTIMCLKSOURCE_LSI; |
| 130 | + #else |
| 131 | + #if (LPTIM_MST_BASE == LPTIM1_BASE) |
| 132 | +- RCC_PeriphCLKInitStruct.Lptim1ClockSelection = RCC_LPTIMCLKSOURCE_LSI; |
| 133 | ++ RCC_PeriphCLKInitStruct->Lptim1ClockSelection = RCC_LPTIMCLKSOURCE_LSI; |
| 134 | + #elif (LPTIM_MST_BASE == LPTIM3_BASE) || (LPTIM_MST_BASE == LPTIM4_BASE) || (LPTIM_MST_BASE == LPTIM5_BASE) |
| 135 | +- RCC_PeriphCLKInitStruct.Lptim345ClockSelection = RCC_LPTIMCLKSOURCE_LSI; |
| 136 | ++ RCC_PeriphCLKInitStruct->Lptim345ClockSelection = RCC_LPTIMCLKSOURCE_LSI; |
| 137 | + #endif /* LPTIM_MST_BASE == LPTIM1 */ |
| 138 | + #endif /* TARGET_STM32L0 */ |
| 139 | ++} |
| 140 | ++ |
| 141 | ++void lp_ticker_reconfigure_with_lsi() { |
| 142 | ++ lp_ticker_disable_interrupt(); |
| 143 | ++ LPTICKER_inited = 0; |
| 144 | ++ using_lse = 0; |
| 145 | ++ lp_ticker_init(); |
| 146 | ++} |
| 147 | ++ |
| 148 | ++void lp_ticker_reconfigure_with_lse() { |
| 149 | ++ lp_ticker_disable_interrupt(); |
| 150 | ++ LPTICKER_inited = 0; |
| 151 | ++ using_lse = 1; |
| 152 | ++ lp_ticker_init(); |
| 153 | ++} |
| 154 | ++ |
| 155 | ++void lp_ticker_init(void) |
| 156 | ++{ |
| 157 | ++ /* Check if LPTIM is already configured */ |
| 158 | ++ if (LPTICKER_inited) { |
| 159 | ++ lp_ticker_disable_interrupt(); |
| 160 | ++ return; |
| 161 | ++ } |
| 162 | ++ LPTICKER_inited = 1; |
| 163 | ++ |
| 164 | ++ RCC_PeriphCLKInitTypeDef RCC_PeriphCLKInitStruct = {0}; |
| 165 | ++ RCC_OscInitTypeDef RCC_OscInitStruct = {0}; |
| 166 | ++ |
| 167 | ++ if (using_lse) { |
| 168 | ++ configureClocksLSE(&RCC_PeriphCLKInitStruct, &RCC_OscInitStruct); |
| 169 | ++ } else { |
| 170 | ++ configureClocksLSI(&RCC_PeriphCLKInitStruct, &RCC_OscInitStruct); |
| 171 | ++ } |
| 172 | + |
| 173 | +-#endif /* MBED_CONF_TARGET_LSE_AVAILABLE */ |
| 174 | + #if defined(DUAL_CORE) && (TARGET_STM32H7) |
| 175 | + while (LL_HSEM_1StepLock(HSEM, CFG_HW_RCC_SEMID)) { |
| 176 | + } |
| 177 | + #endif /* DUAL_CORE */ |
| 178 | + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
| 179 | +- error("HAL_RCC_OscConfig ERROR\n"); |
| 180 | +- return; |
| 181 | ++ |
| 182 | ++ // retry with LSI |
| 183 | ++ using_lse = 0; |
| 184 | ++ configureClocksLSI(&RCC_PeriphCLKInitStruct, &RCC_OscInitStruct); |
| 185 | ++ if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
| 186 | ++ error("HAL_RCC_OscConfig ERROR\n"); |
| 187 | ++ return; |
| 188 | ++ } |
| 189 | + } |
| 190 | + |
| 191 | + if (HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphCLKInitStruct) != HAL_OK) { |
| 192 | +-- |
| 193 | +2.37.1 |
| 194 | + |
0 commit comments