stm32f7xx_hal_rcc.c 44 KB

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