1
0

stm32l4xx_hal_rcc.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_rcc.c
  4. * @author MCD Application Team
  5. * @version V1.7.2
  6. * @date 16-June-2017
  7. * @brief RCC HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the Reset and Clock Control (RCC) peripheral:
  10. * + Initialization and de-initialization functions
  11. * + Peripheral Control functions
  12. *
  13. @verbatim
  14. ==============================================================================
  15. ##### RCC specific features #####
  16. ==============================================================================
  17. [..]
  18. After reset the device is running from Multiple Speed Internal oscillator
  19. (4 MHz) with Flash 0 wait state. Flash prefetch buffer, D-Cache
  20. and I-Cache are disabled, and all peripherals are off except internal
  21. SRAM, Flash and JTAG.
  22. (+) There is no prescaler on High speed (AHBs) and Low speed (APBs) busses:
  23. all peripherals mapped on these busses are running at MSI speed.
  24. (+) The clock for all peripherals is switched off, except the SRAM and FLASH.
  25. (+) All GPIOs are in analog mode, except the JTAG pins which
  26. are assigned to be used for debug purpose.
  27. [..]
  28. Once the device started from reset, the user application has to:
  29. (+) Configure the clock source to be used to drive the System clock
  30. (if the application needs higher frequency/performance)
  31. (+) Configure the System clock frequency and Flash settings
  32. (+) Configure the AHB and APB busses prescalers
  33. (+) Enable the clock for the peripheral(s) to be used
  34. (+) Configure the clock source(s) for peripherals which clocks are not
  35. derived from the System clock (SAIx, RTC, ADC, USB OTG FS/SDMMC1/RNG)
  36. @endverbatim
  37. ******************************************************************************
  38. * @attention
  39. *
  40. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  41. *
  42. * Redistribution and use in source and binary forms, with or without modification,
  43. * are permitted provided that the following conditions are met:
  44. * 1. Redistributions of source code must retain the above copyright notice,
  45. * this list of conditions and the following disclaimer.
  46. * 2. Redistributions in binary form must reproduce the above copyright notice,
  47. * this list of conditions and the following disclaimer in the documentation
  48. * and/or other materials provided with the distribution.
  49. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  50. * may be used to endorse or promote products derived from this software
  51. * without specific prior written permission.
  52. *
  53. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  54. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  55. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  56. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  57. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  58. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  59. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  60. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  61. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  62. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  63. *
  64. ******************************************************************************
  65. */
  66. /* Includes ------------------------------------------------------------------*/
  67. #include "stm32l4xx_hal.h"
  68. /** @addtogroup STM32L4xx_HAL_Driver
  69. * @{
  70. */
  71. /** @defgroup RCC RCC
  72. * @brief RCC HAL module driver
  73. * @{
  74. */
  75. #ifdef HAL_RCC_MODULE_ENABLED
  76. /* Private typedef -----------------------------------------------------------*/
  77. /* Private define ------------------------------------------------------------*/
  78. /** @defgroup RCC_Private_Constants RCC Private Constants
  79. * @{
  80. */
  81. #define HSE_TIMEOUT_VALUE HSE_STARTUP_TIMEOUT
  82. #define HSI_TIMEOUT_VALUE ((uint32_t)2U) /* 2 ms (minimum Tick + 1) */
  83. #define MSI_TIMEOUT_VALUE ((uint32_t)2U) /* 2 ms (minimum Tick + 1) */
  84. #define LSI_TIMEOUT_VALUE ((uint32_t)2U) /* 2 ms (minimum Tick + 1) */
  85. #define HSI48_TIMEOUT_VALUE ((uint32_t)2U) /* 2 ms (minimum Tick + 1) */
  86. #define PLL_TIMEOUT_VALUE ((uint32_t)2U) /* 2 ms (minimum Tick + 1) */
  87. #define CLOCKSWITCH_TIMEOUT_VALUE ((uint32_t)5000U) /* 5 s */
  88. /**
  89. * @}
  90. */
  91. /* Private macro -------------------------------------------------------------*/
  92. /** @defgroup RCC_Private_Macros RCC Private Macros
  93. * @{
  94. */
  95. #define __MCO1_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
  96. #define MCO1_GPIO_PORT GPIOA
  97. #define MCO1_PIN GPIO_PIN_8
  98. #define RCC_PLL_OSCSOURCE_CONFIG(__HAL_RCC_PLLSOURCE__) \
  99. (MODIFY_REG(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC, (uint32_t)(__HAL_RCC_PLLSOURCE__)))
  100. /**
  101. * @}
  102. */
  103. /* Private variables ---------------------------------------------------------*/
  104. /** @defgroup RCC_Private_Variables RCC Private Variables
  105. * @{
  106. */
  107. /**
  108. * @}
  109. */
  110. /* Private function prototypes -----------------------------------------------*/
  111. /** @defgroup RCC_Private_Functions RCC Private Functions
  112. * @{
  113. */
  114. static HAL_StatusTypeDef RCC_SetFlashLatencyFromMSIRange(uint32_t msirange);
  115. /**
  116. * @}
  117. */
  118. /* Exported functions --------------------------------------------------------*/
  119. /** @defgroup RCC_Exported_Functions RCC Exported Functions
  120. * @{
  121. */
  122. /** @defgroup RCC_Exported_Functions_Group1 Initialization and de-initialization functions
  123. * @brief Initialization and Configuration functions
  124. *
  125. @verbatim
  126. ===============================================================================
  127. ##### Initialization and de-initialization functions #####
  128. ===============================================================================
  129. [..]
  130. This section provides functions allowing to configure the internal and external oscillators
  131. (HSE, HSI, LSE, MSI, LSI, PLL, CSS and MCO) and the System busses clocks (SYSCLK, AHB, APB1
  132. and APB2).
  133. [..] Internal/external clock and PLL configuration
  134. (+) HSI (high-speed internal): 16 MHz factory-trimmed RC used directly or through
  135. the PLL as System clock source.
  136. (+) MSI (Mutiple Speed Internal): Its frequency is software trimmable from 100KHZ to 48MHZ.
  137. It can be used to generate the clock for the USB OTG FS (48 MHz).
  138. The number of flash wait states is automatically adjusted when MSI range is updated with
  139. HAL_RCC_OscConfig() and the MSI is used as System clock source.
  140. (+) LSI (low-speed internal): 32 KHz low consumption RC used as IWDG and/or RTC
  141. clock source.
  142. (+) HSE (high-speed external): 4 to 48 MHz crystal oscillator used directly or
  143. through the PLL as System clock source. Can be used also optionally as RTC clock source.
  144. (+) LSE (low-speed external): 32.768 KHz oscillator used optionally as RTC clock source.
  145. (+) PLL (clocked by HSI, HSE or MSI) providing up to three independent output clocks:
  146. (++) The first output is used to generate the high speed system clock (up to 80MHz).
  147. (++) The second output is used to generate the clock for the USB OTG FS (48 MHz),
  148. the random analog generator (<=48 MHz) and the SDMMC1 (<= 48 MHz).
  149. (++) The third output is used to generate an accurate clock to achieve
  150. high-quality audio performance on SAI interface.
  151. (+) PLLSAI1 (clocked by HSI, HSE or MSI) providing up to three independent output clocks:
  152. (++) The first output is used to generate SAR ADC1 clock.
  153. (++) The second output is used to generate the clock for the USB OTG FS (48 MHz),
  154. the random analog generator (<=48 MHz) and the SDMMC1 (<= 48 MHz).
  155. (++) The Third output is used to generate an accurate clock to achieve
  156. high-quality audio performance on SAI interface.
  157. (+) PLLSAI2 (clocked by HSI , HSE or MSI) providing up to two independent output clocks:
  158. (++) The first output is used to generate SAR ADC2 clock.
  159. (++) The second output is used to generate an accurate clock to achieve
  160. high-quality audio performance on SAI interface.
  161. (+) CSS (Clock security system): once enabled, if a HSE clock failure occurs
  162. (HSE used directly or through PLL as System clock source), the System clock
  163. is automatically switched to HSI and an interrupt is generated if enabled.
  164. The interrupt is linked to the Cortex-M4 NMI (Non-Maskable Interrupt)
  165. exception vector.
  166. (+) MCO (microcontroller clock output): used to output MSI, LSI, HSI, LSE, HSE or
  167. main PLL clock (through a configurable prescaler) on PA8 pin.
  168. [..] System, AHB and APB busses clocks configuration
  169. (+) Several clock sources can be used to drive the System clock (SYSCLK): MSI, HSI,
  170. HSE and main PLL.
  171. The AHB clock (HCLK) is derived from System clock through configurable
  172. prescaler and used to clock the CPU, memory and peripherals mapped
  173. on AHB bus (DMA, GPIO...). APB1 (PCLK1) and APB2 (PCLK2) clocks are derived
  174. from AHB clock through configurable prescalers and used to clock
  175. the peripherals mapped on these busses. You can use
  176. "HAL_RCC_GetSysClockFreq()" function to retrieve the frequencies of these clocks.
  177. -@- All the peripheral clocks are derived from the System clock (SYSCLK) except:
  178. (+@) SAI: the SAI clock can be derived either from a specific PLL (PLLSAI1) or (PLLSAI2) or
  179. from an external clock mapped on the SAI_CKIN pin.
  180. You have to use HAL_RCCEx_PeriphCLKConfig() function to configure this clock.
  181. (+@) RTC: the RTC clock can be derived either from the LSI, LSE or HSE clock
  182. divided by 2 to 31.
  183. You have to use __HAL_RCC_RTC_ENABLE() and HAL_RCCEx_PeriphCLKConfig() function
  184. to configure this clock.
  185. (+@) USB OTG FS, SDMMC1 and RNG: USB OTG FS requires a frequency equal to 48 MHz
  186. to work correctly, while the SDMMC1 and RNG peripherals require a frequency
  187. equal or lower than to 48 MHz. This clock is derived of the main PLL or PLLSAI1
  188. through PLLQ divider. You have to enable the peripheral clock and use
  189. HAL_RCCEx_PeriphCLKConfig() function to configure this clock.
  190. (+@) IWDG clock which is always the LSI clock.
  191. (+) The maximum frequency of the SYSCLK, HCLK, PCLK1 and PCLK2 is 80 MHz.
  192. The clock source frequency should be adapted depending on the device voltage range
  193. as listed in the Reference Manual "Clock source frequency versus voltage scaling" chapter.
  194. @endverbatim
  195. Table 1. HCLK clock frequency.
  196. +-------------------------------------------------------+
  197. | Latency | HCLK clock frequency (MHz) |
  198. | |-------------------------------------|
  199. | | voltage range 1 | voltage range 2 |
  200. | | 1.2 V | 1.0 V |
  201. |-----------------|------------------|------------------|
  202. |0WS(1 CPU cycles)| 0 < HCLK <= 16 | 0 < HCLK <= 6 |
  203. |-----------------|------------------|------------------|
  204. |1WS(2 CPU cycles)| 16 < HCLK <= 32 | 6 < HCLK <= 12 |
  205. |-----------------|------------------|------------------|
  206. |2WS(3 CPU cycles)| 32 < HCLK <= 48 | 12 < HCLK <= 18 |
  207. |-----------------|------------------|------------------|
  208. |3WS(4 CPU cycles)| 48 < HCLK <= 64 | 18 < HCLK <= 26 |
  209. |-----------------|------------------|------------------|
  210. |4WS(5 CPU cycles)| 64 < HCLK <= 80 | 18 < HCLK <= 26 |
  211. +-------------------------------------------------------+
  212. * @{
  213. */
  214. /**
  215. * @brief Reset the RCC clock configuration to the default reset state.
  216. * @note The default reset state of the clock configuration is given below:
  217. * - MSI ON and used as system clock source
  218. * - HSE, HSI, PLL, PLLSAI1 and PLLISAI2 OFF
  219. * - AHB, APB1 and APB2 prescaler set to 1.
  220. * - CSS, MCO1 OFF
  221. * - All interrupts disabled
  222. * @note This function doesn't modify the configuration of the
  223. * - Peripheral clocks
  224. * - LSI, LSE and RTC clocks
  225. * @retval None
  226. */
  227. void HAL_RCC_DeInit(void)
  228. {
  229. /* Set MSION bit */
  230. SET_BIT(RCC->CR, RCC_CR_MSION);
  231. /* Insure MSIRDY bit is set before writing default MSIRANGE value */
  232. while(READ_BIT(RCC->CR, RCC_CR_MSIRDY) == RESET) { __NOP(); }
  233. /* Set MSIRANGE default value */
  234. MODIFY_REG(RCC->CR, RCC_CR_MSIRANGE, RCC_MSIRANGE_6);
  235. /* Reset CFGR register (MSI is selected as system clock source) */
  236. CLEAR_REG(RCC->CFGR);
  237. /* Reset HSION, HSIKERON, HSIASFS, HSEON, HSECSSON, PLLON, PLLSAIxON bits */
  238. #if defined(RCC_PLLSAI2_SUPPORT)
  239. CLEAR_BIT(RCC->CR, RCC_CR_HSEON | RCC_CR_HSION | RCC_CR_HSIKERON| RCC_CR_HSIASFS | RCC_CR_PLLON | RCC_CR_PLLSAI1ON | RCC_CR_PLLSAI2ON);
  240. #else
  241. CLEAR_BIT(RCC->CR, RCC_CR_HSEON | RCC_CR_HSION | RCC_CR_HSIKERON| RCC_CR_HSIASFS | RCC_CR_PLLON | RCC_CR_PLLSAI1ON);
  242. #endif /* RCC_PLLSAI2_SUPPORT */
  243. /* Reset PLLCFGR register */
  244. CLEAR_REG(RCC->PLLCFGR);
  245. SET_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN_4 );
  246. /* Reset PLLSAI1CFGR register */
  247. CLEAR_REG(RCC->PLLSAI1CFGR);
  248. SET_BIT(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLSAI1N_4 );
  249. #if defined(RCC_PLLSAI2_SUPPORT)
  250. /* Reset PLLSAI2CFGR register */
  251. CLEAR_REG(RCC->PLLSAI2CFGR);
  252. SET_BIT(RCC->PLLSAI2CFGR, RCC_PLLSAI2CFGR_PLLSAI2N_4 );
  253. #endif /* RCC_PLLSAI2_SUPPORT */
  254. /* Reset HSEBYP bit */
  255. CLEAR_BIT(RCC->CR, RCC_CR_HSEBYP);
  256. /* Disable all interrupts */
  257. CLEAR_REG(RCC->CIER);
  258. /* Update the SystemCoreClock global variable */
  259. SystemCoreClock = MSI_VALUE;
  260. }
  261. /**
  262. * @brief Initialize the RCC Oscillators according to the specified parameters in the
  263. * RCC_OscInitTypeDef.
  264. * @param RCC_OscInitStruct pointer to an RCC_OscInitTypeDef structure that
  265. * contains the configuration information for the RCC Oscillators.
  266. * @note The PLL is not disabled when used as system clock.
  267. * @note Transitions LSE Bypass to LSE On and LSE On to LSE Bypass are not
  268. * supported by this macro. User should request a transition to LSE Off
  269. * first and then LSE On or LSE Bypass.
  270. * @note Transition HSE Bypass to HSE On and HSE On to HSE Bypass are not
  271. * supported by this macro. User should request a transition to HSE Off
  272. * first and then HSE On or HSE Bypass.
  273. * @retval HAL status
  274. */
  275. HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  276. {
  277. uint32_t tickstart = 0;
  278. /* Check the parameters */
  279. assert_param(RCC_OscInitStruct != NULL);
  280. assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct->OscillatorType));
  281. /*----------------------------- MSI Configuration --------------------------*/
  282. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_MSI) == RCC_OSCILLATORTYPE_MSI)
  283. {
  284. /* Check the parameters */
  285. assert_param(IS_RCC_MSI(RCC_OscInitStruct->MSIState));
  286. assert_param(IS_RCC_MSICALIBRATION_VALUE(RCC_OscInitStruct->MSICalibrationValue));
  287. assert_param(IS_RCC_MSI_CLOCK_RANGE(RCC_OscInitStruct->MSIClockRange));
  288. /* When the MSI is used as system clock it will not be disabled */
  289. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_MSI) )
  290. {
  291. if((READ_BIT(RCC->CR, RCC_CR_MSIRDY) != RESET) && (RCC_OscInitStruct->MSIState == RCC_MSI_OFF))
  292. {
  293. return HAL_ERROR;
  294. }
  295. /* Otherwise, just the calibration and MSI range change are allowed */
  296. else
  297. {
  298. /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
  299. must be correctly programmed according to the frequency of the CPU clock
  300. (HCLK) and the supply voltage of the device. */
  301. if(RCC_OscInitStruct->MSIClockRange > __HAL_RCC_GET_MSI_RANGE())
  302. {
  303. /* First increase number of wait states update if necessary */
  304. if(RCC_SetFlashLatencyFromMSIRange(RCC_OscInitStruct->MSIClockRange) != HAL_OK)
  305. {
  306. return HAL_ERROR;
  307. }
  308. /* Selects the Multiple Speed oscillator (MSI) clock range .*/
  309. __HAL_RCC_MSI_RANGE_CONFIG(RCC_OscInitStruct->MSIClockRange);
  310. /* Adjusts the Multiple Speed oscillator (MSI) calibration value.*/
  311. __HAL_RCC_MSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->MSICalibrationValue);
  312. }
  313. else
  314. {
  315. /* Else, keep current flash latency while decreasing applies */
  316. /* Selects the Multiple Speed oscillator (MSI) clock range .*/
  317. __HAL_RCC_MSI_RANGE_CONFIG(RCC_OscInitStruct->MSIClockRange);
  318. /* Adjusts the Multiple Speed oscillator (MSI) calibration value.*/
  319. __HAL_RCC_MSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->MSICalibrationValue);
  320. /* Decrease number of wait states update if necessary */
  321. if(RCC_SetFlashLatencyFromMSIRange(RCC_OscInitStruct->MSIClockRange) != HAL_OK)
  322. {
  323. return HAL_ERROR;
  324. }
  325. }
  326. /* Update the SystemCoreClock global variable */
  327. SystemCoreClock = HAL_RCC_GetSysClockFreq() >> AHBPrescTable[(RCC->CFGR & RCC_CFGR_HPRE) >> RCC_CFGR_HPRE_Pos];
  328. /* Configure the source of time base considering new system clocks settings*/
  329. HAL_InitTick (TICK_INT_PRIORITY);
  330. }
  331. }
  332. else
  333. {
  334. /* Check the MSI State */
  335. if(RCC_OscInitStruct->MSIState != RCC_MSI_OFF)
  336. {
  337. /* Enable the Internal High Speed oscillator (MSI). */
  338. __HAL_RCC_MSI_ENABLE();
  339. /* Get timeout */
  340. tickstart = HAL_GetTick();
  341. /* Wait till MSI is ready */
  342. while(READ_BIT(RCC->CR, RCC_CR_MSIRDY) == RESET)
  343. {
  344. if((HAL_GetTick() - tickstart) > MSI_TIMEOUT_VALUE)
  345. {
  346. return HAL_TIMEOUT;
  347. }
  348. }
  349. /* Selects the Multiple Speed oscillator (MSI) clock range .*/
  350. __HAL_RCC_MSI_RANGE_CONFIG(RCC_OscInitStruct->MSIClockRange);
  351. /* Adjusts the Multiple Speed oscillator (MSI) calibration value.*/
  352. __HAL_RCC_MSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->MSICalibrationValue);
  353. }
  354. else
  355. {
  356. /* Disable the Internal High Speed oscillator (MSI). */
  357. __HAL_RCC_MSI_DISABLE();
  358. /* Get timeout */
  359. tickstart = HAL_GetTick();
  360. /* Wait till MSI is ready */
  361. while(READ_BIT(RCC->CR, RCC_CR_MSIRDY) != RESET)
  362. {
  363. if((HAL_GetTick() - tickstart) > MSI_TIMEOUT_VALUE)
  364. {
  365. return HAL_TIMEOUT;
  366. }
  367. }
  368. }
  369. }
  370. }
  371. /*------------------------------- HSE Configuration ------------------------*/
  372. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE)
  373. {
  374. /* Check the parameters */
  375. assert_param(IS_RCC_HSE(RCC_OscInitStruct->HSEState));
  376. /* When the HSE is used as system clock or clock source for PLL in these cases it is not allowed to be disabled */
  377. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSE) ||
  378. ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL) && (__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_HSE)))
  379. {
  380. if((READ_BIT(RCC->CR, RCC_CR_HSERDY) != RESET) && (RCC_OscInitStruct->HSEState == RCC_HSE_OFF))
  381. {
  382. return HAL_ERROR;
  383. }
  384. }
  385. else
  386. {
  387. /* Set the new HSE configuration ---------------------------------------*/
  388. __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct->HSEState);
  389. /* Check the HSE State */
  390. if(RCC_OscInitStruct->HSEState != RCC_HSE_OFF)
  391. {
  392. /* Get Start Tick*/
  393. tickstart = HAL_GetTick();
  394. /* Wait till HSE is ready */
  395. while(READ_BIT(RCC->CR, RCC_CR_HSERDY) == RESET)
  396. {
  397. if((HAL_GetTick() - tickstart) > HSE_TIMEOUT_VALUE)
  398. {
  399. return HAL_TIMEOUT;
  400. }
  401. }
  402. }
  403. else
  404. {
  405. /* Get Start Tick*/
  406. tickstart = HAL_GetTick();
  407. /* Wait till HSE is disabled */
  408. while(READ_BIT(RCC->CR, RCC_CR_HSERDY) != RESET)
  409. {
  410. if((HAL_GetTick() - tickstart) > HSE_TIMEOUT_VALUE)
  411. {
  412. return HAL_TIMEOUT;
  413. }
  414. }
  415. }
  416. }
  417. }
  418. /*----------------------------- HSI Configuration --------------------------*/
  419. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI)
  420. {
  421. /* Check the parameters */
  422. assert_param(IS_RCC_HSI(RCC_OscInitStruct->HSIState));
  423. assert_param(IS_RCC_HSI_CALIBRATION_VALUE(RCC_OscInitStruct->HSICalibrationValue));
  424. /* Check if HSI is used as system clock or as PLL source when PLL is selected as system clock */
  425. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSI) ||
  426. ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL) && (__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_HSI)))
  427. {
  428. /* When HSI is used as system clock it will not be disabled */
  429. if((READ_BIT(RCC->CR, RCC_CR_HSIRDY) != RESET) && (RCC_OscInitStruct->HSIState == RCC_HSI_OFF))
  430. {
  431. return HAL_ERROR;
  432. }
  433. /* Otherwise, just the calibration is allowed */
  434. else
  435. {
  436. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  437. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  438. }
  439. }
  440. else
  441. {
  442. /* Check the HSI State */
  443. if(RCC_OscInitStruct->HSIState != RCC_HSI_OFF)
  444. {
  445. /* Enable the Internal High Speed oscillator (HSI). */
  446. __HAL_RCC_HSI_ENABLE();
  447. /* Get Start Tick*/
  448. tickstart = HAL_GetTick();
  449. /* Wait till HSI is ready */
  450. while(READ_BIT(RCC->CR, RCC_CR_HSIRDY) == RESET)
  451. {
  452. if((HAL_GetTick() - tickstart) > HSI_TIMEOUT_VALUE)
  453. {
  454. return HAL_TIMEOUT;
  455. }
  456. }
  457. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  458. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  459. }
  460. else
  461. {
  462. /* Disable the Internal High Speed oscillator (HSI). */
  463. __HAL_RCC_HSI_DISABLE();
  464. /* Get Start Tick*/
  465. tickstart = HAL_GetTick();
  466. /* Wait till HSI is disabled */
  467. while(READ_BIT(RCC->CR, RCC_CR_HSIRDY) != RESET)
  468. {
  469. if((HAL_GetTick() - tickstart) > HSI_TIMEOUT_VALUE)
  470. {
  471. return HAL_TIMEOUT;
  472. }
  473. }
  474. }
  475. }
  476. }
  477. /*------------------------------ LSI Configuration -------------------------*/
  478. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI)
  479. {
  480. /* Check the parameters */
  481. assert_param(IS_RCC_LSI(RCC_OscInitStruct->LSIState));
  482. /* Check the LSI State */
  483. if(RCC_OscInitStruct->LSIState != RCC_LSI_OFF)
  484. {
  485. /* Enable the Internal Low Speed oscillator (LSI). */
  486. __HAL_RCC_LSI_ENABLE();
  487. /* Get Start Tick*/
  488. tickstart = HAL_GetTick();
  489. /* Wait till LSI is ready */
  490. while(READ_BIT(RCC->CSR, RCC_CSR_LSIRDY) == RESET)
  491. {
  492. if((HAL_GetTick() - tickstart) > LSI_TIMEOUT_VALUE)
  493. {
  494. return HAL_TIMEOUT;
  495. }
  496. }
  497. }
  498. else
  499. {
  500. /* Disable the Internal Low Speed oscillator (LSI). */
  501. __HAL_RCC_LSI_DISABLE();
  502. /* Get Start Tick*/
  503. tickstart = HAL_GetTick();
  504. /* Wait till LSI is disabled */
  505. while(READ_BIT(RCC->CSR, RCC_CSR_LSIRDY) != RESET)
  506. {
  507. if((HAL_GetTick() - tickstart) > LSI_TIMEOUT_VALUE)
  508. {
  509. return HAL_TIMEOUT;
  510. }
  511. }
  512. }
  513. }
  514. /*------------------------------ LSE Configuration -------------------------*/
  515. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE)
  516. {
  517. FlagStatus pwrclkchanged = RESET;
  518. /* Check the parameters */
  519. assert_param(IS_RCC_LSE(RCC_OscInitStruct->LSEState));
  520. /* Update LSE configuration in Backup Domain control register */
  521. /* Requires to enable write access to Backup Domain of necessary */
  522. if(HAL_IS_BIT_CLR(RCC->APB1ENR1, RCC_APB1ENR1_PWREN))
  523. {
  524. __HAL_RCC_PWR_CLK_ENABLE();
  525. pwrclkchanged = SET;
  526. }
  527. if(HAL_IS_BIT_CLR(PWR->CR1, PWR_CR1_DBP))
  528. {
  529. /* Enable write access to Backup domain */
  530. SET_BIT(PWR->CR1, PWR_CR1_DBP);
  531. /* Wait for Backup domain Write protection disable */
  532. tickstart = HAL_GetTick();
  533. while(HAL_IS_BIT_CLR(PWR->CR1, PWR_CR1_DBP))
  534. {
  535. if((HAL_GetTick() - tickstart) > RCC_DBP_TIMEOUT_VALUE)
  536. {
  537. return HAL_TIMEOUT;
  538. }
  539. }
  540. }
  541. /* Set the new LSE configuration -----------------------------------------*/
  542. __HAL_RCC_LSE_CONFIG(RCC_OscInitStruct->LSEState);
  543. /* Check the LSE State */
  544. if(RCC_OscInitStruct->LSEState != RCC_LSE_OFF)
  545. {
  546. /* Get Start Tick*/
  547. tickstart = HAL_GetTick();
  548. /* Wait till LSE is ready */
  549. while(READ_BIT(RCC->BDCR, RCC_BDCR_LSERDY) == RESET)
  550. {
  551. if((HAL_GetTick() - tickstart) > RCC_LSE_TIMEOUT_VALUE)
  552. {
  553. return HAL_TIMEOUT;
  554. }
  555. }
  556. }
  557. else
  558. {
  559. /* Get Start Tick*/
  560. tickstart = HAL_GetTick();
  561. /* Wait till LSE is disabled */
  562. while(READ_BIT(RCC->BDCR, RCC_BDCR_LSERDY) != RESET)
  563. {
  564. if((HAL_GetTick() - tickstart) > RCC_LSE_TIMEOUT_VALUE)
  565. {
  566. return HAL_TIMEOUT;
  567. }
  568. }
  569. }
  570. /* Restore clock configuration if changed */
  571. if(pwrclkchanged == SET)
  572. {
  573. __HAL_RCC_PWR_CLK_DISABLE();
  574. }
  575. }
  576. #if defined(RCC_HSI48_SUPPORT)
  577. /*------------------------------ HSI48 Configuration -----------------------*/
  578. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI48) == RCC_OSCILLATORTYPE_HSI48)
  579. {
  580. /* Check the parameters */
  581. assert_param(IS_RCC_HSI48(RCC_OscInitStruct->HSI48State));
  582. /* Check the LSI State */
  583. if(RCC_OscInitStruct->HSI48State != RCC_HSI48_OFF)
  584. {
  585. /* Enable the Internal Low Speed oscillator (HSI48). */
  586. __HAL_RCC_HSI48_ENABLE();
  587. /* Get Start Tick*/
  588. tickstart = HAL_GetTick();
  589. /* Wait till HSI48 is ready */
  590. while(READ_BIT(RCC->CRRCR, RCC_CRRCR_HSI48RDY) == RESET)
  591. {
  592. if((HAL_GetTick() - tickstart) > HSI48_TIMEOUT_VALUE)
  593. {
  594. return HAL_TIMEOUT;
  595. }
  596. }
  597. }
  598. else
  599. {
  600. /* Disable the Internal Low Speed oscillator (HSI48). */
  601. __HAL_RCC_HSI48_DISABLE();
  602. /* Get Start Tick*/
  603. tickstart = HAL_GetTick();
  604. /* Wait till HSI48 is disabled */
  605. while(READ_BIT(RCC->CRRCR, RCC_CRRCR_HSI48RDY) != RESET)
  606. {
  607. if((HAL_GetTick() - tickstart) > HSI48_TIMEOUT_VALUE)
  608. {
  609. return HAL_TIMEOUT;
  610. }
  611. }
  612. }
  613. }
  614. #endif /* RCC_HSI48_SUPPORT */
  615. /*-------------------------------- PLL Configuration -----------------------*/
  616. /* Check the parameters */
  617. assert_param(IS_RCC_PLL(RCC_OscInitStruct->PLL.PLLState));
  618. if(RCC_OscInitStruct->PLL.PLLState != RCC_PLL_NONE)
  619. {
  620. /* Check if the PLL is used as system clock or not */
  621. if(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_PLL)
  622. {
  623. if(RCC_OscInitStruct->PLL.PLLState == RCC_PLL_ON)
  624. {
  625. /* Check the parameters */
  626. assert_param(IS_RCC_PLLSOURCE(RCC_OscInitStruct->PLL.PLLSource));
  627. assert_param(IS_RCC_PLLM_VALUE(RCC_OscInitStruct->PLL.PLLM));
  628. assert_param(IS_RCC_PLLN_VALUE(RCC_OscInitStruct->PLL.PLLN));
  629. assert_param(IS_RCC_PLLP_VALUE(RCC_OscInitStruct->PLL.PLLP));
  630. assert_param(IS_RCC_PLLQ_VALUE(RCC_OscInitStruct->PLL.PLLQ));
  631. assert_param(IS_RCC_PLLR_VALUE(RCC_OscInitStruct->PLL.PLLR));
  632. /* Disable the main PLL. */
  633. __HAL_RCC_PLL_DISABLE();
  634. /* Get Start Tick*/
  635. tickstart = HAL_GetTick();
  636. /* Wait till PLL is ready */
  637. while(READ_BIT(RCC->CR, RCC_CR_PLLRDY) != RESET)
  638. {
  639. if((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  640. {
  641. return HAL_TIMEOUT;
  642. }
  643. }
  644. /* Configure the main PLL clock source, multiplication and division factors. */
  645. __HAL_RCC_PLL_CONFIG(RCC_OscInitStruct->PLL.PLLSource,
  646. RCC_OscInitStruct->PLL.PLLM,
  647. RCC_OscInitStruct->PLL.PLLN,
  648. RCC_OscInitStruct->PLL.PLLP,
  649. RCC_OscInitStruct->PLL.PLLQ,
  650. RCC_OscInitStruct->PLL.PLLR);
  651. /* Enable the main PLL. */
  652. __HAL_RCC_PLL_ENABLE();
  653. /* Enable PLL System Clock output. */
  654. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL_SYSCLK);
  655. /* Get Start Tick*/
  656. tickstart = HAL_GetTick();
  657. /* Wait till PLL is ready */
  658. while(READ_BIT(RCC->CR, RCC_CR_PLLRDY) == RESET)
  659. {
  660. if((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  661. {
  662. return HAL_TIMEOUT;
  663. }
  664. }
  665. }
  666. else
  667. {
  668. /* Disable the main PLL. */
  669. __HAL_RCC_PLL_DISABLE();
  670. /* Disable all PLL outputs to save power if no PLLs on */
  671. if((READ_BIT(RCC->CR, RCC_CR_PLLSAI1RDY) == RESET)
  672. #if defined(RCC_PLLSAI2_SUPPORT)
  673. &&
  674. (READ_BIT(RCC->CR, RCC_CR_PLLSAI2RDY) == RESET)
  675. #endif /* RCC_PLLSAI2_SUPPORT */
  676. )
  677. {
  678. MODIFY_REG(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC, RCC_PLLSOURCE_NONE);
  679. }
  680. #if defined(RCC_PLLSAI2_SUPPORT)
  681. __HAL_RCC_PLLCLKOUT_DISABLE(RCC_PLL_SYSCLK | RCC_PLL_48M1CLK | RCC_PLL_SAI3CLK);
  682. #else
  683. __HAL_RCC_PLLCLKOUT_DISABLE(RCC_PLL_SYSCLK | RCC_PLL_48M1CLK | RCC_PLL_SAI2CLK);
  684. #endif /* RCC_PLLSAI2_SUPPORT */
  685. /* Get Start Tick*/
  686. tickstart = HAL_GetTick();
  687. /* Wait till PLL is disabled */
  688. while(READ_BIT(RCC->CR, RCC_CR_PLLRDY) != RESET)
  689. {
  690. if((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  691. {
  692. return HAL_TIMEOUT;
  693. }
  694. }
  695. }
  696. }
  697. else
  698. {
  699. return HAL_ERROR;
  700. }
  701. }
  702. return HAL_OK;
  703. }
  704. /**
  705. * @brief Initialize the CPU, AHB and APB busses clocks according to the specified
  706. * parameters in the RCC_ClkInitStruct.
  707. * @param RCC_ClkInitStruct pointer to an RCC_OscInitTypeDef structure that
  708. * contains the configuration information for the RCC peripheral.
  709. * @param FLatency FLASH Latency
  710. * This parameter can be one of the following values:
  711. * @arg FLASH_LATENCY_0 FLASH 0 Latency cycle
  712. * @arg FLASH_LATENCY_1 FLASH 1 Latency cycle
  713. * @arg FLASH_LATENCY_2 FLASH 2 Latency cycles
  714. * @arg FLASH_LATENCY_3 FLASH 3 Latency cycles
  715. * @arg FLASH_LATENCY_4 FLASH 4 Latency cycles
  716. *
  717. * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency
  718. * and updated by HAL_RCC_GetHCLKFreq() function called within this function
  719. *
  720. * @note The MSI is used by default as system clock source after
  721. * startup from Reset, wake-up from STANDBY mode. After restart from Reset,
  722. * the MSI frequency is set to its default value 4 MHz.
  723. *
  724. * @note The HSI can be selected as system clock source after
  725. * from STOP modes or in case of failure of the HSE used directly or indirectly
  726. * as system clock (if the Clock Security System CSS is enabled).
  727. *
  728. * @note A switch from one clock source to another occurs only if the target
  729. * clock source is ready (clock stable after startup delay or PLL locked).
  730. * If a clock source which is not yet ready is selected, the switch will
  731. * occur when the clock source is ready.
  732. *
  733. * @note You can use HAL_RCC_GetClockConfig() function to know which clock is
  734. * currently used as system clock source.
  735. *
  736. * @note Depending on the device voltage range, the software has to set correctly
  737. * HPRE[3:0] bits to ensure that HCLK not exceed the maximum allowed frequency
  738. * (for more details refer to section above "Initialization/de-initialization functions")
  739. * @retval None
  740. */
  741. HAL_StatusTypeDef HAL_RCC_ClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t FLatency)
  742. {
  743. uint32_t tickstart = 0;
  744. /* Check the parameters */
  745. assert_param(RCC_ClkInitStruct != NULL);
  746. assert_param(IS_RCC_CLOCKTYPE(RCC_ClkInitStruct->ClockType));
  747. assert_param(IS_FLASH_LATENCY(FLatency));
  748. /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
  749. must be correctly programmed according to the frequency of the CPU clock
  750. (HCLK) and the supply voltage of the device. */
  751. /* Increasing the number of wait states because of higher CPU frequency */
  752. if(FLatency > (FLASH->ACR & FLASH_ACR_LATENCY))
  753. {
  754. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  755. __HAL_FLASH_SET_LATENCY(FLatency);
  756. /* Check that the new number of wait states is taken into account to access the Flash
  757. memory by reading the FLASH_ACR register */
  758. if((FLASH->ACR & FLASH_ACR_LATENCY) != FLatency)
  759. {
  760. return HAL_ERROR;
  761. }
  762. }
  763. /*-------------------------- HCLK Configuration --------------------------*/
  764. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)
  765. {
  766. assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
  767. MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
  768. }
  769. /*------------------------- SYSCLK Configuration ---------------------------*/
  770. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_SYSCLK) == RCC_CLOCKTYPE_SYSCLK)
  771. {
  772. assert_param(IS_RCC_SYSCLKSOURCE(RCC_ClkInitStruct->SYSCLKSource));
  773. /* HSE is selected as System Clock Source */
  774. if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
  775. {
  776. /* Check the HSE ready flag */
  777. if(READ_BIT(RCC->CR, RCC_CR_HSERDY) == RESET)
  778. {
  779. return HAL_ERROR;
  780. }
  781. }
  782. /* PLL is selected as System Clock Source */
  783. else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
  784. {
  785. /* Check the PLL ready flag */
  786. if(READ_BIT(RCC->CR, RCC_CR_PLLRDY) == RESET)
  787. {
  788. return HAL_ERROR;
  789. }
  790. }
  791. /* MSI is selected as System Clock Source */
  792. else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_MSI)
  793. {
  794. /* Check the MSI ready flag */
  795. if(READ_BIT(RCC->CR, RCC_CR_MSIRDY) == RESET)
  796. {
  797. return HAL_ERROR;
  798. }
  799. }
  800. /* HSI is selected as System Clock Source */
  801. else
  802. {
  803. /* Check the HSI ready flag */
  804. if(READ_BIT(RCC->CR, RCC_CR_HSIRDY) == RESET)
  805. {
  806. return HAL_ERROR;
  807. }
  808. }
  809. MODIFY_REG(RCC->CFGR, RCC_CFGR_SW, RCC_ClkInitStruct->SYSCLKSource);
  810. /* Get Start Tick*/
  811. tickstart = HAL_GetTick();
  812. if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
  813. {
  814. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_HSE)
  815. {
  816. if((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
  817. {
  818. return HAL_TIMEOUT;
  819. }
  820. }
  821. }
  822. else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
  823. {
  824. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_PLL)
  825. {
  826. if((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
  827. {
  828. return HAL_TIMEOUT;
  829. }
  830. }
  831. }
  832. else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_MSI)
  833. {
  834. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_MSI)
  835. {
  836. if((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
  837. {
  838. return HAL_TIMEOUT;
  839. }
  840. }
  841. }
  842. else
  843. {
  844. while(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_HSI)
  845. {
  846. if((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
  847. {
  848. return HAL_TIMEOUT;
  849. }
  850. }
  851. }
  852. }
  853. /* Decreasing the number of wait states because of lower CPU frequency */
  854. if(FLatency < (FLASH->ACR & FLASH_ACR_LATENCY))
  855. {
  856. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  857. __HAL_FLASH_SET_LATENCY(FLatency);
  858. /* Check that the new number of wait states is taken into account to access the Flash
  859. memory by reading the FLASH_ACR register */
  860. if((FLASH->ACR & FLASH_ACR_LATENCY) != FLatency)
  861. {
  862. return HAL_ERROR;
  863. }
  864. }
  865. /*-------------------------- PCLK1 Configuration ---------------------------*/
  866. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
  867. {
  868. assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB1CLKDivider));
  869. MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE1, RCC_ClkInitStruct->APB1CLKDivider);
  870. }
  871. /*-------------------------- PCLK2 Configuration ---------------------------*/
  872. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK2) == RCC_CLOCKTYPE_PCLK2)
  873. {
  874. assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB2CLKDivider));
  875. MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE2, ((RCC_ClkInitStruct->APB2CLKDivider) << 3U));
  876. }
  877. /* Update the SystemCoreClock global variable */
  878. SystemCoreClock = HAL_RCC_GetSysClockFreq() >> AHBPrescTable[(RCC->CFGR & RCC_CFGR_HPRE) >> RCC_CFGR_HPRE_Pos];
  879. /* Configure the source of time base considering new system clocks settings*/
  880. HAL_InitTick (TICK_INT_PRIORITY);
  881. return HAL_OK;
  882. }
  883. /**
  884. * @}
  885. */
  886. /** @defgroup RCC_Exported_Functions_Group2 Peripheral Control functions
  887. * @brief RCC clocks control functions
  888. *
  889. @verbatim
  890. ===============================================================================
  891. ##### Peripheral Control functions #####
  892. ===============================================================================
  893. [..]
  894. This subsection provides a set of functions allowing to:
  895. (+) Ouput clock to MCO pin.
  896. (+) Retrieve current clock frequencies.
  897. (+) Enable the Clock Security System.
  898. @endverbatim
  899. * @{
  900. */
  901. /**
  902. * @brief Select the clock source to output on MCO pin(PA8).
  903. * @note PA8 should be configured in alternate function mode.
  904. * @param RCC_MCOx specifies the output direction for the clock source.
  905. * For STM32L4xx family this parameter can have only one value:
  906. * @arg @ref RCC_MCO1 Clock source to output on MCO1 pin(PA8).
  907. * @param RCC_MCOSource specifies the clock source to output.
  908. * This parameter can be one of the following values:
  909. * @arg @ref RCC_MCO1SOURCE_NOCLOCK MCO output disabled, no clock on MCO
  910. * @arg @ref RCC_MCO1SOURCE_SYSCLK system clock selected as MCO source
  911. * @arg @ref RCC_MCO1SOURCE_MSI MSI clock selected as MCO source
  912. * @arg @ref RCC_MCO1SOURCE_HSI HSI clock selected as MCO source
  913. * @arg @ref RCC_MCO1SOURCE_HSE HSE clock selected as MCO sourcee
  914. * @arg @ref RCC_MCO1SOURCE_PLLCLK main PLL clock selected as MCO source
  915. * @arg @ref RCC_MCO1SOURCE_LSI LSI clock selected as MCO source
  916. * @arg @ref RCC_MCO1SOURCE_LSE LSE clock selected as MCO source
  917. @if STM32L443xx
  918. * @arg @ref RCC_MCO1SOURCE_HSI48 HSI48 clock selected as MCO source for devices with HSI48
  919. @endif
  920. * @param RCC_MCODiv specifies the MCO prescaler.
  921. * This parameter can be one of the following values:
  922. * @arg @ref RCC_MCODIV_1 no division applied to MCO clock
  923. * @arg @ref RCC_MCODIV_2 division by 2 applied to MCO clock
  924. * @arg @ref RCC_MCODIV_4 division by 4 applied to MCO clock
  925. * @arg @ref RCC_MCODIV_8 division by 8 applied to MCO clock
  926. * @arg @ref RCC_MCODIV_16 division by 16 applied to MCO clock
  927. * @retval None
  928. */
  929. void HAL_RCC_MCOConfig( uint32_t RCC_MCOx, uint32_t RCC_MCOSource, uint32_t RCC_MCODiv)
  930. {
  931. GPIO_InitTypeDef GPIO_InitStruct;
  932. /* Check the parameters */
  933. assert_param(IS_RCC_MCO(RCC_MCOx));
  934. assert_param(IS_RCC_MCODIV(RCC_MCODiv));
  935. assert_param(IS_RCC_MCO1SOURCE(RCC_MCOSource));
  936. /* MCO Clock Enable */
  937. __MCO1_CLK_ENABLE();
  938. /* Configue the MCO1 pin in alternate function mode */
  939. GPIO_InitStruct.Pin = MCO1_PIN;
  940. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  941. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  942. GPIO_InitStruct.Pull = GPIO_NOPULL;
  943. GPIO_InitStruct.Alternate = GPIO_AF0_MCO;
  944. HAL_GPIO_Init(MCO1_GPIO_PORT, &GPIO_InitStruct);
  945. /* Mask MCOSEL[] and MCOPRE[] bits then set MCO1 clock source and prescaler */
  946. MODIFY_REG(RCC->CFGR, (RCC_CFGR_MCOSEL | RCC_CFGR_MCOPRE), (RCC_MCOSource | RCC_MCODiv ));
  947. }
  948. /**
  949. * @brief Return the SYSCLK frequency.
  950. *
  951. * @note The system frequency computed by this function is not the real
  952. * frequency in the chip. It is calculated based on the predefined
  953. * constant and the selected clock source:
  954. * @note If SYSCLK source is MSI, function returns values based on MSI
  955. * Value as defined by the MSI range.
  956. * @note If SYSCLK source is HSI, function returns values based on HSI_VALUE(*)
  957. * @note If SYSCLK source is HSE, function returns values based on HSE_VALUE(**)
  958. * @note If SYSCLK source is PLL, function returns values based on HSE_VALUE(**),
  959. * HSI_VALUE(*) or MSI Value multiplied/divided by the PLL factors.
  960. * @note (*) HSI_VALUE is a constant defined in stm32l4xx_hal_conf.h file (default value
  961. * 16 MHz) but the real value may vary depending on the variations
  962. * in voltage and temperature.
  963. * @note (**) HSE_VALUE is a constant defined in stm32l4xx_hal_conf.h file (default value
  964. * 8 MHz), user has to ensure that HSE_VALUE is same as the real
  965. * frequency of the crystal used. Otherwise, this function may
  966. * have wrong result.
  967. *
  968. * @note The result of this function could be not correct when using fractional
  969. * value for HSE crystal.
  970. *
  971. * @note This function can be used by the user application to compute the
  972. * baudrate for the communication peripherals or configure other parameters.
  973. *
  974. * @note Each time SYSCLK changes, this function must be called to update the
  975. * right SYSCLK value. Otherwise, any configuration based on this function will be incorrect.
  976. *
  977. *
  978. * @retval SYSCLK frequency
  979. */
  980. uint32_t HAL_RCC_GetSysClockFreq(void)
  981. {
  982. uint32_t msirange = 0U, pllvco = 0U, pllsource = 0U, pllr = 2U, pllm = 2U;
  983. uint32_t sysclockfreq = 0U;
  984. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_MSI) ||
  985. ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL) && (__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_MSI)))
  986. {
  987. /* MSI or PLL with MSI source used as system clock source */
  988. /* Get SYSCLK source */
  989. if(READ_BIT(RCC->CR, RCC_CR_MSIRGSEL) == RESET)
  990. { /* MSISRANGE from RCC_CSR applies */
  991. msirange = (RCC->CSR & RCC_CSR_MSISRANGE) >> RCC_CSR_MSISRANGE_Pos;
  992. }
  993. else
  994. { /* MSIRANGE from RCC_CR applies */
  995. msirange = (RCC->CR & RCC_CR_MSIRANGE) >> RCC_CR_MSIRANGE_Pos;
  996. }
  997. /*MSI frequency range in HZ*/
  998. msirange = MSIRangeTable[msirange];
  999. if(__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_MSI)
  1000. {
  1001. /* MSI used as system clock source */
  1002. sysclockfreq = msirange;
  1003. }
  1004. }
  1005. else if(__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSI)
  1006. {
  1007. /* HSI used as system clock source */
  1008. sysclockfreq = HSI_VALUE;
  1009. }
  1010. else if(__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSE)
  1011. {
  1012. /* HSE used as system clock source */
  1013. sysclockfreq = HSE_VALUE;
  1014. }
  1015. if(__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL)
  1016. {
  1017. /* PLL used as system clock source */
  1018. /* PLL_VCO = (HSE_VALUE or HSI_VALUE or MSI_VALUE/ PLLM) * PLLN
  1019. SYSCLK = PLL_VCO / PLLR
  1020. */
  1021. pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC);
  1022. pllm = ((RCC->PLLCFGR & RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1U ;
  1023. switch (pllsource)
  1024. {
  1025. case RCC_PLLSOURCE_HSI: /* HSI used as PLL clock source */
  1026. pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos);
  1027. break;
  1028. case RCC_PLLSOURCE_HSE: /* HSE used as PLL clock source */
  1029. pllvco = (HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos);
  1030. break;
  1031. case RCC_PLLSOURCE_MSI: /* MSI used as PLL clock source */
  1032. default:
  1033. pllvco = (msirange / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos);
  1034. break;
  1035. }
  1036. pllr = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> RCC_PLLCFGR_PLLR_Pos) + 1U ) * 2U;
  1037. sysclockfreq = pllvco/pllr;
  1038. }
  1039. return sysclockfreq;
  1040. }
  1041. /**
  1042. * @brief Return the HCLK frequency.
  1043. * @note Each time HCLK changes, this function must be called to update the
  1044. * right HCLK value. Otherwise, any configuration based on this function will be incorrect.
  1045. *
  1046. * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency.
  1047. * @retval HCLK frequency in Hz
  1048. */
  1049. uint32_t HAL_RCC_GetHCLKFreq(void)
  1050. {
  1051. return SystemCoreClock;
  1052. }
  1053. /**
  1054. * @brief Return the PCLK1 frequency.
  1055. * @note Each time PCLK1 changes, this function must be called to update the
  1056. * right PCLK1 value. Otherwise, any configuration based on this function will be incorrect.
  1057. * @retval PCLK1 frequency in Hz
  1058. */
  1059. uint32_t HAL_RCC_GetPCLK1Freq(void)
  1060. {
  1061. /* Get HCLK source and Compute PCLK1 frequency ---------------------------*/
  1062. return (HAL_RCC_GetHCLKFreq() >> APBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE1) >> RCC_CFGR_PPRE1_Pos]);
  1063. }
  1064. /**
  1065. * @brief Return the PCLK2 frequency.
  1066. * @note Each time PCLK2 changes, this function must be called to update the
  1067. * right PCLK2 value. Otherwise, any configuration based on this function will be incorrect.
  1068. * @retval PCLK2 frequency in Hz
  1069. */
  1070. uint32_t HAL_RCC_GetPCLK2Freq(void)
  1071. {
  1072. /* Get HCLK source and Compute PCLK2 frequency ---------------------------*/
  1073. return (HAL_RCC_GetHCLKFreq()>> APBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE2) >> RCC_CFGR_PPRE2_Pos]);
  1074. }
  1075. /**
  1076. * @brief Configure the RCC_OscInitStruct according to the internal
  1077. * RCC configuration registers.
  1078. * @param RCC_OscInitStruct pointer to an RCC_OscInitTypeDef structure that
  1079. * will be configured.
  1080. * @retval None
  1081. */
  1082. void HAL_RCC_GetOscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  1083. {
  1084. /* Check the parameters */
  1085. assert_param(RCC_OscInitStruct != NULL);
  1086. /* Set all possible values for the Oscillator type parameter ---------------*/
  1087. #if defined(RCC_HSI48_SUPPORT)
  1088. RCC_OscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_MSI | \
  1089. RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_HSI48;
  1090. #else
  1091. RCC_OscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_MSI | \
  1092. RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_LSI;
  1093. #endif /* RCC_HSI48_SUPPORT */
  1094. /* Get the HSE configuration -----------------------------------------------*/
  1095. if((RCC->CR & RCC_CR_HSEBYP) == RCC_CR_HSEBYP)
  1096. {
  1097. RCC_OscInitStruct->HSEState = RCC_HSE_BYPASS;
  1098. }
  1099. else if((RCC->CR & RCC_CR_HSEON) == RCC_CR_HSEON)
  1100. {
  1101. RCC_OscInitStruct->HSEState = RCC_HSE_ON;
  1102. }
  1103. else
  1104. {
  1105. RCC_OscInitStruct->HSEState = RCC_HSE_OFF;
  1106. }
  1107. /* Get the MSI configuration -----------------------------------------------*/
  1108. if((RCC->CR & RCC_CR_MSION) == RCC_CR_MSION)
  1109. {
  1110. RCC_OscInitStruct->MSIState = RCC_MSI_ON;
  1111. }
  1112. else
  1113. {
  1114. RCC_OscInitStruct->MSIState = RCC_MSI_OFF;
  1115. }
  1116. RCC_OscInitStruct->MSICalibrationValue = (uint32_t)((RCC->ICSCR & RCC_ICSCR_MSITRIM) >> RCC_ICSCR_MSITRIM_Pos);
  1117. RCC_OscInitStruct->MSIClockRange = (uint32_t)((RCC->CR & RCC_CR_MSIRANGE) );
  1118. /* Get the HSI configuration -----------------------------------------------*/
  1119. if((RCC->CR & RCC_CR_HSION) == RCC_CR_HSION)
  1120. {
  1121. RCC_OscInitStruct->HSIState = RCC_HSI_ON;
  1122. }
  1123. else
  1124. {
  1125. RCC_OscInitStruct->HSIState = RCC_HSI_OFF;
  1126. }
  1127. RCC_OscInitStruct->HSICalibrationValue = (uint32_t)((RCC->ICSCR & RCC_ICSCR_HSITRIM) >> RCC_ICSCR_HSITRIM_Pos);
  1128. /* Get the LSE configuration -----------------------------------------------*/
  1129. if((RCC->BDCR & RCC_BDCR_LSEBYP) == RCC_BDCR_LSEBYP)
  1130. {
  1131. RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS;
  1132. }
  1133. else if((RCC->BDCR & RCC_BDCR_LSEON) == RCC_BDCR_LSEON)
  1134. {
  1135. RCC_OscInitStruct->LSEState = RCC_LSE_ON;
  1136. }
  1137. else
  1138. {
  1139. RCC_OscInitStruct->LSEState = RCC_LSE_OFF;
  1140. }
  1141. /* Get the LSI configuration -----------------------------------------------*/
  1142. if((RCC->CSR & RCC_CSR_LSION) == RCC_CSR_LSION)
  1143. {
  1144. RCC_OscInitStruct->LSIState = RCC_LSI_ON;
  1145. }
  1146. else
  1147. {
  1148. RCC_OscInitStruct->LSIState = RCC_LSI_OFF;
  1149. }
  1150. #if defined(RCC_HSI48_SUPPORT)
  1151. /* Get the HSI48 configuration ---------------------------------------------*/
  1152. if((RCC->CRRCR & RCC_CRRCR_HSI48ON) == RCC_CRRCR_HSI48ON)
  1153. {
  1154. RCC_OscInitStruct->HSI48State = RCC_HSI48_ON;
  1155. }
  1156. else
  1157. {
  1158. RCC_OscInitStruct->HSI48State = RCC_HSI48_OFF;
  1159. }
  1160. #else
  1161. RCC_OscInitStruct->HSI48State = RCC_HSI48_OFF;
  1162. #endif /* RCC_HSI48_SUPPORT */
  1163. /* Get the PLL configuration -----------------------------------------------*/
  1164. if((RCC->CR & RCC_CR_PLLON) == RCC_CR_PLLON)
  1165. {
  1166. RCC_OscInitStruct->PLL.PLLState = RCC_PLL_ON;
  1167. }
  1168. else
  1169. {
  1170. RCC_OscInitStruct->PLL.PLLState = RCC_PLL_OFF;
  1171. }
  1172. RCC_OscInitStruct->PLL.PLLSource = (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC);
  1173. RCC_OscInitStruct->PLL.PLLM = (uint32_t)(((RCC->PLLCFGR & RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1U);
  1174. RCC_OscInitStruct->PLL.PLLN = (uint32_t)((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos);
  1175. RCC_OscInitStruct->PLL.PLLQ = (uint32_t)((((RCC->PLLCFGR & RCC_PLLCFGR_PLLQ) >> RCC_PLLCFGR_PLLQ_Pos) + 1U) << 1U);
  1176. RCC_OscInitStruct->PLL.PLLR = (uint32_t)((((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> RCC_PLLCFGR_PLLR_Pos) + 1U) << 1U);
  1177. #if defined(RCC_PLLP_DIV_2_31_SUPPORT)
  1178. RCC_OscInitStruct->PLL.PLLP = (uint32_t)((RCC->PLLCFGR & RCC_PLLCFGR_PLLPDIV) >> RCC_PLLCFGR_PLLPDIV_Pos);
  1179. #else
  1180. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) != RESET)
  1181. {
  1182. RCC_OscInitStruct->PLL.PLLP = RCC_PLLP_DIV17;
  1183. }
  1184. else
  1185. {
  1186. RCC_OscInitStruct->PLL.PLLP = RCC_PLLP_DIV7;
  1187. }
  1188. #endif /* RCC_PLLP_DIV_2_31_SUPPORT */
  1189. }
  1190. /**
  1191. * @brief Configure the RCC_ClkInitStruct according to the internal
  1192. * RCC configuration registers.
  1193. * @param RCC_ClkInitStruct pointer to an RCC_ClkInitTypeDef structure that
  1194. * will be configured.
  1195. * @param pFLatency Pointer on the Flash Latency.
  1196. * @retval None
  1197. */
  1198. void HAL_RCC_GetClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t *pFLatency)
  1199. {
  1200. /* Check the parameters */
  1201. assert_param(RCC_ClkInitStruct != NULL);
  1202. assert_param(pFLatency != NULL);
  1203. /* Set all possible values for the Clock type parameter --------------------*/
  1204. RCC_ClkInitStruct->ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  1205. /* Get the SYSCLK configuration --------------------------------------------*/
  1206. RCC_ClkInitStruct->SYSCLKSource = (uint32_t)(RCC->CFGR & RCC_CFGR_SW);
  1207. /* Get the HCLK configuration ----------------------------------------------*/
  1208. RCC_ClkInitStruct->AHBCLKDivider = (uint32_t)(RCC->CFGR & RCC_CFGR_HPRE);
  1209. /* Get the APB1 configuration ----------------------------------------------*/
  1210. RCC_ClkInitStruct->APB1CLKDivider = (uint32_t)(RCC->CFGR & RCC_CFGR_PPRE1);
  1211. /* Get the APB2 configuration ----------------------------------------------*/
  1212. RCC_ClkInitStruct->APB2CLKDivider = (uint32_t)((RCC->CFGR & RCC_CFGR_PPRE2) >> 3U);
  1213. /* Get the Flash Wait State (Latency) configuration ------------------------*/
  1214. *pFLatency = (uint32_t)(FLASH->ACR & FLASH_ACR_LATENCY);
  1215. }
  1216. /**
  1217. * @brief Enable the Clock Security System.
  1218. * @note If a failure is detected on the HSE oscillator clock, this oscillator
  1219. * is automatically disabled and an interrupt is generated to inform the
  1220. * software about the failure (Clock Security System Interrupt, CSSI),
  1221. * allowing the MCU to perform rescue operations. The CSSI is linked to
  1222. * the Cortex-M4 NMI (Non-Maskable Interrupt) exception vector.
  1223. * @note The Clock Security System can only be cleared by reset.
  1224. * @retval None
  1225. */
  1226. void HAL_RCC_EnableCSS(void)
  1227. {
  1228. SET_BIT(RCC->CR, RCC_CR_CSSON) ;
  1229. }
  1230. /**
  1231. * @brief Handle the RCC Clock Security System interrupt request.
  1232. * @note This API should be called under the NMI_Handler().
  1233. * @retval None
  1234. */
  1235. void HAL_RCC_NMI_IRQHandler(void)
  1236. {
  1237. /* Check RCC CSSF interrupt flag */
  1238. if(__HAL_RCC_GET_IT(RCC_IT_CSS))
  1239. {
  1240. /* RCC Clock Security System interrupt user callback */
  1241. HAL_RCC_CSSCallback();
  1242. /* Clear RCC CSS pending bit */
  1243. __HAL_RCC_CLEAR_IT(RCC_IT_CSS);
  1244. }
  1245. }
  1246. /**
  1247. * @brief RCC Clock Security System interrupt callback.
  1248. * @retval none
  1249. */
  1250. __weak void HAL_RCC_CSSCallback(void)
  1251. {
  1252. /* NOTE : This function should not be modified, when the callback is needed,
  1253. the HAL_RCC_CSSCallback should be implemented in the user file
  1254. */
  1255. }
  1256. /**
  1257. * @}
  1258. */
  1259. /**
  1260. * @}
  1261. */
  1262. /* Private function prototypes -----------------------------------------------*/
  1263. /** @addtogroup RCC_Private_Functions
  1264. * @{
  1265. */
  1266. /**
  1267. * @brief Update number of Flash wait states in line with MSI range and current
  1268. voltage range.
  1269. * @param msirange MSI range value from RCC_MSIRANGE_0 to RCC_MSIRANGE_11
  1270. * @retval HAL status
  1271. */
  1272. static HAL_StatusTypeDef RCC_SetFlashLatencyFromMSIRange(uint32_t msirange)
  1273. {
  1274. uint32_t vos = 0;
  1275. uint32_t latency = FLASH_LATENCY_0; /* default value 0WS */
  1276. if(__HAL_RCC_PWR_IS_CLK_ENABLED())
  1277. {
  1278. vos = HAL_PWREx_GetVoltageRange();
  1279. }
  1280. else
  1281. {
  1282. __HAL_RCC_PWR_CLK_ENABLE();
  1283. vos = HAL_PWREx_GetVoltageRange();
  1284. __HAL_RCC_PWR_CLK_DISABLE();
  1285. }
  1286. if(vos == PWR_REGULATOR_VOLTAGE_SCALE1)
  1287. {
  1288. if(msirange > RCC_MSIRANGE_8)
  1289. {
  1290. /* MSI > 16Mhz */
  1291. if(msirange > RCC_MSIRANGE_10)
  1292. {
  1293. /* MSI 48Mhz */
  1294. latency = FLASH_LATENCY_2; /* 2WS */
  1295. }
  1296. else
  1297. {
  1298. /* MSI 24Mhz or 32Mhz */
  1299. latency = FLASH_LATENCY_1; /* 1WS */
  1300. }
  1301. }
  1302. /* else MSI <= 16Mhz default FLASH_LATENCY_0 0WS */
  1303. }
  1304. else
  1305. {
  1306. if(msirange > RCC_MSIRANGE_8)
  1307. {
  1308. /* MSI > 16Mhz */
  1309. latency = FLASH_LATENCY_3; /* 3WS */
  1310. }
  1311. else
  1312. {
  1313. if(msirange == RCC_MSIRANGE_8)
  1314. {
  1315. /* MSI 16Mhz */
  1316. latency = FLASH_LATENCY_2; /* 2WS */
  1317. }
  1318. else if(msirange == RCC_MSIRANGE_7)
  1319. {
  1320. /* MSI 8Mhz */
  1321. latency = FLASH_LATENCY_1; /* 1WS */
  1322. }
  1323. /* else MSI < 8Mhz default FLASH_LATENCY_0 0WS */
  1324. }
  1325. }
  1326. __HAL_FLASH_SET_LATENCY(latency);
  1327. /* Check that the new number of wait states is taken into account to access the Flash
  1328. memory by reading the FLASH_ACR register */
  1329. if((FLASH->ACR & FLASH_ACR_LATENCY) != latency)
  1330. {
  1331. return HAL_ERROR;
  1332. }
  1333. return HAL_OK;
  1334. }
  1335. /**
  1336. * @}
  1337. */
  1338. #endif /* HAL_RCC_MODULE_ENABLED */
  1339. /**
  1340. * @}
  1341. */
  1342. /**
  1343. * @}
  1344. */
  1345. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/