Implement a secure ICS protocol targeting LoRa Node151 microcontroller for controlling irrigation.
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.
 
 
 
 
 
 

1395 lines
49 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_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 multispeed internal oscillator clock
  17. (MSI 2.097MHz) with Flash 0 wait state and Flash prefetch buffer is disabled,
  18. and all peripherals are off except internal SRAM, Flash and JTAG.
  19. (+) There is no prescaler on High speed (AHB) and Low speed (APB) buses;
  20. all peripherals mapped on these buses are running at MSI speed.
  21. (+) The clock for all peripherals is switched off, except the SRAM and FLASH.
  22. (+) All GPIOs are in input floating state, except the JTAG pins which
  23. are assigned to be used for debug purpose.
  24. [..] Once the device started from reset, the user application has to:
  25. (+) Configure the clock source to be used to drive the System clock
  26. (if the application needs higher frequency/performance)
  27. (+) Configure the System clock frequency and Flash settings
  28. (+) Configure the AHB and APB buses prescalers
  29. (+) Enable the clock for the peripheral(s) to be used
  30. (+) Configure the clock source(s) for peripherals whose clocks are not
  31. derived from the System clock (I2S, RTC, ADC, USB OTG FS/SDIO/RNG)
  32. (*) SDIO only for STM32L1xxxD devices
  33. ##### RCC Limitations #####
  34. ==============================================================================
  35. [..]
  36. A delay between an RCC peripheral clock enable and the effective peripheral
  37. enabling should be taken into account in order to manage the peripheral read/write
  38. from/to registers.
  39. (+) This delay depends on the peripheral mapping.
  40. (++) AHB & APB peripherals, 1 dummy read is necessary
  41. [..]
  42. Workarounds:
  43. (#) For AHB & APB peripherals, a dummy read to the peripheral register has been
  44. inserted in each __HAL_RCC_PPP_CLK_ENABLE() macro.
  45. @endverbatim
  46. ******************************************************************************
  47. * @attention
  48. *
  49. * <h2><center>&copy; Copyright(c) 2017 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 "stm32l1xx_hal.h"
  61. /** @addtogroup STM32L1xx_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. /* Private macro -------------------------------------------------------------*/
  72. /** @defgroup RCC_Private_Macros RCC Private Macros
  73. * @{
  74. */
  75. #define MCO1_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
  76. #define MCO1_GPIO_PORT GPIOA
  77. #define MCO1_PIN GPIO_PIN_8
  78. /**
  79. * @}
  80. */
  81. /* Private variables ---------------------------------------------------------*/
  82. /** @defgroup RCC_Private_Variables RCC Private Variables
  83. * @{
  84. */
  85. extern const uint8_t PLLMulTable[]; /* Defined in CMSIS (system_stm32l0xx.c)*/
  86. /**
  87. * @}
  88. */
  89. /* Private function prototypes -----------------------------------------------*/
  90. /** @defgroup RCC_Private_Functions RCC Private Functions
  91. * @{
  92. */
  93. static HAL_StatusTypeDef RCC_SetFlashLatencyFromMSIRange(uint32_t MSIrange);
  94. /**
  95. * @}
  96. */
  97. /* Exported functions ---------------------------------------------------------*/
  98. /** @defgroup RCC_Exported_Functions RCC Exported Functions
  99. * @{
  100. */
  101. /** @defgroup RCC_Exported_Functions_Group1 Initialization and de-initialization functions
  102. * @brief Initialization and Configuration functions
  103. *
  104. @verbatim
  105. ===============================================================================
  106. ##### Initialization and de-initialization functions #####
  107. ===============================================================================
  108. [..]
  109. This section provides functions allowing to configure the internal/external oscillators
  110. (MSI, HSE, HSI, LSE, LSI, PLL, CSS and MCO) and the System buses clocks (SYSCLK, AHB, APB1
  111. and APB2).
  112. [..] Internal/external clock and PLL configuration
  113. (#) MSI (Multispeed internal), Seven frequency ranges are available: 65.536 kHz,
  114. 131.072 kHz, 262.144 kHz, 524.288 kHz, 1.048 MHz, 2.097 MHz (default value) and 4.194 MHz.
  115. (#) HSI (high-speed internal), 16 MHz factory-trimmed RC used directly or through
  116. the PLL as System clock source.
  117. (#) LSI (low-speed internal), ~37 KHz low consumption RC used as IWDG and/or RTC
  118. clock source.
  119. (#) HSE (high-speed external), 1 to 24 MHz crystal oscillator used directly or
  120. through the PLL as System clock source. Can be used also as RTC clock source.
  121. (#) LSE (low-speed external), 32 KHz oscillator used as RTC clock source.
  122. (#) PLL (clocked by HSI or HSE), featuring different output clocks:
  123. (++) The first output is used to generate the high speed system clock (up to 32 MHz)
  124. (++) The second output is used to generate the clock for the USB OTG FS (48 MHz)
  125. (#) CSS (Clock security system), once enable using the macro __HAL_RCC_CSS_ENABLE()
  126. and if a HSE clock failure occurs(HSE used directly or through PLL as System
  127. clock source), the System clocks automatically switched to MSI and an interrupt
  128. is generated if enabled. The interrupt is linked to the Cortex-M3 NMI
  129. (Non-Maskable Interrupt) exception vector.
  130. (#) MCO1 (microcontroller clock output), used to output SYSCLK, HSI, LSI, MSI, LSE,
  131. HSE or PLL clock (through a configurable prescaler) on PA8 pin.
  132. [..] System, AHB and APB buses clocks configuration
  133. (#) Several clock sources can be used to drive the System clock (SYSCLK): MSI, HSI,
  134. HSE and PLL.
  135. The AHB clock (HCLK) is derived from System clock through configurable
  136. prescaler and used to clock the CPU, memory and peripherals mapped
  137. on AHB bus (DMA, GPIO...). APB1 (PCLK1) and APB2 (PCLK2) clocks are derived
  138. from AHB clock through configurable prescalers and used to clock
  139. the peripherals mapped on these buses. You can use
  140. "@ref HAL_RCC_GetSysClockFreq()" function to retrieve the frequencies of these clocks.
  141. -@- All the peripheral clocks are derived from the System clock (SYSCLK) except:
  142. (+@) RTC: RTC clock can be derived either from the LSI, LSE or HSE clock
  143. divided by 2 to 16. You have to use @ref __HAL_RCC_RTC_CONFIG() and @ref __HAL_RCC_RTC_ENABLE()
  144. macros to configure this clock.
  145. (+@) LCD: LCD clock can be derived either from the LSI, LSE or HSE clock
  146. divided by 2 to 16. You have to use @ref __HAL_RCC_LCD_CONFIG()
  147. macros to configure this clock.
  148. (+@) USB OTG FS: USB OTG FS require a frequency equal to 48 MHz
  149. to work correctly. This clock is derived of the main PLL through PLL Multiplier.
  150. (+@) IWDG clock which is always the LSI clock.
  151. (#) The maximum frequency of the SYSCLK and HCLK is 32 MHz, PCLK2 32 MHz
  152. and PCLK1 32 MHz. Depending on the device voltage range, the maximum
  153. frequency should be adapted accordingly.
  154. @endverbatim
  155. * @{
  156. */
  157. /*
  158. Additional consideration on the HCLK based on Latency settings:
  159. +----------------------------------------------------------------------+
  160. | Latency | HCLK clock frequency (MHz) |
  161. | |------------------------------------------------------|
  162. | | voltage range 1 | voltage range 2 | voltage range 3 |
  163. | | 1.8 V | 1.5 V | 1.2 V |
  164. |---------------|------------------|-----------------|-----------------|
  165. |0WS(1CPU cycle)| 0 < HCLK <= 16 | 0 < HCLK <= 8 | 0 < HCLK <= 2 |
  166. |---------------|------------------|-----------------|-----------------|
  167. |1WS(2CPU cycle)| 16 < HCLK <= 32 | 8 < HCLK <= 16 | 2 < HCLK <= 4 |
  168. +----------------------------------------------------------------------+
  169. The following table gives the different clock source frequencies depending on the product
  170. voltage range:
  171. +------------------------------------------------------------------------------------------+
  172. | Product voltage | Clock frequency |
  173. | |------------------|-----------------------------|-----------------------|
  174. | range | MSI | HSI | HSE | PLL |
  175. |-----------------|---------|--------|-----------------------------|-----------------------|
  176. | Range 1 (1.8 V) | 4.2 MHz | 16 MHz | HSE 32 MHz (external clock) | 32 MHz |
  177. | | | | or 24 MHz (crystal) | (PLLVCO max = 96 MHz) |
  178. |-----------------|---------|--------|-----------------------------|-----------------------|
  179. | Range 2 (1.5 V) | 4.2 MHz | 16 MHz | 16 MHz | 16 MHz |
  180. | | | | | (PLLVCO max = 48 MHz) |
  181. |-----------------|---------|--------|-----------------------------|-----------------------|
  182. | Range 3 (1.2 V) | 4.2 MHz | NA | 8 MHz | 4 MHz |
  183. | | | | | (PLLVCO max = 24 MHz) |
  184. +------------------------------------------------------------------------------------------+
  185. */
  186. /**
  187. * @brief Resets the RCC clock configuration to the default reset state.
  188. * @note The default reset state of the clock configuration is given below:
  189. * - MSI ON and used as system clock source
  190. * - HSI, HSE and PLL OFF
  191. * - AHB, APB1 and APB2 prescaler set to 1.
  192. * - CSS and MCO1 OFF
  193. * - All interrupts disabled
  194. * @note This function does not modify the configuration of the
  195. * - Peripheral clocks
  196. * - LSI, LSE and RTC clocks
  197. * @retval HAL status
  198. */
  199. HAL_StatusTypeDef HAL_RCC_DeInit(void)
  200. {
  201. uint32_t tickstart;
  202. HAL_StatusTypeDef status;
  203. /* Set MSIClockRange, HSITRIM and MSITRIM bits to the reset values */
  204. MODIFY_REG(RCC->ICSCR, (RCC_ICSCR_MSITRIM | RCC_ICSCR_HSITRIM | RCC_ICSCR_MSIRANGE), \
  205. ((RCC_MSICALIBRATION_DEFAULT << RCC_ICSCR_MSITRIM_Pos) | (RCC_HSICALIBRATION_DEFAULT << RCC_ICSCR_HSITRIM_Pos) | RCC_ICSCR_MSIRANGE_5));
  206. /* Set MSION bit */
  207. SET_BIT(RCC->CR, RCC_CR_MSION);
  208. /* Get Start Tick*/
  209. tickstart = HAL_GetTick();
  210. /* Wait till MSI is ready */
  211. while (READ_BIT(RCC->CR, RCC_CR_MSIRDY) == 0U)
  212. {
  213. if ((HAL_GetTick() - tickstart) > MSI_TIMEOUT_VALUE)
  214. {
  215. return HAL_TIMEOUT;
  216. }
  217. }
  218. /* Switch SYSCLK to MSI*/
  219. CLEAR_BIT(RCC->CFGR, RCC_CFGR_SW);
  220. /* Wait till MSI as SYSCLK status is ready */
  221. while (READ_BIT(RCC->CFGR, RCC_CFGR_SWS) != 0U)
  222. {
  223. if ((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
  224. {
  225. return HAL_TIMEOUT;
  226. }
  227. }
  228. /* Update the SystemCoreClock global variable */
  229. SystemCoreClock = MSI_VALUE;
  230. /* Configure the source of time base considering new system clock settings */
  231. status = HAL_InitTick(uwTickPrio);
  232. if(status != HAL_OK)
  233. {
  234. return status;
  235. }
  236. /* Reset HSION, HSEON, CSSON & PLLON bits */
  237. CLEAR_BIT(RCC->CR, RCC_CR_HSION | RCC_CR_HSEON | RCC_CR_CSSON | RCC_CR_PLLON);
  238. /* Reset HSEBYP bit */
  239. CLEAR_BIT(RCC->CR, RCC_CR_HSEBYP);
  240. /* Get Start Tick*/
  241. tickstart = HAL_GetTick();
  242. /* Wait till PLL is not ready */
  243. while (READ_BIT(RCC->CR, RCC_CR_PLLRDY) != 0U)
  244. {
  245. if ((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  246. {
  247. return HAL_TIMEOUT;
  248. }
  249. }
  250. /* Reset CFGR register */
  251. CLEAR_REG(RCC->CFGR);
  252. /* Disable all interrupts */
  253. CLEAR_REG(RCC->CIR);
  254. /* Clear all flags */
  255. #if defined(RCC_LSECSS_SUPPORT)
  256. WRITE_REG(RCC->CIR, RCC_CIR_LSIRDYC | RCC_CIR_LSERDYC | RCC_CIR_HSIRDYC | RCC_CIR_HSERDYC | RCC_CIR_PLLRDYC | RCC_CIR_MSIRDYC | RCC_CIR_LSECSSC | RCC_CIR_CSSC);
  257. #else
  258. WRITE_REG(RCC->CIR, RCC_CIR_LSIRDYC | RCC_CIR_LSERDYC | RCC_CIR_HSIRDYC | RCC_CIR_HSERDYC | RCC_CIR_PLLRDYC | RCC_CIR_MSIRDYC | RCC_CIR_CSSC);
  259. #endif
  260. /* Clear all reset flags */
  261. SET_BIT(RCC->CSR, RCC_CSR_RMVF);
  262. return HAL_OK;
  263. }
  264. /**
  265. * @brief Initializes the RCC Oscillators according to the specified parameters in the
  266. * RCC_OscInitTypeDef.
  267. * @param RCC_OscInitStruct pointer to an RCC_OscInitTypeDef structure that
  268. * contains the configuration information for the RCC Oscillators.
  269. * @note The PLL is not disabled when used as system clock.
  270. * @note Transitions LSE Bypass to LSE On and LSE On to LSE Bypass are not
  271. * supported by this macro. User should request a transition to LSE Off
  272. * first and then LSE On or LSE Bypass.
  273. * @note Transition HSE Bypass to HSE On and HSE On to HSE Bypass are not
  274. * supported by this macro. User should request a transition to HSE Off
  275. * first and then HSE On or HSE Bypass.
  276. * @retval HAL status
  277. */
  278. HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  279. {
  280. uint32_t tickstart;
  281. HAL_StatusTypeDef status;
  282. uint32_t sysclk_source, pll_config;
  283. /* Check the parameters */
  284. if(RCC_OscInitStruct == NULL)
  285. {
  286. return HAL_ERROR;
  287. }
  288. assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct->OscillatorType));
  289. sysclk_source = __HAL_RCC_GET_SYSCLK_SOURCE();
  290. pll_config = __HAL_RCC_GET_PLL_OSCSOURCE();
  291. /*------------------------------- HSE Configuration ------------------------*/
  292. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE)
  293. {
  294. /* Check the parameters */
  295. assert_param(IS_RCC_HSE(RCC_OscInitStruct->HSEState));
  296. /* When the HSE is used as system clock or clock source for PLL in these cases it is not allowed to be disabled */
  297. if((sysclk_source == RCC_SYSCLKSOURCE_STATUS_HSE)
  298. || ((sysclk_source == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && (pll_config == RCC_PLLSOURCE_HSE)))
  299. {
  300. if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != 0U) && (RCC_OscInitStruct->HSEState == RCC_HSE_OFF))
  301. {
  302. return HAL_ERROR;
  303. }
  304. }
  305. else
  306. {
  307. /* Set the new HSE configuration ---------------------------------------*/
  308. __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct->HSEState);
  309. /* Check the HSE State */
  310. if(RCC_OscInitStruct->HSEState != RCC_HSE_OFF)
  311. {
  312. /* Get Start Tick */
  313. tickstart = HAL_GetTick();
  314. /* Wait till HSE is ready */
  315. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == 0U)
  316. {
  317. if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
  318. {
  319. return HAL_TIMEOUT;
  320. }
  321. }
  322. }
  323. else
  324. {
  325. /* Get Start Tick */
  326. tickstart = HAL_GetTick();
  327. /* Wait till HSE is disabled */
  328. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != 0U)
  329. {
  330. if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
  331. {
  332. return HAL_TIMEOUT;
  333. }
  334. }
  335. }
  336. }
  337. }
  338. /*----------------------------- HSI Configuration --------------------------*/
  339. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI)
  340. {
  341. /* Check the parameters */
  342. assert_param(IS_RCC_HSI(RCC_OscInitStruct->HSIState));
  343. assert_param(IS_RCC_CALIBRATION_VALUE(RCC_OscInitStruct->HSICalibrationValue));
  344. /* Check if HSI is used as system clock or as PLL source when PLL is selected as system clock */
  345. if((sysclk_source == RCC_SYSCLKSOURCE_STATUS_HSI)
  346. || ((sysclk_source == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && (pll_config == RCC_PLLSOURCE_HSI)))
  347. {
  348. /* When HSI is used as system clock it will not disabled */
  349. if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != 0U) && (RCC_OscInitStruct->HSIState != RCC_HSI_ON))
  350. {
  351. return HAL_ERROR;
  352. }
  353. /* Otherwise, just the calibration is allowed */
  354. else
  355. {
  356. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  357. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  358. }
  359. }
  360. else
  361. {
  362. /* Check the HSI State */
  363. if(RCC_OscInitStruct->HSIState != RCC_HSI_OFF)
  364. {
  365. /* Enable the Internal High Speed oscillator (HSI). */
  366. __HAL_RCC_HSI_ENABLE();
  367. /* Get Start Tick */
  368. tickstart = HAL_GetTick();
  369. /* Wait till HSI is ready */
  370. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == 0U)
  371. {
  372. if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
  373. {
  374. return HAL_TIMEOUT;
  375. }
  376. }
  377. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  378. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  379. }
  380. else
  381. {
  382. /* Disable the Internal High Speed oscillator (HSI). */
  383. __HAL_RCC_HSI_DISABLE();
  384. /* Get Start Tick */
  385. tickstart = HAL_GetTick();
  386. /* Wait till HSI is disabled */
  387. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != 0U)
  388. {
  389. if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
  390. {
  391. return HAL_TIMEOUT;
  392. }
  393. }
  394. }
  395. }
  396. }
  397. /*----------------------------- MSI Configuration --------------------------*/
  398. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_MSI) == RCC_OSCILLATORTYPE_MSI)
  399. {
  400. /* When the MSI is used as system clock it will not be disabled */
  401. if((sysclk_source == RCC_CFGR_SWS_MSI) )
  402. {
  403. if((__HAL_RCC_GET_FLAG(RCC_FLAG_MSIRDY) != 0U) && (RCC_OscInitStruct->MSIState == RCC_MSI_OFF))
  404. {
  405. return HAL_ERROR;
  406. }
  407. /* Otherwise, just the calibration and MSI range change are allowed */
  408. else
  409. {
  410. /* Check MSICalibrationValue and MSIClockRange input parameters */
  411. assert_param(IS_RCC_MSICALIBRATION_VALUE(RCC_OscInitStruct->MSICalibrationValue));
  412. assert_param(IS_RCC_MSI_CLOCK_RANGE(RCC_OscInitStruct->MSIClockRange));
  413. /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
  414. must be correctly programmed according to the frequency of the CPU clock
  415. (HCLK) and the supply voltage of the device. */
  416. if(RCC_OscInitStruct->MSIClockRange > __HAL_RCC_GET_MSI_RANGE())
  417. {
  418. /* First increase number of wait states update if necessary */
  419. if(RCC_SetFlashLatencyFromMSIRange(RCC_OscInitStruct->MSIClockRange) != HAL_OK)
  420. {
  421. return HAL_ERROR;
  422. }
  423. /* Selects the Multiple Speed oscillator (MSI) clock range .*/
  424. __HAL_RCC_MSI_RANGE_CONFIG(RCC_OscInitStruct->MSIClockRange);
  425. /* Adjusts the Multiple Speed oscillator (MSI) calibration value.*/
  426. __HAL_RCC_MSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->MSICalibrationValue);
  427. }
  428. else
  429. {
  430. /* Else, keep current flash latency while decreasing applies */
  431. /* Selects the Multiple Speed oscillator (MSI) clock range .*/
  432. __HAL_RCC_MSI_RANGE_CONFIG(RCC_OscInitStruct->MSIClockRange);
  433. /* Adjusts the Multiple Speed oscillator (MSI) calibration value.*/
  434. __HAL_RCC_MSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->MSICalibrationValue);
  435. /* Decrease number of wait states update if necessary */
  436. if(RCC_SetFlashLatencyFromMSIRange(RCC_OscInitStruct->MSIClockRange) != HAL_OK)
  437. {
  438. return HAL_ERROR;
  439. }
  440. }
  441. /* Update the SystemCoreClock global variable */
  442. SystemCoreClock = (32768U * (1UL << ((RCC_OscInitStruct->MSIClockRange >> RCC_ICSCR_MSIRANGE_Pos) + 1U)))
  443. >> AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> RCC_CFGR_HPRE_Pos)];
  444. /* Configure the source of time base considering new system clocks settings*/
  445. status = HAL_InitTick(uwTickPrio);
  446. if(status != HAL_OK)
  447. {
  448. return status;
  449. }
  450. }
  451. }
  452. else
  453. {
  454. /* Check MSI State */
  455. assert_param(IS_RCC_MSI(RCC_OscInitStruct->MSIState));
  456. /* Check the MSI State */
  457. if(RCC_OscInitStruct->MSIState != RCC_MSI_OFF)
  458. {
  459. /* Enable the Multi Speed oscillator (MSI). */
  460. __HAL_RCC_MSI_ENABLE();
  461. /* Get Start Tick */
  462. tickstart = HAL_GetTick();
  463. /* Wait till MSI is ready */
  464. while(__HAL_RCC_GET_FLAG(RCC_FLAG_MSIRDY) == 0U)
  465. {
  466. if((HAL_GetTick() - tickstart) > MSI_TIMEOUT_VALUE)
  467. {
  468. return HAL_TIMEOUT;
  469. }
  470. }
  471. /* Check MSICalibrationValue and MSIClockRange input parameters */
  472. assert_param(IS_RCC_MSICALIBRATION_VALUE(RCC_OscInitStruct->MSICalibrationValue));
  473. assert_param(IS_RCC_MSI_CLOCK_RANGE(RCC_OscInitStruct->MSIClockRange));
  474. /* Selects the Multiple Speed oscillator (MSI) clock range .*/
  475. __HAL_RCC_MSI_RANGE_CONFIG(RCC_OscInitStruct->MSIClockRange);
  476. /* Adjusts the Multiple Speed oscillator (MSI) calibration value.*/
  477. __HAL_RCC_MSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->MSICalibrationValue);
  478. }
  479. else
  480. {
  481. /* Disable the Multi Speed oscillator (MSI). */
  482. __HAL_RCC_MSI_DISABLE();
  483. /* Get Start Tick */
  484. tickstart = HAL_GetTick();
  485. /* Wait till MSI is ready */
  486. while(__HAL_RCC_GET_FLAG(RCC_FLAG_MSIRDY) != 0U)
  487. {
  488. if((HAL_GetTick() - tickstart) > MSI_TIMEOUT_VALUE)
  489. {
  490. return HAL_TIMEOUT;
  491. }
  492. }
  493. }
  494. }
  495. }
  496. /*------------------------------ LSI Configuration -------------------------*/
  497. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI)
  498. {
  499. /* Check the parameters */
  500. assert_param(IS_RCC_LSI(RCC_OscInitStruct->LSIState));
  501. /* Check the LSI State */
  502. if(RCC_OscInitStruct->LSIState != RCC_LSI_OFF)
  503. {
  504. /* Enable the Internal Low Speed oscillator (LSI). */
  505. __HAL_RCC_LSI_ENABLE();
  506. /* Get Start Tick */
  507. tickstart = HAL_GetTick();
  508. /* Wait till LSI is ready */
  509. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) == 0U)
  510. {
  511. if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
  512. {
  513. return HAL_TIMEOUT;
  514. }
  515. }
  516. }
  517. else
  518. {
  519. /* Disable the Internal Low Speed oscillator (LSI). */
  520. __HAL_RCC_LSI_DISABLE();
  521. /* Get Start Tick */
  522. tickstart = HAL_GetTick();
  523. /* Wait till LSI is disabled */
  524. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) != 0U)
  525. {
  526. if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
  527. {
  528. return HAL_TIMEOUT;
  529. }
  530. }
  531. }
  532. }
  533. /*------------------------------ LSE Configuration -------------------------*/
  534. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE)
  535. {
  536. FlagStatus pwrclkchanged = RESET;
  537. /* Check the parameters */
  538. assert_param(IS_RCC_LSE(RCC_OscInitStruct->LSEState));
  539. /* Update LSE configuration in Backup Domain control register */
  540. /* Requires to enable write access to Backup Domain of necessary */
  541. if(__HAL_RCC_PWR_IS_CLK_DISABLED())
  542. {
  543. __HAL_RCC_PWR_CLK_ENABLE();
  544. pwrclkchanged = SET;
  545. }
  546. if(HAL_IS_BIT_CLR(PWR->CR, PWR_CR_DBP))
  547. {
  548. /* Enable write access to Backup domain */
  549. SET_BIT(PWR->CR, PWR_CR_DBP);
  550. /* Wait for Backup domain Write protection disable */
  551. tickstart = HAL_GetTick();
  552. while(HAL_IS_BIT_CLR(PWR->CR, PWR_CR_DBP))
  553. {
  554. if((HAL_GetTick() - tickstart) > RCC_DBP_TIMEOUT_VALUE)
  555. {
  556. return HAL_TIMEOUT;
  557. }
  558. }
  559. }
  560. /* Set the new LSE configuration -----------------------------------------*/
  561. __HAL_RCC_LSE_CONFIG(RCC_OscInitStruct->LSEState);
  562. /* Check the LSE State */
  563. if(RCC_OscInitStruct->LSEState != RCC_LSE_OFF)
  564. {
  565. /* Get Start Tick */
  566. tickstart = HAL_GetTick();
  567. /* Wait till LSE is ready */
  568. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == 0U)
  569. {
  570. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  571. {
  572. return HAL_TIMEOUT;
  573. }
  574. }
  575. }
  576. else
  577. {
  578. /* Get Start Tick */
  579. tickstart = HAL_GetTick();
  580. /* Wait till LSE is disabled */
  581. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) != 0U)
  582. {
  583. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  584. {
  585. return HAL_TIMEOUT;
  586. }
  587. }
  588. }
  589. /* Require to disable power clock if necessary */
  590. if(pwrclkchanged == SET)
  591. {
  592. __HAL_RCC_PWR_CLK_DISABLE();
  593. }
  594. }
  595. /*-------------------------------- PLL Configuration -----------------------*/
  596. /* Check the parameters */
  597. assert_param(IS_RCC_PLL(RCC_OscInitStruct->PLL.PLLState));
  598. if ((RCC_OscInitStruct->PLL.PLLState) != RCC_PLL_NONE)
  599. {
  600. /* Check if the PLL is used as system clock or not */
  601. if(sysclk_source != RCC_SYSCLKSOURCE_STATUS_PLLCLK)
  602. {
  603. if((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_ON)
  604. {
  605. /* Check the parameters */
  606. assert_param(IS_RCC_PLLSOURCE(RCC_OscInitStruct->PLL.PLLSource));
  607. assert_param(IS_RCC_PLL_MUL(RCC_OscInitStruct->PLL.PLLMUL));
  608. assert_param(IS_RCC_PLL_DIV(RCC_OscInitStruct->PLL.PLLDIV));
  609. /* Disable the main PLL. */
  610. __HAL_RCC_PLL_DISABLE();
  611. /* Get Start Tick */
  612. tickstart = HAL_GetTick();
  613. /* Wait till PLL is disabled */
  614. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != 0U)
  615. {
  616. if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  617. {
  618. return HAL_TIMEOUT;
  619. }
  620. }
  621. /* Configure the main PLL clock source, multiplication and division factors. */
  622. __HAL_RCC_PLL_CONFIG(RCC_OscInitStruct->PLL.PLLSource,
  623. RCC_OscInitStruct->PLL.PLLMUL,
  624. RCC_OscInitStruct->PLL.PLLDIV);
  625. /* Enable the main PLL. */
  626. __HAL_RCC_PLL_ENABLE();
  627. /* Get Start Tick */
  628. tickstart = HAL_GetTick();
  629. /* Wait till PLL is ready */
  630. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == 0U)
  631. {
  632. if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  633. {
  634. return HAL_TIMEOUT;
  635. }
  636. }
  637. }
  638. else
  639. {
  640. /* Disable the main PLL. */
  641. __HAL_RCC_PLL_DISABLE();
  642. /* Get Start Tick */
  643. tickstart = HAL_GetTick();
  644. /* Wait till PLL is disabled */
  645. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != 0U)
  646. {
  647. if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  648. {
  649. return HAL_TIMEOUT;
  650. }
  651. }
  652. }
  653. }
  654. else
  655. {
  656. /* Check if there is a request to disable the PLL used as System clock source */
  657. if((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_OFF)
  658. {
  659. return HAL_ERROR;
  660. }
  661. else
  662. {
  663. /* Do not return HAL_ERROR if request repeats the current configuration */
  664. pll_config = RCC->CFGR;
  665. if((READ_BIT(pll_config, RCC_CFGR_PLLSRC) != RCC_OscInitStruct->PLL.PLLSource) ||
  666. (READ_BIT(pll_config, RCC_CFGR_PLLMUL) != RCC_OscInitStruct->PLL.PLLMUL) ||
  667. (READ_BIT(pll_config, RCC_CFGR_PLLDIV) != RCC_OscInitStruct->PLL.PLLDIV))
  668. {
  669. return HAL_ERROR;
  670. }
  671. }
  672. }
  673. }
  674. return HAL_OK;
  675. }
  676. /**
  677. * @brief Initializes the CPU, AHB and APB buses clocks according to the specified
  678. * parameters in the RCC_ClkInitStruct.
  679. * @param RCC_ClkInitStruct pointer to an RCC_OscInitTypeDef structure that
  680. * contains the configuration information for the RCC peripheral.
  681. * @param FLatency FLASH Latency
  682. * The value of this parameter depend on device used within the same series
  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 MSI is used (enabled by hardware) as system clock source after
  687. * start-up from Reset, wake-up from STOP and STANDBY mode, or in case
  688. * of failure of the HSE used directly or indirectly as system clock
  689. * (if the Clock Security System CSS is enabled).
  690. *
  691. * @note A switch from one clock source to another occurs only if the target
  692. * clock source is ready (clock stable after start-up delay or PLL locked).
  693. * If a clock source which is not yet ready is selected, the switch will
  694. * occur when the clock source will be ready.
  695. * You can use @ref HAL_RCC_GetClockConfig() function to know which clock is
  696. * currently used as system clock source.
  697. * @note Depending on the device voltage range, the software has to set correctly
  698. * HPRE[3:0] bits to ensure that HCLK not exceed the maximum allowed frequency
  699. * (for more details refer to section above "Initialization/de-initialization functions")
  700. * @retval HAL status
  701. */
  702. HAL_StatusTypeDef HAL_RCC_ClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t FLatency)
  703. {
  704. uint32_t tickstart;
  705. HAL_StatusTypeDef status;
  706. /* Check the parameters */
  707. if(RCC_ClkInitStruct == NULL)
  708. {
  709. return HAL_ERROR;
  710. }
  711. assert_param(IS_FLASH_LATENCY(FLatency));
  712. /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
  713. must be correctly programmed according to the frequency of the CPU clock
  714. (HCLK) and the supply voltage of the device. */
  715. /* Increasing the number of wait states because of higher CPU frequency */
  716. if(FLatency > __HAL_FLASH_GET_LATENCY())
  717. {
  718. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  719. __HAL_FLASH_SET_LATENCY(FLatency);
  720. /* Check that the new number of wait states is taken into account to access the Flash
  721. memory by reading the FLASH_ACR register */
  722. if(__HAL_FLASH_GET_LATENCY() != FLatency)
  723. {
  724. return HAL_ERROR;
  725. }
  726. }
  727. /*-------------------------- HCLK Configuration --------------------------*/
  728. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)
  729. {
  730. assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
  731. MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
  732. }
  733. /*------------------------- SYSCLK Configuration ---------------------------*/
  734. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_SYSCLK) == RCC_CLOCKTYPE_SYSCLK)
  735. {
  736. assert_param(IS_RCC_SYSCLKSOURCE(RCC_ClkInitStruct->SYSCLKSource));
  737. /* HSE is selected as System Clock Source */
  738. if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
  739. {
  740. /* Check the HSE ready flag */
  741. if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == 0U)
  742. {
  743. return HAL_ERROR;
  744. }
  745. }
  746. /* PLL is selected as System Clock Source */
  747. else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
  748. {
  749. /* Check the PLL ready flag */
  750. if(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == 0U)
  751. {
  752. return HAL_ERROR;
  753. }
  754. }
  755. /* HSI is selected as System Clock Source */
  756. else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSI)
  757. {
  758. /* Check the HSI ready flag */
  759. if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == 0U)
  760. {
  761. return HAL_ERROR;
  762. }
  763. }
  764. /* MSI is selected as System Clock Source */
  765. else
  766. {
  767. /* Check the MSI ready flag */
  768. if(__HAL_RCC_GET_FLAG(RCC_FLAG_MSIRDY) == 0U)
  769. {
  770. return HAL_ERROR;
  771. }
  772. }
  773. __HAL_RCC_SYSCLK_CONFIG(RCC_ClkInitStruct->SYSCLKSource);
  774. /* Get Start Tick */
  775. tickstart = HAL_GetTick();
  776. if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
  777. {
  778. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_HSE)
  779. {
  780. if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
  781. {
  782. return HAL_TIMEOUT;
  783. }
  784. }
  785. }
  786. else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
  787. {
  788. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_PLLCLK)
  789. {
  790. if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
  791. {
  792. return HAL_TIMEOUT;
  793. }
  794. }
  795. }
  796. else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSI)
  797. {
  798. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_HSI)
  799. {
  800. if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
  801. {
  802. return HAL_TIMEOUT;
  803. }
  804. }
  805. }
  806. else
  807. {
  808. while(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_MSI)
  809. {
  810. if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
  811. {
  812. return HAL_TIMEOUT;
  813. }
  814. }
  815. }
  816. }
  817. /* Decreasing the number of wait states because of lower CPU frequency */
  818. if(FLatency < __HAL_FLASH_GET_LATENCY())
  819. {
  820. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  821. __HAL_FLASH_SET_LATENCY(FLatency);
  822. /* Check that the new number of wait states is taken into account to access the Flash
  823. memory by reading the FLASH_ACR register */
  824. if(__HAL_FLASH_GET_LATENCY() != FLatency)
  825. {
  826. return HAL_ERROR;
  827. }
  828. }
  829. /*-------------------------- PCLK1 Configuration ---------------------------*/
  830. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
  831. {
  832. assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB1CLKDivider));
  833. MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE1, RCC_ClkInitStruct->APB1CLKDivider);
  834. }
  835. /*-------------------------- PCLK2 Configuration ---------------------------*/
  836. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK2) == RCC_CLOCKTYPE_PCLK2)
  837. {
  838. assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB2CLKDivider));
  839. MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE2, ((RCC_ClkInitStruct->APB2CLKDivider) << 3U));
  840. }
  841. /* Update the SystemCoreClock global variable */
  842. SystemCoreClock = HAL_RCC_GetSysClockFreq() >> AHBPrescTable[(RCC->CFGR & RCC_CFGR_HPRE)>> RCC_CFGR_HPRE_Pos];
  843. /* Configure the source of time base considering new system clocks settings*/
  844. status = HAL_InitTick(uwTickPrio);
  845. return status;
  846. }
  847. /**
  848. * @}
  849. */
  850. /** @defgroup RCC_Exported_Functions_Group2 Peripheral Control functions
  851. * @brief RCC clocks control functions
  852. *
  853. @verbatim
  854. ===============================================================================
  855. ##### Peripheral Control functions #####
  856. ===============================================================================
  857. [..]
  858. This subsection provides a set of functions allowing to control the RCC Clocks
  859. frequencies.
  860. @endverbatim
  861. * @{
  862. */
  863. /**
  864. * @brief Selects the clock source to output on MCO pin.
  865. * @note MCO pin should be configured in alternate function mode.
  866. * @param RCC_MCOx specifies the output direction for the clock source.
  867. * This parameter can be one of the following values:
  868. * @arg @ref RCC_MCO1 Clock source to output on MCO1 pin(PA8).
  869. * @param RCC_MCOSource specifies the clock source to output.
  870. * This parameter can be one of the following values:
  871. * @arg @ref RCC_MCO1SOURCE_NOCLOCK No clock selected as MCO clock
  872. * @arg @ref RCC_MCO1SOURCE_SYSCLK System clock selected as MCO clock
  873. * @arg @ref RCC_MCO1SOURCE_HSI HSI selected as MCO clock
  874. * @arg @ref RCC_MCO1SOURCE_HSE HSE selected as MCO clock
  875. * @arg @ref RCC_MCO1SOURCE_MSI MSI oscillator clock selected as MCO clock
  876. * @arg @ref RCC_MCO1SOURCE_PLLCLK PLL clock selected as MCO clock
  877. * @arg @ref RCC_MCO1SOURCE_LSI LSI clock selected as MCO clock
  878. * @arg @ref RCC_MCO1SOURCE_LSE LSE clock selected as MCO clock
  879. * @param RCC_MCODiv specifies the MCO DIV.
  880. * This parameter can be one of the following values:
  881. * @arg @ref RCC_MCODIV_1 no division applied to MCO clock
  882. * @arg @ref RCC_MCODIV_2 division by 2 applied to MCO clock
  883. * @arg @ref RCC_MCODIV_4 division by 4 applied to MCO clock
  884. * @arg @ref RCC_MCODIV_8 division by 8 applied to MCO clock
  885. * @arg @ref RCC_MCODIV_16 division by 16 applied to MCO clock
  886. * @retval None
  887. */
  888. void HAL_RCC_MCOConfig(uint32_t RCC_MCOx, uint32_t RCC_MCOSource, uint32_t RCC_MCODiv)
  889. {
  890. GPIO_InitTypeDef gpio;
  891. /* Check the parameters */
  892. assert_param(IS_RCC_MCO(RCC_MCOx));
  893. assert_param(IS_RCC_MCODIV(RCC_MCODiv));
  894. assert_param(IS_RCC_MCO1SOURCE(RCC_MCOSource));
  895. /* Configure the MCO1 pin in alternate function mode */
  896. gpio.Mode = GPIO_MODE_AF_PP;
  897. gpio.Speed = GPIO_SPEED_FREQ_HIGH;
  898. gpio.Pull = GPIO_NOPULL;
  899. gpio.Pin = MCO1_PIN;
  900. gpio.Alternate = GPIO_AF0_MCO;
  901. /* MCO1 Clock Enable */
  902. MCO1_CLK_ENABLE();
  903. HAL_GPIO_Init(MCO1_GPIO_PORT, &gpio);
  904. /* Configure the MCO clock source */
  905. __HAL_RCC_MCO1_CONFIG(RCC_MCOSource, RCC_MCODiv);
  906. }
  907. /**
  908. * @brief Enables the Clock Security System.
  909. * @note If a failure is detected on the HSE oscillator clock, this oscillator
  910. * is automatically disabled and an interrupt is generated to inform the
  911. * software about the failure (Clock Security System Interrupt, CSSI),
  912. * allowing the MCU to perform rescue operations. The CSSI is linked to
  913. * the Cortex-M3 NMI (Non-Maskable Interrupt) exception vector.
  914. * @retval None
  915. */
  916. void HAL_RCC_EnableCSS(void)
  917. {
  918. *(__IO uint32_t *) RCC_CR_CSSON_BB = (uint32_t)ENABLE;
  919. }
  920. /**
  921. * @brief Disables the Clock Security System.
  922. * @retval None
  923. */
  924. void HAL_RCC_DisableCSS(void)
  925. {
  926. *(__IO uint32_t *) RCC_CR_CSSON_BB = (uint32_t)DISABLE;
  927. }
  928. /**
  929. * @brief Returns the SYSCLK frequency
  930. * @note The system frequency computed by this function is not the real
  931. * frequency in the chip. It is calculated based on the predefined
  932. * constant and the selected clock source:
  933. * @note If SYSCLK source is MSI, function returns a value based on MSI
  934. * Value as defined by the MSI range.
  935. * @note If SYSCLK source is HSI, function returns values based on HSI_VALUE(*)
  936. * @note If SYSCLK source is HSE, function returns a value based on HSE_VALUE(**)
  937. * @note If SYSCLK source is PLL, function returns a value based on HSE_VALUE(**)
  938. * or HSI_VALUE(*) multiplied/divided by the PLL factors.
  939. * @note (*) HSI_VALUE is a constant defined in stm32l1xx_hal_conf.h file (default value
  940. * 16 MHz) but the real value may vary depending on the variations
  941. * in voltage and temperature.
  942. * @note (**) HSE_VALUE is a constant defined in stm32l1xx_hal_conf.h file (default value
  943. * 8 MHz), user has to ensure that HSE_VALUE is same as the real
  944. * frequency of the crystal used. Otherwise, this function may
  945. * have wrong result.
  946. *
  947. * @note The result of this function could be not correct when using fractional
  948. * value for HSE crystal.
  949. *
  950. * @note This function can be used by the user application to compute the
  951. * baud-rate for the communication peripherals or configure other parameters.
  952. *
  953. * @note Each time SYSCLK changes, this function must be called to update the
  954. * right SYSCLK value. Otherwise, any configuration based on this function will be incorrect.
  955. *
  956. * @retval SYSCLK frequency
  957. */
  958. uint32_t HAL_RCC_GetSysClockFreq(void)
  959. {
  960. uint32_t tmpreg, pllm, plld, pllvco, msiclkrange, sysclockfreq;
  961. tmpreg = RCC->CFGR;
  962. /* Get SYSCLK source -------------------------------------------------------*/
  963. switch (tmpreg & RCC_CFGR_SWS)
  964. {
  965. case RCC_SYSCLKSOURCE_STATUS_HSI: /* HSI used as system clock source */
  966. {
  967. sysclockfreq = HSI_VALUE;
  968. break;
  969. }
  970. case RCC_SYSCLKSOURCE_STATUS_HSE: /* HSE used as system clock */
  971. {
  972. sysclockfreq = HSE_VALUE;
  973. break;
  974. }
  975. case RCC_SYSCLKSOURCE_STATUS_PLLCLK: /* PLL used as system clock */
  976. {
  977. pllm = PLLMulTable[(uint32_t)(tmpreg & RCC_CFGR_PLLMUL) >> RCC_CFGR_PLLMUL_Pos];
  978. plld = ((uint32_t)(tmpreg & RCC_CFGR_PLLDIV) >> RCC_CFGR_PLLDIV_Pos) + 1U;
  979. if (__HAL_RCC_GET_PLL_OSCSOURCE() != RCC_PLLSOURCE_HSI)
  980. {
  981. /* HSE used as PLL clock source */
  982. pllvco = (HSE_VALUE * pllm) / plld;
  983. }
  984. else
  985. {
  986. /* HSI used as PLL clock source */
  987. pllvco = (HSI_VALUE * pllm) / plld;
  988. }
  989. sysclockfreq = pllvco;
  990. break;
  991. }
  992. case RCC_SYSCLKSOURCE_STATUS_MSI: /* MSI used as system clock source */
  993. default: /* MSI used as system clock */
  994. {
  995. msiclkrange = (RCC->ICSCR & RCC_ICSCR_MSIRANGE ) >> RCC_ICSCR_MSIRANGE_Pos;
  996. sysclockfreq = (32768U * (1UL << (msiclkrange + 1U)));
  997. break;
  998. }
  999. }
  1000. return sysclockfreq;
  1001. }
  1002. /**
  1003. * @brief Returns the HCLK frequency
  1004. * @note Each time HCLK changes, this function must be called to update the
  1005. * right HCLK value. Otherwise, any configuration based on this function will be incorrect.
  1006. *
  1007. * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency
  1008. * and updated within this function
  1009. * @retval HCLK frequency
  1010. */
  1011. uint32_t HAL_RCC_GetHCLKFreq(void)
  1012. {
  1013. return SystemCoreClock;
  1014. }
  1015. /**
  1016. * @brief Returns the PCLK1 frequency
  1017. * @note Each time PCLK1 changes, this function must be called to update the
  1018. * right PCLK1 value. Otherwise, any configuration based on this function will be incorrect.
  1019. * @retval PCLK1 frequency
  1020. */
  1021. uint32_t HAL_RCC_GetPCLK1Freq(void)
  1022. {
  1023. /* Get HCLK source and Compute PCLK1 frequency ---------------------------*/
  1024. return (HAL_RCC_GetHCLKFreq() >> APBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE1) >> RCC_CFGR_PPRE1_Pos]);
  1025. }
  1026. /**
  1027. * @brief Returns the PCLK2 frequency
  1028. * @note Each time PCLK2 changes, this function must be called to update the
  1029. * right PCLK2 value. Otherwise, any configuration based on this function will be incorrect.
  1030. * @retval PCLK2 frequency
  1031. */
  1032. uint32_t HAL_RCC_GetPCLK2Freq(void)
  1033. {
  1034. /* Get HCLK source and Compute PCLK2 frequency ---------------------------*/
  1035. return (HAL_RCC_GetHCLKFreq()>> APBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE2) >> RCC_CFGR_PPRE2_Pos]);
  1036. }
  1037. /**
  1038. * @brief Configures the RCC_OscInitStruct according to the internal
  1039. * RCC configuration registers.
  1040. * @param RCC_OscInitStruct pointer to an RCC_OscInitTypeDef structure that
  1041. * will be configured.
  1042. * @retval None
  1043. */
  1044. void HAL_RCC_GetOscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  1045. {
  1046. /* Check the parameters */
  1047. assert_param(RCC_OscInitStruct != (void *)NULL);
  1048. /* Set all possible values for the Oscillator type parameter ---------------*/
  1049. RCC_OscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI \
  1050. | RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_MSI;
  1051. /* Get the HSE configuration -----------------------------------------------*/
  1052. if((RCC->CR &RCC_CR_HSEBYP) == RCC_CR_HSEBYP)
  1053. {
  1054. RCC_OscInitStruct->HSEState = RCC_HSE_BYPASS;
  1055. }
  1056. else if((RCC->CR &RCC_CR_HSEON) == RCC_CR_HSEON)
  1057. {
  1058. RCC_OscInitStruct->HSEState = RCC_HSE_ON;
  1059. }
  1060. else
  1061. {
  1062. RCC_OscInitStruct->HSEState = RCC_HSE_OFF;
  1063. }
  1064. /* Get the HSI configuration -----------------------------------------------*/
  1065. if((RCC->CR &RCC_CR_HSION) == RCC_CR_HSION)
  1066. {
  1067. RCC_OscInitStruct->HSIState = RCC_HSI_ON;
  1068. }
  1069. else
  1070. {
  1071. RCC_OscInitStruct->HSIState = RCC_HSI_OFF;
  1072. }
  1073. RCC_OscInitStruct->HSICalibrationValue = (uint32_t)((RCC->ICSCR & RCC_ICSCR_HSITRIM) >> RCC_ICSCR_HSITRIM_Pos);
  1074. /* Get the MSI configuration -----------------------------------------------*/
  1075. if((RCC->CR &RCC_CR_MSION) == RCC_CR_MSION)
  1076. {
  1077. RCC_OscInitStruct->MSIState = RCC_MSI_ON;
  1078. }
  1079. else
  1080. {
  1081. RCC_OscInitStruct->MSIState = RCC_MSI_OFF;
  1082. }
  1083. RCC_OscInitStruct->MSICalibrationValue = (uint32_t)((RCC->ICSCR & RCC_ICSCR_MSITRIM) >> RCC_ICSCR_MSITRIM_Pos);
  1084. RCC_OscInitStruct->MSIClockRange = (uint32_t)((RCC->ICSCR & RCC_ICSCR_MSIRANGE));
  1085. /* Get the LSE configuration -----------------------------------------------*/
  1086. if((RCC->CSR &RCC_CSR_LSEBYP) == RCC_CSR_LSEBYP)
  1087. {
  1088. RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS;
  1089. }
  1090. else if((RCC->CSR &RCC_CSR_LSEON) == RCC_CSR_LSEON)
  1091. {
  1092. RCC_OscInitStruct->LSEState = RCC_LSE_ON;
  1093. }
  1094. else
  1095. {
  1096. RCC_OscInitStruct->LSEState = RCC_LSE_OFF;
  1097. }
  1098. /* Get the LSI configuration -----------------------------------------------*/
  1099. if((RCC->CSR &RCC_CSR_LSION) == RCC_CSR_LSION)
  1100. {
  1101. RCC_OscInitStruct->LSIState = RCC_LSI_ON;
  1102. }
  1103. else
  1104. {
  1105. RCC_OscInitStruct->LSIState = RCC_LSI_OFF;
  1106. }
  1107. /* Get the PLL configuration -----------------------------------------------*/
  1108. if((RCC->CR &RCC_CR_PLLON) == RCC_CR_PLLON)
  1109. {
  1110. RCC_OscInitStruct->PLL.PLLState = RCC_PLL_ON;
  1111. }
  1112. else
  1113. {
  1114. RCC_OscInitStruct->PLL.PLLState = RCC_PLL_OFF;
  1115. }
  1116. RCC_OscInitStruct->PLL.PLLSource = (uint32_t)(RCC->CFGR & RCC_CFGR_PLLSRC);
  1117. RCC_OscInitStruct->PLL.PLLMUL = (uint32_t)(RCC->CFGR & RCC_CFGR_PLLMUL);
  1118. RCC_OscInitStruct->PLL.PLLDIV = (uint32_t)(RCC->CFGR & RCC_CFGR_PLLDIV);
  1119. }
  1120. /**
  1121. * @brief Get the RCC_ClkInitStruct according to the internal
  1122. * RCC configuration registers.
  1123. * @param RCC_ClkInitStruct pointer to an RCC_ClkInitTypeDef structure that
  1124. * contains the current clock configuration.
  1125. * @param pFLatency Pointer on the Flash Latency.
  1126. * @retval None
  1127. */
  1128. void HAL_RCC_GetClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t *pFLatency)
  1129. {
  1130. /* Check the parameters */
  1131. assert_param(RCC_ClkInitStruct != (void *)NULL);
  1132. assert_param(pFLatency != (void *)NULL);
  1133. /* Set all possible values for the Clock type parameter --------------------*/
  1134. RCC_ClkInitStruct->ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  1135. /* Get the SYSCLK configuration --------------------------------------------*/
  1136. RCC_ClkInitStruct->SYSCLKSource = (uint32_t)(RCC->CFGR & RCC_CFGR_SW);
  1137. /* Get the HCLK configuration ----------------------------------------------*/
  1138. RCC_ClkInitStruct->AHBCLKDivider = (uint32_t)(RCC->CFGR & RCC_CFGR_HPRE);
  1139. /* Get the APB1 configuration ----------------------------------------------*/
  1140. RCC_ClkInitStruct->APB1CLKDivider = (uint32_t)(RCC->CFGR & RCC_CFGR_PPRE1);
  1141. /* Get the APB2 configuration ----------------------------------------------*/
  1142. RCC_ClkInitStruct->APB2CLKDivider = (uint32_t)((RCC->CFGR & RCC_CFGR_PPRE2) >> 3U);
  1143. /* Get the Flash Wait State (Latency) configuration ------------------------*/
  1144. *pFLatency = __HAL_FLASH_GET_LATENCY();
  1145. }
  1146. /**
  1147. * @brief This function handles the RCC CSS interrupt request.
  1148. * @note This API should be called under the NMI_Handler().
  1149. * @retval None
  1150. */
  1151. void HAL_RCC_NMI_IRQHandler(void)
  1152. {
  1153. /* Check RCC CSSF flag */
  1154. if(__HAL_RCC_GET_IT(RCC_IT_CSS))
  1155. {
  1156. /* RCC Clock Security System interrupt user callback */
  1157. HAL_RCC_CSSCallback();
  1158. /* Clear RCC CSS pending bit */
  1159. __HAL_RCC_CLEAR_IT(RCC_IT_CSS);
  1160. }
  1161. }
  1162. /**
  1163. * @brief RCC Clock Security System interrupt callback
  1164. * @retval none
  1165. */
  1166. __weak void HAL_RCC_CSSCallback(void)
  1167. {
  1168. /* NOTE : This function Should not be modified, when the callback is needed,
  1169. the HAL_RCC_CSSCallback could be implemented in the user file
  1170. */
  1171. }
  1172. /**
  1173. * @}
  1174. */
  1175. /**
  1176. * @}
  1177. */
  1178. /* Private function prototypes -----------------------------------------------*/
  1179. /** @addtogroup RCC_Private_Functions
  1180. * @{
  1181. */
  1182. /**
  1183. * @brief Update number of Flash wait states in line with MSI range and current
  1184. voltage range
  1185. * @param MSIrange MSI range value from RCC_MSIRANGE_0 to RCC_MSIRANGE_6
  1186. * @retval HAL status
  1187. */
  1188. static HAL_StatusTypeDef RCC_SetFlashLatencyFromMSIRange(uint32_t MSIrange)
  1189. {
  1190. uint32_t vos;
  1191. uint32_t latency = FLASH_LATENCY_0; /* default value 0WS */
  1192. /* HCLK can reach 4 MHz only if AHB prescaler = 1 */
  1193. if (READ_BIT(RCC->CFGR, RCC_CFGR_HPRE) == RCC_SYSCLK_DIV1)
  1194. {
  1195. if(__HAL_RCC_PWR_IS_CLK_ENABLED())
  1196. {
  1197. vos = READ_BIT(PWR->CR, PWR_CR_VOS);
  1198. }
  1199. else
  1200. {
  1201. __HAL_RCC_PWR_CLK_ENABLE();
  1202. vos = READ_BIT(PWR->CR, PWR_CR_VOS);
  1203. __HAL_RCC_PWR_CLK_DISABLE();
  1204. }
  1205. /* Check if need to set latency 1 only for Range 3 & HCLK = 4MHz */
  1206. if((vos == PWR_REGULATOR_VOLTAGE_SCALE3) && (MSIrange == RCC_MSIRANGE_6))
  1207. {
  1208. latency = FLASH_LATENCY_1; /* 1WS */
  1209. }
  1210. }
  1211. __HAL_FLASH_SET_LATENCY(latency);
  1212. /* Check that the new number of wait states is taken into account to access the Flash
  1213. memory by reading the FLASH_ACR register */
  1214. if(__HAL_FLASH_GET_LATENCY() != latency)
  1215. {
  1216. return HAL_ERROR;
  1217. }
  1218. return HAL_OK;
  1219. }
  1220. /**
  1221. * @}
  1222. */
  1223. #endif /* HAL_RCC_MODULE_ENABLED */
  1224. /**
  1225. * @}
  1226. */
  1227. /**
  1228. * @}
  1229. */
  1230. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/