stm32f0xx_hal_rcc.c 50 KB

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