stm32f3xx_hal_rcc.c 44 KB

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