stm32f0xx_hal_rcc.c 49 KB

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