stm32f1xx_hal_rcc.c 48 KB

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