stm32f4xx_hal_rcc.c 44 KB

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