stm32h5xx_hal_rcc.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901
  1. /**
  2. ******************************************************************************
  3. * @file stm32h5xx_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) 2023 STMicroelectronics.
  15. * All rights reserved.
  16. *
  17. * This software is licensed under terms that can be found in the LICENSE file
  18. * in the root directory of this software component.
  19. * If no LICENSE file comes with this software, it is provided AS-IS.
  20. *
  21. ******************************************************************************
  22. @verbatim
  23. ==============================================================================
  24. ##### RCC specific features #####
  25. ==============================================================================
  26. [..]
  27. After reset the device is running from High Speed Internal oscillator
  28. (64 MHz) with Flash 3 wait states. Flash prefetch buffer, D-Cache
  29. and I-Cache are disabled, and all peripherals are off except internal
  30. SRAM, Flash and JTAG.
  31. (+) There is no prescaler on High speed (AHBs) and Low speed (APBs) busses:
  32. all peripherals mapped on these busses are running at HSI speed.
  33. (+) The clock for all peripherals is switched off, except the SRAM and FLASH.
  34. (+) All GPIOs are in analog mode, except the JTAG pins which
  35. are assigned to be used for debug purpose.
  36. [..]
  37. Once the device started from reset, the user application has to:
  38. (+) Configure the clock source to be used to drive the System clock
  39. (if the application needs higher frequency/performance)
  40. (+) Configure the System clock frequency and Flash settings
  41. (+) Configure the AHB and APB busses prescalers
  42. (+) Enable the clock for the peripheral(s) to be used
  43. (+) Configure the clock source(s) for peripherals which clocks are not
  44. derived from the System clock (SAIx, RTC, ADC, USB, SDMMC, etc.)
  45. @endverbatim
  46. */
  47. /* Includes ------------------------------------------------------------------*/
  48. #include "stm32h5xx_hal.h"
  49. /** @addtogroup STM32H5xx_HAL_Driver
  50. * @{
  51. */
  52. /** @defgroup RCC RCC
  53. * @brief RCC HAL module driver
  54. * @{
  55. */
  56. #ifdef HAL_RCC_MODULE_ENABLED
  57. /* Private typedef -----------------------------------------------------------*/
  58. /* Private define ------------------------------------------------------------*/
  59. /** @defgroup RCC_Private_Constants RCC Private Constants
  60. * @{
  61. */
  62. /** @defgroup RCC_Timeout_Value Timeout Values
  63. * @{
  64. */
  65. #define RCC_LSI_TIMEOUT_VALUE ((uint32_t)2U) /* 2 ms (minimum Tick + 1) */
  66. #define RCC_HSI48_TIMEOUT_VALUE ((uint32_t)2U) /* 2 ms (minimum Tick + 1) */
  67. #define RCC_PLL_TIMEOUT_VALUE ((uint32_t)2U) /* 2 ms (minimum Tick + 1) */
  68. #define RCC_CLOCKSWITCH_TIMEOUT_VALUE ((uint32_t)5000U) /* 5 s */
  69. #define RCC_PLL_FRAC_WAIT_VALUE 1U /* PLL Fractional part waiting time before new latch enable : 1 ms */
  70. /**
  71. * @}
  72. */
  73. /**
  74. * @}
  75. */
  76. /* Private macro -------------------------------------------------------------*/
  77. /** @defgroup RCC_Private_Macros RCC Private Macros
  78. * @{
  79. */
  80. #define MCO1_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
  81. #define MCO1_GPIO_PORT GPIOA
  82. #define MCO1_PIN GPIO_PIN_8
  83. #define MCO2_CLK_ENABLE() __HAL_RCC_GPIOC_CLK_ENABLE()
  84. #define MCO2_GPIO_PORT GPIOC
  85. #define MCO2_PIN GPIO_PIN_9
  86. /**
  87. * @}
  88. */
  89. /* Private variables ---------------------------------------------------------*/
  90. /* Private function prototypes -----------------------------------------------*/
  91. /* Exported functions --------------------------------------------------------*/
  92. /** @defgroup RCC_Exported_Functions RCC Exported Functions
  93. * @{
  94. */
  95. /** @defgroup RCC_Exported_Functions_Group1 Initialization and de-initialization functions
  96. * @brief Initialization and Configuration functions
  97. *
  98. @verbatim
  99. ===============================================================================
  100. ##### Initialization and de-initialization functions #####
  101. ===============================================================================
  102. [..]
  103. This section provides functions allowing to configure the internal and external oscillators
  104. (HSE, HSI, LSE, CSI, LSI, PLL1, HSE CSS and MCOs) and the System busses clocks (SYSCLK, AHB, APB1, APB2
  105. and APB3).
  106. [..] Internal/external clock and PLL configuration
  107. (+) HSI (high-speed internal): 64 MHz factory-trimmed RC used directly or through
  108. the PLL as System clock source.
  109. (#) CSI is a low-power RC oscillator which can be used directly as system clock, peripheral
  110. clock, or PLL input. But even with frequency calibration, is less accurate than an
  111. external crystal oscillator or ceramic resonator.
  112. (+) LSI (low-speed internal): 32 KHz low consumption RC used as IWDG and/or RTC
  113. clock source.
  114. (+) HSE (high-speed external): 4 to 48 MHz crystal oscillator used directly or
  115. through the PLL as System clock source. Can be used also optionally as RTC clock source.
  116. (+) LSE (low-speed external): 32.768 KHz oscillator used optionally as RTC clock source.
  117. (+) PLL1 (clocked by HSI, HSE or CSI) providing up to three independent output clocks:
  118. (++) The first output is used to generate the high speed system clock (up to 250MHz).
  119. (++) The second output is used to generate the clock for the USB (48 MHz), the FDCAN1/2,
  120. the SPI1/2/3, the OCTOSPI, the RNG (<=48 MHz), the SDMMC1/2 and to generate an accurate
  121. clock to achieve high-quality audio performance on SAI1/2 interface.
  122. (+) PLL2 (clocked by HSI, HSE or CSI) providing up to three independent output clocks:
  123. (++) The first output is used to generate the clock for the LPTIMs, the SPI1/2/3 and to generate
  124. an accurate clock to achieve high-quality audio performance on SAI1/2 interface.
  125. (++) The second output is used to generate the clock for USARTs, the UARTs, the LPUART1,
  126. the FDCAN1/2, the SPI4/5/6 and the USB.
  127. (++) The third output is used to generate the clock the SDMMC1/2, the ADC/DAC, the I2C1/2,
  128. the I3C1/2 and the OCTOSPI.
  129. (+) PLL3 (clocked by HSI , HSE or CSI) providing up to three independent output clocks:
  130. (++) The first output is used to generate the clock for SPI1/2/3 and to generate an accurate
  131. clock to achieve high-quality audio performance on SAI1/2 interface.
  132. (++) The second output is used to generate the clock for USARTs, the UARTs, the LPUART1,
  133. the SPI4/5/6 and the USB.
  134. (++) The third output is used to generate the clock for the I2Cs, the I3Cs and the LPTIMs.
  135. (+) HSE CSS (HSE Clock Security System): once enabled, if a HSE clock failure occurs
  136. (HSE used directly or through PLL1 as System clock source), the System clock
  137. is automatically switched to HSI and an interrupt is generated if enabled.
  138. The interrupt is linked to the Cortex-M33 NMI (Non-Maskable Interrupt)
  139. exception vector.
  140. (#) MCO1 (micro controller clock output1), used to output HSI, LSE, HSE, PLL1(PLL1_Q)
  141. or HSI48 clock (through a configurable pre-scaler) on PA8 pin.
  142. (#) MCO2 (micro controller clock output2), used to output HSE, PLL2(PLL2_P), SYSCLK,
  143. LSI, CSI, or PLL1(PLL1_P) clock (through a configurable pre-scaler) on PC9 pin.
  144. [..] System, AHB and APB busses clocks configuration
  145. (+) Several clock sources can be used to drive the System clock (SYSCLK): CSI, HSI, HSE and the main PLL.
  146. The AHB clock (HCLK) is derived from System clock through configurable
  147. prescaler and used to clock the CPU, memory and peripherals mapped
  148. on AHB bus (DMA, GPIO...). APB1 (PCLK1), APB2 (PCLK2) and APB3 (PCLK3) clocks are derived
  149. from AHB clock through configurable prescalers and used to clock
  150. the peripherals mapped on these busses. You can use
  151. "HAL_RCC_GetSysClockFreq()" function to retrieve the frequencies of these clocks.
  152. -@- All the peripheral clocks are derived from the System clock (SYSCLK) except:
  153. (+@) SAI: the SAI clock can be derived either from specific PLL (PLL1, PLL2 or PLL3),
  154. the per_ck clock (HSE, HSI or CSI) or from an external clock mapped on the SAI_CKIN pin.
  155. You have to use HAL_RCCEx_PeriphCLKConfig() function to configure this clock.
  156. (+@) SPI/I2S: the SPI1/2/3 clock can be derived either from specific PLL (PLL1, PLL2 or PLL3),
  157. the per_ck clock (HSE, HSI or CSI) or from an external clock mapped on the SPI_CKIN pin.
  158. You have to use HAL_RCCEx_PeriphCLKConfig() function to configure this clock.
  159. (+@) RTC: the RTC clock can be derived either from the LSI, LSE or HSE clock
  160. divided by 2 to 31.
  161. You have to use __HAL_RCC_RTC_ENABLE() and HAL_RCCEx_PeriphCLKConfig() function
  162. to configure this clock.
  163. (+@) USB: USB requires a frequency equal to 48 MHz to work correctly. This clock is derived
  164. of the main PLL or PLL2 through PLLQ divider. You have to use HAL_RCCEx_PeriphCLKConfig()
  165. function to configure this clock.
  166. (+@) UCPD: the UCPD clock is derived from HSI (divided by 4) clock.
  167. (+@) SDMMC: SDMMC1/2 peripherals require a frequency equal or lower than 48 MHz.
  168. This clock is derived from the PLL1 or PLL2 through PLL1Q or PLL2R divider. You have
  169. to use HAL_RCCEx_PeriphCLKConfig() function to configure this clock.
  170. (+@) IWDG clock which is always the LSI clock. You have to use HAL_RCCEx_PeriphCLKConfig()
  171. function to configure this clock.
  172. (+@) RNG: the RNG clock can be derived either from PLL1Q, HSI48, LSE or LSI clock. You have
  173. to use HAL_RCCEx_PeriphCLKConfig() function to configure this clock.
  174. (+@) DAC: the DAC clock can be derived either from LSE or LSI clock. You have
  175. to use HAL_RCCEx_PeriphCLKConfig() function to configure this clock.
  176. (+@) FDCAN: the FDCAN1/2 clock can be derived either from HSE, PLL1Q or PLL2Q clock. You have
  177. to use HAL_RCCEx_PeriphCLKConfig() function to configure this clock.
  178. (+@) CEC: the CEC clock can be derived either from LSE, LSI or CSI (divided by 122) clock.You have
  179. to use HAL_RCCEx_PeriphCLKConfig() function to configure this clock.
  180. (+@) ETH: the Ethernet clock is derived from PLL1Q clock.
  181. (+) The maximum frequency of the SYSCLK, HCLK, PCLK1, PCLK2 and PCLK3 is 250 MHz.
  182. The clock source frequency should be adapted depending on the device voltage range
  183. as listed in the Reference Manual "Clock source frequency versus voltage scaling" chapter.
  184. @endverbatim
  185. Table 1. HCLK clock frequency for STM32H5xx devices
  186. +-----------------------------------------------------------------------------------------------+
  187. | Latency | HCLK clock frequency (MHz) |
  188. | |-----------------------------------------------------------------------------|
  189. | | voltage range 0 | voltage range 1 | voltage range 2 | voltage range 3 |
  190. | | 1.26 - 1.35V | 1.15 - 1.26V | 1.05 - 1.15V | 0,95 - 1,05V |
  191. |-----------------|-------------------|------------------|------------------|-------------------|
  192. |0WS(1 CPU cycles)| 0 < HCLK <= 38 | 0 < HCLK <= 32 | 0 < HCLK <= 26 | 0 < HCLK <= 16 |
  193. |-----------------|-------------------|------------------|------------------|-------------------|
  194. |1WS(2 CPU cycles)| 38 < HCLK <= 76 | 32 < HCLK <= 64 | 26 < HCLK <= 50 | 16 < HCLK <= 32 |
  195. |-----------------|-------------------|------------------|------------------|-------------------|
  196. |2WS(3 CPU cycles)| 76 < HCLK <= 114 | 64 < HCLK <= 96 | 50 < HCLK <= 80 | 32 < HCLK <= 50 |
  197. |-----------------|-------------------|------------------|------------------|-------------------|
  198. |3WS(4 CPU cycles)| 114 < HCLK <= 152 | 96 < HCLK <= 128 | 80 < HCLK <= 106 | 50 < HCLK <= 65 |
  199. |-----------------|-------------------|------------------|------------------|-------------------|
  200. |4WS(5 CPU cycles)| 152 < HCLK <= 190| 128 < HCLK <= 160| 106 < HCLK <= 130| 65 < HCLK <= 80 |
  201. |-----------------|-------------------|------------------|------------------|-------------------|
  202. |5WS(6 CPU cycles)| 190 < HCLK <= 250| 160 < HCLK <= 180| NA | NA |
  203. +-----------------+-------------------+------------------+------------------+-------------------+
  204. * @{
  205. */
  206. /**
  207. * @brief Reset the RCC clock configuration to the default reset state.
  208. * @note The default reset state of the clock configuration is given below:
  209. * - HSI ON and used as system clock source
  210. * - HSE, CSI, PLL, PLL2 and PLL3 OFF
  211. * - AHB, APB1 and APB2 prescaler set to 1.
  212. * - HSECSS, MCO1 and MCO2 OFF
  213. * - All interrupts disabled
  214. * @note This function doesn't modify the configuration of the
  215. * - Peripheral clocks
  216. * - LSI, LSE and RTC clocks
  217. * @retval HAL Status.
  218. */
  219. HAL_StatusTypeDef HAL_RCC_DeInit(void)
  220. {
  221. uint32_t tickstart;
  222. /* Increasing the CPU frequency */
  223. if (FLASH_LATENCY_DEFAULT > __HAL_FLASH_GET_LATENCY())
  224. {
  225. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  226. __HAL_FLASH_SET_LATENCY(FLASH_LATENCY_DEFAULT);
  227. /* Check that the new number of wait states is taken into account to access the Flash
  228. memory by reading the FLASH_ACR register */
  229. if (__HAL_FLASH_GET_LATENCY() != FLASH_LATENCY_DEFAULT)
  230. {
  231. return HAL_ERROR;
  232. }
  233. }
  234. /* Get start tick*/
  235. tickstart = HAL_GetTick();
  236. /* Set HSION bit */
  237. SET_BIT(RCC->CR, RCC_CR_HSION);
  238. /* Wait till HSI is ready */
  239. while (READ_BIT(RCC->CR, RCC_CR_HSIRDY) == 0U)
  240. {
  241. if ((HAL_GetTick() - tickstart) > RCC_HSI_TIMEOUT_VALUE)
  242. {
  243. return HAL_TIMEOUT;
  244. }
  245. }
  246. /* Set HSIDIV Default value */
  247. CLEAR_BIT(RCC->CR, RCC_CR_HSIDIV);
  248. /* Set HSITRIM default value */
  249. WRITE_REG(RCC->HSICFGR, RCC_HSICFGR_HSITRIM_6);
  250. /* Adapt Systick interrupt period */
  251. if (HAL_InitTick(uwTickPrio) != HAL_OK)
  252. {
  253. return HAL_ERROR;
  254. }
  255. /* Get start tick*/
  256. tickstart = HAL_GetTick();
  257. /* Reset CFGR register (HSI is selected as system clock source) */
  258. CLEAR_REG(RCC->CFGR1);
  259. CLEAR_REG(RCC->CFGR2);
  260. /* Wait till clock switch is ready */
  261. while (READ_BIT(RCC->CFGR1, RCC_CFGR1_SWS) != 0U)
  262. {
  263. if ((HAL_GetTick() - tickstart) > RCC_CLOCKSWITCH_TIMEOUT_VALUE)
  264. {
  265. return HAL_TIMEOUT;
  266. }
  267. }
  268. /* Reset HSECSSON, HSEON, HSIKERON, CSION, CSIKERON and HSI48ON bits */
  269. CLEAR_BIT(RCC->CR, RCC_CR_CSION | RCC_CR_CSIKERON | RCC_CR_HSECSSON | RCC_CR_HSIKERON | RCC_CR_HSI48ON | \
  270. RCC_CR_HSEON);
  271. /* Reset HSEEXT bit*/
  272. CLEAR_BIT(RCC->CR, RCC_CR_HSEEXT);
  273. /* Get Start Tick */
  274. tickstart = HAL_GetTick();
  275. /* Clear PLL1ON bit */
  276. CLEAR_BIT(RCC->CR, RCC_CR_PLL1ON);
  277. /* Wait till PLL1 is disabled */
  278. while (READ_BIT(RCC->CR, RCC_CR_PLL1RDY) != 0U)
  279. {
  280. if ((HAL_GetTick() - tickstart) > RCC_PLL_TIMEOUT_VALUE)
  281. {
  282. return HAL_TIMEOUT;
  283. }
  284. }
  285. /* Get Start Tick */
  286. tickstart = HAL_GetTick();
  287. /* Reset PLL2N bit */
  288. CLEAR_BIT(RCC->CR, RCC_CR_PLL2ON);
  289. /* Wait till PLL2 is disabled */
  290. while (READ_BIT(RCC->CR, RCC_CR_PLL2RDY) != 0U)
  291. {
  292. if ((HAL_GetTick() - tickstart) > RCC_PLL_TIMEOUT_VALUE)
  293. {
  294. return HAL_TIMEOUT;
  295. }
  296. }
  297. #if defined(RCC_CR_PLL3ON)
  298. /* Get Start Tick */
  299. tickstart = HAL_GetTick();
  300. /* Reset PLL3 bit */
  301. CLEAR_BIT(RCC->CR, RCC_CR_PLL3ON);
  302. /* Wait till PLL3 is disabled */
  303. while (READ_BIT(RCC->CR, RCC_CR_PLL3RDY) != 0U)
  304. {
  305. if ((HAL_GetTick() - tickstart) > RCC_PLL_TIMEOUT_VALUE)
  306. {
  307. return HAL_TIMEOUT;
  308. }
  309. }
  310. #endif /* RCC_CR_PLL3ON */
  311. /* Reset PLL1CFGR register */
  312. CLEAR_REG(RCC->PLL1CFGR);
  313. /* Reset PLL1DIVR register */
  314. WRITE_REG(RCC->PLL1DIVR, 0x01010280U);
  315. /* Reset PLL1FRACR register */
  316. CLEAR_REG(RCC->PLL1FRACR);
  317. /* Reset PLL2CFGR register */
  318. CLEAR_REG(RCC->PLL2CFGR);
  319. /* Reset PLL2DIVR register */
  320. WRITE_REG(RCC->PLL2DIVR, 0x01010280U);
  321. /* Reset PLL2FRACR register */
  322. CLEAR_REG(RCC->PLL2FRACR);
  323. #if defined(RCC_CR_PLL3ON)
  324. /* Reset PLL3CFGR register */
  325. CLEAR_REG(RCC->PLL3CFGR);
  326. /* Reset PLL3DIVR register */
  327. WRITE_REG(RCC->PLL3DIVR, 0x01010280U);
  328. /* Reset PLL3FRACR register */
  329. CLEAR_REG(RCC->PLL3FRACR);
  330. #endif /* RCC_CR_PLL3ON */
  331. /* Reset HSEBYP bit */
  332. CLEAR_BIT(RCC->CR, RCC_CR_HSEBYP);
  333. /* Disable all interrupts */
  334. CLEAR_REG(RCC->CIER);
  335. /* Clear all interrupts flags */
  336. WRITE_REG(RCC->CICR, 0xFFFFFFFFU);
  337. /* Reset all RSR flags */
  338. SET_BIT(RCC->RSR, RCC_RSR_RMVF);
  339. /* Update the SystemCoreClock global variable */
  340. SystemCoreClock = HSI_VALUE;
  341. /* Decreasing the number of wait states because of lower CPU frequency */
  342. if (FLASH_LATENCY_DEFAULT < __HAL_FLASH_GET_LATENCY())
  343. {
  344. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  345. __HAL_FLASH_SET_LATENCY(FLASH_LATENCY_DEFAULT);
  346. /* Check that the new number of wait states is taken into account to access the Flash
  347. memory by reading the FLASH_ACR register */
  348. if (__HAL_FLASH_GET_LATENCY() != FLASH_LATENCY_DEFAULT)
  349. {
  350. return HAL_ERROR;
  351. }
  352. }
  353. /* Adapt Systick interrupt period */
  354. if (HAL_InitTick(TICK_INT_PRIORITY) != HAL_OK)
  355. {
  356. return HAL_ERROR;
  357. }
  358. else
  359. {
  360. return HAL_OK;
  361. }
  362. }
  363. /**
  364. * @brief Initialize the RCC Oscillators according to the specified parameters in the
  365. * RCC_OscInitTypeDef.
  366. * @param pOscInitStruct pointer to an RCC_OscInitTypeDef structure that
  367. * contains the configuration information for the RCC Oscillators.
  368. * @note The PLL is not disabled when used as system clock.
  369. * @note Transitions LSE Bypass to LSE On and LSE On to LSE Bypass are not
  370. * supported by this macro. User should request a transition to LSE Off
  371. * first and then LSE On or LSE Bypass.
  372. * @note Transition HSE Bypass to HSE On and HSE On to HSE Bypass are not
  373. * supported by this macro. User should request a transition to HSE Off
  374. * first and then HSE On or HSE Bypass.
  375. * @retval HAL status
  376. */
  377. HAL_StatusTypeDef HAL_RCC_OscConfig(const RCC_OscInitTypeDef *pOscInitStruct)
  378. {
  379. uint32_t tickstart;
  380. uint32_t temp_sysclksrc;
  381. uint32_t temp_pllckselr;
  382. uint32_t temp1_pllckcfg;
  383. uint32_t temp2_pllckcfg;
  384. /* Check Null pointer */
  385. if (pOscInitStruct == NULL)
  386. {
  387. return HAL_ERROR;
  388. }
  389. /* Check the parameters */
  390. assert_param(IS_RCC_OSCILLATORTYPE(pOscInitStruct->OscillatorType));
  391. temp_sysclksrc = __HAL_RCC_GET_SYSCLK_SOURCE();
  392. temp_pllckselr = __HAL_RCC_GET_PLL1_OSCSOURCE();
  393. /*----------------------------- CSI Configuration --------------------------*/
  394. if (((pOscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_CSI) == RCC_OSCILLATORTYPE_CSI)
  395. {
  396. /* Check the parameters */
  397. assert_param(IS_RCC_CSI(pOscInitStruct->CSIState));
  398. assert_param(IS_RCC_CSICALIBRATION_VALUE(pOscInitStruct->CSICalibrationValue));
  399. /* When the CSI is used as system clock it will not be disabled */
  400. if ((temp_sysclksrc == RCC_SYSCLKSOURCE_STATUS_CSI) ||
  401. ((temp_sysclksrc == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && (temp_pllckselr == RCC_PLL1_SOURCE_CSI)))
  402. {
  403. if (pOscInitStruct->CSIState == RCC_CSI_OFF)
  404. {
  405. return HAL_ERROR;
  406. }
  407. /* Otherwise, just the calibration and CSI is allowed */
  408. else
  409. {
  410. /* Adjusts the Internal Low-power oscillator (CSI) calibration value.*/
  411. __HAL_RCC_CSI_CALIBRATIONVALUE_ADJUST(pOscInitStruct->CSICalibrationValue);
  412. }
  413. }
  414. else
  415. {
  416. /* Check the CSI State */
  417. if ((pOscInitStruct->CSIState) != RCC_CSI_OFF)
  418. {
  419. /* Enable the Internal High Speed oscillator (CSI). */
  420. __HAL_RCC_CSI_ENABLE();
  421. /* Get Start Tick*/
  422. tickstart = HAL_GetTick();
  423. /* Wait till CSI is ready */
  424. while (READ_BIT(RCC->CR, RCC_CR_CSIRDY) == 0U)
  425. {
  426. if ((HAL_GetTick() - tickstart) > RCC_CSI_TIMEOUT_VALUE)
  427. {
  428. return HAL_TIMEOUT;
  429. }
  430. }
  431. /* Adjusts the Internal High Speed oscillator (CSI) calibration value.*/
  432. __HAL_RCC_CSI_CALIBRATIONVALUE_ADJUST(pOscInitStruct->CSICalibrationValue);
  433. }
  434. else
  435. {
  436. /* Disable the Internal High Speed oscillator (CSI). */
  437. __HAL_RCC_CSI_DISABLE();
  438. /* Get Start Tick*/
  439. tickstart = HAL_GetTick();
  440. /* Wait till CSI is disabled */
  441. while (READ_BIT(RCC->CR, RCC_CR_CSIRDY) != 0U)
  442. {
  443. if ((HAL_GetTick() - tickstart) > RCC_CSI_TIMEOUT_VALUE)
  444. {
  445. return HAL_TIMEOUT;
  446. }
  447. }
  448. }
  449. }
  450. }
  451. /*------------------------------- HSE Configuration ------------------------*/
  452. if (((pOscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE)
  453. {
  454. /* Check the parameters */
  455. assert_param(IS_RCC_HSE(pOscInitStruct->HSEState));
  456. /* When the HSE is used as system clock or clock source for PLL in these cases it is not allowed to be disabled */
  457. if ((temp_sysclksrc == RCC_SYSCLKSOURCE_STATUS_HSE) ||
  458. ((temp_sysclksrc == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && (temp_pllckselr == RCC_PLL1_SOURCE_HSE)))
  459. {
  460. if (pOscInitStruct->HSEState == RCC_HSE_OFF)
  461. {
  462. return HAL_ERROR;
  463. }
  464. }
  465. else
  466. {
  467. /* Set the new HSE configuration ---------------------------------------*/
  468. __HAL_RCC_HSE_CONFIG(pOscInitStruct->HSEState);
  469. /* Check the HSE State */
  470. if (pOscInitStruct->HSEState != RCC_HSE_OFF)
  471. {
  472. /* Get Start Tick*/
  473. tickstart = HAL_GetTick();
  474. /* Wait till HSE is ready */
  475. while (READ_BIT(RCC->CR, RCC_CR_HSERDY) == 0U)
  476. {
  477. if ((HAL_GetTick() - tickstart) > RCC_HSE_TIMEOUT_VALUE)
  478. {
  479. return HAL_TIMEOUT;
  480. }
  481. }
  482. }
  483. else
  484. {
  485. /* Get Start Tick*/
  486. tickstart = HAL_GetTick();
  487. /* Wait till HSE is disabled */
  488. while (READ_BIT(RCC->CR, RCC_CR_HSERDY) != 0U)
  489. {
  490. if ((HAL_GetTick() - tickstart) > RCC_HSE_TIMEOUT_VALUE)
  491. {
  492. return HAL_TIMEOUT;
  493. }
  494. }
  495. }
  496. }
  497. }
  498. /*----------------------------- HSI Configuration --------------------------*/
  499. if (((pOscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI)
  500. {
  501. /* Check the parameters */
  502. assert_param(IS_RCC_HSI(pOscInitStruct->HSIState));
  503. assert_param(IS_RCC_HSIDIV(pOscInitStruct->HSIDiv));
  504. assert_param(IS_RCC_HSI_CALIBRATION_VALUE(pOscInitStruct->HSICalibrationValue));
  505. /* Check if HSI is used as system clock or as PLL source when PLL is selected as system clock */
  506. if ((temp_sysclksrc == RCC_SYSCLKSOURCE_STATUS_HSI) ||
  507. ((temp_sysclksrc == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && (temp_pllckselr == RCC_PLL1_SOURCE_HSI)))
  508. {
  509. /* When HSI is used as system clock it will not be disabled */
  510. if (pOscInitStruct->HSIState == RCC_HSI_OFF)
  511. {
  512. return HAL_ERROR;
  513. }
  514. /* Otherwise, HSI calibration and division may be allowed */
  515. else
  516. {
  517. /* HSI division is allowed if HSI is used as system clock */
  518. if (temp_sysclksrc == RCC_SYSCLKSOURCE_STATUS_HSI)
  519. {
  520. if (__HAL_RCC_GET_HSI_DIVIDER() != (pOscInitStruct->HSIDiv))
  521. {
  522. /* Adjust the HSI division factor */
  523. __HAL_RCC_HSI_DIVIDER_CONFIG(pOscInitStruct->HSIDiv);
  524. /* Update the SystemCoreClock global variable with new HSI value */
  525. (void) HAL_RCC_GetHCLKFreq();
  526. /* Configure the source of time base considering new system clocks settings*/
  527. if (HAL_InitTick(uwTickPrio) != HAL_OK)
  528. {
  529. return HAL_ERROR;
  530. }
  531. }
  532. }
  533. /* Get Start Tick*/
  534. tickstart = HAL_GetTick();
  535. /* Wait till HSI is ready */
  536. while (READ_BIT(RCC->CR, RCC_CR_HSIRDY) == 0U)
  537. {
  538. if ((HAL_GetTick() - tickstart) > RCC_HSI_TIMEOUT_VALUE)
  539. {
  540. return HAL_TIMEOUT;
  541. }
  542. }
  543. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  544. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(pOscInitStruct->HSICalibrationValue);
  545. }
  546. }
  547. else
  548. {
  549. /* Check the HSI State */
  550. if (pOscInitStruct->HSIState != RCC_HSI_OFF)
  551. {
  552. /* Adjust the HSI division factor */
  553. __HAL_RCC_HSI_DIVIDER_CONFIG(pOscInitStruct->HSIDiv);
  554. /* Enable the HSI oscillator */
  555. __HAL_RCC_HSI_ENABLE();
  556. /* Get Start Tick*/
  557. tickstart = HAL_GetTick();
  558. /* Wait till HSI is ready */
  559. while (READ_BIT(RCC->CR, RCC_CR_HSIRDY) == 0U)
  560. {
  561. if ((HAL_GetTick() - tickstart) > RCC_HSI_TIMEOUT_VALUE)
  562. {
  563. return HAL_TIMEOUT;
  564. }
  565. }
  566. /* Adjust the Internal High Speed oscillator (HSI) calibration value.*/
  567. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(pOscInitStruct->HSICalibrationValue);
  568. }
  569. else
  570. {
  571. /* Disable the Internal High Speed oscillator (HSI). */
  572. __HAL_RCC_HSI_DISABLE();
  573. /* Get Start Tick*/
  574. tickstart = HAL_GetTick();
  575. /* Wait till HSI is disabled */
  576. while (READ_BIT(RCC->CR, RCC_CR_HSIRDY) != 0U)
  577. {
  578. if ((HAL_GetTick() - tickstart) > RCC_HSI_TIMEOUT_VALUE)
  579. {
  580. return HAL_TIMEOUT;
  581. }
  582. }
  583. }
  584. }
  585. }
  586. /*------------------------------ LSI Configuration -------------------------*/
  587. if (((pOscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI)
  588. {
  589. /* Check the parameters */
  590. assert_param(IS_RCC_LSI(pOscInitStruct->LSIState));
  591. /* Update LSI configuration in Backup Domain control register */
  592. /* Check the LSI State */
  593. if (pOscInitStruct->LSIState != RCC_LSI_OFF)
  594. {
  595. /* Enable the Internal Low Speed oscillator (LSI). */
  596. __HAL_RCC_LSI_ENABLE();
  597. /* Get Start Tick*/
  598. tickstart = HAL_GetTick();
  599. /* Wait till LSI is ready */
  600. while (READ_BIT(RCC->BDCR, RCC_BDCR_LSIRDY) == 0U)
  601. {
  602. if ((HAL_GetTick() - tickstart) > RCC_LSI_TIMEOUT_VALUE)
  603. {
  604. return HAL_TIMEOUT;
  605. }
  606. }
  607. }
  608. else
  609. {
  610. /* Disable the Internal Low Speed oscillator (LSI). */
  611. __HAL_RCC_LSI_DISABLE();
  612. /* Get Start Tick*/
  613. tickstart = HAL_GetTick();
  614. /* Wait till LSI is disabled */
  615. while (READ_BIT(RCC->BDCR, RCC_BDCR_LSIRDY) != 0U)
  616. {
  617. if ((HAL_GetTick() - tickstart) > RCC_LSI_TIMEOUT_VALUE)
  618. {
  619. return HAL_TIMEOUT;
  620. }
  621. }
  622. }
  623. }
  624. /*------------------------------ LSE Configuration -------------------------*/
  625. if (((pOscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE)
  626. {
  627. /* Check the parameters */
  628. assert_param(IS_RCC_LSE(pOscInitStruct->LSEState));
  629. /* Update LSE configuration in Backup Domain control register */
  630. /* Requires to enable write access to Backup Domain */
  631. if (HAL_IS_BIT_CLR(PWR->DBPCR, PWR_DBPCR_DBP))
  632. {
  633. /* Enable write access to Backup domain */
  634. SET_BIT(PWR->DBPCR, PWR_DBPCR_DBP);
  635. /* Wait for Backup domain Write protection disable */
  636. tickstart = HAL_GetTick();
  637. while (HAL_IS_BIT_CLR(PWR->DBPCR, PWR_DBPCR_DBP))
  638. {
  639. if ((HAL_GetTick() - tickstart) > RCC_DBP_TIMEOUT_VALUE)
  640. {
  641. return HAL_TIMEOUT;
  642. }
  643. }
  644. }
  645. /* Set the new LSE configuration -----------------------------------------*/
  646. __HAL_RCC_LSE_CONFIG(pOscInitStruct->LSEState);
  647. /* Check the LSE State */
  648. if (pOscInitStruct->LSEState != RCC_LSE_OFF)
  649. {
  650. /* Get Start Tick*/
  651. tickstart = HAL_GetTick();
  652. /* Wait till LSE is ready */
  653. while (READ_BIT(RCC->BDCR, RCC_BDCR_LSERDY) == 0U)
  654. {
  655. if ((HAL_GetTick() - tickstart) > RCC_LSE_TIMEOUT_VALUE)
  656. {
  657. return HAL_TIMEOUT;
  658. }
  659. }
  660. }
  661. else
  662. {
  663. /* Get Start Tick*/
  664. tickstart = HAL_GetTick();
  665. /* Wait till LSE is disabled */
  666. while (READ_BIT(RCC->BDCR, RCC_BDCR_LSERDY) != 0U)
  667. {
  668. if ((HAL_GetTick() - tickstart) > RCC_LSE_TIMEOUT_VALUE)
  669. {
  670. return HAL_TIMEOUT;
  671. }
  672. }
  673. }
  674. }
  675. /*------------------------------ HSI48 Configuration -----------------------*/
  676. if (((pOscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI48) == RCC_OSCILLATORTYPE_HSI48)
  677. {
  678. /* Check the parameters */
  679. assert_param(IS_RCC_HSI48(pOscInitStruct->HSI48State));
  680. /* Check the HSI48 State */
  681. if (pOscInitStruct->HSI48State != RCC_HSI48_OFF)
  682. {
  683. /* Enable the Internal High Speed oscillator (HSI48). */
  684. __HAL_RCC_HSI48_ENABLE();
  685. /* Get Start Tick*/
  686. tickstart = HAL_GetTick();
  687. /* Wait till HSI48 is ready */
  688. while (READ_BIT(RCC->CR, RCC_CR_HSI48RDY) == 0U)
  689. {
  690. if ((HAL_GetTick() - tickstart) > RCC_HSI48_TIMEOUT_VALUE)
  691. {
  692. return HAL_TIMEOUT;
  693. }
  694. }
  695. }
  696. else
  697. {
  698. /* Disable the Internal High Speed oscillator (HSI48). */
  699. __HAL_RCC_HSI48_DISABLE();
  700. /* Get Start Tick*/
  701. tickstart = HAL_GetTick();
  702. /* Wait till HSI48 is disabled */
  703. while (READ_BIT(RCC->CR, RCC_CR_HSI48RDY) != 0U)
  704. {
  705. if ((HAL_GetTick() - tickstart) > RCC_HSI48_TIMEOUT_VALUE)
  706. {
  707. return HAL_TIMEOUT;
  708. }
  709. }
  710. }
  711. }
  712. /*-------------------------------- PLL1 Configuration -----------------------*/
  713. /* Check the parameters */
  714. assert_param(IS_RCC_PLL(pOscInitStruct->PLL.PLLState));
  715. if ((pOscInitStruct->PLL.PLLState) != RCC_PLL_NONE)
  716. {
  717. /* Check if the PLL1 is used as system clock or not */
  718. if (temp_sysclksrc != RCC_SYSCLKSOURCE_STATUS_PLLCLK)
  719. {
  720. if ((pOscInitStruct->PLL.PLLState) == RCC_PLL_ON)
  721. {
  722. /* Check the parameters */
  723. assert_param(IS_RCC_PLL1_SOURCE(pOscInitStruct->PLL.PLLSource));
  724. assert_param(IS_RCC_PLL1_DIVM_VALUE(pOscInitStruct->PLL.PLLM));
  725. assert_param(IS_RCC_PLL1_MULN_VALUE(pOscInitStruct->PLL.PLLN));
  726. assert_param(IS_RCC_PLL1_DIVP_VALUE(pOscInitStruct->PLL.PLLP));
  727. assert_param(IS_RCC_PLL1_DIVQ_VALUE(pOscInitStruct->PLL.PLLQ));
  728. assert_param(IS_RCC_PLL1_DIVR_VALUE(pOscInitStruct->PLL.PLLR));
  729. /* Disable the PLL1. */
  730. __HAL_RCC_PLL1_DISABLE();
  731. /* Get Start Tick*/
  732. tickstart = HAL_GetTick();
  733. /* Wait till PLL1 is disabled */
  734. while (READ_BIT(RCC->CR, RCC_CR_PLL1RDY) != 0U)
  735. {
  736. if ((HAL_GetTick() - tickstart) > RCC_PLL_TIMEOUT_VALUE)
  737. {
  738. return HAL_TIMEOUT;
  739. }
  740. }
  741. /* Configure the PLL1 clock source, multiplication and division factors. */
  742. __HAL_RCC_PLL1_CONFIG(pOscInitStruct->PLL.PLLSource,
  743. pOscInitStruct->PLL.PLLM,
  744. pOscInitStruct->PLL.PLLN,
  745. pOscInitStruct->PLL.PLLP,
  746. pOscInitStruct->PLL.PLLQ,
  747. pOscInitStruct->PLL.PLLR);
  748. assert_param(IS_RCC_PLL1_FRACN_VALUE(pOscInitStruct->PLL.PLLFRACN));
  749. /* Disable PLL1FRACN . */
  750. __HAL_RCC_PLL1_FRACN_DISABLE();
  751. /* Configure PLL PLL1FRACN */
  752. __HAL_RCC_PLL1_FRACN_CONFIG(pOscInitStruct->PLL.PLLFRACN);
  753. /* Enable PLL1FRACN . */
  754. __HAL_RCC_PLL1_FRACN_ENABLE();
  755. assert_param(IS_RCC_PLL1_VCIRGE_VALUE(pOscInitStruct->PLL.PLLRGE));
  756. /* Select PLL1 input reference frequency range: VCI */
  757. __HAL_RCC_PLL1_VCIRANGE(pOscInitStruct->PLL.PLLRGE) ;
  758. assert_param(IS_RCC_PLL1_VCORGE_VALUE(pOscInitStruct->PLL.PLLVCOSEL));
  759. /* Select PLL1 output frequency range : VCO */
  760. __HAL_RCC_PLL1_VCORANGE(pOscInitStruct->PLL.PLLVCOSEL) ;
  761. /* Enable PLL1 System Clock output. */
  762. __HAL_RCC_PLL1_CLKOUT_ENABLE(RCC_PLL1_DIVP);
  763. /* Enable the PLL1. */
  764. __HAL_RCC_PLL1_ENABLE();
  765. /* Get Start Tick*/
  766. tickstart = HAL_GetTick();
  767. /* Wait till PLL1 is ready */
  768. while (READ_BIT(RCC->CR, RCC_CR_PLL1RDY) == 0U)
  769. {
  770. if ((HAL_GetTick() - tickstart) > RCC_PLL_TIMEOUT_VALUE)
  771. {
  772. return HAL_TIMEOUT;
  773. }
  774. }
  775. }
  776. else
  777. {
  778. /* Disable the PLL1. */
  779. __HAL_RCC_PLL1_DISABLE();
  780. /* Get Start Tick*/
  781. tickstart = HAL_GetTick();
  782. /* Wait till PLL1 is disabled */
  783. while (READ_BIT(RCC->CR, RCC_CR_PLL1RDY) != 0U)
  784. {
  785. if ((HAL_GetTick() - tickstart) > RCC_PLL_TIMEOUT_VALUE)
  786. {
  787. return HAL_TIMEOUT;
  788. }
  789. }
  790. /* Unselect PLL1 clock source and disable all PLL1 outputs to save power */
  791. RCC->PLL1CFGR &= ~(RCC_PLL1CFGR_PLL1SRC | RCC_PLL1CFGR_PLL1PEN | RCC_PLL1CFGR_PLL1QEN | RCC_PLL1CFGR_PLL1REN);
  792. }
  793. }
  794. else
  795. {
  796. /* Do not return HAL_ERROR if request repeats the current configuration */
  797. temp1_pllckcfg = RCC->PLL1CFGR;
  798. temp2_pllckcfg = RCC->PLL1DIVR;
  799. if (((pOscInitStruct->PLL.PLLState) == RCC_PLL_OFF) ||
  800. (READ_BIT(temp1_pllckcfg, RCC_PLL1CFGR_PLL1SRC) != pOscInitStruct->PLL.PLLSource) ||
  801. ((READ_BIT(temp1_pllckcfg, RCC_PLL1CFGR_PLL1M) >> \
  802. RCC_PLL1CFGR_PLL1M_Pos) != (pOscInitStruct->PLL.PLLM)) ||
  803. (READ_BIT(temp2_pllckcfg, RCC_PLL1DIVR_PLL1N) != (pOscInitStruct->PLL.PLLN - 1U)) ||
  804. ((READ_BIT(temp2_pllckcfg, RCC_PLL1DIVR_PLL1P) >> \
  805. RCC_PLL1DIVR_PLL1P_Pos) != (pOscInitStruct->PLL.PLLP - 1U)) ||
  806. ((READ_BIT(temp2_pllckcfg, RCC_PLL1DIVR_PLL1Q) >> \
  807. RCC_PLL1DIVR_PLL1Q_Pos) != (pOscInitStruct->PLL.PLLQ - 1U)) ||
  808. ((READ_BIT(temp2_pllckcfg, RCC_PLL1DIVR_PLL1R) >> \
  809. RCC_PLL1DIVR_PLL1R_Pos) != (pOscInitStruct->PLL.PLLR - 1U)))
  810. {
  811. return HAL_ERROR;
  812. }
  813. /* FRACN1 on-the-fly value update */
  814. if ((READ_BIT(RCC->PLL1FRACR, RCC_PLL1FRACR_PLL1FRACN) >> \
  815. RCC_PLL1FRACR_PLL1FRACN_Pos) != (pOscInitStruct->PLL.PLLFRACN))
  816. {
  817. assert_param(IS_RCC_PLL1_FRACN_VALUE(pOscInitStruct->PLL.PLLFRACN));
  818. /* Disable PLL1FRACN . */
  819. __HAL_RCC_PLL1_FRACN_DISABLE();
  820. /* Get Start Tick*/
  821. tickstart = HAL_GetTick();
  822. /* Wait at least 2 CK_REF (PLL input source divided by M) period to make sure next latched value
  823. will be taken into account. */
  824. while ((HAL_GetTick() - tickstart) < RCC_PLL_FRAC_WAIT_VALUE)
  825. {
  826. }
  827. /* Configure PLL PLL1FRACN */
  828. __HAL_RCC_PLL1_FRACN_CONFIG(pOscInitStruct->PLL.PLLFRACN);
  829. /* Enable PLL1FRACN to latch the new value. */
  830. __HAL_RCC_PLL1_FRACN_ENABLE();
  831. }
  832. }
  833. }
  834. return HAL_OK;
  835. }
  836. /**
  837. * @brief Initialize the CPU, AHB and APB busses clocks according to the specified
  838. * parameters in the pClkInitStruct.
  839. * @param pClkInitStruct pointer to an RCC_OscInitTypeDef structure that
  840. * contains the configuration information for the RCC peripheral.
  841. * @param FLatency FLASH Latency
  842. * This parameter can be one of the following values:
  843. * @arg FLASH_LATENCY_0 FLASH 0 Latency cycle
  844. * @arg FLASH_LATENCY_1 FLASH 1 Latency cycle
  845. * @arg FLASH_LATENCY_2 FLASH 2 Latency cycles
  846. * @arg FLASH_LATENCY_3 FLASH 3 Latency cycles
  847. * @arg FLASH_LATENCY_4 FLASH 4 Latency cycles
  848. * @arg FLASH_LATENCY_5 FLASH 5 Latency cycles
  849. *
  850. * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency
  851. * and updated by HAL_RCC_GetHCLKFreq() function called within this function
  852. *
  853. * @note The HSI is used by default as system clock source after
  854. * startup from Reset, wake-up from STANDBY mode. After restart from Reset,
  855. * the HSI frequency is set to its default value 64 MHz.
  856. *
  857. * @note The HSI or CSI can be selected as system clock source after wake-up
  858. * from STOP modes or in case of failure of the HSE when used directly or indirectly
  859. * as system clock (if the Clock Security System CSS is enabled).
  860. *
  861. * @note A switch from one clock source to another occurs only if the target
  862. * clock source is ready (clock stable after startup delay or PLL locked).
  863. * If a clock source which is not yet ready is selected, the switch will
  864. * occur when the clock source is ready.
  865. *
  866. * @note You can use HAL_RCC_GetClockConfig() function to know which clock is
  867. * currently used as system clock source.
  868. *
  869. * @retval HAL Status.
  870. */
  871. HAL_StatusTypeDef HAL_RCC_ClockConfig(const RCC_ClkInitTypeDef *pClkInitStruct, uint32_t FLatency)
  872. {
  873. HAL_StatusTypeDef halstatus;
  874. uint32_t tickstart;
  875. /* Check Null pointer */
  876. if (pClkInitStruct == NULL)
  877. {
  878. return HAL_ERROR;
  879. }
  880. /* Check the parameters */
  881. assert_param(IS_RCC_CLOCKTYPE(pClkInitStruct->ClockType));
  882. assert_param(IS_FLASH_LATENCY(FLatency));
  883. /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
  884. must be correctly programmed according to the frequency of the CPU clock
  885. (HCLK) and the supply voltage of the device. */
  886. /* Increasing the number of wait states because of higher CPU frequency */
  887. if (FLatency > __HAL_FLASH_GET_LATENCY())
  888. {
  889. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  890. __HAL_FLASH_SET_LATENCY(FLatency);
  891. /* Check that the new number of wait states is taken into account to access the Flash
  892. memory by reading the FLASH_ACR register */
  893. if (__HAL_FLASH_GET_LATENCY() != FLatency)
  894. {
  895. return HAL_ERROR;
  896. }
  897. }
  898. /* Increasing the BUS frequency divider */
  899. /*-------------------------- PCLK3 Configuration ---------------------------*/
  900. if (((pClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK3) == RCC_CLOCKTYPE_PCLK3)
  901. {
  902. if ((pClkInitStruct->APB3CLKDivider) > ((RCC->CFGR2 & RCC_CFGR2_PPRE3) >> 8))
  903. {
  904. assert_param(IS_RCC_PCLK(pClkInitStruct->APB3CLKDivider));
  905. MODIFY_REG(RCC->CFGR2, RCC_CFGR2_PPRE3, ((pClkInitStruct->APB3CLKDivider) << 8));
  906. }
  907. }
  908. /*-------------------------- PCLK2 Configuration ---------------------------*/
  909. if (((pClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK2) == RCC_CLOCKTYPE_PCLK2)
  910. {
  911. if ((pClkInitStruct->APB2CLKDivider) > ((RCC->CFGR2 & RCC_CFGR2_PPRE2) >> 4))
  912. {
  913. assert_param(IS_RCC_PCLK(pClkInitStruct->APB2CLKDivider));
  914. MODIFY_REG(RCC->CFGR2, RCC_CFGR2_PPRE2, ((pClkInitStruct->APB2CLKDivider) << 4));
  915. }
  916. }
  917. /*-------------------------- PCLK1 Configuration ---------------------------*/
  918. if (((pClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
  919. {
  920. if ((pClkInitStruct->APB1CLKDivider) > (RCC->CFGR2 & RCC_CFGR2_PPRE1))
  921. {
  922. assert_param(IS_RCC_PCLK(pClkInitStruct->APB1CLKDivider));
  923. MODIFY_REG(RCC->CFGR2, RCC_CFGR2_PPRE1, pClkInitStruct->APB1CLKDivider);
  924. }
  925. }
  926. /*-------------------------- HCLK Configuration --------------------------*/
  927. if (((pClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)
  928. {
  929. if ((pClkInitStruct->AHBCLKDivider) > (RCC->CFGR2 & RCC_CFGR2_HPRE))
  930. {
  931. assert_param(IS_RCC_HCLK(pClkInitStruct->AHBCLKDivider));
  932. MODIFY_REG(RCC->CFGR2, RCC_CFGR2_HPRE, pClkInitStruct->AHBCLKDivider);
  933. }
  934. }
  935. /*------------------------- SYSCLK Configuration ---------------------------*/
  936. if (((pClkInitStruct->ClockType) & RCC_CLOCKTYPE_SYSCLK) == RCC_CLOCKTYPE_SYSCLK)
  937. {
  938. assert_param(IS_RCC_SYSCLKSOURCE(pClkInitStruct->SYSCLKSource));
  939. /* PLL is selected as System Clock Source */
  940. if (pClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
  941. {
  942. /* Check the PLL ready flag */
  943. if (READ_BIT(RCC->CR, RCC_CR_PLL1RDY) == 0U)
  944. {
  945. return HAL_ERROR;
  946. }
  947. }
  948. else
  949. {
  950. /* HSE is selected as System Clock Source */
  951. if (pClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
  952. {
  953. /* Check the HSE ready flag */
  954. if (READ_BIT(RCC->CR, RCC_CR_HSERDY) == 0U)
  955. {
  956. return HAL_ERROR;
  957. }
  958. }
  959. /* CSI is selected as System Clock Source */
  960. else if (pClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_CSI)
  961. {
  962. /* Check the CSI ready flag */
  963. if (READ_BIT(RCC->CR, RCC_CR_CSIRDY) == 0U)
  964. {
  965. return HAL_ERROR;
  966. }
  967. }
  968. /* HSI is selected as System Clock Source */
  969. else
  970. {
  971. /* Check the HSI ready flag */
  972. if (READ_BIT(RCC->CR, RCC_CR_HSIRDY) == 0U)
  973. {
  974. return HAL_ERROR;
  975. }
  976. }
  977. }
  978. MODIFY_REG(RCC->CFGR1, RCC_CFGR1_SW, pClkInitStruct->SYSCLKSource);
  979. /* Get Start Tick*/
  980. tickstart = HAL_GetTick();
  981. if (pClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
  982. {
  983. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_PLLCLK)
  984. {
  985. if ((HAL_GetTick() - tickstart) > RCC_CLOCKSWITCH_TIMEOUT_VALUE)
  986. {
  987. return HAL_TIMEOUT;
  988. }
  989. }
  990. }
  991. else
  992. {
  993. if (pClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
  994. {
  995. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_HSE)
  996. {
  997. if ((HAL_GetTick() - tickstart) > RCC_CLOCKSWITCH_TIMEOUT_VALUE)
  998. {
  999. return HAL_TIMEOUT;
  1000. }
  1001. }
  1002. }
  1003. else if (pClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_CSI)
  1004. {
  1005. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_CSI)
  1006. {
  1007. if ((HAL_GetTick() - tickstart) > RCC_CLOCKSWITCH_TIMEOUT_VALUE)
  1008. {
  1009. return HAL_TIMEOUT;
  1010. }
  1011. }
  1012. }
  1013. else
  1014. {
  1015. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_HSI)
  1016. {
  1017. if ((HAL_GetTick() - tickstart) > RCC_CLOCKSWITCH_TIMEOUT_VALUE)
  1018. {
  1019. return HAL_TIMEOUT;
  1020. }
  1021. }
  1022. }
  1023. }
  1024. }
  1025. /* Decreasing the BUS frequency divider */
  1026. /*-------------------------- HCLK Configuration --------------------------*/
  1027. if (((pClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)
  1028. {
  1029. if ((pClkInitStruct->AHBCLKDivider) < (RCC->CFGR2 & RCC_CFGR2_HPRE))
  1030. {
  1031. assert_param(IS_RCC_HCLK(pClkInitStruct->AHBCLKDivider));
  1032. MODIFY_REG(RCC->CFGR2, RCC_CFGR2_HPRE, pClkInitStruct->AHBCLKDivider);
  1033. }
  1034. }
  1035. /* Decreasing the number of wait states because of lower CPU frequency */
  1036. if (FLatency < __HAL_FLASH_GET_LATENCY())
  1037. {
  1038. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  1039. __HAL_FLASH_SET_LATENCY(FLatency);
  1040. /* Check that the new number of wait states is taken into account to access the Flash
  1041. memory by reading the FLASH_ACR register */
  1042. if (__HAL_FLASH_GET_LATENCY() != FLatency)
  1043. {
  1044. return HAL_ERROR;
  1045. }
  1046. }
  1047. /*-------------------------- PCLK1 Configuration ---------------------------*/
  1048. if (((pClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
  1049. {
  1050. if ((pClkInitStruct->APB1CLKDivider) < (RCC->CFGR2 & RCC_CFGR2_PPRE1))
  1051. {
  1052. assert_param(IS_RCC_PCLK(pClkInitStruct->APB1CLKDivider));
  1053. MODIFY_REG(RCC->CFGR2, RCC_CFGR2_PPRE1, pClkInitStruct->APB1CLKDivider);
  1054. }
  1055. }
  1056. /*-------------------------- PCLK2 Configuration ---------------------------*/
  1057. if (((pClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK2) == RCC_CLOCKTYPE_PCLK2)
  1058. {
  1059. if ((pClkInitStruct->APB2CLKDivider) < ((RCC->CFGR2 & RCC_CFGR2_PPRE2) >> 4))
  1060. {
  1061. assert_param(IS_RCC_PCLK(pClkInitStruct->APB2CLKDivider));
  1062. MODIFY_REG(RCC->CFGR2, RCC_CFGR2_PPRE2, ((pClkInitStruct->APB2CLKDivider) << 4));
  1063. }
  1064. }
  1065. /*-------------------------- PCLK3 Configuration ---------------------------*/
  1066. if (((pClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK3) == RCC_CLOCKTYPE_PCLK3)
  1067. {
  1068. if ((pClkInitStruct->APB3CLKDivider) < ((RCC->CFGR2 & RCC_CFGR2_PPRE3) >> 8))
  1069. {
  1070. assert_param(IS_RCC_PCLK(pClkInitStruct->APB3CLKDivider));
  1071. MODIFY_REG(RCC->CFGR2, RCC_CFGR2_PPRE3, ((pClkInitStruct->APB3CLKDivider) << 8));
  1072. }
  1073. }
  1074. /* Update the SystemCoreClock global variable */
  1075. SystemCoreClock = HAL_RCC_GetSysClockFreq() >> AHBPrescTable[(RCC->CFGR2 & RCC_CFGR2_HPRE) >> RCC_CFGR2_HPRE_Pos];
  1076. /* Configure the source of time base considering new system clocks settings*/
  1077. halstatus = HAL_InitTick(uwTickPrio);
  1078. return halstatus;
  1079. }
  1080. /**
  1081. * @}
  1082. */
  1083. /** @defgroup RCC_Exported_Functions_Group2 Peripheral Control functions
  1084. * @brief RCC clocks control functions
  1085. *
  1086. @verbatim
  1087. ===============================================================================
  1088. ##### Peripheral Control functions #####
  1089. ===============================================================================
  1090. [..]
  1091. This subsection provides a set of functions allowing to:
  1092. (+) Output clock to MCO pin.
  1093. (+) Retrieve current clock frequencies.
  1094. (+) Enable the Clock Security System.
  1095. @endverbatim
  1096. * @{
  1097. */
  1098. /**
  1099. * @brief Select the clock source to output on MCO1 pin(PA8) or on MCO2 pin(PC9).
  1100. * @note PA8/PC9 should be configured in alternate function mode.
  1101. * @param RCC_MCOx specifies the output direction for the clock source.
  1102. * For STM32H5xx family this parameter can have only one value:
  1103. * @arg @ref RCC_MCO1 Clock source to output on MCO1 pin(PA8).
  1104. * @arg @ref RCC_MCO2 Clock source to output on MCO2 pin(PC9).
  1105. * @param RCC_MCOSource specifies the clock source to output.
  1106. * This parameter can be one of the following values:
  1107. * @arg RCC_MCO1SOURCE_HSI: HSI clock selected as MCO1 source
  1108. * @arg RCC_MCO1SOURCE_LSE: LSE clock selected as MCO1 source
  1109. * @arg RCC_MCO1SOURCE_HSE: HSE clock selected as MCO1 source
  1110. * @arg RCC_MCO1SOURCE_PLL1QCLK: PLL1Q clock selected as MCO1 source
  1111. * @arg RCC_MCO1SOURCE_HSI48: HSI48 (48MHZ) selected as MCO1 source
  1112. * @arg RCC_MCO2SOURCE_SYSCLK: System clock (SYSCLK) selected as MCO2 source
  1113. * @arg RCC_MCO2SOURCE_PLL2PCLK: PLL2P clock selected as MCO2 source
  1114. * @arg RCC_MCO2SOURCE_HSE: HSE clock selected as MCO2 source
  1115. * @arg RCC_MCO2SOURCE_PLL1PCLK: PLL1P clock selected as MCO2 source
  1116. * @arg RCC_MCO2SOURCE_CSI: CSI clock selected as MCO2 source
  1117. * @arg RCC_MCO2SOURCE_LSI: LSI clock selected as MCO2 source
  1118. * @param RCC_MCODiv specifies the MCO prescaler.
  1119. * This parameter can be one of the following values:
  1120. * @arg RCC_MCODIV_1 up to RCC_MCODIV_15 : divider applied to MCOx clock
  1121. * @retval None
  1122. */
  1123. void HAL_RCC_MCOConfig(uint32_t RCC_MCOx, uint32_t RCC_MCOSource, uint32_t RCC_MCODiv)
  1124. {
  1125. GPIO_InitTypeDef GPIO_InitStruct;
  1126. /* Check the parameters */
  1127. assert_param(IS_RCC_MCO(RCC_MCOx));
  1128. assert_param(IS_RCC_MCODIV(RCC_MCODiv));
  1129. /* RCC_MCO1 */
  1130. if (RCC_MCOx == RCC_MCO1)
  1131. {
  1132. assert_param(IS_RCC_MCO1SOURCE(RCC_MCOSource));
  1133. /* MCO1 Clock Enable */
  1134. MCO1_CLK_ENABLE();
  1135. /* Configure the MCO1 pin in alternate function mode */
  1136. GPIO_InitStruct.Pin = MCO1_PIN;
  1137. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  1138. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  1139. GPIO_InitStruct.Pull = GPIO_NOPULL;
  1140. GPIO_InitStruct.Alternate = GPIO_AF0_MCO;
  1141. HAL_GPIO_Init(MCO1_GPIO_PORT, &GPIO_InitStruct);
  1142. /* Mask MCO1 and MCO1PRE[3:0] bits then Select MCO1 clock source and pre-scaler */
  1143. MODIFY_REG(RCC->CFGR1, (RCC_CFGR1_MCO1SEL | RCC_CFGR1_MCO1PRE), (RCC_MCOSource | RCC_MCODiv));
  1144. }
  1145. else
  1146. {
  1147. assert_param(IS_RCC_MCO2SOURCE(RCC_MCOSource));
  1148. /* MCO2 Clock Enable */
  1149. MCO2_CLK_ENABLE();
  1150. /* Configure the MCO2 pin in alternate function mode */
  1151. GPIO_InitStruct.Pin = MCO2_PIN;
  1152. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  1153. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  1154. GPIO_InitStruct.Pull = GPIO_NOPULL;
  1155. GPIO_InitStruct.Alternate = GPIO_AF0_MCO;
  1156. HAL_GPIO_Init(MCO2_GPIO_PORT, &GPIO_InitStruct);
  1157. /* Mask MCO2 and MCO2PRE[3:0] bits then Select MCO2 clock source and pre-scaler */
  1158. MODIFY_REG(RCC->CFGR1, (RCC_CFGR1_MCO2SEL | RCC_CFGR1_MCO2PRE), (RCC_MCOSource | (RCC_MCODiv << 7U)));
  1159. }
  1160. }
  1161. /**
  1162. * @brief Return the SYSCLK frequency.
  1163. *
  1164. * @note The system frequency computed by this function may not be the real
  1165. * frequency in the chip. It is calculated based on the predefined
  1166. * constants of the selected clock source:
  1167. * @note If SYSCLK source is HSI, function returns values based on HSI_VALUE(*)
  1168. * @note If SYSCLK source is CSI, function returns values based on CSI_VALUE(**)
  1169. * @note If SYSCLK source is HSE, function returns values based on HSE_VALUE(***)
  1170. * @note If SYSCLK source is PLL, function returns values based on HSI_VALUE(*), CSI_VALUE(**)
  1171. * or HSE_VALUE(***) multiplied/divided by the PLL factors.
  1172. * @note (*) HSI_VALUE is a constant defined in stm32h5xx_hal_conf.h file (default value
  1173. * 64 MHz) but the real value may vary depending on the variations
  1174. * in voltage and temperature.
  1175. * @note (**) CSI_VALUE is a constant defined in stm32h5xx_hal_conf.h file (default value
  1176. * 4 MHz) but the real value may vary depending on the variations
  1177. * in voltage and temperature.
  1178. * @note (***) HSE_VALUE is a constant defined in stm32h5xx_hal_conf.h file (default value
  1179. * 24 MHz), user has to ensure that HSE_VALUE is same as the real
  1180. * frequency of the crystal used. Otherwise, this function may
  1181. * have wrong result.
  1182. *
  1183. * @note The result of this function could be not correct when using fractional
  1184. * value for HSE crystal.
  1185. *
  1186. * @note This function can be used by the user application to compute the
  1187. * baudrate for the communication peripherals or configure other parameters.
  1188. *
  1189. * @note Each time SYSCLK changes, this function must be called to update the
  1190. * right SYSCLK value. Otherwise, any configuration based on this function will be incorrect.
  1191. *
  1192. *
  1193. * @retval SYSCLK frequency
  1194. */
  1195. uint32_t HAL_RCC_GetSysClockFreq(void)
  1196. {
  1197. uint32_t pllsource;
  1198. uint32_t pllp;
  1199. uint32_t pllm;
  1200. uint32_t pllfracen;
  1201. uint32_t sysclockfreq;
  1202. uint32_t hsivalue;
  1203. float_t fracn1;
  1204. float_t pllvco;
  1205. if (__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_CSI)
  1206. {
  1207. /* CSI used as system clock source */
  1208. sysclockfreq = CSI_VALUE;
  1209. }
  1210. else if (__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_HSI)
  1211. {
  1212. /* HSI used as system clock source */
  1213. if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIDIVF) != 0U)
  1214. {
  1215. sysclockfreq = (uint32_t)(HSI_VALUE >> (__HAL_RCC_GET_HSI_DIVIDER() >> RCC_CR_HSIDIV_Pos));
  1216. }
  1217. else
  1218. {
  1219. sysclockfreq = (uint32_t) HSI_VALUE;
  1220. }
  1221. }
  1222. else if (__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_HSE)
  1223. {
  1224. /* HSE used as system clock source */
  1225. sysclockfreq = HSE_VALUE;
  1226. }
  1227. else if (__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_PLLCLK)
  1228. {
  1229. /* PLL used as system clock source */
  1230. /* PLL_VCO = (HSE_VALUE or HSI_VALUE or CSI_VALUE/ PLLM) * PLLN
  1231. SYSCLK = PLL_VCO / PLLR
  1232. */
  1233. pllsource = (RCC->PLL1CFGR & RCC_PLL1CFGR_PLL1SRC);
  1234. pllm = ((RCC->PLL1CFGR & RCC_PLL1CFGR_PLL1M) >> RCC_PLL1CFGR_PLL1M_Pos);
  1235. pllfracen = ((RCC->PLL1CFGR & RCC_PLL1CFGR_PLL1FRACEN) >> RCC_PLL1CFGR_PLL1FRACEN_Pos);
  1236. fracn1 = (float_t)(uint32_t)(pllfracen * ((RCC->PLL1FRACR & \
  1237. RCC_PLL1FRACR_PLL1FRACN) >> RCC_PLL1FRACR_PLL1FRACN_Pos));
  1238. if (pllm != 0U)
  1239. {
  1240. switch (pllsource)
  1241. {
  1242. case RCC_PLL1_SOURCE_HSI: /* HSI used as PLL1 clock source */
  1243. if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIDIVF) != 0U)
  1244. {
  1245. hsivalue = (HSI_VALUE >> (__HAL_RCC_GET_HSI_DIVIDER() >> RCC_CR_HSIDIV_Pos));
  1246. pllvco = ((float_t)hsivalue / (float_t)pllm) * ((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_PLL1N) + \
  1247. (fracn1 / (float_t)0x2000) + (float_t)1);
  1248. }
  1249. else
  1250. {
  1251. pllvco = ((float_t)HSI_VALUE / (float_t)pllm) * ((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_PLL1N) + \
  1252. (fracn1 / (float_t)0x2000) + (float_t)1);
  1253. }
  1254. break;
  1255. case RCC_PLL1_SOURCE_HSE: /* HSE used as PLL1 clock source */
  1256. pllvco = ((float_t)HSE_VALUE / (float_t)pllm) * ((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_PLL1N) + \
  1257. (fracn1 / (float_t)0x2000) + (float_t)1);
  1258. break;
  1259. case RCC_PLL1_SOURCE_CSI: /* CSI used as PLL1 clock source */
  1260. default:
  1261. pllvco = ((float_t)CSI_VALUE / (float_t)pllm) * ((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_PLL1N) + \
  1262. (fracn1 / (float_t)0x2000) + (float_t)1);
  1263. break;
  1264. }
  1265. pllp = (((RCC->PLL1DIVR & RCC_PLL1DIVR_PLL1P) >> RCC_PLL1DIVR_PLL1P_Pos) + 1U) ;
  1266. sysclockfreq = (uint32_t)(float_t)(pllvco / (float_t)pllp);
  1267. }
  1268. else
  1269. {
  1270. sysclockfreq = 0;
  1271. }
  1272. }
  1273. else
  1274. {
  1275. /* HSI is the default system clock source */
  1276. sysclockfreq = (uint32_t) HSI_VALUE;
  1277. }
  1278. return sysclockfreq;
  1279. }
  1280. /**
  1281. * @brief Return the HCLK frequency.
  1282. * @note Each time HCLK changes, this function must be called to update the
  1283. * right HCLK value. Otherwise, any configuration based on this function will be incorrect.
  1284. *
  1285. * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency.
  1286. * @retval HCLK frequency in Hz
  1287. */
  1288. uint32_t HAL_RCC_GetHCLKFreq(void)
  1289. {
  1290. SystemCoreClock = HAL_RCC_GetSysClockFreq() >> (AHBPrescTable[(RCC->CFGR2 & RCC_CFGR2_HPRE) \
  1291. >> RCC_CFGR2_HPRE_Pos] & 0x1FU);
  1292. return SystemCoreClock;
  1293. }
  1294. /**
  1295. * @brief Return the PCLK1 frequency.
  1296. * @note Each time PCLK1 changes, this function must be called to update the
  1297. * right PCLK1 value. Otherwise, any configuration based on this function will be incorrect.
  1298. * @retval PCLK1 frequency in Hz
  1299. */
  1300. uint32_t HAL_RCC_GetPCLK1Freq(void)
  1301. {
  1302. /* Get HCLK source and Compute PCLK1 frequency ---------------------------*/
  1303. return (HAL_RCC_GetHCLKFreq() >> ((APBPrescTable[(RCC->CFGR2 & RCC_CFGR2_PPRE1) >> RCC_CFGR2_PPRE1_Pos]) & 0x1FU));
  1304. }
  1305. /**
  1306. * @brief Return the PCLK2 frequency.
  1307. * @note Each time PCLK2 changes, this function must be called to update the
  1308. * right PCLK2 value. Otherwise, any configuration based on this function will be incorrect.
  1309. * @retval PCLK2 frequency in Hz
  1310. */
  1311. uint32_t HAL_RCC_GetPCLK2Freq(void)
  1312. {
  1313. /* Get HCLK source and Compute PCLK2 frequency ---------------------------*/
  1314. return (HAL_RCC_GetHCLKFreq() >> ((APBPrescTable[(RCC->CFGR2 & RCC_CFGR2_PPRE2) >> RCC_CFGR2_PPRE2_Pos]) & 0x1FU));
  1315. }
  1316. /**
  1317. * @brief Return the PCLK3 frequency.
  1318. * @note Each time PCLK3 changes, this function must be called to update the
  1319. * right PCLK3 value. Otherwise, any configuration based on this function will be incorrect.
  1320. * @retval PCLK3 frequency in Hz
  1321. */
  1322. uint32_t HAL_RCC_GetPCLK3Freq(void)
  1323. {
  1324. /* Get HCLK source and Compute PCLK3 frequency ---------------------------*/
  1325. return (HAL_RCC_GetHCLKFreq() >> ((APBPrescTable[(RCC->CFGR2 & RCC_CFGR2_PPRE3) >> RCC_CFGR2_PPRE3_Pos]) & 0x1FU));
  1326. }
  1327. /**
  1328. * @brief Configure the pOscInitStruct according to the internal
  1329. * RCC configuration registers.
  1330. * @param pOscInitStruct pointer to an RCC_OscInitTypeDef structure that
  1331. * will be configured.
  1332. * @retval None
  1333. */
  1334. void HAL_RCC_GetOscConfig(RCC_OscInitTypeDef *pOscInitStruct)
  1335. {
  1336. uint32_t regval;
  1337. uint32_t reg1val;
  1338. uint32_t reg2val;
  1339. /* Check the parameters */
  1340. assert_param(pOscInitStruct != (void *)NULL);
  1341. /* Set all possible values for the Oscillator type parameter ---------------*/
  1342. pOscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_CSI | \
  1343. RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_HSI48;
  1344. /* Get Control register */
  1345. regval = RCC->CR;
  1346. /* Get the HSE configuration -----------------------------------------------*/
  1347. pOscInitStruct->HSEState = (regval & (RCC_CR_HSEON | RCC_CR_HSEBYP | RCC_CR_HSEEXT));
  1348. /* Get the CSI configuration -----------------------------------------------*/
  1349. pOscInitStruct->CSIState = regval & RCC_CR_CSION;
  1350. /* Get the HSI configuration -----------------------------------------------*/
  1351. pOscInitStruct->HSIState = regval & RCC_CR_HSION;
  1352. pOscInitStruct->HSIDiv = regval & RCC_CR_HSIDIV;
  1353. pOscInitStruct->HSICalibrationValue = (uint32_t)(READ_BIT(RCC->HSICFGR, \
  1354. RCC_HSICFGR_HSITRIM) >> RCC_HSICFGR_HSITRIM_Pos);
  1355. /* Get BDCR register */
  1356. regval = RCC->BDCR;
  1357. /* Get the LSE configuration -----------------------------------------------*/
  1358. pOscInitStruct->LSEState = (regval & (RCC_BDCR_LSEON | RCC_BDCR_LSEBYP | RCC_BDCR_LSEEXT));
  1359. /* Get the LSI configuration -----------------------------------------------*/
  1360. pOscInitStruct->LSIState = regval & RCC_BDCR_LSION;
  1361. /* Get Control register */
  1362. regval = RCC->CR;
  1363. /* Get the HSI48 configuration ---------------------------------------------*/
  1364. pOscInitStruct->HSI48State = regval & RCC_CR_HSI48ON;
  1365. /* Get the PLL configuration -----------------------------------------------*/
  1366. if ((regval & RCC_CR_PLL1ON) == RCC_CR_PLL1ON)
  1367. {
  1368. pOscInitStruct->PLL.PLLState = RCC_PLL_ON;
  1369. }
  1370. else
  1371. {
  1372. pOscInitStruct->PLL.PLLState = RCC_PLL_OFF;
  1373. }
  1374. /* Get PLL configuration register */
  1375. reg1val = RCC->PLL1CFGR;
  1376. reg2val = RCC->PLL1DIVR;
  1377. pOscInitStruct->PLL.PLLSource = (uint32_t)(reg1val & RCC_PLL1CFGR_PLL1SRC);
  1378. pOscInitStruct->PLL.PLLM = (uint32_t)((reg1val & RCC_PLL1CFGR_PLL1M) >> RCC_PLL1CFGR_PLL1M_Pos);
  1379. pOscInitStruct->PLL.PLLN = (uint32_t)(((reg2val & RCC_PLL1DIVR_PLL1N) >> RCC_PLL1DIVR_PLL1N_Pos) + 1U);
  1380. pOscInitStruct->PLL.PLLQ = (uint32_t)(((reg2val & RCC_PLL1DIVR_PLL1Q) >> RCC_PLL1DIVR_PLL1Q_Pos) + 1U);
  1381. pOscInitStruct->PLL.PLLR = (uint32_t)(((reg2val & RCC_PLL1DIVR_PLL1R) >> RCC_PLL1DIVR_PLL1R_Pos) + 1U);
  1382. pOscInitStruct->PLL.PLLP = (uint32_t)(((reg2val & RCC_PLL1DIVR_PLL1P) >> RCC_PLL1DIVR_PLL1P_Pos) + 1U);
  1383. pOscInitStruct->PLL.PLLRGE = (uint32_t)((reg1val & RCC_PLL1CFGR_PLL1RGE));
  1384. pOscInitStruct->PLL.PLLVCOSEL = (uint32_t)((reg1val & RCC_PLL1CFGR_PLL1VCOSEL) >> RCC_PLL1CFGR_PLL1VCOSEL_Pos);
  1385. pOscInitStruct->PLL.PLLFRACN = (uint32_t)(((RCC->PLL1FRACR & RCC_PLL1FRACR_PLL1FRACN) \
  1386. >> RCC_PLL1FRACR_PLL1FRACN_Pos));
  1387. }
  1388. /**
  1389. * @brief Configure the pClkInitStruct according to the internal
  1390. * RCC configuration registers.
  1391. * @param pClkInitStruct pointer to an RCC_ClkInitTypeDef structure that
  1392. * will be configured.
  1393. * @param pFLatency Pointer on the Flash Latency.
  1394. * @retval None
  1395. */
  1396. void HAL_RCC_GetClockConfig(RCC_ClkInitTypeDef *pClkInitStruct, uint32_t *pFLatency)
  1397. {
  1398. uint32_t regval;
  1399. /* Check the parameters */
  1400. assert_param(pClkInitStruct != (void *)NULL);
  1401. assert_param(pFLatency != (void *)NULL);
  1402. /* Set all possible values for the Clock type parameter --------------------*/
  1403. pClkInitStruct->ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2 | \
  1404. RCC_CLOCKTYPE_PCLK3;
  1405. /* Get the SYSCLK configuration --------------------------------------------*/
  1406. pClkInitStruct->SYSCLKSource = (uint32_t)(RCC->CFGR1 & RCC_CFGR1_SW);
  1407. /* Get the HCLK configuration ----------------------------------------------*/
  1408. regval = RCC->CFGR2;
  1409. pClkInitStruct->AHBCLKDivider = (uint32_t)(regval & RCC_CFGR2_HPRE);
  1410. /* Get the APB1 configuration ----------------------------------------------*/
  1411. pClkInitStruct->APB1CLKDivider = (uint32_t)(regval & RCC_CFGR2_PPRE1);
  1412. /* Get the APB2 configuration ----------------------------------------------*/
  1413. pClkInitStruct->APB2CLKDivider = (uint32_t)((regval & RCC_CFGR2_PPRE2) >> 4);
  1414. /* Get the APB3 configuration ----------------------------------------------*/
  1415. pClkInitStruct->APB3CLKDivider = (uint32_t)((regval & RCC_CFGR2_PPRE3) >> 8);
  1416. /* Get the Flash Wait State (Latency) configuration ------------------------*/
  1417. *pFLatency = (uint32_t)(FLASH->ACR & FLASH_ACR_LATENCY);
  1418. }
  1419. /**
  1420. * @brief Get and clear reset flags
  1421. * @note Once reset flags are retrieved, this API is clearing them in order
  1422. * to isolate next reset reason.
  1423. * @retval can be a combination of @ref RCC_Reset_Flag
  1424. */
  1425. uint32_t HAL_RCC_GetResetSource(void)
  1426. {
  1427. uint32_t reset;
  1428. /* Get all reset flags */
  1429. reset = RCC->RSR & RCC_RESET_FLAG_ALL;
  1430. /* Clear Reset flags */
  1431. RCC->RSR |= RCC_RSR_RMVF;
  1432. return reset;
  1433. }
  1434. /**
  1435. * @brief Enable the HSE Clock Security System.
  1436. * @note If a failure is detected on the HSE oscillator clock, this oscillator
  1437. * is automatically disabled and an interrupt is generated to inform the
  1438. * software about the failure (Clock Security System Interrupt, CSSI),
  1439. * allowing the MCU to perform rescue operations. The CSSI is linked to
  1440. * the Cortex-M NMI (Non-Maskable Interrupt) exception vector.
  1441. * @note The Clock Security System can only be cleared by reset.
  1442. * @retval None
  1443. */
  1444. void HAL_RCC_EnableCSS(void)
  1445. {
  1446. SET_BIT(RCC->CR, RCC_CR_HSECSSON);
  1447. }
  1448. /**
  1449. * @brief Handle the RCC Clock Security System interrupt request.
  1450. * @note This API should be called under the NMI_Handler().
  1451. * @retval None
  1452. */
  1453. void HAL_RCC_NMI_IRQHandler(void)
  1454. {
  1455. /* Check RCC CSSF interrupt flag */
  1456. if (__HAL_RCC_GET_IT(RCC_IT_HSECSS))
  1457. {
  1458. /* Clear RCC CSS pending bit */
  1459. __HAL_RCC_CLEAR_IT(RCC_IT_HSECSS);
  1460. /* RCC Clock Security System interrupt user callback */
  1461. HAL_RCC_CSSCallback();
  1462. }
  1463. }
  1464. /**
  1465. * @brief RCC HSE Clock Security System interrupt callback.
  1466. * @retval none
  1467. */
  1468. __weak void HAL_RCC_CSSCallback(void)
  1469. {
  1470. /* NOTE : This function should not be modified, when the callback is needed,
  1471. the HAL_RCC_CSSCallback should be implemented in the user file
  1472. */
  1473. }
  1474. /**
  1475. * @}
  1476. */
  1477. /** @defgroup RCC_Exported_Functions_Group3 Attributes management functions
  1478. * @brief Attributes management functions.
  1479. *
  1480. @verbatim
  1481. ===============================================================================
  1482. ##### RCC attributes functions #####
  1483. ===============================================================================
  1484. [..]
  1485. This subsection provides a set of functions allowing to:
  1486. (+) Configure the RCC item(s) attributes.
  1487. (+) Get the attribute of an RCC item.
  1488. @endverbatim
  1489. * @{
  1490. */
  1491. /**
  1492. * @brief Configure the RCC item(s) attribute(s).
  1493. * @note Available attributes are to secure items and set RCC as privileged (*).
  1494. * Default state is non-secure and unprivileged access allowed.
  1495. * @note Secure and non-secure attributes can only be set from the secure
  1496. * state when the system implements the security (TZEN=1).
  1497. * @note As the privileged attributes concern either all secure or all non-secure
  1498. * RCC resources accesses and not each RCC individual items access attribute,
  1499. * the application must ensure that the privilege access attribute configurations
  1500. * are coherent amongst the security level set on RCC individual items
  1501. * so not to overwrite a previous more restricted access rule (consider either
  1502. * all secure and/or all non-secure RCC resources accesses by privileged-only
  1503. * transactions or privileged and unprivileged transactions).
  1504. * @param Item Item(s) to set attributes on.
  1505. * This parameter can be a one or a combination of @ref RCC_items (**).
  1506. * @param Attributes specifies the RCC secure/privilege attributes.
  1507. * This parameter can be a value of @ref RCC_attributes
  1508. * @retval None
  1509. *
  1510. * (*) : For stm32h503xx devices, attributes specifies the privilege attribute only (no items).
  1511. * (**) : For stm32h503xx devices, this parameter is unused, it can take 0 or any other numerical value.
  1512. */
  1513. void HAL_RCC_ConfigAttributes(uint32_t Item, uint32_t Attributes)
  1514. {
  1515. /* Check the parameters */
  1516. assert_param(IS_RCC_ATTRIBUTES(Attributes));
  1517. #if defined(RCC_SECCFGR_HSISEC)
  1518. assert_param(IS_RCC_ITEM_ATTRIBUTES(Item));
  1519. switch (Attributes)
  1520. {
  1521. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  1522. /* Secure Privilege attribute */
  1523. case RCC_SEC_PRIV:
  1524. SET_BIT(RCC->SECCFGR, Item);
  1525. SET_BIT(RCC->PRIVCFGR, RCC_PRIVCFGR_SPRIV);
  1526. break;
  1527. /* Secure Non-Privilege attribute */
  1528. case RCC_SEC_NPRIV:
  1529. SET_BIT(RCC->SECCFGR, Item);
  1530. CLEAR_BIT(RCC->PRIVCFGR, RCC_PRIVCFGR_SPRIV);
  1531. break;
  1532. /* Non-secure Privilege attribute */
  1533. case RCC_NSEC_PRIV:
  1534. CLEAR_BIT(RCC->SECCFGR, Item);
  1535. SET_BIT(RCC->PRIVCFGR, RCC_PRIVCFGR_NSPRIV);
  1536. break;
  1537. /* Non-secure Non-Privilege attribute */
  1538. case RCC_NSEC_NPRIV:
  1539. CLEAR_BIT(RCC->SECCFGR, Item);
  1540. CLEAR_BIT(RCC->PRIVCFGR, RCC_PRIVCFGR_NSPRIV);
  1541. break;
  1542. #else /* __ARM_FEATURE_CMSE */
  1543. /* Non-secure Privilege attribute */
  1544. case RCC_NSEC_PRIV:
  1545. SET_BIT(RCC->PRIVCFGR, RCC_PRIVCFGR_NSPRIV);
  1546. break;
  1547. /* Non-secure Non-Privilege attribute */
  1548. case RCC_NSEC_NPRIV:
  1549. CLEAR_BIT(RCC->PRIVCFGR, RCC_PRIVCFGR_NSPRIV);
  1550. break;
  1551. #endif /* __ARM_FEATURE_CMSE */
  1552. default:
  1553. /* Nothing to do */
  1554. break;
  1555. }
  1556. #else /* RCC_SECCFGR_HSISEC */
  1557. UNUSED(Item);
  1558. switch (Attributes)
  1559. {
  1560. /* Privilege attribute */
  1561. case RCC_PRIV:
  1562. SET_BIT(RCC->PRIVCFGR, RCC_PRIVCFGR_PRIV);
  1563. break;
  1564. /* Non-secure Non-Privilege attribute */
  1565. case RCC_NPRIV:
  1566. CLEAR_BIT(RCC->PRIVCFGR, RCC_PRIVCFGR_PRIV);
  1567. break;
  1568. default:
  1569. /* Nothing to do */
  1570. break;
  1571. }
  1572. #endif /* RCC_SECCFGR_HSISEC */
  1573. }
  1574. /**
  1575. * @brief Get the attribute of an RCC item.
  1576. * @note Secure and non-secure attributes are only available from secure state
  1577. * when the system implements the security (TZEN=1)
  1578. * @param Item Single item to get secure/non-secure and privilege/non-privilege attribute from.
  1579. * This parameter can be a one value of @ref RCC_items except RCC_ALL. (*)
  1580. * @param pAttributes pointer to return the attributes.
  1581. * @retval HAL Status.
  1582. *
  1583. * (*) : This parameter is unused for stm32h503xx devices, it can take 0 or any other numerical value.
  1584. */
  1585. HAL_StatusTypeDef HAL_RCC_GetConfigAttributes(uint32_t Item, uint32_t *pAttributes)
  1586. {
  1587. uint32_t attributes;
  1588. /* Check null pointer */
  1589. if (pAttributes == NULL)
  1590. {
  1591. return HAL_ERROR;
  1592. }
  1593. #if defined(RCC_SECCFGR_HSISEC)
  1594. /* Check the parameters */
  1595. assert_param(IS_RCC_SINGLE_ITEM_ATTRIBUTES(Item));
  1596. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  1597. /* Check item security */
  1598. if ((RCC->SECCFGR & Item) == Item)
  1599. {
  1600. /* Get Secure privileges attribute */
  1601. attributes = ((RCC->PRIVCFGR & RCC_PRIVCFGR_SPRIV) == 0U) ? RCC_SEC_NPRIV : RCC_SEC_PRIV;
  1602. }
  1603. else
  1604. {
  1605. /* Get Non-Secure privileges attribute */
  1606. attributes = ((RCC->PRIVCFGR & RCC_PRIVCFGR_NSPRIV) == 0U) ? RCC_NSEC_NPRIV : RCC_NSEC_PRIV;
  1607. }
  1608. #else /* __ARM_FEATURE_CMSE */
  1609. attributes = ((RCC->PRIVCFGR & RCC_PRIVCFGR_NSPRIV) == 0U) ? RCC_NSEC_NPRIV : RCC_NSEC_PRIV;
  1610. #endif /* __ARM_FEATURE_CMSE */
  1611. #else /* RCC_SECCFGR_HSISEC */
  1612. UNUSED(Item);
  1613. /* Get privileges attribute */
  1614. attributes = ((RCC->PRIVCFGR & RCC_PRIVCFGR_PRIV) == 0U) ? RCC_NPRIV : RCC_PRIV;
  1615. #endif /* RCC_SECCFGR_HSISEC */
  1616. /* return value */
  1617. *pAttributes = attributes;
  1618. return HAL_OK;
  1619. }
  1620. /**
  1621. * @}
  1622. */
  1623. /**
  1624. * @}
  1625. */
  1626. /* Private function prototypes -----------------------------------------------*/
  1627. #endif /* HAL_RCC_MODULE_ENABLED */
  1628. /**
  1629. * @}
  1630. */
  1631. /**
  1632. * @}
  1633. */