stm32h7xx_hal_rcc.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793
  1. /**
  2. ******************************************************************************
  3. * @file stm32h7xx_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. @verbatim
  12. ==============================================================================
  13. ##### RCC specific features #####
  14. ==============================================================================
  15. [..]
  16. After reset the device is running from Internal High Speed oscillator
  17. (HSI 64MHz) with Flash 0 wait state,and all peripherals are off except
  18. internal SRAM, Flash, JTAG and PWR
  19. (+) There is no pre-scaler on High speed (AHB) and Low speed (APB) buses;
  20. all peripherals mapped on these buses are running at HSI speed.
  21. (+) The clock for all peripherals is switched off, except the SRAM and FLASH.
  22. (+) All GPIOs are in analogue mode , except the JTAG pins which
  23. are assigned to be used for debug purpose.
  24. [..]
  25. Once the device started from reset, the user application has to:
  26. (+) Configure the clock source to be used to drive the System clock
  27. (if the application needs higher frequency/performance)
  28. (+) Configure the System clock frequency and Flash settings
  29. (+) Configure the AHB and APB buses pre-scalers
  30. (+) Enable the clock for the peripheral(s) to be used
  31. (+) Configure the clock kernel source(s) for peripherals which clocks are not
  32. derived from the System clock through :RCC_D1CCIPR,RCC_D2CCIP1R,RCC_D2CCIP2R
  33. and RCC_D3CCIPR registers
  34. ##### RCC Limitations #####
  35. ==============================================================================
  36. [..]
  37. A delay between an RCC peripheral clock enable and the effective peripheral
  38. enabling should be taken into account in order to manage the peripheral read/write
  39. from/to registers.
  40. (+) This delay depends on the peripheral mapping.
  41. (+) If peripheral is mapped on AHB: the delay is 2 AHB clock cycle
  42. after the clock enable bit is set on the hardware register
  43. (+) If peripheral is mapped on APB: the delay is 2 APB clock cycle
  44. after the clock enable bit is set on the hardware register
  45. [..]
  46. Implemented Workaround:
  47. (+) For AHB & APB peripherals, a dummy read to the peripheral register has been
  48. inserted in each __HAL_RCC_PPP_CLK_ENABLE() macro.
  49. @endverbatim
  50. ******************************************************************************
  51. * @attention
  52. *
  53. * Copyright (c) 2017 STMicroelectronics.
  54. * All rights reserved.
  55. *
  56. * This software is licensed under terms that can be found in the LICENSE file in
  57. * the root directory of this software component.
  58. * If no LICENSE file comes with this software, it is provided AS-IS.
  59. ******************************************************************************
  60. */
  61. /* Includes ------------------------------------------------------------------*/
  62. #include "stm32h7xx_hal.h"
  63. /** @addtogroup STM32H7xx_HAL_Driver
  64. * @{
  65. */
  66. /** @defgroup RCC RCC
  67. * @brief RCC HAL module driver
  68. * @{
  69. */
  70. #ifdef HAL_RCC_MODULE_ENABLED
  71. /* Private typedef -----------------------------------------------------------*/
  72. /* Private define ------------------------------------------------------------*/
  73. /* Private macro -------------------------------------------------------------*/
  74. /** @defgroup RCC_Private_Macros RCC Private Macros
  75. * @{
  76. */
  77. #define MCO1_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
  78. #define MCO1_GPIO_PORT GPIOA
  79. #define MCO1_PIN GPIO_PIN_8
  80. #define MCO2_CLK_ENABLE() __HAL_RCC_GPIOC_CLK_ENABLE()
  81. #define MCO2_GPIO_PORT GPIOC
  82. #define MCO2_PIN GPIO_PIN_9
  83. /**
  84. * @}
  85. */
  86. /* Private variables ---------------------------------------------------------*/
  87. /** @defgroup RCC_Private_Variables RCC Private Variables
  88. * @{
  89. */
  90. /**
  91. * @}
  92. */
  93. /* Private function prototypes -----------------------------------------------*/
  94. /* Exported functions --------------------------------------------------------*/
  95. /** @defgroup RCC_Exported_Functions RCC Exported Functions
  96. * @{
  97. */
  98. /** @defgroup RCC_Exported_Functions_Group1 Initialization and de-initialization functions
  99. * @brief Initialization and Configuration functions
  100. *
  101. @verbatim
  102. ===============================================================================
  103. ##### Initialization and de-initialization functions #####
  104. ===============================================================================
  105. [..]
  106. This section provides functions allowing to configure the internal/external oscillators
  107. (HSE, HSI, LSE,CSI, LSI,HSI48, PLL, CSS and MCO) and the System buses clocks (SYSCLK, AHB3, AHB1
  108. AHB2,AHB4,APB3, APB1L, APB1H, APB2, and APB4).
  109. [..] Internal/external clock and PLL configuration
  110. (#) HSI (high-speed internal), 64 MHz factory-trimmed RC used directly or through
  111. the PLL as System clock source.
  112. (#) CSI is a low-power RC oscillator which can be used directly as system clock, peripheral
  113. clock, or PLL input.But even with frequency calibration, is less accurate than an
  114. external crystal oscillator or ceramic resonator.
  115. (#) LSI (low-speed internal), 32 KHz low consumption RC used as IWDG and/or RTC
  116. clock source.
  117. (#) HSE (high-speed external), 4 to 48 MHz crystal oscillator used directly or
  118. through the PLL as System clock source. Can be used also as RTC clock source.
  119. (#) LSE (low-speed external), 32 KHz oscillator used as RTC clock source.
  120. (#) PLL , The RCC features three independent PLLs (clocked by HSI , HSE or CSI),
  121. featuring three different output clocks and able to work either in integer or Fractional mode.
  122. (++) A main PLL, PLL1, which is generally used to provide clocks to the CPU
  123. and to some peripherals.
  124. (++) Two dedicated PLLs, PLL2 and PLL3, which are used to generate the kernel clock for peripherals.
  125. (#) CSS (Clock security system), once enabled and if a HSE clock failure occurs
  126. (HSE used directly or through PLL as System clock source), the System clock
  127. is automatically switched to HSI and an interrupt is generated if enabled.
  128. The interrupt is linked to the Cortex-M NMI (Non-Mask-able Interrupt)
  129. exception vector.
  130. (#) MCO1 (micro controller clock output), used to output HSI, LSE, HSE, PLL1(PLL1_Q)
  131. or HSI48 clock (through a configurable pre-scaler) on PA8 pin.
  132. (#) MCO2 (micro controller clock output), used to output HSE, PLL2(PLL2_P), SYSCLK,
  133. LSI, CSI, or PLL1(PLL1_P) clock (through a configurable pre-scaler) on PC9 pin.
  134. [..] System, AHB and APB buses clocks configuration
  135. (#) Several clock sources can be used to drive the System clock (SYSCLK): CSI,HSI,
  136. HSE and PLL.
  137. The AHB clock (HCLK) is derived from System core clock through configurable
  138. pre-scaler and used to clock the CPU, memory and peripherals mapped
  139. on AHB and APB bus of the 3 Domains (D1, D2, D3)* through configurable pre-scalers
  140. and used to clock the peripherals mapped on these buses. You can use
  141. "HAL_RCC_GetSysClockFreq()" function to retrieve system clock frequency.
  142. -@- All the peripheral clocks are derived from the System clock (SYSCLK) except those
  143. with dual clock domain where kernel source clock could be selected through
  144. RCC_D1CCIPR,RCC_D2CCIP1R,RCC_D2CCIP2R and RCC_D3CCIPR registers.
  145. (*) : 2 Domains (CD and SRD) for stm32h7a3xx and stm32h7b3xx family lines.
  146. @endverbatim
  147. * @{
  148. */
  149. /**
  150. * @brief Resets the RCC clock configuration to the default reset state.
  151. * @note The default reset state of the clock configuration is given below:
  152. * - HSI ON and used as system clock source
  153. * - HSE, PLL1, PLL2 and PLL3 OFF
  154. * - AHB, APB Bus pre-scaler set to 1.
  155. * - CSS, MCO1 and MCO2 OFF
  156. * - All interrupts disabled
  157. * @note This function doesn't modify the configuration of the
  158. * - Peripheral clocks
  159. * - LSI, LSE and RTC clocks
  160. * @retval HAL status
  161. */
  162. HAL_StatusTypeDef HAL_RCC_DeInit(void)
  163. {
  164. uint32_t tickstart;
  165. /* Increasing the CPU frequency */
  166. if(FLASH_LATENCY_DEFAULT > __HAL_FLASH_GET_LATENCY())
  167. {
  168. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  169. __HAL_FLASH_SET_LATENCY(FLASH_LATENCY_DEFAULT);
  170. /* Check that the new number of wait states is taken into account to access the Flash
  171. memory by reading the FLASH_ACR register */
  172. if(__HAL_FLASH_GET_LATENCY() != FLASH_LATENCY_DEFAULT)
  173. {
  174. return HAL_ERROR;
  175. }
  176. }
  177. /* Get Start Tick */
  178. tickstart = HAL_GetTick();
  179. /* Set HSION bit */
  180. SET_BIT(RCC->CR, RCC_CR_HSION);
  181. /* Wait till HSI is ready */
  182. while (READ_BIT(RCC->CR, RCC_CR_HSIRDY) == 0U)
  183. {
  184. if ((HAL_GetTick() - tickstart) > HSI_TIMEOUT_VALUE)
  185. {
  186. return HAL_TIMEOUT;
  187. }
  188. }
  189. /* Set HSITRIM[6:0] bits to the reset value */
  190. SET_BIT(RCC->HSICFGR, RCC_HSICFGR_HSITRIM_6);
  191. /* Reset CFGR register */
  192. CLEAR_REG(RCC->CFGR);
  193. /* Update the SystemCoreClock and SystemD2Clock global variables */
  194. SystemCoreClock = HSI_VALUE;
  195. SystemD2Clock = HSI_VALUE;
  196. /* Adapt Systick interrupt period */
  197. if(HAL_InitTick(uwTickPrio) != HAL_OK)
  198. {
  199. return HAL_ERROR;
  200. }
  201. /* Get Start Tick */
  202. tickstart = HAL_GetTick();
  203. /* Wait till clock switch is ready */
  204. while (READ_BIT(RCC->CFGR, RCC_CFGR_SWS) != 0U)
  205. {
  206. if ((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
  207. {
  208. return HAL_TIMEOUT;
  209. }
  210. }
  211. /* Get Start Tick */
  212. tickstart = HAL_GetTick();
  213. /* Reset CSION, CSIKERON, HSEON, HSI48ON, HSECSSON, HSIDIV bits */
  214. CLEAR_BIT(RCC->CR, RCC_CR_HSEON | RCC_CR_HSIKERON| RCC_CR_HSIDIV| RCC_CR_HSIDIVF| RCC_CR_CSION | RCC_CR_CSIKERON \
  215. | RCC_CR_HSI48ON | RCC_CR_CSSHSEON);
  216. /* Wait till HSE is disabled */
  217. while (READ_BIT(RCC->CR, RCC_CR_HSERDY) != 0U)
  218. {
  219. if ((HAL_GetTick() - tickstart) > HSE_TIMEOUT_VALUE)
  220. {
  221. return HAL_TIMEOUT;
  222. }
  223. }
  224. /* Get Start Tick */
  225. tickstart = HAL_GetTick();
  226. /* Clear PLLON bit */
  227. CLEAR_BIT(RCC->CR, RCC_CR_PLL1ON);
  228. /* Wait till PLL is disabled */
  229. while (READ_BIT(RCC->CR, RCC_CR_PLL1RDY) != 0U)
  230. {
  231. if ((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  232. {
  233. return HAL_TIMEOUT;
  234. }
  235. }
  236. /* Get Start Tick */
  237. tickstart = HAL_GetTick();
  238. /* Reset PLL2ON bit */
  239. CLEAR_BIT(RCC->CR, RCC_CR_PLL2ON);
  240. /* Wait till PLL2 is disabled */
  241. while (READ_BIT(RCC->CR, RCC_CR_PLL2RDY) != 0U)
  242. {
  243. if ((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  244. {
  245. return HAL_TIMEOUT;
  246. }
  247. }
  248. /* Get Start Tick */
  249. tickstart = HAL_GetTick();
  250. /* Reset PLL3 bit */
  251. CLEAR_BIT(RCC->CR, RCC_CR_PLL3ON);
  252. /* Wait till PLL3 is disabled */
  253. while (READ_BIT(RCC->CR, RCC_CR_PLL3RDY) != 0U)
  254. {
  255. if ((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  256. {
  257. return HAL_TIMEOUT;
  258. }
  259. }
  260. #if defined(RCC_D1CFGR_HPRE)
  261. /* Reset D1CFGR register */
  262. CLEAR_REG(RCC->D1CFGR);
  263. /* Reset D2CFGR register */
  264. CLEAR_REG(RCC->D2CFGR);
  265. /* Reset D3CFGR register */
  266. CLEAR_REG(RCC->D3CFGR);
  267. #else
  268. /* Reset CDCFGR1 register */
  269. CLEAR_REG(RCC->CDCFGR1);
  270. /* Reset CDCFGR2 register */
  271. CLEAR_REG(RCC->CDCFGR2);
  272. /* Reset SRDCFGR register */
  273. CLEAR_REG(RCC->SRDCFGR);
  274. #endif
  275. /* Reset PLLCKSELR register to default value */
  276. RCC->PLLCKSELR= RCC_PLLCKSELR_DIVM1_5|RCC_PLLCKSELR_DIVM2_5|RCC_PLLCKSELR_DIVM3_5;
  277. /* Reset PLLCFGR register to default value */
  278. WRITE_REG(RCC->PLLCFGR, 0x01FF0000U);
  279. /* Reset PLL1DIVR register to default value */
  280. WRITE_REG(RCC->PLL1DIVR,0x01010280U);
  281. /* Reset PLL1FRACR register */
  282. CLEAR_REG(RCC->PLL1FRACR);
  283. /* Reset PLL2DIVR register to default value */
  284. WRITE_REG(RCC->PLL2DIVR,0x01010280U);
  285. /* Reset PLL2FRACR register */
  286. CLEAR_REG(RCC->PLL2FRACR);
  287. /* Reset PLL3DIVR register to default value */
  288. WRITE_REG(RCC->PLL3DIVR,0x01010280U);
  289. /* Reset PLL3FRACR register */
  290. CLEAR_REG(RCC->PLL3FRACR);
  291. #if defined(RCC_CR_HSEEXT)
  292. /* Reset HSEEXT */
  293. CLEAR_BIT(RCC->CR, RCC_CR_HSEEXT);
  294. #endif /* RCC_CR_HSEEXT */
  295. /* Reset HSEBYP bit */
  296. CLEAR_BIT(RCC->CR, RCC_CR_HSEBYP);
  297. /* Disable all interrupts */
  298. CLEAR_REG(RCC->CIER);
  299. /* Clear all interrupts flags */
  300. WRITE_REG(RCC->CICR,0xFFFFFFFFU);
  301. /* Reset all RSR flags */
  302. SET_BIT(RCC->RSR, RCC_RSR_RMVF);
  303. /* Decreasing the number of wait states because of lower CPU frequency */
  304. if(FLASH_LATENCY_DEFAULT < __HAL_FLASH_GET_LATENCY())
  305. {
  306. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  307. __HAL_FLASH_SET_LATENCY(FLASH_LATENCY_DEFAULT);
  308. /* Check that the new number of wait states is taken into account to access the Flash
  309. memory by reading the FLASH_ACR register */
  310. if(__HAL_FLASH_GET_LATENCY() != FLASH_LATENCY_DEFAULT)
  311. {
  312. return HAL_ERROR;
  313. }
  314. }
  315. return HAL_OK;
  316. }
  317. /**
  318. * @brief Initializes the RCC Oscillators according to the specified parameters in the
  319. * RCC_OscInitTypeDef.
  320. * @param RCC_OscInitStruct: pointer to an RCC_OscInitTypeDef structure that
  321. * contains the configuration information for the RCC Oscillators.
  322. * @note The PLL is not disabled when used as system clock.
  323. * @note Transitions LSE Bypass to LSE On and LSE On to LSE Bypass are not
  324. * supported by this function. User should request a transition to LSE Off
  325. * first and then LSE On or LSE Bypass.
  326. * @note Transition HSE Bypass to HSE On and HSE On to HSE Bypass are not
  327. * supported by this function. User should request a transition to HSE Off
  328. * first and then HSE On or HSE Bypass.
  329. * @retval HAL status
  330. */
  331. __weak HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  332. {
  333. uint32_t tickstart;
  334. uint32_t temp1_pllckcfg, temp2_pllckcfg;
  335. /* Check Null pointer */
  336. if(RCC_OscInitStruct == NULL)
  337. {
  338. return HAL_ERROR;
  339. }
  340. /* Check the parameters */
  341. assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct->OscillatorType));
  342. /*------------------------------- HSE Configuration ------------------------*/
  343. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE)
  344. {
  345. /* Check the parameters */
  346. assert_param(IS_RCC_HSE(RCC_OscInitStruct->HSEState));
  347. const uint32_t temp_sysclksrc = __HAL_RCC_GET_SYSCLK_SOURCE();
  348. const uint32_t temp_pllckselr = RCC->PLLCKSELR;
  349. /* When the HSE is used as system clock or clock source for PLL in these cases HSE will not disabled */
  350. if((temp_sysclksrc == RCC_CFGR_SWS_HSE) || ((temp_sysclksrc == RCC_CFGR_SWS_PLL1) && ((temp_pllckselr & RCC_PLLCKSELR_PLLSRC) == RCC_PLLCKSELR_PLLSRC_HSE)))
  351. {
  352. if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != 0U) && (RCC_OscInitStruct->HSEState == RCC_HSE_OFF))
  353. {
  354. return HAL_ERROR;
  355. }
  356. }
  357. else
  358. {
  359. /* Set the new HSE configuration ---------------------------------------*/
  360. __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct->HSEState);
  361. /* Check the HSE State */
  362. if(RCC_OscInitStruct->HSEState != RCC_HSE_OFF)
  363. {
  364. /* Get Start Tick*/
  365. tickstart = HAL_GetTick();
  366. /* Wait till HSE is ready */
  367. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == 0U)
  368. {
  369. if((uint32_t) (HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
  370. {
  371. return HAL_TIMEOUT;
  372. }
  373. }
  374. }
  375. else
  376. {
  377. /* Get Start Tick*/
  378. tickstart = HAL_GetTick();
  379. /* Wait till HSE is disabled */
  380. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != 0U)
  381. {
  382. if((uint32_t) (HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
  383. {
  384. return HAL_TIMEOUT;
  385. }
  386. }
  387. }
  388. }
  389. }
  390. /*----------------------------- HSI Configuration --------------------------*/
  391. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI)
  392. {
  393. /* Check the parameters */
  394. assert_param(IS_RCC_HSI(RCC_OscInitStruct->HSIState));
  395. assert_param(IS_RCC_HSICALIBRATION_VALUE(RCC_OscInitStruct->HSICalibrationValue));
  396. /* When the HSI is used as system clock it will not be disabled */
  397. const uint32_t temp_sysclksrc = __HAL_RCC_GET_SYSCLK_SOURCE();
  398. const uint32_t temp_pllckselr = RCC->PLLCKSELR;
  399. if((temp_sysclksrc == RCC_CFGR_SWS_HSI) || ((temp_sysclksrc == RCC_CFGR_SWS_PLL1) && ((temp_pllckselr & RCC_PLLCKSELR_PLLSRC) == RCC_PLLCKSELR_PLLSRC_HSI)))
  400. {
  401. /* When HSI is used as system clock it will not be disabled */
  402. if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != 0U) && (RCC_OscInitStruct->HSIState == RCC_HSI_OFF))
  403. {
  404. return HAL_ERROR;
  405. }
  406. /* Otherwise, only HSI division and calibration are allowed */
  407. else
  408. {
  409. /* Enable the Internal High Speed oscillator (HSI, HSIDIV2, HSIDIV4, or HSIDIV8) */
  410. __HAL_RCC_HSI_CONFIG(RCC_OscInitStruct->HSIState);
  411. /* Get Start Tick*/
  412. tickstart = HAL_GetTick();
  413. /* Wait till HSI is ready */
  414. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == 0U)
  415. {
  416. if((uint32_t) (HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
  417. {
  418. return HAL_TIMEOUT;
  419. }
  420. }
  421. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  422. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  423. }
  424. }
  425. else
  426. {
  427. /* Check the HSI State */
  428. if((RCC_OscInitStruct->HSIState)!= RCC_HSI_OFF)
  429. {
  430. /* Enable the Internal High Speed oscillator (HSI, HSIDIV2,HSIDIV4, or HSIDIV8) */
  431. __HAL_RCC_HSI_CONFIG(RCC_OscInitStruct->HSIState);
  432. /* Get Start Tick*/
  433. tickstart = HAL_GetTick();
  434. /* Wait till HSI is ready */
  435. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == 0U)
  436. {
  437. if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
  438. {
  439. return HAL_TIMEOUT;
  440. }
  441. }
  442. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  443. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  444. }
  445. else
  446. {
  447. /* Disable the Internal High Speed oscillator (HSI). */
  448. __HAL_RCC_HSI_DISABLE();
  449. /* Get Start Tick*/
  450. tickstart = HAL_GetTick();
  451. /* Wait till HSI is disabled */
  452. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != 0U)
  453. {
  454. if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
  455. {
  456. return HAL_TIMEOUT;
  457. }
  458. }
  459. }
  460. }
  461. }
  462. /*----------------------------- CSI Configuration --------------------------*/
  463. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_CSI) == RCC_OSCILLATORTYPE_CSI)
  464. {
  465. /* Check the parameters */
  466. assert_param(IS_RCC_CSI(RCC_OscInitStruct->CSIState));
  467. assert_param(IS_RCC_CSICALIBRATION_VALUE(RCC_OscInitStruct->CSICalibrationValue));
  468. /* When the CSI is used as system clock it will not disabled */
  469. const uint32_t temp_sysclksrc = __HAL_RCC_GET_SYSCLK_SOURCE();
  470. const uint32_t temp_pllckselr = RCC->PLLCKSELR;
  471. if((temp_sysclksrc == RCC_CFGR_SWS_CSI) || ((temp_sysclksrc == RCC_CFGR_SWS_PLL1) && ((temp_pllckselr & RCC_PLLCKSELR_PLLSRC) == RCC_PLLCKSELR_PLLSRC_CSI)))
  472. {
  473. /* When CSI is used as system clock it will not disabled */
  474. if((__HAL_RCC_GET_FLAG(RCC_FLAG_CSIRDY) != 0U) && (RCC_OscInitStruct->CSIState != RCC_CSI_ON))
  475. {
  476. return HAL_ERROR;
  477. }
  478. /* Otherwise, just the calibration is allowed */
  479. else
  480. {
  481. /* Adjusts the Internal High Speed oscillator (CSI) calibration value.*/
  482. __HAL_RCC_CSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->CSICalibrationValue);
  483. }
  484. }
  485. else
  486. {
  487. /* Check the CSI State */
  488. if((RCC_OscInitStruct->CSIState)!= RCC_CSI_OFF)
  489. {
  490. /* Enable the Internal High Speed oscillator (CSI). */
  491. __HAL_RCC_CSI_ENABLE();
  492. /* Get Start Tick*/
  493. tickstart = HAL_GetTick();
  494. /* Wait till CSI is ready */
  495. while(__HAL_RCC_GET_FLAG(RCC_FLAG_CSIRDY) == 0U)
  496. {
  497. if((HAL_GetTick() - tickstart ) > CSI_TIMEOUT_VALUE)
  498. {
  499. return HAL_TIMEOUT;
  500. }
  501. }
  502. /* Adjusts the Internal High Speed oscillator (CSI) calibration value.*/
  503. __HAL_RCC_CSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->CSICalibrationValue);
  504. }
  505. else
  506. {
  507. /* Disable the Internal High Speed oscillator (CSI). */
  508. __HAL_RCC_CSI_DISABLE();
  509. /* Get Start Tick*/
  510. tickstart = HAL_GetTick();
  511. /* Wait till CSI is disabled */
  512. while(__HAL_RCC_GET_FLAG(RCC_FLAG_CSIRDY) != 0U)
  513. {
  514. if((HAL_GetTick() - tickstart ) > CSI_TIMEOUT_VALUE)
  515. {
  516. return HAL_TIMEOUT;
  517. }
  518. }
  519. }
  520. }
  521. }
  522. /*------------------------------ LSI Configuration -------------------------*/
  523. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI)
  524. {
  525. /* Check the parameters */
  526. assert_param(IS_RCC_LSI(RCC_OscInitStruct->LSIState));
  527. /* Check the LSI State */
  528. if((RCC_OscInitStruct->LSIState)!= RCC_LSI_OFF)
  529. {
  530. /* Enable the Internal Low Speed oscillator (LSI). */
  531. __HAL_RCC_LSI_ENABLE();
  532. /* Get Start Tick*/
  533. tickstart = HAL_GetTick();
  534. /* Wait till LSI is ready */
  535. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) == 0U)
  536. {
  537. if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
  538. {
  539. return HAL_TIMEOUT;
  540. }
  541. }
  542. }
  543. else
  544. {
  545. /* Disable the Internal Low Speed oscillator (LSI). */
  546. __HAL_RCC_LSI_DISABLE();
  547. /* Get Start Tick*/
  548. tickstart = HAL_GetTick();
  549. /* Wait till LSI is ready */
  550. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) != 0U)
  551. {
  552. if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
  553. {
  554. return HAL_TIMEOUT;
  555. }
  556. }
  557. }
  558. }
  559. /*------------------------------ HSI48 Configuration -------------------------*/
  560. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI48) == RCC_OSCILLATORTYPE_HSI48)
  561. {
  562. /* Check the parameters */
  563. assert_param(IS_RCC_HSI48(RCC_OscInitStruct->HSI48State));
  564. /* Check the HSI48 State */
  565. if((RCC_OscInitStruct->HSI48State)!= RCC_HSI48_OFF)
  566. {
  567. /* Enable the Internal Low Speed oscillator (HSI48). */
  568. __HAL_RCC_HSI48_ENABLE();
  569. /* Get time-out */
  570. tickstart = HAL_GetTick();
  571. /* Wait till HSI48 is ready */
  572. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSI48RDY) == 0U)
  573. {
  574. if((HAL_GetTick() - tickstart ) > HSI48_TIMEOUT_VALUE)
  575. {
  576. return HAL_TIMEOUT;
  577. }
  578. }
  579. }
  580. else
  581. {
  582. /* Disable the Internal Low Speed oscillator (HSI48). */
  583. __HAL_RCC_HSI48_DISABLE();
  584. /* Get time-out */
  585. tickstart = HAL_GetTick();
  586. /* Wait till HSI48 is ready */
  587. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSI48RDY) != 0U)
  588. {
  589. if((HAL_GetTick() - tickstart ) > HSI48_TIMEOUT_VALUE)
  590. {
  591. return HAL_TIMEOUT;
  592. }
  593. }
  594. }
  595. }
  596. /*------------------------------ LSE Configuration -------------------------*/
  597. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE)
  598. {
  599. /* Check the parameters */
  600. assert_param(IS_RCC_LSE(RCC_OscInitStruct->LSEState));
  601. /* Enable write access to Backup domain */
  602. PWR->CR1 |= PWR_CR1_DBP;
  603. /* Wait for Backup domain Write protection disable */
  604. tickstart = HAL_GetTick();
  605. while((PWR->CR1 & PWR_CR1_DBP) == 0U)
  606. {
  607. if((HAL_GetTick() - tickstart ) > RCC_DBP_TIMEOUT_VALUE)
  608. {
  609. return HAL_TIMEOUT;
  610. }
  611. }
  612. /* Set the new LSE configuration -----------------------------------------*/
  613. __HAL_RCC_LSE_CONFIG(RCC_OscInitStruct->LSEState);
  614. /* Check the LSE State */
  615. if((RCC_OscInitStruct->LSEState) != RCC_LSE_OFF)
  616. {
  617. /* Get Start Tick*/
  618. tickstart = HAL_GetTick();
  619. /* Wait till LSE is ready */
  620. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == 0U)
  621. {
  622. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  623. {
  624. return HAL_TIMEOUT;
  625. }
  626. }
  627. }
  628. else
  629. {
  630. /* Get Start Tick*/
  631. tickstart = HAL_GetTick();
  632. /* Wait till LSE is disabled */
  633. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) != 0U)
  634. {
  635. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  636. {
  637. return HAL_TIMEOUT;
  638. }
  639. }
  640. }
  641. }
  642. /*-------------------------------- PLL Configuration -----------------------*/
  643. /* Check the parameters */
  644. assert_param(IS_RCC_PLL(RCC_OscInitStruct->PLL.PLLState));
  645. if ((RCC_OscInitStruct->PLL.PLLState) != RCC_PLL_NONE)
  646. {
  647. /* Check if the PLL is used as system clock or not */
  648. if(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_PLL1)
  649. {
  650. if((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_ON)
  651. {
  652. /* Check the parameters */
  653. assert_param(IS_RCC_PLLSOURCE(RCC_OscInitStruct->PLL.PLLSource));
  654. assert_param(IS_RCC_PLLRGE_VALUE(RCC_OscInitStruct->PLL.PLLRGE));
  655. assert_param(IS_RCC_PLLVCO_VALUE(RCC_OscInitStruct->PLL.PLLVCOSEL));
  656. assert_param(IS_RCC_PLLM_VALUE(RCC_OscInitStruct->PLL.PLLM));
  657. assert_param(IS_RCC_PLLN_VALUE(RCC_OscInitStruct->PLL.PLLN));
  658. assert_param(IS_RCC_PLLP_VALUE(RCC_OscInitStruct->PLL.PLLP));
  659. assert_param(IS_RCC_PLLQ_VALUE(RCC_OscInitStruct->PLL.PLLQ));
  660. assert_param(IS_RCC_PLLR_VALUE(RCC_OscInitStruct->PLL.PLLR));
  661. assert_param(IS_RCC_PLLFRACN_VALUE(RCC_OscInitStruct->PLL.PLLFRACN));
  662. /* Disable the main PLL. */
  663. __HAL_RCC_PLL_DISABLE();
  664. /* Get Start Tick*/
  665. tickstart = HAL_GetTick();
  666. /* Wait till PLL is disabled */
  667. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != 0U)
  668. {
  669. if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  670. {
  671. return HAL_TIMEOUT;
  672. }
  673. }
  674. /* Configure the main PLL clock source, multiplication and division factors. */
  675. __HAL_RCC_PLL_CONFIG(RCC_OscInitStruct->PLL.PLLSource,
  676. RCC_OscInitStruct->PLL.PLLM,
  677. RCC_OscInitStruct->PLL.PLLN,
  678. RCC_OscInitStruct->PLL.PLLP,
  679. RCC_OscInitStruct->PLL.PLLQ,
  680. RCC_OscInitStruct->PLL.PLLR);
  681. /* Disable PLLFRACN . */
  682. __HAL_RCC_PLLFRACN_DISABLE();
  683. /* Configure PLL PLL1FRACN */
  684. __HAL_RCC_PLLFRACN_CONFIG(RCC_OscInitStruct->PLL.PLLFRACN);
  685. /* Select PLL1 input reference frequency range: VCI */
  686. __HAL_RCC_PLL_VCIRANGE(RCC_OscInitStruct->PLL.PLLRGE) ;
  687. /* Select PLL1 output frequency range : VCO */
  688. __HAL_RCC_PLL_VCORANGE(RCC_OscInitStruct->PLL.PLLVCOSEL) ;
  689. /* Enable PLL System Clock output. */
  690. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL1_DIVP);
  691. /* Enable PLL1Q Clock output. */
  692. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL1_DIVQ);
  693. /* Enable PLL1R Clock output. */
  694. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL1_DIVR);
  695. /* Enable PLL1FRACN . */
  696. __HAL_RCC_PLLFRACN_ENABLE();
  697. /* Enable the main PLL. */
  698. __HAL_RCC_PLL_ENABLE();
  699. /* Get Start Tick*/
  700. tickstart = HAL_GetTick();
  701. /* Wait till PLL is ready */
  702. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == 0U)
  703. {
  704. if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  705. {
  706. return HAL_TIMEOUT;
  707. }
  708. }
  709. }
  710. else
  711. {
  712. /* Disable the main PLL. */
  713. __HAL_RCC_PLL_DISABLE();
  714. /* Get Start Tick*/
  715. tickstart = HAL_GetTick();
  716. /* Wait till PLL is disabled */
  717. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != 0U)
  718. {
  719. if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  720. {
  721. return HAL_TIMEOUT;
  722. }
  723. }
  724. }
  725. }
  726. else
  727. {
  728. /* Do not return HAL_ERROR if request repeats the current configuration */
  729. temp1_pllckcfg = RCC->PLLCKSELR;
  730. temp2_pllckcfg = RCC->PLL1DIVR;
  731. if(((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_OFF) ||
  732. (READ_BIT(temp1_pllckcfg, RCC_PLLCKSELR_PLLSRC) != RCC_OscInitStruct->PLL.PLLSource) ||
  733. ((READ_BIT(temp1_pllckcfg, RCC_PLLCKSELR_DIVM1) >> RCC_PLLCKSELR_DIVM1_Pos) != RCC_OscInitStruct->PLL.PLLM) ||
  734. (READ_BIT(temp2_pllckcfg, RCC_PLL1DIVR_N1) != (RCC_OscInitStruct->PLL.PLLN - 1U)) ||
  735. ((READ_BIT(temp2_pllckcfg, RCC_PLL1DIVR_P1) >> RCC_PLL1DIVR_P1_Pos) != (RCC_OscInitStruct->PLL.PLLP - 1U)) ||
  736. ((READ_BIT(temp2_pllckcfg, RCC_PLL1DIVR_Q1) >> RCC_PLL1DIVR_Q1_Pos) != (RCC_OscInitStruct->PLL.PLLQ - 1U)) ||
  737. ((READ_BIT(temp2_pllckcfg, RCC_PLL1DIVR_R1) >> RCC_PLL1DIVR_R1_Pos) != (RCC_OscInitStruct->PLL.PLLR - 1U)))
  738. {
  739. return HAL_ERROR;
  740. }
  741. }
  742. }
  743. return HAL_OK;
  744. }
  745. /**
  746. * @brief Initializes the CPU, AHB and APB buses clocks according to the specified
  747. * parameters in the RCC_ClkInitStruct.
  748. * @param RCC_ClkInitStruct: pointer to an RCC_OscInitTypeDef structure that
  749. * contains the configuration information for the RCC peripheral.
  750. * @param FLatency: FLASH Latency, this parameter depend on device selected
  751. *
  752. * @note The SystemCoreClock CMSIS variable is used to store System Core Clock Frequency
  753. * and updated by HAL_InitTick() function called within this function
  754. *
  755. * @note The HSI is used (enabled by hardware) as system clock source after
  756. * start-up from Reset, wake-up from STOP and STANDBY mode, or in case
  757. * of failure of the HSE used directly or indirectly as system clock
  758. * (if the Clock Security System CSS is enabled).
  759. *
  760. * @note A switch from one clock source to another occurs only if the target
  761. * clock source is ready (clock stable after start-up delay or PLL locked).
  762. * If a clock source which is not yet ready is selected, the switch will
  763. * occur when the clock source will be ready.
  764. * You can use HAL_RCC_GetClockConfig() function to know which clock is
  765. * currently used as system clock source.
  766. * @note Depending on the device voltage range, the software has to set correctly
  767. * D1CPRE[3:0] bits to ensure that Domain1 core clock not exceed the maximum allowed frequency
  768. * (for more details refer to section above "Initialization/de-initialization functions")
  769. * @retval None
  770. */
  771. HAL_StatusTypeDef HAL_RCC_ClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t FLatency)
  772. {
  773. HAL_StatusTypeDef halstatus;
  774. uint32_t tickstart;
  775. uint32_t common_system_clock;
  776. /* Check Null pointer */
  777. if(RCC_ClkInitStruct == NULL)
  778. {
  779. return HAL_ERROR;
  780. }
  781. /* Check the parameters */
  782. assert_param(IS_RCC_CLOCKTYPE(RCC_ClkInitStruct->ClockType));
  783. assert_param(IS_FLASH_LATENCY(FLatency));
  784. /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
  785. must be correctly programmed according to the frequency of the CPU clock
  786. (HCLK) and the supply voltage of the device. */
  787. /* Increasing the CPU frequency */
  788. if(FLatency > __HAL_FLASH_GET_LATENCY())
  789. {
  790. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  791. __HAL_FLASH_SET_LATENCY(FLatency);
  792. /* Check that the new number of wait states is taken into account to access the Flash
  793. memory by reading the FLASH_ACR register */
  794. if(__HAL_FLASH_GET_LATENCY() != FLatency)
  795. {
  796. return HAL_ERROR;
  797. }
  798. }
  799. /* Increasing the BUS frequency divider */
  800. /*-------------------------- D1PCLK1/CDPCLK1 Configuration ---------------------------*/
  801. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_D1PCLK1) == RCC_CLOCKTYPE_D1PCLK1)
  802. {
  803. #if defined (RCC_D1CFGR_D1PPRE)
  804. if((RCC_ClkInitStruct->APB3CLKDivider) > (RCC->D1CFGR & RCC_D1CFGR_D1PPRE))
  805. {
  806. assert_param(IS_RCC_D1PCLK1(RCC_ClkInitStruct->APB3CLKDivider));
  807. MODIFY_REG(RCC->D1CFGR, RCC_D1CFGR_D1PPRE, RCC_ClkInitStruct->APB3CLKDivider);
  808. }
  809. #else
  810. if((RCC_ClkInitStruct->APB3CLKDivider) > (RCC->CDCFGR1 & RCC_CDCFGR1_CDPPRE))
  811. {
  812. assert_param(IS_RCC_CDPCLK1(RCC_ClkInitStruct->APB3CLKDivider));
  813. MODIFY_REG(RCC->CDCFGR1, RCC_CDCFGR1_CDPPRE, RCC_ClkInitStruct->APB3CLKDivider);
  814. }
  815. #endif
  816. }
  817. /*-------------------------- PCLK1 Configuration ---------------------------*/
  818. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
  819. {
  820. #if defined (RCC_D2CFGR_D2PPRE1)
  821. if((RCC_ClkInitStruct->APB1CLKDivider) > (RCC->D2CFGR & RCC_D2CFGR_D2PPRE1))
  822. {
  823. assert_param(IS_RCC_PCLK1(RCC_ClkInitStruct->APB1CLKDivider));
  824. MODIFY_REG(RCC->D2CFGR, RCC_D2CFGR_D2PPRE1, (RCC_ClkInitStruct->APB1CLKDivider));
  825. }
  826. #else
  827. if((RCC_ClkInitStruct->APB1CLKDivider) > (RCC->CDCFGR2 & RCC_CDCFGR2_CDPPRE1))
  828. {
  829. assert_param(IS_RCC_PCLK1(RCC_ClkInitStruct->APB1CLKDivider));
  830. MODIFY_REG(RCC->CDCFGR2, RCC_CDCFGR2_CDPPRE1, (RCC_ClkInitStruct->APB1CLKDivider));
  831. }
  832. #endif
  833. }
  834. /*-------------------------- PCLK2 Configuration ---------------------------*/
  835. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK2) == RCC_CLOCKTYPE_PCLK2)
  836. {
  837. #if defined(RCC_D2CFGR_D2PPRE2)
  838. if((RCC_ClkInitStruct->APB2CLKDivider) > (RCC->D2CFGR & RCC_D2CFGR_D2PPRE2))
  839. {
  840. assert_param(IS_RCC_PCLK2(RCC_ClkInitStruct->APB2CLKDivider));
  841. MODIFY_REG(RCC->D2CFGR, RCC_D2CFGR_D2PPRE2, (RCC_ClkInitStruct->APB2CLKDivider));
  842. }
  843. #else
  844. if((RCC_ClkInitStruct->APB2CLKDivider) > (RCC->CDCFGR2 & RCC_CDCFGR2_CDPPRE2))
  845. {
  846. assert_param(IS_RCC_PCLK2(RCC_ClkInitStruct->APB2CLKDivider));
  847. MODIFY_REG(RCC->CDCFGR2, RCC_CDCFGR2_CDPPRE2, (RCC_ClkInitStruct->APB2CLKDivider));
  848. }
  849. #endif
  850. }
  851. /*-------------------------- D3PCLK1 Configuration ---------------------------*/
  852. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_D3PCLK1) == RCC_CLOCKTYPE_D3PCLK1)
  853. {
  854. #if defined(RCC_D3CFGR_D3PPRE)
  855. if((RCC_ClkInitStruct->APB4CLKDivider) > (RCC->D3CFGR & RCC_D3CFGR_D3PPRE))
  856. {
  857. assert_param(IS_RCC_D3PCLK1(RCC_ClkInitStruct->APB4CLKDivider));
  858. MODIFY_REG(RCC->D3CFGR, RCC_D3CFGR_D3PPRE, (RCC_ClkInitStruct->APB4CLKDivider) );
  859. }
  860. #else
  861. if((RCC_ClkInitStruct->APB4CLKDivider) > (RCC->SRDCFGR & RCC_SRDCFGR_SRDPPRE))
  862. {
  863. assert_param(IS_RCC_D3PCLK1(RCC_ClkInitStruct->APB4CLKDivider));
  864. MODIFY_REG(RCC->SRDCFGR, RCC_SRDCFGR_SRDPPRE, (RCC_ClkInitStruct->APB4CLKDivider) );
  865. }
  866. #endif
  867. }
  868. /*-------------------------- HCLK Configuration --------------------------*/
  869. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)
  870. {
  871. #if defined (RCC_D1CFGR_HPRE)
  872. if((RCC_ClkInitStruct->AHBCLKDivider) > (RCC->D1CFGR & RCC_D1CFGR_HPRE))
  873. {
  874. /* Set the new HCLK clock divider */
  875. assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
  876. MODIFY_REG(RCC->D1CFGR, RCC_D1CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
  877. }
  878. #else
  879. if((RCC_ClkInitStruct->AHBCLKDivider) > (RCC->CDCFGR1 & RCC_CDCFGR1_HPRE))
  880. {
  881. /* Set the new HCLK clock divider */
  882. assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
  883. MODIFY_REG(RCC->CDCFGR1, RCC_CDCFGR1_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
  884. }
  885. #endif
  886. }
  887. /*------------------------- SYSCLK Configuration -------------------------*/
  888. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_SYSCLK) == RCC_CLOCKTYPE_SYSCLK)
  889. {
  890. assert_param(IS_RCC_SYSCLK(RCC_ClkInitStruct->SYSCLKDivider));
  891. assert_param(IS_RCC_SYSCLKSOURCE(RCC_ClkInitStruct->SYSCLKSource));
  892. #if defined(RCC_D1CFGR_D1CPRE)
  893. MODIFY_REG(RCC->D1CFGR, RCC_D1CFGR_D1CPRE, RCC_ClkInitStruct->SYSCLKDivider);
  894. #else
  895. MODIFY_REG(RCC->CDCFGR1, RCC_CDCFGR1_CDCPRE, RCC_ClkInitStruct->SYSCLKDivider);
  896. #endif
  897. /* HSE is selected as System Clock Source */
  898. if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
  899. {
  900. /* Check the HSE ready flag */
  901. if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == 0U)
  902. {
  903. return HAL_ERROR;
  904. }
  905. }
  906. /* PLL is selected as System Clock Source */
  907. else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
  908. {
  909. /* Check the PLL ready flag */
  910. if(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == 0U)
  911. {
  912. return HAL_ERROR;
  913. }
  914. }
  915. /* CSI is selected as System Clock Source */
  916. else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_CSI)
  917. {
  918. /* Check the PLL ready flag */
  919. if(__HAL_RCC_GET_FLAG(RCC_FLAG_CSIRDY) == 0U)
  920. {
  921. return HAL_ERROR;
  922. }
  923. }
  924. /* HSI is selected as System Clock Source */
  925. else
  926. {
  927. /* Check the HSI ready flag */
  928. if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == 0U)
  929. {
  930. return HAL_ERROR;
  931. }
  932. }
  933. MODIFY_REG(RCC->CFGR, RCC_CFGR_SW, RCC_ClkInitStruct->SYSCLKSource);
  934. /* Get Start Tick*/
  935. tickstart = HAL_GetTick();
  936. while (__HAL_RCC_GET_SYSCLK_SOURCE() != (RCC_ClkInitStruct->SYSCLKSource << RCC_CFGR_SWS_Pos))
  937. {
  938. if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
  939. {
  940. return HAL_TIMEOUT;
  941. }
  942. }
  943. }
  944. /* Decreasing the BUS frequency divider */
  945. /*-------------------------- HCLK Configuration --------------------------*/
  946. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)
  947. {
  948. #if defined(RCC_D1CFGR_HPRE)
  949. if((RCC_ClkInitStruct->AHBCLKDivider) < (RCC->D1CFGR & RCC_D1CFGR_HPRE))
  950. {
  951. /* Set the new HCLK clock divider */
  952. assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
  953. MODIFY_REG(RCC->D1CFGR, RCC_D1CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
  954. }
  955. #else
  956. if((RCC_ClkInitStruct->AHBCLKDivider) < (RCC->CDCFGR1 & RCC_CDCFGR1_HPRE))
  957. {
  958. /* Set the new HCLK clock divider */
  959. assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
  960. MODIFY_REG(RCC->CDCFGR1, RCC_CDCFGR1_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
  961. }
  962. #endif
  963. }
  964. /* Decreasing the number of wait states because of lower CPU frequency */
  965. if(FLatency < __HAL_FLASH_GET_LATENCY())
  966. {
  967. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  968. __HAL_FLASH_SET_LATENCY(FLatency);
  969. /* Check that the new number of wait states is taken into account to access the Flash
  970. memory by reading the FLASH_ACR register */
  971. if(__HAL_FLASH_GET_LATENCY() != FLatency)
  972. {
  973. return HAL_ERROR;
  974. }
  975. }
  976. /*-------------------------- D1PCLK1/CDPCLK Configuration ---------------------------*/
  977. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_D1PCLK1) == RCC_CLOCKTYPE_D1PCLK1)
  978. {
  979. #if defined(RCC_D1CFGR_D1PPRE)
  980. if((RCC_ClkInitStruct->APB3CLKDivider) < (RCC->D1CFGR & RCC_D1CFGR_D1PPRE))
  981. {
  982. assert_param(IS_RCC_D1PCLK1(RCC_ClkInitStruct->APB3CLKDivider));
  983. MODIFY_REG(RCC->D1CFGR, RCC_D1CFGR_D1PPRE, RCC_ClkInitStruct->APB3CLKDivider);
  984. }
  985. #else
  986. if((RCC_ClkInitStruct->APB3CLKDivider) < (RCC->CDCFGR1 & RCC_CDCFGR1_CDPPRE))
  987. {
  988. assert_param(IS_RCC_CDPCLK1(RCC_ClkInitStruct->APB3CLKDivider));
  989. MODIFY_REG(RCC->CDCFGR1, RCC_CDCFGR1_CDPPRE, RCC_ClkInitStruct->APB3CLKDivider);
  990. }
  991. #endif
  992. }
  993. /*-------------------------- PCLK1 Configuration ---------------------------*/
  994. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
  995. {
  996. #if defined(RCC_D2CFGR_D2PPRE1)
  997. if((RCC_ClkInitStruct->APB1CLKDivider) < (RCC->D2CFGR & RCC_D2CFGR_D2PPRE1))
  998. {
  999. assert_param(IS_RCC_PCLK1(RCC_ClkInitStruct->APB1CLKDivider));
  1000. MODIFY_REG(RCC->D2CFGR, RCC_D2CFGR_D2PPRE1, (RCC_ClkInitStruct->APB1CLKDivider));
  1001. }
  1002. #else
  1003. if((RCC_ClkInitStruct->APB1CLKDivider) < (RCC->CDCFGR2 & RCC_CDCFGR2_CDPPRE1))
  1004. {
  1005. assert_param(IS_RCC_PCLK1(RCC_ClkInitStruct->APB1CLKDivider));
  1006. MODIFY_REG(RCC->CDCFGR2, RCC_CDCFGR2_CDPPRE1, (RCC_ClkInitStruct->APB1CLKDivider));
  1007. }
  1008. #endif
  1009. }
  1010. /*-------------------------- PCLK2 Configuration ---------------------------*/
  1011. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK2) == RCC_CLOCKTYPE_PCLK2)
  1012. {
  1013. #if defined (RCC_D2CFGR_D2PPRE2)
  1014. if((RCC_ClkInitStruct->APB2CLKDivider) < (RCC->D2CFGR & RCC_D2CFGR_D2PPRE2))
  1015. {
  1016. assert_param(IS_RCC_PCLK2(RCC_ClkInitStruct->APB2CLKDivider));
  1017. MODIFY_REG(RCC->D2CFGR, RCC_D2CFGR_D2PPRE2, (RCC_ClkInitStruct->APB2CLKDivider));
  1018. }
  1019. #else
  1020. if((RCC_ClkInitStruct->APB2CLKDivider) < (RCC->CDCFGR2 & RCC_CDCFGR2_CDPPRE2))
  1021. {
  1022. assert_param(IS_RCC_PCLK2(RCC_ClkInitStruct->APB2CLKDivider));
  1023. MODIFY_REG(RCC->CDCFGR2, RCC_CDCFGR2_CDPPRE2, (RCC_ClkInitStruct->APB2CLKDivider));
  1024. }
  1025. #endif
  1026. }
  1027. /*-------------------------- D3PCLK1/SRDPCLK1 Configuration ---------------------------*/
  1028. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_D3PCLK1) == RCC_CLOCKTYPE_D3PCLK1)
  1029. {
  1030. #if defined(RCC_D3CFGR_D3PPRE)
  1031. if((RCC_ClkInitStruct->APB4CLKDivider) < (RCC->D3CFGR & RCC_D3CFGR_D3PPRE))
  1032. {
  1033. assert_param(IS_RCC_D3PCLK1(RCC_ClkInitStruct->APB4CLKDivider));
  1034. MODIFY_REG(RCC->D3CFGR, RCC_D3CFGR_D3PPRE, (RCC_ClkInitStruct->APB4CLKDivider) );
  1035. }
  1036. #else
  1037. if((RCC_ClkInitStruct->APB4CLKDivider) < (RCC->SRDCFGR & RCC_SRDCFGR_SRDPPRE))
  1038. {
  1039. assert_param(IS_RCC_SRDPCLK1(RCC_ClkInitStruct->APB4CLKDivider));
  1040. MODIFY_REG(RCC->SRDCFGR, RCC_SRDCFGR_SRDPPRE, (RCC_ClkInitStruct->APB4CLKDivider) );
  1041. }
  1042. #endif
  1043. }
  1044. /* Update the SystemCoreClock global variable */
  1045. #if defined(RCC_D1CFGR_D1CPRE)
  1046. common_system_clock = HAL_RCC_GetSysClockFreq() >> ((D1CorePrescTable[(RCC->D1CFGR & RCC_D1CFGR_D1CPRE)>> RCC_D1CFGR_D1CPRE_Pos]) & 0x1FU);
  1047. #else
  1048. common_system_clock = HAL_RCC_GetSysClockFreq() >> ((D1CorePrescTable[(RCC->CDCFGR1 & RCC_CDCFGR1_CDCPRE)>> RCC_CDCFGR1_CDCPRE_Pos]) & 0x1FU);
  1049. #endif
  1050. #if defined(RCC_D1CFGR_HPRE)
  1051. SystemD2Clock = (common_system_clock >> ((D1CorePrescTable[(RCC->D1CFGR & RCC_D1CFGR_HPRE)>> RCC_D1CFGR_HPRE_Pos]) & 0x1FU));
  1052. #else
  1053. SystemD2Clock = (common_system_clock >> ((D1CorePrescTable[(RCC->CDCFGR1 & RCC_CDCFGR1_HPRE)>> RCC_CDCFGR1_HPRE_Pos]) & 0x1FU));
  1054. #endif
  1055. #if defined(DUAL_CORE) && defined(CORE_CM4)
  1056. SystemCoreClock = SystemD2Clock;
  1057. #else
  1058. SystemCoreClock = common_system_clock;
  1059. #endif /* DUAL_CORE && CORE_CM4 */
  1060. /* Configure the source of time base considering new system clocks settings*/
  1061. halstatus = HAL_InitTick (uwTickPrio);
  1062. return halstatus;
  1063. }
  1064. /**
  1065. * @}
  1066. */
  1067. /** @defgroup RCC_Group2 Peripheral Control functions
  1068. * @brief RCC clocks control functions
  1069. *
  1070. @verbatim
  1071. ===============================================================================
  1072. ##### Peripheral Control functions #####
  1073. ===============================================================================
  1074. [..]
  1075. This subsection provides a set of functions allowing to control the RCC Clocks
  1076. frequencies.
  1077. @endverbatim
  1078. * @{
  1079. */
  1080. /**
  1081. * @brief Selects the clock source to output on MCO1 pin(PA8) or on MCO2 pin(PC9).
  1082. * @note PA8/PC9 should be configured in alternate function mode.
  1083. * @param RCC_MCOx: specifies the output direction for the clock source.
  1084. * This parameter can be one of the following values:
  1085. * @arg RCC_MCO1: Clock source to output on MCO1 pin(PA8).
  1086. * @arg RCC_MCO2: Clock source to output on MCO2 pin(PC9).
  1087. * @param RCC_MCOSource: specifies the clock source to output.
  1088. * This parameter can be one of the following values:
  1089. * @arg RCC_MCO1SOURCE_HSI: HSI clock selected as MCO1 source
  1090. * @arg RCC_MCO1SOURCE_LSE: LSE clock selected as MCO1 source
  1091. * @arg RCC_MCO1SOURCE_HSE: HSE clock selected as MCO1 source
  1092. * @arg RCC_MCO1SOURCE_PLL1QCLK: PLL1Q clock selected as MCO1 source
  1093. * @arg RCC_MCO1SOURCE_HSI48: HSI48 (48MHZ) selected as MCO1 source
  1094. * @arg RCC_MCO2SOURCE_SYSCLK: System clock (SYSCLK) selected as MCO2 source
  1095. * @arg RCC_MCO2SOURCE_PLL2PCLK: PLL2P clock selected as MCO2 source
  1096. * @arg RCC_MCO2SOURCE_HSE: HSE clock selected as MCO2 source
  1097. * @arg RCC_MCO2SOURCE_PLLCLK: PLL1P clock selected as MCO2 source
  1098. * @arg RCC_MCO2SOURCE_CSICLK: CSI clock selected as MCO2 source
  1099. * @arg RCC_MCO2SOURCE_LSICLK: LSI clock selected as MCO2 source
  1100. * @param RCC_MCODiv: specifies the MCOx pre-scaler.
  1101. * This parameter can be one of the following values:
  1102. * @arg RCC_MCODIV_1 up to RCC_MCODIV_15 : divider applied to MCOx clock
  1103. * @retval None
  1104. */
  1105. void HAL_RCC_MCOConfig(uint32_t RCC_MCOx, uint32_t RCC_MCOSource, uint32_t RCC_MCODiv)
  1106. {
  1107. GPIO_InitTypeDef GPIO_InitStruct;
  1108. /* Check the parameters */
  1109. assert_param(IS_RCC_MCO(RCC_MCOx));
  1110. assert_param(IS_RCC_MCODIV(RCC_MCODiv));
  1111. /* RCC_MCO1 */
  1112. if(RCC_MCOx == RCC_MCO1)
  1113. {
  1114. assert_param(IS_RCC_MCO1SOURCE(RCC_MCOSource));
  1115. /* MCO1 Clock Enable */
  1116. MCO1_CLK_ENABLE();
  1117. /* Configure the MCO1 pin in alternate function mode */
  1118. GPIO_InitStruct.Pin = MCO1_PIN;
  1119. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  1120. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  1121. GPIO_InitStruct.Pull = GPIO_NOPULL;
  1122. GPIO_InitStruct.Alternate = GPIO_AF0_MCO;
  1123. HAL_GPIO_Init(MCO1_GPIO_PORT, &GPIO_InitStruct);
  1124. /* Mask MCO1 and MCO1PRE[3:0] bits then Select MCO1 clock source and pre-scaler */
  1125. MODIFY_REG(RCC->CFGR, (RCC_CFGR_MCO1 | RCC_CFGR_MCO1PRE), (RCC_MCOSource | RCC_MCODiv));
  1126. }
  1127. else
  1128. {
  1129. assert_param(IS_RCC_MCO2SOURCE(RCC_MCOSource));
  1130. /* MCO2 Clock Enable */
  1131. MCO2_CLK_ENABLE();
  1132. /* Configure the MCO2 pin in alternate function mode */
  1133. GPIO_InitStruct.Pin = MCO2_PIN;
  1134. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  1135. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  1136. GPIO_InitStruct.Pull = GPIO_NOPULL;
  1137. GPIO_InitStruct.Alternate = GPIO_AF0_MCO;
  1138. HAL_GPIO_Init(MCO2_GPIO_PORT, &GPIO_InitStruct);
  1139. /* Mask MCO2 and MCO2PRE[3:0] bits then Select MCO2 clock source and pre-scaler */
  1140. MODIFY_REG(RCC->CFGR, (RCC_CFGR_MCO2 | RCC_CFGR_MCO2PRE), (RCC_MCOSource | (RCC_MCODiv << 7U)));
  1141. }
  1142. }
  1143. /**
  1144. * @brief Enables the Clock Security System.
  1145. * @note If a failure is detected on the HSE oscillator clock, this oscillator
  1146. * is automatically disabled and an interrupt is generated to inform the
  1147. * software about the failure (Clock Security System Interrupt, CSSI),
  1148. * allowing the MCU to perform rescue operations. The CSSI is linked to
  1149. * the Cortex-M NMI (Non-Mask-able Interrupt) exception vector.
  1150. * @retval None
  1151. */
  1152. void HAL_RCC_EnableCSS(void)
  1153. {
  1154. SET_BIT(RCC->CR, RCC_CR_CSSHSEON) ;
  1155. }
  1156. /**
  1157. * @brief Disables the Clock Security System.
  1158. * @retval None
  1159. */
  1160. void HAL_RCC_DisableCSS(void)
  1161. {
  1162. CLEAR_BIT(RCC->CR, RCC_CR_CSSHSEON);
  1163. }
  1164. /**
  1165. * @brief Returns the SYSCLK frequency
  1166. *
  1167. * @note The system frequency computed by this function is not the real
  1168. * frequency in the chip. It is calculated based on the predefined
  1169. * constant and the selected clock source:
  1170. * @note If SYSCLK source is CSI, function returns values based on CSI_VALUE(*)
  1171. * @note If SYSCLK source is HSI, function returns values based on HSI_VALUE(**)
  1172. * @note If SYSCLK source is HSE, function returns values based on HSE_VALUE(***)
  1173. * @note If SYSCLK source is PLL, function returns values based on CSI_VALUE(*),
  1174. * HSI_VALUE(**) or HSE_VALUE(***) multiplied/divided by the PLL factors.
  1175. * @note (*) CSI_VALUE is a constant defined in stm32h7xx_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 (**) HSI_VALUE is a constant defined in stm32h7xx_hal_conf.h file (default value
  1179. * 64 MHz) but the real value may vary depending on the variations
  1180. * in voltage and temperature.
  1181. * @note (***) HSE_VALUE is a constant defined in stm32h7xx_hal_conf.h file (default value
  1182. * 25 MHz), user has to ensure that HSE_VALUE is same as the real
  1183. * frequency of the crystal used. Otherwise, this function may
  1184. * have wrong result.
  1185. *
  1186. * @note The result of this function could be not correct when using fractional
  1187. * value for HSE crystal.
  1188. *
  1189. * @note This function can be used by the user application to compute the
  1190. * baud rate for the communication peripherals or configure other parameters.
  1191. *
  1192. * @note Each time SYSCLK changes, this function must be called to update the
  1193. * right SYSCLK value. Otherwise, any configuration based on this function will be incorrect.
  1194. *
  1195. *
  1196. * @retval SYSCLK frequency
  1197. */
  1198. uint32_t HAL_RCC_GetSysClockFreq(void)
  1199. {
  1200. uint32_t pllp, pllsource, pllm, pllfracen, hsivalue;
  1201. float_t fracn1, pllvco;
  1202. uint32_t sysclockfreq;
  1203. /* Get SYSCLK source -------------------------------------------------------*/
  1204. switch (RCC->CFGR & RCC_CFGR_SWS)
  1205. {
  1206. case RCC_CFGR_SWS_HSI: /* HSI used as system clock source */
  1207. if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIDIV) != 0U)
  1208. {
  1209. sysclockfreq = (uint32_t) (HSI_VALUE >> (__HAL_RCC_GET_HSI_DIVIDER()>> 3));
  1210. }
  1211. else
  1212. {
  1213. sysclockfreq = (uint32_t) HSI_VALUE;
  1214. }
  1215. break;
  1216. case RCC_CFGR_SWS_CSI: /* CSI used as system clock source */
  1217. sysclockfreq = CSI_VALUE;
  1218. break;
  1219. case RCC_CFGR_SWS_HSE: /* HSE used as system clock source */
  1220. sysclockfreq = HSE_VALUE;
  1221. break;
  1222. case RCC_CFGR_SWS_PLL1: /* PLL1 used as system clock source */
  1223. /* PLL_VCO = (HSE_VALUE or HSI_VALUE or CSI_VALUE/ PLLM) * PLLN
  1224. SYSCLK = PLL_VCO / PLLR
  1225. */
  1226. pllsource = (RCC->PLLCKSELR & RCC_PLLCKSELR_PLLSRC);
  1227. pllm = ((RCC->PLLCKSELR & RCC_PLLCKSELR_DIVM1)>> 4) ;
  1228. pllfracen = ((RCC-> PLLCFGR & RCC_PLLCFGR_PLL1FRACEN)>>RCC_PLLCFGR_PLL1FRACEN_Pos);
  1229. fracn1 = (float_t)(uint32_t)(pllfracen* ((RCC->PLL1FRACR & RCC_PLL1FRACR_FRACN1)>> 3));
  1230. if (pllm != 0U)
  1231. {
  1232. switch (pllsource)
  1233. {
  1234. case RCC_PLLSOURCE_HSI: /* HSI used as PLL clock source */
  1235. if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIDIV) != 0U)
  1236. {
  1237. hsivalue= (HSI_VALUE >> (__HAL_RCC_GET_HSI_DIVIDER()>> 3));
  1238. pllvco = ( (float_t)hsivalue / (float_t)pllm) * ((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_N1) + (fracn1/(float_t)0x2000) +(float_t)1 );
  1239. }
  1240. else
  1241. {
  1242. pllvco = ((float_t)HSI_VALUE / (float_t)pllm) * ((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_N1) + (fracn1/(float_t)0x2000) +(float_t)1 );
  1243. }
  1244. break;
  1245. case RCC_PLLSOURCE_CSI: /* CSI used as PLL clock source */
  1246. pllvco = ((float_t)CSI_VALUE / (float_t)pllm) * ((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_N1) + (fracn1/(float_t)0x2000) +(float_t)1 );
  1247. break;
  1248. case RCC_PLLSOURCE_HSE: /* HSE used as PLL clock source */
  1249. pllvco = ((float_t)HSE_VALUE / (float_t)pllm) * ((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_N1) + (fracn1/(float_t)0x2000) +(float_t)1 );
  1250. break;
  1251. default:
  1252. pllvco = ((float_t)CSI_VALUE / (float_t)pllm) * ((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_N1) + (fracn1/(float_t)0x2000) +(float_t)1 );
  1253. break;
  1254. }
  1255. pllp = (((RCC->PLL1DIVR & RCC_PLL1DIVR_P1) >>9) + 1U ) ;
  1256. sysclockfreq = (uint32_t)(float_t)(pllvco/(float_t)pllp);
  1257. }
  1258. else
  1259. {
  1260. sysclockfreq = 0U;
  1261. }
  1262. break;
  1263. default:
  1264. sysclockfreq = CSI_VALUE;
  1265. break;
  1266. }
  1267. return sysclockfreq;
  1268. }
  1269. /**
  1270. * @brief Returns the HCLK frequency
  1271. * @note Each time HCLK changes, this function must be called to update the
  1272. * right HCLK value. Otherwise, any configuration based on this function will be incorrect.
  1273. *
  1274. * @note The SystemD2Clock CMSIS variable is used to store System domain2 Clock Frequency
  1275. * and updated within this function
  1276. * @retval HCLK frequency
  1277. */
  1278. uint32_t HAL_RCC_GetHCLKFreq(void)
  1279. {
  1280. uint32_t common_system_clock;
  1281. #if defined(RCC_D1CFGR_D1CPRE)
  1282. common_system_clock = HAL_RCC_GetSysClockFreq() >> (D1CorePrescTable[(RCC->D1CFGR & RCC_D1CFGR_D1CPRE)>> RCC_D1CFGR_D1CPRE_Pos] & 0x1FU);
  1283. #else
  1284. common_system_clock = HAL_RCC_GetSysClockFreq() >> (D1CorePrescTable[(RCC->CDCFGR1 & RCC_CDCFGR1_CDCPRE)>> RCC_CDCFGR1_CDCPRE_Pos] & 0x1FU);
  1285. #endif
  1286. #if defined(RCC_D1CFGR_HPRE)
  1287. SystemD2Clock = (common_system_clock >> ((D1CorePrescTable[(RCC->D1CFGR & RCC_D1CFGR_HPRE)>> RCC_D1CFGR_HPRE_Pos]) & 0x1FU));
  1288. #else
  1289. SystemD2Clock = (common_system_clock >> ((D1CorePrescTable[(RCC->CDCFGR1 & RCC_CDCFGR1_HPRE)>> RCC_CDCFGR1_HPRE_Pos]) & 0x1FU));
  1290. #endif
  1291. #if defined(DUAL_CORE) && defined(CORE_CM4)
  1292. SystemCoreClock = SystemD2Clock;
  1293. #else
  1294. SystemCoreClock = common_system_clock;
  1295. #endif /* DUAL_CORE && CORE_CM4 */
  1296. return SystemD2Clock;
  1297. }
  1298. /**
  1299. * @brief Returns the PCLK1 frequency
  1300. * @note Each time PCLK1 changes, this function must be called to update the
  1301. * right PCLK1 value. Otherwise, any configuration based on this function will be incorrect.
  1302. * @retval PCLK1 frequency
  1303. */
  1304. uint32_t HAL_RCC_GetPCLK1Freq(void)
  1305. {
  1306. #if defined (RCC_D2CFGR_D2PPRE1)
  1307. /* Get HCLK source and Compute PCLK1 frequency ---------------------------*/
  1308. return (HAL_RCC_GetHCLKFreq() >> ((D1CorePrescTable[(RCC->D2CFGR & RCC_D2CFGR_D2PPRE1)>> RCC_D2CFGR_D2PPRE1_Pos]) & 0x1FU));
  1309. #else
  1310. /* Get HCLK source and Compute PCLK1 frequency ---------------------------*/
  1311. return (HAL_RCC_GetHCLKFreq() >> ((D1CorePrescTable[(RCC->CDCFGR2 & RCC_CDCFGR2_CDPPRE1)>> RCC_CDCFGR2_CDPPRE1_Pos]) & 0x1FU));
  1312. #endif
  1313. }
  1314. /**
  1315. * @brief Returns the PCLK2 frequency
  1316. * @note Each time PCLK2 changes, this function must be called to update the
  1317. * right PCLK2 value. Otherwise, any configuration based on this function will be incorrect.
  1318. * @retval PCLK1 frequency
  1319. */
  1320. uint32_t HAL_RCC_GetPCLK2Freq(void)
  1321. {
  1322. /* Get HCLK source and Compute PCLK1 frequency ---------------------------*/
  1323. #if defined(RCC_D2CFGR_D2PPRE2)
  1324. return (HAL_RCC_GetHCLKFreq() >> ((D1CorePrescTable[(RCC->D2CFGR & RCC_D2CFGR_D2PPRE2)>> RCC_D2CFGR_D2PPRE2_Pos]) & 0x1FU));
  1325. #else
  1326. return (HAL_RCC_GetHCLKFreq() >> ((D1CorePrescTable[(RCC->CDCFGR2 & RCC_CDCFGR2_CDPPRE2)>> RCC_CDCFGR2_CDPPRE2_Pos]) & 0x1FU));
  1327. #endif
  1328. }
  1329. /**
  1330. * @brief Configures the RCC_OscInitStruct according to the internal
  1331. * RCC configuration registers.
  1332. * @param RCC_OscInitStruct: pointer to an RCC_OscInitTypeDef structure that
  1333. * will be configured.
  1334. * @retval None
  1335. */
  1336. void HAL_RCC_GetOscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  1337. {
  1338. /* Set all possible values for the Oscillator type parameter ---------------*/
  1339. RCC_OscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_CSI | \
  1340. RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_LSI| RCC_OSCILLATORTYPE_HSI48;
  1341. /* Get the HSE configuration -----------------------------------------------*/
  1342. #if defined(RCC_CR_HSEEXT)
  1343. if((RCC->CR &(RCC_CR_HSEBYP | RCC_CR_HSEEXT)) == RCC_CR_HSEBYP)
  1344. {
  1345. RCC_OscInitStruct->HSEState = RCC_HSE_BYPASS;
  1346. }
  1347. else if((RCC->CR &(RCC_CR_HSEBYP | RCC_CR_HSEEXT)) == (RCC_CR_HSEBYP | RCC_CR_HSEEXT))
  1348. {
  1349. RCC_OscInitStruct->HSEState = RCC_HSE_BYPASS_DIGITAL;
  1350. }
  1351. else if((RCC->CR &RCC_CR_HSEON) == RCC_CR_HSEON)
  1352. {
  1353. RCC_OscInitStruct->HSEState = RCC_HSE_ON;
  1354. }
  1355. else
  1356. {
  1357. RCC_OscInitStruct->HSEState = RCC_HSE_OFF;
  1358. }
  1359. #else
  1360. if((RCC->CR &RCC_CR_HSEBYP) == RCC_CR_HSEBYP)
  1361. {
  1362. RCC_OscInitStruct->HSEState = RCC_HSE_BYPASS;
  1363. }
  1364. else if((RCC->CR &RCC_CR_HSEON) == RCC_CR_HSEON)
  1365. {
  1366. RCC_OscInitStruct->HSEState = RCC_HSE_ON;
  1367. }
  1368. else
  1369. {
  1370. RCC_OscInitStruct->HSEState = RCC_HSE_OFF;
  1371. }
  1372. #endif /* RCC_CR_HSEEXT */
  1373. /* Get the CSI configuration -----------------------------------------------*/
  1374. if((RCC->CR &RCC_CR_CSION) == RCC_CR_CSION)
  1375. {
  1376. RCC_OscInitStruct->CSIState = RCC_CSI_ON;
  1377. }
  1378. else
  1379. {
  1380. RCC_OscInitStruct->CSIState = RCC_CSI_OFF;
  1381. }
  1382. #if defined(RCC_VER_X)
  1383. if(HAL_GetREVID() <= REV_ID_Y)
  1384. {
  1385. RCC_OscInitStruct->CSICalibrationValue = (uint32_t)(READ_BIT(RCC->HSICFGR, HAL_RCC_REV_Y_CSITRIM_Msk) >> HAL_RCC_REV_Y_CSITRIM_Pos);
  1386. }
  1387. else
  1388. {
  1389. RCC_OscInitStruct->CSICalibrationValue = (uint32_t)(READ_BIT(RCC->CSICFGR, RCC_CSICFGR_CSITRIM) >> RCC_CSICFGR_CSITRIM_Pos);
  1390. }
  1391. #else
  1392. RCC_OscInitStruct->CSICalibrationValue = (uint32_t)(READ_BIT(RCC->CSICFGR, RCC_CSICFGR_CSITRIM) >> RCC_CSICFGR_CSITRIM_Pos);
  1393. #endif /*RCC_VER_X*/
  1394. /* Get the HSI configuration -----------------------------------------------*/
  1395. if((RCC->CR &RCC_CR_HSION) == RCC_CR_HSION)
  1396. {
  1397. RCC_OscInitStruct->HSIState = RCC_HSI_ON;
  1398. }
  1399. else
  1400. {
  1401. RCC_OscInitStruct->HSIState = RCC_HSI_OFF;
  1402. }
  1403. #if defined(RCC_VER_X)
  1404. if(HAL_GetREVID() <= REV_ID_Y)
  1405. {
  1406. RCC_OscInitStruct->HSICalibrationValue = (uint32_t)(READ_BIT(RCC->HSICFGR, HAL_RCC_REV_Y_HSITRIM_Msk) >> HAL_RCC_REV_Y_HSITRIM_Pos);
  1407. }
  1408. else
  1409. {
  1410. RCC_OscInitStruct->HSICalibrationValue = (uint32_t)(READ_BIT(RCC->HSICFGR, RCC_HSICFGR_HSITRIM) >> RCC_HSICFGR_HSITRIM_Pos);
  1411. }
  1412. #else
  1413. RCC_OscInitStruct->HSICalibrationValue = (uint32_t)(READ_BIT(RCC->HSICFGR, RCC_HSICFGR_HSITRIM) >> RCC_HSICFGR_HSITRIM_Pos);
  1414. #endif /*RCC_VER_X*/
  1415. /* Get the LSE configuration -----------------------------------------------*/
  1416. #if defined(RCC_BDCR_LSEEXT)
  1417. if((RCC->BDCR &(RCC_BDCR_LSEBYP|RCC_BDCR_LSEEXT)) == RCC_BDCR_LSEBYP)
  1418. {
  1419. RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS;
  1420. }
  1421. else if((RCC->BDCR &(RCC_BDCR_LSEBYP|RCC_BDCR_LSEEXT)) == (RCC_BDCR_LSEBYP|RCC_BDCR_LSEEXT))
  1422. {
  1423. RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS_DIGITAL;
  1424. }
  1425. else if((RCC->BDCR &RCC_BDCR_LSEON) == RCC_BDCR_LSEON)
  1426. {
  1427. RCC_OscInitStruct->LSEState = RCC_LSE_ON;
  1428. }
  1429. else
  1430. {
  1431. RCC_OscInitStruct->LSEState = RCC_LSE_OFF;
  1432. }
  1433. #else
  1434. if((RCC->BDCR &RCC_BDCR_LSEBYP) == RCC_BDCR_LSEBYP)
  1435. {
  1436. RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS;
  1437. }
  1438. else if((RCC->BDCR &RCC_BDCR_LSEON) == RCC_BDCR_LSEON)
  1439. {
  1440. RCC_OscInitStruct->LSEState = RCC_LSE_ON;
  1441. }
  1442. else
  1443. {
  1444. RCC_OscInitStruct->LSEState = RCC_LSE_OFF;
  1445. }
  1446. #endif /* RCC_BDCR_LSEEXT */
  1447. /* Get the LSI configuration -----------------------------------------------*/
  1448. if((RCC->CSR &RCC_CSR_LSION) == RCC_CSR_LSION)
  1449. {
  1450. RCC_OscInitStruct->LSIState = RCC_LSI_ON;
  1451. }
  1452. else
  1453. {
  1454. RCC_OscInitStruct->LSIState = RCC_LSI_OFF;
  1455. }
  1456. /* Get the HSI48 configuration ---------------------------------------------*/
  1457. if((RCC->CR & RCC_CR_HSI48ON) == RCC_CR_HSI48ON)
  1458. {
  1459. RCC_OscInitStruct->HSI48State = RCC_HSI48_ON;
  1460. }
  1461. else
  1462. {
  1463. RCC_OscInitStruct->HSI48State = RCC_HSI48_OFF;
  1464. }
  1465. /* Get the PLL configuration -----------------------------------------------*/
  1466. if((RCC->CR &RCC_CR_PLLON) == RCC_CR_PLLON)
  1467. {
  1468. RCC_OscInitStruct->PLL.PLLState = RCC_PLL_ON;
  1469. }
  1470. else
  1471. {
  1472. RCC_OscInitStruct->PLL.PLLState = RCC_PLL_OFF;
  1473. }
  1474. RCC_OscInitStruct->PLL.PLLSource = (uint32_t)(RCC->PLLCKSELR & RCC_PLLCKSELR_PLLSRC);
  1475. RCC_OscInitStruct->PLL.PLLM = (uint32_t)((RCC->PLLCKSELR & RCC_PLLCKSELR_DIVM1)>> RCC_PLLCKSELR_DIVM1_Pos);
  1476. RCC_OscInitStruct->PLL.PLLN = (uint32_t)((RCC->PLL1DIVR & RCC_PLL1DIVR_N1) >> RCC_PLL1DIVR_N1_Pos)+ 1U;
  1477. RCC_OscInitStruct->PLL.PLLR = (uint32_t)((RCC->PLL1DIVR & RCC_PLL1DIVR_R1) >> RCC_PLL1DIVR_R1_Pos)+ 1U;
  1478. RCC_OscInitStruct->PLL.PLLP = (uint32_t)((RCC->PLL1DIVR & RCC_PLL1DIVR_P1) >> RCC_PLL1DIVR_P1_Pos)+ 1U;
  1479. RCC_OscInitStruct->PLL.PLLQ = (uint32_t)((RCC->PLL1DIVR & RCC_PLL1DIVR_Q1) >> RCC_PLL1DIVR_Q1_Pos)+ 1U;
  1480. RCC_OscInitStruct->PLL.PLLRGE = (uint32_t)((RCC->PLLCFGR & RCC_PLLCFGR_PLL1RGE));
  1481. RCC_OscInitStruct->PLL.PLLVCOSEL = (uint32_t)((RCC->PLLCFGR & RCC_PLLCFGR_PLL1VCOSEL) >> RCC_PLLCFGR_PLL1VCOSEL_Pos);
  1482. RCC_OscInitStruct->PLL.PLLFRACN = (uint32_t)(((RCC->PLL1FRACR & RCC_PLL1FRACR_FRACN1) >> RCC_PLL1FRACR_FRACN1_Pos));
  1483. }
  1484. /**
  1485. * @brief Configures the RCC_ClkInitStruct according to the internal
  1486. * RCC configuration registers.
  1487. * @param RCC_ClkInitStruct: pointer to an RCC_ClkInitTypeDef structure that
  1488. * will be configured.
  1489. * @param pFLatency: Pointer on the Flash Latency.
  1490. * @retval None
  1491. */
  1492. void HAL_RCC_GetClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t *pFLatency)
  1493. {
  1494. /* Set all possible values for the Clock type parameter --------------------*/
  1495. RCC_ClkInitStruct->ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_D1PCLK1 | RCC_CLOCKTYPE_PCLK1 |
  1496. RCC_CLOCKTYPE_PCLK2 | RCC_CLOCKTYPE_D3PCLK1 ;
  1497. /* Get the SYSCLK configuration --------------------------------------------*/
  1498. RCC_ClkInitStruct->SYSCLKSource = (uint32_t)(RCC->CFGR & RCC_CFGR_SW);
  1499. #if defined(RCC_D1CFGR_D1CPRE)
  1500. /* Get the SYSCLK configuration ----------------------------------------------*/
  1501. RCC_ClkInitStruct->SYSCLKDivider = (uint32_t)(RCC->D1CFGR & RCC_D1CFGR_D1CPRE);
  1502. /* Get the D1HCLK configuration ----------------------------------------------*/
  1503. RCC_ClkInitStruct->AHBCLKDivider = (uint32_t)(RCC->D1CFGR & RCC_D1CFGR_HPRE);
  1504. /* Get the APB3 configuration ----------------------------------------------*/
  1505. RCC_ClkInitStruct->APB3CLKDivider = (uint32_t)(RCC->D1CFGR & RCC_D1CFGR_D1PPRE);
  1506. /* Get the APB1 configuration ----------------------------------------------*/
  1507. RCC_ClkInitStruct->APB1CLKDivider = (uint32_t)(RCC->D2CFGR & RCC_D2CFGR_D2PPRE1);
  1508. /* Get the APB2 configuration ----------------------------------------------*/
  1509. RCC_ClkInitStruct->APB2CLKDivider = (uint32_t)(RCC->D2CFGR & RCC_D2CFGR_D2PPRE2);
  1510. /* Get the APB4 configuration ----------------------------------------------*/
  1511. RCC_ClkInitStruct->APB4CLKDivider = (uint32_t)(RCC->D3CFGR & RCC_D3CFGR_D3PPRE);
  1512. #else
  1513. /* Get the SYSCLK configuration ----------------------------------------------*/
  1514. RCC_ClkInitStruct->SYSCLKDivider = (uint32_t)(RCC->CDCFGR1 & RCC_CDCFGR1_CDCPRE);
  1515. /* Get the D1HCLK configuration ----------------------------------------------*/
  1516. RCC_ClkInitStruct->AHBCLKDivider = (uint32_t)(RCC->CDCFGR1 & RCC_CDCFGR1_HPRE);
  1517. /* Get the APB3 configuration ----------------------------------------------*/
  1518. RCC_ClkInitStruct->APB3CLKDivider = (uint32_t)(RCC->CDCFGR1 & RCC_CDCFGR1_CDPPRE);
  1519. /* Get the APB1 configuration ----------------------------------------------*/
  1520. RCC_ClkInitStruct->APB1CLKDivider = (uint32_t)(RCC->CDCFGR2 & RCC_CDCFGR2_CDPPRE1);
  1521. /* Get the APB2 configuration ----------------------------------------------*/
  1522. RCC_ClkInitStruct->APB2CLKDivider = (uint32_t)(RCC->CDCFGR2 & RCC_CDCFGR2_CDPPRE2);
  1523. /* Get the APB4 configuration ----------------------------------------------*/
  1524. RCC_ClkInitStruct->APB4CLKDivider = (uint32_t)(RCC->SRDCFGR & RCC_SRDCFGR_SRDPPRE);
  1525. #endif
  1526. /* Get the Flash Wait State (Latency) configuration ------------------------*/
  1527. *pFLatency = (uint32_t)(FLASH->ACR & FLASH_ACR_LATENCY);
  1528. }
  1529. /**
  1530. * @brief This function handles the RCC CSS interrupt request.
  1531. * @note This API should be called under the NMI_Handler().
  1532. * @retval None
  1533. */
  1534. void HAL_RCC_NMI_IRQHandler(void)
  1535. {
  1536. /* Check RCC CSSF flag */
  1537. if(__HAL_RCC_GET_IT(RCC_IT_CSS))
  1538. {
  1539. /* RCC Clock Security System interrupt user callback */
  1540. HAL_RCC_CCSCallback();
  1541. /* Clear RCC CSS pending bit */
  1542. __HAL_RCC_CLEAR_IT(RCC_IT_CSS);
  1543. }
  1544. }
  1545. /**
  1546. * @brief RCC Clock Security System interrupt callback
  1547. * @retval none
  1548. */
  1549. __weak void HAL_RCC_CCSCallback(void)
  1550. {
  1551. /* NOTE : This function Should not be modified, when the callback is needed,
  1552. the HAL_RCC_CCSCallback could be implemented in the user file
  1553. */
  1554. }
  1555. /**
  1556. * @}
  1557. */
  1558. /**
  1559. * @}
  1560. */
  1561. #endif /* HAL_RCC_MODULE_ENABLED */
  1562. /**
  1563. * @}
  1564. */
  1565. /**
  1566. * @}
  1567. */