stm32f4xx_hal_rcc.c 46 KB

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