You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1746 lines
61 KiB

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