You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1841 lines
63 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32wbxx_hal_rcc.c
  4. * @author MCD Application Team
  5. * @brief RCC HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Reset and Clock Control (RCC) peripheral:
  8. * + Initialization and de-initialization functions
  9. * + Peripheral Control functions
  10. *
  11. @verbatim
  12. ==============================================================================
  13. ##### RCC specific features #####
  14. ==============================================================================
  15. [..]
  16. After reset the device is running from Multiple Speed Internal oscillator
  17. (4 MHz) with Flash 0 wait state. Flash prefetch buffer, D-Cache
  18. and I-Cache are disabled, and all peripherals are off except internal
  19. SRAM, Flash and JTAG.
  20. (+) There is no prescaler on High speed (AHBs) and Low speed (APBs) buses:
  21. all peripherals mapped on these buses are running at MSI speed.
  22. (+) The clock for all peripherals is switched off, except the SRAM and FLASH.
  23. (+) All GPIOs are in analog mode, except the JTAG pins which
  24. are assigned to be used for debug purpose.
  25. [..]
  26. Once the device started from reset, the user application has to:
  27. (+) Configure the clock source to be used to drive the System clock
  28. (if the application needs higher frequency/performance)
  29. (+) Configure the System clock frequency and Flash settings
  30. (+) Configure the AHB and APB buses prescalers
  31. (+) Enable the clock for the peripheral(s) to be used
  32. (+) Configure the clock source(s) for peripherals which clocks are not
  33. derived from the System clock (SAI1, RTC, ADC, USB/RNG, USART1, LPUART1, LPTIMx, I2Cx, SMPS)
  34. @endverbatim
  35. ******************************************************************************
  36. * @attention
  37. *
  38. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  39. * All rights reserved.</center></h2>
  40. *
  41. * This software component is licensed by ST 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. */
  48. /* Includes ------------------------------------------------------------------*/
  49. #include "stm32wbxx_hal.h"
  50. /** @addtogroup STM32WBxx_HAL_Driver
  51. * @{
  52. */
  53. /** @defgroup RCC RCC
  54. * @brief RCC HAL module driver
  55. * @{
  56. */
  57. #ifdef HAL_RCC_MODULE_ENABLED
  58. /* Private typedef -----------------------------------------------------------*/
  59. /* Private define ------------------------------------------------------------*/
  60. /** @defgroup RCC_Private_Constants RCC Private Constants
  61. * @{
  62. */
  63. #define HSE_TIMEOUT_VALUE HSE_STARTUP_TIMEOUT
  64. #define HSI_TIMEOUT_VALUE (2U) /* 2 ms (minimum Tick + 1) */
  65. #define MSI_TIMEOUT_VALUE (2U) /* 2 ms (minimum Tick + 1) */
  66. #define LSI1_TIMEOUT_VALUE (2U) /* 2 ms (minimum Tick + 1) */
  67. #define LSI2_TIMEOUT_VALUE (3U) /* to be adjusted with DS */
  68. #define HSI48_TIMEOUT_VALUE (2U) /* 2 ms (minimum Tick + 1) */
  69. #define PLL_TIMEOUT_VALUE (2U) /* 2 ms (minimum Tick + 1) */
  70. #if defined(SAI1)
  71. #define PLLSAI1_TIMEOUT_VALUE (2U) /* 2 ms (minimum Tick + 1) */
  72. #endif
  73. #define PRESCALER_TIMEOUT_VALUE (2U) /* 2 ms (minimum Tick + 1) */
  74. #define LATENCY_TIMEOUT_VALUE (2U) /* 2 ms (minimum Tick + 1) */
  75. #define CLOCKSWITCH_TIMEOUT_VALUE (5000U) /* 5 s */
  76. #define PLLSOURCE_NONE (0U)
  77. #define MEGA_HZ 1000000U /* Division factor to convert Hz in Mhz */
  78. /**
  79. * @}
  80. */
  81. /* Private macro -------------------------------------------------------------*/
  82. /** @defgroup RCC_Private_Macros RCC Private Macros
  83. * @{
  84. */
  85. #define __MCO1_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
  86. #define MCO1_GPIO_PORT GPIOA
  87. #define MCO1_PIN GPIO_PIN_8
  88. #define __MCO2_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
  89. #define MCO2_GPIO_PORT GPIOB
  90. #define MCO2_PIN GPIO_PIN_6
  91. #define __MCO3_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
  92. #define MCO3_GPIO_PORT GPIOA
  93. #define MCO3_PIN GPIO_PIN_15
  94. #define RCC_PLL_OSCSOURCE_CONFIG(__HAL_RCC_PLLSOURCE__) \
  95. (MODIFY_REG(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC, (uint32_t)(__HAL_RCC_PLLSOURCE__)))
  96. #define __COUNTOF(_A_) (sizeof(_A_) / sizeof(*(_A_)))
  97. /**
  98. * @}
  99. */
  100. /* Private variables ---------------------------------------------------------*/
  101. /** @defgroup RCC_Private_Variables RCC Private Variables
  102. * @{
  103. */
  104. /**
  105. * @}
  106. */
  107. /* Private function prototypes -----------------------------------------------*/
  108. /** @defgroup RCC_Private_Functions RCC Private Functions
  109. * @{
  110. */
  111. static HAL_StatusTypeDef RCC_SetFlashLatencyFromMSIRange(uint32_t MSI_Range);
  112. static HAL_StatusTypeDef RCC_SetFlashLatency(uint32_t Flash_ClkSrcFreq, uint32_t VCORE_Voltage);
  113. /**
  114. * @}
  115. */
  116. /* Exported functions --------------------------------------------------------*/
  117. /** @defgroup RCC_Exported_Functions RCC Exported Functions
  118. * @{
  119. */
  120. /** @defgroup RCC_Exported_Functions_Group1 Initialization and de-initialization functions
  121. * @brief Initialization and Configuration functions
  122. *
  123. @verbatim
  124. ===============================================================================
  125. ##### Initialization and de-initialization functions #####
  126. ===============================================================================
  127. [..]
  128. This section provides functions allowing to configure the internal and external oscillators
  129. (HSE, HSI, LSE, MSI, LSI1, LSI2, PLL, CSS and MCO) and the System buses clocks (SYSCLK, HCLK1, HCLK2, HCLK4, PCLK1
  130. and PCLK2).
  131. [..] Internal/external clock and PLL configuration
  132. (+) HSI (high-speed internal): 16 MHz factory-trimmed RC used directly or through
  133. the PLL as System clock source.
  134. (+) MSI (Mutiple Speed Internal): Its frequency is software trimmable from 100KHZ to 48MHZ.
  135. It can be used to generate the clock for the USB FS (48 MHz).
  136. The number of flash wait states is automatically adjusted when MSI range is updated with
  137. HAL_RCC_OscConfig() and the MSI is used as System clock source.
  138. (+) LSI1/LSI2 (low-speed internal): 32 KHz low consumption RC used as IWDG and/or RTC
  139. clock source.
  140. (+) HSE (high-speed external): 32 MHz crystal oscillator used directly or
  141. through the PLL as System clock source. Can be used also optionally as RTC clock source.
  142. (+) LSE (low-speed external): 32.768 KHz oscillator used optionally as RTC clock source
  143. or the RF system Auto-wakeup from Stop and Standby modes.
  144. (+) PLL (clocked by HSI, HSE or MSI) providing up to three independent output clocks:
  145. (++) The first output is used to generate the high speed system clock (up to 64MHz).
  146. (++) The second output is used to generate the clock for the USB FS (48 MHz),
  147. the random analog generator (<=48 MHz)
  148. (++) The third output is used to generate an accurate clock to achieve
  149. high-quality audio performance on SAI interface.
  150. (+) PLLSAI1 (clocked by HSI, HSE or MSI) providing up to three independent output clocks:
  151. (++) The first output is used to generate SAR ADC clock.
  152. (++) The second output is used to generate the clock for the USB FS (48 MHz),
  153. the random analog generator (<=48 MHz).
  154. (++) The Third output is used to generate an accurate clock to achieve
  155. high-quality audio performance on SAI interface.
  156. (+) CSS (Clock security system): once enabled, if a HSE clock failure occurs
  157. (HSE used directly or through PLL as System clock source), the System clock
  158. is automatically switched to MSI or the HSI oscillator (depending on the
  159. STOPWUCK configuration) and an interrupt is generated if enabled.
  160. The interrupt is linked to the CPU1 and CPU2 NMI (Non-Maskable Interrupt) exception vector.
  161. (+) LSECSS: once enabled, if a LSE clock failure occurs, the LSE
  162. clock is no longer supplied to the RTC but no hardware action is made to the registers. If the
  163. MSI was in PLL-mode, this mode is disabled.
  164. In Standby mode a wakeup is generated. In other modes an interrupt can be sent to wakeup
  165. the software
  166. (+) MCO (microcontroller clock output): used to output MSI, LSI1, LSI2, HSI, LSE, HSE (before and
  167. after stabilization), SYSCLK, HSI48 or main PLL clock (through a configurable prescaler) on PA8, PB6 & PA15 pins.
  168. [..] System, AHB and APB buses clocks configuration
  169. (+) Several clock sources can be used to drive the System clock (SYSCLK): MSI, HSI,
  170. HSE and main PLL.
  171. The AHB clock (HCLK1) is derived from System clock through configurable
  172. prescaler and used to clock the CPU, memory and peripherals mapped
  173. on AHB bus (DMA, GPIO...). APB1 (PCLK1) and APB2 (PCLK2) clocks are derived
  174. from AHB clock through configurable prescalers and used to clock
  175. the peripherals mapped on these buses. You can use
  176. HAL_RCC_GetSysClockFreq() function to retrieve the frequencies of these clocks.
  177. The AHB4 clock (HCLK4) is derived from System clock through configurable
  178. prescaler and used to clock the FLASH
  179. -@- All the peripheral clocks are derived from the System clock (SYSCLK) except:
  180. (+@) SAI: the SAI clock can be derived either from a specific PLL (PLLSAI1) or (PLLSYS) or
  181. from an external clock mapped on the SAI_CKIN pin.
  182. You have to use HAL_RCCEx_PeriphCLKConfig() function to configure this clock.
  183. (+@) RTC: the RTC clock can be derived either from the LSI, LSE or HSE clock
  184. divided by 32.
  185. You have to use __HAL_RCC_RTC_ENABLE() and HAL_RCCEx_PeriphCLKConfig() function
  186. to configure this clock.
  187. (+@) USB FS and RNG: USB FS requires a frequency equal to 48 MHz
  188. to work correctly, while RNG peripherals requires a frequency
  189. equal or lower than to 48 MHz. This clock is derived of the main PLL or PLLSAI1
  190. through PLLQ divider. You have to enable the peripheral clock and use
  191. HAL_RCCEx_PeriphCLKConfig() function to configure this clock.
  192. (+@) IWDG clock which is always the LSI clock.
  193. (+) The maximum frequency of the SYSCLK, HCLK1, HCLK4, PCLK1 and PCLK2 is 64 MHz.
  194. The maximum frequency of the HCLK2 is 32 MHz.
  195. The clock source frequency should be adapted depending on the device voltage range
  196. as listed in the Reference Manual "Clock source frequency versus voltage scaling" chapter.
  197. @endverbatim
  198. Table 1. HCLK4 clock frequency.
  199. +-------------------------------------------------------+
  200. | Latency | HCLK4 clock frequency (MHz) |
  201. | |-------------------------------------|
  202. | | voltage range 1 | voltage range 2 |
  203. | | 1.2 V | 1.0 V |
  204. |-----------------|------------------|------------------|
  205. |0WS(1 CPU cycles)| HCLK4 <= 18 | HCLK4 <= 6 |
  206. |-----------------|------------------|------------------|
  207. |1WS(2 CPU cycles)| HCLK4 <= 36 | HCLK4 <= 12 |
  208. |-----------------|------------------|------------------|
  209. |2WS(3 CPU cycles)| HCLK4 <= 54 | HCLK4 <= 16 |
  210. |-----------------|------------------|------------------|
  211. |3WS(4 CPU cycles)| HCLK4 <= 64 | HCLK4 <= n.a. |
  212. |-----------------|------------------|------------------|
  213. * @{
  214. */
  215. /**
  216. * @brief Reset the RCC clock configuration to the default reset state.
  217. * @note The default reset state of the clock configuration is given below:
  218. * - MSI ON and used as system clock source
  219. * - HSE, HSI, PLL, PLLSAI1
  220. * - HCLK1, HCLK2, HCLK4, PCLK1 and PCLK2 prescalers set to 1.
  221. * - CSS, MCO OFF
  222. * - All interrupts disabled
  223. * @note This function doesn't modify the configuration of the
  224. * - Peripheral clocks
  225. * - LSI, LSE and RTC clocks
  226. * @retval HAL status
  227. */
  228. HAL_StatusTypeDef HAL_RCC_DeInit(void)
  229. {
  230. uint32_t tickstart;
  231. /* Get Start Tick*/
  232. tickstart = HAL_GetTick();
  233. /* MSI PLL OFF */
  234. LL_RCC_MSI_DisablePLLMode();
  235. /* Set MSION bit */
  236. LL_RCC_MSI_Enable();
  237. /* Wait till MSI is ready */
  238. while (LL_RCC_MSI_IsReady() == 0U)
  239. {
  240. if ((HAL_GetTick() - tickstart) > MSI_TIMEOUT_VALUE)
  241. {
  242. return HAL_TIMEOUT;
  243. }
  244. }
  245. /* Set MSIRANGE default value */
  246. LL_RCC_MSI_SetRange(LL_RCC_MSIRANGE_6);
  247. /* Set MSITRIM bits to the reset value*/
  248. LL_RCC_MSI_SetCalibTrimming(0);
  249. /* Set HSITRIM bits to the reset value*/
  250. LL_RCC_HSI_SetCalibTrimming(0x40U);
  251. /* Get Start Tick*/
  252. tickstart = HAL_GetTick();
  253. /* Reset CFGR register (MSI is selected as system clock source) */
  254. CLEAR_REG(RCC->CFGR);
  255. /* Wait till MSI is ready */
  256. while (READ_BIT(RCC->CFGR, RCC_CFGR_SWS) != 0U)
  257. {
  258. if ((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
  259. {
  260. return HAL_TIMEOUT;
  261. }
  262. }
  263. /* Reset HSION, HSIKERON, HSIASFS, HSEON, PLLON, PLLSAI11ON, HSEPRE bits */
  264. #if defined(SAI1)
  265. CLEAR_BIT(RCC->CR, RCC_CR_HSION | RCC_CR_HSIKERON | RCC_CR_HSIASFS | RCC_CR_HSEON | RCC_CR_HSEPRE | RCC_CR_PLLON | RCC_CR_PLLSAI1ON);
  266. #else
  267. CLEAR_BIT(RCC->CR, RCC_CR_HSION | RCC_CR_HSIKERON | RCC_CR_HSIASFS | RCC_CR_HSEON | RCC_CR_HSEPRE | RCC_CR_PLLON);
  268. #endif
  269. /* Get Start Tick*/
  270. tickstart = HAL_GetTick();
  271. /* Wait till PLL is ready */
  272. while (LL_RCC_PLL_IsReady() != 0U)
  273. {
  274. if ((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  275. {
  276. return HAL_TIMEOUT;
  277. }
  278. }
  279. /* once PLL is OFF, reset PLLCFGR register to default value */
  280. WRITE_REG(RCC->PLLCFGR, RCC_PLLCFGR_PLLR_0 | RCC_PLLCFGR_PLLQ_0 | RCC_PLLCFGR_PLLP_1 | RCC_PLLCFGR_PLLN_0);
  281. #if defined(SAI1)
  282. /* Get Start Tick*/
  283. tickstart = HAL_GetTick();
  284. /* Wait till PLL is ready */
  285. while (LL_RCC_PLLSAI1_IsReady() != 0U)
  286. {
  287. if ((HAL_GetTick() - tickstart) > PLLSAI1_TIMEOUT_VALUE)
  288. {
  289. return HAL_TIMEOUT;
  290. }
  291. }
  292. /* once PLLSAI1 is OFF, reset PLLSAI1CFGR register to default value */
  293. WRITE_REG(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLR_0 | RCC_PLLSAI1CFGR_PLLQ_0 | RCC_PLLSAI1CFGR_PLLP_1 | RCC_PLLSAI1CFGR_PLLN_0);
  294. #endif
  295. /* Disable all interrupts */
  296. CLEAR_REG(RCC->CIER);
  297. /* Clear all interrupt flags */
  298. WRITE_REG(RCC->CICR, 0xFFFFFFFFU);
  299. /* EXTCFGR reset*/
  300. LL_RCC_WriteReg(EXTCFGR, 0x00030000U);
  301. /* Update the SystemCoreClock global variable */
  302. SystemCoreClock = MSI_VALUE;
  303. /* Adapt Systick interrupt period */
  304. if (HAL_InitTick(uwTickPrio) != HAL_OK)
  305. {
  306. return HAL_ERROR;
  307. }
  308. else
  309. {
  310. return HAL_OK;
  311. }
  312. }
  313. /**
  314. * @brief Initialize the RCC Oscillators according to the specified parameters in the
  315. * @ref RCC_OscInitTypeDef.
  316. * @param RCC_OscInitStruct pointer to a @ref RCC_OscInitTypeDef structure that
  317. * contains the configuration information for the RCC Oscillators.
  318. * @note The PLL is not disabled when used as system clock.
  319. * @note The PLL source is not updated when used as PLLSAI1 clock source.
  320. * @retval HAL status
  321. */
  322. HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  323. {
  324. uint32_t tickstart;
  325. /* Check Null pointer */
  326. if (RCC_OscInitStruct == NULL)
  327. {
  328. return HAL_ERROR;
  329. }
  330. /* Check the parameters */
  331. assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct->OscillatorType));
  332. /*----------------------------- MSI Configuration --------------------------*/
  333. if (((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_MSI) == RCC_OSCILLATORTYPE_MSI)
  334. {
  335. /* Check the parameters */
  336. assert_param(IS_RCC_MSI(RCC_OscInitStruct->MSIState));
  337. assert_param(IS_RCC_MSICALIBRATION_VALUE(RCC_OscInitStruct->MSICalibrationValue));
  338. assert_param(IS_RCC_MSI_CLOCK_RANGE(RCC_OscInitStruct->MSIClockRange));
  339. /* When the MSI is used as system clock it will not be disabled */
  340. const uint32_t temp_sysclksrc = __HAL_RCC_GET_SYSCLK_SOURCE();
  341. const uint32_t temp_plloscsrc = __HAL_RCC_GET_PLL_OSCSOURCE();
  342. if ((temp_sysclksrc == RCC_SYSCLKSOURCE_STATUS_MSI) ||
  343. ((temp_sysclksrc == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && (temp_plloscsrc == RCC_PLLSOURCE_MSI)))
  344. {
  345. if ((LL_RCC_MSI_IsReady() != 0U) && (RCC_OscInitStruct->MSIState == RCC_MSI_OFF))
  346. {
  347. return HAL_ERROR;
  348. }
  349. /* Otherwise, just the calibration and MSI range change are allowed */
  350. else
  351. {
  352. /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
  353. must be correctly programmed according to the frequency of the AHB4 clock
  354. and the supply voltage of the device. */
  355. if (RCC_OscInitStruct->MSIClockRange > __HAL_RCC_GET_MSI_RANGE())
  356. {
  357. /* First increase number of wait states update if necessary */
  358. if (RCC_SetFlashLatencyFromMSIRange(RCC_OscInitStruct->MSIClockRange) != HAL_OK)
  359. {
  360. return HAL_ERROR;
  361. }
  362. /* Selects the Multiple Speed oscillator (MSI) clock range .*/
  363. __HAL_RCC_MSI_RANGE_CONFIG(RCC_OscInitStruct->MSIClockRange);
  364. /* Adjusts the Multiple Speed oscillator (MSI) calibration value.*/
  365. __HAL_RCC_MSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->MSICalibrationValue);
  366. }
  367. else
  368. {
  369. /* Else, keep current flash latency while decreasing applies */
  370. /* Selects the Multiple Speed oscillator (MSI) clock range .*/
  371. __HAL_RCC_MSI_RANGE_CONFIG(RCC_OscInitStruct->MSIClockRange);
  372. /* Adjusts the Multiple Speed oscillator (MSI) calibration value.*/
  373. __HAL_RCC_MSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->MSICalibrationValue);
  374. /* Decrease number of wait states update if necessary */
  375. if (RCC_SetFlashLatencyFromMSIRange(RCC_OscInitStruct->MSIClockRange) != HAL_OK)
  376. {
  377. return HAL_ERROR;
  378. }
  379. }
  380. /* Update the SystemCoreClock global variable */
  381. SystemCoreClock = HAL_RCC_GetHCLKFreq();
  382. if (HAL_InitTick(uwTickPrio) != HAL_OK)
  383. {
  384. return HAL_ERROR;
  385. }
  386. }
  387. }
  388. else
  389. {
  390. /* Check the MSI State */
  391. if (RCC_OscInitStruct->MSIState != RCC_MSI_OFF)
  392. {
  393. /* Enable the Internal High Speed oscillator (MSI). */
  394. __HAL_RCC_MSI_ENABLE();
  395. /* Get timeout */
  396. tickstart = HAL_GetTick();
  397. /* Wait till MSI is ready */
  398. while (LL_RCC_MSI_IsReady() == 0U)
  399. {
  400. if ((HAL_GetTick() - tickstart) > MSI_TIMEOUT_VALUE)
  401. {
  402. return HAL_TIMEOUT;
  403. }
  404. }
  405. /* Selects the Multiple Speed oscillator (MSI) clock range .*/
  406. __HAL_RCC_MSI_RANGE_CONFIG(RCC_OscInitStruct->MSIClockRange);
  407. /* Adjusts the Multiple Speed oscillator (MSI) calibration value.*/
  408. __HAL_RCC_MSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->MSICalibrationValue);
  409. }
  410. else
  411. {
  412. /* Disable the Internal High Speed oscillator (MSI). */
  413. __HAL_RCC_MSI_DISABLE();
  414. /* Get timeout */
  415. tickstart = HAL_GetTick();
  416. /* Wait till MSI is disabled */
  417. while (LL_RCC_MSI_IsReady() != 0U)
  418. {
  419. if ((HAL_GetTick() - tickstart) > MSI_TIMEOUT_VALUE)
  420. {
  421. return HAL_TIMEOUT;
  422. }
  423. }
  424. }
  425. }
  426. }
  427. /*------------------------------- HSE Configuration ------------------------*/
  428. if (((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE)
  429. {
  430. /* Check the parameters */
  431. assert_param(IS_RCC_HSE(RCC_OscInitStruct->HSEState));
  432. /* When the HSE is used as system clock or clock source for PLL in these cases it is not allowed to be disabled */
  433. const uint32_t temp_sysclksrc = __HAL_RCC_GET_SYSCLK_SOURCE();
  434. const uint32_t temp_plloscsrc = __HAL_RCC_GET_PLL_OSCSOURCE();
  435. if ((temp_sysclksrc == RCC_SYSCLKSOURCE_STATUS_HSE) ||
  436. ((temp_sysclksrc == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && (temp_plloscsrc == RCC_PLLSOURCE_HSE)))
  437. {
  438. if ((LL_RCC_HSE_IsReady() != 0U) && (RCC_OscInitStruct->HSEState == RCC_HSE_OFF))
  439. {
  440. return HAL_ERROR;
  441. }
  442. }
  443. else
  444. {
  445. /* Set the new HSE configuration ---------------------------------------*/
  446. __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct->HSEState);
  447. /* Check the HSE State */
  448. if (RCC_OscInitStruct->HSEState != RCC_HSE_OFF)
  449. {
  450. /* Get Start Tick*/
  451. tickstart = HAL_GetTick();
  452. /* Wait till HSE is ready */
  453. while (LL_RCC_HSE_IsReady() == 0U)
  454. {
  455. if ((HAL_GetTick() - tickstart) > HSE_TIMEOUT_VALUE)
  456. {
  457. return HAL_TIMEOUT;
  458. }
  459. }
  460. }
  461. else
  462. {
  463. /* Get Start Tick*/
  464. tickstart = HAL_GetTick();
  465. /* Wait till HSE is disabled */
  466. while (LL_RCC_HSE_IsReady() != 0U)
  467. {
  468. if ((HAL_GetTick() - tickstart) > HSE_TIMEOUT_VALUE)
  469. {
  470. return HAL_TIMEOUT;
  471. }
  472. }
  473. }
  474. }
  475. }
  476. /*----------------------------- HSI Configuration --------------------------*/
  477. if (((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI)
  478. {
  479. /* Check the parameters */
  480. assert_param(IS_RCC_HSI(RCC_OscInitStruct->HSIState));
  481. assert_param(IS_RCC_HSI_CALIBRATION_VALUE(RCC_OscInitStruct->HSICalibrationValue));
  482. /* Check if HSI is used as system clock or as PLL source when PLL is selected as system clock */
  483. const uint32_t temp_sysclksrc = __HAL_RCC_GET_SYSCLK_SOURCE();
  484. const uint32_t temp_plloscsrc = __HAL_RCC_GET_PLL_OSCSOURCE();
  485. if ((temp_sysclksrc == RCC_SYSCLKSOURCE_STATUS_HSI) ||
  486. ((temp_sysclksrc == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && (temp_plloscsrc == RCC_PLLSOURCE_HSI)))
  487. {
  488. /* When HSI is used as system clock it will not be disabled */
  489. if ((LL_RCC_HSI_IsReady() != 0U) && (RCC_OscInitStruct->HSIState == RCC_HSI_OFF))
  490. {
  491. return HAL_ERROR;
  492. }
  493. /* Otherwise, just the calibration is allowed */
  494. else
  495. {
  496. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  497. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  498. }
  499. }
  500. else
  501. {
  502. /* Check the HSI State */
  503. if (RCC_OscInitStruct->HSIState != RCC_HSI_OFF)
  504. {
  505. /* Enable the Internal High Speed oscillator (HSI). */
  506. __HAL_RCC_HSI_ENABLE();
  507. /* Get Start Tick*/
  508. tickstart = HAL_GetTick();
  509. /* Wait till HSI is ready */
  510. while (LL_RCC_HSI_IsReady() == 0U)
  511. {
  512. if ((HAL_GetTick() - tickstart) > HSI_TIMEOUT_VALUE)
  513. {
  514. return HAL_TIMEOUT;
  515. }
  516. }
  517. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  518. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  519. }
  520. else
  521. {
  522. /* Disable the Internal High Speed oscillator (HSI). */
  523. __HAL_RCC_HSI_DISABLE();
  524. /* Get Start Tick*/
  525. tickstart = HAL_GetTick();
  526. /* Wait till HSI is disabled */
  527. while (LL_RCC_HSI_IsReady() != 0U)
  528. {
  529. if ((HAL_GetTick() - tickstart) > HSI_TIMEOUT_VALUE)
  530. {
  531. return HAL_TIMEOUT;
  532. }
  533. }
  534. }
  535. }
  536. }
  537. /*------------------------------ LSI Configuration (LSI1 or LSI2) -------------------------*/
  538. if ((((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI1) == RCC_OSCILLATORTYPE_LSI1) || \
  539. (((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI2) == RCC_OSCILLATORTYPE_LSI2))
  540. {
  541. /* Check the parameters */
  542. assert_param(IS_RCC_LSI(RCC_OscInitStruct->LSIState));
  543. /* Check the LSI State */
  544. if (RCC_OscInitStruct->LSIState != RCC_LSI_OFF)
  545. {
  546. /*------------------------------ LSI2 selected by default (when Switch ON) -------------------------*/
  547. if (((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI2) == RCC_OSCILLATORTYPE_LSI2)
  548. {
  549. assert_param(IS_RCC_LSI2_CALIBRATION_VALUE(RCC_OscInitStruct->LSI2CalibrationValue));
  550. /* 1. Check LSI1 state and enable if required */
  551. if (LL_RCC_LSI1_IsReady() == 0U)
  552. {
  553. /* This is required to enable LSI1 before enabling LSI2 */
  554. __HAL_RCC_LSI1_ENABLE();
  555. /* Get Start Tick*/
  556. tickstart = HAL_GetTick();
  557. /* Wait till LSI1 is ready */
  558. while (LL_RCC_LSI1_IsReady() == 0U)
  559. {
  560. if ((HAL_GetTick() - tickstart) > LSI1_TIMEOUT_VALUE)
  561. {
  562. return HAL_TIMEOUT;
  563. }
  564. }
  565. }
  566. /* 2. Enable the Internal Low Speed oscillator (LSI2) and set trimming value */
  567. __HAL_RCC_LSI2_ENABLE();
  568. /* Get Start Tick*/
  569. tickstart = HAL_GetTick();
  570. /* Wait till LSI2 is ready */
  571. while (LL_RCC_LSI2_IsReady() == 0U)
  572. {
  573. if ((HAL_GetTick() - tickstart) > LSI2_TIMEOUT_VALUE)
  574. {
  575. return HAL_TIMEOUT;
  576. }
  577. }
  578. /* Adjusts the Internal Low Spee oscillator (LSI2) calibration value */
  579. __HAL_RCC_LSI2_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->LSI2CalibrationValue);
  580. /* 3. Disable LSI1 */
  581. /* LSI1 was initially not enable, require to disable it */
  582. __HAL_RCC_LSI1_DISABLE();
  583. /* Get Start Tick*/
  584. tickstart = HAL_GetTick();
  585. /* Wait till LSI1 is disabled */
  586. while (LL_RCC_LSI1_IsReady() != 0U)
  587. {
  588. if ((HAL_GetTick() - tickstart) > LSI1_TIMEOUT_VALUE)
  589. {
  590. return HAL_TIMEOUT;
  591. }
  592. }
  593. }
  594. else
  595. {
  596. /*------------------------------ LSI1 selected (only if LSI2 OFF)-------------------------*/
  597. /* 1. Enable the Internal Low Speed oscillator (LSI1). */
  598. __HAL_RCC_LSI1_ENABLE();
  599. /* Get Start Tick*/
  600. tickstart = HAL_GetTick();
  601. /* Wait till LSI1 is ready */
  602. while (LL_RCC_LSI1_IsReady() == 0U)
  603. {
  604. if ((HAL_GetTick() - tickstart) > LSI1_TIMEOUT_VALUE)
  605. {
  606. return HAL_TIMEOUT;
  607. }
  608. }
  609. /*2. Switch OFF LSI2*/
  610. /* Disable the Internal Low Speed oscillator (LSI2). */
  611. __HAL_RCC_LSI2_DISABLE();
  612. /* Wait till LSI2 is disabled */
  613. while (LL_RCC_LSI2_IsReady() != 0U)
  614. {
  615. if ((HAL_GetTick() - tickstart) > LSI2_TIMEOUT_VALUE)
  616. {
  617. return HAL_TIMEOUT;
  618. }
  619. }
  620. }
  621. }
  622. else
  623. {
  624. /* Disable the Internal Low Speed oscillator (LSI2). */
  625. __HAL_RCC_LSI2_DISABLE();
  626. /* Get Start Tick*/
  627. tickstart = HAL_GetTick();
  628. /* Wait till LSI2 is disabled */
  629. while (LL_RCC_LSI2_IsReady() != 0U)
  630. {
  631. if ((HAL_GetTick() - tickstart) > LSI2_TIMEOUT_VALUE)
  632. {
  633. return HAL_TIMEOUT;
  634. }
  635. }
  636. /* Disable the Internal Low Speed oscillator (LSI1). */
  637. __HAL_RCC_LSI1_DISABLE();
  638. /* Get Start Tick*/
  639. tickstart = HAL_GetTick();
  640. /* Wait till LSI1 is disabled */
  641. while (LL_RCC_LSI1_IsReady() != 0U)
  642. {
  643. if ((HAL_GetTick() - tickstart) > LSI1_TIMEOUT_VALUE)
  644. {
  645. return HAL_TIMEOUT;
  646. }
  647. }
  648. }
  649. }
  650. /*------------------------------ LSE Configuration -------------------------*/
  651. if (((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE)
  652. {
  653. /* Check the parameters */
  654. assert_param(IS_RCC_LSE(RCC_OscInitStruct->LSEState));
  655. /* Update LSE configuration in Backup Domain control register */
  656. /* Requires to enable write access to Backup Domain of necessary */
  657. if (HAL_IS_BIT_CLR(PWR->CR1, PWR_CR1_DBP))
  658. {
  659. /* Enable write access to Backup domain */
  660. HAL_PWR_EnableBkUpAccess();
  661. /* Wait for Backup domain Write protection disable */
  662. tickstart = HAL_GetTick();
  663. while (HAL_IS_BIT_CLR(PWR->CR1, PWR_CR1_DBP))
  664. {
  665. if ((HAL_GetTick() - tickstart) > RCC_DBP_TIMEOUT_VALUE)
  666. {
  667. return HAL_TIMEOUT;
  668. }
  669. }
  670. }
  671. /* Set the new LSE configuration -----------------------------------------*/
  672. __HAL_RCC_LSE_CONFIG(RCC_OscInitStruct->LSEState);
  673. /* Check the LSE State */
  674. if (RCC_OscInitStruct->LSEState != RCC_LSE_OFF)
  675. {
  676. /* Get Start Tick*/
  677. tickstart = HAL_GetTick();
  678. /* Wait till LSE is ready */
  679. while (LL_RCC_LSE_IsReady() == 0U)
  680. {
  681. if ((HAL_GetTick() - tickstart) > RCC_LSE_TIMEOUT_VALUE)
  682. {
  683. return HAL_TIMEOUT;
  684. }
  685. }
  686. }
  687. else
  688. {
  689. /* Get Start Tick*/
  690. tickstart = HAL_GetTick();
  691. /* Wait till LSE is disabled */
  692. while (LL_RCC_LSE_IsReady() != 0U)
  693. {
  694. if ((HAL_GetTick() - tickstart) > RCC_LSE_TIMEOUT_VALUE)
  695. {
  696. return HAL_TIMEOUT;
  697. }
  698. }
  699. }
  700. }
  701. #if defined(RCC_HSI48_SUPPORT)
  702. /*------------------------------ HSI48 Configuration -----------------------*/
  703. if (((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI48) == RCC_OSCILLATORTYPE_HSI48)
  704. {
  705. /* Check the parameters */
  706. assert_param(IS_RCC_HSI48(RCC_OscInitStruct->HSI48State));
  707. /* Check the LSI State */
  708. if (RCC_OscInitStruct->HSI48State != RCC_HSI48_OFF)
  709. {
  710. /* Enable the Internal Low Speed oscillator (HSI48). */
  711. __HAL_RCC_HSI48_ENABLE();
  712. /* Get Start Tick*/
  713. tickstart = HAL_GetTick();
  714. /* Wait till HSI48 is ready */
  715. while (LL_RCC_HSI48_IsReady() == 0U)
  716. {
  717. if ((HAL_GetTick() - tickstart) > HSI48_TIMEOUT_VALUE)
  718. {
  719. return HAL_TIMEOUT;
  720. }
  721. }
  722. }
  723. else
  724. {
  725. /* Disable the Internal Low Speed oscillator (HSI48). */
  726. __HAL_RCC_HSI48_DISABLE();
  727. /* Get Start Tick*/
  728. tickstart = HAL_GetTick();
  729. /* Wait till HSI48 is disabled */
  730. while (LL_RCC_HSI48_IsReady() != 0U)
  731. {
  732. if ((HAL_GetTick() - tickstart) > HSI48_TIMEOUT_VALUE)
  733. {
  734. return HAL_TIMEOUT;
  735. }
  736. }
  737. }
  738. }
  739. #endif
  740. /*-------------------------------- PLL Configuration -----------------------*/
  741. /* Check the parameters */
  742. assert_param(IS_RCC_PLL(RCC_OscInitStruct->PLL.PLLState));
  743. if(RCC_OscInitStruct->PLL.PLLState != RCC_PLL_NONE)
  744. {
  745. const uint32_t temp_sysclksrc = __HAL_RCC_GET_SYSCLK_SOURCE();
  746. const uint32_t temp_pllconfig = RCC->PLLCFGR;
  747. /* PLL On ? */
  748. if(RCC_OscInitStruct->PLL.PLLState == RCC_PLL_ON)
  749. {
  750. /* Check the parameters */
  751. assert_param(IS_RCC_PLLSOURCE(RCC_OscInitStruct->PLL.PLLSource));
  752. assert_param(IS_RCC_PLLM_VALUE(RCC_OscInitStruct->PLL.PLLM));
  753. assert_param(IS_RCC_PLLN_VALUE(RCC_OscInitStruct->PLL.PLLN));
  754. assert_param(IS_RCC_PLLP_VALUE(RCC_OscInitStruct->PLL.PLLP));
  755. assert_param(IS_RCC_PLLQ_VALUE(RCC_OscInitStruct->PLL.PLLQ));
  756. assert_param(IS_RCC_PLLR_VALUE(RCC_OscInitStruct->PLL.PLLR));
  757. /* Do nothing if PLL configuration is unchanged */
  758. if ((READ_BIT(temp_pllconfig, RCC_PLLCFGR_PLLSRC) != RCC_OscInitStruct->PLL.PLLSource) ||
  759. (READ_BIT(temp_pllconfig, RCC_PLLCFGR_PLLM) != RCC_OscInitStruct->PLL.PLLM) ||
  760. ((READ_BIT(temp_pllconfig, RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos) != RCC_OscInitStruct->PLL.PLLN) ||
  761. (READ_BIT(temp_pllconfig, RCC_PLLCFGR_PLLP) != RCC_OscInitStruct->PLL.PLLP) ||
  762. (READ_BIT(temp_pllconfig, RCC_PLLCFGR_PLLQ) != RCC_OscInitStruct->PLL.PLLQ) ||
  763. (READ_BIT(temp_pllconfig, RCC_PLLCFGR_PLLR) != RCC_OscInitStruct->PLL.PLLR))
  764. {
  765. /* Check if the PLL is used as system clock or not */
  766. if (temp_sysclksrc != RCC_SYSCLKSOURCE_STATUS_PLLCLK)
  767. {
  768. #if defined(SAI1)
  769. /* Check if main PLL can be updated */
  770. /* Not possible if the source is shared by other enabled PLLSAIx */
  771. if (READ_BIT(RCC->CR, RCC_CR_PLLSAI1ON) != 0U)
  772. {
  773. return HAL_ERROR;
  774. }
  775. else
  776. #endif
  777. {
  778. /* Disable the main PLL. */
  779. __HAL_RCC_PLL_DISABLE();
  780. /* Get Start Tick*/
  781. tickstart = HAL_GetTick();
  782. /* Wait till PLL is ready */
  783. while (READ_BIT(RCC->CR, RCC_CR_PLLRDY) != 0U)
  784. {
  785. if ((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  786. {
  787. return HAL_TIMEOUT;
  788. }
  789. }
  790. /* Configure the main PLL clock source, multiplication and division factors. */
  791. __HAL_RCC_PLL_CONFIG(RCC_OscInitStruct->PLL.PLLSource,
  792. RCC_OscInitStruct->PLL.PLLM,
  793. RCC_OscInitStruct->PLL.PLLN,
  794. RCC_OscInitStruct->PLL.PLLP,
  795. RCC_OscInitStruct->PLL.PLLQ,
  796. RCC_OscInitStruct->PLL.PLLR);
  797. /* Enable the main PLL. */
  798. __HAL_RCC_PLL_ENABLE();
  799. /* Enable PLL System Clock output. */
  800. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL_SYSCLK);
  801. /* Get Start Tick*/
  802. tickstart = HAL_GetTick();
  803. /* Wait till PLL is ready */
  804. while (READ_BIT(RCC->CR, RCC_CR_PLLRDY) == 0U)
  805. {
  806. if ((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  807. {
  808. return HAL_TIMEOUT;
  809. }
  810. }
  811. }
  812. }
  813. else
  814. {
  815. /* PLL is already used as System core clock */
  816. return HAL_ERROR;
  817. }
  818. }
  819. else
  820. {
  821. /* PLL configuration is unchanged */
  822. /* Re-enable PLL if it was disabled (ie. low power mode) */
  823. if (READ_BIT(RCC->CR, RCC_CR_PLLRDY) == 0U)
  824. {
  825. /* Enable the main PLL. */
  826. __HAL_RCC_PLL_ENABLE();
  827. /* Enable PLL System Clock output. */
  828. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL_SYSCLK);
  829. /* Get Start Tick*/
  830. tickstart = HAL_GetTick();
  831. /* Wait till PLL is ready */
  832. while (READ_BIT(RCC->CR, RCC_CR_PLLRDY) == 0U)
  833. {
  834. if((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  835. {
  836. return HAL_TIMEOUT;
  837. }
  838. }
  839. }
  840. }
  841. }
  842. else
  843. {
  844. /* Check that PLL is not used as system clock or not */
  845. if (temp_sysclksrc != RCC_SYSCLKSOURCE_STATUS_PLLCLK)
  846. {
  847. /* Disable the main PLL. */
  848. __HAL_RCC_PLL_DISABLE();
  849. /* Disable all PLL outputs to save power */
  850. MODIFY_REG(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC, PLLSOURCE_NONE);
  851. #if defined(SAI1) && defined(USB)
  852. __HAL_RCC_PLLCLKOUT_DISABLE(RCC_PLL_SYSCLK | RCC_PLL_USBCLK | RCC_PLL_SAI1CLK);
  853. #else
  854. __HAL_RCC_PLLCLKOUT_DISABLE(RCC_PLL_SYSCLK);
  855. #endif
  856. /* Get Start Tick*/
  857. tickstart = HAL_GetTick();
  858. /* Wait till PLL is disabled */
  859. while (READ_BIT(RCC->CR, RCC_CR_PLLRDY) != 0U)
  860. {
  861. if ((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  862. {
  863. return HAL_TIMEOUT;
  864. }
  865. }
  866. }
  867. else
  868. {
  869. /* PLL is already used as System core clock */
  870. return HAL_ERROR;
  871. }
  872. }
  873. }
  874. return HAL_OK;
  875. }
  876. /**
  877. * @brief Initialize the CPU, AHB and APB buses clocks according to the specified
  878. * parameters in the RCC_ClkInitStruct.
  879. * @param RCC_ClkInitStruct pointer to a @ref RCC_ClkInitTypeDef structure that
  880. * contains the configuration information for the RCC peripheral.
  881. * @param FLatency FLASH Latency
  882. * This parameter can be one of the following values:
  883. * @arg FLASH_LATENCY_0 FLASH 0 Latency cycle
  884. * @arg FLASH_LATENCY_1 FLASH 1 Latency cycle
  885. * @arg FLASH_LATENCY_2 FLASH 2 Latency cycle
  886. * @arg FLASH_LATENCY_3 FLASH 3 Latency cycle
  887. *
  888. * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency
  889. * and updated by @ref HAL_RCC_GetHCLKFreq() function called within this function
  890. *
  891. * @note The MSI is used by default as system clock source after
  892. * startup from Reset, wake-up from STANDBY mode. After restart from Reset,
  893. * the MSI frequency is set to its default value 4 MHz.
  894. *
  895. * @note The HSI can be selected as system clock source after
  896. * from STOP modes or in case of failure of the HSE used directly or indirectly
  897. * as system clock (if the Clock Security System CSS is enabled).
  898. *
  899. * @note A switch from one clock source to another occurs only if the target
  900. * clock source is ready (clock stable after startup delay or PLL locked).
  901. * If a clock source which is not yet ready is selected, the switch will
  902. * occur when the clock source is ready.
  903. *
  904. * @note You can use @ref HAL_RCC_GetClockConfig() function to know which clock is
  905. * currently used as system clock source.
  906. *
  907. * @note Depending on the device voltage range, the software has to set correctly
  908. * HPRE[3:0] bits to ensure that HCLK1 not exceed the maximum allowed frequency
  909. * (for more details refer to section above "Initialization/de-initialization functions")
  910. * @retval None
  911. */
  912. HAL_StatusTypeDef HAL_RCC_ClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t FLatency)
  913. {
  914. uint32_t tickstart;
  915. /* Check Null pointer */
  916. if (RCC_ClkInitStruct == NULL)
  917. {
  918. return HAL_ERROR;
  919. }
  920. /* Check the parameters */
  921. assert_param(IS_RCC_CLOCKTYPE(RCC_ClkInitStruct->ClockType));
  922. assert_param(IS_FLASH_LATENCY(FLatency));
  923. /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
  924. must be correctly programmed according to the frequency of the FLASH clock
  925. (HCLK4) and the supply voltage of the device. */
  926. /* Increasing the number of wait states because of higher CPU frequency */
  927. if (FLatency > __HAL_FLASH_GET_LATENCY())
  928. {
  929. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  930. __HAL_FLASH_SET_LATENCY(FLatency);
  931. /* Get Start Tick*/
  932. tickstart = HAL_GetTick();
  933. /* Check that the new number of wait states is taken into account to access the Flash
  934. memory by reading the FLASH_ACR register */
  935. while (__HAL_FLASH_GET_LATENCY() != FLatency)
  936. {
  937. if ((HAL_GetTick() - tickstart) > LATENCY_TIMEOUT_VALUE)
  938. {
  939. return HAL_TIMEOUT;
  940. }
  941. }
  942. }
  943. /*-------------------------- HCLK1 Configuration --------------------------*/
  944. if (((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)
  945. {
  946. assert_param(IS_RCC_HCLKx(RCC_ClkInitStruct->AHBCLKDivider));
  947. LL_RCC_SetAHBPrescaler(RCC_ClkInitStruct->AHBCLKDivider);
  948. /* HCLK1 prescaler flag when value applied */
  949. tickstart = HAL_GetTick();
  950. while (LL_RCC_IsActiveFlag_HPRE() == 0U)
  951. {
  952. if ((HAL_GetTick() - tickstart) > PRESCALER_TIMEOUT_VALUE)
  953. {
  954. return HAL_TIMEOUT;
  955. }
  956. }
  957. }
  958. /*-------------------------- HCLK2 Configuration --------------------------*/
  959. if (((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK2) == RCC_CLOCKTYPE_HCLK2)
  960. {
  961. assert_param(IS_RCC_HCLKx(RCC_ClkInitStruct->AHBCLK2Divider));
  962. LL_C2_RCC_SetAHBPrescaler(RCC_ClkInitStruct->AHBCLK2Divider);
  963. /* HCLK2 prescaler flag when value applied */
  964. tickstart = HAL_GetTick();
  965. while (LL_RCC_IsActiveFlag_C2HPRE() == 0U)
  966. {
  967. if ((HAL_GetTick() - tickstart) > PRESCALER_TIMEOUT_VALUE)
  968. {
  969. return HAL_TIMEOUT;
  970. }
  971. }
  972. }
  973. /*-------------------------- HCLK4 Configuration --------------------------*/
  974. if (((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK4) == RCC_CLOCKTYPE_HCLK4)
  975. {
  976. assert_param(IS_RCC_HCLKx(RCC_ClkInitStruct->AHBCLK4Divider));
  977. LL_RCC_SetAHB4Prescaler(RCC_ClkInitStruct->AHBCLK4Divider);
  978. /* AHB shared prescaler flag when value applied */
  979. tickstart = HAL_GetTick();
  980. while (LL_RCC_IsActiveFlag_SHDHPRE() == 0U)
  981. {
  982. if ((HAL_GetTick() - tickstart) > PRESCALER_TIMEOUT_VALUE)
  983. {
  984. return HAL_TIMEOUT;
  985. }
  986. }
  987. }
  988. /*-------------------------- PCLK1 Configuration ---------------------------*/
  989. if (((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
  990. {
  991. assert_param(IS_RCC_PCLKx(RCC_ClkInitStruct->APB1CLKDivider));
  992. LL_RCC_SetAPB1Prescaler(RCC_ClkInitStruct->APB1CLKDivider);
  993. /* APB1 prescaler flag when value applied */
  994. tickstart = HAL_GetTick();
  995. while (LL_RCC_IsActiveFlag_PPRE1() == 0U)
  996. {
  997. if ((HAL_GetTick() - tickstart) > PRESCALER_TIMEOUT_VALUE)
  998. {
  999. return HAL_TIMEOUT;
  1000. }
  1001. }
  1002. }
  1003. /*-------------------------- PCLK2 Configuration ---------------------------*/
  1004. if (((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK2) == RCC_CLOCKTYPE_PCLK2)
  1005. {
  1006. assert_param(IS_RCC_PCLKx(RCC_ClkInitStruct->APB2CLKDivider));
  1007. LL_RCC_SetAPB2Prescaler((RCC_ClkInitStruct->APB2CLKDivider) << 3U);
  1008. /* APB2 prescaler flag when value applied */
  1009. tickstart = HAL_GetTick();
  1010. while (LL_RCC_IsActiveFlag_PPRE2() == 0U)
  1011. {
  1012. if ((HAL_GetTick() - tickstart) > PRESCALER_TIMEOUT_VALUE)
  1013. {
  1014. return HAL_TIMEOUT;
  1015. }
  1016. }
  1017. }
  1018. /*------------------------- SYSCLK Configuration ---------------------------*/
  1019. if (((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_SYSCLK) == RCC_CLOCKTYPE_SYSCLK)
  1020. {
  1021. assert_param(IS_RCC_SYSCLKSOURCE(RCC_ClkInitStruct->SYSCLKSource));
  1022. /* HSE is selected as System Clock Source */
  1023. if (RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
  1024. {
  1025. /* Check the HSE ready flag */
  1026. if (LL_RCC_HSE_IsReady() == 0U)
  1027. {
  1028. return HAL_ERROR;
  1029. }
  1030. }
  1031. /* PLL is selected as System Clock Source */
  1032. else if (RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
  1033. {
  1034. /* Check the PLL ready flag */
  1035. if (LL_RCC_PLL_IsReady() == 0U)
  1036. {
  1037. return HAL_ERROR;
  1038. }
  1039. }
  1040. /* MSI is selected as System Clock Source */
  1041. else if (RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_MSI)
  1042. {
  1043. /* Check the MSI ready flag */
  1044. if (LL_RCC_MSI_IsReady() == 0U)
  1045. {
  1046. return HAL_ERROR;
  1047. }
  1048. }
  1049. /* HSI is selected as System Clock Source */
  1050. else
  1051. {
  1052. /* Check the HSI ready flag */
  1053. if (LL_RCC_HSI_IsReady() == 0U)
  1054. {
  1055. return HAL_ERROR;
  1056. }
  1057. }
  1058. /* apply system clock switch */
  1059. LL_RCC_SetSysClkSource(RCC_ClkInitStruct->SYSCLKSource);
  1060. /* Get Start Tick*/
  1061. tickstart = HAL_GetTick();
  1062. /* check system clock source switch status */
  1063. while (__HAL_RCC_GET_SYSCLK_SOURCE() != (RCC_ClkInitStruct->SYSCLKSource << RCC_CFGR_SWS_Pos))
  1064. {
  1065. if ((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
  1066. {
  1067. return HAL_TIMEOUT;
  1068. }
  1069. }
  1070. }
  1071. /* Decreasing the number of wait states because of lower CPU frequency */
  1072. if (FLatency < __HAL_FLASH_GET_LATENCY())
  1073. {
  1074. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  1075. __HAL_FLASH_SET_LATENCY(FLatency);
  1076. /* Get Start Tick*/
  1077. tickstart = HAL_GetTick();
  1078. /* Check that the new number of wait states is taken into account to access the Flash
  1079. memory by reading the FLASH_ACR register */
  1080. while (__HAL_FLASH_GET_LATENCY() != FLatency)
  1081. {
  1082. if ((HAL_GetTick() - tickstart) > LATENCY_TIMEOUT_VALUE)
  1083. {
  1084. return HAL_TIMEOUT;
  1085. }
  1086. }
  1087. }
  1088. /*---------------------------------------------------------------------------*/
  1089. /* Update the SystemCoreClock global variable */
  1090. SystemCoreClock = HAL_RCC_GetHCLKFreq();
  1091. /* Configure the source of time base considering new system clocks settings*/
  1092. return HAL_InitTick(HAL_GetTickPrio());
  1093. }
  1094. /**
  1095. * @}
  1096. */
  1097. /** @defgroup RCC_Exported_Functions_Group2 Peripheral Control functions
  1098. * @brief RCC clocks control functions
  1099. *
  1100. @verbatim
  1101. ===============================================================================
  1102. ##### Peripheral Control functions #####
  1103. ===============================================================================
  1104. [..]
  1105. This subsection provides a set of functions allowing to:
  1106. (+) Ouput clock to MCO pin.
  1107. (+) Retrieve current clock frequencies.
  1108. (+) Enable the Clock Security System.
  1109. @endverbatim
  1110. * @{
  1111. */
  1112. /**
  1113. * @brief Select the clock source to output on MCO1 pin(PA8) or MC02 pin (PB6) or MCO3 pin (PA15).
  1114. * @note PA8, PB6 or PA15 should be configured in alternate function mode.
  1115. * @param RCC_MCOx specifies the output direction for the clock source.
  1116. * @arg @ref RCC_MCO1 Clock source to output on MCO1 pin(PA8)
  1117. * @arg @ref RCC_MCO2 Clock source to output on MCO2 pin(PB6)
  1118. * @arg @ref RCC_MCO3 Clock source to output on MCO3 pin(PA15)
  1119. * @param RCC_MCOSource specifies the clock source to output.
  1120. * This parameter can be one of the following values:
  1121. * @arg @ref RCC_MCO1SOURCE_NOCLOCK MCO output disabled, no clock on MCO
  1122. * @arg @ref RCC_MCO1SOURCE_SYSCLK system clock selected as MCO source
  1123. * @arg @ref RCC_MCO1SOURCE_MSI MSI clock selected as MCO source
  1124. * @arg @ref RCC_MCO1SOURCE_HSI HSI clock selected as MCO source
  1125. * @arg @ref RCC_MCO1SOURCE_HSE HSE clock selected as MCO sourcee
  1126. * @arg @ref RCC_MCO1SOURCE_PLLCLK main PLL clock selected as MCO source
  1127. * @arg @ref RCC_MCO1SOURCE_LSI1 LSI1 clock selected as MCO source
  1128. * @arg @ref RCC_MCO1SOURCE_LSI2 LSI2 clock selected as MCO source
  1129. * @arg @ref RCC_MCO1SOURCE_LSE LSE clock selected as MCO source
  1130. * @arg @ref RCC_MCO1SOURCE_HSI48 HSI48 clock selected as MCO source for devices with HSI48
  1131. * @arg @ref RCC_MCO1SOURCE_HSE_BEFORE_STAB HSE clock before stabilization selected as MCO source
  1132. * @param RCC_MCODiv specifies the MCO prescaler.
  1133. * This parameter can be one of the following values:
  1134. * @arg @ref RCC_MCODIV_1 no division applied to MCO clock
  1135. * @arg @ref RCC_MCODIV_2 division by 2 applied to MCO clock
  1136. * @arg @ref RCC_MCODIV_4 division by 4 applied to MCO clock
  1137. * @arg @ref RCC_MCODIV_8 division by 8 applied to MCO clock
  1138. * @arg @ref RCC_MCODIV_16 division by 16 applied to MCO clock
  1139. * @retval None
  1140. */
  1141. void HAL_RCC_MCOConfig(uint32_t RCC_MCOx, uint32_t RCC_MCOSource, uint32_t RCC_MCODiv)
  1142. {
  1143. GPIO_InitTypeDef GPIO_InitStruct;
  1144. /* Check the parameters */
  1145. assert_param(IS_RCC_MCO(RCC_MCOx));
  1146. assert_param(IS_RCC_MCODIV(RCC_MCODiv));
  1147. assert_param(IS_RCC_MCO1SOURCE(RCC_MCOSource));
  1148. /* Common GPIO init parameters */
  1149. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  1150. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  1151. GPIO_InitStruct.Pull = GPIO_NOPULL;
  1152. /* RCC_MCO1 */
  1153. if (RCC_MCOx == RCC_MCO1)
  1154. {
  1155. /* MCO1 Clock Enable */
  1156. __MCO1_CLK_ENABLE();
  1157. /* Configue the MCO1 pin in alternate function mode */
  1158. GPIO_InitStruct.Pin = MCO1_PIN;
  1159. GPIO_InitStruct.Alternate = GPIO_AF0_MCO;
  1160. HAL_GPIO_Init(MCO1_GPIO_PORT, &GPIO_InitStruct);
  1161. }
  1162. else if (RCC_MCOx == RCC_MCO2)
  1163. {
  1164. /* MCO2 Clock Enable */
  1165. __MCO2_CLK_ENABLE();
  1166. /* Configue the MCO2 pin in alternate function mode */
  1167. GPIO_InitStruct.Pin = MCO2_PIN;
  1168. GPIO_InitStruct.Alternate = GPIO_AF0_MCO;
  1169. HAL_GPIO_Init(MCO2_GPIO_PORT, &GPIO_InitStruct);
  1170. }
  1171. #if defined(RCC_MCO3_SUPPORT)
  1172. else if (RCC_MCOx == RCC_MCO3)
  1173. {
  1174. /* MCO3 Clock Enable */
  1175. __MCO3_CLK_ENABLE();
  1176. /* Configue the MCO3 pin in alternate function mode */
  1177. GPIO_InitStruct.Pin = MCO3_PIN;
  1178. GPIO_InitStruct.Alternate = GPIO_AF6_MCO;
  1179. HAL_GPIO_Init(MCO3_GPIO_PORT, &GPIO_InitStruct);
  1180. }
  1181. #endif
  1182. else
  1183. {
  1184. ;
  1185. }
  1186. /* Mask MCOSEL[] and MCOPRE[] bits then set MCO clock source and prescaler */
  1187. LL_RCC_ConfigMCO(RCC_MCOSource, RCC_MCODiv);
  1188. }
  1189. /**
  1190. * @brief Return the SYSCLK frequency.
  1191. *
  1192. * @note The system computed by this function is not the real
  1193. * frequency in the chip. It is calculated based on the predefined
  1194. * constant and the selected clock source:
  1195. * @note If SYSCLK source is MSI, function returns values based on MSI range
  1196. * @note If SYSCLK source is HSI, function returns values based on HSI_VALUE(*)
  1197. * @note If SYSCLK source is HSE, function returns values based on HSE_VALUE(**)
  1198. * @note If SYSCLK source is PLL, function returns values based on HSE_VALUE(**),
  1199. * HSI_VALUE(*) or MSI Value multiplied/divided by the PLL factors.
  1200. * @note (*) HSI_VALUE is a constant defined in stm32wbxx_hal_conf.h file (default value
  1201. * 16 MHz) but the real value may vary depending on the variations
  1202. * in voltage and temperature.
  1203. * @note (**) HSE_VALUE is a constant defined in stm32wbxx_hal_conf.h file (default value
  1204. * 32 MHz), user has to ensure that HSE_VALUE is same as the real
  1205. * frequency of the crystal used. Otherwise, this function may
  1206. * have wrong result.
  1207. *
  1208. * @note The result of this function could be not correct when using fractional
  1209. * value for HSE crystal.
  1210. *
  1211. * @note This function can be used by the user application to compute the
  1212. * baudrate for the communication peripherals or configure other parameters.
  1213. *
  1214. * @note Each time SYSCLK changes, this function must be called to update the
  1215. * right SYSCLK value. Otherwise, any configuration based on this function will be incorrect.
  1216. *
  1217. *
  1218. * @retval SYSCLK frequency
  1219. */
  1220. uint32_t HAL_RCC_GetSysClockFreq(void)
  1221. {
  1222. uint32_t pllsource;
  1223. uint32_t sysclockfreq, pllinputfreq;
  1224. const uint32_t temp_sysclksrc = __HAL_RCC_GET_SYSCLK_SOURCE();
  1225. if (temp_sysclksrc == RCC_SYSCLKSOURCE_STATUS_MSI)
  1226. {
  1227. /* Retrieve MSI frequency range in HZ*/
  1228. /* MSI used as system clock source */
  1229. sysclockfreq = __LL_RCC_CALC_MSI_FREQ(LL_RCC_MSI_GetRange());
  1230. }
  1231. else if (temp_sysclksrc == RCC_SYSCLKSOURCE_STATUS_HSI)
  1232. {
  1233. /* HSI used as system clock source */
  1234. sysclockfreq = HSI_VALUE;
  1235. }
  1236. else if (temp_sysclksrc == RCC_SYSCLKSOURCE_STATUS_HSE)
  1237. {
  1238. /* HSE used as system clock source */
  1239. if (LL_RCC_HSE_IsEnabledDiv2() == 1U)
  1240. {
  1241. sysclockfreq = HSE_VALUE / 2U;
  1242. }
  1243. else
  1244. {
  1245. sysclockfreq = HSE_VALUE;
  1246. }
  1247. }
  1248. else
  1249. {
  1250. /* PLL used as system clock source */
  1251. pllsource = LL_RCC_PLL_GetMainSource();
  1252. switch (pllsource)
  1253. {
  1254. case RCC_PLLSOURCE_HSI: /* HSI used as PLL clock source */
  1255. pllinputfreq = HSI_VALUE;
  1256. break;
  1257. case RCC_PLLSOURCE_HSE: /* HSE used as PLL clock source */
  1258. if (LL_RCC_HSE_IsEnabledDiv2() == 1U)
  1259. {
  1260. pllinputfreq = HSE_VALUE / 2U;
  1261. }
  1262. else
  1263. {
  1264. pllinputfreq = HSE_VALUE;
  1265. }
  1266. break;
  1267. case RCC_PLLSOURCE_MSI: /* MSI used as PLL clock source */
  1268. default:
  1269. pllinputfreq = __LL_RCC_CALC_MSI_FREQ(LL_RCC_MSI_GetRange());
  1270. break;
  1271. }
  1272. sysclockfreq = __LL_RCC_CALC_PLLCLK_FREQ(pllinputfreq, LL_RCC_PLL_GetDivider(), LL_RCC_PLL_GetN(), LL_RCC_PLL_GetR());
  1273. }
  1274. return sysclockfreq;
  1275. }
  1276. /**
  1277. * @brief Return the HCLK frequency.
  1278. * @retval HCLK frequency in Hz
  1279. */
  1280. uint32_t HAL_RCC_GetHCLKFreq(void)
  1281. {
  1282. /* Get SysClock and Compute HCLK1 frequency ---------------------------*/
  1283. return ((uint32_t)(__LL_RCC_CALC_HCLK1_FREQ(HAL_RCC_GetSysClockFreq(), LL_RCC_GetAHBPrescaler())));
  1284. }
  1285. /**
  1286. * @brief Return the HCLK2 frequency.
  1287. * @retval HCLK2 frequency in Hz
  1288. */
  1289. uint32_t HAL_RCC_GetHCLK2Freq(void)
  1290. {
  1291. /* Get SysClock and Compute HCLK2 frequency ---------------------------*/
  1292. return ((uint32_t)(__LL_RCC_CALC_HCLK2_FREQ(HAL_RCC_GetSysClockFreq(), LL_C2_RCC_GetAHBPrescaler())));
  1293. }
  1294. /**
  1295. * @brief Return the HCLK4 frequency.
  1296. * @retval HCLK4 frequency in Hz
  1297. */
  1298. uint32_t HAL_RCC_GetHCLK4Freq(void)
  1299. {
  1300. /* Get SysClock and Compute AHB4 frequency ---------------------------*/
  1301. return ((uint32_t)(__LL_RCC_CALC_HCLK4_FREQ(HAL_RCC_GetSysClockFreq(), LL_RCC_GetAHB4Prescaler())));
  1302. }
  1303. /**
  1304. * @brief Return the PCLK1 frequency.
  1305. * @note Each time PCLK1 changes, this function must be called to update the
  1306. * right PCLK1 value. Otherwise, any configuration based on this function will be incorrect.
  1307. * @retval PCLK1 frequency in Hz
  1308. */
  1309. uint32_t HAL_RCC_GetPCLK1Freq(void)
  1310. {
  1311. /* Get HCLK source and Compute PCLK1 frequency ---------------------------*/
  1312. return ((uint32_t)(__LL_RCC_CALC_PCLK1_FREQ(HAL_RCC_GetHCLKFreq(), LL_RCC_GetAPB1Prescaler())));
  1313. }
  1314. /**
  1315. * @brief Return the PCLK2 frequency.
  1316. * @note Each time PCLK2 changes, this function must be called to update the
  1317. * right PCLK2 value. Otherwise, any configuration based on this function will be incorrect.
  1318. * @retval PCLK2 frequency in Hz
  1319. */
  1320. uint32_t HAL_RCC_GetPCLK2Freq(void)
  1321. {
  1322. /* Get HCLK source and Compute PCLK2 frequency ---------------------------*/
  1323. return ((uint32_t)(__LL_RCC_CALC_PCLK2_FREQ(HAL_RCC_GetHCLKFreq(), LL_RCC_GetAPB2Prescaler())));
  1324. }
  1325. /**
  1326. * @brief Configure the RCC_OscInitStruct according to the internal
  1327. * RCC configuration registers.
  1328. * @param RCC_OscInitStruct pointer to an RCC_OscInitTypeDef structure that
  1329. * will be configured.
  1330. * @retval None
  1331. */
  1332. void HAL_RCC_GetOscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  1333. {
  1334. /* Check the parameters */
  1335. assert_param(RCC_OscInitStruct != (void *)NULL);
  1336. /* Set all possible values for the Oscillator type parameter ---------------*/
  1337. RCC_OscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_MSI | \
  1338. RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_LSI1 | RCC_OSCILLATORTYPE_LSI2;
  1339. #if defined(RCC_HSI48_SUPPORT)
  1340. RCC_OscInitStruct->OscillatorType |= RCC_OSCILLATORTYPE_HSI48;
  1341. #endif
  1342. /* Get the HSE configuration -----------------------------------------------*/
  1343. if ((RCC->CR & RCC_CR_HSEON) == RCC_CR_HSEON)
  1344. {
  1345. RCC_OscInitStruct->HSEState = RCC_HSE_ON;
  1346. }
  1347. else
  1348. {
  1349. RCC_OscInitStruct->HSEState = RCC_HSE_OFF;
  1350. }
  1351. /* Get the MSI configuration -----------------------------------------------*/
  1352. if ((RCC->CR & RCC_CR_MSION) == RCC_CR_MSION)
  1353. {
  1354. RCC_OscInitStruct->MSIState = RCC_MSI_ON;
  1355. }
  1356. else
  1357. {
  1358. RCC_OscInitStruct->MSIState = RCC_MSI_OFF;
  1359. }
  1360. RCC_OscInitStruct->MSICalibrationValue = LL_RCC_MSI_GetCalibTrimming();
  1361. RCC_OscInitStruct->MSIClockRange = LL_RCC_MSI_GetRange();
  1362. /* Get the HSI configuration -----------------------------------------------*/
  1363. if ((RCC->CR & RCC_CR_HSION) == RCC_CR_HSION)
  1364. {
  1365. RCC_OscInitStruct->HSIState = RCC_HSI_ON;
  1366. }
  1367. else
  1368. {
  1369. RCC_OscInitStruct->HSIState = RCC_HSI_OFF;
  1370. }
  1371. RCC_OscInitStruct->HSICalibrationValue = LL_RCC_HSI_GetCalibTrimming();
  1372. /* Get the LSE configuration -----------------------------------------------*/
  1373. if ((RCC->BDCR & RCC_BDCR_LSEBYP) == RCC_BDCR_LSEBYP)
  1374. {
  1375. RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS;
  1376. }
  1377. else if ((RCC->BDCR & RCC_BDCR_LSEON) == RCC_BDCR_LSEON)
  1378. {
  1379. RCC_OscInitStruct->LSEState = RCC_LSE_ON;
  1380. }
  1381. else
  1382. {
  1383. RCC_OscInitStruct->LSEState = RCC_LSE_OFF;
  1384. }
  1385. /* Get the LSI configuration -----------------------------------------------*/
  1386. const uint32_t temp_lsi1on = (RCC->CSR & RCC_CSR_LSI1ON);
  1387. const uint32_t temp_lsi2on = (RCC->CSR & RCC_CSR_LSI2ON);
  1388. if ((temp_lsi1on == RCC_CSR_LSI1ON) || (temp_lsi2on == RCC_CSR_LSI2ON))
  1389. {
  1390. RCC_OscInitStruct->LSIState = RCC_LSI_ON;
  1391. }
  1392. else
  1393. {
  1394. RCC_OscInitStruct->LSIState = RCC_LSI_OFF;
  1395. }
  1396. #if defined(RCC_HSI48_SUPPORT)
  1397. /* Get the HSI48 configuration ---------------------------------------------*/
  1398. if ((RCC->CRRCR & RCC_CRRCR_HSI48ON) == RCC_CRRCR_HSI48ON)
  1399. {
  1400. RCC_OscInitStruct->HSI48State = RCC_HSI48_ON;
  1401. }
  1402. else
  1403. {
  1404. RCC_OscInitStruct->HSI48State = RCC_HSI48_OFF;
  1405. }
  1406. #endif
  1407. /* Get the PLL configuration -----------------------------------------------*/
  1408. if ((RCC->CR & RCC_CR_PLLON) == RCC_CR_PLLON)
  1409. {
  1410. RCC_OscInitStruct->PLL.PLLState = RCC_PLL_ON;
  1411. }
  1412. else
  1413. {
  1414. RCC_OscInitStruct->PLL.PLLState = RCC_PLL_OFF;
  1415. }
  1416. RCC_OscInitStruct->PLL.PLLSource = LL_RCC_PLL_GetMainSource();
  1417. RCC_OscInitStruct->PLL.PLLM = LL_RCC_PLL_GetDivider();
  1418. RCC_OscInitStruct->PLL.PLLN = LL_RCC_PLL_GetN();
  1419. RCC_OscInitStruct->PLL.PLLP = LL_RCC_PLL_GetP();
  1420. RCC_OscInitStruct->PLL.PLLQ = LL_RCC_PLL_GetQ();
  1421. RCC_OscInitStruct->PLL.PLLR = LL_RCC_PLL_GetR();
  1422. }
  1423. /**
  1424. * @brief Configure the RCC_ClkInitStruct according to the internal
  1425. * RCC configuration registers.
  1426. * @param RCC_ClkInitStruct Pointer to a @ref RCC_ClkInitTypeDef structure that
  1427. * will be configured.
  1428. * @param pFLatency Pointer on the Flash Latency.
  1429. * @retval None
  1430. */
  1431. void HAL_RCC_GetClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t *pFLatency)
  1432. {
  1433. /* Check the parameters */
  1434. assert_param(RCC_ClkInitStruct != (void *)NULL);
  1435. assert_param(pFLatency != (void *)NULL);
  1436. /* Set all possible values for the Clock type parameter --------------------*/
  1437. RCC_ClkInitStruct->ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2 | \
  1438. RCC_CLOCKTYPE_HCLK2 | RCC_CLOCKTYPE_HCLK4);
  1439. /* Get the SYSCLK configuration --------------------------------------------*/
  1440. RCC_ClkInitStruct->SYSCLKSource = LL_RCC_GetSysClkSource();
  1441. /* Get the HCLK configuration ----------------------------------------------*/
  1442. RCC_ClkInitStruct->AHBCLKDivider = LL_RCC_GetAHBPrescaler();
  1443. /* Get the APB1 configuration ----------------------------------------------*/
  1444. RCC_ClkInitStruct->APB1CLKDivider = LL_RCC_GetAPB1Prescaler();
  1445. /* Get the APB2 configuration ----------------------------------------------*/
  1446. RCC_ClkInitStruct->APB2CLKDivider = LL_RCC_GetAPB2Prescaler();
  1447. /* Get the AHBCLK2Divider configuration ------------------------------------*/
  1448. RCC_ClkInitStruct->AHBCLK2Divider = LL_C2_RCC_GetAHBPrescaler();
  1449. /* Get the AHBCLK4Divider configuration ------------------------------------*/
  1450. RCC_ClkInitStruct->AHBCLK4Divider = LL_RCC_GetAHB4Prescaler();
  1451. /* Get the Flash Wait State (Latency) configuration ------------------------*/
  1452. *pFLatency = __HAL_FLASH_GET_LATENCY();
  1453. }
  1454. /**
  1455. * @brief Enable the Clock Security System.
  1456. * @note If a failure is detected on the HSE oscillator clock, this oscillator
  1457. * is automatically disabled and an interrupt is generated to inform the
  1458. * software about the failure (Clock Security System Interrupt, CSSI),
  1459. * allowing the MCU to perform rescue operations. The CSSI is linked to
  1460. * CPU1 and CPU2 NMI (Non-Maskable Interrupt) exception vector.
  1461. * @note The Clock Security System can only be cleared by reset.
  1462. * @retval None
  1463. */
  1464. void HAL_RCC_EnableCSS(void)
  1465. {
  1466. LL_RCC_HSE_EnableCSS();
  1467. }
  1468. /**
  1469. * @brief Handle the RCC HSE Clock Security System interrupt request.
  1470. * @note This API should be called under the NMI_Handler().
  1471. * @retval None
  1472. */
  1473. void HAL_RCC_NMI_IRQHandler(void)
  1474. {
  1475. /* Check RCC CSSF interrupt flag */
  1476. if (__HAL_RCC_GET_IT(RCC_IT_HSECSS))
  1477. {
  1478. /* RCC Clock Security System interrupt user callback */
  1479. HAL_RCC_CSSCallback();
  1480. /* Clear RCC CSS pending bit */
  1481. __HAL_RCC_CLEAR_IT(RCC_IT_HSECSS);
  1482. }
  1483. }
  1484. /**
  1485. * @brief Handle the RCC HSE Clock Security System interrupt callback.
  1486. * @retval none
  1487. */
  1488. __weak void HAL_RCC_CSSCallback(void)
  1489. {
  1490. /* NOTE : This function should not be modified, when the callback is needed,
  1491. the @ref HAL_RCC_CSSCallback should be implemented in the user file
  1492. */
  1493. }
  1494. /**
  1495. * @}
  1496. */
  1497. /**
  1498. * @}
  1499. */
  1500. /* Private function prototypes -----------------------------------------------*/
  1501. /** @addtogroup RCC_Private_Functions
  1502. * @{
  1503. */
  1504. /**
  1505. * @brief Update number of Flash wait states in line with MSI range and current
  1506. voltage range.
  1507. * @param MSI_Range MSI range value from @ref RCC_MSIRANGE_0 to @ref RCC_MSIRANGE_11
  1508. * @retval HAL status
  1509. */
  1510. static HAL_StatusTypeDef RCC_SetFlashLatencyFromMSIRange(uint32_t MSI_Range)
  1511. {
  1512. uint32_t flash_clksrcfreq, msifreq;
  1513. /* Check the parameters */
  1514. assert_param(IS_RCC_MSI_CLOCK_RANGE(MSI_Range));
  1515. /* MSI frequency range in Hz */
  1516. if (MSI_Range > RCC_MSIRANGE_11)
  1517. {
  1518. msifreq = __LL_RCC_CALC_MSI_FREQ(RCC_MSIRANGE_11);
  1519. }
  1520. else
  1521. {
  1522. msifreq = __LL_RCC_CALC_MSI_FREQ(MSI_Range);
  1523. }
  1524. flash_clksrcfreq = __LL_RCC_CALC_HCLK4_FREQ(msifreq, LL_RCC_GetAHB4Prescaler());
  1525. #if defined(PWR_CR1_VOS)
  1526. return RCC_SetFlashLatency((flash_clksrcfreq / MEGA_HZ), HAL_PWREx_GetVoltageRange());
  1527. #else
  1528. return RCC_SetFlashLatency((flash_clksrcfreq / MEGA_HZ), PWR_REGULATOR_VOLTAGE_SCALE1);
  1529. #endif
  1530. }
  1531. /**
  1532. * @brief Update number of Flash wait states.
  1533. * @param Flash_ClkSrcFreq Flash Clock Source (in MHz)
  1534. * @param VCORE_Voltage Current Vcore voltage (PWR_REGULATOR_VOLTAGE_SCALE1 or PWR_REGULATOR_VOLTAGE_SCALE2)
  1535. * @retval HAL status
  1536. */
  1537. static HAL_StatusTypeDef RCC_SetFlashLatency(uint32_t Flash_ClkSrcFreq, uint32_t VCORE_Voltage)
  1538. {
  1539. /* Flash Clock source (HCLK4) range in MHz with a VCORE is range1 */
  1540. const uint32_t FLASH_CLK_SRC_RANGE_VOS1[] = {18UL, 36UL, 54UL, 64UL};
  1541. #if defined(PWR_CR1_VOS)
  1542. /* Flash Clock source (HCLK4) range in MHz with a VCORE is range2 */
  1543. const uint32_t FLASH_CLK_SRC_RANGE_VOS2[] = {6UL, 12UL, 16UL};
  1544. #endif
  1545. /* Flash Latency range */
  1546. const uint32_t FLASH_LATENCY_RANGE[] = {FLASH_LATENCY_0, FLASH_LATENCY_1, FLASH_LATENCY_2, FLASH_LATENCY_3};
  1547. uint32_t latency = FLASH_LATENCY_0; /* default value 0WS */
  1548. uint32_t tickstart;
  1549. #if defined(PWR_CR1_VOS)
  1550. if (VCORE_Voltage == PWR_REGULATOR_VOLTAGE_SCALE1)
  1551. {
  1552. for (uint32_t index = 0; index < __COUNTOF(FLASH_CLK_SRC_RANGE_VOS1); index++)
  1553. {
  1554. if (Flash_ClkSrcFreq <= FLASH_CLK_SRC_RANGE_VOS1[index])
  1555. {
  1556. latency = FLASH_LATENCY_RANGE[index];
  1557. break;
  1558. }
  1559. }
  1560. }
  1561. else /* PWR_REGULATOR_VOLTAGE_SCALE2 */
  1562. {
  1563. for (uint32_t index = 0; index < __COUNTOF(FLASH_CLK_SRC_RANGE_VOS2); index++)
  1564. {
  1565. if (Flash_ClkSrcFreq <= FLASH_CLK_SRC_RANGE_VOS2[index])
  1566. {
  1567. latency = FLASH_LATENCY_RANGE[index];
  1568. break;
  1569. }
  1570. }
  1571. }
  1572. #else
  1573. for (uint32_t index = 0; index < __COUNTOF(FLASH_CLK_SRC_RANGE_VOS1); index++)
  1574. {
  1575. if (Flash_ClkSrcFreq <= FLASH_CLK_SRC_RANGE_VOS1[index])
  1576. {
  1577. latency = FLASH_LATENCY_RANGE[index];
  1578. break;
  1579. }
  1580. }
  1581. #endif
  1582. __HAL_FLASH_SET_LATENCY(latency);
  1583. /* Get Start Tick*/
  1584. tickstart = HAL_GetTick();
  1585. /* Check that the new number of wait states is taken into account to access the Flash
  1586. memory by reading the FLASH_ACR register */
  1587. while (__HAL_FLASH_GET_LATENCY() != latency)
  1588. {
  1589. if ((HAL_GetTick() - tickstart) > LATENCY_TIMEOUT_VALUE)
  1590. {
  1591. return HAL_TIMEOUT;
  1592. }
  1593. }
  1594. return HAL_OK;
  1595. }
  1596. /**
  1597. * @}
  1598. */
  1599. #endif /* HAL_RCC_MODULE_ENABLED */
  1600. /**
  1601. * @}
  1602. */
  1603. /**
  1604. * @}
  1605. */
  1606. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/