stm32f1xx_hal_rcc.c 48 KB

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