stm32f4xx_hal_rcc.c 45 KB

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