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.

stm32f4xx_hal_rcc.c 41KB

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