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.
 
 
 

1112 lines
41 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_rcc.c
  4. * @author MCD Application Team
  5. * @version V1.7.1
  6. * @date 14-April-2017
  7. * @brief RCC HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the Reset and Clock Control (RCC) peripheral:
  10. * + Initialization and de-initialization functions
  11. * + Peripheral Control functions
  12. *
  13. @verbatim
  14. ==============================================================================
  15. ##### RCC specific features #####
  16. ==============================================================================
  17. [..]
  18. After reset the device is running from Internal High Speed oscillator
  19. (HSI 16MHz) with Flash 0 wait state, Flash prefetch buffer, D-Cache
  20. and I-Cache are disabled, and all peripherals are off except internal
  21. SRAM, Flash and JTAG.
  22. (+) There is no prescaler on High speed (AHB) and Low speed (APB) busses;
  23. all peripherals mapped on these busses are running at HSI speed.
  24. (+) The clock for all peripherals is switched off, except the SRAM and FLASH.
  25. (+) All GPIOs are in input floating state, except the JTAG pins which
  26. are assigned to be used for debug purpose.
  27. [..]
  28. Once the device started from reset, the user application has to:
  29. (+) Configure the clock source to be used to drive the System clock
  30. (if the application needs higher frequency/performance)
  31. (+) Configure the System clock frequency and Flash settings
  32. (+) Configure the AHB and APB busses prescalers
  33. (+) Enable the clock for the peripheral(s) to be used
  34. (+) Configure the clock source(s) for peripherals which clocks are not
  35. derived from the System clock (I2S, RTC, ADC, USB OTG FS/SDIO/RNG)
  36. ##### RCC Limitations #####
  37. ==============================================================================
  38. [..]
  39. A delay between an RCC peripheral clock enable and the effective peripheral
  40. enabling should be taken into account in order to manage the peripheral read/write
  41. from/to registers.
  42. (+) This delay depends on the peripheral mapping.
  43. (+) If peripheral is mapped on AHB: the delay is 2 AHB clock cycle
  44. after the clock enable bit is set on the hardware register
  45. (+) If peripheral is mapped on APB: the delay is 2 APB clock cycle
  46. after the clock enable bit is set on the hardware register
  47. [..]
  48. Implemented Workaround:
  49. (+) For AHB & APB peripherals, a dummy read to the peripheral register has been
  50. inserted in each __HAL_RCC_PPP_CLK_ENABLE() macro.
  51. @endverbatim
  52. ******************************************************************************
  53. * @attention
  54. *
  55. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  56. *
  57. * Redistribution and use in source and binary forms, with or without modification,
  58. * are permitted provided that the following conditions are met:
  59. * 1. Redistributions of source code must retain the above copyright notice,
  60. * this list of conditions and the following disclaimer.
  61. * 2. Redistributions in binary form must reproduce the above copyright notice,
  62. * this list of conditions and the following disclaimer in the documentation
  63. * and/or other materials provided with the distribution.
  64. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  65. * may be used to endorse or promote products derived from this software
  66. * without specific prior written permission.
  67. *
  68. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  69. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  70. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  71. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  72. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  73. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  74. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  75. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  76. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  77. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  78. *
  79. ******************************************************************************
  80. */
  81. /* Includes ------------------------------------------------------------------*/
  82. #include "stm32f4xx_hal.h"
  83. /** @addtogroup STM32F4xx_HAL_Driver
  84. * @{
  85. */
  86. /** @defgroup RCC RCC
  87. * @brief RCC HAL module driver
  88. * @{
  89. */
  90. #ifdef HAL_RCC_MODULE_ENABLED
  91. /* Private typedef -----------------------------------------------------------*/
  92. /* Private define ------------------------------------------------------------*/
  93. /** @addtogroup RCC_Private_Constants
  94. * @{
  95. */
  96. #define CLOCKSWITCH_TIMEOUT_VALUE 5000U /* 5 s */
  97. /* Private macro -------------------------------------------------------------*/
  98. #define __MCO1_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
  99. #define MCO1_GPIO_PORT GPIOA
  100. #define MCO1_PIN GPIO_PIN_8
  101. #define __MCO2_CLK_ENABLE() __HAL_RCC_GPIOC_CLK_ENABLE()
  102. #define MCO2_GPIO_PORT GPIOC
  103. #define MCO2_PIN GPIO_PIN_9
  104. /**
  105. * @}
  106. */
  107. /* Private variables ---------------------------------------------------------*/
  108. /** @defgroup RCC_Private_Variables RCC Private Variables
  109. * @{
  110. */
  111. /**
  112. * @}
  113. */
  114. /* Private function prototypes -----------------------------------------------*/
  115. /* Private functions ---------------------------------------------------------*/
  116. /** @defgroup RCC_Exported_Functions RCC Exported Functions
  117. * @{
  118. */
  119. /** @defgroup RCC_Exported_Functions_Group1 Initialization and de-initialization functions
  120. * @brief Initialization and Configuration functions
  121. *
  122. @verbatim
  123. ===============================================================================
  124. ##### Initialization and de-initialization functions #####
  125. ===============================================================================
  126. [..]
  127. This section provides functions allowing to configure the internal/external oscillators
  128. (HSE, HSI, LSE, LSI, PLL, CSS and MCO) and the System busses clocks (SYSCLK, AHB, APB1
  129. and APB2).
  130. [..] Internal/external clock and PLL configuration
  131. (#) HSI (high-speed internal), 16 MHz factory-trimmed RC used directly or through
  132. the PLL as System clock source.
  133. (#) LSI (low-speed internal), 32 KHz low consumption RC used as IWDG and/or RTC
  134. clock source.
  135. (#) HSE (high-speed external), 4 to 26 MHz crystal oscillator used directly or
  136. through the PLL as System clock source. Can be used also as RTC clock source.
  137. (#) LSE (low-speed external), 32 KHz oscillator used as RTC clock source.
  138. (#) PLL (clocked by HSI or HSE), featuring two different output clocks:
  139. (++) The first output is used to generate the high speed system clock (up to 168 MHz)
  140. (++) The second output is used to generate the clock for the USB OTG FS (48 MHz),
  141. the random analog generator (<=48 MHz) and the SDIO (<= 48 MHz).
  142. (#) CSS (Clock security system), once enable using the macro __HAL_RCC_CSS_ENABLE()
  143. and if a HSE clock failure occurs(HSE used directly or through PLL as System
  144. clock source), the System clocks automatically switched to HSI and an interrupt
  145. is generated if enabled. The interrupt is linked to the Cortex-M4 NMI
  146. (Non-Maskable Interrupt) exception vector.
  147. (#) MCO1 (microcontroller clock output), used to output HSI, LSE, HSE or PLL
  148. clock (through a configurable prescaler) on PA8 pin.
  149. (#) MCO2 (microcontroller clock output), used to output HSE, PLL, SYSCLK or PLLI2S
  150. clock (through a configurable prescaler) on PC9 pin.
  151. [..] System, AHB and APB busses clocks configuration
  152. (#) Several clock sources can be used to drive the System clock (SYSCLK): HSI,
  153. HSE and PLL.
  154. The AHB clock (HCLK) is derived from System clock through configurable
  155. prescaler and used to clock the CPU, memory and peripherals mapped
  156. on AHB bus (DMA, GPIO...). APB1 (PCLK1) and APB2 (PCLK2) clocks are derived
  157. from AHB clock through configurable prescalers and used to clock
  158. the peripherals mapped on these busses. You can use
  159. "HAL_RCC_GetSysClockFreq()" function to retrieve the frequencies of these clocks.
  160. (#) For the STM32F405xx/07xx and STM32F415xx/17xx devices, the maximum
  161. frequency of the SYSCLK and HCLK is 168 MHz, PCLK2 84 MHz and PCLK1 42 MHz.
  162. Depending on the device voltage range, the maximum frequency should
  163. be adapted accordingly (refer to the product datasheets for more details).
  164. (#) For the STM32F42xxx, STM32F43xxx, STM32F446xx, STM32F469xx and STM32F479xx devices,
  165. the maximum frequency of the SYSCLK and HCLK is 180 MHz, PCLK2 90 MHz and PCLK1 45 MHz.
  166. Depending on the device voltage range, the maximum frequency should
  167. be adapted accordingly (refer to the product datasheets for more details).
  168. (#) For the STM32F401xx, the maximum frequency of the SYSCLK and HCLK is 84 MHz,
  169. PCLK2 84 MHz and PCLK1 42 MHz.
  170. Depending on the device voltage range, the maximum frequency should
  171. be adapted accordingly (refer to the product datasheets for more details).
  172. (#) For the STM32F41xxx, the maximum frequency of the SYSCLK and HCLK is 100 MHz,
  173. PCLK2 100 MHz and PCLK1 50 MHz.
  174. Depending on the device voltage range, the maximum frequency should
  175. be adapted accordingly (refer to the product datasheets for more details).
  176. @endverbatim
  177. * @{
  178. */
  179. /**
  180. * @brief Resets the RCC clock configuration to the default reset state.
  181. * @note The default reset state of the clock configuration is given below:
  182. * - HSI ON and used as system clock source
  183. * - HSE and PLL OFF
  184. * - AHB, APB1 and APB2 prescaler set to 1.
  185. * - CSS, MCO1 and MCO2 OFF
  186. * - All interrupts disabled
  187. * @note This function doesn't modify the configuration of the
  188. * - Peripheral clocks
  189. * - LSI, LSE and RTC clocks
  190. * @retval None
  191. */
  192. __weak void HAL_RCC_DeInit(void)
  193. {}
  194. /**
  195. * @brief Initializes the RCC Oscillators according to the specified parameters in the
  196. * RCC_OscInitTypeDef.
  197. * @param RCC_OscInitStruct: pointer to an RCC_OscInitTypeDef structure that
  198. * contains the configuration information for the RCC Oscillators.
  199. * @note The PLL is not disabled when used as system clock.
  200. * @note Transitions LSE Bypass to LSE On and LSE On to LSE Bypass are not
  201. * supported by this API. User should request a transition to LSE Off
  202. * first and then LSE On or LSE Bypass.
  203. * @note Transition HSE Bypass to HSE On and HSE On to HSE Bypass are not
  204. * supported by this API. User should request a transition to HSE Off
  205. * first and then HSE On or HSE Bypass.
  206. * @retval HAL status
  207. */
  208. __weak HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  209. {
  210. uint32_t tickstart = 0U;
  211. /* Check the parameters */
  212. assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct->OscillatorType));
  213. /*------------------------------- HSE Configuration ------------------------*/
  214. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE)
  215. {
  216. /* Check the parameters */
  217. assert_param(IS_RCC_HSE(RCC_OscInitStruct->HSEState));
  218. /* When the HSE is used as system clock or clock source for PLL in these cases HSE will not disabled */
  219. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSE) ||\
  220. ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL) && ((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLCFGR_PLLSRC_HSE)))
  221. {
  222. if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET) && (RCC_OscInitStruct->HSEState == RCC_HSE_OFF))
  223. {
  224. return HAL_ERROR;
  225. }
  226. }
  227. else
  228. {
  229. /* Set the new HSE configuration ---------------------------------------*/
  230. __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct->HSEState);
  231. /* Check the HSE State */
  232. if((RCC_OscInitStruct->HSEState) != RCC_HSE_OFF)
  233. {
  234. /* Get Start Tick*/
  235. tickstart = HAL_GetTick();
  236. /* Wait till HSE is ready */
  237. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)
  238. {
  239. if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
  240. {
  241. return HAL_TIMEOUT;
  242. }
  243. }
  244. }
  245. else
  246. {
  247. /* Get Start Tick*/
  248. tickstart = HAL_GetTick();
  249. /* Wait till HSE is bypassed or disabled */
  250. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET)
  251. {
  252. if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
  253. {
  254. return HAL_TIMEOUT;
  255. }
  256. }
  257. }
  258. }
  259. }
  260. /*----------------------------- HSI Configuration --------------------------*/
  261. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI)
  262. {
  263. /* Check the parameters */
  264. assert_param(IS_RCC_HSI(RCC_OscInitStruct->HSIState));
  265. assert_param(IS_RCC_CALIBRATION_VALUE(RCC_OscInitStruct->HSICalibrationValue));
  266. /* Check if HSI is used as system clock or as PLL source when PLL is selected as system clock */
  267. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSI) ||\
  268. ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL) && ((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLCFGR_PLLSRC_HSI)))
  269. {
  270. /* When HSI is used as system clock it will not disabled */
  271. if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET) && (RCC_OscInitStruct->HSIState != RCC_HSI_ON))
  272. {
  273. return HAL_ERROR;
  274. }
  275. /* Otherwise, just the calibration is allowed */
  276. else
  277. {
  278. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  279. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  280. }
  281. }
  282. else
  283. {
  284. /* Check the HSI State */
  285. if((RCC_OscInitStruct->HSIState)!= RCC_HSI_OFF)
  286. {
  287. /* Enable the Internal High Speed oscillator (HSI). */
  288. __HAL_RCC_HSI_ENABLE();
  289. /* Get Start Tick*/
  290. tickstart = HAL_GetTick();
  291. /* Wait till HSI is ready */
  292. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET)
  293. {
  294. if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
  295. {
  296. return HAL_TIMEOUT;
  297. }
  298. }
  299. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  300. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  301. }
  302. else
  303. {
  304. /* Disable the Internal High Speed oscillator (HSI). */
  305. __HAL_RCC_HSI_DISABLE();
  306. /* Get Start Tick*/
  307. tickstart = HAL_GetTick();
  308. /* Wait till HSI is ready */
  309. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET)
  310. {
  311. if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
  312. {
  313. return HAL_TIMEOUT;
  314. }
  315. }
  316. }
  317. }
  318. }
  319. /*------------------------------ LSI Configuration -------------------------*/
  320. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI)
  321. {
  322. /* Check the parameters */
  323. assert_param(IS_RCC_LSI(RCC_OscInitStruct->LSIState));
  324. /* Check the LSI State */
  325. if((RCC_OscInitStruct->LSIState)!= RCC_LSI_OFF)
  326. {
  327. /* Enable the Internal Low Speed oscillator (LSI). */
  328. __HAL_RCC_LSI_ENABLE();
  329. /* Get Start Tick*/
  330. tickstart = HAL_GetTick();
  331. /* Wait till LSI is ready */
  332. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) == RESET)
  333. {
  334. if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
  335. {
  336. return HAL_TIMEOUT;
  337. }
  338. }
  339. }
  340. else
  341. {
  342. /* Disable the Internal Low Speed oscillator (LSI). */
  343. __HAL_RCC_LSI_DISABLE();
  344. /* Get Start Tick*/
  345. tickstart = HAL_GetTick();
  346. /* Wait till LSI is ready */
  347. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) != RESET)
  348. {
  349. if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
  350. {
  351. return HAL_TIMEOUT;
  352. }
  353. }
  354. }
  355. }
  356. /*------------------------------ LSE Configuration -------------------------*/
  357. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE)
  358. {
  359. /* Check the parameters */
  360. assert_param(IS_RCC_LSE(RCC_OscInitStruct->LSEState));
  361. /* Enable Power Clock*/
  362. __HAL_RCC_PWR_CLK_ENABLE();
  363. /* Enable write access to Backup domain */
  364. PWR->CR |= PWR_CR_DBP;
  365. /* Wait for Backup domain Write protection enable */
  366. tickstart = HAL_GetTick();
  367. while((PWR->CR & PWR_CR_DBP) == RESET)
  368. {
  369. if((HAL_GetTick() - tickstart ) > RCC_DBP_TIMEOUT_VALUE)
  370. {
  371. return HAL_TIMEOUT;
  372. }
  373. }
  374. /* Set the new LSE configuration -----------------------------------------*/
  375. __HAL_RCC_LSE_CONFIG(RCC_OscInitStruct->LSEState);
  376. /* Check the LSE State */
  377. if((RCC_OscInitStruct->LSEState) != RCC_LSE_OFF)
  378. {
  379. /* Get Start Tick*/
  380. tickstart = HAL_GetTick();
  381. /* Wait till LSE is ready */
  382. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
  383. {
  384. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  385. {
  386. return HAL_TIMEOUT;
  387. }
  388. }
  389. }
  390. else
  391. {
  392. /* Get Start Tick*/
  393. tickstart = HAL_GetTick();
  394. /* Wait till LSE is ready */
  395. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) != RESET)
  396. {
  397. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  398. {
  399. return HAL_TIMEOUT;
  400. }
  401. }
  402. }
  403. }
  404. /*-------------------------------- PLL Configuration -----------------------*/
  405. /* Check the parameters */
  406. assert_param(IS_RCC_PLL(RCC_OscInitStruct->PLL.PLLState));
  407. if ((RCC_OscInitStruct->PLL.PLLState) != RCC_PLL_NONE)
  408. {
  409. /* Check if the PLL is used as system clock or not */
  410. if(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_PLL)
  411. {
  412. if((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_ON)
  413. {
  414. /* Check the parameters */
  415. assert_param(IS_RCC_PLLSOURCE(RCC_OscInitStruct->PLL.PLLSource));
  416. assert_param(IS_RCC_PLLM_VALUE(RCC_OscInitStruct->PLL.PLLM));
  417. assert_param(IS_RCC_PLLN_VALUE(RCC_OscInitStruct->PLL.PLLN));
  418. assert_param(IS_RCC_PLLP_VALUE(RCC_OscInitStruct->PLL.PLLP));
  419. assert_param(IS_RCC_PLLQ_VALUE(RCC_OscInitStruct->PLL.PLLQ));
  420. /* Disable the main PLL. */
  421. __HAL_RCC_PLL_DISABLE();
  422. /* Get Start Tick*/
  423. tickstart = HAL_GetTick();
  424. /* Wait till PLL is ready */
  425. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
  426. {
  427. if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  428. {
  429. return HAL_TIMEOUT;
  430. }
  431. }
  432. /* Configure the main PLL clock source, multiplication and division factors. */
  433. WRITE_REG(RCC->PLLCFGR, (RCC_OscInitStruct->PLL.PLLSource | \
  434. RCC_OscInitStruct->PLL.PLLM | \
  435. (RCC_OscInitStruct->PLL.PLLN << POSITION_VAL(RCC_PLLCFGR_PLLN)) | \
  436. (((RCC_OscInitStruct->PLL.PLLP >> 1U) - 1U) << POSITION_VAL(RCC_PLLCFGR_PLLP)) | \
  437. (RCC_OscInitStruct->PLL.PLLQ << POSITION_VAL(RCC_PLLCFGR_PLLQ))));
  438. /* Enable the main PLL. */
  439. __HAL_RCC_PLL_ENABLE();
  440. /* Get Start Tick*/
  441. tickstart = HAL_GetTick();
  442. /* Wait till PLL is ready */
  443. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
  444. {
  445. if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  446. {
  447. return HAL_TIMEOUT;
  448. }
  449. }
  450. }
  451. else
  452. {
  453. /* Disable the main PLL. */
  454. __HAL_RCC_PLL_DISABLE();
  455. /* Get Start Tick*/
  456. tickstart = HAL_GetTick();
  457. /* Wait till PLL is ready */
  458. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
  459. {
  460. if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  461. {
  462. return HAL_TIMEOUT;
  463. }
  464. }
  465. }
  466. }
  467. else
  468. {
  469. return HAL_ERROR;
  470. }
  471. }
  472. return HAL_OK;
  473. }
  474. /**
  475. * @brief Initializes the CPU, AHB and APB busses clocks according to the specified
  476. * parameters in the RCC_ClkInitStruct.
  477. * @param RCC_ClkInitStruct: pointer to an RCC_OscInitTypeDef structure that
  478. * contains the configuration information for the RCC peripheral.
  479. * @param FLatency: FLASH Latency, this parameter depend on device selected
  480. *
  481. * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency
  482. * and updated by HAL_RCC_GetHCLKFreq() function called within this function
  483. *
  484. * @note The HSI is used (enabled by hardware) as system clock source after
  485. * startup from Reset, wake-up from STOP and STANDBY mode, or in case
  486. * of failure of the HSE used directly or indirectly as system clock
  487. * (if the Clock Security System CSS is enabled).
  488. *
  489. * @note A switch from one clock source to another occurs only if the target
  490. * clock source is ready (clock stable after startup delay or PLL locked).
  491. * If a clock source which is not yet ready is selected, the switch will
  492. * occur when the clock source will be ready.
  493. *
  494. * @note Depending on the device voltage range, the software has to set correctly
  495. * HPRE[3:0] bits to ensure that HCLK not exceed the maximum allowed frequency
  496. * (for more details refer to section above "Initialization/de-initialization functions")
  497. * @retval None
  498. */
  499. HAL_StatusTypeDef HAL_RCC_ClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t FLatency)
  500. {
  501. uint32_t tickstart = 0U;
  502. /* Check the parameters */
  503. assert_param(IS_RCC_CLOCKTYPE(RCC_ClkInitStruct->ClockType));
  504. assert_param(IS_FLASH_LATENCY(FLatency));
  505. /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
  506. must be correctly programmed according to the frequency of the CPU clock
  507. (HCLK) and the supply voltage of the device. */
  508. /* Increasing the number of wait states because of higher CPU frequency */
  509. if(FLatency > (FLASH->ACR & FLASH_ACR_LATENCY))
  510. {
  511. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  512. __HAL_FLASH_SET_LATENCY(FLatency);
  513. /* Check that the new number of wait states is taken into account to access the Flash
  514. memory by reading the FLASH_ACR register */
  515. if((FLASH->ACR & FLASH_ACR_LATENCY) != FLatency)
  516. {
  517. return HAL_ERROR;
  518. }
  519. }
  520. /*-------------------------- HCLK Configuration --------------------------*/
  521. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)
  522. {
  523. assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
  524. MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
  525. }
  526. /*------------------------- SYSCLK Configuration ---------------------------*/
  527. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_SYSCLK) == RCC_CLOCKTYPE_SYSCLK)
  528. {
  529. assert_param(IS_RCC_SYSCLKSOURCE(RCC_ClkInitStruct->SYSCLKSource));
  530. /* HSE is selected as System Clock Source */
  531. if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
  532. {
  533. /* Check the HSE ready flag */
  534. if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)
  535. {
  536. return HAL_ERROR;
  537. }
  538. }
  539. /* PLL is selected as System Clock Source */
  540. else if((RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK) ||
  541. (RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLRCLK))
  542. {
  543. /* Check the PLL ready flag */
  544. if(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
  545. {
  546. return HAL_ERROR;
  547. }
  548. }
  549. /* HSI is selected as System Clock Source */
  550. else
  551. {
  552. /* Check the HSI ready flag */
  553. if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET)
  554. {
  555. return HAL_ERROR;
  556. }
  557. }
  558. __HAL_RCC_SYSCLK_CONFIG(RCC_ClkInitStruct->SYSCLKSource);
  559. /* Get Start Tick*/
  560. tickstart = HAL_GetTick();
  561. if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
  562. {
  563. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_HSE)
  564. {
  565. if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
  566. {
  567. return HAL_TIMEOUT;
  568. }
  569. }
  570. }
  571. else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
  572. {
  573. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_PLLCLK)
  574. {
  575. if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
  576. {
  577. return HAL_TIMEOUT;
  578. }
  579. }
  580. }
  581. else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLRCLK)
  582. {
  583. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_PLLRCLK)
  584. {
  585. if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
  586. {
  587. return HAL_TIMEOUT;
  588. }
  589. }
  590. }
  591. else
  592. {
  593. while(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_HSI)
  594. {
  595. if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
  596. {
  597. return HAL_TIMEOUT;
  598. }
  599. }
  600. }
  601. }
  602. /* Decreasing the number of wait states because of lower CPU frequency */
  603. if(FLatency < (FLASH->ACR & FLASH_ACR_LATENCY))
  604. {
  605. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  606. __HAL_FLASH_SET_LATENCY(FLatency);
  607. /* Check that the new number of wait states is taken into account to access the Flash
  608. memory by reading the FLASH_ACR register */
  609. if((FLASH->ACR & FLASH_ACR_LATENCY) != FLatency)
  610. {
  611. return HAL_ERROR;
  612. }
  613. }
  614. /*-------------------------- PCLK1 Configuration ---------------------------*/
  615. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
  616. {
  617. assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB1CLKDivider));
  618. MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE1, RCC_ClkInitStruct->APB1CLKDivider);
  619. }
  620. /*-------------------------- PCLK2 Configuration ---------------------------*/
  621. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK2) == RCC_CLOCKTYPE_PCLK2)
  622. {
  623. assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB2CLKDivider));
  624. MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE2, ((RCC_ClkInitStruct->APB2CLKDivider) << 3U));
  625. }
  626. /* Update the SystemCoreClock global variable */
  627. SystemCoreClock = HAL_RCC_GetSysClockFreq() >> AHBPrescTable[(RCC->CFGR & RCC_CFGR_HPRE)>> POSITION_VAL(RCC_CFGR_HPRE)];
  628. /* Configure the source of time base considering new system clocks settings*/
  629. HAL_InitTick (TICK_INT_PRIORITY);
  630. return HAL_OK;
  631. }
  632. /**
  633. * @}
  634. */
  635. /** @defgroup RCC_Exported_Functions_Group2 Peripheral Control functions
  636. * @brief RCC clocks control functions
  637. *
  638. @verbatim
  639. ===============================================================================
  640. ##### Peripheral Control functions #####
  641. ===============================================================================
  642. [..]
  643. This subsection provides a set of functions allowing to control the RCC Clocks
  644. frequencies.
  645. @endverbatim
  646. * @{
  647. */
  648. /**
  649. * @brief Selects the clock source to output on MCO1 pin(PA8) or on MCO2 pin(PC9).
  650. * @note PA8/PC9 should be configured in alternate function mode.
  651. * @param RCC_MCOx: specifies the output direction for the clock source.
  652. * This parameter can be one of the following values:
  653. * @arg RCC_MCO1: Clock source to output on MCO1 pin(PA8).
  654. * @arg RCC_MCO2: Clock source to output on MCO2 pin(PC9).
  655. * @param RCC_MCOSource: specifies the clock source to output.
  656. * This parameter can be one of the following values:
  657. * @arg RCC_MCO1SOURCE_HSI: HSI clock selected as MCO1 source
  658. * @arg RCC_MCO1SOURCE_LSE: LSE clock selected as MCO1 source
  659. * @arg RCC_MCO1SOURCE_HSE: HSE clock selected as MCO1 source
  660. * @arg RCC_MCO1SOURCE_PLLCLK: main PLL clock selected as MCO1 source
  661. * @arg RCC_MCO2SOURCE_SYSCLK: System clock (SYSCLK) selected as MCO2 source
  662. * @arg RCC_MCO2SOURCE_PLLI2SCLK: PLLI2S clock selected as MCO2 source, available for all STM32F4 devices except STM32F410xx
  663. * @arg RCC_MCO2SOURCE_I2SCLK: I2SCLK clock selected as MCO2 source, available only for STM32F410Rx devices
  664. * @arg RCC_MCO2SOURCE_HSE: HSE clock selected as MCO2 source
  665. * @arg RCC_MCO2SOURCE_PLLCLK: main PLL clock selected as MCO2 source
  666. * @param RCC_MCODiv: specifies the MCOx prescaler.
  667. * This parameter can be one of the following values:
  668. * @arg RCC_MCODIV_1: no division applied to MCOx clock
  669. * @arg RCC_MCODIV_2: division by 2 applied to MCOx clock
  670. * @arg RCC_MCODIV_3: division by 3 applied to MCOx clock
  671. * @arg RCC_MCODIV_4: division by 4 applied to MCOx clock
  672. * @arg RCC_MCODIV_5: division by 5 applied to MCOx clock
  673. * @note For STM32F410Rx devices to output I2SCLK clock on MCO2 you should have
  674. * at last one of the SPI clocks enabled (SPI1, SPI2 or SPI5).
  675. * @retval None
  676. */
  677. void HAL_RCC_MCOConfig(uint32_t RCC_MCOx, uint32_t RCC_MCOSource, uint32_t RCC_MCODiv)
  678. {
  679. GPIO_InitTypeDef GPIO_InitStruct;
  680. /* Check the parameters */
  681. assert_param(IS_RCC_MCO(RCC_MCOx));
  682. assert_param(IS_RCC_MCODIV(RCC_MCODiv));
  683. /* RCC_MCO1 */
  684. if(RCC_MCOx == RCC_MCO1)
  685. {
  686. assert_param(IS_RCC_MCO1SOURCE(RCC_MCOSource));
  687. /* MCO1 Clock Enable */
  688. __MCO1_CLK_ENABLE();
  689. /* Configure the MCO1 pin in alternate function mode */
  690. GPIO_InitStruct.Pin = MCO1_PIN;
  691. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  692. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  693. GPIO_InitStruct.Pull = GPIO_NOPULL;
  694. GPIO_InitStruct.Alternate = GPIO_AF0_MCO;
  695. HAL_GPIO_Init(MCO1_GPIO_PORT, &GPIO_InitStruct);
  696. /* Mask MCO1 and MCO1PRE[2:0] bits then Select MCO1 clock source and prescaler */
  697. MODIFY_REG(RCC->CFGR, (RCC_CFGR_MCO1 | RCC_CFGR_MCO1PRE), (RCC_MCOSource | RCC_MCODiv));
  698. /* This RCC MCO1 enable feature is available only on STM32F410xx devices */
  699. #if defined(RCC_CFGR_MCO1EN)
  700. __HAL_RCC_MCO1_ENABLE();
  701. #endif /* RCC_CFGR_MCO1EN */
  702. }
  703. #if defined(RCC_CFGR_MCO2)
  704. else
  705. {
  706. assert_param(IS_RCC_MCO2SOURCE(RCC_MCOSource));
  707. /* MCO2 Clock Enable */
  708. __MCO2_CLK_ENABLE();
  709. /* Configure the MCO2 pin in alternate function mode */
  710. GPIO_InitStruct.Pin = MCO2_PIN;
  711. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  712. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  713. GPIO_InitStruct.Pull = GPIO_NOPULL;
  714. GPIO_InitStruct.Alternate = GPIO_AF0_MCO;
  715. HAL_GPIO_Init(MCO2_GPIO_PORT, &GPIO_InitStruct);
  716. /* Mask MCO2 and MCO2PRE[2:0] bits then Select MCO2 clock source and prescaler */
  717. MODIFY_REG(RCC->CFGR, (RCC_CFGR_MCO2 | RCC_CFGR_MCO2PRE), (RCC_MCOSource | (RCC_MCODiv << 3U)));
  718. /* This RCC MCO2 enable feature is available only on STM32F410Rx devices */
  719. #if defined(RCC_CFGR_MCO2EN)
  720. __HAL_RCC_MCO2_ENABLE();
  721. #endif /* RCC_CFGR_MCO2EN */
  722. }
  723. #endif /* RCC_CFGR_MCO2 */
  724. }
  725. /**
  726. * @brief Enables the Clock Security System.
  727. * @note If a failure is detected on the HSE oscillator clock, this oscillator
  728. * is automatically disabled and an interrupt is generated to inform the
  729. * software about the failure (Clock Security System Interrupt, CSSI),
  730. * allowing the MCU to perform rescue operations. The CSSI is linked to
  731. * the Cortex-M4 NMI (Non-Maskable Interrupt) exception vector.
  732. * @retval None
  733. */
  734. void HAL_RCC_EnableCSS(void)
  735. {
  736. *(__IO uint32_t *) RCC_CR_CSSON_BB = (uint32_t)ENABLE;
  737. }
  738. /**
  739. * @brief Disables the Clock Security System.
  740. * @retval None
  741. */
  742. void HAL_RCC_DisableCSS(void)
  743. {
  744. *(__IO uint32_t *) RCC_CR_CSSON_BB = (uint32_t)DISABLE;
  745. }
  746. /**
  747. * @brief Returns the SYSCLK frequency
  748. *
  749. * @note The system frequency computed by this function is not the real
  750. * frequency in the chip. It is calculated based on the predefined
  751. * constant and the selected clock source:
  752. * @note If SYSCLK source is HSI, function returns values based on HSI_VALUE(*)
  753. * @note If SYSCLK source is HSE, function returns values based on HSE_VALUE(**)
  754. * @note If SYSCLK source is PLL, function returns values based on HSE_VALUE(**)
  755. * or HSI_VALUE(*) multiplied/divided by the PLL factors.
  756. * @note (*) HSI_VALUE is a constant defined in stm32f4xx_hal_conf.h file (default value
  757. * 16 MHz) but the real value may vary depending on the variations
  758. * in voltage and temperature.
  759. * @note (**) HSE_VALUE is a constant defined in stm32f4xx_hal_conf.h file (default value
  760. * 25 MHz), user has to ensure that HSE_VALUE is same as the real
  761. * frequency of the crystal used. Otherwise, this function may
  762. * have wrong result.
  763. *
  764. * @note The result of this function could be not correct when using fractional
  765. * value for HSE crystal.
  766. *
  767. * @note This function can be used by the user application to compute the
  768. * baudrate for the communication peripherals or configure other parameters.
  769. *
  770. * @note Each time SYSCLK changes, this function must be called to update the
  771. * right SYSCLK value. Otherwise, any configuration based on this function will be incorrect.
  772. *
  773. *
  774. * @retval SYSCLK frequency
  775. */
  776. __weak uint32_t HAL_RCC_GetSysClockFreq(void)
  777. {
  778. uint32_t pllm = 0U, pllvco = 0U, pllp = 0U;
  779. uint32_t sysclockfreq = 0U;
  780. /* Get SYSCLK source -------------------------------------------------------*/
  781. switch (RCC->CFGR & RCC_CFGR_SWS)
  782. {
  783. case RCC_CFGR_SWS_HSI: /* HSI used as system clock source */
  784. {
  785. sysclockfreq = HSI_VALUE;
  786. break;
  787. }
  788. case RCC_CFGR_SWS_HSE: /* HSE used as system clock source */
  789. {
  790. sysclockfreq = HSE_VALUE;
  791. break;
  792. }
  793. case RCC_CFGR_SWS_PLL: /* PLL used as system clock source */
  794. {
  795. /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM) * PLLN
  796. SYSCLK = PLL_VCO / PLLP */
  797. pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
  798. if(__HAL_RCC_GET_PLL_OSCSOURCE() != RCC_PLLSOURCE_HSI)
  799. {
  800. /* HSE used as PLL clock source */
  801. //pllvco = ((HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> POSITION_VAL(RCC_PLLCFGR_PLLN)));
  802. // dpgeorge: Adjust the way the arithmetic is done so it retains
  803. // precision for the case that pllm doesn't evenly divide HSE_VALUE.
  804. // Must be sure not to overflow, so divide by 4 first. HSE_VALUE
  805. // should be a multiple of 4 (being a multiple of 100 is enough).
  806. pllvco = ((HSE_VALUE / 4) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> POSITION_VAL(RCC_PLLCFGR_PLLN))) / pllm * 4;
  807. }
  808. else
  809. {
  810. /* HSI used as PLL clock source */
  811. pllvco = ((HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> POSITION_VAL(RCC_PLLCFGR_PLLN)));
  812. }
  813. pllp = ((((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >> POSITION_VAL(RCC_PLLCFGR_PLLP)) + 1U) *2U);
  814. sysclockfreq = pllvco/pllp;
  815. break;
  816. }
  817. default:
  818. {
  819. sysclockfreq = HSI_VALUE;
  820. break;
  821. }
  822. }
  823. return sysclockfreq;
  824. }
  825. /**
  826. * @brief Returns the HCLK frequency
  827. * @note Each time HCLK changes, this function must be called to update the
  828. * right HCLK value. Otherwise, any configuration based on this function will be incorrect.
  829. *
  830. * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency
  831. * and updated within this function
  832. * @retval HCLK frequency
  833. */
  834. uint32_t HAL_RCC_GetHCLKFreq(void)
  835. {
  836. return SystemCoreClock;
  837. }
  838. /**
  839. * @brief Returns the PCLK1 frequency
  840. * @note Each time PCLK1 changes, this function must be called to update the
  841. * right PCLK1 value. Otherwise, any configuration based on this function will be incorrect.
  842. * @retval PCLK1 frequency
  843. */
  844. uint32_t HAL_RCC_GetPCLK1Freq(void)
  845. {
  846. /* Get HCLK source and Compute PCLK1 frequency ---------------------------*/
  847. return (HAL_RCC_GetHCLKFreq() >> APBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE1)>> POSITION_VAL(RCC_CFGR_PPRE1)]);
  848. }
  849. /**
  850. * @brief Returns the PCLK2 frequency
  851. * @note Each time PCLK2 changes, this function must be called to update the
  852. * right PCLK2 value. Otherwise, any configuration based on this function will be incorrect.
  853. * @retval PCLK2 frequency
  854. */
  855. uint32_t HAL_RCC_GetPCLK2Freq(void)
  856. {
  857. /* Get HCLK source and Compute PCLK2 frequency ---------------------------*/
  858. return (HAL_RCC_GetHCLKFreq()>> APBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE2)>> POSITION_VAL(RCC_CFGR_PPRE2)]);
  859. }
  860. /**
  861. * @brief Configures the RCC_OscInitStruct according to the internal
  862. * RCC configuration registers.
  863. * @param RCC_OscInitStruct: pointer to an RCC_OscInitTypeDef structure that
  864. * will be configured.
  865. * @retval None
  866. */
  867. __weak void HAL_RCC_GetOscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  868. {
  869. /* Set all possible values for the Oscillator type parameter ---------------*/
  870. RCC_OscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_LSI;
  871. /* Get the HSE configuration -----------------------------------------------*/
  872. if((RCC->CR &RCC_CR_HSEBYP) == RCC_CR_HSEBYP)
  873. {
  874. RCC_OscInitStruct->HSEState = RCC_HSE_BYPASS;
  875. }
  876. else if((RCC->CR &RCC_CR_HSEON) == RCC_CR_HSEON)
  877. {
  878. RCC_OscInitStruct->HSEState = RCC_HSE_ON;
  879. }
  880. else
  881. {
  882. RCC_OscInitStruct->HSEState = RCC_HSE_OFF;
  883. }
  884. /* Get the HSI configuration -----------------------------------------------*/
  885. if((RCC->CR &RCC_CR_HSION) == RCC_CR_HSION)
  886. {
  887. RCC_OscInitStruct->HSIState = RCC_HSI_ON;
  888. }
  889. else
  890. {
  891. RCC_OscInitStruct->HSIState = RCC_HSI_OFF;
  892. }
  893. RCC_OscInitStruct->HSICalibrationValue = (uint32_t)((RCC->CR &RCC_CR_HSITRIM) >> POSITION_VAL(RCC_CR_HSITRIM));
  894. /* Get the LSE configuration -----------------------------------------------*/
  895. if((RCC->BDCR &RCC_BDCR_LSEBYP) == RCC_BDCR_LSEBYP)
  896. {
  897. RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS;
  898. }
  899. else if((RCC->BDCR &RCC_BDCR_LSEON) == RCC_BDCR_LSEON)
  900. {
  901. RCC_OscInitStruct->LSEState = RCC_LSE_ON;
  902. }
  903. else
  904. {
  905. RCC_OscInitStruct->LSEState = RCC_LSE_OFF;
  906. }
  907. /* Get the LSI configuration -----------------------------------------------*/
  908. if((RCC->CSR &RCC_CSR_LSION) == RCC_CSR_LSION)
  909. {
  910. RCC_OscInitStruct->LSIState = RCC_LSI_ON;
  911. }
  912. else
  913. {
  914. RCC_OscInitStruct->LSIState = RCC_LSI_OFF;
  915. }
  916. /* Get the PLL configuration -----------------------------------------------*/
  917. if((RCC->CR &RCC_CR_PLLON) == RCC_CR_PLLON)
  918. {
  919. RCC_OscInitStruct->PLL.PLLState = RCC_PLL_ON;
  920. }
  921. else
  922. {
  923. RCC_OscInitStruct->PLL.PLLState = RCC_PLL_OFF;
  924. }
  925. RCC_OscInitStruct->PLL.PLLSource = (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC);
  926. RCC_OscInitStruct->PLL.PLLM = (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM);
  927. RCC_OscInitStruct->PLL.PLLN = (uint32_t)((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> POSITION_VAL(RCC_PLLCFGR_PLLN));
  928. RCC_OscInitStruct->PLL.PLLP = (uint32_t)((((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) + RCC_PLLCFGR_PLLP_0) << 1U) >> POSITION_VAL(RCC_PLLCFGR_PLLP));
  929. RCC_OscInitStruct->PLL.PLLQ = (uint32_t)((RCC->PLLCFGR & RCC_PLLCFGR_PLLQ) >> POSITION_VAL(RCC_PLLCFGR_PLLQ));
  930. }
  931. /**
  932. * @brief Configures the RCC_ClkInitStruct according to the internal
  933. * RCC configuration registers.
  934. * @param RCC_ClkInitStruct: pointer to an RCC_ClkInitTypeDef structure that
  935. * will be configured.
  936. * @param pFLatency: Pointer on the Flash Latency.
  937. * @retval None
  938. */
  939. void HAL_RCC_GetClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t *pFLatency)
  940. {
  941. /* Set all possible values for the Clock type parameter --------------------*/
  942. RCC_ClkInitStruct->ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  943. /* Get the SYSCLK configuration --------------------------------------------*/
  944. RCC_ClkInitStruct->SYSCLKSource = (uint32_t)(RCC->CFGR & RCC_CFGR_SW);
  945. /* Get the HCLK configuration ----------------------------------------------*/
  946. RCC_ClkInitStruct->AHBCLKDivider = (uint32_t)(RCC->CFGR & RCC_CFGR_HPRE);
  947. /* Get the APB1 configuration ----------------------------------------------*/
  948. RCC_ClkInitStruct->APB1CLKDivider = (uint32_t)(RCC->CFGR & RCC_CFGR_PPRE1);
  949. /* Get the APB2 configuration ----------------------------------------------*/
  950. RCC_ClkInitStruct->APB2CLKDivider = (uint32_t)((RCC->CFGR & RCC_CFGR_PPRE2) >> 3U);
  951. /* Get the Flash Wait State (Latency) configuration ------------------------*/
  952. *pFLatency = (uint32_t)(FLASH->ACR & FLASH_ACR_LATENCY);
  953. }
  954. /**
  955. * @brief This function handles the RCC CSS interrupt request.
  956. * @note This API should be called under the NMI_Handler().
  957. * @retval None
  958. */
  959. void HAL_RCC_NMI_IRQHandler(void)
  960. {
  961. /* Check RCC CSSF flag */
  962. if(__HAL_RCC_GET_IT(RCC_IT_CSS))
  963. {
  964. /* RCC Clock Security System interrupt user callback */
  965. HAL_RCC_CSSCallback();
  966. /* Clear RCC CSS pending bit */
  967. __HAL_RCC_CLEAR_IT(RCC_IT_CSS);
  968. }
  969. }
  970. /**
  971. * @brief RCC Clock Security System interrupt callback
  972. * @retval None
  973. */
  974. __weak void HAL_RCC_CSSCallback(void)
  975. {
  976. /* NOTE : This function Should not be modified, when the callback is needed,
  977. the HAL_RCC_CSSCallback could be implemented in the user file
  978. */
  979. }
  980. /**
  981. * @}
  982. */
  983. /**
  984. * @}
  985. */
  986. #endif /* HAL_RCC_MODULE_ENABLED */
  987. /**
  988. * @}
  989. */
  990. /**
  991. * @}
  992. */
  993. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/