stm32f7xx_hal_rcc.c 44 KB

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