stm32f2xx_hal_rcc.c 44 KB

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