stm32f3xx_hal_rcc.c 44 KB

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