stm32f0xx_hal_rcc.c 50 KB

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