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.
 
 
 

1491 lines
52 KiB

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