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. * Copyright (c) 2016 STMicroelectronics.
  49. * All rights reserved.
  50. *
  51. * This software is licensed under terms that can be found in the LICENSE file in
  52. * the root directory of this software component.
  53. * If no LICENSE file comes with this software, it is provided AS-IS.
  54. ******************************************************************************
  55. */
  56. /* Includes ------------------------------------------------------------------*/
  57. #include "stm32f3xx_hal.h"
  58. /** @addtogroup STM32F3xx_HAL_Driver
  59. * @{
  60. */
  61. /** @defgroup RCC RCC
  62. * @brief RCC HAL module driver
  63. * @{
  64. */
  65. #ifdef HAL_RCC_MODULE_ENABLED
  66. /* Private typedef -----------------------------------------------------------*/
  67. /* Private define ------------------------------------------------------------*/
  68. /** @defgroup RCC_Private_Constants RCC Private Constants
  69. * @{
  70. */
  71. /* Bits position in in the CFGR register */
  72. #define RCC_CFGR_HPRE_BITNUMBER POSITION_VAL(RCC_CFGR_HPRE)
  73. #define RCC_CFGR_PPRE1_BITNUMBER POSITION_VAL(RCC_CFGR_PPRE1)
  74. #define RCC_CFGR_PPRE2_BITNUMBER POSITION_VAL(RCC_CFGR_PPRE2)
  75. /**
  76. * @}
  77. */
  78. /* Private macro -------------------------------------------------------------*/
  79. /** @defgroup RCC_Private_Macros RCC Private Macros
  80. * @{
  81. */
  82. #define MCO1_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
  83. #define MCO1_GPIO_PORT GPIOA
  84. #define MCO1_PIN GPIO_PIN_8
  85. /**
  86. * @}
  87. */
  88. /* Private variables ---------------------------------------------------------*/
  89. /** @defgroup RCC_Private_Variables RCC Private Variables
  90. * @{
  91. */
  92. static const uint8_t aPLLMULFactorTable[16U] = { 2U, 3U, 4U, 5U, 6U, 7U, 8U, 9U,
  93. 10U, 11U, 12U, 13U, 14U, 15U, 16U, 16U};
  94. static const uint8_t aPredivFactorTable[16U] = { 1U, 2U, 3U, 4U, 5U, 6U, 7U, 8U,
  95. 9U,10U, 11U, 12U, 13U, 14U, 15U, 16U};
  96. /**
  97. * @}
  98. */
  99. /* Private function prototypes -----------------------------------------------*/
  100. /* Exported functions ---------------------------------------------------------*/
  101. /** @defgroup RCC_Exported_Functions RCC Exported Functions
  102. * @{
  103. */
  104. /** @defgroup RCC_Exported_Functions_Group1 Initialization and de-initialization functions
  105. * @brief Initialization and Configuration functions
  106. *
  107. @verbatim
  108. ===============================================================================
  109. ##### Initialization and de-initialization functions #####
  110. ===============================================================================
  111. [..]
  112. This section provides functions allowing to configure the internal/external oscillators
  113. (HSE, HSI, LSE, LSI, PLL, CSS and MCO) and the System buses clocks (SYSCLK, AHB, APB1
  114. and APB2).
  115. [..] Internal/external clock and PLL configuration
  116. (#) HSI (high-speed internal), 8 MHz factory-trimmed RC used directly or through
  117. the PLL as System clock source.
  118. The HSI clock can be used also to clock the USART and I2C peripherals.
  119. (#) LSI (low-speed internal), ~40 KHz low consumption RC used as IWDG and/or RTC
  120. clock source.
  121. (#) HSE (high-speed external), 4 to 32 MHz crystal oscillator used directly or
  122. through the PLL as System clock source. Can be used also as RTC clock source.
  123. (#) LSE (low-speed external), 32 KHz oscillator used as RTC clock source.
  124. (#) PLL (clocked by HSI or HSE), featuring different output clocks:
  125. (++) The first output is used to generate the high speed system clock (up to 72 MHz)
  126. (++) The second output is used to generate the clock for the USB FS (48 MHz)
  127. (++) The third output may be used to generate the clock for the ADC peripherals (up to 72 MHz)
  128. (++) The fourth output may be used to generate the clock for the TIM peripherals (144 MHz)
  129. (#) CSS (Clock security system), once enable using the macro __HAL_RCC_CSS_ENABLE()
  130. and if a HSE clock failure occurs(HSE used directly or through PLL as System
  131. clock source), the System clocks automatically switched to HSI and an interrupt
  132. is generated if enabled. The interrupt is linked to the Cortex-M4 NMI
  133. (Non-Maskable Interrupt) exception vector.
  134. (#) MCO (microcontroller clock output), used to output SYSCLK, HSI, HSE, LSI, LSE or PLL
  135. clock (divided by 2) output on pin (such as PA8 pin).
  136. [..] System, AHB and APB buses clocks configuration
  137. (#) Several clock sources can be used to drive the System clock (SYSCLK): HSI,
  138. HSE and PLL.
  139. The AHB clock (HCLK) is derived from System clock through configurable
  140. prescaler and used to clock the CPU, memory and peripherals mapped
  141. on AHB bus (DMA, GPIO...). APB1 (PCLK1) and APB2 (PCLK2) clocks are derived
  142. from AHB clock through configurable prescalers and used to clock
  143. the peripherals mapped on these buses. You can use
  144. "HAL_RCC_GetSysClockFreq()" function to retrieve the frequencies of these clocks.
  145. (#) All the peripheral clocks are derived from the System clock (SYSCLK) except:
  146. (++) The FLASH program/erase clock which is always HSI 8MHz clock.
  147. (++) The USB 48 MHz clock which is derived from the PLL VCO clock.
  148. (++) The USART clock which can be derived as well from HSI 8MHz, LSI or LSE.
  149. (++) The I2C clock which can be derived as well from HSI 8MHz clock.
  150. (++) The ADC clock which is derived from PLL output.
  151. (++) The RTC clock which is derived from the LSE, LSI or 1 MHz HSE_RTC
  152. (HSE divided by a programmable prescaler). The System clock (SYSCLK)
  153. frequency must be higher or equal to the RTC clock frequency.
  154. (++) IWDG clock which is always the LSI clock.
  155. (#) For the STM32F3xx devices, the maximum frequency of the SYSCLK, HCLK, PCLK1 and PCLK2 is 72 MHz,
  156. Depending on the SYSCLK frequency, the flash latency should be adapted accordingly.
  157. (#) After reset, the System clock source is the HSI (8 MHz) with 0 WS and
  158. prefetch is disabled.
  159. @endverbatim
  160. * @{
  161. */
  162. /*
  163. Additional consideration on the SYSCLK based on Latency settings:
  164. +-----------------------------------------------+
  165. | Latency | SYSCLK clock frequency (MHz) |
  166. |---------------|-------------------------------|
  167. |0WS(1CPU cycle)| 0 < SYSCLK <= 24 |
  168. |---------------|-------------------------------|
  169. |1WS(2CPU cycle)| 24 < SYSCLK <= 48 |
  170. |---------------|-------------------------------|
  171. |2WS(3CPU cycle)| 48 < SYSCLK <= 72 |
  172. +-----------------------------------------------+
  173. */
  174. /**
  175. * @brief Resets the RCC clock configuration to the default reset state.
  176. * @note The default reset state of the clock configuration is given below:
  177. * - HSI ON and used as system clock source
  178. * - HSE and PLL OFF
  179. * - AHB, APB1 and APB2 prescaler set to 1.
  180. * - CSS and MCO1 OFF
  181. * - All interrupts disabled
  182. * @note This function does not modify the configuration of the
  183. * - Peripheral clocks
  184. * - LSI, LSE and RTC clocks
  185. * @retval HAL status
  186. */
  187. HAL_StatusTypeDef HAL_RCC_DeInit(void)
  188. {
  189. uint32_t tickstart = 0;
  190. /* Set HSION bit */
  191. SET_BIT(RCC->CR, RCC_CR_HSION);
  192. /* Insure HSIRDY bit is set before writing default HSITRIM value */
  193. /* Get start tick */
  194. tickstart = HAL_GetTick();
  195. /* Wait till HSI is ready */
  196. while(READ_BIT(RCC->CR, RCC_CR_HSIRDY) == RESET)
  197. {
  198. if((HAL_GetTick() - tickstart) > HSI_TIMEOUT_VALUE)
  199. {
  200. return HAL_TIMEOUT;
  201. }
  202. }
  203. /* Set HSITRIM default value */
  204. MODIFY_REG(RCC->CR, RCC_CR_HSITRIM, RCC_CR_HSITRIM_4);
  205. /* Reset SW[1:0], HPRE[3:0], PPRE1[2:0], PPRE2[2:0] and MCOSEL[2:0] bits */
  206. CLEAR_BIT(RCC->CFGR, RCC_CFGR_SW | RCC_CFGR_HPRE | RCC_CFGR_PPRE1 | RCC_CFGR_PPRE2 | RCC_CFGR_MCO);
  207. /* Insure HSI selected as system clock source */
  208. /* Get start tick */
  209. tickstart = HAL_GetTick();
  210. /* Wait till system clock source is ready */
  211. while(READ_BIT(RCC->CFGR, RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI)
  212. {
  213. if((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
  214. {
  215. return HAL_TIMEOUT;
  216. }
  217. }
  218. /* Update the SystemCoreClock global variable for HSI as system clock source */
  219. SystemCoreClock = HSI_VALUE;
  220. /* Configure the source of time base considering new system clock settings */
  221. if(HAL_InitTick(uwTickPrio) != HAL_OK)
  222. {
  223. return HAL_ERROR;
  224. }
  225. /* Reset HSEON, CSSON, PLLON bits */
  226. CLEAR_BIT(RCC->CR, RCC_CR_PLLON | RCC_CR_CSSON | RCC_CR_HSEON);
  227. /* Reset HSEBYP bit */
  228. CLEAR_BIT(RCC->CR, RCC_CR_HSEBYP);
  229. /* Insure PLLRDY is reset */
  230. /* Get start tick */
  231. tickstart = HAL_GetTick();
  232. while(READ_BIT(RCC->CR, RCC_CR_PLLRDY) != 0U)
  233. {
  234. if((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  235. {
  236. return HAL_TIMEOUT;
  237. }
  238. }
  239. /* Reset CFGR register */
  240. CLEAR_REG(RCC->CFGR);
  241. /* Reset CFGR2 register */
  242. CLEAR_REG(RCC->CFGR2);
  243. /* Reset CFGR3 register */
  244. CLEAR_REG(RCC->CFGR3);
  245. /* Clear all interrupt flags */
  246. SET_BIT(RCC->CIR, RCC_CIR_LSIRDYC | RCC_CIR_LSERDYC | RCC_CIR_HSIRDYC | RCC_CIR_HSERDYC | RCC_CIR_PLLRDYC | RCC_CIR_CSSC);
  247. /* Disable all interrupts */
  248. CLEAR_REG(RCC->CIR);
  249. /* Reset all CSR flags */
  250. __HAL_RCC_CLEAR_RESET_FLAGS();
  251. return HAL_OK;
  252. }
  253. /**
  254. * @brief Initializes the RCC Oscillators according to the specified parameters in the
  255. * RCC_OscInitTypeDef.
  256. * @param RCC_OscInitStruct pointer to an RCC_OscInitTypeDef structure that
  257. * contains the configuration information for the RCC Oscillators.
  258. * @note The PLL is not disabled when used as system clock.
  259. * @note Transitions LSE Bypass to LSE On and LSE On to LSE Bypass are not
  260. * supported by this macro. User should request a transition to LSE Off
  261. * first and then LSE On or LSE Bypass.
  262. * @note Transition HSE Bypass to HSE On and HSE On to HSE Bypass are not
  263. * supported by this macro. User should request a transition to HSE Off
  264. * first and then HSE On or HSE Bypass.
  265. * @retval HAL status
  266. */
  267. HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  268. {
  269. uint32_t tickstart;
  270. uint32_t pll_config;
  271. #if defined(RCC_CFGR_PLLSRC_HSI_PREDIV)
  272. uint32_t pll_config2;
  273. #endif /* RCC_CFGR_PLLSRC_HSI_PREDIV */
  274. /* Check Null pointer */
  275. if(RCC_OscInitStruct == NULL)
  276. {
  277. return HAL_ERROR;
  278. }
  279. /* Check the parameters */
  280. assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct->OscillatorType));
  281. /*------------------------------- HSE Configuration ------------------------*/
  282. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE)
  283. {
  284. /* Check the parameters */
  285. assert_param(IS_RCC_HSE(RCC_OscInitStruct->HSEState));
  286. /* When the HSE is used as system clock or clock source for PLL in these cases it is not allowed to be disabled */
  287. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_HSE)
  288. || ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && (__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_HSE)))
  289. {
  290. if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET) && (RCC_OscInitStruct->HSEState == RCC_HSE_OFF))
  291. {
  292. return HAL_ERROR;
  293. }
  294. }
  295. else
  296. {
  297. /* Set the new HSE configuration ---------------------------------------*/
  298. __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct->HSEState);
  299. #if defined(RCC_CFGR_PLLSRC_HSI_DIV2)
  300. /* Configure the HSE predivision factor --------------------------------*/
  301. __HAL_RCC_HSE_PREDIV_CONFIG(RCC_OscInitStruct->HSEPredivValue);
  302. #endif /* RCC_CFGR_PLLSRC_HSI_DIV2 */
  303. /* Check the HSE State */
  304. if(RCC_OscInitStruct->HSEState != RCC_HSE_OFF)
  305. {
  306. /* Get Start Tick */
  307. tickstart = HAL_GetTick();
  308. /* Wait till HSE is ready */
  309. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)
  310. {
  311. if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
  312. {
  313. return HAL_TIMEOUT;
  314. }
  315. }
  316. }
  317. else
  318. {
  319. /* Get Start Tick */
  320. tickstart = HAL_GetTick();
  321. /* Wait till HSE is disabled */
  322. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET)
  323. {
  324. if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
  325. {
  326. return HAL_TIMEOUT;
  327. }
  328. }
  329. }
  330. }
  331. }
  332. /*----------------------------- HSI Configuration --------------------------*/
  333. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI)
  334. {
  335. /* Check the parameters */
  336. assert_param(IS_RCC_HSI(RCC_OscInitStruct->HSIState));
  337. assert_param(IS_RCC_CALIBRATION_VALUE(RCC_OscInitStruct->HSICalibrationValue));
  338. /* Check if HSI is used as system clock or as PLL source when PLL is selected as system clock */
  339. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_HSI)
  340. || ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && (__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_HSI)))
  341. {
  342. /* When HSI is used as system clock it will not disabled */
  343. if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET) && (RCC_OscInitStruct->HSIState != RCC_HSI_ON))
  344. {
  345. return HAL_ERROR;
  346. }
  347. /* Otherwise, just the calibration is allowed */
  348. else
  349. {
  350. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  351. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  352. }
  353. }
  354. else
  355. {
  356. /* Check the HSI State */
  357. if(RCC_OscInitStruct->HSIState != RCC_HSI_OFF)
  358. {
  359. /* Enable the Internal High Speed oscillator (HSI). */
  360. __HAL_RCC_HSI_ENABLE();
  361. /* Get Start Tick */
  362. tickstart = HAL_GetTick();
  363. /* Wait till HSI is ready */
  364. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET)
  365. {
  366. if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
  367. {
  368. return HAL_TIMEOUT;
  369. }
  370. }
  371. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  372. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  373. }
  374. else
  375. {
  376. /* Disable the Internal High Speed oscillator (HSI). */
  377. __HAL_RCC_HSI_DISABLE();
  378. /* Get Start Tick */
  379. tickstart = HAL_GetTick();
  380. /* Wait till HSI is disabled */
  381. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET)
  382. {
  383. if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
  384. {
  385. return HAL_TIMEOUT;
  386. }
  387. }
  388. }
  389. }
  390. }
  391. /*------------------------------ LSI Configuration -------------------------*/
  392. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI)
  393. {
  394. /* Check the parameters */
  395. assert_param(IS_RCC_LSI(RCC_OscInitStruct->LSIState));
  396. /* Check the LSI State */
  397. if(RCC_OscInitStruct->LSIState != RCC_LSI_OFF)
  398. {
  399. /* Enable the Internal Low Speed oscillator (LSI). */
  400. __HAL_RCC_LSI_ENABLE();
  401. /* Get Start Tick */
  402. tickstart = HAL_GetTick();
  403. /* Wait till LSI is ready */
  404. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) == RESET)
  405. {
  406. if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
  407. {
  408. return HAL_TIMEOUT;
  409. }
  410. }
  411. }
  412. else
  413. {
  414. /* Disable the Internal Low Speed oscillator (LSI). */
  415. __HAL_RCC_LSI_DISABLE();
  416. /* Get Start Tick */
  417. tickstart = HAL_GetTick();
  418. /* Wait till LSI is disabled */
  419. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) != RESET)
  420. {
  421. if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
  422. {
  423. return HAL_TIMEOUT;
  424. }
  425. }
  426. }
  427. }
  428. /*------------------------------ LSE Configuration -------------------------*/
  429. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE)
  430. {
  431. FlagStatus pwrclkchanged = RESET;
  432. /* Check the parameters */
  433. assert_param(IS_RCC_LSE(RCC_OscInitStruct->LSEState));
  434. /* Update LSE configuration in Backup Domain control register */
  435. /* Requires to enable write access to Backup Domain of necessary */
  436. if(__HAL_RCC_PWR_IS_CLK_DISABLED())
  437. {
  438. __HAL_RCC_PWR_CLK_ENABLE();
  439. pwrclkchanged = SET;
  440. }
  441. if(HAL_IS_BIT_CLR(PWR->CR, PWR_CR_DBP))
  442. {
  443. /* Enable write access to Backup domain */
  444. SET_BIT(PWR->CR, PWR_CR_DBP);
  445. /* Wait for Backup domain Write protection disable */
  446. tickstart = HAL_GetTick();
  447. while(HAL_IS_BIT_CLR(PWR->CR, PWR_CR_DBP))
  448. {
  449. if((HAL_GetTick() - tickstart) > RCC_DBP_TIMEOUT_VALUE)
  450. {
  451. return HAL_TIMEOUT;
  452. }
  453. }
  454. }
  455. /* Set the new LSE configuration -----------------------------------------*/
  456. __HAL_RCC_LSE_CONFIG(RCC_OscInitStruct->LSEState);
  457. /* Check the LSE State */
  458. if(RCC_OscInitStruct->LSEState != RCC_LSE_OFF)
  459. {
  460. /* Get Start Tick */
  461. tickstart = HAL_GetTick();
  462. /* Wait till LSE is ready */
  463. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
  464. {
  465. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  466. {
  467. return HAL_TIMEOUT;
  468. }
  469. }
  470. }
  471. else
  472. {
  473. /* Get Start Tick */
  474. tickstart = HAL_GetTick();
  475. /* Wait till LSE is disabled */
  476. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) != RESET)
  477. {
  478. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  479. {
  480. return HAL_TIMEOUT;
  481. }
  482. }
  483. }
  484. /* Require to disable power clock if necessary */
  485. if(pwrclkchanged == SET)
  486. {
  487. __HAL_RCC_PWR_CLK_DISABLE();
  488. }
  489. }
  490. /*-------------------------------- PLL Configuration -----------------------*/
  491. /* Check the parameters */
  492. assert_param(IS_RCC_PLL(RCC_OscInitStruct->PLL.PLLState));
  493. if ((RCC_OscInitStruct->PLL.PLLState) != RCC_PLL_NONE)
  494. {
  495. /* Check if the PLL is used as system clock or not */
  496. if(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_PLLCLK)
  497. {
  498. if((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_ON)
  499. {
  500. /* Check the parameters */
  501. assert_param(IS_RCC_PLLSOURCE(RCC_OscInitStruct->PLL.PLLSource));
  502. assert_param(IS_RCC_PLL_MUL(RCC_OscInitStruct->PLL.PLLMUL));
  503. #if defined(RCC_CFGR_PLLSRC_HSI_PREDIV)
  504. assert_param(IS_RCC_PREDIV(RCC_OscInitStruct->PLL.PREDIV));
  505. #endif
  506. /* Disable the main PLL. */
  507. __HAL_RCC_PLL_DISABLE();
  508. /* Get Start Tick */
  509. tickstart = HAL_GetTick();
  510. /* Wait till PLL is disabled */
  511. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
  512. {
  513. if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  514. {
  515. return HAL_TIMEOUT;
  516. }
  517. }
  518. #if defined(RCC_CFGR_PLLSRC_HSI_PREDIV)
  519. /* Configure the main PLL clock source, predivider and multiplication factor. */
  520. __HAL_RCC_PLL_CONFIG(RCC_OscInitStruct->PLL.PLLSource,
  521. RCC_OscInitStruct->PLL.PREDIV,
  522. RCC_OscInitStruct->PLL.PLLMUL);
  523. #else
  524. /* Configure the main PLL clock source and multiplication factor. */
  525. __HAL_RCC_PLL_CONFIG(RCC_OscInitStruct->PLL.PLLSource,
  526. RCC_OscInitStruct->PLL.PLLMUL);
  527. #endif /* RCC_CFGR_PLLSRC_HSI_PREDIV */
  528. /* Enable the main PLL. */
  529. __HAL_RCC_PLL_ENABLE();
  530. /* Get Start Tick */
  531. tickstart = HAL_GetTick();
  532. /* Wait till PLL is ready */
  533. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
  534. {
  535. if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  536. {
  537. return HAL_TIMEOUT;
  538. }
  539. }
  540. }
  541. else
  542. {
  543. /* Disable the main PLL. */
  544. __HAL_RCC_PLL_DISABLE();
  545. /* Get Start Tick */
  546. tickstart = HAL_GetTick();
  547. /* Wait till PLL is disabled */
  548. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
  549. {
  550. if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  551. {
  552. return HAL_TIMEOUT;
  553. }
  554. }
  555. }
  556. }
  557. else
  558. {
  559. /* Check if there is a request to disable the PLL used as System clock source */
  560. if((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_OFF)
  561. {
  562. return HAL_ERROR;
  563. }
  564. else
  565. {
  566. /* Do not return HAL_ERROR if request repeats the current configuration */
  567. pll_config = RCC->CFGR;
  568. #if defined(RCC_CFGR_PLLSRC_HSI_PREDIV)
  569. pll_config2 = RCC->CFGR2;
  570. if((READ_BIT(pll_config, RCC_CFGR_PLLSRC) != RCC_OscInitStruct->PLL.PLLSource) ||
  571. (READ_BIT(pll_config, RCC_CFGR_PLLMUL) != RCC_OscInitStruct->PLL.PLLMUL) ||
  572. (READ_BIT(pll_config2, RCC_CFGR2_PREDIV) != RCC_OscInitStruct->PLL.PREDIV))
  573. #else
  574. if((READ_BIT(pll_config, RCC_CFGR_PLLSRC) != RCC_OscInitStruct->PLL.PLLSource) ||
  575. (READ_BIT(pll_config, RCC_CFGR_PLLMUL) != RCC_OscInitStruct->PLL.PLLMUL))
  576. #endif
  577. {
  578. return HAL_ERROR;
  579. }
  580. }
  581. }
  582. }
  583. return HAL_OK;
  584. }
  585. /**
  586. * @brief Initializes the CPU, AHB and APB buses clocks according to the specified
  587. * parameters in the RCC_ClkInitStruct.
  588. * @param RCC_ClkInitStruct pointer to an RCC_OscInitTypeDef structure that
  589. * contains the configuration information for the RCC peripheral.
  590. * @param FLatency FLASH Latency
  591. * The value of this parameter depend on device used within the same series
  592. * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency
  593. * and updated by @ref HAL_RCC_GetHCLKFreq() function called within this function
  594. *
  595. * @note The HSI is used (enabled by hardware) as system clock source after
  596. * start-up from Reset, wake-up from STOP and STANDBY mode, or in case
  597. * of failure of the HSE used directly or indirectly as system clock
  598. * (if the Clock Security System CSS is enabled).
  599. *
  600. * @note A switch from one clock source to another occurs only if the target
  601. * clock source is ready (clock stable after start-up delay or PLL locked).
  602. * If a clock source which is not yet ready is selected, the switch will
  603. * occur when the clock source will be ready.
  604. * You can use @ref HAL_RCC_GetClockConfig() function to know which clock is
  605. * currently used as system clock source.
  606. * @retval HAL status
  607. */
  608. HAL_StatusTypeDef HAL_RCC_ClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t FLatency)
  609. {
  610. uint32_t tickstart = 0U;
  611. /* Check Null pointer */
  612. if(RCC_ClkInitStruct == NULL)
  613. {
  614. return HAL_ERROR;
  615. }
  616. /* Check the parameters */
  617. assert_param(IS_RCC_CLOCKTYPE(RCC_ClkInitStruct->ClockType));
  618. assert_param(IS_FLASH_LATENCY(FLatency));
  619. /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
  620. must be correctly programmed according to the frequency of the CPU clock
  621. (HCLK) of the device. */
  622. /* Increasing the number of wait states because of higher CPU frequency */
  623. if(FLatency > __HAL_FLASH_GET_LATENCY())
  624. {
  625. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  626. __HAL_FLASH_SET_LATENCY(FLatency);
  627. /* Check that the new number of wait states is taken into account to access the Flash
  628. memory by reading the FLASH_ACR register */
  629. if(__HAL_FLASH_GET_LATENCY() != FLatency)
  630. {
  631. return HAL_ERROR;
  632. }
  633. }
  634. /*-------------------------- HCLK Configuration --------------------------*/
  635. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)
  636. {
  637. assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
  638. MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
  639. }
  640. /*------------------------- SYSCLK Configuration ---------------------------*/
  641. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_SYSCLK) == RCC_CLOCKTYPE_SYSCLK)
  642. {
  643. assert_param(IS_RCC_SYSCLKSOURCE(RCC_ClkInitStruct->SYSCLKSource));
  644. /* HSE is selected as System Clock Source */
  645. if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
  646. {
  647. /* Check the HSE ready flag */
  648. if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)
  649. {
  650. return HAL_ERROR;
  651. }
  652. }
  653. /* PLL is selected as System Clock Source */
  654. else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
  655. {
  656. /* Check the PLL ready flag */
  657. if(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
  658. {
  659. return HAL_ERROR;
  660. }
  661. }
  662. /* HSI is selected as System Clock Source */
  663. else
  664. {
  665. /* Check the HSI ready flag */
  666. if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET)
  667. {
  668. return HAL_ERROR;
  669. }
  670. }
  671. __HAL_RCC_SYSCLK_CONFIG(RCC_ClkInitStruct->SYSCLKSource);
  672. /* Get Start Tick */
  673. tickstart = HAL_GetTick();
  674. while (__HAL_RCC_GET_SYSCLK_SOURCE() != (RCC_ClkInitStruct->SYSCLKSource << RCC_CFGR_SWS_Pos))
  675. {
  676. if ((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
  677. {
  678. return HAL_TIMEOUT;
  679. }
  680. }
  681. }
  682. /* Decreasing the number of wait states because of lower CPU frequency */
  683. if(FLatency < __HAL_FLASH_GET_LATENCY())
  684. {
  685. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  686. __HAL_FLASH_SET_LATENCY(FLatency);
  687. /* Check that the new number of wait states is taken into account to access the Flash
  688. memory by reading the FLASH_ACR register */
  689. if(__HAL_FLASH_GET_LATENCY() != FLatency)
  690. {
  691. return HAL_ERROR;
  692. }
  693. }
  694. /*-------------------------- PCLK1 Configuration ---------------------------*/
  695. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
  696. {
  697. assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB1CLKDivider));
  698. MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE1, RCC_ClkInitStruct->APB1CLKDivider);
  699. }
  700. /*-------------------------- PCLK2 Configuration ---------------------------*/
  701. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK2) == RCC_CLOCKTYPE_PCLK2)
  702. {
  703. assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB2CLKDivider));
  704. MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE2, ((RCC_ClkInitStruct->APB2CLKDivider) << 3U));
  705. }
  706. /* Update the SystemCoreClock global variable */
  707. SystemCoreClock = HAL_RCC_GetSysClockFreq() >> AHBPrescTable[(RCC->CFGR & RCC_CFGR_HPRE)>> RCC_CFGR_HPRE_BITNUMBER];
  708. /* Configure the source of time base considering new system clocks settings*/
  709. HAL_InitTick (uwTickPrio);
  710. return HAL_OK;
  711. }
  712. /**
  713. * @}
  714. */
  715. /** @defgroup RCC_Exported_Functions_Group2 Peripheral Control functions
  716. * @brief RCC clocks control functions
  717. *
  718. @verbatim
  719. ===============================================================================
  720. ##### Peripheral Control functions #####
  721. ===============================================================================
  722. [..]
  723. This subsection provides a set of functions allowing to control the RCC Clocks
  724. frequencies.
  725. @endverbatim
  726. * @{
  727. */
  728. #if defined(RCC_CFGR_MCOPRE)
  729. /**
  730. * @brief Selects the clock source to output on MCO pin.
  731. * @note MCO pin should be configured in alternate function mode.
  732. * @param RCC_MCOx specifies the output direction for the clock source.
  733. * This parameter can be one of the following values:
  734. * @arg @ref RCC_MCO1 Clock source to output on MCO1 pin(PA8).
  735. * @param RCC_MCOSource specifies the clock source to output.
  736. * This parameter can be one of the following values:
  737. * @arg @ref RCC_MCO1SOURCE_NOCLOCK No clock selected
  738. * @arg @ref RCC_MCO1SOURCE_SYSCLK System Clock selected as MCO clock
  739. * @arg @ref RCC_MCO1SOURCE_HSI HSI selected as MCO clock
  740. * @arg @ref RCC_MCO1SOURCE_HSE HSE selected as MCO clock
  741. * @arg @ref RCC_MCO1SOURCE_LSI LSI selected as MCO clock
  742. * @arg @ref RCC_MCO1SOURCE_LSE LSE selected as MCO clock
  743. * @arg @ref RCC_MCO1SOURCE_PLLCLK PLLCLK selected as MCO clock
  744. * @arg @ref RCC_MCO1SOURCE_PLLCLK_DIV2 PLLCLK Divided by 2 selected as MCO clock
  745. * @param RCC_MCODiv specifies the MCO DIV.
  746. * This parameter can be one of the following values:
  747. * @arg @ref RCC_MCODIV_1 no division applied to MCO clock
  748. * @arg @ref RCC_MCODIV_2 division by 2 applied to MCO clock
  749. * @arg @ref RCC_MCODIV_4 division by 4 applied to MCO clock
  750. * @arg @ref RCC_MCODIV_8 division by 8 applied to MCO clock
  751. * @arg @ref RCC_MCODIV_16 division by 16 applied to MCO clock
  752. * @arg @ref RCC_MCODIV_32 division by 32 applied to MCO clock
  753. * @arg @ref RCC_MCODIV_64 division by 64 applied to MCO clock
  754. * @arg @ref RCC_MCODIV_128 division by 128 applied to MCO clock
  755. * @retval None
  756. */
  757. #else
  758. /**
  759. * @brief Selects the clock source to output on MCO pin.
  760. * @note MCO pin should be configured in alternate function mode.
  761. * @param RCC_MCOx specifies the output direction for the clock source.
  762. * This parameter can be one of the following values:
  763. * @arg @ref RCC_MCO1 Clock source to output on MCO1 pin(PA8).
  764. * @param RCC_MCOSource specifies the clock source to output.
  765. * This parameter can be one of the following values:
  766. * @arg @ref RCC_MCO1SOURCE_NOCLOCK No clock selected as MCO clock
  767. * @arg @ref RCC_MCO1SOURCE_SYSCLK System clock selected as MCO clock
  768. * @arg @ref RCC_MCO1SOURCE_HSI HSI selected as MCO clock
  769. * @arg @ref RCC_MCO1SOURCE_HSE HSE selected as MCO clock
  770. * @arg @ref RCC_MCO1SOURCE_LSI LSI selected as MCO clock
  771. * @arg @ref RCC_MCO1SOURCE_LSE LSE selected as MCO clock
  772. * @arg @ref RCC_MCO1SOURCE_PLLCLK_DIV2 PLLCLK Divided by 2 selected as MCO clock
  773. * @param RCC_MCODiv specifies the MCO DIV.
  774. * This parameter can be one of the following values:
  775. * @arg @ref RCC_MCODIV_1 no division applied to MCO clock
  776. * @retval None
  777. */
  778. #endif
  779. void HAL_RCC_MCOConfig(uint32_t RCC_MCOx, uint32_t RCC_MCOSource, uint32_t RCC_MCODiv)
  780. {
  781. GPIO_InitTypeDef gpio;
  782. /* Check the parameters */
  783. assert_param(IS_RCC_MCO(RCC_MCOx));
  784. assert_param(IS_RCC_MCODIV(RCC_MCODiv));
  785. assert_param(IS_RCC_MCO1SOURCE(RCC_MCOSource));
  786. /* Prevent unused argument(s) compilation warning */
  787. UNUSED(RCC_MCOx);
  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) >> RCC_CFGR_PLLMUL_Pos];
  866. prediv = aPredivFactorTable[(uint32_t)(RCC->CFGR2 & RCC_CFGR2_PREDIV) >> RCC_CFGR2_PREDIV_Pos];
  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) >> RCC_CR_HSITRIM_Pos);
  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. */