py32f002b_hal_rcc.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  1. /**
  2. ******************************************************************************
  3. * @file py32f002b_hal_rcc.c
  4. * @author MCU 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 4 MHz to reach 24MHz) 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) busses:
  21. all peripherals mapped on these busses 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 busses 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. * <h2><center>&copy; Copyright (c) 2023 Puya Semiconductor Co.
  39. * All rights reserved.</center></h2>
  40. *
  41. * This software component is licensed by Puya under BSD 3-Clause license,
  42. * the "License"; You may not use this file except in compliance with the
  43. * License. You may obtain a copy of the License at:
  44. * opensource.org/licenses/BSD-3-Clause
  45. *
  46. ******************************************************************************
  47. * @attention
  48. *
  49. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  50. * All rights reserved.</center></h2>
  51. *
  52. * This software component is licensed by ST under BSD 3-Clause license,
  53. * the "License"; You may not use this file except in compliance with the
  54. * License. You may obtain a copy of the License at:
  55. * opensource.org/licenses/BSD-3-Clause
  56. *
  57. ******************************************************************************
  58. */
  59. /* Includes ------------------------------------------------------------------*/
  60. #include "py32f0xx_hal.h"
  61. /** @addtogroup PY32F002B_HAL_Driver
  62. * @{
  63. */
  64. /** @defgroup RCC RCC
  65. * @brief RCC HAL module driver
  66. * @{
  67. */
  68. #ifdef HAL_RCC_MODULE_ENABLED
  69. /* Private typedef -----------------------------------------------------------*/
  70. /* Private define ------------------------------------------------------------*/
  71. /** @defgroup RCC_Private_Constants RCC Private Constants
  72. * @{
  73. */
  74. #define CLOCKSWITCH_TIMEOUT_VALUE (5000U) /* 5 s */
  75. #define HSI_TIMEOUT_VALUE (2U) /* 2 ms (minimum Tick + 1) */
  76. #define LSI_TIMEOUT_VALUE (2U) /* 2 ms (minimum Tick + 1) */
  77. #define AHB_24MHZ (24000000U)
  78. /**
  79. * @}
  80. */
  81. /* Private macro -------------------------------------------------------------*/
  82. /* Private variables ---------------------------------------------------------*/
  83. /** @defgroup RCC_Private_Variables RCC Private Variables
  84. * @{
  85. */
  86. /**
  87. * @}
  88. */
  89. /* Private function prototypes -----------------------------------------------*/
  90. /* Exported functions --------------------------------------------------------*/
  91. /** @defgroup RCC_Exported_Functions RCC Exported Functions
  92. * @{
  93. */
  94. /** @defgroup RCC_Exported_Functions_Group1 Initialization and de-initialization functions
  95. * @brief Initialization and Configuration functions
  96. *
  97. @verbatim
  98. ===============================================================================
  99. ##### Initialization and de-initialization functions #####
  100. ===============================================================================
  101. [..]
  102. This section provides functions allowing to configure the internal and external oscillators
  103. (HSE, HSI, LSE, LSI, CSS and MCO) and the System busses clocks (SYSCLK, AHB, APB)
  104. [..] Internal/external clock and PLL configuration
  105. (+) HSI (high-speed internal): 8 MHz factory-trimmed RC used directly or through
  106. the PLL as System clock source.
  107. (+) LSI (low-speed internal): 32 KHz low consumption RC used as IWDG and/or RTC
  108. clock source.
  109. (+) HSE (high-speed external): 4 to 48 MHz crystal oscillator used directly or
  110. through the PLL as System clock source. Can be used also optionally as RTC clock source.
  111. (+) LSE (low-speed external): 32.768 KHz oscillator used optionally as RTC clock source.
  112. (+) PLL (clocked by HSI, HSE) providing up to three independent output clocks:
  113. (+) CSS (Clock security system): once enabled, if a HSE clock failure occurs
  114. (HSE used directly or through PLL as System clock source), the System clock
  115. is automatically switched respectively to HSI or LSI and an interrupt is generated
  116. if enabled. The interrupt is linked to the Cortex-M0+ NMI (Non-Maskable Interrupt)
  117. exception vector.
  118. (+) MCO (microcontroller clock output): used to output LSI, HSI, LSE, HSE or
  119. main PLL clock (through a configurable prescaler) on PA8 pin.
  120. [..] System, AHB and APB busses clocks configuration
  121. (+) Several clock sources can be used to drive the System clock (SYSCLK): HSI,
  122. HSE, LSI, LSE and main PLL.
  123. The AHB clock (HCLK) is derived from System clock through configurable
  124. prescaler and used to clock the CPU, memory and peripherals mapped
  125. on AHB bus (DMA, GPIO...).and APB (PCLK1) clock is derived
  126. from AHB clock through configurable prescalers and used to clock
  127. the peripherals mapped on these busses. You can use
  128. "@ref HAL_RCC_GetSysClockFreq()" function to retrieve the frequencies of these clocks.
  129. -@- All the peripheral clocks are derived from the System clock (SYSCLK) except:
  130. (+@) RTC: the RTC clock can be derived either from the LSI, LSE or HSE clock
  131. divided by 2 to 31.
  132. You have to use @ref __HAL_RCC_RTC_ENABLE() and @ref HAL_RCCEx_PeriphCLKConfig() function
  133. to configure this clock.
  134. (+@) RNG(*) requires a frequency equal or lower than 48 MHz.
  135. This clock is derived from the main PLL or HSI or System clock.
  136. (*) available on certain devices only
  137. (+@) IWDG clock which is always the LSI clock.
  138. (+) The maximum frequency of the SYSCLK, HCLK, PCLK is 48 MHz.
  139. Depending on the device voltage range, the maximum frequency should be
  140. adapted accordingly.
  141. @endverbatim
  142. * @{
  143. */
  144. /**
  145. * @brief Reset the RCC clock configuration to the default reset state.
  146. * @note The default reset state of the clock configuration is given below:
  147. * - HSI ON and used as system clock source
  148. * - HSE OFF
  149. * - AHB and APB prescaler set to 1.
  150. * - CSS, MCO1 OFF
  151. * - All interrupts disabled
  152. * @note This function does not modify the configuration of the
  153. * - Peripheral clocks
  154. * - LSI, LSE and RTC clocks
  155. * @retval HAL status
  156. */
  157. HAL_StatusTypeDef HAL_RCC_DeInit(void)
  158. {
  159. uint32_t tickstart;
  160. /* Get Start Tick*/
  161. tickstart = HAL_GetTick();
  162. /* Set HSION bit to the reset value */
  163. SET_BIT(RCC->CR, RCC_CR_HSION);
  164. /* Wait till HSI is ready */
  165. while (READ_BIT(RCC->CR, RCC_CR_HSIRDY) == 0U)
  166. {
  167. if ((HAL_GetTick() - tickstart) > HSI_TIMEOUT_VALUE)
  168. {
  169. return HAL_TIMEOUT;
  170. }
  171. }
  172. /* Set ICSCR to the reset value */
  173. RCC->ICSCR = 0x00FF10FF;
  174. /* Get Start Tick*/
  175. tickstart = HAL_GetTick();
  176. /* Reset CFGR register (HSI is selected as system clock source) */
  177. RCC->CFGR = 0x00000000u;
  178. /* Wait till HSI is ready */
  179. while (READ_BIT(RCC->CFGR, RCC_CFGR_SWS) != 0U)
  180. {
  181. if ((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
  182. {
  183. return HAL_TIMEOUT;
  184. }
  185. }
  186. /* Clear CR register in 2 steps: first to clear HSEON in case bypass was enabled */
  187. RCC->CR = RCC_CR_HSION;
  188. /* Then again to HSEBYP in case bypass was enabled */
  189. RCC->CR = RCC_CR_HSION;
  190. /* Disable all interrupts */
  191. RCC->CIER = 0x00000000u;
  192. /* Clear all flags */
  193. RCC->CICR = 0xFFFFFFFFu;
  194. /* Update the SystemCoreClock global variable */
  195. SystemCoreClock = HSI_VALUE;
  196. /* Adapt Systick interrupt period */
  197. if (HAL_InitTick(uwTickPrio) != HAL_OK)
  198. {
  199. return HAL_ERROR;
  200. }
  201. else
  202. {
  203. return HAL_OK;
  204. }
  205. }
  206. /**
  207. * @brief Initialize the RCC Oscillators according to the specified parameters in the
  208. * @ref RCC_OscInitTypeDef.
  209. * @param RCC_OscInitStruct pointer to a @ref RCC_OscInitTypeDef structure that
  210. * contains the configuration information for the RCC Oscillators.
  211. * @note Transition HSE Bypass to HSE On and HSE On to HSE Bypass are not
  212. * supported by this function. User should request a transition to HSE Off
  213. * first and then to HSE On or HSE Bypass.
  214. * @note Transition LSE Bypass to LSE On and LSE On to LSE Bypass are not
  215. * supported by this function. User should request a transition to LSE Off
  216. * first and then to LSE On or LSE Bypass.
  217. * @note Depending on devices and packages, some clocks may not be available.
  218. * Refer to device datasheet for clocks availability.
  219. * @retval HAL status
  220. */
  221. HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  222. {
  223. uint32_t tickstart;
  224. uint32_t temp_sysclksrc;
  225. /* Check Null pointer */
  226. if (RCC_OscInitStruct == NULL)
  227. {
  228. return HAL_ERROR;
  229. }
  230. /* Check the parameters */
  231. assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct->OscillatorType));
  232. /*------------------------------- HSE Configuration ------------------------*/
  233. if (((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE)
  234. {
  235. /* Check the parameters */
  236. assert_param(IS_RCC_HSE(RCC_OscInitStruct->HSEState));
  237. temp_sysclksrc = __HAL_RCC_GET_SYSCLK_SOURCE();
  238. if (temp_sysclksrc == RCC_CFGR_SWS_HSE)
  239. {
  240. if (RCC_OscInitStruct->HSEState == RCC_HSE_BYPASS_DISABLE)
  241. {
  242. return HAL_ERROR;
  243. }
  244. }
  245. else
  246. {
  247. /* Set the new HSE configuration ---------------------------------------*/
  248. __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct->HSEState);
  249. }
  250. }
  251. /*----------------------------- HSI Configuration --------------------------*/
  252. if (((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI)
  253. {
  254. /* Check the parameters */
  255. assert_param(IS_RCC_HSI(RCC_OscInitStruct->HSIState));
  256. assert_param(IS_RCC_HSI_CALIBRATION_VALUE(RCC_OscInitStruct->HSICalibrationValue));
  257. assert_param(IS_RCC_HSIDIV(RCC_OscInitStruct->HSIDiv));
  258. /* Check if HSI is used as system clock */
  259. temp_sysclksrc = __HAL_RCC_GET_SYSCLK_SOURCE();
  260. if (temp_sysclksrc == RCC_CFGR_SWS_HSISYS)
  261. {
  262. /* When HSI is used as system clock it can not be disabled */
  263. if ((READ_BIT(RCC->CR, RCC_CR_HSIRDY) != 0U) && (RCC_OscInitStruct->HSIState == RCC_HSI_OFF))
  264. {
  265. return HAL_ERROR;
  266. }
  267. /* Otherwise, just the calibration is allowed */
  268. else
  269. {
  270. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  271. __HAL_FLASH_SET_LATENCY(FLASH_LATENCY_1);
  272. /* Adjust the HSI division factor */
  273. __HAL_RCC_HSI_CONFIG(RCC_OscInitStruct->HSIDiv);
  274. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  275. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  276. /* Get Start Tick*/
  277. tickstart = HAL_GetTick();
  278. /* Wait till HSI is ready */
  279. while (READ_BIT(RCC->CR, RCC_CR_HSIRDY) == 0U)
  280. {
  281. if ((HAL_GetTick() - tickstart) > HSI_TIMEOUT_VALUE)
  282. {
  283. return HAL_TIMEOUT;
  284. }
  285. }
  286. /* Update the SystemCoreClock global variable with HSISYS value */
  287. SystemCoreClock = (HAL_RCC_GetSysClockFreq() >> ((AHBPrescTable[(RCC->CFGR & RCC_CFGR_HPRE) >> RCC_CFGR_HPRE_Pos]) & 0x1FU));
  288. if(SystemCoreClock <= AHB_24MHZ)
  289. {
  290. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  291. __HAL_FLASH_SET_LATENCY(FLASH_LATENCY_0);
  292. }
  293. /* Adapt Systick interrupt period */
  294. if (HAL_InitTick(uwTickPrio) != HAL_OK)
  295. {
  296. return HAL_ERROR;
  297. }
  298. }
  299. }
  300. else
  301. {
  302. /* Check the HSI State */
  303. if (RCC_OscInitStruct->HSIState != RCC_HSI_OFF)
  304. {
  305. /* Configure the HSI division factor */
  306. __HAL_RCC_HSI_CONFIG(RCC_OscInitStruct->HSIDiv);
  307. /* Enable the Internal High Speed oscillator (HSI). */
  308. __HAL_RCC_HSI_ENABLE();
  309. /* Get Start Tick*/
  310. tickstart = HAL_GetTick();
  311. /* Wait till HSI is ready */
  312. while (READ_BIT(RCC->CR, RCC_CR_HSIRDY) == 0U)
  313. {
  314. if ((HAL_GetTick() - tickstart) > HSI_TIMEOUT_VALUE)
  315. {
  316. return HAL_TIMEOUT;
  317. }
  318. }
  319. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  320. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  321. }
  322. else
  323. {
  324. /* Disable the Internal High Speed oscillator (HSI). */
  325. __HAL_RCC_HSI_DISABLE();
  326. /* Get Start Tick*/
  327. tickstart = HAL_GetTick();
  328. /* Wait till HSI is disabled */
  329. while (READ_BIT(RCC->CR, RCC_CR_HSIRDY) != 0U)
  330. {
  331. if ((HAL_GetTick() - tickstart) > HSI_TIMEOUT_VALUE)
  332. {
  333. return HAL_TIMEOUT;
  334. }
  335. }
  336. }
  337. }
  338. }
  339. /*------------------------------ LSI Configuration -------------------------*/
  340. if (((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI)
  341. {
  342. /* Check the parameters */
  343. assert_param(IS_RCC_LSI(RCC_OscInitStruct->LSIState));
  344. /* Check if LSI is used as system clock */
  345. if (__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_LSI)
  346. {
  347. /* When LSI is used as system clock it will not be disabled */
  348. if ((((RCC->CSR) & RCC_CSR_LSIRDY) != 0U) && (RCC_OscInitStruct->LSIState == RCC_LSI_OFF))
  349. {
  350. return HAL_ERROR;
  351. }
  352. else
  353. {
  354. /* Adjusts the Internal Low Speed oscillator (LSI) calibration value.*/
  355. __HAL_RCC_LSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->LSICalibrationValue);
  356. /* Get Start Tick*/
  357. tickstart = HAL_GetTick();
  358. /* Wait till LSI is ready */
  359. while (READ_BIT(RCC->CSR, RCC_CSR_LSIRDY) == 0U)
  360. {
  361. if ((HAL_GetTick() - tickstart) > LSI_TIMEOUT_VALUE)
  362. {
  363. return HAL_TIMEOUT;
  364. }
  365. }
  366. /* Update the SystemCoreClock global variable with LSI value */
  367. SystemCoreClock = (HAL_RCC_GetSysClockFreq() >> ((AHBPrescTable[(RCC->CFGR & RCC_CFGR_HPRE) >> RCC_CFGR_HPRE_Pos]) & 0x1FU));
  368. /* Adapt Systick interrupt period */
  369. if (HAL_InitTick(uwTickPrio) != HAL_OK)
  370. {
  371. return HAL_ERROR;
  372. }
  373. }
  374. }
  375. else
  376. {
  377. /* Check the LSI State */
  378. if (RCC_OscInitStruct->LSIState != RCC_LSI_OFF)
  379. {
  380. /* Enable the Internal Low Speed oscillator (LSI). */
  381. __HAL_RCC_LSI_ENABLE();
  382. /* Get Start Tick*/
  383. tickstart = HAL_GetTick();
  384. /* Wait till LSI is ready */
  385. while (READ_BIT(RCC->CSR, RCC_CSR_LSIRDY) == 0U)
  386. {
  387. if ((HAL_GetTick() - tickstart) > LSI_TIMEOUT_VALUE)
  388. {
  389. return HAL_TIMEOUT;
  390. }
  391. }
  392. /* Adjusts the Internal Low Speed oscillator (LSI) calibration value.*/
  393. __HAL_RCC_LSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->LSICalibrationValue);
  394. }
  395. else
  396. {
  397. /* Disable the Internal Low Speed oscillator (LSI). */
  398. __HAL_RCC_LSI_DISABLE();
  399. /* Get Start Tick*/
  400. tickstart = HAL_GetTick();
  401. /* Wait till LSI is disabled */
  402. while (READ_BIT(RCC->CSR, RCC_CSR_LSIRDY) != 0U)
  403. {
  404. if ((HAL_GetTick() - tickstart) > LSI_TIMEOUT_VALUE)
  405. {
  406. return HAL_TIMEOUT;
  407. }
  408. }
  409. }
  410. }
  411. }
  412. /*------------------------------ LSE Configuration -------------------------*/
  413. if (((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE)
  414. {
  415. /* Check the parameters */
  416. assert_param(IS_RCC_LSE(RCC_OscInitStruct->LSEState));
  417. /* When the LSE is used as system clock, it is not allowed disable it */
  418. if (__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_LSE)
  419. {
  420. if ((((RCC->BDCR) & RCC_BDCR_LSERDY) != 0U) && (RCC_OscInitStruct->LSEState == RCC_LSE_OFF))
  421. {
  422. return HAL_ERROR;
  423. }
  424. }
  425. else
  426. {
  427. /* Set driver factor of the LSE*/
  428. if (RCC_OscInitStruct->LSEState != RCC_LSE_OFF)
  429. {
  430. MODIFY_REG(RCC->ECSCR, RCC_ECSCR_LSE_DRIVER_Msk, RCC_OscInitStruct->LSEDriver);
  431. }
  432. /* Set the new LSE configuration -----------------------------------------*/
  433. __HAL_RCC_LSE_CONFIG(RCC_OscInitStruct->LSEState);
  434. /* Check the LSE State */
  435. if (RCC_OscInitStruct->LSEState != RCC_LSE_OFF)
  436. {
  437. #ifndef RCC_NO_DETECT_LSE_READY
  438. /* Get Start Tick*/
  439. tickstart = HAL_GetTick();
  440. /* Wait till LSE is ready */
  441. while (READ_BIT(RCC->BDCR, RCC_BDCR_LSERDY) == 0U)
  442. {
  443. if ((HAL_GetTick() - tickstart) > RCC_LSE_TIMEOUT_VALUE)
  444. {
  445. return HAL_TIMEOUT;
  446. }
  447. }
  448. #endif
  449. }
  450. else
  451. {
  452. /* Get Start Tick*/
  453. tickstart = HAL_GetTick();
  454. /* Wait till LSE is disabled */
  455. while (READ_BIT(RCC->BDCR, RCC_BDCR_LSERDY) != 0U)
  456. {
  457. if ((HAL_GetTick() - tickstart) > RCC_LSE_TIMEOUT_VALUE)
  458. {
  459. return HAL_TIMEOUT;
  460. }
  461. }
  462. }
  463. }
  464. }
  465. return HAL_OK;
  466. }
  467. /**
  468. * @brief Initialize the CPU, AHB and APB busses clocks according to the specified
  469. * parameters in the RCC_ClkInitStruct.
  470. * @param RCC_ClkInitStruct pointer to a @ref RCC_ClkInitTypeDef structure that
  471. * contains the configuration information for the RCC peripheral.
  472. * @param FLatency FLASH Latency
  473. * This parameter can be one of the following values:
  474. * @arg FLASH_LATENCY_0 FLASH 0 Latency cycle
  475. * @arg FLASH_LATENCY_1 FLASH 1 Latency cycle
  476. *
  477. * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency
  478. * and updated by @ref HAL_RCC_GetHCLKFreq() function called within this function
  479. *
  480. * @note The HSI is used by default as system clock source after
  481. * startup from Reset, wake-up from STANDBY mode. After restart from Reset,
  482. * the HSI frequency is set to 4 MHz, then it reaches its default value 8 MHz.
  483. *
  484. * @note The HSI can be selected as system clock source after
  485. * from STOP modes or in case of failure of the HSE used directly or indirectly
  486. * as system clock (if the Clock Security System CSS is enabled).
  487. *
  488. * @note The LSI can be selected as system clock source after
  489. * in case of failure of the LSE used directly or indirectly
  490. * as system clock (if the Clock Security System LSECSS is enabled).
  491. *
  492. * @note A switch from one clock source to another occurs only if the target
  493. * clock source is ready (clock stable after startup delay).
  494. * If a clock source which is not yet ready is selected, the switch will
  495. * occur when the clock source is ready.
  496. *
  497. * @note You can use @ref HAL_RCC_GetClockConfig() function to know which clock is
  498. * currently used as system clock source.
  499. *
  500. * @note Depending on the device voltage range, the software has to set correctly
  501. * HPRE[3:0] bits to ensure that HCLK not exceed the maximum allowed frequency
  502. * (for more details refer to section above "Initialization/de-initialization functions")
  503. * @retval None
  504. */
  505. HAL_StatusTypeDef HAL_RCC_ClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t FLatency)
  506. {
  507. uint32_t tickstart;
  508. /* Check Null pointer */
  509. if (RCC_ClkInitStruct == NULL)
  510. {
  511. return HAL_ERROR;
  512. }
  513. /* Check the parameters */
  514. assert_param(IS_RCC_CLOCKTYPE(RCC_ClkInitStruct->ClockType));
  515. assert_param(IS_FLASH_LATENCY(FLatency));
  516. /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
  517. must be correctly programmed according to the frequency of the FLASH clock
  518. (HCLK) and the supply voltage of the device. */
  519. /* Increasing the number of wait states because of higher CPU frequency */
  520. if (FLatency > __HAL_FLASH_GET_LATENCY())
  521. {
  522. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  523. __HAL_FLASH_SET_LATENCY(FLatency);
  524. /* Check that the new number of wait states is taken into account to access the Flash
  525. memory by polling the FLASH_ACR register */
  526. tickstart = HAL_GetTick();
  527. while ((FLASH->ACR & FLASH_ACR_LATENCY) != FLatency)
  528. {
  529. if ((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
  530. {
  531. return HAL_TIMEOUT;
  532. }
  533. }
  534. }
  535. /*-------------------------- HCLK Configuration --------------------------*/
  536. if (((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)
  537. {
  538. /* Set the highest APB divider in order to ensure that we do not go through
  539. a non-spec phase whatever we decrease or increase HCLK. */
  540. if (((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
  541. {
  542. MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE, RCC_HCLK_DIV16);
  543. }
  544. /* Set the new HCLK clock divider */
  545. assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
  546. MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
  547. }
  548. /*------------------------- SYSCLK Configuration ---------------------------*/
  549. if (((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_SYSCLK) == RCC_CLOCKTYPE_SYSCLK)
  550. {
  551. assert_param(IS_RCC_SYSCLKSOURCE(RCC_ClkInitStruct->SYSCLKSource));
  552. /* HSE is selected as System Clock Source */
  553. if (RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
  554. {
  555. /* Check the HSE Enable */
  556. if (READ_BIT(RCC->CR, RCC_CR_HSEEN) == 0U)
  557. {
  558. return HAL_ERROR;
  559. }
  560. }
  561. /* HSI is selected as System Clock Source */
  562. else if (RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSISYS)
  563. {
  564. /* Check the HSI ready flag */
  565. if (READ_BIT(RCC->CR, RCC_CR_HSIRDY) == 0U)
  566. {
  567. return HAL_ERROR;
  568. }
  569. }
  570. /* LSE is selected as System Clock Source */
  571. else if (RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_LSE)
  572. {
  573. /* Check the LSE ready flag */
  574. if (READ_BIT(RCC->BDCR, RCC_BDCR_LSERDY) == 0U)
  575. {
  576. return HAL_ERROR;
  577. }
  578. }
  579. /* LSI is selected as System Clock Source */
  580. else
  581. {
  582. /* Check the LSI ready flag */
  583. if (READ_BIT(RCC->CSR, RCC_CSR_LSIRDY) == 0U)
  584. {
  585. return HAL_ERROR;
  586. }
  587. }
  588. MODIFY_REG(RCC->CFGR, RCC_CFGR_SW, RCC_ClkInitStruct->SYSCLKSource);
  589. /* Get Start Tick*/
  590. tickstart = HAL_GetTick();
  591. while (__HAL_RCC_GET_SYSCLK_SOURCE() != (RCC_ClkInitStruct->SYSCLKSource << RCC_CFGR_SWS_Pos))
  592. {
  593. if ((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
  594. {
  595. return HAL_TIMEOUT;
  596. }
  597. }
  598. }
  599. /* Decreasing the number of wait states because of lower CPU frequency */
  600. if (FLatency < __HAL_FLASH_GET_LATENCY())
  601. {
  602. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  603. __HAL_FLASH_SET_LATENCY(FLatency);
  604. /* Check that the new number of wait states is taken into account to access the Flash
  605. memory by polling the FLASH_ACR register */
  606. tickstart = HAL_GetTick();
  607. while ((FLASH->ACR & FLASH_ACR_LATENCY) != FLatency)
  608. {
  609. if ((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
  610. {
  611. return HAL_TIMEOUT;
  612. }
  613. }
  614. }
  615. /*-------------------------- PCLK1 Configuration ---------------------------*/
  616. if (((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
  617. {
  618. assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB1CLKDivider));
  619. MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE, RCC_ClkInitStruct->APB1CLKDivider);
  620. }
  621. /* Update the SystemCoreClock global variable */
  622. SystemCoreClock = (HAL_RCC_GetSysClockFreq() >> ((AHBPrescTable[(RCC->CFGR & RCC_CFGR_HPRE) >> RCC_CFGR_HPRE_Pos]) & 0x1FU));
  623. /* Configure the source of time base considering new system clocks settings*/
  624. return HAL_InitTick(uwTickPrio);
  625. }
  626. /**
  627. * @}
  628. */
  629. /** @defgroup RCC_Exported_Functions_Group2 Peripheral Control functions
  630. * @brief RCC clocks control functions
  631. *
  632. @verbatim
  633. ===============================================================================
  634. ##### Peripheral Control functions #####
  635. ===============================================================================
  636. [..]
  637. This subsection provides a set of functions allowing to:
  638. (+) Ouput clock to MCO pin.
  639. (+) Retrieve current clock frequencies.
  640. (+) Enable the Clock Security System.
  641. @endverbatim
  642. * @{
  643. */
  644. /**
  645. * @brief Select the clock source to output on MCO pin.
  646. * @param RCC_MCOx specifies the output direction for the clock source.
  647. * @arg @ref RCC_MCO1 Clock source to output on MCO1 pin(PA07).
  648. * @arg @ref RCC_MCO2 Clock source to output on MCO2 pin(PB01).
  649. * @param RCC_MCOSource specifies the clock source to output.
  650. * This parameter can be one of the following values:
  651. * @arg @ref RCC_MCOSOURCE_NOCLOCK MCO output disabled, no clock on MCO
  652. * @arg @ref RCC_MCOSOURCE_SYSCLK system clock selected as MCO source
  653. * @arg @ref RCC_MCOSOURCE_HSI HSI clock selected as MCO source
  654. * @arg @ref RCC_MCOSOURCE_HSE HSE clock selected as MCO sourcee
  655. * @arg @ref RCC_MCOSOURCE_LSI LSI clock selected as MCO source
  656. * @arg @ref RCC_MCOSOURCE_LSE LSE clock selected as MCO source
  657. * @note Depending on devices and packages, some clocks may not be available.
  658. * Refer to device datasheet for clocks availability.
  659. * @param RCC_MCODiv specifies the MCO prescaler.
  660. * This parameter can be one of the following values:
  661. * @arg @ref RCC_MCODIV_1 no division applied to MCO clock
  662. * @arg @ref RCC_MCODIV_2 division by 2 applied to MCO clock
  663. * @arg @ref RCC_MCODIV_4 division by 4 applied to MCO clock
  664. * @arg @ref RCC_MCODIV_8 division by 8 applied to MCO clock
  665. * @arg @ref RCC_MCODIV_16 division by 16 applied to MCO clock
  666. * @arg @ref RCC_MCODIV_32 division by 32 applied to MCO clock
  667. * @arg @ref RCC_MCODIV_64 division by 64 applied to MCO clock
  668. * @arg @ref RCC_MCODIV_128 division by 128 applied to MCO clock
  669. * @retval None
  670. */
  671. void HAL_RCC_MCOConfig(uint32_t RCC_MCOx, uint32_t RCC_MCOSource, uint32_t RCC_MCODiv)
  672. {
  673. GPIO_InitTypeDef GPIO_InitStruct;
  674. /* Check the parameters */
  675. assert_param(IS_RCC_MCO(RCC_MCOx));
  676. assert_param(IS_RCC_MCODIV(RCC_MCODiv));
  677. assert_param(IS_RCC_MCO1SOURCE(RCC_MCOSource));
  678. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  679. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  680. GPIO_InitStruct.Pull = GPIO_NOPULL;
  681. switch(RCC_MCOx)
  682. {
  683. case RCC_MCO2 : /* Configure PB01 as the clock output */
  684. __HAL_RCC_GPIOB_CLK_ENABLE();
  685. GPIO_InitStruct.Pin = GPIO_PIN_1;
  686. GPIO_InitStruct.Alternate = GPIO_AF4_MCO;
  687. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  688. break;
  689. case RCC_MCO1 : /* Configure PA07 as the clock output */
  690. default :
  691. __HAL_RCC_GPIOA_CLK_ENABLE();
  692. GPIO_InitStruct.Pin = GPIO_PIN_7;
  693. GPIO_InitStruct.Alternate = GPIO_AF4_MCO;
  694. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  695. break;
  696. }
  697. /* Mask MCOSEL[] and MCOPRE[] bits then set MCO1 clock source and prescaler */
  698. MODIFY_REG(RCC->CFGR, (RCC_CFGR_MCOSEL | RCC_CFGR_MCOPRE), (RCC_MCOSource | RCC_MCODiv));
  699. }
  700. /**
  701. * @brief Return the SYSCLK frequency.
  702. *
  703. * @note The system frequency computed by this function is not the real
  704. * frequency in the chip. It is calculated based on the predefined
  705. * constant and the selected clock source:
  706. * @note If SYSCLK source is HSI, function returns values based on HSI_VALUE/HSIDIV(*)
  707. * @note If SYSCLK source is HSE, function returns values based on HSE_VALUE(**)
  708. * @note If SYSCLK source is LSI, function returns values based on LSI_VALUE(***)
  709. * @note If SYSCLK source is LSE, function returns values based on LSE_VALUE(****)
  710. * @note (*) HSI_VALUE is a constant defined in py32f002b_hal_conf.h file (default value
  711. * 8 MHz) but the real value may vary depending on the variations
  712. * in voltage and temperature.
  713. * @note (**) HSE_VALUE is a constant defined in py32f002b_hal_conf.h file (default value
  714. * 8 MHz), user has to ensure that HSE_VALUE is same as the real
  715. * frequency of the crystal used. Otherwise, this function may
  716. * have wrong result.
  717. * @note (***) LSE_VALUE is a constant defined in py32f002b_hal_conf.h file (default value
  718. * 32768 Hz).
  719. * @note (****) LSI_VALUE is a constant defined in py32f002b_hal_conf.h file (default value
  720. * 32768 Hz).
  721. *
  722. * @note Depending on devices and packages, some clocks may not be available.
  723. * Refer to device datasheet for clocks availability.
  724. *
  725. * @note The result of this function could be not correct when using fractional
  726. * value for HSE crystal.
  727. *
  728. * @note This function can be used by the user application to compute the
  729. * baudrate for the communication peripherals or configure other parameters.
  730. *
  731. * @note Each time SYSCLK changes, this function must be called to update the
  732. * right SYSCLK value. Otherwise, any configuration based on this function will be incorrect.
  733. *
  734. *
  735. * @retval SYSCLK frequency
  736. */
  737. uint32_t HAL_RCC_GetSysClockFreq(void)
  738. {
  739. uint32_t hsidiv;
  740. uint32_t sysclockfreq;
  741. #if defined(RCC_HSI48M_SUPPORT)
  742. const uint32_t hsiValue[8] = {0U, 0U, 0U, 0U, 24000000U, 48000000U, 0U, 0U};
  743. #else
  744. const uint32_t hsiValue[8] = {0U, 0U, 0U, 0U, 24000000U, 0U, 0U, 0U};
  745. #endif
  746. uint32_t hsiIndex;
  747. if (__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSISYS)
  748. {
  749. /* HSISYS can be derived for HSI */
  750. hsidiv = (1UL << ((READ_BIT(RCC->CR, RCC_CR_HSIDIV)) >> RCC_CR_HSIDIV_Pos));
  751. /* HSISYS used as system clock source */
  752. hsiIndex = (RCC->ICSCR&RCC_ICSCR_HSI_FS_Msk)>>RCC_ICSCR_HSI_FS_Pos;
  753. sysclockfreq = (hsiValue[hsiIndex] / hsidiv);
  754. }
  755. else if (__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSE)
  756. {
  757. /* HSE used as system clock source */
  758. sysclockfreq = HSE_VALUE;
  759. }
  760. else if (__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_LSE)
  761. {
  762. /* LSE used as system clock source */
  763. sysclockfreq = LSE_VALUE;
  764. }
  765. else if (__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_LSI)
  766. {
  767. /* LSI used as system clock source */
  768. if ((READ_BIT(RCC->ICSCR, RCC_ICSCR_LSI_TRIM) >> RCC_ICSCR_LSI_TRIM_Pos) == RCC_LSICALIBRATION_32768Hz)
  769. {
  770. sysclockfreq = 32768U;
  771. }
  772. else if ((READ_BIT(RCC->ICSCR, RCC_ICSCR_LSI_TRIM) >> RCC_ICSCR_LSI_TRIM_Pos) == RCC_LSICALIBRATION_38400Hz)
  773. {
  774. sysclockfreq = 38400U;
  775. }
  776. else
  777. {
  778. sysclockfreq = 0U;
  779. }
  780. }
  781. else
  782. {
  783. sysclockfreq = 0U;
  784. }
  785. return sysclockfreq;
  786. }
  787. /**
  788. * @brief Return the HCLK frequency.
  789. * @note Each time HCLK changes, this function must be called to update the
  790. * right HCLK value. Otherwise, any configuration based on this function will be incorrect.
  791. *
  792. * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency.
  793. * @retval HCLK frequency in Hz
  794. */
  795. uint32_t HAL_RCC_GetHCLKFreq(void)
  796. {
  797. return SystemCoreClock;
  798. }
  799. /**
  800. * @brief Return the PCLK1 frequency.
  801. * @note Each time PCLK1 changes, this function must be called to update the
  802. * right PCLK1 value. Otherwise, any configuration based on this function will be incorrect.
  803. * @retval PCLK1 frequency in Hz
  804. */
  805. uint32_t HAL_RCC_GetPCLK1Freq(void)
  806. {
  807. /* Get HCLK source and Compute PCLK1 frequency ---------------------------*/
  808. return (uint32_t)((HAL_RCC_GetHCLKFreq()) >> (APBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE) >> RCC_CFGR_PPRE_Pos] & 0x1FU));
  809. }
  810. /**
  811. * @brief Configure the RCC_OscInitStruct according to the internal
  812. * RCC configuration registers.
  813. * @param RCC_OscInitStruct pointer to an RCC_OscInitTypeDef structure that
  814. * will be configured.
  815. * @retval None
  816. */
  817. void HAL_RCC_GetOscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  818. {
  819. /* Check the parameters */
  820. assert_param(RCC_OscInitStruct != (void *)NULL);
  821. /* Set all possible values for the Oscillator type parameter ---------------*/
  822. RCC_OscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | \
  823. RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_LSI;
  824. /* Get the HSE configuration -----------------------------------------------*/
  825. if ((RCC->CR & RCC_CR_HSEEN) == RCC_CR_HSEEN)
  826. {
  827. RCC_OscInitStruct->HSEState = RCC_HSE_BYPASS_ENABLE;
  828. }
  829. else
  830. {
  831. RCC_OscInitStruct->HSEState = RCC_HSE_BYPASS_DISABLE;
  832. }
  833. /* Get the HSI configuration -----------------------------------------------*/
  834. if ((RCC->CR & RCC_CR_HSION) == RCC_CR_HSION)
  835. {
  836. RCC_OscInitStruct->HSIState = RCC_HSI_ON;
  837. }
  838. else
  839. {
  840. RCC_OscInitStruct->HSIState = RCC_HSI_OFF;
  841. }
  842. RCC_OscInitStruct->HSICalibrationValue = (RCC->ICSCR & (RCC_ICSCR_HSI_FS | RCC_ICSCR_HSI_TRIM));
  843. RCC_OscInitStruct->HSIDiv = (RCC->CR & RCC_CR_HSIDIV);
  844. /* Get the LSI configuration -----------------------------------------------*/
  845. if ((RCC->CSR & RCC_CSR_LSION) == RCC_CSR_LSION)
  846. {
  847. RCC_OscInitStruct->LSIState = RCC_LSI_ON;
  848. }
  849. else
  850. {
  851. RCC_OscInitStruct->LSIState = RCC_LSI_OFF;
  852. }
  853. RCC_OscInitStruct->LSICalibrationValue = ((RCC->ICSCR & RCC_ICSCR_LSI_TRIM) >> RCC_ICSCR_LSI_TRIM_Pos);
  854. /* Get the LSE configuration -----------------------------------------------*/
  855. if ((RCC->BDCR & RCC_BDCR_LSEBYP) == RCC_BDCR_LSEBYP)
  856. {
  857. RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS;
  858. }
  859. else if ((RCC->BDCR & RCC_BDCR_LSEON) == RCC_BDCR_LSEON)
  860. {
  861. RCC_OscInitStruct->LSEState = RCC_LSE_ON;
  862. }
  863. else
  864. {
  865. RCC_OscInitStruct->LSEState = RCC_LSE_OFF;
  866. }
  867. RCC_OscInitStruct->LSEDriver = (RCC->ECSCR & RCC_ECSCR_LSE_DRIVER);
  868. }
  869. /**
  870. * @brief Configure the RCC_ClkInitStruct according to the internal
  871. * RCC configuration registers.
  872. * @param RCC_ClkInitStruct Pointer to a @ref RCC_ClkInitTypeDef structure that
  873. * will be configured.
  874. * @param pFLatency Pointer on the Flash Latency.
  875. * @retval None
  876. */
  877. void HAL_RCC_GetClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t *pFLatency)
  878. {
  879. /* Check the parameters */
  880. assert_param(RCC_ClkInitStruct != (void *)NULL);
  881. assert_param(pFLatency != (void *)NULL);
  882. /* Set all possible values for the Clock type parameter --------------------*/
  883. RCC_ClkInitStruct->ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1;
  884. /* Get the SYSCLK configuration --------------------------------------------*/
  885. RCC_ClkInitStruct->SYSCLKSource = (uint32_t)(RCC->CFGR & RCC_CFGR_SW);
  886. /* Get the HCLK configuration ----------------------------------------------*/
  887. RCC_ClkInitStruct->AHBCLKDivider = (uint32_t)(RCC->CFGR & RCC_CFGR_HPRE);
  888. /* Get the APB1 configuration ----------------------------------------------*/
  889. RCC_ClkInitStruct->APB1CLKDivider = (uint32_t)(RCC->CFGR & RCC_CFGR_PPRE);
  890. /* Get the Flash Wait State (Latency) configuration ------------------------*/
  891. *pFLatency = (uint32_t)(FLASH->ACR & FLASH_ACR_LATENCY);
  892. }
  893. /**
  894. * @brief Enable the LSE Clock Security System.
  895. * @note If a failure is detected on the LSE oscillator clock, this oscillator
  896. * is automatically disabled and an interrupt is generated to inform the
  897. * software about the failure (Clock Security System Interrupt, CSSI),
  898. * allowing the MCU to perform rescue operations. The CSSI is linked to
  899. * the Cortex-M0+ NMI (Non-Maskable Interrupt) exception vector.
  900. * @note The LSE Clock Security System Detection bit (LSECSSD in BDCR) can only be
  901. * cleared by a backup domain reset.
  902. * @retval None
  903. */
  904. void HAL_RCC_EnableLSECSS(void)
  905. {
  906. SET_BIT(RCC->BDCR, RCC_BDCR_LSECSSON) ;
  907. }
  908. /**
  909. * @brief Disable the LSE Clock Security System.
  910. * @note After LSE failure detection, the software must disable LSECSSON
  911. * @note The Clock Security System can only be cleared by reset otherwise.
  912. * @retval None
  913. */
  914. void HAL_RCC_DisableLSECSS(void)
  915. {
  916. CLEAR_BIT(RCC->BDCR, RCC_BDCR_LSECSSON) ;
  917. }
  918. /**
  919. * @brief Handle the RCC Clock Security System interrupt request.
  920. * @note This API should be called under the NMI_Handler().
  921. * @retval None
  922. */
  923. void HAL_RCC_NMI_IRQHandler(void)
  924. {
  925. uint32_t itflag = RCC->CIFR;
  926. /* Clear interrupt flags related to CSS */
  927. RCC->CICR = (itflag & RCC_CIFR_LSECSSF);
  928. /* Check RCC LSECSSF interrupt flag */
  929. if ((itflag & RCC_CIFR_LSECSSF) != 0x00u)
  930. {
  931. /* RCC Clock Security System interrupt user callback */
  932. HAL_RCC_LSECSSCallback();
  933. }
  934. }
  935. /**
  936. * @brief Handle the RCC HSE Clock Security System interrupt callback.
  937. * @retval none
  938. */
  939. __weak void HAL_RCC_CSSCallback(void)
  940. {
  941. /* NOTE : This function should not be modified, when the callback is needed,
  942. the @ref HAL_RCC_CSSCallback should be implemented in the user file
  943. */
  944. }
  945. #if defined(RCC_LSE_SUPPORT)
  946. /**
  947. * @brief RCC LSE Clock Security System interrupt callback.
  948. * @retval none
  949. */
  950. __weak void HAL_RCC_LSECSSCallback(void)
  951. {
  952. /* NOTE : This function should not be modified, when the callback is needed,
  953. the HAL_RCC_LSECSSCallback should be implemented in the user file
  954. */
  955. }
  956. #endif
  957. /**
  958. * @}
  959. */
  960. /**
  961. * @}
  962. */
  963. #endif /* HAL_RCC_MODULE_ENABLED */
  964. /**
  965. * @}
  966. */
  967. /**
  968. * @}
  969. */
  970. /************************ (C) COPYRIGHT Puya *****END OF FILE****/