py32f0xx_hal_rcc.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484
  1. /**
  2. ******************************************************************************
  3. * @file py32f0xx_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 PY32F0xx_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 HSE_TIMEOUT_VALUE HSE_STARTUP_TIMEOUT
  76. #define HSI_TIMEOUT_VALUE (2U) /* 2 ms (minimum Tick + 1) */
  77. #define LSI_TIMEOUT_VALUE (2U) /* 2 ms (minimum Tick + 1) */
  78. #if defined(RCC_PLL_SUPPORT)
  79. #define PLL_TIMEOUT_VALUE (2U) /* 2 ms (minimum Tick + 1) */
  80. #define PLLSOURCE_NONE (0U)
  81. #define PLLSOURCE_FREQ_MIN (16000000U)
  82. #define PLLSOURCE_FREQ_MAX (24000000U)
  83. #endif
  84. /**
  85. * @}
  86. */
  87. /* Private macro -------------------------------------------------------------*/
  88. /** @defgroup RCC_Private_Macros RCC Private Macros
  89. * @{
  90. */
  91. #define RCC_PLL_OSCSOURCE_CONFIG(__HAL_RCC_PLLSOURCE__) \
  92. (MODIFY_REG(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC, (uint32_t)(__HAL_RCC_PLLSOURCE__)))
  93. /**
  94. * @}
  95. */
  96. /* Private variables ---------------------------------------------------------*/
  97. /** @defgroup RCC_Private_Variables RCC Private Variables
  98. * @{
  99. */
  100. /**
  101. * @}
  102. */
  103. /* Private function prototypes -----------------------------------------------*/
  104. #if defined(RCC_PLL_SUPPORT)
  105. static uint32_t RCC_GetHSIFreq(void);
  106. #endif
  107. /* Exported functions --------------------------------------------------------*/
  108. /** @defgroup RCC_Exported_Functions RCC Exported Functions
  109. * @{
  110. */
  111. /** @defgroup RCC_Exported_Functions_Group1 Initialization and de-initialization functions
  112. * @brief Initialization and Configuration functions
  113. *
  114. @verbatim
  115. ===============================================================================
  116. ##### Initialization and de-initialization functions #####
  117. ===============================================================================
  118. [..]
  119. This section provides functions allowing to configure the internal and external oscillators
  120. (HSE, HSI, LSE, LSI, PLL, CSS and MCO) and the System busses clocks (SYSCLK, AHB, APB)
  121. [..] Internal/external clock and PLL configuration
  122. (+) HSI (high-speed internal): 8 MHz factory-trimmed RC used directly or through
  123. the PLL as System clock source.
  124. (+) LSI (low-speed internal): 32 KHz low consumption RC used as IWDG and/or RTC
  125. clock source.
  126. (+) HSE (high-speed external): 4 to 48 MHz crystal oscillator used directly or
  127. through the PLL as System clock source. Can be used also optionally as RTC clock source.
  128. (+) LSE (low-speed external): 32.768 KHz oscillator used optionally as RTC clock source.
  129. (+) PLL (clocked by HSI, HSE) providing up to three independent output clocks:
  130. (+) CSS (Clock security system): once enabled, if a HSE clock failure occurs
  131. (HSE used directly or through PLL as System clock source), the System clock
  132. is automatically switched respectively to HSI or LSI and an interrupt is generated
  133. if enabled. The interrupt is linked to the Cortex-M0+ NMI (Non-Maskable Interrupt)
  134. exception vector.
  135. (+) MCO (microcontroller clock output): used to output LSI, HSI, LSE, HSE or
  136. main PLL clock (through a configurable prescaler) on PA8 pin.
  137. [..] System, AHB and APB busses clocks configuration
  138. (+) Several clock sources can be used to drive the System clock (SYSCLK): HSI,
  139. HSE, LSI, LSE and main PLL.
  140. The AHB clock (HCLK) is derived from System clock through configurable
  141. prescaler and used to clock the CPU, memory and peripherals mapped
  142. on AHB bus (DMA, GPIO...).and APB (PCLK1) clock is derived
  143. from AHB clock through configurable prescalers and used to clock
  144. the peripherals mapped on these busses. You can use
  145. "@ref HAL_RCC_GetSysClockFreq()" function to retrieve the frequencies of these clocks.
  146. -@- All the peripheral clocks are derived from the System clock (SYSCLK) except:
  147. (+@) RTC: the RTC clock can be derived either from the LSI, LSE or HSE clock
  148. divided by 2 to 31.
  149. You have to use @ref __HAL_RCC_RTC_ENABLE() and @ref HAL_RCCEx_PeriphCLKConfig() function
  150. to configure this clock.
  151. (+@) RNG(*) requires a frequency equal or lower than 48 MHz.
  152. This clock is derived from the main PLL or HSI or System clock.
  153. (*) available on certain devices only
  154. (+@) IWDG clock which is always the LSI clock.
  155. (+) The maximum frequency of the SYSCLK, HCLK, PCLK is 48 MHz.
  156. Depending on the device voltage range, the maximum frequency should be
  157. adapted accordingly.
  158. @endverbatim
  159. * @{
  160. */
  161. /**
  162. * @brief Reset the RCC clock configuration to the default reset state.
  163. * @note The default reset state of the clock configuration is given below:
  164. * - HSI ON and used as system clock source
  165. * - HSE, PLL OFF
  166. * - AHB and APB prescaler set to 1.
  167. * - CSS, MCO1 OFF
  168. * - All interrupts disabled
  169. * @note This function does not modify the configuration of the
  170. * - Peripheral clocks
  171. * - LSI, LSE and RTC clocks
  172. * @retval HAL status
  173. */
  174. HAL_StatusTypeDef HAL_RCC_DeInit(void)
  175. {
  176. uint32_t tickstart;
  177. /* Get Start Tick*/
  178. tickstart = HAL_GetTick();
  179. /* Set HSION bit to the reset value */
  180. SET_BIT(RCC->CR, RCC_CR_HSION);
  181. /* Wait till HSI is ready */
  182. while (READ_BIT(RCC->CR, RCC_CR_HSIRDY) == 0U)
  183. {
  184. if ((HAL_GetTick() - tickstart) > HSI_TIMEOUT_VALUE)
  185. {
  186. return HAL_TIMEOUT;
  187. }
  188. }
  189. /* Set the HSI clock to 8MHz by default */
  190. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_HSICALIBRATION_8MHz);
  191. /* Get Start Tick*/
  192. tickstart = HAL_GetTick();
  193. /* Reset CFGR register (HSI is selected as system clock source) */
  194. RCC->CFGR = 0x00000000u;
  195. /* Wait till HSI is ready */
  196. while (READ_BIT(RCC->CFGR, RCC_CFGR_SWS) != 0U)
  197. {
  198. if ((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
  199. {
  200. return HAL_TIMEOUT;
  201. }
  202. }
  203. /* Clear CR register in 2 steps: first to clear HSEON in case bypass was enabled */
  204. RCC->CR = RCC_CR_HSION;
  205. /* Then again to HSEBYP in case bypass was enabled */
  206. RCC->CR = RCC_CR_HSION;
  207. #if defined(RCC_PLL_SUPPORT)
  208. /* Get Start Tick*/
  209. tickstart = HAL_GetTick();
  210. /* Wait till PLL is ready */
  211. while (READ_BIT(RCC->CR, RCC_CR_PLLRDY) != 0U)
  212. {
  213. if ((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  214. {
  215. return HAL_TIMEOUT;
  216. }
  217. }
  218. /* once PLL is OFF, reset PLLCFGR register to default value */
  219. RCC->PLLCFGR = 0x00000000u;
  220. #endif
  221. /* Disable all interrupts */
  222. RCC->CIER = 0x00000000u;
  223. /* Clear all flags */
  224. RCC->CICR = 0xFFFFFFFFu;
  225. /* Update the SystemCoreClock global variable */
  226. SystemCoreClock = HSI_VALUE;
  227. /* Adapt Systick interrupt period */
  228. if (HAL_InitTick(uwTickPrio) != HAL_OK)
  229. {
  230. return HAL_ERROR;
  231. }
  232. else
  233. {
  234. return HAL_OK;
  235. }
  236. }
  237. /**
  238. * @brief Initialize the RCC Oscillators according to the specified parameters in the
  239. * @ref RCC_OscInitTypeDef.
  240. * @param RCC_OscInitStruct pointer to a @ref RCC_OscInitTypeDef structure that
  241. * contains the configuration information for the RCC Oscillators.
  242. * @note The PLL is not disabled when used as system clock.
  243. * @note Transition HSE Bypass to HSE On and HSE On to HSE Bypass are not
  244. * supported by this function. User should request a transition to HSE Off
  245. * first and then to HSE On or HSE Bypass.
  246. * @note Transition LSE Bypass to LSE On and LSE On to LSE Bypass are not
  247. * supported by this function. User should request a transition to LSE Off
  248. * first and then to LSE On or LSE Bypass.
  249. * @note Depending on devices and packages, some clocks may not be available.
  250. * Refer to device datasheet for clocks availability.
  251. * @retval HAL status
  252. */
  253. HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  254. {
  255. uint32_t tickstart;
  256. uint32_t temp_sysclksrc;
  257. #if defined(RCC_PLL_SUPPORT)
  258. uint32_t temp_pllckcfg;
  259. #endif
  260. /* Check Null pointer */
  261. if (RCC_OscInitStruct == NULL)
  262. {
  263. return HAL_ERROR;
  264. }
  265. /* Check the parameters */
  266. assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct->OscillatorType));
  267. /*------------------------------- HSE Configuration ------------------------*/
  268. if (((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE)
  269. {
  270. /* Check the parameters */
  271. assert_param(IS_RCC_HSE(RCC_OscInitStruct->HSEState));
  272. temp_sysclksrc = __HAL_RCC_GET_SYSCLK_SOURCE();
  273. #if defined(RCC_PLL_SUPPORT)
  274. temp_pllckcfg = __HAL_RCC_GET_PLL_OSCSOURCE();
  275. /* When the HSE is used as system clock or clock source for PLL in these cases it is not allowed to be disabled */
  276. if (((temp_sysclksrc == RCC_CFGR_SWS_PLL) && (temp_pllckcfg == RCC_PLLSOURCE_HSE)) || (temp_sysclksrc == RCC_CFGR_SWS_HSE))
  277. #else
  278. if (temp_sysclksrc == RCC_CFGR_SWS_HSE)
  279. #endif
  280. {
  281. if ((READ_BIT(RCC->CR, RCC_CR_HSERDY) != 0U) && (RCC_OscInitStruct->HSEState == RCC_HSE_OFF))
  282. {
  283. return HAL_ERROR;
  284. }
  285. }
  286. else
  287. {
  288. /* Set frequency range of the HSE */
  289. if (RCC_OscInitStruct->HSEState != RCC_HSE_OFF)
  290. {
  291. assert_param(IS_RCC_HSE_FREQ(RCC_OscInitStruct->HSEFreq));
  292. if (RCC_OscInitStruct->HSEFreq != 0)
  293. {
  294. __HAL_RCC_HSE_CONFIG(RCC_HSE_OFF);
  295. /* Get Start Tick*/
  296. tickstart = HAL_GetTick();
  297. /* Wait till HSE is off */
  298. while (READ_BIT(RCC->CR, RCC_CR_HSERDY) == 1U)
  299. {
  300. if ((HAL_GetTick() - tickstart) > HSE_TIMEOUT_VALUE)
  301. {
  302. return HAL_TIMEOUT;
  303. }
  304. }
  305. MODIFY_REG(RCC->ECSCR, RCC_ECSCR_HSE_FREQ_Msk, RCC_OscInitStruct->HSEFreq);
  306. } else
  307. {
  308. return HAL_ERROR;
  309. }
  310. }
  311. /* Set the new HSE configuration ---------------------------------------*/
  312. __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct->HSEState);
  313. /* Check the HSE State */
  314. if (RCC_OscInitStruct->HSEState != RCC_HSE_OFF)
  315. {
  316. #ifndef RCC_NO_DETECT_HSE_READY
  317. /* Get Start Tick*/
  318. tickstart = HAL_GetTick();
  319. /* Wait till HSE is ready */
  320. while (READ_BIT(RCC->CR, RCC_CR_HSERDY) == 0U)
  321. {
  322. if ((HAL_GetTick() - tickstart) > HSE_TIMEOUT_VALUE)
  323. {
  324. return HAL_TIMEOUT;
  325. }
  326. }
  327. #endif
  328. }
  329. else
  330. {
  331. /* Get Start Tick*/
  332. tickstart = HAL_GetTick();
  333. /* Wait till HSE is disabled */
  334. while (READ_BIT(RCC->CR, RCC_CR_HSERDY) != 0U)
  335. {
  336. if ((HAL_GetTick() - tickstart) > HSE_TIMEOUT_VALUE)
  337. {
  338. return HAL_TIMEOUT;
  339. }
  340. }
  341. }
  342. }
  343. }
  344. /*----------------------------- HSI Configuration --------------------------*/
  345. if (((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI)
  346. {
  347. /* Check the parameters */
  348. assert_param(IS_RCC_HSI(RCC_OscInitStruct->HSIState));
  349. assert_param(IS_RCC_HSI_CALIBRATION_VALUE(RCC_OscInitStruct->HSICalibrationValue));
  350. #if defined(RCC_HSIDIV_SUPPORT)
  351. assert_param(IS_RCC_HSIDIV(RCC_OscInitStruct->HSIDiv));
  352. #endif
  353. /* Check if HSI is used as system clock or as PLL source when PLL is selected as system clock */
  354. temp_sysclksrc = __HAL_RCC_GET_SYSCLK_SOURCE();
  355. #if defined(RCC_PLL_SUPPORT)
  356. temp_pllckcfg = __HAL_RCC_GET_PLL_OSCSOURCE();
  357. if (((temp_sysclksrc == RCC_CFGR_SWS_PLL) && (temp_pllckcfg == RCC_PLLSOURCE_HSI)) || (temp_sysclksrc == RCC_CFGR_SWS_HSI))
  358. #else
  359. if (temp_sysclksrc == RCC_CFGR_SWS_HSI)
  360. #endif
  361. {
  362. /* When HSI is used as system clock or as PLL input clock it can not be disabled */
  363. if ((READ_BIT(RCC->CR, RCC_CR_HSIRDY) != 0U) && (RCC_OscInitStruct->HSIState == RCC_HSI_OFF))
  364. {
  365. return HAL_ERROR;
  366. }
  367. /* Otherwise, just the calibration is allowed */
  368. else
  369. {
  370. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  371. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  372. /* Get Start Tick*/
  373. tickstart = HAL_GetTick();
  374. /* Wait till HSI is ready */
  375. while (READ_BIT(RCC->CR, RCC_CR_HSIRDY) == 0U)
  376. {
  377. if ((HAL_GetTick() - tickstart) > HSI_TIMEOUT_VALUE)
  378. {
  379. return HAL_TIMEOUT;
  380. }
  381. }
  382. if (temp_sysclksrc == RCC_CFGR_SWS_HSI)
  383. {
  384. #if defined(RCC_HSIDIV_SUPPORT)
  385. /* Adjust the HSI division factor */
  386. __HAL_RCC_HSI_CONFIG(RCC_OscInitStruct->HSIDiv);
  387. #endif
  388. /* Update the SystemCoreClock global variable with HSISYS value */
  389. SystemCoreClock = (HAL_RCC_GetSysClockFreq() >> ((AHBPrescTable[(RCC->CFGR & RCC_CFGR_HPRE) >> RCC_CFGR_HPRE_Pos]) & 0x1FU));
  390. }
  391. /* Adapt Systick interrupt period */
  392. if (HAL_InitTick(uwTickPrio) != HAL_OK)
  393. {
  394. return HAL_ERROR;
  395. }
  396. }
  397. }
  398. else
  399. {
  400. /* Check the HSI State */
  401. if (RCC_OscInitStruct->HSIState != RCC_HSI_OFF)
  402. {
  403. #if defined(RCC_HSIDIV_SUPPORT)
  404. /* Configure the HSI division factor */
  405. __HAL_RCC_HSI_CONFIG(RCC_OscInitStruct->HSIDiv);
  406. #endif
  407. /* Enable the Internal High Speed oscillator (HSI). */
  408. __HAL_RCC_HSI_ENABLE();
  409. /* Get Start Tick*/
  410. tickstart = HAL_GetTick();
  411. /* Wait till HSI is ready */
  412. while (READ_BIT(RCC->CR, RCC_CR_HSIRDY) == 0U)
  413. {
  414. if ((HAL_GetTick() - tickstart) > HSI_TIMEOUT_VALUE)
  415. {
  416. return HAL_TIMEOUT;
  417. }
  418. }
  419. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  420. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  421. }
  422. else
  423. {
  424. /* Disable the Internal High Speed oscillator (HSI). */
  425. __HAL_RCC_HSI_DISABLE();
  426. /* Get Start Tick*/
  427. tickstart = HAL_GetTick();
  428. /* Wait till HSI is disabled */
  429. while (READ_BIT(RCC->CR, RCC_CR_HSIRDY) != 0U)
  430. {
  431. if ((HAL_GetTick() - tickstart) > HSI_TIMEOUT_VALUE)
  432. {
  433. return HAL_TIMEOUT;
  434. }
  435. }
  436. }
  437. }
  438. }
  439. /*------------------------------ LSI Configuration -------------------------*/
  440. if (((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI)
  441. {
  442. /* Check the parameters */
  443. assert_param(IS_RCC_LSI(RCC_OscInitStruct->LSIState));
  444. /* Check if LSI is used as system clock */
  445. if (__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_LSI)
  446. {
  447. /* When LSI is used as system clock it will not be disabled */
  448. if ((((RCC->CSR) & RCC_CSR_LSIRDY) != 0U) && (RCC_OscInitStruct->LSIState == RCC_LSI_OFF))
  449. {
  450. return HAL_ERROR;
  451. }
  452. }
  453. else
  454. {
  455. /* Check the LSI State */
  456. if (RCC_OscInitStruct->LSIState != RCC_LSI_OFF)
  457. {
  458. /* Enable the Internal Low Speed oscillator (LSI). */
  459. __HAL_RCC_LSI_ENABLE();
  460. /* Get Start Tick*/
  461. tickstart = HAL_GetTick();
  462. /* Wait till LSI is ready */
  463. while (READ_BIT(RCC->CSR, RCC_CSR_LSIRDY) == 0U)
  464. {
  465. if ((HAL_GetTick() - tickstart) > LSI_TIMEOUT_VALUE)
  466. {
  467. return HAL_TIMEOUT;
  468. }
  469. }
  470. }
  471. else
  472. {
  473. /* Disable the Internal Low Speed oscillator (LSI). */
  474. __HAL_RCC_LSI_DISABLE();
  475. /* Get Start Tick*/
  476. tickstart = HAL_GetTick();
  477. /* Wait till LSI is disabled */
  478. while (READ_BIT(RCC->CSR, RCC_CSR_LSIRDY) != 0U)
  479. {
  480. if ((HAL_GetTick() - tickstart) > LSI_TIMEOUT_VALUE)
  481. {
  482. return HAL_TIMEOUT;
  483. }
  484. }
  485. }
  486. }
  487. }
  488. #if defined(RCC_LSE_SUPPORT)
  489. /*------------------------------ LSE Configuration -------------------------*/
  490. if (((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE)
  491. {
  492. FlagStatus pwrclkchanged = RESET;
  493. /* Check the parameters */
  494. assert_param(IS_RCC_LSE(RCC_OscInitStruct->LSEState));
  495. /* When the LSE is used as system clock, it is not allowed disable it */
  496. if (__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_LSE)
  497. {
  498. if ((((RCC->BDCR) & RCC_BDCR_LSERDY) != 0U) && (RCC_OscInitStruct->LSEState == RCC_LSE_OFF))
  499. {
  500. return HAL_ERROR;
  501. }
  502. }
  503. else
  504. {
  505. /* Update LSE configuration in Backup Domain control register */
  506. /* Requires to enable write access to Backup Domain of necessary */
  507. if (__HAL_RCC_PWR_IS_CLK_DISABLED() != 0U)
  508. {
  509. __HAL_RCC_PWR_CLK_ENABLE();
  510. pwrclkchanged = SET;
  511. }
  512. if (HAL_IS_BIT_CLR(PWR->CR1, PWR_CR1_DBP))
  513. {
  514. /* Enable write access to Backup domain */
  515. SET_BIT(PWR->CR1, PWR_CR1_DBP);
  516. /* Wait for Backup domain Write protection disable */
  517. tickstart = HAL_GetTick();
  518. while (HAL_IS_BIT_CLR(PWR->CR1, PWR_CR1_DBP))
  519. {
  520. if ((HAL_GetTick() - tickstart) > RCC_DBP_TIMEOUT_VALUE)
  521. {
  522. return HAL_TIMEOUT;
  523. }
  524. }
  525. }
  526. /* Set driver factor of the LSE*/
  527. if (RCC_OscInitStruct->LSEState != RCC_LSE_OFF)
  528. {
  529. __HAL_RCC_LSE_CONFIG(RCC_LSE_OFF);
  530. /* Get Start Tick*/
  531. tickstart = HAL_GetTick();
  532. /* Wait till LSE is off */
  533. while (READ_BIT(RCC->BDCR, RCC_BDCR_LSERDY) == 1U)
  534. {
  535. if ((HAL_GetTick() - tickstart) > RCC_LSE_TIMEOUT_VALUE)
  536. {
  537. return HAL_TIMEOUT;
  538. }
  539. }
  540. if (((RCC_OscInitStruct->LSEDriver) & RCC_ECSCR_LSE_DRIVER) == 0U)
  541. {
  542. MODIFY_REG(RCC->ECSCR, RCC_ECSCR_LSE_DRIVER_Msk, RCC_ECSCR_LSE_DRIVER_1);
  543. } else
  544. {
  545. MODIFY_REG(RCC->ECSCR, RCC_ECSCR_LSE_DRIVER_Msk, RCC_OscInitStruct->LSEDriver);
  546. }
  547. }
  548. /* Set the new LSE configuration -----------------------------------------*/
  549. __HAL_RCC_LSE_CONFIG(RCC_OscInitStruct->LSEState);
  550. /* Check the LSE State */
  551. if (RCC_OscInitStruct->LSEState != RCC_LSE_OFF)
  552. {
  553. #ifndef RCC_NO_DETECT_LSE_READY
  554. /* Get Start Tick*/
  555. tickstart = HAL_GetTick();
  556. /* Wait till LSE is ready */
  557. while (READ_BIT(RCC->BDCR, RCC_BDCR_LSERDY) == 0U)
  558. {
  559. if ((HAL_GetTick() - tickstart) > RCC_LSE_TIMEOUT_VALUE)
  560. {
  561. return HAL_TIMEOUT;
  562. }
  563. }
  564. #endif
  565. }
  566. else
  567. {
  568. /* Get Start Tick*/
  569. tickstart = HAL_GetTick();
  570. /* Wait till LSE is disabled */
  571. while (READ_BIT(RCC->BDCR, RCC_BDCR_LSERDY) != 0U)
  572. {
  573. if ((HAL_GetTick() - tickstart) > RCC_LSE_TIMEOUT_VALUE)
  574. {
  575. return HAL_TIMEOUT;
  576. }
  577. }
  578. }
  579. /* Restore clock configuration if changed */
  580. if (pwrclkchanged == SET)
  581. {
  582. __HAL_RCC_PWR_CLK_DISABLE();
  583. }
  584. }
  585. }
  586. #endif
  587. #if defined(RCC_PLL_SUPPORT)
  588. /*-------------------------------- PLL Configuration -----------------------*/
  589. /* Check the parameters */
  590. assert_param(IS_RCC_PLL(RCC_OscInitStruct->PLL.PLLState));
  591. if (RCC_OscInitStruct->PLL.PLLState != RCC_PLL_NONE)
  592. {
  593. /* Check if the PLL is used as system clock or not */
  594. if (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_PLL)
  595. {
  596. if (RCC_OscInitStruct->PLL.PLLState == RCC_PLL_ON)
  597. {
  598. /* Check the parameters */
  599. assert_param(IS_RCC_PLLSOURCE(RCC_OscInitStruct->PLL.PLLSource));
  600. /* Disable the main PLL. */
  601. __HAL_RCC_PLL_DISABLE();
  602. /* Get Start Tick*/
  603. tickstart = HAL_GetTick();
  604. /* Wait till PLL is cancel the 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. /* PLL input source frequency must be greater than or equal to PLLSOURCE_MIN_FREQ */
  613. if(((RCC_OscInitStruct->PLL.PLLSource == RCC_PLLSOURCE_HSE) && \
  614. (((HSE_VALUE < PLLSOURCE_FREQ_MIN)) || (HSE_VALUE > PLLSOURCE_FREQ_MAX))) || \
  615. ((RCC_OscInitStruct->PLL.PLLSource == RCC_PLLSOURCE_HSI) && (RCC_GetHSIFreq() < PLLSOURCE_FREQ_MIN)))
  616. {
  617. return HAL_ERROR;
  618. }
  619. /* Configure the main PLL clock source, multiplication and division factors. */
  620. __HAL_RCC_PLL_PLLSOURCE_CONFIG(RCC_OscInitStruct->PLL.PLLSource);
  621. /* Enable the main PLL. */
  622. __HAL_RCC_PLL_ENABLE();
  623. /* Get Start Tick*/
  624. tickstart = HAL_GetTick();
  625. /* Wait till PLL is ready */
  626. while (READ_BIT(RCC->CR, RCC_CR_PLLRDY) == 0U)
  627. {
  628. if ((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  629. {
  630. return HAL_TIMEOUT;
  631. }
  632. }
  633. }
  634. else
  635. {
  636. /* Disable the main PLL. */
  637. __HAL_RCC_PLL_DISABLE();
  638. /* Disable all PLL outputs to save power */
  639. MODIFY_REG(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC, PLLSOURCE_NONE);
  640. /* Get Start Tick*/
  641. tickstart = HAL_GetTick();
  642. /* Wait till PLL is disabled */
  643. while (READ_BIT(RCC->CR, RCC_CR_PLLRDY) != 0U)
  644. {
  645. if ((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  646. {
  647. return HAL_TIMEOUT;
  648. }
  649. }
  650. }
  651. }
  652. else
  653. {
  654. /* Check if there is a request to disable the PLL used as System clock source */
  655. if((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_OFF)
  656. {
  657. return HAL_ERROR;
  658. }
  659. else
  660. {
  661. /* Do not return HAL_ERROR if request repeats the current configuration */
  662. temp_pllckcfg = RCC->PLLCFGR;
  663. if(READ_BIT(temp_pllckcfg, RCC_PLLCFGR_PLLSRC) != RCC_OscInitStruct->PLL.PLLSource)
  664. {
  665. return HAL_ERROR;
  666. }
  667. }
  668. }
  669. }
  670. #endif
  671. return HAL_OK;
  672. }
  673. /**
  674. * @brief Initialize the CPU, AHB and APB busses clocks according to the specified
  675. * parameters in the RCC_ClkInitStruct.
  676. * @param RCC_ClkInitStruct pointer to a @ref RCC_ClkInitTypeDef structure that
  677. * contains the configuration information for the RCC peripheral.
  678. * @param FLatency FLASH Latency
  679. * This parameter can be one of the following values:
  680. * @arg FLASH_LATENCY_0 FLASH 0 Latency cycle
  681. * @arg FLASH_LATENCY_1 FLASH 1 Latency cycle
  682. *
  683. * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency
  684. * and updated by @ref HAL_RCC_GetHCLKFreq() function called within this function
  685. *
  686. * @note The HSI is used by default as system clock source after
  687. * startup from Reset, wake-up from STANDBY mode. After restart from Reset,
  688. * the HSI frequency is set to 4 MHz, then it reaches its default value 8 MHz.
  689. *
  690. * @note The HSI can be selected as system clock source after
  691. * from STOP modes or in case of failure of the HSE used directly or indirectly
  692. * as system clock (if the Clock Security System CSS is enabled).
  693. *
  694. * @note The LSI can be selected as system clock source after
  695. * in case of failure of the LSE used directly or indirectly
  696. * as system clock (if the Clock Security System LSECSS is enabled).
  697. *
  698. * @note A switch from one clock source to another occurs only if the target
  699. * clock source is ready (clock stable after startup delay or PLL locked).
  700. * If a clock source which is not yet ready is selected, the switch will
  701. * occur when the clock source is ready.
  702. *
  703. * @note You can use @ref HAL_RCC_GetClockConfig() function to know which clock is
  704. * currently used as system clock source.
  705. *
  706. * @note Depending on the device voltage range, the software has to set correctly
  707. * HPRE[3:0] bits to ensure that HCLK not exceed the maximum allowed frequency
  708. * (for more details refer to section above "Initialization/de-initialization functions")
  709. * @retval None
  710. */
  711. HAL_StatusTypeDef HAL_RCC_ClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t FLatency)
  712. {
  713. uint32_t tickstart;
  714. /* Check Null pointer */
  715. if (RCC_ClkInitStruct == NULL)
  716. {
  717. return HAL_ERROR;
  718. }
  719. /* Check the parameters */
  720. assert_param(IS_RCC_CLOCKTYPE(RCC_ClkInitStruct->ClockType));
  721. assert_param(IS_FLASH_LATENCY(FLatency));
  722. /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
  723. must be correctly programmed according to the frequency of the FLASH clock
  724. (HCLK) and the supply voltage of the device. */
  725. /* Increasing the number of wait states because of higher CPU frequency */
  726. if (FLatency > __HAL_FLASH_GET_LATENCY())
  727. {
  728. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  729. __HAL_FLASH_SET_LATENCY(FLatency);
  730. /* Check that the new number of wait states is taken into account to access the Flash
  731. memory by polling the FLASH_ACR register */
  732. tickstart = HAL_GetTick();
  733. while ((FLASH->ACR & FLASH_ACR_LATENCY) != FLatency)
  734. {
  735. if ((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
  736. {
  737. return HAL_TIMEOUT;
  738. }
  739. }
  740. }
  741. /*-------------------------- HCLK Configuration --------------------------*/
  742. if (((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)
  743. {
  744. /* Set the highest APB divider in order to ensure that we do not go through
  745. a non-spec phase whatever we decrease or increase HCLK. */
  746. if (((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
  747. {
  748. MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE, RCC_HCLK_DIV16);
  749. }
  750. /* Set the new HCLK clock divider */
  751. assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
  752. MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
  753. }
  754. /*------------------------- SYSCLK Configuration ---------------------------*/
  755. if (((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_SYSCLK) == RCC_CLOCKTYPE_SYSCLK)
  756. {
  757. assert_param(IS_RCC_SYSCLKSOURCE(RCC_ClkInitStruct->SYSCLKSource));
  758. /* HSE is selected as System Clock Source */
  759. if (RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
  760. {
  761. /* Check the HSE ready flag */
  762. if (READ_BIT(RCC->CR, RCC_CR_HSERDY) == 0U)
  763. {
  764. return HAL_ERROR;
  765. }
  766. }
  767. /* HSI is selected as System Clock Source */
  768. else if (RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSISYS)
  769. {
  770. /* Check the HSI ready flag */
  771. if (READ_BIT(RCC->CR, RCC_CR_HSIRDY) == 0U)
  772. {
  773. return HAL_ERROR;
  774. }
  775. }
  776. #if defined(RCC_PLL_SUPPORT)
  777. /* PLL is selected as System Clock Source */
  778. else if (RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
  779. {
  780. /* Check the PLL ready flag */
  781. if (READ_BIT(RCC->CR, RCC_CR_PLLRDY) == 0U)
  782. {
  783. return HAL_ERROR;
  784. }
  785. }
  786. #endif
  787. #if defined(RCC_LSE_SUPPORT)
  788. /* LSE is selected as System Clock Source */
  789. else if (RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_LSE)
  790. {
  791. /* Check the LSE ready flag */
  792. if (READ_BIT(RCC->BDCR, RCC_BDCR_LSERDY) == 0U)
  793. {
  794. return HAL_ERROR;
  795. }
  796. }
  797. #endif
  798. /* LSI is selected as System Clock Source */
  799. else
  800. {
  801. /* Check the LSI ready flag */
  802. if (READ_BIT(RCC->CSR, RCC_CSR_LSIRDY) == 0U)
  803. {
  804. return HAL_ERROR;
  805. }
  806. }
  807. MODIFY_REG(RCC->CFGR, RCC_CFGR_SW, RCC_ClkInitStruct->SYSCLKSource);
  808. /* Get Start Tick*/
  809. tickstart = HAL_GetTick();
  810. while (__HAL_RCC_GET_SYSCLK_SOURCE() != (RCC_ClkInitStruct->SYSCLKSource << RCC_CFGR_SWS_Pos))
  811. {
  812. if ((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
  813. {
  814. return HAL_TIMEOUT;
  815. }
  816. }
  817. }
  818. /* Decreasing the number of wait states because of lower CPU frequency */
  819. if (FLatency < __HAL_FLASH_GET_LATENCY())
  820. {
  821. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  822. __HAL_FLASH_SET_LATENCY(FLatency);
  823. /* Check that the new number of wait states is taken into account to access the Flash
  824. memory by polling the FLASH_ACR register */
  825. tickstart = HAL_GetTick();
  826. while ((FLASH->ACR & FLASH_ACR_LATENCY) != FLatency)
  827. {
  828. if ((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
  829. {
  830. return HAL_TIMEOUT;
  831. }
  832. }
  833. }
  834. /*-------------------------- PCLK1 Configuration ---------------------------*/
  835. if (((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
  836. {
  837. assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB1CLKDivider));
  838. MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE, RCC_ClkInitStruct->APB1CLKDivider);
  839. }
  840. /* Update the SystemCoreClock global variable */
  841. SystemCoreClock = (HAL_RCC_GetSysClockFreq() >> ((AHBPrescTable[(RCC->CFGR & RCC_CFGR_HPRE) >> RCC_CFGR_HPRE_Pos]) & 0x1FU));
  842. /* Configure the source of time base considering new system clocks settings*/
  843. return HAL_InitTick(uwTickPrio);
  844. }
  845. /**
  846. * @}
  847. */
  848. /** @defgroup RCC_Exported_Functions_Group2 Peripheral Control functions
  849. * @brief RCC clocks control functions
  850. *
  851. @verbatim
  852. ===============================================================================
  853. ##### Peripheral Control functions #####
  854. ===============================================================================
  855. [..]
  856. This subsection provides a set of functions allowing to:
  857. (+) Ouput clock to MCO pin.
  858. (+) Retrieve current clock frequencies.
  859. (+) Enable the Clock Security System.
  860. @endverbatim
  861. * @{
  862. */
  863. /**
  864. * @brief Select the clock source to output on MCO pin(PA8).
  865. * @note PA8 should be configured in alternate function mode.
  866. * @param RCC_MCOx specifies the output direction for the clock source.
  867. * @arg @ref RCC_MCO1 Clock source to output on MCO1 pin(PA8).
  868. * @arg @ref RCC_MCO2 Clock source to output on MCO2 pin(PA1).
  869. * @arg @ref RCC_MCO3 Clock source to output on MCO3 pin(PA5).
  870. * @arg @ref RCC_MCO4 Clock source to output on MCO4 pin(PA9).
  871. * @arg @ref RCC_MCO5 Clock source to output on MCO5 pin(PA13).
  872. * @arg @ref RCC_MCO6 Clock source to output on MCO6 pin(PA14).
  873. * @arg @ref RCC_MCO7 Clock source to output on MCO7 pin(PF2).
  874. * @param RCC_MCOSource specifies the clock source to output.
  875. * This parameter can be one of the following values:
  876. * @arg @ref RCC_MCO1SOURCE_NOCLOCK MCO output disabled, no clock on MCO
  877. * @arg @ref RCC_MCO1SOURCE_SYSCLK system clock selected as MCO source
  878. * @arg @ref RCC_MCO1SOURCE_HSI HSI clock selected as MCO source
  879. * @arg @ref RCC_MCO1SOURCE_HSE HSE clock selected as MCO sourcee
  880. * @arg @ref RCC_MCO1SOURCE_PLLCLK main PLL clock selected as MCO source
  881. * @arg @ref RCC_MCO1SOURCE_LSI LSI clock selected as MCO source
  882. * @arg @ref RCC_MCO1SOURCE_LSE LSE clock selected as MCO source
  883. * @note Depending on devices and packages, some clocks may not be available.
  884. * Refer to device datasheet for clocks availability.
  885. * @param RCC_MCODiv specifies the MCO prescaler.
  886. * This parameter can be one of the following values:
  887. * @arg @ref RCC_MCODIV_1 no division applied to MCO clock
  888. * @arg @ref RCC_MCODIV_2 division by 2 applied to MCO clock
  889. * @arg @ref RCC_MCODIV_4 division by 4 applied to MCO clock
  890. * @arg @ref RCC_MCODIV_8 division by 8 applied to MCO clock
  891. * @arg @ref RCC_MCODIV_16 division by 16 applied to MCO clock
  892. * @arg @ref RCC_MCODIV_32 division by 32 applied to MCO clock
  893. * @arg @ref RCC_MCODIV_64 division by 64 applied to MCO clock
  894. * @arg @ref RCC_MCODIV_128 division by 128 applied to MCO clock
  895. * @retval None
  896. */
  897. void HAL_RCC_MCOConfig(uint32_t RCC_MCOx, uint32_t RCC_MCOSource, uint32_t RCC_MCODiv)
  898. {
  899. GPIO_InitTypeDef GPIO_InitStruct;
  900. /* Check the parameters */
  901. assert_param(IS_RCC_MCO(RCC_MCOx));
  902. assert_param(IS_RCC_MCODIV(RCC_MCODiv));
  903. assert_param(IS_RCC_MCO1SOURCE(RCC_MCOSource));
  904. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  905. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  906. GPIO_InitStruct.Pull = GPIO_NOPULL;
  907. switch(RCC_MCOx)
  908. {
  909. case RCC_MCO2 : /* Configure PA01 as the clock output */
  910. __HAL_RCC_GPIOA_CLK_ENABLE();
  911. GPIO_InitStruct.Pin = GPIO_PIN_1;
  912. GPIO_InitStruct.Alternate = GPIO_AF15_MCO;
  913. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  914. break;
  915. case RCC_MCO3 : /* Configure PA05 as the clock output */
  916. __HAL_RCC_GPIOA_CLK_ENABLE();
  917. GPIO_InitStruct.Pin = GPIO_PIN_5;
  918. GPIO_InitStruct.Alternate = GPIO_AF15_MCO;
  919. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  920. break;
  921. case RCC_MCO4 : /* Configure PA09 as the clock output */
  922. __HAL_RCC_GPIOA_CLK_ENABLE();
  923. GPIO_InitStruct.Pin = GPIO_PIN_9;
  924. GPIO_InitStruct.Alternate = GPIO_AF5_MCO;
  925. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  926. break;
  927. case RCC_MCO5 : /* Configure PA13 as the clock output. Note:PA13 is SWD_SWDIO Pin */
  928. __HAL_RCC_GPIOA_CLK_ENABLE();
  929. GPIO_InitStruct.Pin = GPIO_PIN_13;
  930. GPIO_InitStruct.Alternate = GPIO_AF15_MCO;
  931. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  932. break;
  933. case RCC_MCO6 : /* Configure PA14 as the clock output. Note:PA14 is SWD_SWCLK Pin */
  934. __HAL_RCC_GPIOA_CLK_ENABLE();
  935. GPIO_InitStruct.Pin = GPIO_PIN_14;
  936. GPIO_InitStruct.Alternate = GPIO_AF15_MCO;
  937. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  938. break;
  939. case RCC_MCO7 : /* Configure PF02 as the clock output. Note:Defaults to the reset pin, Optionbyte needs to be configured */
  940. __HAL_RCC_GPIOF_CLK_ENABLE();
  941. GPIO_InitStruct.Pin = GPIO_PIN_2;
  942. GPIO_InitStruct.Alternate = GPIO_AF6_MCO;
  943. HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
  944. break;
  945. case RCC_MCO1 : /* PA08 */
  946. default :
  947. __HAL_RCC_GPIOA_CLK_ENABLE();
  948. GPIO_InitStruct.Pin = GPIO_PIN_8;
  949. GPIO_InitStruct.Alternate = GPIO_AF5_MCO;
  950. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  951. break;
  952. }
  953. /* Mask MCOSEL[] and MCOPRE[] bits then set MCO1 clock source and prescaler */
  954. MODIFY_REG(RCC->CFGR, (RCC_CFGR_MCOSEL | RCC_CFGR_MCOPRE), (RCC_MCOSource | RCC_MCODiv));
  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 py32f0xx_hal_conf.h file (default value
  969. * 8 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 py32f0xx_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 py32f0xx_hal_conf.h file (default value
  976. * 32768 Hz).
  977. * @note (****) LSI_VALUE is a constant defined in py32f0xx_hal_conf.h file (default value
  978. * 32768 Hz).
  979. *
  980. * @note Depending on devices and packages, some clocks may not be available.
  981. * Refer to device datasheet for clocks availability.
  982. *
  983. * @note The result of this function could be not correct when using fractional
  984. * value for HSE crystal.
  985. *
  986. * @note This function can be used by the user application to compute the
  987. * baudrate for the communication peripherals or configure other parameters.
  988. *
  989. * @note Each time SYSCLK changes, this function must be called to update the
  990. * right SYSCLK value. Otherwise, any configuration based on this function will be incorrect.
  991. *
  992. *
  993. * @retval SYSCLK frequency
  994. */
  995. uint32_t HAL_RCC_GetSysClockFreq(void)
  996. {
  997. #if defined(RCC_HSIDIV_SUPPORT)
  998. uint32_t hsidiv;
  999. #endif
  1000. uint32_t sysclockfreq;
  1001. const uint32_t hsiValue[5] = {4000000,8000000,16000000,22120000,24000000};
  1002. uint32_t hsiIndex;
  1003. #if defined(RCC_PLL_SUPPORT)
  1004. uint32_t pllsource;
  1005. #endif
  1006. if (__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSI)
  1007. {
  1008. /* HSISYS used as system clock source */
  1009. hsiIndex = (RCC->ICSCR&RCC_ICSCR_HSI_FS_Msk)>>RCC_ICSCR_HSI_FS_Pos;
  1010. if (hsiIndex > 4)
  1011. {
  1012. hsiIndex = 0;
  1013. }
  1014. #if defined(RCC_HSIDIV_SUPPORT)
  1015. /* HSISYS can be derived for HSI */
  1016. hsidiv = (1UL << ((READ_BIT(RCC->CR, RCC_CR_HSIDIV)) >> RCC_CR_HSIDIV_Pos));
  1017. sysclockfreq = (hsiValue[hsiIndex] / hsidiv);
  1018. #else
  1019. sysclockfreq = hsiValue[hsiIndex];
  1020. #endif
  1021. }
  1022. else if (__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSE)
  1023. {
  1024. /* HSE used as system clock source */
  1025. sysclockfreq = HSE_VALUE;
  1026. }
  1027. #if defined(RCC_PLL_SUPPORT)
  1028. else if (__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL)
  1029. {
  1030. pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC);
  1031. switch (pllsource)
  1032. {
  1033. case RCC_PLLSOURCE_HSE: /* HSE used as PLL clock source */
  1034. sysclockfreq = HSE_VALUE * 2;
  1035. break;
  1036. case RCC_PLLSOURCE_HSI: /* HSI used as PLL clock source */
  1037. hsiIndex = (RCC->ICSCR&RCC_ICSCR_HSI_FS_Msk)>>RCC_ICSCR_HSI_FS_Pos;
  1038. if (hsiIndex > 4)
  1039. {
  1040. hsiIndex = 0;
  1041. }
  1042. sysclockfreq = hsiValue[hsiIndex] * 2;
  1043. break;
  1044. default: /* HSI used as PLL clock source */
  1045. sysclockfreq = hsiValue[4] * 2;
  1046. break;
  1047. }
  1048. }
  1049. #endif
  1050. #if defined(RCC_LSE_SUPPORT)
  1051. else if (__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_LSE)
  1052. {
  1053. /* LSE used as system clock source */
  1054. sysclockfreq = LSE_VALUE;
  1055. }
  1056. #endif
  1057. else if (__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_LSI)
  1058. {
  1059. /* LSI used as system clock source */
  1060. sysclockfreq = LSI_VALUE;
  1061. }
  1062. else
  1063. {
  1064. sysclockfreq = 0U;
  1065. }
  1066. return sysclockfreq;
  1067. }
  1068. /**
  1069. * @brief Return the HCLK frequency.
  1070. * @note Each time HCLK changes, this function must be called to update the
  1071. * right HCLK value. Otherwise, any configuration based on this function will be incorrect.
  1072. *
  1073. * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency.
  1074. * @retval HCLK frequency in Hz
  1075. */
  1076. uint32_t HAL_RCC_GetHCLKFreq(void)
  1077. {
  1078. return SystemCoreClock;
  1079. }
  1080. /**
  1081. * @brief Return the PCLK1 frequency.
  1082. * @note Each time PCLK1 changes, this function must be called to update the
  1083. * right PCLK1 value. Otherwise, any configuration based on this function will be incorrect.
  1084. * @retval PCLK1 frequency in Hz
  1085. */
  1086. uint32_t HAL_RCC_GetPCLK1Freq(void)
  1087. {
  1088. /* Get HCLK source and Compute PCLK1 frequency ---------------------------*/
  1089. return (uint32_t)((HAL_RCC_GetHCLKFreq()) >> (APBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE) >> RCC_CFGR_PPRE_Pos] & 0x1FU));
  1090. }
  1091. /**
  1092. * @brief Configure the RCC_OscInitStruct according to the internal
  1093. * RCC configuration registers.
  1094. * @param RCC_OscInitStruct pointer to an RCC_OscInitTypeDef structure that
  1095. * will be configured.
  1096. * @retval None
  1097. */
  1098. void HAL_RCC_GetOscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  1099. {
  1100. /* Check the parameters */
  1101. assert_param(RCC_OscInitStruct != (void *)NULL);
  1102. /* Set all possible values for the Oscillator type parameter ---------------*/
  1103. #if defined(RCC_LSE_SUPPORT)
  1104. RCC_OscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | \
  1105. RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_LSI;
  1106. #else
  1107. RCC_OscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | \
  1108. RCC_OSCILLATORTYPE_LSI;
  1109. #endif
  1110. /* Get the HSE configuration -----------------------------------------------*/
  1111. if ((RCC->CR & RCC_CR_HSEBYP) == RCC_CR_HSEBYP)
  1112. {
  1113. RCC_OscInitStruct->HSEState = RCC_HSE_BYPASS;
  1114. }
  1115. else if ((RCC->CR & RCC_CR_HSEON) == RCC_CR_HSEON)
  1116. {
  1117. RCC_OscInitStruct->HSEState = RCC_HSE_ON;
  1118. }
  1119. else
  1120. {
  1121. RCC_OscInitStruct->HSEState = RCC_HSE_OFF;
  1122. }
  1123. RCC_OscInitStruct->HSEFreq = (RCC->ECSCR & RCC_ECSCR_HSE_FREQ);
  1124. /* Get the HSI configuration -----------------------------------------------*/
  1125. if ((RCC->CR & RCC_CR_HSION) == RCC_CR_HSION)
  1126. {
  1127. RCC_OscInitStruct->HSIState = RCC_HSI_ON;
  1128. }
  1129. else
  1130. {
  1131. RCC_OscInitStruct->HSIState = RCC_HSI_OFF;
  1132. }
  1133. RCC_OscInitStruct->HSICalibrationValue = (RCC->ICSCR & (RCC_ICSCR_HSI_FS | RCC_ICSCR_HSI_TRIM));
  1134. #if defined(RCC_HSIDIV_SUPPORT)
  1135. RCC_OscInitStruct->HSIDiv = (RCC->CR & RCC_CR_HSIDIV);
  1136. #endif
  1137. /* Get the LSI configuration -----------------------------------------------*/
  1138. if ((RCC->CSR & RCC_CSR_LSION) == RCC_CSR_LSION)
  1139. {
  1140. RCC_OscInitStruct->LSIState = RCC_LSI_ON;
  1141. }
  1142. else
  1143. {
  1144. RCC_OscInitStruct->LSIState = RCC_LSI_OFF;
  1145. }
  1146. #if defined(RCC_LSE_SUPPORT)
  1147. /* Get the LSE configuration -----------------------------------------------*/
  1148. if ((RCC->BDCR & RCC_BDCR_LSEBYP) == RCC_BDCR_LSEBYP)
  1149. {
  1150. RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS;
  1151. }
  1152. else if ((RCC->BDCR & RCC_BDCR_LSEON) == RCC_BDCR_LSEON)
  1153. {
  1154. RCC_OscInitStruct->LSEState = RCC_LSE_ON;
  1155. }
  1156. else
  1157. {
  1158. RCC_OscInitStruct->LSEState = RCC_LSE_OFF;
  1159. }
  1160. RCC_OscInitStruct->LSEDriver = (RCC->ECSCR & RCC_ECSCR_LSE_DRIVER);
  1161. #endif
  1162. #if defined(RCC_PLL_SUPPORT)
  1163. /* Get the PLL configuration -----------------------------------------------*/
  1164. if ((RCC->CR & RCC_CR_PLLON) == RCC_CR_PLLON)
  1165. {
  1166. RCC_OscInitStruct->PLL.PLLState = RCC_PLL_ON;
  1167. }
  1168. else
  1169. {
  1170. RCC_OscInitStruct->PLL.PLLState = RCC_PLL_OFF;
  1171. }
  1172. RCC_OscInitStruct->PLL.PLLSource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC);
  1173. #endif
  1174. }
  1175. /**
  1176. * @brief Configure the RCC_ClkInitStruct according to the internal
  1177. * RCC configuration registers.
  1178. * @param RCC_ClkInitStruct Pointer to a @ref RCC_ClkInitTypeDef structure that
  1179. * will be configured.
  1180. * @param pFLatency Pointer on the Flash Latency.
  1181. * @retval None
  1182. */
  1183. void HAL_RCC_GetClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t *pFLatency)
  1184. {
  1185. /* Check the parameters */
  1186. assert_param(RCC_ClkInitStruct != (void *)NULL);
  1187. assert_param(pFLatency != (void *)NULL);
  1188. /* Set all possible values for the Clock type parameter --------------------*/
  1189. RCC_ClkInitStruct->ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1;
  1190. /* Get the SYSCLK configuration --------------------------------------------*/
  1191. RCC_ClkInitStruct->SYSCLKSource = (uint32_t)(RCC->CFGR & RCC_CFGR_SW);
  1192. /* Get the HCLK configuration ----------------------------------------------*/
  1193. RCC_ClkInitStruct->AHBCLKDivider = (uint32_t)(RCC->CFGR & RCC_CFGR_HPRE);
  1194. /* Get the APB1 configuration ----------------------------------------------*/
  1195. RCC_ClkInitStruct->APB1CLKDivider = (uint32_t)(RCC->CFGR & RCC_CFGR_PPRE);
  1196. /* Get the Flash Wait State (Latency) configuration ------------------------*/
  1197. *pFLatency = (uint32_t)(FLASH->ACR & FLASH_ACR_LATENCY);
  1198. }
  1199. /**
  1200. * @brief Enable the Clock Security System.
  1201. * @note If a failure is detected on the HSE oscillator clock, this oscillator
  1202. * is automatically disabled and an interrupt is generated to inform the
  1203. * software about the failure (Clock Security System Interrupt, CSSI),
  1204. * allowing the MCU to perform rescue operations. The CSSI is linked to
  1205. * the Cortex-M0+ NMI (Non-Maskable Interrupt) exception vector.
  1206. * @note The Clock Security System can only be cleared by reset.
  1207. * @retval None
  1208. */
  1209. void HAL_RCC_EnableCSS(void)
  1210. {
  1211. SET_BIT(RCC->CR, RCC_CR_CSSON) ;
  1212. }
  1213. #if defined(RCC_LSE_SUPPORT)
  1214. /**
  1215. * @brief Enable the LSE Clock Security System.
  1216. * @note If a failure is detected on the LSE oscillator clock, this oscillator
  1217. * is automatically disabled and an interrupt is generated to inform the
  1218. * software about the failure (Clock Security System Interrupt, CSSI),
  1219. * allowing the MCU to perform rescue operations. The CSSI is linked to
  1220. * the Cortex-M0+ NMI (Non-Maskable Interrupt) exception vector.
  1221. * @note The LSE Clock Security System Detection bit (LSECSSD in BDCR) can only be
  1222. * cleared by a backup domain reset.
  1223. * @retval None
  1224. */
  1225. void HAL_RCC_EnableLSECSS(void)
  1226. {
  1227. SET_BIT(RCC->BDCR, RCC_BDCR_LSECSSON) ;
  1228. }
  1229. /**
  1230. * @brief Disable the LSE Clock Security System.
  1231. * @note After LSE failure detection, the software must disable LSECSSON
  1232. * @note The Clock Security System can only be cleared by reset otherwise.
  1233. * @retval None
  1234. */
  1235. void HAL_RCC_DisableLSECSS(void)
  1236. {
  1237. CLEAR_BIT(RCC->BDCR, RCC_BDCR_LSECSSON) ;
  1238. }
  1239. #endif
  1240. /**
  1241. * @brief Handle the RCC Clock Security System interrupt request.
  1242. * @note This API should be called under the NMI_Handler().
  1243. * @retval None
  1244. */
  1245. void HAL_RCC_NMI_IRQHandler(void)
  1246. {
  1247. uint32_t itflag = RCC->CIFR;
  1248. /* Clear interrupt flags related to CSS */
  1249. #if defined(RCC_LSE_SUPPORT)
  1250. RCC->CICR = (itflag & (RCC_CIFR_CSSF | RCC_CIFR_LSECSSF));
  1251. #else
  1252. RCC->CICR = (itflag & RCC_CIFR_CSSF);
  1253. #endif
  1254. /* Check RCC CSSF interrupt flag */
  1255. if ((itflag & RCC_CIFR_CSSF) != 0x00u)
  1256. {
  1257. /* RCC Clock Security System interrupt user callback */
  1258. HAL_RCC_CSSCallback();
  1259. }
  1260. #if defined(RCC_LSE_SUPPORT)
  1261. /* Check RCC LSECSSF interrupt flag */
  1262. if ((itflag & RCC_CIFR_LSECSSF) != 0x00u)
  1263. {
  1264. /* RCC Clock Security System interrupt user callback */
  1265. HAL_RCC_LSECSSCallback();
  1266. }
  1267. #endif
  1268. }
  1269. #if defined(RCC_PLL_SUPPORT)
  1270. /**
  1271. * @brief Get HSI frequency.
  1272. * @retval HSI frequency
  1273. */
  1274. static uint32_t RCC_GetHSIFreq(void)
  1275. {
  1276. uint32_t hsifs;
  1277. hsifs = ((READ_BIT(RCC->ICSCR, RCC_ICSCR_HSI_FS)) >> RCC_ICSCR_HSI_FS_Pos);
  1278. return HSIFreqTable[hsifs%8];
  1279. }
  1280. #endif
  1281. /**
  1282. * @brief Handle the RCC HSE Clock Security System interrupt callback.
  1283. * @retval none
  1284. */
  1285. __weak void HAL_RCC_CSSCallback(void)
  1286. {
  1287. /* NOTE : This function should not be modified, when the callback is needed,
  1288. the @ref HAL_RCC_CSSCallback should be implemented in the user file
  1289. */
  1290. }
  1291. #if defined(RCC_LSE_SUPPORT)
  1292. /**
  1293. * @brief RCC LSE Clock Security System interrupt callback.
  1294. * @retval none
  1295. */
  1296. __weak void HAL_RCC_LSECSSCallback(void)
  1297. {
  1298. /* NOTE : This function should not be modified, when the callback is needed,
  1299. the HAL_RCC_LSECSSCallback should be implemented in the user file
  1300. */
  1301. }
  1302. #endif
  1303. /**
  1304. * @}
  1305. */
  1306. /**
  1307. * @}
  1308. */
  1309. #endif /* HAL_RCC_MODULE_ENABLED */
  1310. /**
  1311. * @}
  1312. */
  1313. /**
  1314. * @}
  1315. */
  1316. /************************ (C) COPYRIGHT Puya *****END OF FILE****/