stm32l5xx_hal_rcc.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047
  1. /**
  2. ******************************************************************************
  3. * @file stm32l5xx_hal_rcc.c
  4. * @author MCD Application Team
  5. * @brief RCC HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Reset and Clock Control (RCC) peripheral:
  8. * + Initialization and de-initialization functions
  9. * + Peripheral Control functions
  10. *
  11. ******************************************************************************
  12. * @attention
  13. *
  14. * Copyright (c) 2019 STMicroelectronics.
  15. * All rights reserved.
  16. *
  17. * This software is licensed under terms that can be found in the LICENSE file in
  18. * the root directory of this software component.
  19. * If no LICENSE file comes with this software, it is provided AS-IS.
  20. ******************************************************************************
  21. @verbatim
  22. ==============================================================================
  23. ##### RCC specific features #####
  24. ==============================================================================
  25. [..]
  26. After reset the device is running from Multiple Speed Internal oscillator
  27. (4 MHz) with Flash 0 wait state. I-Cache is disabled, and all peripherals
  28. are off except internal SRAMs, Flash and JTAG.
  29. (+) There is no prescaler on High speed (AHBs) and Low speed (APBs) buses:
  30. all peripherals mapped on these buses are running at MSI speed.
  31. (+) The clock for all peripherals is switched off, except the SRAM and FLASH.
  32. (+) All GPIOs are in analog mode, except the JTAG pins which
  33. are assigned to be used for debug purpose.
  34. [..]
  35. Once the device is started from reset, the user application has to:
  36. (+) Configure the clock source to be used to drive the System clock
  37. (if the application needs higher frequency/performance)
  38. (+) Configure the System clock frequency and Flash settings
  39. (+) Configure the AHB and APB buses prescalers
  40. (+) Enable the clock for the peripheral(s) to be used
  41. (+) Configure the clock source(s) for peripherals which clocks are not
  42. derived from the System clock (SAIx, RTC, ADC, USB FS/SDMMC1/RNG, FDCAN)
  43. @endverbatim
  44. ******************************************************************************
  45. */
  46. /* Includes ------------------------------------------------------------------*/
  47. #include "stm32l5xx_hal.h"
  48. /** @addtogroup STM32L5xx_HAL_Driver
  49. * @{
  50. */
  51. /** @defgroup RCC RCC
  52. * @brief RCC HAL module driver
  53. * @{
  54. */
  55. #ifdef HAL_RCC_MODULE_ENABLED
  56. /* Private typedef -----------------------------------------------------------*/
  57. /* Private define ------------------------------------------------------------*/
  58. /** @defgroup RCC_Private_Constants RCC Private Constants
  59. * @{
  60. */
  61. #define LSI_TIMEOUT_VALUE 7UL /* 7 ms (maximum 6ms + 1) */
  62. #define HSI48_TIMEOUT_VALUE 2UL /* 2 ms (minimum Tick + 1) */
  63. #define PLL_TIMEOUT_VALUE 2UL /* 2 ms (minimum Tick + 1) */
  64. #define CLOCKSWITCH_TIMEOUT_VALUE 5000UL /* 5 s */
  65. /**
  66. * @}
  67. */
  68. /* Private macro -------------------------------------------------------------*/
  69. /** @defgroup RCC_Private_Macros RCC Private Macros
  70. * @{
  71. */
  72. #define __MCO1_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
  73. #define MCO1_GPIO_PORT GPIOA
  74. #define MCO1_PIN GPIO_PIN_8
  75. /**
  76. * @}
  77. */
  78. /* Private variables ---------------------------------------------------------*/
  79. /* Private function prototypes -----------------------------------------------*/
  80. /** @defgroup RCC_Private_Functions RCC Private Functions
  81. * @{
  82. */
  83. static HAL_StatusTypeDef RCC_SetFlashLatencyFromMSIRange(uint32_t msirange);
  84. static uint32_t RCC_GetSysClockFreqFromPLLSource(void);
  85. /**
  86. * @}
  87. */
  88. /* Exported functions --------------------------------------------------------*/
  89. /** @defgroup RCC_Exported_Functions RCC Exported Functions
  90. * @{
  91. */
  92. /** @defgroup RCC_Exported_Functions_Group1 Initialization and de-initialization functions
  93. * @brief Initialization and Configuration functions
  94. *
  95. @verbatim
  96. ===============================================================================
  97. ##### Initialization and de-initialization functions #####
  98. ===============================================================================
  99. [..]
  100. This section provides functions allowing to configure the internal and external oscillators
  101. (HSE, HSI, LSE, MSI, LSI, PLL, CSS and MCO) and the System buses clocks (SYSCLK, AHB, APB1
  102. and APB2).
  103. [..] Internal/external clock and PLL configuration
  104. (+) HSI (high-speed internal): 16 MHz factory-trimmed RC used directly or through
  105. the PLL as System clock source.
  106. (+) MSI (Multiple Speed Internal): Its frequency is software trimmable from 100KHz to 48MHz.
  107. It can be used to generate the clock for the USB FS (48 MHz).
  108. The number of flash wait states is automatically adjusted when MSI range is updated with
  109. HAL_RCC_OscConfig() and the MSI is used as System clock source.
  110. (+) LSI (low-speed internal): 32 KHz low consumption RC used as IWDG and/or RTC
  111. clock source.
  112. (+) HSE (high-speed external): 4 to 48 MHz crystal oscillator used directly or
  113. through the PLL as System clock source. Can be used also optionally as RTC clock source.
  114. (+) LSE (low-speed external): 32.768 KHz oscillator used optionally as RTC clock source.
  115. (+) PLL (clocked by HSI, HSE or MSI) providing up to three independent output clocks:
  116. (++) The first output is used to generate the high speed system clock (up to 110 MHz).
  117. (++) The second output is used to generate the clock for the USB FS (48 MHz),
  118. the random analog generator (<=48 MHz) and the SDMMC1 (<= 48 MHz).
  119. (++) The third output is used to generate an accurate clock to achieve
  120. high-quality audio performance on SAI interface.
  121. (+) PLLSAI1 (clocked by HSI, HSE or MSI) providing up to three independent output clocks:
  122. (++) The first output is used to generate the ADCs clock.
  123. (++) The second output is used to generate the clock for the USB FS (48 MHz),
  124. the random analog generator (<=48 MHz) and the SDMMC1 (<= 48 MHz).
  125. (++) The third output is used to generate an accurate clock to achieve
  126. high-quality audio performance on SAI interface.
  127. (+) PLLSAI2 (clocked by HSI, HSE or MSI) providing an independent output clock:
  128. (++) The output is used to generate an accurate clock to achieve
  129. high-quality audio performance on SAI interface.
  130. (+) CSS (Clock security system): once enabled, if a HSE clock failure occurs
  131. (HSE used directly or through PLL as System clock source), the System clock
  132. is automatically switched to HSI and an interrupt is generated.
  133. The interrupt is linked to the Cortex-M33 NMI (non-maskable interrupt)
  134. exception vector.
  135. (+) CSS on LSE (Clock security system on LSE): once enabled for RTC, if a LSE clock
  136. failure occurs it is not supplied anymore to the RTC. If the MSI was used in
  137. PLL-mode, this mode is disabled. The CSS on LSE failure is detected by a tamper event.
  138. (+) MCO (microcontroller clock output): used to output LSI, LSE, System clock, HSI, HSI48,
  139. HSE, main PLL clock or MSI (through a configurable prescaler) on PA8 pin.
  140. [..] System, AHB and APB buses clocks configuration
  141. (+) Several clock sources can be used to drive the System clock (SYSCLK): MSI, HSI,
  142. HSE and main PLL.
  143. The AHB clock (HCLK) is derived from System clock through configurable
  144. prescaler and used to clock the CPU, memory and peripherals mapped
  145. on AHB bus (DMA, GPIO...). APB1 (PCLK1) and APB2 (PCLK2) clocks are derived
  146. from AHB clock through configurable prescalers and used to clock
  147. the peripherals mapped on these buses. You can use
  148. "HAL_RCC_GetSysClockFreq()" function to retrieve the frequencies of these clocks.
  149. -@- All the peripheral clocks are derived from the System clock (SYSCLK) except:
  150. (+@) SAI: the SAI clock can be derived either from a specific PLL (PLLSAI1) or (PLLSAI2) or
  151. from an external clock mapped on the SAI_CKIN pin.
  152. You have to use HAL_RCCEx_PeriphCLKConfig() function to configure this clock.
  153. (+@) RTC: the RTC clock can be derived either from the LSI, LSE or HSE clock
  154. divided by 2 to 31.
  155. You have to use __HAL_RCC_RTC_ENABLE() and HAL_RCCEx_PeriphCLKConfig() function
  156. to configure this clock.
  157. (+@) USB FS, SDMMC1 and RNG: USB FS requires a frequency equal to 48 MHz
  158. to work correctly, while the SDMMC1 and RNG peripherals require a frequency
  159. equal or lower than to 48 MHz. This clock is derived of the main PLL or PLLSAI1
  160. through PLLQ divider. You have to enable the peripheral clock and use
  161. HAL_RCCEx_PeriphCLKConfig() function to configure this clock.
  162. (+@) IWDG clock which is always the LSI clock.
  163. (+) The maximum frequency of the SYSCLK, HCLK, PCLK1 and PCLK2 is 110 MHz.
  164. The clock source frequency should be adapted depending on the device voltage range
  165. as listed in the Reference Manual "Clock source frequency versus voltage scaling" chapter.
  166. @endverbatim
  167. @internal
  168. Depending on the device voltage range, the maximum frequency should be
  169. adapted accordingly:
  170. (++) Table 1. HCLK clock frequency for STM32L5 devices
  171. (++) +---------------------------------------------------------------------------+
  172. (++) | Latency | HCLK clock frequency (MHz) |
  173. (++) | |---------------------------------------------------------|
  174. (++) | | voltage range 0 | voltage range 1 | voltage range 2 |
  175. (++) |-----------------|-------------------|------------------|------------------|
  176. (++) |0WS(1 CPU cycles)| 0 < HCLK <= 20 | 0 < HCLK <= 8 | 0 < HCLK <= 8 |
  177. (++) |-----------------|-------------------|------------------|------------------|
  178. (++) |1WS(2 CPU cycles)| 20 < HCLK <= 40 | 20 < HCLK <= 40 | 8 < HCLK <= 16 |
  179. (++) |-----------------|-------------------|------------------|------------------|
  180. (++) |2WS(3 CPU cycles)| 40 < HCLK <= 60 | 40 < HCLK <= 60 | 16 < HCLK <= 26 |
  181. (++) |-----------------|-------------------|------------------|------------------|
  182. (++) |3WS(4 CPU cycles)| 60 < HCLK <= 80 | 60 < HCLK <= 80 | |
  183. (++) |-----------------|-------------------|------------------|------------------|
  184. (++) |4WS(5 CPU cycles)| 80 < HCLK <= 100 | | |
  185. (++) |-----------------|-------------------|------------------|------------------|
  186. (++) |5WS(6 CPU cycles)| 100 < HCLK <= 110 | | |
  187. (++) +---------------------------------------------------------------------------+
  188. @endinternal
  189. * @{
  190. */
  191. /**
  192. * @brief Reset the RCC clock configuration to the default reset state.
  193. * @note The default reset state of the clock configuration is given below:
  194. * - MSI ON and used as system clock source
  195. * - HSE, HSI, HSI48, LSI, LSE, PLL, PLLSAI1 and PLLISAI2 OFF
  196. * - AHB, APB1 and APB2 prescaler set to 1.
  197. * - CSS, MCO1 OFF
  198. * - All interrupts disabled
  199. * - All interrupt and reset flags cleared
  200. * @note This function doesn't modify the configuration of the
  201. * - Peripheral clocks source selection
  202. * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency
  203. * and is updated by this function
  204. * @retval HAL status
  205. */
  206. HAL_StatusTypeDef HAL_RCC_DeInit(void)
  207. {
  208. uint32_t tickstart;
  209. FlagStatus pwrclkchanged = RESET;
  210. /* Set MSION bit */
  211. SET_BIT(RCC->CR, RCC_CR_MSION);
  212. /* Insure MSIRDY bit is set before writing default MSIRANGE value */
  213. /* Get start tick */
  214. tickstart = HAL_GetTick();
  215. /* Wait till MSI is ready */
  216. while (READ_BIT(RCC->CR, RCC_CR_MSIRDY) == 0U)
  217. {
  218. if ((HAL_GetTick() - tickstart) > MSI_TIMEOUT_VALUE)
  219. {
  220. /* New check to avoid false timeout detection in case of preemption */
  221. if (READ_BIT(RCC->CR, RCC_CR_MSIRDY) == 0U)
  222. {
  223. return HAL_TIMEOUT;
  224. }
  225. }
  226. }
  227. /* Set MSIRANGE default value */
  228. MODIFY_REG(RCC->CR, RCC_CR_MSIRANGE, RCC_MSIRANGE_6);
  229. /* Reset CFGR register (MSI is selected as system clock source) */
  230. CLEAR_REG(RCC->CFGR);
  231. /* Insure MSI selected as system clock source */
  232. /* Get start tick */
  233. tickstart = HAL_GetTick();
  234. /* Update the SystemCoreClock global variable for MSI as system clock source */
  235. SystemCoreClock = MSI_VALUE;
  236. /* Configure the source of time base considering new system clock settings */
  237. if (HAL_InitTick(uwTickPrio) != HAL_OK)
  238. {
  239. return HAL_ERROR;
  240. }
  241. /* Wait till system clock source is ready */
  242. while (READ_BIT(RCC->CFGR, RCC_CFGR_SWS) != RCC_SYSCLKSOURCE_STATUS_MSI)
  243. {
  244. if ((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
  245. {
  246. /* New check to avoid false timeout detection in case of preemption */
  247. if (READ_BIT(RCC->CFGR, RCC_CFGR_SWS) != RCC_SYSCLKSOURCE_STATUS_MSI)
  248. {
  249. return HAL_TIMEOUT;
  250. }
  251. }
  252. }
  253. /* Reset HSION, HSIKERON, HSIASFS, HSEON, HSECSSON, PLLON, PLLSAIxON bits */
  254. CLEAR_BIT(RCC->CR, RCC_CR_CSSON | RCC_CR_HSEON | RCC_CR_HSION | RCC_CR_HSIKERON | RCC_CR_HSIASFS | RCC_CR_PLLON |
  255. RCC_CR_PLLSAI1ON | RCC_CR_PLLSAI2ON);
  256. /* Insure PLLRDY, PLLSAI1RDY and PLLSAI2RDY (if present) are reset */
  257. /* Get start tick */
  258. tickstart = HAL_GetTick();
  259. #if defined(RCC_PLLSAI2_SUPPORT)
  260. while (READ_BIT(RCC->CR, RCC_CR_PLLRDY | RCC_CR_PLLSAI1RDY | RCC_CR_PLLSAI2RDY) != 0U)
  261. {
  262. if ((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  263. {
  264. /* New check to avoid false timeout detection in case of preemption */
  265. if (READ_BIT(RCC->CR, RCC_CR_PLLRDY | RCC_CR_PLLSAI1RDY | RCC_CR_PLLSAI2RDY) != 0U)
  266. {
  267. return HAL_TIMEOUT;
  268. }
  269. }
  270. }
  271. #else
  272. while (READ_BIT(RCC->CR, RCC_CR_PLLRDY | RCC_CR_PLLSAI1RDY) != 0U)
  273. {
  274. if ((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  275. {
  276. /* New check to avoid false timeout detection in case of preemption */
  277. if (READ_BIT(RCC->CR, RCC_CR_PLLRDY | RCC_CR_PLLSAI1RDY) != 0U)
  278. {
  279. return HAL_TIMEOUT;
  280. }
  281. }
  282. }
  283. #endif /* RCC_PLLSAI2_SUPPORT */
  284. /* Reset PLLCFGR register */
  285. CLEAR_REG(RCC->PLLCFGR);
  286. SET_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN_4);
  287. /* Reset PLLSAI1CFGR register */
  288. CLEAR_REG(RCC->PLLSAI1CFGR);
  289. SET_BIT(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLSAI1N_4);
  290. /* Reset PLLSAI2CFGR register */
  291. CLEAR_REG(RCC->PLLSAI2CFGR);
  292. SET_BIT(RCC->PLLSAI2CFGR, RCC_PLLSAI2CFGR_PLLSAI2N_4);
  293. /* Reset LSION bit */
  294. CLEAR_BIT(RCC->CSR, RCC_CSR_LSION);
  295. /* Insure LSIRDY bit is reset before LSIPRE bit reset */
  296. /* Get start tick */
  297. tickstart = HAL_GetTick();
  298. /* Wait till LSI is disabled */
  299. while (READ_BIT(RCC->CSR, RCC_CSR_LSIRDY) != 0U)
  300. {
  301. if ((HAL_GetTick() - tickstart) > LSI_TIMEOUT_VALUE)
  302. {
  303. /* New check to avoid false timeout detection in case of preemption */
  304. if (READ_BIT(RCC->CSR, RCC_CSR_LSIRDY) != 0U)
  305. {
  306. return HAL_TIMEOUT;
  307. }
  308. }
  309. }
  310. /* Reset LSIPRE bit */
  311. CLEAR_BIT(RCC->CSR, RCC_CSR_LSIPRE);
  312. /* Reset HSI48ON bit */
  313. CLEAR_BIT(RCC->CRRCR, RCC_CRRCR_HSI48ON);
  314. /* Reset HSEBYP bit */
  315. CLEAR_BIT(RCC->CR, RCC_CR_HSEBYP);
  316. /* Disable all interrupts */
  317. CLEAR_REG(RCC->CIER);
  318. /* Clear all interrupt flags */
  319. WRITE_REG(RCC->CICR, 0xFFFFFFFFU);
  320. /* Clear all reset flags */
  321. SET_BIT(RCC->CSR, RCC_CSR_RMVF);
  322. /* Reset LSEON/LSESYSON/LSEBYP in Backup domain register */
  323. /* Requires to enable write access to Backup Domain if necessary */
  324. if (HAL_IS_BIT_CLR(RCC->APB1ENR1, RCC_APB1ENR1_PWREN))
  325. {
  326. __HAL_RCC_PWR_CLK_ENABLE();
  327. pwrclkchanged = SET;
  328. }
  329. if (HAL_IS_BIT_CLR(PWR->CR1, PWR_CR1_DBP))
  330. {
  331. /* Enable write access to Backup domain */
  332. SET_BIT(PWR->CR1, PWR_CR1_DBP);
  333. }
  334. /* Reset LSEON/LSEBYP/LSESYSEN bit */
  335. CLEAR_BIT(RCC->BDCR, RCC_BDCR_LSEON);
  336. CLEAR_BIT(RCC->BDCR, RCC_BDCR_LSEBYP | RCC_BDCR_LSESYSEN);
  337. /* Restore clock configuration if changed */
  338. if (pwrclkchanged == SET)
  339. {
  340. __HAL_RCC_PWR_CLK_DISABLE();
  341. }
  342. return HAL_OK;
  343. }
  344. /**
  345. * @brief Initialize the RCC Oscillators according to the specified parameters in the
  346. * RCC_OscInitTypeDef.
  347. * @param RCC_OscInitStruct pointer to an RCC_OscInitTypeDef structure that
  348. * contains the configuration information for the RCC Oscillators.
  349. * @note The PLL is not disabled when used as system clock.
  350. * @note Transitions LSE Bypass to LSE On and LSE On to LSE Bypass are not
  351. * supported by this macro. User should request a transition to LSE Off
  352. * first and then LSE On or LSE Bypass.
  353. * @note Transition HSE Bypass to HSE On and HSE On to HSE Bypass are not
  354. * supported by this macro. User should request a transition to HSE Off
  355. * first and then HSE On or HSE Bypass.
  356. * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency
  357. * and is updated by this function in case of simple MSI range update when MSI
  358. * used as system clock.
  359. * @retval HAL status
  360. */
  361. HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  362. {
  363. uint32_t tickstart;
  364. HAL_StatusTypeDef status;
  365. uint32_t sysclk_source, pll_config;
  366. /* Check Null pointer */
  367. if (RCC_OscInitStruct == NULL)
  368. {
  369. return HAL_ERROR;
  370. }
  371. /* Check the parameters */
  372. assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct->OscillatorType));
  373. sysclk_source = __HAL_RCC_GET_SYSCLK_SOURCE();
  374. pll_config = __HAL_RCC_GET_PLL_OSCSOURCE();
  375. /*----------------------------- MSI Configuration --------------------------*/
  376. if (((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_MSI) == RCC_OSCILLATORTYPE_MSI)
  377. {
  378. /* Check the parameters */
  379. assert_param(IS_RCC_MSI(RCC_OscInitStruct->MSIState));
  380. assert_param(IS_RCC_MSICALIBRATION_VALUE(RCC_OscInitStruct->MSICalibrationValue));
  381. assert_param(IS_RCC_MSI_CLOCK_RANGE(RCC_OscInitStruct->MSIClockRange));
  382. /* Check if MSI is used as system clock or as PLL source when PLL is selected as system clock */
  383. if ((sysclk_source == RCC_SYSCLKSOURCE_STATUS_MSI) ||
  384. ((sysclk_source == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && (pll_config == RCC_PLLSOURCE_MSI)))
  385. {
  386. if ((READ_BIT(RCC->CR, RCC_CR_MSIRDY) != 0U) && (RCC_OscInitStruct->MSIState == RCC_MSI_OFF))
  387. {
  388. return HAL_ERROR;
  389. }
  390. /* Otherwise, just the calibration and MSI range change are allowed */
  391. else
  392. {
  393. /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
  394. must be correctly programmed according to the frequency of the CPU clock
  395. (HCLK) and the supply voltage of the device. */
  396. if (RCC_OscInitStruct->MSIClockRange > __HAL_RCC_GET_MSI_RANGE())
  397. {
  398. /* First increase number of wait states update if necessary */
  399. if (RCC_SetFlashLatencyFromMSIRange(RCC_OscInitStruct->MSIClockRange) != HAL_OK)
  400. {
  401. return HAL_ERROR;
  402. }
  403. /* Selects the Multiple Speed oscillator (MSI) clock range .*/
  404. __HAL_RCC_MSI_RANGE_CONFIG(RCC_OscInitStruct->MSIClockRange);
  405. /* Adjusts the Multiple Speed oscillator (MSI) calibration value.*/
  406. __HAL_RCC_MSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->MSICalibrationValue);
  407. }
  408. else
  409. {
  410. /* Else, keep current flash latency while decreasing applies */
  411. /* Selects the Multiple Speed oscillator (MSI) clock range .*/
  412. __HAL_RCC_MSI_RANGE_CONFIG(RCC_OscInitStruct->MSIClockRange);
  413. /* Adjusts the Multiple Speed oscillator (MSI) calibration value.*/
  414. __HAL_RCC_MSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->MSICalibrationValue);
  415. /* Decrease number of wait states update if necessary */
  416. /* Only possible when MSI is the System clock source */
  417. if (sysclk_source == RCC_SYSCLKSOURCE_STATUS_MSI)
  418. {
  419. if (RCC_SetFlashLatencyFromMSIRange(RCC_OscInitStruct->MSIClockRange) != HAL_OK)
  420. {
  421. return HAL_ERROR;
  422. }
  423. }
  424. }
  425. /* Update the SystemCoreClock global variable */
  426. SystemCoreClock = HAL_RCC_GetHCLKFreq();
  427. /* Configure the source of time base considering new system clocks settings*/
  428. status = HAL_InitTick(uwTickPrio);
  429. if (status != HAL_OK)
  430. {
  431. return status;
  432. }
  433. }
  434. }
  435. else
  436. {
  437. /* Check the MSI State */
  438. if (RCC_OscInitStruct->MSIState != RCC_MSI_OFF)
  439. {
  440. /* Enable the Internal High Speed oscillator (MSI). */
  441. __HAL_RCC_MSI_ENABLE();
  442. /* Get timeout */
  443. tickstart = HAL_GetTick();
  444. /* Wait till MSI is ready */
  445. while (READ_BIT(RCC->CR, RCC_CR_MSIRDY) == 0U)
  446. {
  447. if ((HAL_GetTick() - tickstart) > MSI_TIMEOUT_VALUE)
  448. {
  449. /* New check to avoid false timeout detection in case of preemption */
  450. if (READ_BIT(RCC->CR, RCC_CR_MSIRDY) == 0U)
  451. {
  452. return HAL_TIMEOUT;
  453. }
  454. }
  455. }
  456. /* Selects the Multiple Speed oscillator (MSI) clock range .*/
  457. __HAL_RCC_MSI_RANGE_CONFIG(RCC_OscInitStruct->MSIClockRange);
  458. /* Adjusts the Multiple Speed oscillator (MSI) calibration value.*/
  459. __HAL_RCC_MSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->MSICalibrationValue);
  460. }
  461. else
  462. {
  463. /* Disable the Internal High Speed oscillator (MSI). */
  464. __HAL_RCC_MSI_DISABLE();
  465. /* Get timeout */
  466. tickstart = HAL_GetTick();
  467. /* Wait till MSI is ready */
  468. while (READ_BIT(RCC->CR, RCC_CR_MSIRDY) != 0U)
  469. {
  470. if ((HAL_GetTick() - tickstart) > MSI_TIMEOUT_VALUE)
  471. {
  472. /* New check to avoid false timeout detection in case of preemption */
  473. if (READ_BIT(RCC->CR, RCC_CR_MSIRDY) != 0U)
  474. {
  475. return HAL_TIMEOUT;
  476. }
  477. }
  478. }
  479. }
  480. }
  481. }
  482. /*------------------------------- HSE Configuration ------------------------*/
  483. if (((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE)
  484. {
  485. /* Check the parameters */
  486. assert_param(IS_RCC_HSE(RCC_OscInitStruct->HSEState));
  487. /* When the HSE is used as system clock or clock source for PLL in these cases it is not allowed to be disabled */
  488. if ((sysclk_source == RCC_SYSCLKSOURCE_STATUS_HSE) ||
  489. ((sysclk_source == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && (pll_config == RCC_PLLSOURCE_HSE)))
  490. {
  491. if ((READ_BIT(RCC->CR, RCC_CR_HSERDY) != 0U) && (RCC_OscInitStruct->HSEState == RCC_HSE_OFF))
  492. {
  493. return HAL_ERROR;
  494. }
  495. }
  496. else
  497. {
  498. /* Set the new HSE configuration ---------------------------------------*/
  499. __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct->HSEState);
  500. /* Check the HSE State */
  501. if (RCC_OscInitStruct->HSEState != RCC_HSE_OFF)
  502. {
  503. /* Get Start Tick*/
  504. tickstart = HAL_GetTick();
  505. /* Wait till HSE is ready */
  506. while (READ_BIT(RCC->CR, RCC_CR_HSERDY) == 0U)
  507. {
  508. if ((HAL_GetTick() - tickstart) > HSE_TIMEOUT_VALUE)
  509. {
  510. /* New check to avoid false timeout detection in case of preemption */
  511. if (READ_BIT(RCC->CR, RCC_CR_HSERDY) == 0U)
  512. {
  513. return HAL_TIMEOUT;
  514. }
  515. }
  516. }
  517. }
  518. else
  519. {
  520. /* Get Start Tick*/
  521. tickstart = HAL_GetTick();
  522. /* Wait till HSE is disabled */
  523. while (READ_BIT(RCC->CR, RCC_CR_HSERDY) != 0U)
  524. {
  525. if ((HAL_GetTick() - tickstart) > HSE_TIMEOUT_VALUE)
  526. {
  527. /* New check to avoid false timeout detection in case of preemption */
  528. if (READ_BIT(RCC->CR, RCC_CR_HSERDY) != 0U)
  529. {
  530. return HAL_TIMEOUT;
  531. }
  532. }
  533. }
  534. }
  535. }
  536. }
  537. /*----------------------------- HSI Configuration --------------------------*/
  538. if (((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI)
  539. {
  540. /* Check the parameters */
  541. assert_param(IS_RCC_HSI(RCC_OscInitStruct->HSIState));
  542. assert_param(IS_RCC_HSI_CALIBRATION_VALUE(RCC_OscInitStruct->HSICalibrationValue));
  543. /* Check if HSI is used as system clock or as PLL source when PLL is selected as system clock */
  544. if ((sysclk_source == RCC_SYSCLKSOURCE_STATUS_HSI) ||
  545. ((sysclk_source == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && (pll_config == RCC_PLLSOURCE_HSI)))
  546. {
  547. /* When HSI is used as system clock it will not be disabled */
  548. if ((READ_BIT(RCC->CR, RCC_CR_HSIRDY) != 0U) && (RCC_OscInitStruct->HSIState == RCC_HSI_OFF))
  549. {
  550. return HAL_ERROR;
  551. }
  552. /* Otherwise, just the calibration is allowed */
  553. else
  554. {
  555. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  556. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  557. }
  558. }
  559. else
  560. {
  561. /* Check the HSI State */
  562. if (RCC_OscInitStruct->HSIState != RCC_HSI_OFF)
  563. {
  564. /* Enable the Internal High Speed oscillator (HSI). */
  565. __HAL_RCC_HSI_ENABLE();
  566. /* Get Start Tick*/
  567. tickstart = HAL_GetTick();
  568. /* Wait till HSI is ready */
  569. while (READ_BIT(RCC->CR, RCC_CR_HSIRDY) == 0U)
  570. {
  571. if ((HAL_GetTick() - tickstart) > HSI_TIMEOUT_VALUE)
  572. {
  573. /* New check to avoid false timeout detection in case of preemption */
  574. if (READ_BIT(RCC->CR, RCC_CR_HSIRDY) == 0U)
  575. {
  576. return HAL_TIMEOUT;
  577. }
  578. }
  579. }
  580. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  581. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  582. }
  583. else
  584. {
  585. /* Disable the Internal High Speed oscillator (HSI). */
  586. __HAL_RCC_HSI_DISABLE();
  587. /* Get Start Tick*/
  588. tickstart = HAL_GetTick();
  589. /* Wait till HSI is disabled */
  590. while (READ_BIT(RCC->CR, RCC_CR_HSIRDY) != 0U)
  591. {
  592. if ((HAL_GetTick() - tickstart) > HSI_TIMEOUT_VALUE)
  593. {
  594. /* New check to avoid false timeout detection in case of preemption */
  595. if (READ_BIT(RCC->CR, RCC_CR_HSIRDY) != 0U)
  596. {
  597. return HAL_TIMEOUT;
  598. }
  599. }
  600. }
  601. }
  602. }
  603. }
  604. /*------------------------------ LSI Configuration -------------------------*/
  605. if (((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI)
  606. {
  607. /* Check the parameters */
  608. assert_param(IS_RCC_LSI(RCC_OscInitStruct->LSIState));
  609. /* Check the LSI State */
  610. if (RCC_OscInitStruct->LSIState != RCC_LSI_OFF)
  611. {
  612. /* Apply prescaler value */
  613. if (RCC_OscInitStruct->LSIDiv == RCC_LSI_DIV1)
  614. {
  615. CLEAR_BIT(RCC->CSR, RCC_CSR_LSIPRE);
  616. }
  617. else
  618. {
  619. SET_BIT(RCC->CSR, RCC_CSR_LSIPRE);
  620. }
  621. /* Enable the Internal Low Speed oscillator (LSI). */
  622. __HAL_RCC_LSI_ENABLE();
  623. /* Get Start Tick*/
  624. tickstart = HAL_GetTick();
  625. /* Wait till LSI is ready */
  626. while (READ_BIT(RCC->CSR, RCC_CSR_LSIRDY) == 0U)
  627. {
  628. if ((HAL_GetTick() - tickstart) > LSI_TIMEOUT_VALUE)
  629. {
  630. /* New check to avoid false timeout detection in case of preemption */
  631. if (READ_BIT(RCC->CSR, RCC_CSR_LSIRDY) == 0U)
  632. {
  633. return HAL_TIMEOUT;
  634. }
  635. }
  636. }
  637. }
  638. else
  639. {
  640. /* Disable the Internal Low Speed oscillator (LSI). */
  641. __HAL_RCC_LSI_DISABLE();
  642. /* Get Start Tick*/
  643. tickstart = HAL_GetTick();
  644. /* Wait till LSI is disabled */
  645. while (READ_BIT(RCC->CSR, RCC_CSR_LSIRDY) != 0U)
  646. {
  647. if ((HAL_GetTick() - tickstart) > LSI_TIMEOUT_VALUE)
  648. {
  649. /* New check to avoid false timeout detection in case of preemption */
  650. if (READ_BIT(RCC->CSR, RCC_CSR_LSIRDY) != 0U)
  651. {
  652. return HAL_TIMEOUT;
  653. }
  654. }
  655. }
  656. }
  657. }
  658. /*------------------------------ LSE Configuration -------------------------*/
  659. if (((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE)
  660. {
  661. FlagStatus pwrclkchanged = RESET;
  662. /* Check the parameters */
  663. assert_param(IS_RCC_LSE(RCC_OscInitStruct->LSEState));
  664. /* Update LSE configuration in Backup Domain control register */
  665. /* Requires to enable write access to Backup Domain of necessary */
  666. if (HAL_IS_BIT_CLR(RCC->APB1ENR1, RCC_APB1ENR1_PWREN))
  667. {
  668. __HAL_RCC_PWR_CLK_ENABLE();
  669. pwrclkchanged = SET;
  670. }
  671. if (HAL_IS_BIT_CLR(PWR->CR1, PWR_CR1_DBP))
  672. {
  673. /* Enable write access to Backup domain */
  674. SET_BIT(PWR->CR1, PWR_CR1_DBP);
  675. /* Wait for Backup domain Write protection disable */
  676. tickstart = HAL_GetTick();
  677. while (HAL_IS_BIT_CLR(PWR->CR1, PWR_CR1_DBP))
  678. {
  679. if ((HAL_GetTick() - tickstart) > RCC_DBP_TIMEOUT_VALUE)
  680. {
  681. /* New check to avoid false timeout detection in case of preemption */
  682. if (HAL_IS_BIT_CLR(PWR->CR1, PWR_CR1_DBP))
  683. {
  684. return HAL_TIMEOUT;
  685. }
  686. }
  687. }
  688. }
  689. /* Set the new LSE configuration -----------------------------------------*/
  690. if ((RCC_OscInitStruct->LSEState & RCC_BDCR_LSEON) != 0U)
  691. {
  692. if ((RCC_OscInitStruct->LSEState & RCC_BDCR_LSEBYP) != 0U)
  693. {
  694. /* LSE oscillator bypass enable */
  695. SET_BIT(RCC->BDCR, RCC_BDCR_LSEBYP);
  696. SET_BIT(RCC->BDCR, RCC_BDCR_LSEON);
  697. }
  698. else
  699. {
  700. /* LSE oscillator enable */
  701. SET_BIT(RCC->BDCR, RCC_BDCR_LSEON);
  702. }
  703. }
  704. else
  705. {
  706. CLEAR_BIT(RCC->BDCR, RCC_BDCR_LSEON);
  707. CLEAR_BIT(RCC->BDCR, RCC_BDCR_LSEBYP);
  708. }
  709. /* Check the LSE State */
  710. if (RCC_OscInitStruct->LSEState != RCC_LSE_OFF)
  711. {
  712. /* Get Start Tick*/
  713. tickstart = HAL_GetTick();
  714. /* Wait till LSE is ready */
  715. while (READ_BIT(RCC->BDCR, RCC_BDCR_LSERDY) == 0U)
  716. {
  717. if ((HAL_GetTick() - tickstart) > RCC_LSE_TIMEOUT_VALUE)
  718. {
  719. /* New check to avoid false timeout detection in case of preemption */
  720. if (READ_BIT(RCC->BDCR, RCC_BDCR_LSERDY) == 0U)
  721. {
  722. return HAL_TIMEOUT;
  723. }
  724. }
  725. }
  726. /* Enable LSESYS additionally if requested */
  727. if ((RCC_OscInitStruct->LSEState & RCC_BDCR_LSESYSEN) != 0U)
  728. {
  729. SET_BIT(RCC->BDCR, RCC_BDCR_LSESYSEN);
  730. /* Wait till LSESYS is ready */
  731. while (READ_BIT(RCC->BDCR, RCC_BDCR_LSESYSRDY) == 0U)
  732. {
  733. if ((HAL_GetTick() - tickstart) > RCC_LSE_TIMEOUT_VALUE)
  734. {
  735. /* New check to avoid false timeout detection in case of preemption */
  736. if (READ_BIT(RCC->BDCR, RCC_BDCR_LSESYSRDY) == 0U)
  737. {
  738. return HAL_TIMEOUT;
  739. }
  740. }
  741. }
  742. }
  743. else
  744. {
  745. /* Make sure LSESYSEN/LSESYSRDY are reset */
  746. CLEAR_BIT(RCC->BDCR, RCC_BDCR_LSESYSEN);
  747. /* Wait till LSESYSRDY is cleared */
  748. while (READ_BIT(RCC->BDCR, RCC_BDCR_LSESYSRDY) != 0U)
  749. {
  750. if ((HAL_GetTick() - tickstart) > RCC_LSE_TIMEOUT_VALUE)
  751. {
  752. /* New check to avoid false timeout detection in case of preemption */
  753. if (READ_BIT(RCC->BDCR, RCC_BDCR_LSESYSRDY) != 0U)
  754. {
  755. return HAL_TIMEOUT;
  756. }
  757. }
  758. }
  759. }
  760. }
  761. else
  762. {
  763. /* Get Start Tick*/
  764. tickstart = HAL_GetTick();
  765. /* Wait till LSE is disabled */
  766. while (READ_BIT(RCC->BDCR, RCC_BDCR_LSERDY) != 0U)
  767. {
  768. if ((HAL_GetTick() - tickstart) > RCC_LSE_TIMEOUT_VALUE)
  769. {
  770. /* New check to avoid false timeout detection in case of preemption */
  771. if (READ_BIT(RCC->BDCR, RCC_BDCR_LSERDY) != 0U)
  772. {
  773. return HAL_TIMEOUT;
  774. }
  775. }
  776. }
  777. if (READ_BIT(RCC->BDCR, RCC_BDCR_LSESYSEN) != 0U)
  778. {
  779. /* Reset LSESYSEN once LSE is disabled */
  780. CLEAR_BIT(RCC->BDCR, RCC_BDCR_LSESYSEN);
  781. /* Wait till LSESYSRDY is cleared */
  782. while (READ_BIT(RCC->BDCR, RCC_BDCR_LSESYSRDY) != 0U)
  783. {
  784. if ((HAL_GetTick() - tickstart) > RCC_LSE_TIMEOUT_VALUE)
  785. {
  786. /* New check to avoid false timeout detection in case of preemption */
  787. if (READ_BIT(RCC->BDCR, RCC_BDCR_LSESYSRDY) != 0U)
  788. {
  789. return HAL_TIMEOUT;
  790. }
  791. }
  792. }
  793. }
  794. }
  795. /* Restore clock configuration if changed */
  796. if (pwrclkchanged == SET)
  797. {
  798. __HAL_RCC_PWR_CLK_DISABLE();
  799. }
  800. }
  801. /*------------------------------ HSI48 Configuration -----------------------*/
  802. if (((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI48) == RCC_OSCILLATORTYPE_HSI48)
  803. {
  804. /* Check the parameters */
  805. assert_param(IS_RCC_HSI48(RCC_OscInitStruct->HSI48State));
  806. /* Check the LSI State */
  807. if (RCC_OscInitStruct->HSI48State != RCC_HSI48_OFF)
  808. {
  809. /* Enable the Internal Low Speed oscillator (HSI48). */
  810. __HAL_RCC_HSI48_ENABLE();
  811. /* Get Start Tick*/
  812. tickstart = HAL_GetTick();
  813. /* Wait till HSI48 is ready */
  814. while (READ_BIT(RCC->CRRCR, RCC_CRRCR_HSI48RDY) == 0U)
  815. {
  816. if ((HAL_GetTick() - tickstart) > HSI48_TIMEOUT_VALUE)
  817. {
  818. /* New check to avoid false timeout detection in case of preemption */
  819. if (READ_BIT(RCC->CRRCR, RCC_CRRCR_HSI48RDY) == 0U)
  820. {
  821. return HAL_TIMEOUT;
  822. }
  823. }
  824. }
  825. }
  826. else
  827. {
  828. /* Disable the Internal Low Speed oscillator (HSI48). */
  829. __HAL_RCC_HSI48_DISABLE();
  830. /* Get Start Tick*/
  831. tickstart = HAL_GetTick();
  832. /* Wait till HSI48 is disabled */
  833. while (READ_BIT(RCC->CRRCR, RCC_CRRCR_HSI48RDY) != 0U)
  834. {
  835. if ((HAL_GetTick() - tickstart) > HSI48_TIMEOUT_VALUE)
  836. {
  837. /* New check to avoid false timeout detection in case of preemption */
  838. if (READ_BIT(RCC->CRRCR, RCC_CRRCR_HSI48RDY) != 0U)
  839. {
  840. return HAL_TIMEOUT;
  841. }
  842. }
  843. }
  844. }
  845. }
  846. /*-------------------------------- PLL Configuration -----------------------*/
  847. /* Check the parameters */
  848. assert_param(IS_RCC_PLL(RCC_OscInitStruct->PLL.PLLState));
  849. if (RCC_OscInitStruct->PLL.PLLState != RCC_PLL_NONE)
  850. {
  851. /* Check if the PLL is used as system clock or not */
  852. if (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_PLLCLK)
  853. {
  854. if (RCC_OscInitStruct->PLL.PLLState == RCC_PLL_ON)
  855. {
  856. /* Check the parameters */
  857. assert_param(IS_RCC_PLLSOURCE(RCC_OscInitStruct->PLL.PLLSource));
  858. assert_param(IS_RCC_PLLM_VALUE(RCC_OscInitStruct->PLL.PLLM));
  859. assert_param(IS_RCC_PLLN_VALUE(RCC_OscInitStruct->PLL.PLLN));
  860. assert_param(IS_RCC_PLLP_VALUE(RCC_OscInitStruct->PLL.PLLP));
  861. assert_param(IS_RCC_PLLQ_VALUE(RCC_OscInitStruct->PLL.PLLQ));
  862. assert_param(IS_RCC_PLLR_VALUE(RCC_OscInitStruct->PLL.PLLR));
  863. /* Disable the main PLL. */
  864. __HAL_RCC_PLL_DISABLE();
  865. /* Get Start Tick*/
  866. tickstart = HAL_GetTick();
  867. /* Wait till PLL is ready */
  868. while (READ_BIT(RCC->CR, RCC_CR_PLLRDY) != 0U)
  869. {
  870. if ((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  871. {
  872. /* New check to avoid false timeout detection in case of preemption */
  873. if (READ_BIT(RCC->CR, RCC_CR_PLLRDY) != 0U)
  874. {
  875. return HAL_TIMEOUT;
  876. }
  877. }
  878. }
  879. /* Configure the main PLL clock source, multiplication and division factors. */
  880. __HAL_RCC_PLL_CONFIG(RCC_OscInitStruct->PLL.PLLSource,
  881. RCC_OscInitStruct->PLL.PLLM,
  882. RCC_OscInitStruct->PLL.PLLN,
  883. RCC_OscInitStruct->PLL.PLLP,
  884. RCC_OscInitStruct->PLL.PLLQ,
  885. RCC_OscInitStruct->PLL.PLLR);
  886. /* Enable the main PLL. */
  887. __HAL_RCC_PLL_ENABLE();
  888. /* Enable PLL System Clock output. */
  889. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL_SYSCLK);
  890. /* Get Start Tick*/
  891. tickstart = HAL_GetTick();
  892. /* Wait till PLL is ready */
  893. while (READ_BIT(RCC->CR, RCC_CR_PLLRDY) == 0U)
  894. {
  895. if ((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  896. {
  897. /* New check to avoid false timeout detection in case of preemption */
  898. if (READ_BIT(RCC->CR, RCC_CR_PLLRDY) == 0U)
  899. {
  900. return HAL_TIMEOUT;
  901. }
  902. }
  903. }
  904. }
  905. else
  906. {
  907. /* Disable the main PLL. */
  908. __HAL_RCC_PLL_DISABLE();
  909. /* Get Start Tick*/
  910. tickstart = HAL_GetTick();
  911. /* Wait till PLL is disabled */
  912. while (READ_BIT(RCC->CR, RCC_CR_PLLRDY) != 0U)
  913. {
  914. if ((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  915. {
  916. /* New check to avoid false timeout detection in case of preemption */
  917. if (READ_BIT(RCC->CR, RCC_CR_PLLRDY) != 0U)
  918. {
  919. return HAL_TIMEOUT;
  920. }
  921. }
  922. }
  923. /* Unselect PLL clock source and disable outputs to save power */
  924. RCC->PLLCFGR &= ~(RCC_PLLCFGR_PLLSRC | RCC_PLL_SYSCLK | RCC_PLL_48M1CLK | RCC_PLL_SAI3CLK);
  925. }
  926. }
  927. else
  928. {
  929. /* Check if there is a request to disable the PLL used as System clock source */
  930. if (RCC_OscInitStruct->PLL.PLLState == RCC_PLL_OFF)
  931. {
  932. return HAL_ERROR;
  933. }
  934. else
  935. {
  936. pll_config = RCC->PLLCFGR;
  937. /* Do not return HAL_ERROR if request repeats the current configuration */
  938. if ((READ_BIT(pll_config, RCC_PLLCFGR_PLLSRC) != RCC_OscInitStruct->PLL.PLLSource) ||
  939. (READ_BIT(pll_config, RCC_PLLCFGR_PLLM) != ((RCC_OscInitStruct->PLL.PLLM - 1U) << RCC_PLLCFGR_PLLM_Pos)) ||
  940. (READ_BIT(pll_config, RCC_PLLCFGR_PLLN) != (RCC_OscInitStruct->PLL.PLLN << RCC_PLLCFGR_PLLN_Pos)) ||
  941. (READ_BIT(pll_config, RCC_PLLCFGR_PLLPDIV) != (RCC_OscInitStruct->PLL.PLLP << RCC_PLLCFGR_PLLPDIV_Pos)) ||
  942. (READ_BIT(pll_config, RCC_PLLCFGR_PLLQ) != ((((RCC_OscInitStruct->PLL.PLLQ) >> 1U) - 1U) << RCC_PLLCFGR_PLLQ_Pos)) ||
  943. (READ_BIT(pll_config, RCC_PLLCFGR_PLLR) != ((((RCC_OscInitStruct->PLL.PLLR) >> 1U) - 1U) << RCC_PLLCFGR_PLLR_Pos)))
  944. {
  945. return HAL_ERROR;
  946. }
  947. }
  948. }
  949. }
  950. return HAL_OK;
  951. }
  952. /**
  953. * @brief Initialize the CPU, AHB and APB buses clocks according to the specified
  954. * parameters in the RCC_ClkInitStruct.
  955. * @param RCC_ClkInitStruct pointer to an RCC_OscInitTypeDef structure that
  956. * contains the configuration information for the RCC peripheral.
  957. * @param FLatency FLASH Latency
  958. * This parameter can be one of the following values:
  959. * @arg FLASH_LATENCY_0 FLASH 0 Latency cycle
  960. * @arg FLASH_LATENCY_1 FLASH 1 Latency cycle
  961. * @arg FLASH_LATENCY_2 FLASH 2 Latency cycles
  962. * @arg FLASH_LATENCY_3 FLASH 3 Latency cycles
  963. * @arg FLASH_LATENCY_4 FLASH 4 Latency cycles
  964. * @arg FLASH_LATENCY_5 FLASH 5 Latency cycles
  965. * @arg FLASH_LATENCY_6 FLASH 6 Latency cycles
  966. * @arg FLASH_LATENCY_7 FLASH 7 Latency cycles
  967. * @arg FLASH_LATENCY_8 FLASH 8 Latency cycles
  968. * @arg FLASH_LATENCY_9 FLASH 9 Latency cycles
  969. * @arg FLASH_LATENCY_10 FLASH 10 Latency cycles
  970. * @arg FLASH_LATENCY_11 FLASH 11 Latency cycles
  971. * @arg FLASH_LATENCY_12 FLASH 12 Latency cycles
  972. * @arg FLASH_LATENCY_13 FLASH 13 Latency cycles
  973. * @arg FLASH_LATENCY_14 FLASH 14 Latency cycles
  974. * @arg FLASH_LATENCY_15 FLASH 15 Latency cycles
  975. *
  976. * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency
  977. * and is updated by this function
  978. *
  979. * @note The MSI is used by default as system clock source after
  980. * startup from Reset, wake-up from STANDBY mode. After restart from Reset,
  981. * the MSI frequency is set to its default value 4 MHz.
  982. *
  983. * @note The HSI can be selected as system clock source after
  984. * from STOP modes or in case of failure of the HSE used directly or indirectly
  985. * as system clock (if the Clock Security System CSS is enabled).
  986. *
  987. * @note A switch from one clock source to another occurs only if the target
  988. * clock source is ready (clock stable after startup delay or PLL locked).
  989. * If a clock source which is not yet ready is selected, the switch will
  990. * occur when the clock source is ready.
  991. *
  992. * @note HAL_RCC_ClockConfig() function takes care of clock switching transition state
  993. * with AHB prescaler when switching from HSE or HSI or MSI to PLL with AHB
  994. * frequency (HCLK) higher than 80 MHz and when switching from PLL with HCLK
  995. * higher than 80 MHz to HSE or HSI or MSI currently used as system clock source.
  996. *
  997. * @note You can use HAL_RCC_GetClockConfig() function to know which clock is
  998. * currently used as system clock source.
  999. *
  1000. * @note Depending on the device voltage range, the software has to set correctly
  1001. * HPRE[3:0] bits to ensure that HCLK not exceed the maximum allowed frequency
  1002. * (for more details refer to section above "Initialization/de-initialization functions")
  1003. * @retval None
  1004. */
  1005. HAL_StatusTypeDef HAL_RCC_ClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t FLatency)
  1006. {
  1007. uint32_t tickstart;
  1008. uint32_t pllfreq;
  1009. uint32_t hpre = RCC_SYSCLK_DIV1;
  1010. /* Check Null pointer */
  1011. if (RCC_ClkInitStruct == NULL)
  1012. {
  1013. return HAL_ERROR;
  1014. }
  1015. /* Check the parameters */
  1016. assert_param(IS_RCC_CLOCKTYPE(RCC_ClkInitStruct->ClockType));
  1017. assert_param(IS_FLASH_LATENCY(FLatency));
  1018. /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
  1019. must be correctly programmed according to the frequency of the CPU clock
  1020. (HCLK) and the supply voltage of the device. */
  1021. /* Increasing the number of wait states because of higher CPU frequency */
  1022. if (FLatency > __HAL_FLASH_GET_LATENCY())
  1023. {
  1024. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  1025. __HAL_FLASH_SET_LATENCY(FLatency);
  1026. /* Check that the new number of wait states is taken into account to access the Flash
  1027. memory by reading the FLASH_ACR register */
  1028. if (__HAL_FLASH_GET_LATENCY() != FLatency)
  1029. {
  1030. return HAL_ERROR;
  1031. }
  1032. }
  1033. /*----------------- HCLK Configuration prior to SYSCLK----------------------*/
  1034. /* Apply higher HCLK prescaler request here to ensure CPU clock is not of of spec when SYSCLK is increased */
  1035. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)
  1036. {
  1037. assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
  1038. if(RCC_ClkInitStruct->AHBCLKDivider > READ_BIT(RCC->CFGR, RCC_CFGR_HPRE))
  1039. {
  1040. MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
  1041. }
  1042. }
  1043. /*------------------------- SYSCLK Configuration ---------------------------*/
  1044. if (((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_SYSCLK) == RCC_CLOCKTYPE_SYSCLK)
  1045. {
  1046. assert_param(IS_RCC_SYSCLKSOURCE(RCC_ClkInitStruct->SYSCLKSource));
  1047. /* PLL is selected as System Clock Source */
  1048. if (RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
  1049. {
  1050. /* Check the PLL ready flag */
  1051. if (READ_BIT(RCC->CR, RCC_CR_PLLRDY) == 0U)
  1052. {
  1053. return HAL_ERROR;
  1054. }
  1055. /* Transition state management when selecting PLL as SYSCLK source and */
  1056. /* target frequency above 80Mhz */
  1057. /* Compute target PLL output frequency */
  1058. pllfreq = RCC_GetSysClockFreqFromPLLSource();
  1059. /* Intermediate step with HCLK prescaler 2 necessary before to go over 80Mhz */
  1060. if (pllfreq > 80000000U)
  1061. {
  1062. /* If lowest HCLK prescaler, apply intermediate step with HCLK prescaler 2 necessary before to go over 80Mhz */
  1063. if (READ_BIT(RCC->CFGR, RCC_CFGR_HPRE) == RCC_SYSCLK_DIV1)
  1064. {
  1065. MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_SYSCLK_DIV2);
  1066. hpre = RCC_SYSCLK_DIV2;
  1067. }
  1068. }
  1069. }
  1070. else
  1071. {
  1072. /* HSE is selected as System Clock Source */
  1073. if (RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
  1074. {
  1075. /* Check the HSE ready flag */
  1076. if (READ_BIT(RCC->CR, RCC_CR_HSERDY) == 0U)
  1077. {
  1078. return HAL_ERROR;
  1079. }
  1080. }
  1081. /* MSI is selected as System Clock Source */
  1082. else if (RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_MSI)
  1083. {
  1084. /* Check the MSI ready flag */
  1085. if (READ_BIT(RCC->CR, RCC_CR_MSIRDY) == 0U)
  1086. {
  1087. return HAL_ERROR;
  1088. }
  1089. }
  1090. /* HSI is selected as System Clock Source */
  1091. else
  1092. {
  1093. /* Check the HSI ready flag */
  1094. if (READ_BIT(RCC->CR, RCC_CR_HSIRDY) == 0U)
  1095. {
  1096. return HAL_ERROR;
  1097. }
  1098. }
  1099. /* Transition state management when when going down from PLL used as */
  1100. /* SYSCLK source and frequency above 80Mhz */
  1101. pllfreq = HAL_RCC_GetSysClockFreq();
  1102. /* Intermediate step with HCLK prescaler 2 necessary before to go under 80Mhz */
  1103. if (pllfreq > 80000000U)
  1104. {
  1105. /* If lowest HCLK prescaler, apply intermediate step with HCLK prescaler 2 necessary before to go under 80Mhz */
  1106. if(READ_BIT(RCC->CFGR, RCC_CFGR_HPRE) == RCC_SYSCLK_DIV1)
  1107. {
  1108. MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_SYSCLK_DIV2);
  1109. hpre = RCC_SYSCLK_DIV2;
  1110. }
  1111. }
  1112. }
  1113. MODIFY_REG(RCC->CFGR, RCC_CFGR_SW, RCC_ClkInitStruct->SYSCLKSource);
  1114. /* Get Start Tick*/
  1115. tickstart = HAL_GetTick();
  1116. while (__HAL_RCC_GET_SYSCLK_SOURCE() != (RCC_ClkInitStruct->SYSCLKSource << RCC_CFGR_SWS_Pos))
  1117. {
  1118. if ((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
  1119. {
  1120. /* New check to avoid false timeout detection in case of preemption */
  1121. if (__HAL_RCC_GET_SYSCLK_SOURCE() != (RCC_ClkInitStruct->SYSCLKSource << RCC_CFGR_SWS_Pos))
  1122. {
  1123. return HAL_TIMEOUT;
  1124. }
  1125. }
  1126. }
  1127. }
  1128. /* Is intermediate HCLK prescaler 2 applied internally, resume with HCLK prescaler 1 */
  1129. if(hpre == RCC_SYSCLK_DIV2)
  1130. {
  1131. MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_SYSCLK_DIV1);
  1132. }
  1133. /*----------------- HCLK Configuration after SYSCLK-------------------------*/
  1134. /* Apply lower HCLK prescaler request here to ensure CPU clock is not of of spec when SYSCLK is set */
  1135. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)
  1136. {
  1137. if(RCC_ClkInitStruct->AHBCLKDivider < READ_BIT(RCC->CFGR, RCC_CFGR_HPRE))
  1138. {
  1139. MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
  1140. }
  1141. }
  1142. /* Allow decreasing of the number of wait states (because of lower CPU frequency expected) */
  1143. if (FLatency < __HAL_FLASH_GET_LATENCY())
  1144. {
  1145. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  1146. __HAL_FLASH_SET_LATENCY(FLatency);
  1147. /* Check that the new number of wait states is taken into account to access the Flash
  1148. memory by reading the FLASH_ACR register */
  1149. if (__HAL_FLASH_GET_LATENCY() != FLatency)
  1150. {
  1151. return HAL_ERROR;
  1152. }
  1153. }
  1154. /*-------------------------- PCLK1 Configuration ---------------------------*/
  1155. if (((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
  1156. {
  1157. assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB1CLKDivider));
  1158. MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE1, RCC_ClkInitStruct->APB1CLKDivider);
  1159. }
  1160. /*-------------------------- PCLK2 Configuration ---------------------------*/
  1161. if (((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK2) == RCC_CLOCKTYPE_PCLK2)
  1162. {
  1163. assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB2CLKDivider));
  1164. MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE2, ((RCC_ClkInitStruct->APB2CLKDivider) << 3U));
  1165. }
  1166. /* Update the SystemCoreClock global variable */
  1167. SystemCoreClock = HAL_RCC_GetHCLKFreq();
  1168. /* Configure the source of time base considering new system clocks settings*/
  1169. return HAL_InitTick(uwTickPrio);
  1170. }
  1171. /**
  1172. * @}
  1173. */
  1174. /** @defgroup RCC_Exported_Functions_Group2 Peripheral Control functions
  1175. * @brief RCC clocks control functions
  1176. *
  1177. @verbatim
  1178. ===============================================================================
  1179. ##### Peripheral Control functions #####
  1180. ===============================================================================
  1181. [..]
  1182. This subsection provides a set of functions allowing to:
  1183. (+) Output clock to MCO pin.
  1184. (+) Retrieve current clock frequencies.
  1185. (+) Enable the Clock Security System.
  1186. @endverbatim
  1187. * @{
  1188. */
  1189. /**
  1190. * @brief Select the clock source to output on MCO pin(PA8).
  1191. * @note PA8 should be configured in alternate function mode.
  1192. * @param RCC_MCOx specifies the output direction for the clock source.
  1193. * For STM32L5xx family this parameter can have only one value:
  1194. * @arg @ref RCC_MCO1 Clock source to output on MCO1 pin(PA8).
  1195. * @param RCC_MCOSource specifies the clock source to output.
  1196. * This parameter can be one of the following values:
  1197. * @arg @ref RCC_MCO1SOURCE_NOCLOCK MCO output disabled, no clock on MCO
  1198. * @arg @ref RCC_MCO1SOURCE_SYSCLK system clock selected as MCO source
  1199. * @arg @ref RCC_MCO1SOURCE_MSI MSI clock selected as MCO source
  1200. * @arg @ref RCC_MCO1SOURCE_HSI HSI clock selected as MCO source
  1201. * @arg @ref RCC_MCO1SOURCE_HSE HSE clock selected as MCO sourcee
  1202. * @arg @ref RCC_MCO1SOURCE_PLLCLK main PLL clock selected as MCO source
  1203. * @arg @ref RCC_MCO1SOURCE_LSI LSI clock selected as MCO source
  1204. * @arg @ref RCC_MCO1SOURCE_LSE LSE clock selected as MCO source
  1205. * @arg @ref RCC_MCO1SOURCE_HSI48 HSI48 clock selected as MCO source
  1206. * @param RCC_MCODiv specifies the MCO prescaler.
  1207. * This parameter can be one of the following values:
  1208. * @arg @ref RCC_MCODIV_1 no division applied to MCO clock
  1209. * @arg @ref RCC_MCODIV_2 division by 2 applied to MCO clock
  1210. * @arg @ref RCC_MCODIV_4 division by 4 applied to MCO clock
  1211. * @arg @ref RCC_MCODIV_8 division by 8 applied to MCO clock
  1212. * @arg @ref RCC_MCODIV_16 division by 16 applied to MCO clock
  1213. * @retval None
  1214. */
  1215. void HAL_RCC_MCOConfig(uint32_t RCC_MCOx, uint32_t RCC_MCOSource, uint32_t RCC_MCODiv)
  1216. {
  1217. GPIO_InitTypeDef GPIO_InitStruct;
  1218. /* Check the parameters */
  1219. assert_param(IS_RCC_MCO(RCC_MCOx));
  1220. assert_param(IS_RCC_MCODIV(RCC_MCODiv));
  1221. assert_param(IS_RCC_MCO1SOURCE(RCC_MCOSource));
  1222. /* MCO Clock Enable */
  1223. __MCO1_CLK_ENABLE();
  1224. /* Configure the MCO1 pin in alternate function mode */
  1225. GPIO_InitStruct.Pin = MCO1_PIN;
  1226. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  1227. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  1228. GPIO_InitStruct.Pull = GPIO_NOPULL;
  1229. GPIO_InitStruct.Alternate = GPIO_AF0_MCO;
  1230. HAL_GPIO_Init(MCO1_GPIO_PORT, &GPIO_InitStruct);
  1231. /* Mask MCOSEL[] and MCOPRE[] bits then set MCO1 clock source and prescaler */
  1232. MODIFY_REG(RCC->CFGR, (RCC_CFGR_MCOSEL | RCC_CFGR_MCOPRE), (RCC_MCOSource | RCC_MCODiv));
  1233. }
  1234. /**
  1235. * @brief Return the SYSCLK frequency.
  1236. *
  1237. * @note The system frequency computed by this function is not the real
  1238. * frequency in the chip. It is calculated based on the predefined
  1239. * constant and the selected clock source:
  1240. * @note If SYSCLK source is MSI, function returns values based on MSI
  1241. * Value as defined by the MSI range.
  1242. * @note If SYSCLK source is HSI, function returns values based on HSI_VALUE(*)
  1243. * @note If SYSCLK source is HSE, function returns values based on HSE_VALUE(**)
  1244. * @note If SYSCLK source is PLL, function returns values based on HSE_VALUE(**),
  1245. * HSI_VALUE(*) or MSI Value multiplied/divided by the PLL factors.
  1246. * @note (*) HSI_VALUE is a constant defined in stm32l5xx_hal_conf.h file (default value
  1247. * 16 MHz) but the real value may vary depending on the variations
  1248. * in voltage and temperature.
  1249. * @note (**) HSE_VALUE is a constant defined in stm32l5xx_hal_conf.h file (default value
  1250. * 8 MHz), user has to ensure that HSE_VALUE is same as the real
  1251. * frequency of the crystal used. Otherwise, this function may
  1252. * have wrong result.
  1253. *
  1254. * @note The result of this function could be not correct when using fractional
  1255. * value for HSE crystal.
  1256. *
  1257. * @note This function can be used by the user application to compute the
  1258. * baudrate for the communication peripherals or configure other parameters.
  1259. *
  1260. * @note Each time SYSCLK changes, this function must be called to update the
  1261. * right SYSCLK value. Otherwise, any configuration based on this function will be incorrect.
  1262. *
  1263. *
  1264. * @retval SYSCLK frequency
  1265. */
  1266. uint32_t HAL_RCC_GetSysClockFreq(void)
  1267. {
  1268. uint32_t msirange = 0U, sysclockfreq = 0U;
  1269. uint32_t pllvco, pllsource, pllr, pllm; /* no init needed */
  1270. uint32_t sysclk_source, pll_oscsource;
  1271. sysclk_source = __HAL_RCC_GET_SYSCLK_SOURCE();
  1272. pll_oscsource = __HAL_RCC_GET_PLL_OSCSOURCE();
  1273. if ((sysclk_source == RCC_SYSCLKSOURCE_STATUS_MSI) ||
  1274. ((sysclk_source == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && (pll_oscsource == RCC_PLLSOURCE_MSI)))
  1275. {
  1276. /* MSI or PLL with MSI source used as system clock source */
  1277. /* Get SYSCLK source */
  1278. if (READ_BIT(RCC->CR, RCC_CR_MSIRGSEL) == 0U)
  1279. {
  1280. /* MSISRANGE from RCC_CSR applies */
  1281. msirange = READ_BIT(RCC->CSR, RCC_CSR_MSISRANGE) >> RCC_CSR_MSISRANGE_Pos;
  1282. }
  1283. else
  1284. {
  1285. /* MSIRANGE from RCC_CR applies */
  1286. msirange = READ_BIT(RCC->CR, RCC_CR_MSIRANGE) >> RCC_CR_MSIRANGE_Pos;
  1287. }
  1288. /*MSI frequency range in Hz*/
  1289. msirange = MSIRangeTable[msirange];
  1290. if (sysclk_source == RCC_SYSCLKSOURCE_STATUS_MSI)
  1291. {
  1292. /* MSI used as system clock source */
  1293. sysclockfreq = msirange;
  1294. }
  1295. }
  1296. else if (sysclk_source == RCC_SYSCLKSOURCE_STATUS_HSI)
  1297. {
  1298. /* HSI used as system clock source */
  1299. sysclockfreq = HSI_VALUE;
  1300. }
  1301. else if (sysclk_source == RCC_SYSCLKSOURCE_STATUS_HSE)
  1302. {
  1303. /* HSE used as system clock source */
  1304. sysclockfreq = HSE_VALUE;
  1305. }
  1306. else
  1307. {
  1308. /* unexpected case: sysclockfreq at 0 */
  1309. }
  1310. if (sysclk_source == RCC_SYSCLKSOURCE_STATUS_PLLCLK)
  1311. {
  1312. /* PLL used as system clock source */
  1313. /* PLL_VCO = (HSE_VALUE or HSI_VALUE or MSI_VALUE/ PLLM) * PLLN
  1314. SYSCLK = PLL_VCO / PLLR
  1315. */
  1316. pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC);
  1317. pllm = ((RCC->PLLCFGR & RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1U ;
  1318. switch (pllsource)
  1319. {
  1320. case RCC_PLLSOURCE_HSI: /* HSI used as PLL clock source */
  1321. pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos);
  1322. break;
  1323. case RCC_PLLSOURCE_HSE: /* HSE used as PLL clock source */
  1324. pllvco = (HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos);
  1325. break;
  1326. case RCC_PLLSOURCE_MSI: /* MSI used as PLL clock source */
  1327. default:
  1328. pllvco = (msirange / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos);
  1329. break;
  1330. }
  1331. pllr = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> RCC_PLLCFGR_PLLR_Pos) + 1U) * 2U;
  1332. sysclockfreq = pllvco / pllr;
  1333. }
  1334. return sysclockfreq;
  1335. }
  1336. /**
  1337. * @brief Return the HCLK frequency.
  1338. * @note Each time HCLK changes, this function must be called to update the
  1339. * right HCLK value. Otherwise, any configuration based on this function will be incorrect.
  1340. * @retval HCLK frequency in Hz
  1341. */
  1342. uint32_t HAL_RCC_GetHCLKFreq(void)
  1343. {
  1344. return (HAL_RCC_GetSysClockFreq() >> AHBPrescTable[(RCC->CFGR & RCC_CFGR_HPRE) >> RCC_CFGR_HPRE_Pos]);
  1345. }
  1346. /**
  1347. * @brief Return the PCLK1 frequency.
  1348. * @note Each time PCLK1 changes, this function must be called to update the
  1349. * right PCLK1 value. Otherwise, any configuration based on this function will be incorrect.
  1350. * @retval PCLK1 frequency in Hz
  1351. */
  1352. uint32_t HAL_RCC_GetPCLK1Freq(void)
  1353. {
  1354. /* Get HCLK source and Compute PCLK1 frequency ---------------------------*/
  1355. return (HAL_RCC_GetHCLKFreq() >> APBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE1) >> RCC_CFGR_PPRE1_Pos]);
  1356. }
  1357. /**
  1358. * @brief Return the PCLK2 frequency.
  1359. * @note Each time PCLK2 changes, this function must be called to update the
  1360. * right PCLK2 value. Otherwise, any configuration based on this function will be incorrect.
  1361. * @retval PCLK2 frequency in Hz
  1362. */
  1363. uint32_t HAL_RCC_GetPCLK2Freq(void)
  1364. {
  1365. /* Get HCLK source and Compute PCLK2 frequency ---------------------------*/
  1366. return (HAL_RCC_GetHCLKFreq() >> APBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE2) >> RCC_CFGR_PPRE2_Pos]);
  1367. }
  1368. /**
  1369. * @brief Configure the RCC_OscInitStruct according to the internal
  1370. * RCC configuration registers.
  1371. * @param RCC_OscInitStruct pointer to an RCC_OscInitTypeDef structure that
  1372. * will be configured.
  1373. * @retval None
  1374. */
  1375. void HAL_RCC_GetOscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  1376. {
  1377. /* Check the parameters */
  1378. assert_param(RCC_OscInitStruct != (void *)NULL);
  1379. /* Set all possible values for the Oscillator type parameter ---------------*/
  1380. RCC_OscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_MSI | \
  1381. RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_HSI48;
  1382. /* Get the HSE configuration -----------------------------------------------*/
  1383. if ((RCC->CR & RCC_CR_HSEBYP) == RCC_CR_HSEBYP)
  1384. {
  1385. RCC_OscInitStruct->HSEState = RCC_HSE_BYPASS;
  1386. }
  1387. else if ((RCC->CR & RCC_CR_HSEON) == RCC_CR_HSEON)
  1388. {
  1389. RCC_OscInitStruct->HSEState = RCC_HSE_ON;
  1390. }
  1391. else
  1392. {
  1393. RCC_OscInitStruct->HSEState = RCC_HSE_OFF;
  1394. }
  1395. /* Get the MSI configuration -----------------------------------------------*/
  1396. if ((RCC->CR & RCC_CR_MSION) == RCC_CR_MSION)
  1397. {
  1398. RCC_OscInitStruct->MSIState = RCC_MSI_ON;
  1399. }
  1400. else
  1401. {
  1402. RCC_OscInitStruct->MSIState = RCC_MSI_OFF;
  1403. }
  1404. RCC_OscInitStruct->MSICalibrationValue = (uint32_t)((RCC->ICSCR & RCC_ICSCR_MSITRIM) >> RCC_ICSCR_MSITRIM_Pos);
  1405. RCC_OscInitStruct->MSIClockRange = (uint32_t)((RCC->CR & RCC_CR_MSIRANGE));
  1406. /* Get the HSI configuration -----------------------------------------------*/
  1407. if ((RCC->CR & RCC_CR_HSION) == RCC_CR_HSION)
  1408. {
  1409. RCC_OscInitStruct->HSIState = RCC_HSI_ON;
  1410. }
  1411. else
  1412. {
  1413. RCC_OscInitStruct->HSIState = RCC_HSI_OFF;
  1414. }
  1415. RCC_OscInitStruct->HSICalibrationValue = (uint32_t)((RCC->ICSCR & RCC_ICSCR_HSITRIM) >> RCC_ICSCR_HSITRIM_Pos);
  1416. /* Get the LSE configuration -----------------------------------------------*/
  1417. if ((RCC->BDCR & RCC_BDCR_LSEBYP) == RCC_BDCR_LSEBYP)
  1418. {
  1419. if ((RCC->BDCR & RCC_BDCR_LSESYSEN) == RCC_BDCR_LSESYSEN)
  1420. {
  1421. RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS;
  1422. }
  1423. else
  1424. {
  1425. RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS_RTC_ONLY;
  1426. }
  1427. }
  1428. else if ((RCC->BDCR & RCC_BDCR_LSEON) == RCC_BDCR_LSEON)
  1429. {
  1430. if ((RCC->BDCR & RCC_BDCR_LSESYSEN) == RCC_BDCR_LSESYSEN)
  1431. {
  1432. RCC_OscInitStruct->LSEState = RCC_LSE_ON;
  1433. }
  1434. else
  1435. {
  1436. RCC_OscInitStruct->LSEState = RCC_LSE_ON_RTC_ONLY;
  1437. }
  1438. }
  1439. else
  1440. {
  1441. RCC_OscInitStruct->LSEState = RCC_LSE_OFF;
  1442. }
  1443. /* Get the LSI configuration -----------------------------------------------*/
  1444. if ((RCC->CSR & RCC_CSR_LSION) == RCC_CSR_LSION)
  1445. {
  1446. RCC_OscInitStruct->LSIState = RCC_LSI_ON;
  1447. }
  1448. else
  1449. {
  1450. RCC_OscInitStruct->LSIState = RCC_LSI_OFF;
  1451. }
  1452. if ((RCC->CSR & RCC_CSR_LSIPRE) == RCC_CSR_LSIPRE)
  1453. {
  1454. RCC_OscInitStruct->LSIDiv = RCC_LSI_DIV128;
  1455. }
  1456. else
  1457. {
  1458. RCC_OscInitStruct->LSIDiv = RCC_LSI_DIV1;
  1459. }
  1460. /* Get the HSI48 configuration ---------------------------------------------*/
  1461. if ((RCC->CRRCR & RCC_CRRCR_HSI48ON) == RCC_CRRCR_HSI48ON)
  1462. {
  1463. RCC_OscInitStruct->HSI48State = RCC_HSI48_ON;
  1464. }
  1465. else
  1466. {
  1467. RCC_OscInitStruct->HSI48State = RCC_HSI48_OFF;
  1468. }
  1469. /* Get the PLL configuration -----------------------------------------------*/
  1470. if ((RCC->CR & RCC_CR_PLLON) == RCC_CR_PLLON)
  1471. {
  1472. RCC_OscInitStruct->PLL.PLLState = RCC_PLL_ON;
  1473. }
  1474. else
  1475. {
  1476. RCC_OscInitStruct->PLL.PLLState = RCC_PLL_OFF;
  1477. }
  1478. RCC_OscInitStruct->PLL.PLLSource = (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC);
  1479. RCC_OscInitStruct->PLL.PLLM = (uint32_t)(((RCC->PLLCFGR & RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1U);
  1480. RCC_OscInitStruct->PLL.PLLN = (uint32_t)((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos);
  1481. RCC_OscInitStruct->PLL.PLLQ = (uint32_t)((((RCC->PLLCFGR & RCC_PLLCFGR_PLLQ) >> RCC_PLLCFGR_PLLQ_Pos) + 1U) << 1U);
  1482. RCC_OscInitStruct->PLL.PLLR = (uint32_t)((((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> RCC_PLLCFGR_PLLR_Pos) + 1U) << 1U);
  1483. RCC_OscInitStruct->PLL.PLLP = (uint32_t)((RCC->PLLCFGR & RCC_PLLCFGR_PLLPDIV) >> RCC_PLLCFGR_PLLPDIV_Pos);
  1484. }
  1485. /**
  1486. * @brief Configure the RCC_ClkInitStruct according to the internal
  1487. * RCC configuration registers.
  1488. * @param RCC_ClkInitStruct pointer to an RCC_ClkInitTypeDef structure that
  1489. * will be configured.
  1490. * @param pFLatency Pointer on the Flash Latency.
  1491. * @retval None
  1492. */
  1493. void HAL_RCC_GetClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t *pFLatency)
  1494. {
  1495. /* Check the parameters */
  1496. assert_param(RCC_ClkInitStruct != (void *)NULL);
  1497. assert_param(pFLatency != (void *)NULL);
  1498. /* Set all possible values for the Clock type parameter --------------------*/
  1499. RCC_ClkInitStruct->ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  1500. /* Get the SYSCLK configuration --------------------------------------------*/
  1501. RCC_ClkInitStruct->SYSCLKSource = (uint32_t)(RCC->CFGR & RCC_CFGR_SW);
  1502. /* Get the HCLK configuration ----------------------------------------------*/
  1503. RCC_ClkInitStruct->AHBCLKDivider = (uint32_t)(RCC->CFGR & RCC_CFGR_HPRE);
  1504. /* Get the APB1 configuration ----------------------------------------------*/
  1505. RCC_ClkInitStruct->APB1CLKDivider = (uint32_t)(RCC->CFGR & RCC_CFGR_PPRE1);
  1506. /* Get the APB2 configuration ----------------------------------------------*/
  1507. RCC_ClkInitStruct->APB2CLKDivider = (uint32_t)((RCC->CFGR & RCC_CFGR_PPRE2) >> 3U);
  1508. /* Get the Flash Wait State (Latency) configuration ------------------------*/
  1509. *pFLatency = (uint32_t)(FLASH->ACR & FLASH_ACR_LATENCY);
  1510. }
  1511. /**
  1512. * @brief Enable the Clock Security System.
  1513. * @note If a failure is detected on the HSE oscillator clock, this oscillator
  1514. * is automatically disabled and an interrupt is generated to inform the
  1515. * software about the failure (Clock Security System Interrupt, CSSI),
  1516. * allowing the MCU to perform rescue operations. The CSSI is linked to
  1517. * the Cortex-M33 NMI (Non-Maskable Interrupt) exception vector.
  1518. * @note The Clock Security System can only be cleared by reset.
  1519. * @retval None
  1520. */
  1521. void HAL_RCC_EnableCSS(void)
  1522. {
  1523. SET_BIT(RCC->CR, RCC_CR_CSSON) ;
  1524. }
  1525. /**
  1526. * @brief Handle the RCC Clock Security System interrupt request.
  1527. * @note This API should be called under the NMI_Handler().
  1528. * @retval None
  1529. */
  1530. void HAL_RCC_NMI_IRQHandler(void)
  1531. {
  1532. /* Check RCC CSSF interrupt flag */
  1533. if (__HAL_RCC_GET_IT(RCC_IT_CSS))
  1534. {
  1535. /* RCC Clock Security System interrupt user callback */
  1536. HAL_RCC_CSSCallback();
  1537. /* Clear RCC CSS pending bit */
  1538. __HAL_RCC_CLEAR_IT(RCC_IT_CSS);
  1539. }
  1540. }
  1541. /**
  1542. * @brief RCC Clock Security System interrupt callback.
  1543. * @retval none
  1544. */
  1545. __weak void HAL_RCC_CSSCallback(void)
  1546. {
  1547. /* NOTE : This function should not be modified, when the callback is needed,
  1548. the HAL_RCC_CSSCallback should be implemented in the user file
  1549. */
  1550. }
  1551. /**
  1552. * @brief Get and clear reset flags
  1553. * @note Once reset flags are retrieved, this API is clearing them in order
  1554. * to isolate next reset reason.
  1555. * @retval can be a combination of @ref RCC_Reset_Flag
  1556. */
  1557. uint32_t HAL_RCC_GetResetSource(void)
  1558. {
  1559. uint32_t reset;
  1560. /* Get all reset flags */
  1561. reset = RCC->CSR & RCC_RESET_FLAG_ALL;
  1562. /* Clear Reset flags */
  1563. RCC->CSR |= RCC_CSR_RMVF;
  1564. return reset;
  1565. }
  1566. /**
  1567. * @}
  1568. */
  1569. /** @defgroup RCC_Exported_Functions_Group3 Attributes management functions
  1570. * @brief Attributes management functions.
  1571. *
  1572. @verbatim
  1573. ===============================================================================
  1574. ##### RCC attributes functions #####
  1575. ===============================================================================
  1576. @endverbatim
  1577. * @{
  1578. */
  1579. /**
  1580. * @brief Configure the RCC item attribute(s).
  1581. * @note Available attributes are to secure items and set RCC as privileged.
  1582. * Default state is not secure and unprivileged access allowed.
  1583. * @note Secure and non-secure attributes can only be set from the secure
  1584. * state when the system implements the security (TZEN=1).
  1585. * @note Security and privilege attributes can be set independently.
  1586. * @param Item Item(s) to set attributes on.
  1587. * This parameter can be a one or a combination of @ref RCC_items
  1588. * @param Attributes can be one or a combination of the following values:
  1589. * @arg @ref RCC_PRIV Privileged-only access
  1590. * @arg @ref RCC_NPRIV Privileged/Non-privileged access
  1591. * @arg @ref RCC_SEC Secure-only access
  1592. * @arg @ref RCC_NSEC Secure/Non-secure access
  1593. * @retval None
  1594. */
  1595. void HAL_RCC_ConfigAttributes(uint32_t Item, uint32_t Attributes)
  1596. {
  1597. /* Check the parameters */
  1598. assert_param(IS_RCC_ITEMS_ATTRIBUTES(Item));
  1599. assert_param(IS_RCC_ATTRIBUTES(Attributes));
  1600. /* Privilege/non-privilege attribute */
  1601. if ((Attributes & RCC_PRIV) == RCC_PRIV)
  1602. {
  1603. SET_BIT(RCC->CR, RCC_CR_PRIV);
  1604. }
  1605. else if ((Attributes & RCC_NPRIV) == RCC_NPRIV)
  1606. {
  1607. CLEAR_BIT(RCC->CR, RCC_CR_PRIV);
  1608. }
  1609. else
  1610. {
  1611. /* do nothing */
  1612. }
  1613. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  1614. /* Secure/non-secure attribute */
  1615. if ((Attributes & RCC_SEC) == RCC_SEC)
  1616. {
  1617. SET_BIT(RCC_S->SECCFGR, Item);
  1618. }
  1619. else if ((Attributes & RCC_NSEC) == RCC_NSEC)
  1620. {
  1621. CLEAR_BIT(RCC_S->SECCFGR, Item);
  1622. }
  1623. else
  1624. {
  1625. /* do nothing */
  1626. }
  1627. #endif /* __ARM_FEATURE_CMSE */
  1628. }
  1629. /**
  1630. * @brief Get the attribute of a RCC item.
  1631. * @note Secure and non-secure attributes are only available from secure state
  1632. * when the system implements the security (TZEN=1)
  1633. * @param Item Single item to get secure/non-secure and privilege/non-privilege attribute from.
  1634. * @param pAttributes pointer to return the attributes value.
  1635. * @retval HAL Status.
  1636. */
  1637. HAL_StatusTypeDef HAL_RCC_GetConfigAttributes(uint32_t Item, uint32_t *pAttributes)
  1638. {
  1639. uint32_t attributes;
  1640. /* Check null pointer */
  1641. if (pAttributes == NULL)
  1642. {
  1643. return HAL_ERROR;
  1644. }
  1645. /* Check the parameters */
  1646. assert_param(IS_RCC_ITEMS_ATTRIBUTES(Item));
  1647. /* Get privilege or non-privilege attribute */
  1648. if (READ_BIT(RCC->CR, RCC_CR_PRIV) != 0U)
  1649. {
  1650. attributes = RCC_PRIV;
  1651. }
  1652. else
  1653. {
  1654. attributes = RCC_NPRIV;
  1655. }
  1656. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  1657. /* Get the secure or non-secure attribute state */
  1658. if ((RCC_S->SECCFGR & Item) == Item)
  1659. {
  1660. attributes |= RCC_SEC;
  1661. }
  1662. else
  1663. {
  1664. attributes |= RCC_NSEC;
  1665. }
  1666. #endif /* __ARM_FEATURE_CMSE */
  1667. /* return value */
  1668. *pAttributes = attributes;
  1669. return HAL_OK;
  1670. }
  1671. /**
  1672. * @}
  1673. */
  1674. /**
  1675. * @}
  1676. */
  1677. /* Private function prototypes -----------------------------------------------*/
  1678. /** @addtogroup RCC_Private_Functions
  1679. * @{
  1680. */
  1681. /**
  1682. * @brief Update number of Flash wait states in line with MSI range and current
  1683. voltage range.
  1684. * @param msirange MSI range value from RCC_MSIRANGE_0 to RCC_MSIRANGE_11
  1685. * @retval HAL status
  1686. */
  1687. static HAL_StatusTypeDef RCC_SetFlashLatencyFromMSIRange(uint32_t msirange)
  1688. {
  1689. uint32_t latency = FLASH_LATENCY_0; /* default value 0WS */
  1690. uint32_t vos;
  1691. if (__HAL_RCC_PWR_IS_CLK_ENABLED())
  1692. {
  1693. vos = HAL_PWREx_GetVoltageRange();
  1694. }
  1695. else
  1696. {
  1697. __HAL_RCC_PWR_CLK_ENABLE();
  1698. vos = HAL_PWREx_GetVoltageRange();
  1699. __HAL_RCC_PWR_CLK_DISABLE();
  1700. }
  1701. if ((vos == PWR_REGULATOR_VOLTAGE_SCALE0) || (vos == PWR_REGULATOR_VOLTAGE_SCALE1))
  1702. {
  1703. if (msirange > RCC_MSIRANGE_8)
  1704. {
  1705. /* MSI > 16Mhz */
  1706. if (msirange > RCC_MSIRANGE_10)
  1707. {
  1708. /* MSI 48Mhz */
  1709. latency = FLASH_LATENCY_2; /* 2WS */
  1710. }
  1711. else
  1712. {
  1713. /* MSI 24Mhz or 32Mhz */
  1714. latency = FLASH_LATENCY_1; /* 1WS */
  1715. }
  1716. }
  1717. /* else MSI <= 16Mhz default FLASH_LATENCY_0 0WS */
  1718. }
  1719. else
  1720. {
  1721. if (msirange > RCC_MSIRANGE_8)
  1722. {
  1723. /* MSI > 16Mhz */
  1724. latency = FLASH_LATENCY_3; /* 3WS */
  1725. }
  1726. else
  1727. {
  1728. if (msirange == RCC_MSIRANGE_8)
  1729. {
  1730. /* MSI 16Mhz */
  1731. latency = FLASH_LATENCY_2; /* 2WS */
  1732. }
  1733. else if (msirange == RCC_MSIRANGE_7)
  1734. {
  1735. /* MSI 8Mhz */
  1736. latency = FLASH_LATENCY_1; /* 1WS */
  1737. }
  1738. else
  1739. {
  1740. /* MSI < 8Mhz default FLASH_LATENCY_0 0WS */
  1741. }
  1742. }
  1743. }
  1744. __HAL_FLASH_SET_LATENCY(latency);
  1745. /* Check that the new number of wait states is taken into account to access the Flash
  1746. memory by reading the FLASH_ACR register */
  1747. if ((FLASH->ACR & FLASH_ACR_LATENCY) != latency)
  1748. {
  1749. return HAL_ERROR;
  1750. }
  1751. return HAL_OK;
  1752. }
  1753. /**
  1754. * @brief Compute SYSCLK frequency based on PLL SYSCLK source.
  1755. * @retval SYSCLK frequency
  1756. */
  1757. static uint32_t RCC_GetSysClockFreqFromPLLSource(void)
  1758. {
  1759. uint32_t msirange, pllvco, pllsource, pllr, pllm, sysclockfreq; /* no init needed */
  1760. /* PLL_VCO = (HSE_VALUE or HSI_VALUE or MSI_VALUE/ PLLM) * PLLN
  1761. SYSCLK = PLL_VCO / PLLR
  1762. */
  1763. pllsource = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC);
  1764. pllm = (READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1U ;
  1765. switch (pllsource)
  1766. {
  1767. case RCC_PLLSOURCE_HSI: /* HSI used as PLL clock source */
  1768. pllvco = (HSI_VALUE / pllm) * (READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos);
  1769. break;
  1770. case RCC_PLLSOURCE_HSE: /* HSE used as PLL clock source */
  1771. pllvco = (HSE_VALUE / pllm) * (READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos);
  1772. break;
  1773. case RCC_PLLSOURCE_MSI: /* MSI used as PLL clock source */
  1774. /* Get MSI range source */
  1775. if(READ_BIT(RCC->CR, RCC_CR_MSIRGSEL) == 0U)
  1776. { /* MSISRANGE from RCC_CSR applies */
  1777. msirange = READ_BIT(RCC->CSR, RCC_CSR_MSISRANGE) >> RCC_CSR_MSISRANGE_Pos;
  1778. }
  1779. else
  1780. { /* MSIRANGE from RCC_CR applies */
  1781. msirange = READ_BIT(RCC->CR, RCC_CR_MSIRANGE) >> RCC_CR_MSIRANGE_Pos;
  1782. }
  1783. /*MSI frequency range in HZ*/
  1784. pllvco = MSIRangeTable[msirange];
  1785. break;
  1786. default:
  1787. /* unexpected */
  1788. pllvco = 0;
  1789. break;
  1790. }
  1791. pllr = ((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLR) >> RCC_PLLCFGR_PLLR_Pos) + 1U) * 2U;
  1792. sysclockfreq = pllvco / pllr;
  1793. return sysclockfreq;
  1794. }
  1795. /**
  1796. * @}
  1797. */
  1798. #endif /* HAL_RCC_MODULE_ENABLED */
  1799. /**
  1800. * @}
  1801. */
  1802. /**
  1803. * @}
  1804. */