stm32g0xx_hal_rcc.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472
  1. /**
  2. ******************************************************************************
  3. * @file stm32g0xx_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. (from 8 MHz to reach 16MHz) with Flash 0 wait state. Flash prefetch buffer,
  18. D-Cache 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 (AHB) and Low speed (APB) 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 (RTC, ADC, RNG, HSTIM)
  34. @endverbatim
  35. ******************************************************************************
  36. * @attention
  37. *
  38. * Copyright (c) 2018 STMicroelectronics.
  39. * All rights reserved.
  40. *
  41. * This software is licensed under terms that can be found in the LICENSE file in
  42. * the root directory of this software component.
  43. * If no LICENSE file comes with this software, it is provided AS-IS.
  44. ******************************************************************************
  45. */
  46. /* Includes ------------------------------------------------------------------*/
  47. #include "stm32g0xx_hal.h"
  48. /** @addtogroup STM32G0xx_HAL_Driver
  49. * @{
  50. */
  51. /** @defgroup RCC RCC
  52. * @brief RCC HAL module driver
  53. * @{
  54. */
  55. #ifdef HAL_RCC_MODULE_ENABLED
  56. /* Private typedef -----------------------------------------------------------*/
  57. /* Private define ------------------------------------------------------------*/
  58. /** @defgroup RCC_Private_Constants RCC Private Constants
  59. * @{
  60. */
  61. #define HSE_TIMEOUT_VALUE HSE_STARTUP_TIMEOUT
  62. #define HSI_TIMEOUT_VALUE (2U) /* 2 ms (minimum Tick + 1) */
  63. #define LSI_TIMEOUT_VALUE (2U) /* 2 ms (minimum Tick + 1) */
  64. #define PLL_TIMEOUT_VALUE (2U) /* 2 ms (minimum Tick + 1) */
  65. #if defined(RCC_HSI48_SUPPORT)
  66. #define HSI48_TIMEOUT_VALUE (2U) /* 2 ms (minimum Tick + 1) */
  67. #endif /* RCC_HSI48_SUPPORT */
  68. #define CLOCKSWITCH_TIMEOUT_VALUE (5000U) /* 5 s */
  69. #define PLLSOURCE_NONE (0U)
  70. /**
  71. * @}
  72. */
  73. /* Private macro -------------------------------------------------------------*/
  74. /** @defgroup RCC_Private_Macros RCC Private Macros
  75. * @{
  76. */
  77. #define RCC_GET_MCO_GPIO_PIN(__RCC_MCOx__) ((__RCC_MCOx__) & GPIO_PIN_MASK)
  78. #define RCC_GET_MCO_GPIO_AF(__RCC_MCOx__) (((__RCC_MCOx__) & RCC_MCO_GPIOAF_MASK) >> RCC_MCO_GPIOAF_POS)
  79. #define RCC_GET_MCO_GPIO_INDEX(__RCC_MCOx__) (((__RCC_MCOx__) & RCC_MCO_GPIOPORT_MASK) >> RCC_MCO_GPIOPORT_POS)
  80. #define RCC_GET_MCO_GPIO_PORT(__RCC_MCOx__) (IOPORT_BASE + ((0x00000400UL) * RCC_GET_MCO_GPIO_INDEX(__RCC_MCOx__)))
  81. #define RCC_PLL_OSCSOURCE_CONFIG(__HAL_RCC_PLLSOURCE__) \
  82. (MODIFY_REG(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC, (uint32_t)(__HAL_RCC_PLLSOURCE__)))
  83. /**
  84. * @}
  85. */
  86. /* Private variables ---------------------------------------------------------*/
  87. /** @defgroup RCC_Private_Variables RCC Private Variables
  88. * @{
  89. */
  90. /**
  91. * @}
  92. */
  93. /* Private function prototypes -----------------------------------------------*/
  94. /* Exported functions --------------------------------------------------------*/
  95. /** @defgroup RCC_Exported_Functions RCC Exported Functions
  96. * @{
  97. */
  98. /** @defgroup RCC_Exported_Functions_Group1 Initialization and de-initialization functions
  99. * @brief Initialization and Configuration functions
  100. *
  101. @verbatim
  102. ===============================================================================
  103. ##### Initialization and de-initialization functions #####
  104. ===============================================================================
  105. [..]
  106. This section provides functions allowing to configure the internal and external oscillators
  107. (HSE, HSI, LSE, LSI, PLL, CSS and MCO) and the System buses clocks (SYSCLK, AHB, APB)
  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 (R) is used to generate the high speed system clock (up to 64MHz).
  118. (++) The second output(Q) is used to generate the clock for the random analog generator and HStim.
  119. (++) The Third output (P) is used to generate the clock for the Analog to Digital Converter and I2S.
  120. (+) CSS (Clock security system): once enabled, if a HSE or LSE clock failure occurs
  121. (HSE used directly or through PLL as System clock source), the System clock
  122. is automatically switched respectively to HSI or LSI and an interrupt is generated
  123. if enabled. The interrupt is linked to the Cortex-M0+ NMI (Non-Maskable Interrupt)
  124. exception vector.
  125. (+) MCOx (microcontroller clock output):
  126. (++) MCO1 used to output LSI, HSI48(*), HSI, LSE, HSE or main PLL clock (through a configurable prescaler) on PA8 pin.
  127. (++) MCO2(*) used to output LSI, HSI48(*), HSI, LSE, HSE, main PLLR clock, PLLQ clock, PLLP clock, RTC clock or RTC_Wakeup (through a configurable prescaler) on PA10 pin.
  128. (*) available on certain devices only
  129. [..] System, AHB and APB buses clocks configuration
  130. (+) Several clock sources can be used to drive the System clock (SYSCLK): HSI,
  131. HSE, LSI, LSE and main PLL.
  132. The AHB clock (HCLK) is derived from System clock through configurable
  133. prescaler and used to clock the CPU, memory and peripherals mapped
  134. on AHB bus (DMA, GPIO...).and APB (PCLK1) clock is derived
  135. from AHB clock through configurable prescalers and used to clock
  136. the peripherals mapped on these buses. You can use
  137. "HAL_RCC_GetSysClockFreq()" function to retrieve the frequencies of these clocks.
  138. -@- All the peripheral clocks are derived from the System clock (SYSCLK) except:
  139. (+@) RTC: the RTC clock can be derived either from the LSI, LSE or HSE clock
  140. divided by 2 to 31.
  141. You have to use __HAL_RCC_RTC_ENABLE() and HAL_RCCEx_PeriphCLKConfig() function
  142. to configure this clock.
  143. (+@) RNG(*) requires a frequency equal or lower than 48 MHz.
  144. This clock is derived from the main PLL or HSI or System clock.
  145. (*) available on certain devices only
  146. (+@) IWDG clock which is always the LSI clock.
  147. (+) The maximum frequency of the SYSCLK, HCLK, PCLK is 64 MHz.
  148. Depending on the device voltage range, the maximum frequency should be
  149. adapted accordingly.
  150. @endverbatim
  151. (++) Table 1. HCLK clock frequency.
  152. (++) +-------------------------------------------------------+
  153. (++) | Latency | HCLK clock frequency (MHz) |
  154. (++) | |-------------------------------------|
  155. (++) | | voltage range 1 | voltage range 2 |
  156. (++) | | 1.2 V | 1.0 V |
  157. (++) |-----------------|------------------|------------------|
  158. (++) |0WS(1 CPU cycles)| HCLK <= 24 | HCLK <= 8 |
  159. (++) |-----------------|------------------|------------------|
  160. (++) |1WS(2 CPU cycles)| HCLK <= 48 | HCLK <= 16 |
  161. (++) |-----------------|------------------|------------------|
  162. (++) |2WS(3 CPU cycles)| HCLK <= 64 | - |
  163. (++) |-----------------|------------------|------------------|
  164. * @{
  165. */
  166. /**
  167. * @brief Reset the RCC clock configuration to the default reset state.
  168. * @note The default reset state of the clock configuration is given below:
  169. * - HSI ON and used as system clock source
  170. * - HSE, PLL OFF
  171. * - AHB and APB prescaler set to 1.
  172. * - CSS, MCO1 OFF
  173. * - All interrupts disabled
  174. * @note This function does not modify the configuration of the
  175. * - Peripheral clocks
  176. * - LSI, LSE and RTC clocks
  177. * @retval HAL status
  178. */
  179. HAL_StatusTypeDef HAL_RCC_DeInit(void)
  180. {
  181. uint32_t tickstart;
  182. /* Get Start Tick*/
  183. tickstart = HAL_GetTick();
  184. /* Set HSION bit to the reset value */
  185. SET_BIT(RCC->CR, RCC_CR_HSION);
  186. /* Wait till HSI is ready */
  187. while (READ_BIT(RCC->CR, RCC_CR_HSIRDY) == 0U)
  188. {
  189. if ((HAL_GetTick() - tickstart) > HSI_TIMEOUT_VALUE)
  190. {
  191. return HAL_TIMEOUT;
  192. }
  193. }
  194. /* Set HSITRIM[6:0] bits to the reset value */
  195. RCC->ICSCR = RCC_ICSCR_HSITRIM_6;
  196. /* Get Start Tick*/
  197. tickstart = HAL_GetTick();
  198. /* Reset CFGR register (HSI is selected as system clock source) */
  199. RCC->CFGR = 0x00000000u;
  200. /* Wait till HSI is ready */
  201. while (READ_BIT(RCC->CFGR, RCC_CFGR_SWS) != 0U)
  202. {
  203. if ((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
  204. {
  205. return HAL_TIMEOUT;
  206. }
  207. }
  208. /* Clear CR register in 2 steps: first to clear HSEON in case bypass was enabled */
  209. RCC->CR = RCC_CR_HSION;
  210. /* Then again to HSEBYP in case bypass was enabled */
  211. RCC->CR = RCC_CR_HSION;
  212. /* Get Start Tick*/
  213. tickstart = HAL_GetTick();
  214. /* Wait till PLL is ready */
  215. while (READ_BIT(RCC->CR, RCC_CR_PLLRDY) != 0U)
  216. {
  217. if ((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  218. {
  219. return HAL_TIMEOUT;
  220. }
  221. }
  222. /* once PLL is OFF, reset PLLCFGR register to default value */
  223. RCC->PLLCFGR = RCC_PLLCFGR_PLLN_4;
  224. /* Disable all interrupts */
  225. RCC->CIER = 0x00000000u;
  226. /* Clear all flags */
  227. RCC->CICR = 0xFFFFFFFFu;
  228. /* Update the SystemCoreClock global variable */
  229. SystemCoreClock = HSI_VALUE;
  230. /* Adapt Systick interrupt period */
  231. if (HAL_InitTick(uwTickPrio) != HAL_OK)
  232. {
  233. return HAL_ERROR;
  234. }
  235. else
  236. {
  237. return HAL_OK;
  238. }
  239. }
  240. /**
  241. * @brief Initialize the RCC Oscillators according to the specified parameters in the
  242. * @ref RCC_OscInitTypeDef.
  243. * @param RCC_OscInitStruct pointer to a @ref RCC_OscInitTypeDef structure that
  244. * contains the configuration information for the RCC Oscillators.
  245. * @note The PLL is not disabled when used as system clock.
  246. * @note Transition HSE Bypass to HSE On and HSE On to HSE Bypass are not
  247. * supported by this function. User should request a transition to HSE Off
  248. * first and then to HSE On or HSE Bypass.
  249. * @note Transition LSE Bypass to LSE On and LSE On to LSE Bypass are not
  250. * supported by this function. User should request a transition to LSE Off
  251. * first and then to LSE On or LSE Bypass.
  252. * @retval HAL status
  253. */
  254. HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  255. {
  256. uint32_t tickstart;
  257. uint32_t temp_sysclksrc;
  258. uint32_t temp_pllckcfg;
  259. /* Check Null pointer */
  260. if (RCC_OscInitStruct == NULL)
  261. {
  262. return HAL_ERROR;
  263. }
  264. /* Check the parameters */
  265. assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct->OscillatorType));
  266. /*------------------------------- HSE Configuration ------------------------*/
  267. if (((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE)
  268. {
  269. /* Check the parameters */
  270. assert_param(IS_RCC_HSE(RCC_OscInitStruct->HSEState));
  271. temp_sysclksrc = __HAL_RCC_GET_SYSCLK_SOURCE();
  272. temp_pllckcfg = __HAL_RCC_GET_PLL_OSCSOURCE();
  273. /* When the HSE is used as system clock or clock source for PLL in these cases it is not allowed to be disabled */
  274. if (((temp_sysclksrc == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && (temp_pllckcfg == RCC_PLLSOURCE_HSE))
  275. || (temp_sysclksrc == RCC_SYSCLKSOURCE_STATUS_HSE))
  276. {
  277. if ((READ_BIT(RCC->CR, RCC_CR_HSERDY) != 0U) && (RCC_OscInitStruct->HSEState == RCC_HSE_OFF))
  278. {
  279. return HAL_ERROR;
  280. }
  281. }
  282. else
  283. {
  284. /* Set the new HSE configuration ---------------------------------------*/
  285. __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct->HSEState);
  286. /* Check the HSE State */
  287. if (RCC_OscInitStruct->HSEState != RCC_HSE_OFF)
  288. {
  289. /* Get Start Tick*/
  290. tickstart = HAL_GetTick();
  291. /* Wait till HSE is ready */
  292. while (READ_BIT(RCC->CR, RCC_CR_HSERDY) == 0U)
  293. {
  294. if ((HAL_GetTick() - tickstart) > HSE_TIMEOUT_VALUE)
  295. {
  296. return HAL_TIMEOUT;
  297. }
  298. }
  299. }
  300. else
  301. {
  302. /* Get Start Tick*/
  303. tickstart = HAL_GetTick();
  304. /* Wait till HSE is disabled */
  305. while (READ_BIT(RCC->CR, RCC_CR_HSERDY) != 0U)
  306. {
  307. if ((HAL_GetTick() - tickstart) > HSE_TIMEOUT_VALUE)
  308. {
  309. return HAL_TIMEOUT;
  310. }
  311. }
  312. }
  313. }
  314. }
  315. /*----------------------------- HSI Configuration --------------------------*/
  316. if (((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI)
  317. {
  318. /* Check the parameters */
  319. assert_param(IS_RCC_HSI(RCC_OscInitStruct->HSIState));
  320. assert_param(IS_RCC_HSI_CALIBRATION_VALUE(RCC_OscInitStruct->HSICalibrationValue));
  321. assert_param(IS_RCC_HSIDIV(RCC_OscInitStruct->HSIDiv));
  322. /* Check if HSI16 is used as system clock or as PLL source when PLL is selected as system clock */
  323. temp_sysclksrc = __HAL_RCC_GET_SYSCLK_SOURCE();
  324. temp_pllckcfg = __HAL_RCC_GET_PLL_OSCSOURCE();
  325. if (((temp_sysclksrc == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && (temp_pllckcfg == RCC_PLLSOURCE_HSI))
  326. || (temp_sysclksrc == RCC_SYSCLKSOURCE_STATUS_HSI))
  327. {
  328. /* When HSI is used as system clock or as PLL input clock it can not be disabled */
  329. if ((READ_BIT(RCC->CR, RCC_CR_HSIRDY) != 0U) && (RCC_OscInitStruct->HSIState == RCC_HSI_OFF))
  330. {
  331. return HAL_ERROR;
  332. }
  333. /* Otherwise, just the calibration is allowed */
  334. else
  335. {
  336. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  337. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  338. if (temp_sysclksrc == RCC_SYSCLKSOURCE_STATUS_HSI)
  339. {
  340. /* Adjust the HSI16 division factor */
  341. __HAL_RCC_HSI_CONFIG(RCC_OscInitStruct->HSIDiv);
  342. /* Update the SystemCoreClock global variable with HSISYS value */
  343. SystemCoreClock = (HSI_VALUE / (1UL << ((READ_BIT(RCC->CR, RCC_CR_HSIDIV)) >> RCC_CR_HSIDIV_Pos)));
  344. }
  345. /* Adapt Systick interrupt period */
  346. if (HAL_InitTick(uwTickPrio) != HAL_OK)
  347. {
  348. return HAL_ERROR;
  349. }
  350. }
  351. }
  352. else
  353. {
  354. /* Check the HSI State */
  355. if (RCC_OscInitStruct->HSIState != RCC_HSI_OFF)
  356. {
  357. /* Configure the HSI16 division factor */
  358. __HAL_RCC_HSI_CONFIG(RCC_OscInitStruct->HSIDiv);
  359. /* Enable the Internal High Speed oscillator (HSI16). */
  360. __HAL_RCC_HSI_ENABLE();
  361. /* Get Start Tick*/
  362. tickstart = HAL_GetTick();
  363. /* Wait till HSI is ready */
  364. while (READ_BIT(RCC->CR, RCC_CR_HSIRDY) == 0U)
  365. {
  366. if ((HAL_GetTick() - tickstart) > HSI_TIMEOUT_VALUE)
  367. {
  368. return HAL_TIMEOUT;
  369. }
  370. }
  371. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  372. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  373. }
  374. else
  375. {
  376. /* Disable the Internal High Speed oscillator (HSI16). */
  377. __HAL_RCC_HSI_DISABLE();
  378. /* Get Start Tick*/
  379. tickstart = HAL_GetTick();
  380. /* Wait till HSI is disabled */
  381. while (READ_BIT(RCC->CR, RCC_CR_HSIRDY) != 0U)
  382. {
  383. if ((HAL_GetTick() - tickstart) > HSI_TIMEOUT_VALUE)
  384. {
  385. return HAL_TIMEOUT;
  386. }
  387. }
  388. }
  389. }
  390. }
  391. /*------------------------------ LSI Configuration -------------------------*/
  392. if (((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI)
  393. {
  394. /* Check the parameters */
  395. assert_param(IS_RCC_LSI(RCC_OscInitStruct->LSIState));
  396. /* Check if LSI is used as system clock */
  397. if (__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_LSI)
  398. {
  399. /* When LSI is used as system clock it will not be disabled */
  400. if ((((RCC->CSR) & RCC_CSR_LSIRDY) != 0U) && (RCC_OscInitStruct->LSIState == RCC_LSI_OFF))
  401. {
  402. return HAL_ERROR;
  403. }
  404. }
  405. else
  406. {
  407. /* Check the LSI State */
  408. if (RCC_OscInitStruct->LSIState != RCC_LSI_OFF)
  409. {
  410. /* Enable the Internal Low Speed oscillator (LSI). */
  411. __HAL_RCC_LSI_ENABLE();
  412. /* Get Start Tick*/
  413. tickstart = HAL_GetTick();
  414. /* Wait till LSI is ready */
  415. while (READ_BIT(RCC->CSR, RCC_CSR_LSIRDY) == 0U)
  416. {
  417. if ((HAL_GetTick() - tickstart) > LSI_TIMEOUT_VALUE)
  418. {
  419. return HAL_TIMEOUT;
  420. }
  421. }
  422. }
  423. else
  424. {
  425. /* Disable the Internal Low Speed oscillator (LSI). */
  426. __HAL_RCC_LSI_DISABLE();
  427. /* Get Start Tick*/
  428. tickstart = HAL_GetTick();
  429. /* Wait till LSI is disabled */
  430. while (READ_BIT(RCC->CSR, RCC_CSR_LSIRDY) != 0U)
  431. {
  432. if ((HAL_GetTick() - tickstart) > LSI_TIMEOUT_VALUE)
  433. {
  434. return HAL_TIMEOUT;
  435. }
  436. }
  437. }
  438. }
  439. }
  440. /*------------------------------ LSE Configuration -------------------------*/
  441. if (((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE)
  442. {
  443. FlagStatus pwrclkchanged = RESET;
  444. /* Check the parameters */
  445. assert_param(IS_RCC_LSE(RCC_OscInitStruct->LSEState));
  446. /* When the LSE is used as system clock, it is not allowed disable it */
  447. if (__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_LSE)
  448. {
  449. if ((((RCC->BDCR) & RCC_BDCR_LSERDY) != 0U) && (RCC_OscInitStruct->LSEState == RCC_LSE_OFF))
  450. {
  451. return HAL_ERROR;
  452. }
  453. }
  454. else
  455. {
  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() != 0U)
  459. {
  460. __HAL_RCC_PWR_CLK_ENABLE();
  461. pwrclkchanged = SET;
  462. }
  463. if (HAL_IS_BIT_CLR(PWR->CR1, PWR_CR1_DBP))
  464. {
  465. /* Enable write access to Backup domain */
  466. SET_BIT(PWR->CR1, PWR_CR1_DBP);
  467. /* Wait for Backup domain Write protection disable */
  468. tickstart = HAL_GetTick();
  469. while (HAL_IS_BIT_CLR(PWR->CR1, PWR_CR1_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 (READ_BIT(RCC->BDCR, RCC_BDCR_LSERDY) == 0U)
  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 (READ_BIT(RCC->BDCR, RCC_BDCR_LSERDY) != 0U)
  499. {
  500. if ((HAL_GetTick() - tickstart) > RCC_LSE_TIMEOUT_VALUE)
  501. {
  502. return HAL_TIMEOUT;
  503. }
  504. }
  505. }
  506. /* Restore clock configuration if changed */
  507. if (pwrclkchanged == SET)
  508. {
  509. __HAL_RCC_PWR_CLK_DISABLE();
  510. }
  511. }
  512. }
  513. #if defined(RCC_HSI48_SUPPORT)
  514. /*------------------------------ HSI48 Configuration -----------------------*/
  515. if (((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI48) == RCC_OSCILLATORTYPE_HSI48)
  516. {
  517. /* Check the parameters */
  518. assert_param(IS_RCC_HSI48(RCC_OscInitStruct->HSI48State));
  519. /* Check the LSI State */
  520. if (RCC_OscInitStruct->HSI48State != RCC_HSI48_OFF)
  521. {
  522. /* Enable the Internal Low Speed oscillator (HSI48). */
  523. __HAL_RCC_HSI48_ENABLE();
  524. /* Get Start Tick*/
  525. tickstart = HAL_GetTick();
  526. /* Wait till HSI48 is ready */
  527. while (READ_BIT(RCC->CR, RCC_CR_HSI48RDY) == 0U)
  528. {
  529. if ((HAL_GetTick() - tickstart) > HSI48_TIMEOUT_VALUE)
  530. {
  531. return HAL_TIMEOUT;
  532. }
  533. }
  534. }
  535. else
  536. {
  537. /* Disable the Internal Low Speed oscillator (HSI48). */
  538. __HAL_RCC_HSI48_DISABLE();
  539. /* Get Start Tick*/
  540. tickstart = HAL_GetTick();
  541. /* Wait till HSI48 is disabled */
  542. while (READ_BIT(RCC->CR, RCC_CR_HSI48RDY) != 0U)
  543. {
  544. if ((HAL_GetTick() - tickstart) > HSI48_TIMEOUT_VALUE)
  545. {
  546. return HAL_TIMEOUT;
  547. }
  548. }
  549. }
  550. }
  551. #endif /* RCC_HSI48_SUPPORT */
  552. /*-------------------------------- PLL Configuration -----------------------*/
  553. /* Check the parameters */
  554. assert_param(IS_RCC_PLL(RCC_OscInitStruct->PLL.PLLState));
  555. if (RCC_OscInitStruct->PLL.PLLState != RCC_PLL_NONE)
  556. {
  557. /* Check if the PLL is used as system clock or not */
  558. if (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_PLLCLK)
  559. {
  560. if (RCC_OscInitStruct->PLL.PLLState == RCC_PLL_ON)
  561. {
  562. /* Check the parameters */
  563. assert_param(IS_RCC_PLLSOURCE(RCC_OscInitStruct->PLL.PLLSource));
  564. assert_param(IS_RCC_PLLM_VALUE(RCC_OscInitStruct->PLL.PLLM));
  565. assert_param(IS_RCC_PLLN_VALUE(RCC_OscInitStruct->PLL.PLLN));
  566. assert_param(IS_RCC_PLLP_VALUE(RCC_OscInitStruct->PLL.PLLP));
  567. #if defined(RCC_PLLQ_SUPPORT)
  568. assert_param(IS_RCC_PLLQ_VALUE(RCC_OscInitStruct->PLL.PLLQ));
  569. #endif /* RCC_PLLQ_SUPPORT */
  570. assert_param(IS_RCC_PLLR_VALUE(RCC_OscInitStruct->PLL.PLLR));
  571. /* Disable the main PLL. */
  572. __HAL_RCC_PLL_DISABLE();
  573. /* Get Start Tick*/
  574. tickstart = HAL_GetTick();
  575. /* Wait till PLL is ready */
  576. while (READ_BIT(RCC->CR, RCC_CR_PLLRDY) != 0U)
  577. {
  578. if ((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  579. {
  580. return HAL_TIMEOUT;
  581. }
  582. }
  583. /* Configure the main PLL clock source, multiplication and division factors. */
  584. #if defined(RCC_PLLQ_SUPPORT)
  585. __HAL_RCC_PLL_CONFIG(RCC_OscInitStruct->PLL.PLLSource,
  586. RCC_OscInitStruct->PLL.PLLM,
  587. RCC_OscInitStruct->PLL.PLLN,
  588. RCC_OscInitStruct->PLL.PLLP,
  589. RCC_OscInitStruct->PLL.PLLQ,
  590. RCC_OscInitStruct->PLL.PLLR);
  591. #else /* !RCC_PLLQ_SUPPORT */
  592. __HAL_RCC_PLL_CONFIG(RCC_OscInitStruct->PLL.PLLSource,
  593. RCC_OscInitStruct->PLL.PLLM,
  594. RCC_OscInitStruct->PLL.PLLN,
  595. RCC_OscInitStruct->PLL.PLLP,
  596. RCC_OscInitStruct->PLL.PLLR);
  597. #endif /* RCC_PLLQ_SUPPORT */
  598. /* Enable the main PLL. */
  599. __HAL_RCC_PLL_ENABLE();
  600. /* Enable PLLR Clock output. */
  601. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLLRCLK);
  602. /* Get Start Tick*/
  603. tickstart = HAL_GetTick();
  604. /* Wait till PLL is ready */
  605. while (READ_BIT(RCC->CR, RCC_CR_PLLRDY) == 0U)
  606. {
  607. if ((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  608. {
  609. return HAL_TIMEOUT;
  610. }
  611. }
  612. }
  613. else
  614. {
  615. /* Disable the main PLL. */
  616. __HAL_RCC_PLL_DISABLE();
  617. /* Get Start Tick*/
  618. tickstart = HAL_GetTick();
  619. /* Wait till PLL is disabled */
  620. while (READ_BIT(RCC->CR, RCC_CR_PLLRDY) != 0U)
  621. {
  622. if ((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  623. {
  624. return HAL_TIMEOUT;
  625. }
  626. }
  627. /* Unselect main PLL clock source and disable main PLL outputs to save power */
  628. #if defined(RCC_PLLQ_SUPPORT)
  629. RCC->PLLCFGR &= ~(RCC_PLLCFGR_PLLSRC | RCC_PLLCFGR_PLLPEN | RCC_PLLCFGR_PLLQEN | RCC_PLLCFGR_PLLREN);
  630. #else
  631. RCC->PLLCFGR &= ~(RCC_PLLCFGR_PLLSRC | RCC_PLLCFGR_PLLPEN | RCC_PLLCFGR_PLLREN);
  632. #endif /* RCC_PLLQ_SUPPORT */
  633. }
  634. }
  635. else
  636. {
  637. /* Check if there is a request to disable the PLL used as System clock source */
  638. if ((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_OFF)
  639. {
  640. return HAL_ERROR;
  641. }
  642. else
  643. {
  644. /* Do not return HAL_ERROR if request repeats the current configuration */
  645. temp_pllckcfg = RCC->PLLCFGR;
  646. if ((READ_BIT(temp_pllckcfg, RCC_PLLCFGR_PLLSRC) != RCC_OscInitStruct->PLL.PLLSource) ||
  647. (READ_BIT(temp_pllckcfg, RCC_PLLCFGR_PLLM) != RCC_OscInitStruct->PLL.PLLM) ||
  648. (READ_BIT(temp_pllckcfg, RCC_PLLCFGR_PLLN) != (RCC_OscInitStruct->PLL.PLLN << RCC_PLLCFGR_PLLN_Pos)) ||
  649. (READ_BIT(temp_pllckcfg, RCC_PLLCFGR_PLLP) != RCC_OscInitStruct->PLL.PLLP) ||
  650. #if defined (RCC_PLLQ_SUPPORT)
  651. (READ_BIT(temp_pllckcfg, RCC_PLLCFGR_PLLQ) != RCC_OscInitStruct->PLL.PLLQ) ||
  652. #endif /* RCC_PLLQ_SUPPORT */
  653. (READ_BIT(temp_pllckcfg, RCC_PLLCFGR_PLLR) != RCC_OscInitStruct->PLL.PLLR))
  654. {
  655. return HAL_ERROR;
  656. }
  657. }
  658. }
  659. }
  660. return HAL_OK;
  661. }
  662. /**
  663. * @brief Initialize the CPU, AHB and APB buses clocks according to the specified
  664. * parameters in the RCC_ClkInitStruct.
  665. * @param RCC_ClkInitStruct pointer to a @ref RCC_ClkInitTypeDef structure that
  666. * contains the configuration information for the RCC peripheral.
  667. * @param FLatency FLASH Latency
  668. * This parameter can be one of the following values:
  669. * @arg FLASH_LATENCY_0 FLASH 0 Latency cycle
  670. * @arg FLASH_LATENCY_1 FLASH 1 Latency cycle
  671. *
  672. * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency
  673. * and updated by @ref HAL_RCC_GetHCLKFreq() function called within this function
  674. *
  675. * @note The HSI is used by default as system clock source after
  676. * startup from Reset, wake-up from STANDBY mode. After restart from Reset,
  677. * the HSI frequency is set to 8 Mhz, then it reaches its default value 16 MHz.
  678. *
  679. * @note The HSI can be selected as system clock source after
  680. * from STOP modes or in case of failure of the HSE used directly or indirectly
  681. * as system clock (if the Clock Security System CSS is enabled).
  682. *
  683. * @note The LSI can be selected as system clock source after
  684. * in case of failure of the LSE used directly or indirectly
  685. * as system clock (if the Clock Security System LSECSS is enabled).
  686. *
  687. * @note A switch from one clock source to another occurs only if the target
  688. * clock source is ready (clock stable after startup delay or PLL locked).
  689. * If a clock source which is not yet ready is selected, the switch will
  690. * occur when the clock source is ready.
  691. *
  692. * @note You can use @ref HAL_RCC_GetClockConfig() function to know which clock is
  693. * currently used as system clock source.
  694. *
  695. * @note Depending on the device voltage range, the software has to set correctly
  696. * HPRE[3:0] bits to ensure that HCLK not exceed the maximum allowed frequency
  697. * (for more details refer to section above "Initialization/de-initialization functions")
  698. * @retval None
  699. */
  700. HAL_StatusTypeDef HAL_RCC_ClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t FLatency)
  701. {
  702. uint32_t tickstart;
  703. /* Check Null pointer */
  704. if (RCC_ClkInitStruct == NULL)
  705. {
  706. return HAL_ERROR;
  707. }
  708. /* Check the parameters */
  709. assert_param(IS_RCC_CLOCKTYPE(RCC_ClkInitStruct->ClockType));
  710. assert_param(IS_FLASH_LATENCY(FLatency));
  711. /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
  712. must be correctly programmed according to the frequency of the FLASH clock
  713. (HCLK) and the supply voltage of the device. */
  714. /* Increasing the number of wait states because of higher CPU frequency */
  715. if (FLatency > __HAL_FLASH_GET_LATENCY())
  716. {
  717. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  718. __HAL_FLASH_SET_LATENCY(FLatency);
  719. /* Check that the new number of wait states is taken into account to access the Flash
  720. memory by polling the FLASH_ACR register */
  721. tickstart = HAL_GetTick();
  722. while ((FLASH->ACR & FLASH_ACR_LATENCY) != FLatency)
  723. {
  724. if ((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
  725. {
  726. return HAL_TIMEOUT;
  727. }
  728. }
  729. }
  730. /*-------------------------- HCLK Configuration --------------------------*/
  731. if (((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)
  732. {
  733. /* Set the highest APB divider in order to ensure that we do not go through
  734. a non-spec phase whatever we decrease or increase HCLK. */
  735. if (((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
  736. {
  737. MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE, RCC_HCLK_DIV16);
  738. }
  739. /* Set the new HCLK clock divider */
  740. assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
  741. MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
  742. }
  743. /*------------------------- SYSCLK Configuration ---------------------------*/
  744. if (((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_SYSCLK) == RCC_CLOCKTYPE_SYSCLK)
  745. {
  746. assert_param(IS_RCC_SYSCLKSOURCE(RCC_ClkInitStruct->SYSCLKSource));
  747. /* HSE is selected as System Clock Source */
  748. if (RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
  749. {
  750. /* Check the HSE ready flag */
  751. if (READ_BIT(RCC->CR, RCC_CR_HSERDY) == 0U)
  752. {
  753. return HAL_ERROR;
  754. }
  755. }
  756. /* PLL is selected as System Clock Source */
  757. else if (RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
  758. {
  759. /* Check the PLL ready flag */
  760. if (READ_BIT(RCC->CR, RCC_CR_PLLRDY) == 0U)
  761. {
  762. return HAL_ERROR;
  763. }
  764. }
  765. /* HSI is selected as System Clock Source */
  766. else if (RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSI)
  767. {
  768. /* Check the HSI ready flag */
  769. if (READ_BIT(RCC->CR, RCC_CR_HSIRDY) == 0U)
  770. {
  771. return HAL_ERROR;
  772. }
  773. }
  774. /* LSI is selected as System Clock Source */
  775. else if (RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_LSI)
  776. {
  777. /* Check the LSI ready flag */
  778. if (READ_BIT(RCC->CSR, RCC_CSR_LSIRDY) == 0U)
  779. {
  780. return HAL_ERROR;
  781. }
  782. }
  783. /* LSE is selected as System Clock Source */
  784. else
  785. {
  786. /* Check the LSE ready flag */
  787. if (READ_BIT(RCC->BDCR, RCC_BDCR_LSERDY) == 0U)
  788. {
  789. return HAL_ERROR;
  790. }
  791. }
  792. MODIFY_REG(RCC->CFGR, RCC_CFGR_SW, RCC_ClkInitStruct->SYSCLKSource);
  793. /* Get Start Tick*/
  794. tickstart = HAL_GetTick();
  795. while (__HAL_RCC_GET_SYSCLK_SOURCE() != (RCC_ClkInitStruct->SYSCLKSource << RCC_CFGR_SWS_Pos))
  796. {
  797. if ((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
  798. {
  799. return HAL_TIMEOUT;
  800. }
  801. }
  802. }
  803. /* Decreasing the number of wait states because of lower CPU frequency */
  804. if (FLatency < __HAL_FLASH_GET_LATENCY())
  805. {
  806. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  807. __HAL_FLASH_SET_LATENCY(FLatency);
  808. /* Check that the new number of wait states is taken into account to access the Flash
  809. memory by polling the FLASH_ACR register */
  810. tickstart = HAL_GetTick();
  811. while ((FLASH->ACR & FLASH_ACR_LATENCY) != FLatency)
  812. {
  813. if ((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
  814. {
  815. return HAL_TIMEOUT;
  816. }
  817. }
  818. }
  819. /*-------------------------- PCLK1 Configuration ---------------------------*/
  820. if (((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
  821. {
  822. assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB1CLKDivider));
  823. MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE, RCC_ClkInitStruct->APB1CLKDivider);
  824. }
  825. /* Update the SystemCoreClock global variable */
  826. SystemCoreClock = (HAL_RCC_GetSysClockFreq() >> ((AHBPrescTable[(RCC->CFGR & RCC_CFGR_HPRE) >> RCC_CFGR_HPRE_Pos]) & 0x1FU));
  827. /* Configure the source of time base considering new system clocks settings*/
  828. return HAL_InitTick(uwTickPrio);
  829. }
  830. /**
  831. * @}
  832. */
  833. /** @defgroup RCC_Exported_Functions_Group2 Peripheral Control functions
  834. * @brief RCC clocks control functions
  835. *
  836. @verbatim
  837. ===============================================================================
  838. ##### Peripheral Control functions #####
  839. ===============================================================================
  840. [..]
  841. This subsection provides a set of functions allowing to:
  842. (+) Output clock to MCO pin.
  843. (+) Retrieve current clock frequencies.
  844. (+) Enable the Clock Security System.
  845. @endverbatim
  846. * @{
  847. */
  848. /**
  849. * @brief Select the clock source to output on MCO1 pin(PA8) or MC02 pin (PA10)(*).
  850. * @note PA8, PA10(*) should be configured in alternate function mode.
  851. * @param RCC_MCOx specifies the output direction for the clock source.
  852. * For STM32G0xx family this parameter can have only one value:
  853. * @arg @ref RCC_MCO_PA8 Clock source to output on MCO1 pin(PA8).
  854. * @arg @ref RCC_MCO_PA9 Clock source to output on MCO1 pin(PA9).
  855. * @arg @ref RCC_MCO_PD10 Clock source to output on MCO1 pin(PD10)(*).
  856. * @arg @ref RCC_MCO_PF2 Clock source to output on MCO1 pin(PF2)(*).
  857. * @arg @ref RCC_MCO_PA10 Clock source to output on MCO2 pin(PA10)(*).
  858. * @arg @ref RCC_MCO_PA15 Clock source to output on MCO2 pin(PA15)(*).
  859. * @arg @ref RCC_MCO_PB2 Clock source to output on MCO2 pin(PB2)(*).
  860. * @arg @ref RCC_MCO_PD7 Clock source to output on MCO2 pin(PD7)(*).
  861. * @param RCC_MCOSource specifies the clock source to output.
  862. * This parameter can be one of the following values:
  863. * @arg @ref RCC_MCO1SOURCE_NOCLOCK MCO output disabled, no clock on MCO
  864. * @arg @ref RCC_MCO1SOURCE_SYSCLK system clock selected as MCO source
  865. * @arg @ref RCC_MCO1SOURCE_HSI48 HSI48 clock selected as MCO source for devices with HSI48(*)
  866. * @arg @ref RCC_MCO1SOURCE_HSI HSI clock selected as MCO source
  867. * @arg @ref RCC_MCO1SOURCE_HSE HSE clock selected as MCO source
  868. * @arg @ref RCC_MCO1SOURCE_PLLCLK main PLLR clock selected as MCO source
  869. * @arg @ref RCC_MCO1SOURCE_LSI LSI clock selected as MCO source
  870. * @arg @ref RCC_MCO1SOURCE_LSE LSE clock selected as MCO source
  871. * @arg @ref RCC_MCO1SOURCE_PLLPCLK PLLP clock selected as MCO1 source(*)
  872. * @arg @ref RCC_MCO1SOURCE_PLLQCLK PLLQ clock selected as MCO1 source(*)
  873. * @arg @ref RCC_MCO1SOURCE_RTCCLK RTC clock selected as MCO1 source(*)
  874. * @arg @ref RCC_MCO1SOURCE_RTC_WKUP RTC_Wakeup selected as MCO1 source(*)
  875. * @arg @ref RCC_MCO2SOURCE_NOCLOCK MCO2 output disabled, no clock on MCO2(*)
  876. * @arg @ref RCC_MCO2SOURCE_SYSCLK system clock selected as MCO2 source(*)
  877. * @arg @ref RCC_MCO2SOURCE_HSI48 HSI48 clock selected as MCO2 source for devices with HSI48(*)
  878. * @arg @ref RCC_MCO2SOURCE_HSI HSI clock selected as MCO2 source(*)
  879. * @arg @ref RCC_MCO2SOURCE_HSE HSE clock selected as MCO2 source(*)
  880. * @arg @ref RCC_MCO2SOURCE_PLLCLK main PLLR clock selected as MCO2 source(*)
  881. * @arg @ref RCC_MCO2SOURCE_LSI LSI clock selected as MCO2 source(*)
  882. * @arg @ref RCC_MCO2SOURCE_LSE LSE clock selected as MCO2 source(*)
  883. * @arg @ref RCC_MCO2SOURCE_PLLPCLK PLLP clock selected as MCO2 source(*)
  884. * @arg @ref RCC_MCO2SOURCE_PLLQCLK PLLQ clock selected as MCO2 source(*)
  885. * @arg @ref RCC_MCO2SOURCE_RTCCLK RTC clock selected as MCO2 source(*)
  886. * @arg @ref RCC_MCO2SOURCE_RTC_WKUP RTC_Wakeup selected as MCO2 source(*)
  887. * @param RCC_MCODiv specifies the MCO prescaler.
  888. * This parameter can be one of the following values:
  889. * @arg @ref RCC_MCODIV_1 no division applied to MCO clock
  890. * @arg @ref RCC_MCODIV_2 division by 2 applied to MCO clock
  891. * @arg @ref RCC_MCODIV_4 division by 4 applied to MCO clock
  892. * @arg @ref RCC_MCODIV_8 division by 8 applied to MCO clock
  893. * @arg @ref RCC_MCODIV_16 division by 16 applied to MCO clock
  894. * @arg @ref RCC_MCODIV_32 division by 32 applied to MCO clock
  895. * @arg @ref RCC_MCODIV_64 division by 64 applied to MCO clock
  896. * @arg @ref RCC_MCODIV_128 division by 128 applied to MCO clock
  897. * @arg @ref RCC_MCO2DIV_1 no division applied to MCO2 clock(*)
  898. * @arg @ref RCC_MCO2DIV_2 division by 2 applied to MCO2 clock(*)
  899. * @arg @ref RCC_MCO2DIV_4 division by 4 applied to MCO2 clock(*)
  900. * @arg @ref RCC_MCO2DIV_8 division by 8 applied to MCO2 clock(*)
  901. * @arg @ref RCC_MCO2DIV_16 division by 16 applied to MCO2 clock(*)
  902. * @arg @ref RCC_MCO2DIV_32 division by 32 applied to MCO2 clock(*)
  903. * @arg @ref RCC_MCO2DIV_64 division by 64 applied to MCO2 clock(*)
  904. * @arg @ref RCC_MCO2DIV_128 division by 128 applied to MCO2 clock(*)
  905. * @arg @ref RCC_MCO2DIV_256 division by 256 applied to MCO2 clock(*)
  906. * @arg @ref RCC_MCO2DIV_512 division by 512 applied to MCO2 clock(*)
  907. * @arg @ref RCC_MCO2DIV_1024 division by 1024 applied to MCO2 clock(*)
  908. *
  909. * (*) Feature not available on all devices of the family
  910. * @retval None
  911. */
  912. void HAL_RCC_MCOConfig(uint32_t RCC_MCOx, uint32_t RCC_MCOSource, uint32_t RCC_MCODiv)
  913. {
  914. GPIO_InitTypeDef gpio_initstruct;
  915. uint32_t mcoindex;
  916. uint32_t mco_gpio_index;
  917. GPIO_TypeDef * mco_gpio_port;
  918. /* Check the parameters */
  919. assert_param(IS_RCC_MCO(RCC_MCOx));
  920. /* Common GPIO init parameters */
  921. gpio_initstruct.Mode = GPIO_MODE_AF_PP;
  922. gpio_initstruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  923. gpio_initstruct.Pull = GPIO_NOPULL;
  924. /* Get MCOx selection */
  925. mcoindex = RCC_MCOx & RCC_MCO_INDEX_MASK;
  926. /* Get MCOx GPIO Port */
  927. mco_gpio_port = (GPIO_TypeDef *) RCC_GET_MCO_GPIO_PORT(RCC_MCOx);
  928. /* MCOx Clock Enable */
  929. mco_gpio_index = RCC_GET_MCO_GPIO_INDEX(RCC_MCOx);
  930. SET_BIT(RCC->IOPENR, (1UL << mco_gpio_index ));
  931. /* Configure the MCOx pin in alternate function mode */
  932. gpio_initstruct.Pin = RCC_GET_MCO_GPIO_PIN(RCC_MCOx);
  933. gpio_initstruct.Alternate = RCC_GET_MCO_GPIO_AF(RCC_MCOx);
  934. HAL_GPIO_Init(mco_gpio_port, &gpio_initstruct);
  935. if (mcoindex == RCC_MCO1_INDEX)
  936. {
  937. assert_param(IS_RCC_MCODIV(RCC_MCODiv));
  938. assert_param(IS_RCC_MCO1SOURCE(RCC_MCOSource));
  939. /* Mask MCOSEL[] and MCOPRE[] bits then set MCO clock source and prescaler */
  940. MODIFY_REG(RCC->CFGR, (RCC_CFGR_MCOSEL | RCC_CFGR_MCOPRE), (RCC_MCOSource | RCC_MCODiv));
  941. }
  942. #if defined(RCC_MCO2_SUPPORT)
  943. else if (mcoindex == RCC_MCO2_INDEX)
  944. {
  945. assert_param(IS_RCC_MCO2DIV(RCC_MCODiv));
  946. assert_param(IS_RCC_MCO2SOURCE(RCC_MCOSource));
  947. /* Mask MCOSEL[] and MCOPRE[] bits then set MCO clock source and prescaler */
  948. MODIFY_REG(RCC->CFGR, (RCC_CFGR_MCO2SEL | RCC_CFGR_MCO2PRE), (RCC_MCOSource | RCC_MCODiv));
  949. }
  950. #endif /* RCC_MCO2_SUPPORT */
  951. else
  952. {
  953. /* Nothing to do */
  954. }
  955. }
  956. /**
  957. * @brief Return the SYSCLK frequency.
  958. *
  959. * @note The system frequency computed by this function is not the real
  960. * frequency in the chip. It is calculated based on the predefined
  961. * constant and the selected clock source:
  962. * @note If SYSCLK source is HSI, function returns values based on HSI_VALUE/HSIDIV(*)
  963. * @note If SYSCLK source is HSE, function returns values based on HSE_VALUE(**)
  964. * @note If SYSCLK source is PLL, function returns values based on HSE_VALUE(**),
  965. * or HSI_VALUE(*) multiplied/divided by the PLL factors.
  966. * @note If SYSCLK source is LSI, function returns values based on LSI_VALUE(***)
  967. * @note If SYSCLK source is LSE, function returns values based on LSE_VALUE(****)
  968. * @note (*) HSI_VALUE is a constant defined in stm32g0xx_hal_conf.h file (default value
  969. * 16 MHz) but the real value may vary depending on the variations
  970. * in voltage and temperature.
  971. * @note (**) HSE_VALUE is a constant defined in stm32g0xx_hal_conf.h file (default value
  972. * 8 MHz), user has to ensure that HSE_VALUE is same as the real
  973. * frequency of the crystal used. Otherwise, this function may
  974. * have wrong result.
  975. * @note (***) LSE_VALUE is a constant defined in stm32g0xx_hal_conf.h file (default value
  976. * 32768 Hz).
  977. * @note (****) LSI_VALUE is a constant defined in stm32g0xx_hal_conf.h file (default value
  978. * 32000 Hz).
  979. *
  980. * @note The result of this function could be not correct when using fractional
  981. * value for HSE crystal.
  982. *
  983. * @note This function can be used by the user application to compute the
  984. * baudrate for the communication peripherals or configure other parameters.
  985. *
  986. * @note Each time SYSCLK changes, this function must be called to update the
  987. * right SYSCLK value. Otherwise, any configuration based on this function will be incorrect.
  988. *
  989. *
  990. * @retval SYSCLK frequency
  991. */
  992. uint32_t HAL_RCC_GetSysClockFreq(void)
  993. {
  994. uint32_t pllvco, pllsource, pllr, pllm, hsidiv;
  995. uint32_t sysclockfreq;
  996. if (__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_HSI)
  997. {
  998. /* HSISYS can be derived for HSI16 */
  999. hsidiv = (1UL << ((READ_BIT(RCC->CR, RCC_CR_HSIDIV)) >> RCC_CR_HSIDIV_Pos));
  1000. /* HSI used as system clock source */
  1001. sysclockfreq = (HSI_VALUE / hsidiv);
  1002. }
  1003. else if (__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_HSE)
  1004. {
  1005. /* HSE used as system clock source */
  1006. sysclockfreq = HSE_VALUE;
  1007. }
  1008. else if (__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_PLLCLK)
  1009. {
  1010. /* PLL used as system clock source */
  1011. /* PLL_VCO = ((HSE_VALUE or HSI_VALUE)/ PLLM) * PLLN
  1012. SYSCLK = PLL_VCO / PLLR
  1013. */
  1014. pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC);
  1015. pllm = ((RCC->PLLCFGR & RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1U ;
  1016. switch (pllsource)
  1017. {
  1018. case RCC_PLLSOURCE_HSE: /* HSE used as PLL clock source */
  1019. pllvco = (HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos);
  1020. break;
  1021. case RCC_PLLSOURCE_HSI: /* HSI16 used as PLL clock source */
  1022. default: /* HSI16 used as PLL clock source */
  1023. pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos) ;
  1024. break;
  1025. }
  1026. pllr = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> RCC_PLLCFGR_PLLR_Pos) + 1U);
  1027. sysclockfreq = pllvco / pllr;
  1028. }
  1029. else if (__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_LSE)
  1030. {
  1031. /* LSE used as system clock source */
  1032. sysclockfreq = LSE_VALUE;
  1033. }
  1034. else if (__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_LSI)
  1035. {
  1036. /* LSI used as system clock source */
  1037. sysclockfreq = LSI_VALUE;
  1038. }
  1039. else
  1040. {
  1041. sysclockfreq = 0U;
  1042. }
  1043. return sysclockfreq;
  1044. }
  1045. /**
  1046. * @brief Return the HCLK frequency.
  1047. * @note Each time HCLK changes, this function must be called to update the
  1048. * right HCLK value. Otherwise, any configuration based on this function will be incorrect.
  1049. *
  1050. * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency.
  1051. * @retval HCLK frequency in Hz
  1052. */
  1053. uint32_t HAL_RCC_GetHCLKFreq(void)
  1054. {
  1055. return SystemCoreClock;
  1056. }
  1057. /**
  1058. * @brief Return the PCLK1 frequency.
  1059. * @note Each time PCLK1 changes, this function must be called to update the
  1060. * right PCLK1 value. Otherwise, any configuration based on this function will be incorrect.
  1061. * @retval PCLK1 frequency in Hz
  1062. */
  1063. uint32_t HAL_RCC_GetPCLK1Freq(void)
  1064. {
  1065. /* Get HCLK source and Compute PCLK1 frequency ---------------------------*/
  1066. return ((uint32_t)(__LL_RCC_CALC_PCLK1_FREQ(HAL_RCC_GetHCLKFreq(), LL_RCC_GetAPB1Prescaler())));
  1067. }
  1068. /**
  1069. * @brief Configure the RCC_OscInitStruct according to the internal
  1070. * RCC configuration registers.
  1071. * @param RCC_OscInitStruct pointer to an RCC_OscInitTypeDef structure that
  1072. * will be configured.
  1073. * @retval None
  1074. */
  1075. void HAL_RCC_GetOscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  1076. {
  1077. /* Check the parameters */
  1078. assert_param(RCC_OscInitStruct != (void *)NULL);
  1079. /* Set all possible values for the Oscillator type parameter ---------------*/
  1080. #if defined(RCC_HSI48_SUPPORT)
  1081. RCC_OscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | \
  1082. RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_HSI48;
  1083. #else
  1084. RCC_OscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | \
  1085. RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_LSI;
  1086. #endif /* RCC_HSI48_SUPPORT */
  1087. /* Get the HSE configuration -----------------------------------------------*/
  1088. if ((RCC->CR & RCC_CR_HSEBYP) == RCC_CR_HSEBYP)
  1089. {
  1090. RCC_OscInitStruct->HSEState = RCC_HSE_BYPASS;
  1091. }
  1092. else if ((RCC->CR & RCC_CR_HSEON) == RCC_CR_HSEON)
  1093. {
  1094. RCC_OscInitStruct->HSEState = RCC_HSE_ON;
  1095. }
  1096. else
  1097. {
  1098. RCC_OscInitStruct->HSEState = RCC_HSE_OFF;
  1099. }
  1100. /* Get the HSI configuration -----------------------------------------------*/
  1101. if ((RCC->CR & RCC_CR_HSION) == RCC_CR_HSION)
  1102. {
  1103. RCC_OscInitStruct->HSIState = RCC_HSI_ON;
  1104. }
  1105. else
  1106. {
  1107. RCC_OscInitStruct->HSIState = RCC_HSI_OFF;
  1108. }
  1109. RCC_OscInitStruct->HSICalibrationValue = ((RCC->ICSCR & RCC_ICSCR_HSITRIM) >> RCC_ICSCR_HSITRIM_Pos);
  1110. RCC_OscInitStruct->HSIDiv = (RCC->CR & RCC_CR_HSIDIV);
  1111. /* Get the LSE configuration -----------------------------------------------*/
  1112. if ((RCC->BDCR & RCC_BDCR_LSEBYP) == RCC_BDCR_LSEBYP)
  1113. {
  1114. RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS;
  1115. }
  1116. else if ((RCC->BDCR & RCC_BDCR_LSEON) == RCC_BDCR_LSEON)
  1117. {
  1118. RCC_OscInitStruct->LSEState = RCC_LSE_ON;
  1119. }
  1120. else
  1121. {
  1122. RCC_OscInitStruct->LSEState = RCC_LSE_OFF;
  1123. }
  1124. /* Get the LSI configuration -----------------------------------------------*/
  1125. if ((RCC->CSR & RCC_CSR_LSION) == RCC_CSR_LSION)
  1126. {
  1127. RCC_OscInitStruct->LSIState = RCC_LSI_ON;
  1128. }
  1129. else
  1130. {
  1131. RCC_OscInitStruct->LSIState = RCC_LSI_OFF;
  1132. }
  1133. #if defined(RCC_HSI48_SUPPORT)
  1134. /* Get the HSI48 configuration ---------------------------------------------*/
  1135. if (READ_BIT(RCC->CR, RCC_CR_HSI48ON) == RCC_CR_HSI48ON)
  1136. {
  1137. RCC_OscInitStruct->HSI48State = RCC_HSI48_ON;
  1138. }
  1139. else
  1140. {
  1141. RCC_OscInitStruct->HSI48State = RCC_HSI48_OFF;
  1142. }
  1143. #endif /* RCC_HSI48_SUPPORT */
  1144. /* Get the PLL configuration -----------------------------------------------*/
  1145. if ((RCC->CR & RCC_CR_PLLON) == RCC_CR_PLLON)
  1146. {
  1147. RCC_OscInitStruct->PLL.PLLState = RCC_PLL_ON;
  1148. }
  1149. else
  1150. {
  1151. RCC_OscInitStruct->PLL.PLLState = RCC_PLL_OFF;
  1152. }
  1153. RCC_OscInitStruct->PLL.PLLSource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC);
  1154. RCC_OscInitStruct->PLL.PLLM = (RCC->PLLCFGR & RCC_PLLCFGR_PLLM);
  1155. RCC_OscInitStruct->PLL.PLLN = ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos);
  1156. RCC_OscInitStruct->PLL.PLLP = (RCC->PLLCFGR & RCC_PLLCFGR_PLLP);
  1157. #if defined(RCC_PLLQ_SUPPORT)
  1158. RCC_OscInitStruct->PLL.PLLQ = (RCC->PLLCFGR & RCC_PLLCFGR_PLLQ);
  1159. #endif /* RCC_PLLQ_SUPPORT */
  1160. RCC_OscInitStruct->PLL.PLLR = (RCC->PLLCFGR & RCC_PLLCFGR_PLLR);
  1161. }
  1162. /**
  1163. * @brief Configure the RCC_ClkInitStruct according to the internal
  1164. * RCC configuration registers.
  1165. * @param RCC_ClkInitStruct Pointer to a @ref RCC_ClkInitTypeDef structure that
  1166. * will be configured.
  1167. * @param pFLatency Pointer on the Flash Latency.
  1168. * @retval None
  1169. */
  1170. void HAL_RCC_GetClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t *pFLatency)
  1171. {
  1172. /* Check the parameters */
  1173. assert_param(RCC_ClkInitStruct != (void *)NULL);
  1174. assert_param(pFLatency != (void *)NULL);
  1175. /* Set all possible values for the Clock type parameter --------------------*/
  1176. RCC_ClkInitStruct->ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1;
  1177. /* Get the SYSCLK configuration --------------------------------------------*/
  1178. RCC_ClkInitStruct->SYSCLKSource = (uint32_t)(RCC->CFGR & RCC_CFGR_SW);
  1179. /* Get the HCLK configuration ----------------------------------------------*/
  1180. RCC_ClkInitStruct->AHBCLKDivider = (uint32_t)(RCC->CFGR & RCC_CFGR_HPRE);
  1181. /* Get the APB1 configuration ----------------------------------------------*/
  1182. RCC_ClkInitStruct->APB1CLKDivider = (uint32_t)(RCC->CFGR & RCC_CFGR_PPRE);
  1183. /* Get the Flash Wait State (Latency) configuration ------------------------*/
  1184. *pFLatency = (uint32_t)(FLASH->ACR & FLASH_ACR_LATENCY);
  1185. }
  1186. /**
  1187. * @brief Enable the Clock Security System.
  1188. * @note If a failure is detected on the HSE oscillator clock, this oscillator
  1189. * is automatically disabled and an interrupt is generated to inform the
  1190. * software about the failure (Clock Security System Interrupt, CSSI),
  1191. * allowing the MCU to perform rescue operations. The CSSI is linked to
  1192. * the Cortex-M0+ NMI (Non-Maskable Interrupt) exception vector.
  1193. * @note The Clock Security System can only be cleared by reset.
  1194. * @retval None
  1195. */
  1196. void HAL_RCC_EnableCSS(void)
  1197. {
  1198. SET_BIT(RCC->CR, RCC_CR_CSSON) ;
  1199. }
  1200. /**
  1201. * @brief Enable the LSE Clock Security System.
  1202. * @note If a failure is detected on the LSE oscillator clock, this oscillator
  1203. * is automatically disabled and an interrupt is generated to inform the
  1204. * software about the failure (Clock Security System Interrupt, CSSI),
  1205. * allowing the MCU to perform rescue operations. The CSSI is linked to
  1206. * the Cortex-M0+ NMI (Non-Maskable Interrupt) exception vector.
  1207. * @note The LSE Clock Security System Detection bit (LSECSSD in BDCR) can only be
  1208. * cleared by a backup domain reset.
  1209. * @retval None
  1210. */
  1211. void HAL_RCC_EnableLSECSS(void)
  1212. {
  1213. SET_BIT(RCC->BDCR, RCC_BDCR_LSECSSON) ;
  1214. }
  1215. /**
  1216. * @brief Disable the LSE Clock Security System.
  1217. * @note After LSE failure detection, the software must disable LSECSSON
  1218. * @note The Clock Security System can only be cleared by reset otherwise.
  1219. * @retval None
  1220. */
  1221. void HAL_RCC_DisableLSECSS(void)
  1222. {
  1223. CLEAR_BIT(RCC->BDCR, RCC_BDCR_LSECSSON) ;
  1224. }
  1225. /**
  1226. * @brief Handle the RCC Clock Security System interrupt request.
  1227. * @note This API should be called under the NMI_Handler().
  1228. * @retval None
  1229. */
  1230. void HAL_RCC_NMI_IRQHandler(void)
  1231. {
  1232. uint32_t itflag = RCC->CIFR;
  1233. /* Clear interrupt flags related to CSS */
  1234. RCC->CICR = (itflag & (RCC_CIFR_CSSF | RCC_CIFR_LSECSSF));
  1235. /* Check RCC CSSF interrupt flag */
  1236. if ((itflag & RCC_CIFR_CSSF) != 0x00u)
  1237. {
  1238. /* RCC Clock Security System interrupt user callback */
  1239. HAL_RCC_CSSCallback();
  1240. }
  1241. /* Check RCC LSECSSF interrupt flag */
  1242. if ((itflag & RCC_CIFR_LSECSSF) != 0x00u)
  1243. {
  1244. /* RCC Clock Security System interrupt user callback */
  1245. HAL_RCC_LSECSSCallback();
  1246. }
  1247. }
  1248. /**
  1249. * @brief Handle the RCC HSE Clock Security System interrupt callback.
  1250. * @retval none
  1251. */
  1252. __weak void HAL_RCC_CSSCallback(void)
  1253. {
  1254. /* NOTE : This function should not be modified, when the callback is needed,
  1255. the @ref HAL_RCC_CSSCallback should be implemented in the user file
  1256. */
  1257. }
  1258. /**
  1259. * @brief RCC LSE Clock Security System interrupt callback.
  1260. * @retval none
  1261. */
  1262. __weak void HAL_RCC_LSECSSCallback(void)
  1263. {
  1264. /* NOTE : This function should not be modified, when the callback is needed,
  1265. the HAL_RCC_LSECSSCallback should be implemented in the user file
  1266. */
  1267. }
  1268. /**
  1269. * @brief Get and clear reset flags
  1270. * @note Once reset flags are retrieved, this API is clearing them in order
  1271. * to isolate next reset reason.
  1272. * @retval can be a combination of @ref RCC_Reset_Flag
  1273. */
  1274. uint32_t HAL_RCC_GetResetSource(void)
  1275. {
  1276. uint32_t reset;
  1277. /* Get all reset flags */
  1278. reset = RCC->CSR & RCC_RESET_FLAG_ALL;
  1279. /* Clear Reset flags */
  1280. RCC->CSR |= RCC_CSR_RMVF;
  1281. return reset;
  1282. }
  1283. /**
  1284. * @}
  1285. */
  1286. /**
  1287. * @}
  1288. */
  1289. #endif /* HAL_RCC_MODULE_ENABLED */
  1290. /**
  1291. * @}
  1292. */
  1293. /**
  1294. * @}
  1295. */