stm32g4xx_hal_rcc.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
  1. /**
  2. ******************************************************************************
  3. * @file stm32g4xx_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 High Speed Internal oscillator
  17. (16 MHz) with Flash 0 wait state. Flash prefetch buffer, D-Cache
  18. and I-Cache are disabled, and all peripherals are off except internal
  19. SRAM, Flash and JTAG.
  20. (+) There is no prescaler on High speed (AHBs) and Low speed (APBs) buses:
  21. all peripherals mapped on these buses are running at HSI speed.
  22. (+) The clock for all peripherals is switched off, except the SRAM and FLASH.
  23. (+) All GPIOs are in analog mode, except the JTAG pins which
  24. are assigned to be used for debug purpose.
  25. [..]
  26. Once the device started from reset, the user application has to:
  27. (+) Configure the clock source to be used to drive the System clock
  28. (if the application needs higher frequency/performance)
  29. (+) Configure the System clock frequency and Flash settings
  30. (+) Configure the AHB and APB buses prescalers
  31. (+) Enable the clock for the peripheral(s) to be used
  32. (+) Configure the clock source(s) for peripherals which clocks are not
  33. derived from the System clock (USB, RNG, USART, LPUART, FDCAN, some TIMERs,
  34. UCPD, I2S, I2C, LPTIM, ADC, QSPI)
  35. @endverbatim
  36. ******************************************************************************
  37. * @attention
  38. *
  39. * Copyright (c) 2019 STMicroelectronics.
  40. * All rights reserved.
  41. *
  42. * This software is licensed under terms that can be found in the LICENSE file in
  43. * the root directory of this software component.
  44. * If no LICENSE file comes with this software, it is provided AS-IS.
  45. ******************************************************************************
  46. */
  47. /* Includes ------------------------------------------------------------------*/
  48. #include "stm32g4xx_hal.h"
  49. /** @addtogroup STM32G4xx_HAL_Driver
  50. * @{
  51. */
  52. /** @defgroup RCC RCC
  53. * @brief RCC HAL module driver
  54. * @{
  55. */
  56. #ifdef HAL_RCC_MODULE_ENABLED
  57. /* Private typedef -----------------------------------------------------------*/
  58. /* Private define ------------------------------------------------------------*/
  59. /** @defgroup RCC_Private_Constants RCC Private Constants
  60. * @{
  61. */
  62. #define HSE_TIMEOUT_VALUE HSE_STARTUP_TIMEOUT
  63. #define HSI_TIMEOUT_VALUE 2U /* 2 ms (minimum Tick + 1) */
  64. #define LSI_TIMEOUT_VALUE 2U /* 2 ms (minimum Tick + 1) */
  65. #define HSI48_TIMEOUT_VALUE 2U /* 2 ms (minimum Tick + 1) */
  66. #define PLL_TIMEOUT_VALUE 2U /* 2 ms (minimum Tick + 1) */
  67. #define CLOCKSWITCH_TIMEOUT_VALUE 5000U /* 5 s */
  68. /**
  69. * @}
  70. */
  71. /* Private macro -------------------------------------------------------------*/
  72. /** @defgroup RCC_Private_Macros RCC Private Macros
  73. * @{
  74. */
  75. #define RCC_GET_MCO_GPIO_PIN(__RCC_MCOx__) ((__RCC_MCOx__) & GPIO_PIN_MASK)
  76. #define RCC_GET_MCO_GPIO_AF(__RCC_MCOx__) (((__RCC_MCOx__) & RCC_MCO_GPIOAF_MASK) >> RCC_MCO_GPIOAF_POS)
  77. #define RCC_GET_MCO_GPIO_INDEX(__RCC_MCOx__) (((__RCC_MCOx__) & RCC_MCO_GPIOPORT_MASK) >> RCC_MCO_GPIOPORT_POS)
  78. #define RCC_GET_MCO_GPIO_PORT(__RCC_MCOx__) (AHB2PERIPH_BASE + ((0x00000400UL) * RCC_GET_MCO_GPIO_INDEX(__RCC_MCOx__)))
  79. #define RCC_PLL_OSCSOURCE_CONFIG(__HAL_RCC_PLLSOURCE__) \
  80. (MODIFY_REG(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC, (__HAL_RCC_PLLSOURCE__)))
  81. /**
  82. * @}
  83. */
  84. /* Private variables ---------------------------------------------------------*/
  85. /* Private function prototypes -----------------------------------------------*/
  86. /** @defgroup RCC_Private_Functions RCC Private Functions
  87. * @{
  88. */
  89. static uint32_t RCC_GetSysClockFreqFromPLLSource(void);
  90. /**
  91. * @}
  92. */
  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 and 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): 16 MHz factory-trimmed RC used directly or through
  110. the PLL as System clock source.
  111. (+) LSI (low-speed internal): 32 KHz low consumption RC used as IWDG and/or RTC
  112. clock source.
  113. (+) HSE (high-speed external): 4 to 48 MHz crystal oscillator used directly or
  114. through the PLL as System clock source. Can be used also optionally as RTC clock source.
  115. (+) LSE (low-speed external): 32.768 KHz oscillator used optionally as RTC clock source.
  116. (+) PLL (clocked by HSI, HSE) providing up to three independent output clocks:
  117. (++) The first output is used to generate the high speed system clock (up to 170 MHz).
  118. (++) The second output is used to generate the clock for the USB (48 MHz),
  119. the QSPI (<= 48 MHz), the FDCAN, the SAI and the I2S.
  120. (++) The third output is used to generate a clock for ADC
  121. (+) CSS (Clock security system): once enabled, if a HSE clock failure occurs
  122. (HSE used directly or through PLL as System clock source), the System clock
  123. is automatically switched to HSI and an interrupt is generated if enabled.
  124. The interrupt is linked to the Cortex-M4 NMI (Non-Maskable Interrupt)
  125. exception vector.
  126. (+) MCO (microcontroller clock output): used to output LSI, HSI, LSE, HSE,
  127. main PLL clock, system clock or RC48 clock (through a configurable prescaler) on PA8 pin.
  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 main 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. "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: the RTC clock can be derived either from the LSI, LSE or HSE clock
  139. divided by 2 to 31.
  140. You have to use __HAL_RCC_RTC_ENABLE() and HAL_RCCEx_PeriphCLKConfig() function
  141. to configure this clock.
  142. (+@) USB FS and RNG: USB FS requires a frequency equal to 48 MHz
  143. to work correctly, while the RNG peripheral requires a frequency
  144. equal or lower than to 48 MHz. This clock is derived of the main PLL
  145. through PLLQ divider. You have to enable the peripheral clock and use
  146. HAL_RCCEx_PeriphCLKConfig() function to configure this clock.
  147. (+@) IWDG clock which is always the LSI clock.
  148. (+) The maximum frequency of the SYSCLK, HCLK, PCLK1 and PCLK2 is 170 MHz.
  149. The clock source frequency should be adapted depending on the device voltage range
  150. as listed in the Reference Manual "Clock source frequency versus voltage scaling" chapter.
  151. @endverbatim
  152. Table 1. HCLK clock frequency for STM32G4xx devices
  153. +----------------------------------------------------------------------------+
  154. | Latency | HCLK clock frequency (MHz) |
  155. | |----------------------------------------------------------|
  156. | | voltage range 1 | voltage range 1 | voltage range 2 |
  157. | | boost mode 1.28 V | normal mode 1.2 V | 1.0 V |
  158. |-----------------|-------------------|-------------------|------------------|
  159. |0WS(1 CPU cycles)| HCLK <= 34 | HCLK <= 30 | HCLK <= 13 |
  160. |-----------------|-------------------|-------------------|------------------|
  161. |1WS(2 CPU cycles)| HCLK <= 68 | HCLK <= 60 | HCLK <= 26 |
  162. |-----------------|-------------------|-------------------|------------------|
  163. |2WS(3 CPU cycles)| HCLK <= 102 | HCLK <= 90 | - |
  164. |-----------------|-------------------|-------------------|------------------|
  165. |3WS(4 CPU cycles)| HCLK <= 136 | HCLK <= 120 | - |
  166. |-----------------|-------------------|-------------------|------------------|
  167. |4WS(5 CPU cycles)| HCLK <= 170 | HCLK <= 150 | - |
  168. +----------------------------------------------------------------------------+
  169. * @{
  170. */
  171. /**
  172. * @brief Reset the RCC clock configuration to the default reset state.
  173. * @note The default reset state of the clock configuration is given below:
  174. * - HSI ON and used as system clock source
  175. * - HSE, PLL OFF
  176. * - AHB, APB1 and APB2 prescaler set to 1.
  177. * - CSS, MCO1 OFF
  178. * - All interrupts disabled
  179. * - All interrupt and reset flags cleared
  180. * @note This function doesn't modify the configuration of the
  181. * - Peripheral clocks
  182. * - LSI, LSE and RTC clocks
  183. * @retval HAL status
  184. */
  185. HAL_StatusTypeDef HAL_RCC_DeInit(void)
  186. {
  187. uint32_t tickstart;
  188. /* Get Start Tick*/
  189. tickstart = HAL_GetTick();
  190. /* Set HSION bit to the reset value */
  191. SET_BIT(RCC->CR, RCC_CR_HSION);
  192. /* Wait till HSI is ready */
  193. while (READ_BIT(RCC->CR, RCC_CR_HSIRDY) == 0U)
  194. {
  195. if ((HAL_GetTick() - tickstart) > HSI_TIMEOUT_VALUE)
  196. {
  197. return HAL_TIMEOUT;
  198. }
  199. }
  200. /* Set HSITRIM[6:0] bits to the reset value */
  201. SET_BIT(RCC->ICSCR, RCC_HSICALIBRATION_DEFAULT << RCC_ICSCR_HSITRIM_Pos);
  202. /* Get Start Tick*/
  203. tickstart = HAL_GetTick();
  204. /* Reset CFGR register (HSI is selected as system clock source) */
  205. RCC->CFGR = 0x00000001u;
  206. /* Wait till HSI is ready */
  207. while (READ_BIT(RCC->CFGR, RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI)
  208. {
  209. if ((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
  210. {
  211. return HAL_TIMEOUT;
  212. }
  213. }
  214. /* Update the SystemCoreClock global variable */
  215. SystemCoreClock = HSI_VALUE;
  216. /* Adapt Systick interrupt period */
  217. if (HAL_InitTick(uwTickPrio) != HAL_OK)
  218. {
  219. return HAL_ERROR;
  220. }
  221. /* Clear CR register in 2 steps: first to clear HSEON in case bypass was enabled */
  222. RCC->CR = RCC_CR_HSION;
  223. /* Then again to HSEBYP in case bypass was enabled */
  224. RCC->CR = RCC_CR_HSION;
  225. /* Get Start Tick*/
  226. tickstart = HAL_GetTick();
  227. /* Wait till PLL is OFF */
  228. while (READ_BIT(RCC->CR, RCC_CR_PLLRDY) != 0U)
  229. {
  230. if ((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  231. {
  232. return HAL_TIMEOUT;
  233. }
  234. }
  235. /* once PLL is OFF, reset PLLCFGR register to default value */
  236. RCC->PLLCFGR = RCC_PLLCFGR_PLLN_4;
  237. /* Disable all interrupts */
  238. CLEAR_REG(RCC->CIER);
  239. /* Clear all interrupt flags */
  240. WRITE_REG(RCC->CICR, 0xFFFFFFFFU);
  241. /* Clear all reset flags */
  242. SET_BIT(RCC->CSR, RCC_CSR_RMVF);
  243. return HAL_OK;
  244. }
  245. /**
  246. * @brief Initialize the RCC Oscillators according to the specified parameters in the
  247. * RCC_OscInitTypeDef.
  248. * @param RCC_OscInitStruct pointer to an RCC_OscInitTypeDef structure that
  249. * contains the configuration information for the RCC Oscillators.
  250. * @note The PLL is not disabled when used as system clock.
  251. * @note Transitions LSE Bypass to LSE On and LSE On to LSE Bypass are not
  252. * supported by this macro. User should request a transition to LSE Off
  253. * first and then LSE On or LSE Bypass.
  254. * @note Transition HSE Bypass to HSE On and HSE On to HSE Bypass are not
  255. * supported by this macro. User should request a transition to HSE Off
  256. * first and then HSE On or HSE Bypass.
  257. * @retval HAL status
  258. */
  259. HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  260. {
  261. uint32_t tickstart;
  262. uint32_t temp_sysclksrc;
  263. uint32_t temp_pllckcfg;
  264. /* Check Null pointer */
  265. if (RCC_OscInitStruct == NULL)
  266. {
  267. return HAL_ERROR;
  268. }
  269. /* Check the parameters */
  270. assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct->OscillatorType));
  271. /*------------------------------- HSE Configuration ------------------------*/
  272. if (((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE)
  273. {
  274. /* Check the parameters */
  275. assert_param(IS_RCC_HSE(RCC_OscInitStruct->HSEState));
  276. temp_sysclksrc = __HAL_RCC_GET_SYSCLK_SOURCE();
  277. temp_pllckcfg = __HAL_RCC_GET_PLL_OSCSOURCE();
  278. /* When the HSE is used as system clock or clock source for PLL in these cases it is not allowed to be disabled */
  279. if (((temp_sysclksrc == RCC_CFGR_SWS_PLL) && (temp_pllckcfg == RCC_PLLSOURCE_HSE)) || (temp_sysclksrc == RCC_CFGR_SWS_HSE))
  280. {
  281. if ((READ_BIT(RCC->CR, RCC_CR_HSERDY) != 0U) && (RCC_OscInitStruct->HSEState == RCC_HSE_OFF))
  282. {
  283. return HAL_ERROR;
  284. }
  285. }
  286. else
  287. {
  288. /* Set the new HSE configuration ---------------------------------------*/
  289. __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct->HSEState);
  290. /* Check the HSE State */
  291. if (RCC_OscInitStruct->HSEState != RCC_HSE_OFF)
  292. {
  293. /* Get Start Tick*/
  294. tickstart = HAL_GetTick();
  295. /* Wait till HSE is ready */
  296. while (READ_BIT(RCC->CR, RCC_CR_HSERDY) == 0U)
  297. {
  298. if ((HAL_GetTick() - tickstart) > HSE_TIMEOUT_VALUE)
  299. {
  300. return HAL_TIMEOUT;
  301. }
  302. }
  303. }
  304. else
  305. {
  306. /* Get Start Tick*/
  307. tickstart = HAL_GetTick();
  308. /* Wait till HSE is disabled */
  309. while (READ_BIT(RCC->CR, RCC_CR_HSERDY) != 0U)
  310. {
  311. if ((HAL_GetTick() - tickstart) > HSE_TIMEOUT_VALUE)
  312. {
  313. return HAL_TIMEOUT;
  314. }
  315. }
  316. }
  317. }
  318. }
  319. /*----------------------------- HSI Configuration --------------------------*/
  320. if (((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI)
  321. {
  322. /* Check the parameters */
  323. assert_param(IS_RCC_HSI(RCC_OscInitStruct->HSIState));
  324. assert_param(IS_RCC_HSI_CALIBRATION_VALUE(RCC_OscInitStruct->HSICalibrationValue));
  325. /* Check if HSI is used as system clock or as PLL source when PLL is selected as system clock */
  326. temp_sysclksrc = __HAL_RCC_GET_SYSCLK_SOURCE();
  327. temp_pllckcfg = __HAL_RCC_GET_PLL_OSCSOURCE();
  328. if (((temp_sysclksrc == RCC_CFGR_SWS_PLL) && (temp_pllckcfg == RCC_PLLSOURCE_HSI)) || (temp_sysclksrc == RCC_CFGR_SWS_HSI))
  329. {
  330. /* When HSI is used as system clock it will not be disabled */
  331. if ((READ_BIT(RCC->CR, RCC_CR_HSIRDY) != 0U) && (RCC_OscInitStruct->HSIState == RCC_HSI_OFF))
  332. {
  333. return HAL_ERROR;
  334. }
  335. /* Otherwise, just the calibration is allowed */
  336. else
  337. {
  338. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  339. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  340. /* Adapt Systick interrupt period */
  341. if (HAL_InitTick(uwTickPrio) != HAL_OK)
  342. {
  343. return HAL_ERROR;
  344. }
  345. }
  346. }
  347. else
  348. {
  349. /* Check the HSI State */
  350. if (RCC_OscInitStruct->HSIState != RCC_HSI_OFF)
  351. {
  352. /* Enable the Internal High Speed oscillator (HSI). */
  353. __HAL_RCC_HSI_ENABLE();
  354. /* Get Start Tick*/
  355. tickstart = HAL_GetTick();
  356. /* Wait till HSI is ready */
  357. while (READ_BIT(RCC->CR, RCC_CR_HSIRDY) == 0U)
  358. {
  359. if ((HAL_GetTick() - tickstart) > HSI_TIMEOUT_VALUE)
  360. {
  361. return HAL_TIMEOUT;
  362. }
  363. }
  364. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  365. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  366. }
  367. else
  368. {
  369. /* Disable the Internal High Speed oscillator (HSI). */
  370. __HAL_RCC_HSI_DISABLE();
  371. /* Get Start Tick*/
  372. tickstart = HAL_GetTick();
  373. /* Wait till HSI is disabled */
  374. while (READ_BIT(RCC->CR, RCC_CR_HSIRDY) != 0U)
  375. {
  376. if ((HAL_GetTick() - tickstart) > HSI_TIMEOUT_VALUE)
  377. {
  378. return HAL_TIMEOUT;
  379. }
  380. }
  381. }
  382. }
  383. }
  384. /*------------------------------ LSI Configuration -------------------------*/
  385. if (((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI)
  386. {
  387. /* Check the parameters */
  388. assert_param(IS_RCC_LSI(RCC_OscInitStruct->LSIState));
  389. /* Check the LSI State */
  390. if(RCC_OscInitStruct->LSIState != RCC_LSI_OFF)
  391. {
  392. /* Enable the Internal Low Speed oscillator (LSI). */
  393. __HAL_RCC_LSI_ENABLE();
  394. /* Get Start Tick*/
  395. tickstart = HAL_GetTick();
  396. /* Wait till LSI is ready */
  397. while (READ_BIT(RCC->CSR, RCC_CSR_LSIRDY) == 0U)
  398. {
  399. if ((HAL_GetTick() - tickstart) > LSI_TIMEOUT_VALUE)
  400. {
  401. return HAL_TIMEOUT;
  402. }
  403. }
  404. }
  405. else
  406. {
  407. /* Disable the Internal Low Speed oscillator (LSI). */
  408. __HAL_RCC_LSI_DISABLE();
  409. /* Get Start Tick*/
  410. tickstart = HAL_GetTick();
  411. /* Wait till LSI is disabled */
  412. while(READ_BIT(RCC->CSR, RCC_CSR_LSIRDY) != 0U)
  413. {
  414. if((HAL_GetTick() - tickstart) > LSI_TIMEOUT_VALUE)
  415. {
  416. return HAL_TIMEOUT;
  417. }
  418. }
  419. }
  420. }
  421. /*------------------------------ LSE Configuration -------------------------*/
  422. if (((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE)
  423. {
  424. FlagStatus pwrclkchanged = RESET;
  425. /* Check the parameters */
  426. assert_param(IS_RCC_LSE(RCC_OscInitStruct->LSEState));
  427. /* Update LSE configuration in Backup Domain control register */
  428. /* Requires to enable write access to Backup Domain if necessary */
  429. if (__HAL_RCC_PWR_IS_CLK_DISABLED() != 0U)
  430. {
  431. __HAL_RCC_PWR_CLK_ENABLE();
  432. pwrclkchanged = SET;
  433. }
  434. if (HAL_IS_BIT_CLR(PWR->CR1, PWR_CR1_DBP))
  435. {
  436. /* Enable write access to Backup domain */
  437. SET_BIT(PWR->CR1, PWR_CR1_DBP);
  438. /* Wait for Backup domain Write protection disable */
  439. tickstart = HAL_GetTick();
  440. while (HAL_IS_BIT_CLR(PWR->CR1, PWR_CR1_DBP))
  441. {
  442. if ((HAL_GetTick() - tickstart) > RCC_DBP_TIMEOUT_VALUE)
  443. {
  444. return HAL_TIMEOUT;
  445. }
  446. }
  447. }
  448. /* Set the new LSE configuration -----------------------------------------*/
  449. __HAL_RCC_LSE_CONFIG(RCC_OscInitStruct->LSEState);
  450. /* Check the LSE State */
  451. if (RCC_OscInitStruct->LSEState != RCC_LSE_OFF)
  452. {
  453. /* Get Start Tick*/
  454. tickstart = HAL_GetTick();
  455. /* Wait till LSE is ready */
  456. while (READ_BIT(RCC->BDCR, RCC_BDCR_LSERDY) == 0U)
  457. {
  458. if((HAL_GetTick() - tickstart) > RCC_LSE_TIMEOUT_VALUE)
  459. {
  460. return HAL_TIMEOUT;
  461. }
  462. }
  463. }
  464. else
  465. {
  466. /* Get Start Tick*/
  467. tickstart = HAL_GetTick();
  468. /* Wait till LSE is disabled */
  469. while (READ_BIT(RCC->BDCR, RCC_BDCR_LSERDY) != 0U)
  470. {
  471. if((HAL_GetTick() - tickstart) > RCC_LSE_TIMEOUT_VALUE)
  472. {
  473. return HAL_TIMEOUT;
  474. }
  475. }
  476. }
  477. /* Restore clock configuration if changed */
  478. if (pwrclkchanged == SET)
  479. {
  480. __HAL_RCC_PWR_CLK_DISABLE();
  481. }
  482. }
  483. /*------------------------------ HSI48 Configuration -----------------------*/
  484. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI48) == RCC_OSCILLATORTYPE_HSI48)
  485. {
  486. /* Check the parameters */
  487. assert_param(IS_RCC_HSI48(RCC_OscInitStruct->HSI48State));
  488. /* Check the HSI48 State */
  489. if(RCC_OscInitStruct->HSI48State != RCC_HSI48_OFF)
  490. {
  491. /* Enable the Internal Low Speed oscillator (HSI48). */
  492. __HAL_RCC_HSI48_ENABLE();
  493. /* Get Start Tick*/
  494. tickstart = HAL_GetTick();
  495. /* Wait till HSI48 is ready */
  496. while(READ_BIT(RCC->CRRCR, RCC_CRRCR_HSI48RDY) == 0U)
  497. {
  498. if((HAL_GetTick() - tickstart) > HSI48_TIMEOUT_VALUE)
  499. {
  500. return HAL_TIMEOUT;
  501. }
  502. }
  503. }
  504. else
  505. {
  506. /* Disable the Internal Low Speed oscillator (HSI48). */
  507. __HAL_RCC_HSI48_DISABLE();
  508. /* Get Start Tick*/
  509. tickstart = HAL_GetTick();
  510. /* Wait till HSI48 is disabled */
  511. while(READ_BIT(RCC->CRRCR, RCC_CRRCR_HSI48RDY) != 0U)
  512. {
  513. if((HAL_GetTick() - tickstart) > HSI48_TIMEOUT_VALUE)
  514. {
  515. return HAL_TIMEOUT;
  516. }
  517. }
  518. }
  519. }
  520. /*-------------------------------- PLL Configuration -----------------------*/
  521. /* Check the parameters */
  522. assert_param(IS_RCC_PLL(RCC_OscInitStruct->PLL.PLLState));
  523. if (RCC_OscInitStruct->PLL.PLLState != RCC_PLL_NONE)
  524. {
  525. /* Check if the PLL is used as system clock or not */
  526. if (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_PLL)
  527. {
  528. if (RCC_OscInitStruct->PLL.PLLState == RCC_PLL_ON)
  529. {
  530. /* Check the parameters */
  531. assert_param(IS_RCC_PLLSOURCE(RCC_OscInitStruct->PLL.PLLSource));
  532. assert_param(IS_RCC_PLLM_VALUE(RCC_OscInitStruct->PLL.PLLM));
  533. assert_param(IS_RCC_PLLN_VALUE(RCC_OscInitStruct->PLL.PLLN));
  534. assert_param(IS_RCC_PLLP_VALUE(RCC_OscInitStruct->PLL.PLLP));
  535. assert_param(IS_RCC_PLLQ_VALUE(RCC_OscInitStruct->PLL.PLLQ));
  536. assert_param(IS_RCC_PLLR_VALUE(RCC_OscInitStruct->PLL.PLLR));
  537. /* Disable the main PLL. */
  538. __HAL_RCC_PLL_DISABLE();
  539. /* Get Start Tick*/
  540. tickstart = HAL_GetTick();
  541. /* Wait till PLL is disabled */
  542. while (READ_BIT(RCC->CR, RCC_CR_PLLRDY) != 0U)
  543. {
  544. if ((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  545. {
  546. return HAL_TIMEOUT;
  547. }
  548. }
  549. /* Configure the main PLL clock source, multiplication and division factors. */
  550. __HAL_RCC_PLL_CONFIG(RCC_OscInitStruct->PLL.PLLSource,
  551. RCC_OscInitStruct->PLL.PLLM,
  552. RCC_OscInitStruct->PLL.PLLN,
  553. RCC_OscInitStruct->PLL.PLLP,
  554. RCC_OscInitStruct->PLL.PLLQ,
  555. RCC_OscInitStruct->PLL.PLLR);
  556. /* Enable the main PLL. */
  557. __HAL_RCC_PLL_ENABLE();
  558. /* Enable PLL System Clock output. */
  559. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL_SYSCLK);
  560. /* Get Start Tick*/
  561. tickstart = HAL_GetTick();
  562. /* Wait till PLL is ready */
  563. while (READ_BIT(RCC->CR, RCC_CR_PLLRDY) == 0U)
  564. {
  565. if ((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  566. {
  567. return HAL_TIMEOUT;
  568. }
  569. }
  570. }
  571. else
  572. {
  573. /* Disable the main PLL. */
  574. __HAL_RCC_PLL_DISABLE();
  575. /* Get Start Tick*/
  576. tickstart = HAL_GetTick();
  577. /* Wait till PLL is disabled */
  578. while (READ_BIT(RCC->CR, RCC_CR_PLLRDY) != 0U)
  579. {
  580. if ((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  581. {
  582. return HAL_TIMEOUT;
  583. }
  584. }
  585. /* Unselect PLL clock source and disable outputs to save power */
  586. RCC->PLLCFGR &= ~(RCC_PLLCFGR_PLLSRC | RCC_PLL_SYSCLK | RCC_PLL_48M1CLK | RCC_PLL_ADCCLK);
  587. }
  588. }
  589. else
  590. {
  591. /* Check if there is a request to disable the PLL used as System clock source */
  592. if((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_OFF)
  593. {
  594. return HAL_ERROR;
  595. }
  596. else
  597. {
  598. /* Do not return HAL_ERROR if request repeats the current configuration */
  599. temp_pllckcfg = RCC->PLLCFGR;
  600. if((READ_BIT(temp_pllckcfg, RCC_PLLCFGR_PLLSRC) != RCC_OscInitStruct->PLL.PLLSource) ||
  601. (READ_BIT(temp_pllckcfg, RCC_PLLCFGR_PLLM) != (((RCC_OscInitStruct->PLL.PLLM) - 1U) << RCC_PLLCFGR_PLLM_Pos)) ||
  602. (READ_BIT(temp_pllckcfg, RCC_PLLCFGR_PLLN) != ((RCC_OscInitStruct->PLL.PLLN) << RCC_PLLCFGR_PLLN_Pos)) ||
  603. (READ_BIT(temp_pllckcfg, RCC_PLLCFGR_PLLPDIV) != ((RCC_OscInitStruct->PLL.PLLP) << RCC_PLLCFGR_PLLPDIV_Pos)) ||
  604. (READ_BIT(temp_pllckcfg, RCC_PLLCFGR_PLLQ) != ((((RCC_OscInitStruct->PLL.PLLQ) >> 1U) - 1U) << RCC_PLLCFGR_PLLQ_Pos)) ||
  605. (READ_BIT(temp_pllckcfg, RCC_PLLCFGR_PLLR) != ((((RCC_OscInitStruct->PLL.PLLR) >> 1U) - 1U) << RCC_PLLCFGR_PLLR_Pos)))
  606. {
  607. return HAL_ERROR;
  608. }
  609. }
  610. }
  611. }
  612. return HAL_OK;
  613. }
  614. /**
  615. * @brief Initialize the CPU, AHB and APB buses clocks according to the specified
  616. * parameters in the RCC_ClkInitStruct.
  617. * @param RCC_ClkInitStruct pointer to an RCC_OscInitTypeDef structure that
  618. * contains the configuration information for the RCC peripheral.
  619. * @param FLatency FLASH Latency
  620. * This parameter can be one of the following values:
  621. * @arg FLASH_LATENCY_0 FLASH 0 Latency cycle
  622. * @arg FLASH_LATENCY_1 FLASH 1 Latency cycle
  623. * @arg FLASH_LATENCY_2 FLASH 2 Latency cycles
  624. * @arg FLASH_LATENCY_3 FLASH 3 Latency cycles
  625. * @arg FLASH_LATENCY_4 FLASH 4 Latency cycles
  626. * @arg FLASH_LATENCY_5 FLASH 5 Latency cycles
  627. * @arg FLASH_LATENCY_6 FLASH 6 Latency cycles
  628. * @arg FLASH_LATENCY_7 FLASH 7 Latency cycles
  629. * @arg FLASH_LATENCY_8 FLASH 8 Latency cycles
  630. * @arg FLASH_LATENCY_9 FLASH 9 Latency cycles
  631. * @arg FLASH_LATENCY_10 FLASH 10 Latency cycles
  632. * @arg FLASH_LATENCY_11 FLASH 11 Latency cycles
  633. * @arg FLASH_LATENCY_12 FLASH 12 Latency cycles
  634. * @arg FLASH_LATENCY_13 FLASH 13 Latency cycles
  635. * @arg FLASH_LATENCY_14 FLASH 14 Latency cycles
  636. * @arg FLASH_LATENCY_15 FLASH 15 Latency cycles
  637. *
  638. * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency
  639. * and updated by HAL_RCC_GetHCLKFreq() function called within this function
  640. *
  641. * @note The HSI is used by default as system clock source after
  642. * startup from Reset, wake-up from STANDBY mode. After restart from Reset,
  643. * the HSI frequency is set to its default value 16 MHz.
  644. *
  645. * @note The HSI can be selected as system clock source after
  646. * from STOP modes or in case of failure of the HSE used directly or indirectly
  647. * as system clock (if the Clock Security System CSS is enabled).
  648. *
  649. * @note A switch from one clock source to another occurs only if the target
  650. * clock source is ready (clock stable after startup delay or PLL locked).
  651. * If a clock source which is not yet ready is selected, the switch will
  652. * occur when the clock source is ready.
  653. *
  654. * @note You can use HAL_RCC_GetClockConfig() function to know which clock is
  655. * currently used as system clock source.
  656. *
  657. * @note Depending on the device voltage range, the software has to set correctly
  658. * HPRE[3:0] bits to ensure that HCLK not exceed the maximum allowed frequency
  659. * (for more details refer to section above "Initialization/de-initialization functions")
  660. * @retval None
  661. */
  662. HAL_StatusTypeDef HAL_RCC_ClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t FLatency)
  663. {
  664. uint32_t tickstart;
  665. uint32_t pllfreq;
  666. uint32_t hpre = RCC_SYSCLK_DIV1;
  667. /* Check Null pointer */
  668. if (RCC_ClkInitStruct == NULL)
  669. {
  670. return HAL_ERROR;
  671. }
  672. /* Check the parameters */
  673. assert_param(IS_RCC_CLOCKTYPE(RCC_ClkInitStruct->ClockType));
  674. assert_param(IS_FLASH_LATENCY(FLatency));
  675. /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
  676. must be correctly programmed according to the frequency of the CPU clock
  677. (HCLK) and the supply voltage of the device. */
  678. /* Increasing the number of wait states because of higher CPU frequency */
  679. if (FLatency > __HAL_FLASH_GET_LATENCY())
  680. {
  681. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  682. __HAL_FLASH_SET_LATENCY(FLatency);
  683. /* Check that the new number of wait states is taken into account to access the Flash
  684. memory by reading the FLASH_ACR register */
  685. if (__HAL_FLASH_GET_LATENCY() != FLatency)
  686. {
  687. return HAL_ERROR;
  688. }
  689. }
  690. /*------------------------- SYSCLK Configuration ---------------------------*/
  691. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_SYSCLK) == RCC_CLOCKTYPE_SYSCLK)
  692. {
  693. assert_param(IS_RCC_SYSCLKSOURCE(RCC_ClkInitStruct->SYSCLKSource));
  694. /* PLL is selected as System Clock Source */
  695. if (RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
  696. {
  697. /* Check the PLL ready flag */
  698. if (READ_BIT(RCC->CR, RCC_CR_PLLRDY) == 0U)
  699. {
  700. return HAL_ERROR;
  701. }
  702. /* Undershoot management when selection PLL as SYSCLK source and frequency above 80Mhz */
  703. /* Compute target PLL output frequency */
  704. pllfreq = RCC_GetSysClockFreqFromPLLSource();
  705. /* Intermediate step with HCLK prescaler 2 necessary before to go over 80Mhz */
  706. if(pllfreq > 80000000U)
  707. {
  708. if (((READ_BIT(RCC->CFGR, RCC_CFGR_HPRE) == RCC_SYSCLK_DIV1)) ||
  709. (((((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK) &&
  710. (RCC_ClkInitStruct->AHBCLKDivider == RCC_SYSCLK_DIV1))))
  711. {
  712. MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_SYSCLK_DIV2);
  713. hpre = RCC_SYSCLK_DIV2;
  714. }
  715. }
  716. }
  717. else
  718. {
  719. /* HSE is selected as System Clock Source */
  720. if (RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
  721. {
  722. /* Check the HSE ready flag */
  723. if(READ_BIT(RCC->CR, RCC_CR_HSERDY) == 0U)
  724. {
  725. return HAL_ERROR;
  726. }
  727. }
  728. /* HSI is selected as System Clock Source */
  729. else
  730. {
  731. /* Check the HSI ready flag */
  732. if(READ_BIT(RCC->CR, RCC_CR_HSIRDY) == 0U)
  733. {
  734. return HAL_ERROR;
  735. }
  736. }
  737. /* Overshoot management when going down from PLL as SYSCLK source and frequency above 80Mhz */
  738. pllfreq = HAL_RCC_GetSysClockFreq();
  739. /* Intermediate step with HCLK prescaler 2 necessary before to go under 80Mhz */
  740. if(pllfreq > 80000000U)
  741. {
  742. MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_SYSCLK_DIV2);
  743. hpre = RCC_SYSCLK_DIV2;
  744. }
  745. }
  746. MODIFY_REG(RCC->CFGR, RCC_CFGR_SW, RCC_ClkInitStruct->SYSCLKSource);
  747. /* Get Start Tick*/
  748. tickstart = HAL_GetTick();
  749. while (__HAL_RCC_GET_SYSCLK_SOURCE() != (RCC_ClkInitStruct->SYSCLKSource << RCC_CFGR_SWS_Pos))
  750. {
  751. if ((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
  752. {
  753. return HAL_TIMEOUT;
  754. }
  755. }
  756. }
  757. /*-------------------------- HCLK Configuration --------------------------*/
  758. if (((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)
  759. {
  760. /* Set the highest APB divider in order to ensure that we do not go through
  761. a non-spec phase whatever we decrease or increase HCLK. */
  762. if (((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
  763. {
  764. MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE1, RCC_HCLK_DIV16);
  765. }
  766. if (((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK2) == RCC_CLOCKTYPE_PCLK2)
  767. {
  768. MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE2, RCC_HCLK_DIV16);
  769. }
  770. /* Set the new HCLK clock divider */
  771. assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
  772. MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
  773. }
  774. else
  775. {
  776. /* Is intermediate HCLK prescaler 2 applied internally, complete with HCLK prescaler 1 */
  777. if(hpre == RCC_SYSCLK_DIV2)
  778. {
  779. MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_SYSCLK_DIV1);
  780. }
  781. }
  782. /* Decreasing the number of wait states because of lower CPU frequency */
  783. if (FLatency < __HAL_FLASH_GET_LATENCY())
  784. {
  785. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  786. __HAL_FLASH_SET_LATENCY(FLatency);
  787. /* Check that the new number of wait states is taken into account to access the Flash
  788. memory by polling the FLASH_ACR register */
  789. tickstart = HAL_GetTick();
  790. while (__HAL_FLASH_GET_LATENCY() != FLatency)
  791. {
  792. if ((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
  793. {
  794. return HAL_TIMEOUT;
  795. }
  796. }
  797. }
  798. /*-------------------------- PCLK1 Configuration ---------------------------*/
  799. if (((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
  800. {
  801. assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB1CLKDivider));
  802. MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE1, RCC_ClkInitStruct->APB1CLKDivider);
  803. }
  804. /*-------------------------- PCLK2 Configuration ---------------------------*/
  805. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK2) == RCC_CLOCKTYPE_PCLK2)
  806. {
  807. assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB2CLKDivider));
  808. MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE2, ((RCC_ClkInitStruct->APB2CLKDivider) << 3U));
  809. }
  810. /* Update the SystemCoreClock global variable */
  811. SystemCoreClock = HAL_RCC_GetSysClockFreq() >> (AHBPrescTable[READ_BIT(RCC->CFGR, RCC_CFGR_HPRE) >> RCC_CFGR_HPRE_Pos] & 0x1FU);
  812. /* Configure the source of time base considering new system clocks settings*/
  813. return HAL_InitTick(uwTickPrio);
  814. }
  815. /**
  816. * @}
  817. */
  818. /** @defgroup RCC_Exported_Functions_Group2 Peripheral Control functions
  819. * @brief RCC clocks control functions
  820. *
  821. @verbatim
  822. ===============================================================================
  823. ##### Peripheral Control functions #####
  824. ===============================================================================
  825. [..]
  826. This subsection provides a set of functions allowing to:
  827. (+) Output clock to MCO pin.
  828. (+) Retrieve current clock frequencies.
  829. (+) Enable the Clock Security System.
  830. @endverbatim
  831. * @{
  832. */
  833. /**
  834. * @brief Select the clock source to output on MCO pin(PA8/PG10).
  835. * @note PA8/PG10 should be configured in alternate function mode.
  836. * @note The default configuration of the GPIOG pin 10 (PG10) is set to reset mode (NRST pin)
  837. * and user shall set the NRST_MODE Bit in the FLASH OPTR register to be able to use it
  838. * as an MCO pin.
  839. * The @ref HAL_FLASHEx_OBProgram() API can be used to configure the NRST_MODE Bit value.
  840. * @param RCC_MCOx specifies the output direction for the clock source.
  841. * For STM32G4xx family this parameter can have only one value:
  842. * @arg @ref RCC_MCO_PA8 Clock source to output on MCO1 pin(PA8).
  843. * @arg @ref RCC_MCO_PG10 Clock source to output on MCO1 pin(PG10).
  844. * @param RCC_MCOSource specifies the clock source to output.
  845. * This parameter can be one of the following values:
  846. * @arg @ref RCC_MCO1SOURCE_NOCLOCK MCO output disabled, no clock on MCO
  847. * @arg @ref RCC_MCO1SOURCE_SYSCLK system clock selected as MCO source
  848. * @arg @ref RCC_MCO1SOURCE_HSI HSI clock selected as MCO source
  849. * @arg @ref RCC_MCO1SOURCE_HSE HSE clock selected as MCO source
  850. * @arg @ref RCC_MCO1SOURCE_PLLCLK main PLL clock selected as MCO source
  851. * @arg @ref RCC_MCO1SOURCE_LSI LSI clock selected as MCO source
  852. * @arg @ref RCC_MCO1SOURCE_LSE LSE clock selected as MCO source
  853. * @arg @ref RCC_MCO1SOURCE_HSI48 HSI48 clock selected as MCO source for devices with HSI48
  854. * @param RCC_MCODiv specifies the MCO prescaler.
  855. * This parameter can be one of the following values:
  856. * @arg @ref RCC_MCODIV_1 no division applied to MCO clock
  857. * @arg @ref RCC_MCODIV_2 division by 2 applied to MCO clock
  858. * @arg @ref RCC_MCODIV_4 division by 4 applied to MCO clock
  859. * @arg @ref RCC_MCODIV_8 division by 8 applied to MCO clock
  860. * @arg @ref RCC_MCODIV_16 division by 16 applied to MCO clock
  861. * @retval None
  862. */
  863. void HAL_RCC_MCOConfig(uint32_t RCC_MCOx, uint32_t RCC_MCOSource, uint32_t RCC_MCODiv)
  864. {
  865. GPIO_InitTypeDef gpio_initstruct;
  866. uint32_t mcoindex;
  867. uint32_t mco_gpio_index;
  868. GPIO_TypeDef * mco_gpio_port;
  869. /* Check the parameters */
  870. assert_param(IS_RCC_MCO(RCC_MCOx));
  871. /* Common GPIO init parameters */
  872. gpio_initstruct.Mode = GPIO_MODE_AF_PP;
  873. gpio_initstruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  874. gpio_initstruct.Pull = GPIO_NOPULL;
  875. /* Get MCOx selection */
  876. mcoindex = RCC_MCOx & RCC_MCO_INDEX_MASK;
  877. /* Get MCOx GPIO Port */
  878. mco_gpio_port = (GPIO_TypeDef *) RCC_GET_MCO_GPIO_PORT(RCC_MCOx);
  879. /* MCOx Clock Enable */
  880. mco_gpio_index = RCC_GET_MCO_GPIO_INDEX(RCC_MCOx);
  881. SET_BIT(RCC->AHB2ENR, (1UL << mco_gpio_index ));
  882. /* Configure the MCOx pin in alternate function mode */
  883. gpio_initstruct.Pin = RCC_GET_MCO_GPIO_PIN(RCC_MCOx);
  884. gpio_initstruct.Alternate = RCC_GET_MCO_GPIO_AF(RCC_MCOx);
  885. HAL_GPIO_Init(mco_gpio_port, &gpio_initstruct);
  886. if (mcoindex == RCC_MCO1_INDEX)
  887. {
  888. assert_param(IS_RCC_MCODIV(RCC_MCODiv));
  889. assert_param(IS_RCC_MCO1SOURCE(RCC_MCOSource));
  890. /* Mask MCOSEL[] and MCOPRE[] bits then set MCO clock source and prescaler */
  891. MODIFY_REG(RCC->CFGR, (RCC_CFGR_MCOSEL | RCC_CFGR_MCOPRE), (RCC_MCOSource | RCC_MCODiv));
  892. }
  893. }
  894. /**
  895. * @brief Return the SYSCLK frequency.
  896. *
  897. * @note The system frequency computed by this function is not the real
  898. * frequency in the chip. It is calculated based on the predefined
  899. * constant and the selected clock source:
  900. * @note If SYSCLK source is HSI, function returns values based on HSI_VALUE(*)
  901. * @note If SYSCLK source is HSE, function returns values based on HSE_VALUE(**)
  902. * @note If SYSCLK source is PLL, function returns values based on HSE_VALUE(**),
  903. * HSI_VALUE(*) Value multiplied/divided by the PLL factors.
  904. * @note (*) HSI_VALUE is a constant defined in stm32g4xx_hal_conf.h file (default value
  905. * 16 MHz) but the real value may vary depending on the variations
  906. * in voltage and temperature.
  907. * @note (**) HSE_VALUE is a constant defined in stm32g4xx_hal_conf.h file (default value
  908. * 8 MHz), user has to ensure that HSE_VALUE is same as the real
  909. * frequency of the crystal used. Otherwise, this function may
  910. * have wrong result.
  911. *
  912. * @note The result of this function could be not correct when using fractional
  913. * value for HSE crystal.
  914. *
  915. * @note This function can be used by the user application to compute the
  916. * baudrate for the communication peripherals or configure other parameters.
  917. *
  918. * @note Each time SYSCLK changes, this function must be called to update the
  919. * right SYSCLK value. Otherwise, any configuration based on this function will be incorrect.
  920. *
  921. *
  922. * @retval SYSCLK frequency
  923. */
  924. uint32_t HAL_RCC_GetSysClockFreq(void)
  925. {
  926. uint32_t pllvco, pllsource, pllr, pllm;
  927. uint32_t sysclockfreq;
  928. if (__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSI)
  929. {
  930. /* HSI used as system clock source */
  931. sysclockfreq = HSI_VALUE;
  932. }
  933. else if (__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSE)
  934. {
  935. /* HSE used as system clock source */
  936. sysclockfreq = HSE_VALUE;
  937. }
  938. else if (__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL)
  939. {
  940. /* PLL used as system clock source */
  941. /* PLL_VCO = ((HSE_VALUE or HSI_VALUE)/ PLLM) * PLLN
  942. SYSCLK = PLL_VCO / PLLR
  943. */
  944. pllsource = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC);
  945. pllm = (READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1U ;
  946. switch (pllsource)
  947. {
  948. case RCC_PLLSOURCE_HSE: /* HSE used as PLL clock source */
  949. pllvco = (HSE_VALUE / pllm) * (READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos);
  950. break;
  951. case RCC_PLLSOURCE_HSI: /* HSI used as PLL clock source */
  952. default:
  953. pllvco = (HSI_VALUE / pllm) * (READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos);
  954. break;
  955. }
  956. pllr = ((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLR) >> RCC_PLLCFGR_PLLR_Pos) + 1U ) * 2U;
  957. sysclockfreq = pllvco/pllr;
  958. }
  959. else
  960. {
  961. sysclockfreq = 0U;
  962. }
  963. return sysclockfreq;
  964. }
  965. /**
  966. * @brief Return the HCLK frequency.
  967. * @note Each time HCLK changes, this function must be called to update the
  968. * right HCLK value. Otherwise, any configuration based on this function will be incorrect.
  969. *
  970. * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency.
  971. * @retval HCLK frequency in Hz
  972. */
  973. uint32_t HAL_RCC_GetHCLKFreq(void)
  974. {
  975. return SystemCoreClock;
  976. }
  977. /**
  978. * @brief Return the PCLK1 frequency.
  979. * @note Each time PCLK1 changes, this function must be called to update the
  980. * right PCLK1 value. Otherwise, any configuration based on this function will be incorrect.
  981. * @retval PCLK1 frequency in Hz
  982. */
  983. uint32_t HAL_RCC_GetPCLK1Freq(void)
  984. {
  985. /* Get HCLK source and Compute PCLK1 frequency ---------------------------*/
  986. return (HAL_RCC_GetHCLKFreq() >> (APBPrescTable[READ_BIT(RCC->CFGR, RCC_CFGR_PPRE1) >> RCC_CFGR_PPRE1_Pos] & 0x1FU));
  987. }
  988. /**
  989. * @brief Return the PCLK2 frequency.
  990. * @note Each time PCLK2 changes, this function must be called to update the
  991. * right PCLK2 value. Otherwise, any configuration based on this function will be incorrect.
  992. * @retval PCLK2 frequency in Hz
  993. */
  994. uint32_t HAL_RCC_GetPCLK2Freq(void)
  995. {
  996. /* Get HCLK source and Compute PCLK2 frequency ---------------------------*/
  997. return (HAL_RCC_GetHCLKFreq()>> (APBPrescTable[READ_BIT(RCC->CFGR, RCC_CFGR_PPRE2) >> RCC_CFGR_PPRE2_Pos] & 0x1FU));
  998. }
  999. /**
  1000. * @brief Configure the RCC_OscInitStruct according to the internal
  1001. * RCC configuration registers.
  1002. * @param RCC_OscInitStruct pointer to an RCC_OscInitTypeDef structure that
  1003. * will be configured.
  1004. * @retval None
  1005. */
  1006. void HAL_RCC_GetOscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  1007. {
  1008. /* Check the parameters */
  1009. assert_param(RCC_OscInitStruct != (void *)NULL);
  1010. /* Set all possible values for the Oscillator type parameter ---------------*/
  1011. RCC_OscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | \
  1012. RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_HSI48;
  1013. /* Get the HSE configuration -----------------------------------------------*/
  1014. if(READ_BIT(RCC->CR, RCC_CR_HSEBYP) == RCC_CR_HSEBYP)
  1015. {
  1016. RCC_OscInitStruct->HSEState = RCC_HSE_BYPASS;
  1017. }
  1018. else if(READ_BIT(RCC->CR, RCC_CR_HSEON) == RCC_CR_HSEON)
  1019. {
  1020. RCC_OscInitStruct->HSEState = RCC_HSE_ON;
  1021. }
  1022. else
  1023. {
  1024. RCC_OscInitStruct->HSEState = RCC_HSE_OFF;
  1025. }
  1026. /* Get the HSI configuration -----------------------------------------------*/
  1027. if(READ_BIT(RCC->CR, RCC_CR_HSION) == RCC_CR_HSION)
  1028. {
  1029. RCC_OscInitStruct->HSIState = RCC_HSI_ON;
  1030. }
  1031. else
  1032. {
  1033. RCC_OscInitStruct->HSIState = RCC_HSI_OFF;
  1034. }
  1035. RCC_OscInitStruct->HSICalibrationValue = READ_BIT(RCC->ICSCR, RCC_ICSCR_HSITRIM) >> RCC_ICSCR_HSITRIM_Pos;
  1036. /* Get the LSE configuration -----------------------------------------------*/
  1037. if(READ_BIT(RCC->BDCR, RCC_BDCR_LSEBYP) == RCC_BDCR_LSEBYP)
  1038. {
  1039. RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS;
  1040. }
  1041. else if(READ_BIT(RCC->BDCR, RCC_BDCR_LSEON) == RCC_BDCR_LSEON)
  1042. {
  1043. RCC_OscInitStruct->LSEState = RCC_LSE_ON;
  1044. }
  1045. else
  1046. {
  1047. RCC_OscInitStruct->LSEState = RCC_LSE_OFF;
  1048. }
  1049. /* Get the LSI configuration -----------------------------------------------*/
  1050. if(READ_BIT(RCC->CSR, RCC_CSR_LSION) == RCC_CSR_LSION)
  1051. {
  1052. RCC_OscInitStruct->LSIState = RCC_LSI_ON;
  1053. }
  1054. else
  1055. {
  1056. RCC_OscInitStruct->LSIState = RCC_LSI_OFF;
  1057. }
  1058. /* Get the HSI48 configuration ---------------------------------------------*/
  1059. if(READ_BIT(RCC->CRRCR, RCC_CRRCR_HSI48ON) == RCC_CRRCR_HSI48ON)
  1060. {
  1061. RCC_OscInitStruct->HSI48State = RCC_HSI48_ON;
  1062. }
  1063. else
  1064. {
  1065. RCC_OscInitStruct->HSI48State = RCC_HSI48_OFF;
  1066. }
  1067. /* Get the PLL configuration -----------------------------------------------*/
  1068. if(READ_BIT(RCC->CR, RCC_CR_PLLON) == RCC_CR_PLLON)
  1069. {
  1070. RCC_OscInitStruct->PLL.PLLState = RCC_PLL_ON;
  1071. }
  1072. else
  1073. {
  1074. RCC_OscInitStruct->PLL.PLLState = RCC_PLL_OFF;
  1075. }
  1076. RCC_OscInitStruct->PLL.PLLSource = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC);
  1077. RCC_OscInitStruct->PLL.PLLM = (READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1U;
  1078. RCC_OscInitStruct->PLL.PLLN = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos;
  1079. RCC_OscInitStruct->PLL.PLLQ = (((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLQ) >> RCC_PLLCFGR_PLLQ_Pos) + 1U) << 1U);
  1080. RCC_OscInitStruct->PLL.PLLR = (((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLR) >> RCC_PLLCFGR_PLLR_Pos) + 1U) << 1U);
  1081. RCC_OscInitStruct->PLL.PLLP = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLPDIV) >> RCC_PLLCFGR_PLLPDIV_Pos;
  1082. }
  1083. /**
  1084. * @brief Configure the RCC_ClkInitStruct according to the internal
  1085. * RCC configuration registers.
  1086. * @param RCC_ClkInitStruct pointer to an RCC_ClkInitTypeDef structure that
  1087. * will be configured.
  1088. * @param pFLatency Pointer on the Flash Latency.
  1089. * @retval None
  1090. */
  1091. void HAL_RCC_GetClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t *pFLatency)
  1092. {
  1093. /* Check the parameters */
  1094. assert_param(RCC_ClkInitStruct != (void *)NULL);
  1095. assert_param(pFLatency != (void *)NULL);
  1096. /* Set all possible values for the Clock type parameter --------------------*/
  1097. RCC_ClkInitStruct->ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  1098. /* Get the SYSCLK configuration --------------------------------------------*/
  1099. RCC_ClkInitStruct->SYSCLKSource = READ_BIT(RCC->CFGR, RCC_CFGR_SW);
  1100. /* Get the HCLK configuration ----------------------------------------------*/
  1101. RCC_ClkInitStruct->AHBCLKDivider = READ_BIT(RCC->CFGR, RCC_CFGR_HPRE);
  1102. /* Get the APB1 configuration ----------------------------------------------*/
  1103. RCC_ClkInitStruct->APB1CLKDivider = READ_BIT(RCC->CFGR, RCC_CFGR_PPRE1);
  1104. /* Get the APB2 configuration ----------------------------------------------*/
  1105. RCC_ClkInitStruct->APB2CLKDivider = (READ_BIT(RCC->CFGR, RCC_CFGR_PPRE2) >> 3U);
  1106. /* Get the Flash Wait State (Latency) configuration ------------------------*/
  1107. *pFLatency = __HAL_FLASH_GET_LATENCY();
  1108. }
  1109. /**
  1110. * @brief Enable the Clock Security System.
  1111. * @note If a failure is detected on the HSE oscillator clock, this oscillator
  1112. * is automatically disabled and an interrupt is generated to inform the
  1113. * software about the failure (Clock Security System Interrupt, CSSI),
  1114. * allowing the MCU to perform rescue operations. The CSSI is linked to
  1115. * the Cortex-M4 NMI (Non-Maskable Interrupt) exception vector.
  1116. * @note The Clock Security System can only be cleared by reset.
  1117. * @retval None
  1118. */
  1119. void HAL_RCC_EnableCSS(void)
  1120. {
  1121. SET_BIT(RCC->CR, RCC_CR_CSSON) ;
  1122. }
  1123. /**
  1124. * @brief Enable the LSE Clock Security System.
  1125. * @note If a failure is detected on the external 32 kHz oscillator,
  1126. * the LSE clock is no longer supplied to the RTC but no hardware action
  1127. * is made to the registers. If enabled, an interrupt will be generated
  1128. * and handle through @ref RCCEx_EXTI_LINE_LSECSS
  1129. * @note The Clock Security System can only be cleared by reset or after a LSE failure detection.
  1130. * @retval None
  1131. */
  1132. void HAL_RCC_EnableLSECSS(void)
  1133. {
  1134. SET_BIT(RCC->BDCR, RCC_BDCR_LSECSSON) ;
  1135. }
  1136. /**
  1137. * @brief Disable the LSE Clock Security System.
  1138. * @note After LSE failure detection, the software must disable LSECSSON
  1139. * @note The Clock Security System can only be cleared by reset otherwise.
  1140. * @retval None
  1141. */
  1142. void HAL_RCC_DisableLSECSS(void)
  1143. {
  1144. CLEAR_BIT(RCC->BDCR, RCC_BDCR_LSECSSON) ;
  1145. }
  1146. /**
  1147. * @brief Handle the RCC Clock Security System interrupt request.
  1148. * @note This API should be called under the NMI_Handler().
  1149. * @retval None
  1150. */
  1151. void HAL_RCC_NMI_IRQHandler(void)
  1152. {
  1153. /* Check RCC CSSF interrupt flag */
  1154. if(__HAL_RCC_GET_IT(RCC_IT_CSS))
  1155. {
  1156. /* RCC Clock Security System interrupt user callback */
  1157. HAL_RCC_CSSCallback();
  1158. /* Clear RCC CSS pending bit */
  1159. __HAL_RCC_CLEAR_IT(RCC_IT_CSS);
  1160. }
  1161. }
  1162. /**
  1163. * @brief RCC Clock Security System interrupt callback.
  1164. * @retval none
  1165. */
  1166. __weak void HAL_RCC_CSSCallback(void)
  1167. {
  1168. /* NOTE : This function should not be modified, when the callback is needed,
  1169. the HAL_RCC_CSSCallback should be implemented in the user file
  1170. */
  1171. }
  1172. /**
  1173. * @}
  1174. */
  1175. /**
  1176. * @}
  1177. */
  1178. /* Private function prototypes -----------------------------------------------*/
  1179. /** @addtogroup RCC_Private_Functions
  1180. * @{
  1181. */
  1182. /**
  1183. * @brief Compute SYSCLK frequency based on PLL SYSCLK source.
  1184. * @retval SYSCLK frequency
  1185. */
  1186. static uint32_t RCC_GetSysClockFreqFromPLLSource(void)
  1187. {
  1188. uint32_t pllvco, pllsource, pllr, pllm;
  1189. uint32_t sysclockfreq;
  1190. /* PLL_VCO = (HSE_VALUE or HSI_VALUE/ PLLM) * PLLN
  1191. SYSCLK = PLL_VCO / PLLR
  1192. */
  1193. pllsource = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC);
  1194. pllm = (READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1U ;
  1195. switch (pllsource)
  1196. {
  1197. case RCC_PLLSOURCE_HSE: /* HSE used as PLL clock source */
  1198. pllvco = (HSE_VALUE / pllm) * (READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos);
  1199. break;
  1200. case RCC_PLLSOURCE_HSI: /* HSI used as PLL clock source */
  1201. default:
  1202. pllvco = (HSI_VALUE / pllm) * (READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos);
  1203. break;
  1204. }
  1205. pllr = ((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLR) >> RCC_PLLCFGR_PLLR_Pos) + 1U ) * 2U;
  1206. sysclockfreq = pllvco/pllr;
  1207. return sysclockfreq;
  1208. }
  1209. /**
  1210. * @}
  1211. */
  1212. #endif /* HAL_RCC_MODULE_ENABLED */
  1213. /**
  1214. * @}
  1215. */
  1216. /**
  1217. * @}
  1218. */