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.
 
 
 

1440 lines
52 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_rcc.c
  4. * @author MCD Application Team
  5. * @version V1.3.0
  6. * @date 29-January-2016
  7. * @brief RCC HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the Reset and Clock Control (RCC) peripheral:
  10. * + Initialization and de-initialization functions
  11. * + Peripheral Control functions
  12. *
  13. @verbatim
  14. ==============================================================================
  15. ##### RCC specific features #####
  16. ==============================================================================
  17. [..]
  18. After reset the device is running from Multiple Speed Internal oscillator
  19. (4 MHz) with Flash 0 wait state. Flash prefetch buffer, D-Cache
  20. and I-Cache are disabled, and all peripherals are off except internal
  21. SRAM, Flash and JTAG.
  22. (+) There is no prescaler on High speed (AHBs) and Low speed (APBs) busses:
  23. all peripherals mapped on these busses are running at MSI speed.
  24. (+) The clock for all peripherals is switched off, except the SRAM and FLASH.
  25. (+) All GPIOs are in analog mode, except the JTAG pins which
  26. are assigned to be used for debug purpose.
  27. [..]
  28. Once the device started from reset, the user application has to:
  29. (+) Configure the clock source to be used to drive the System clock
  30. (if the application needs higher frequency/performance)
  31. (+) Configure the System clock frequency and Flash settings
  32. (+) Configure the AHB and APB busses prescalers
  33. (+) Enable the clock for the peripheral(s) to be used
  34. (+) Configure the clock source(s) for peripherals which clocks are not
  35. derived from the System clock (SAIx, RTC, ADC, USB OTG FS/SDMMC1/RNG)
  36. @endverbatim
  37. ******************************************************************************
  38. * @attention
  39. *
  40. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  41. *
  42. * Redistribution and use in source and binary forms, with or without modification,
  43. * are permitted provided that the following conditions are met:
  44. * 1. Redistributions of source code must retain the above copyright notice,
  45. * this list of conditions and the following disclaimer.
  46. * 2. Redistributions in binary form must reproduce the above copyright notice,
  47. * this list of conditions and the following disclaimer in the documentation
  48. * and/or other materials provided with the distribution.
  49. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  50. * may be used to endorse or promote products derived from this software
  51. * without specific prior written permission.
  52. *
  53. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  54. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  55. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  56. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  57. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  58. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  59. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  60. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  61. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  62. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  63. *
  64. ******************************************************************************
  65. */
  66. /* Includes ------------------------------------------------------------------*/
  67. #include "stm32l4xx_hal.h"
  68. /** @addtogroup STM32L4xx_HAL_Driver
  69. * @{
  70. */
  71. /** @defgroup RCC RCC
  72. * @brief RCC HAL module driver
  73. * @{
  74. */
  75. #ifdef HAL_RCC_MODULE_ENABLED
  76. /* Private typedef -----------------------------------------------------------*/
  77. /* Private define ------------------------------------------------------------*/
  78. /** @defgroup RCC_Private_Constants RCC Private Constants
  79. * @{
  80. */
  81. #define HSE_TIMEOUT_VALUE HSE_STARTUP_TIMEOUT
  82. #define HSI_TIMEOUT_VALUE ((uint32_t)2U) /* 2 ms (minimum Tick + 1) */
  83. #define MSI_TIMEOUT_VALUE ((uint32_t)2U) /* 2 ms (minimum Tick + 1) */
  84. #define LSI_TIMEOUT_VALUE ((uint32_t)2U) /* 2 ms (minimum Tick + 1) */
  85. #define PLL_TIMEOUT_VALUE ((uint32_t)2U) /* 2 ms (minimum Tick + 1) */
  86. #define CLOCKSWITCH_TIMEOUT_VALUE ((uint32_t)5000U) /* 5 s */
  87. #define PLLSOURCE_NONE ((uint32_t)0U)
  88. /**
  89. * @}
  90. */
  91. /* Private macro -------------------------------------------------------------*/
  92. /** @defgroup RCC_Private_Macros RCC Private Macros
  93. * @{
  94. */
  95. #define __MCO1_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
  96. #define MCO1_GPIO_PORT GPIOA
  97. #define MCO1_PIN GPIO_PIN_8
  98. #define RCC_PLL_OSCSOURCE_CONFIG(__HAL_RCC_PLLSOURCE__) \
  99. (MODIFY_REG(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC, (uint32_t)(__HAL_RCC_PLLSOURCE__)))
  100. /**
  101. * @}
  102. */
  103. /* Private variables ---------------------------------------------------------*/
  104. /** @defgroup RCC_Private_Variables RCC Private Variables
  105. * @{
  106. */
  107. /**
  108. * @}
  109. */
  110. /* Private function prototypes -----------------------------------------------*/
  111. /** @defgroup RCC_Private_Functions RCC Private Functions
  112. * @{
  113. */
  114. static HAL_StatusTypeDef RCC_SetFlashLatencyFromMSIRange(uint32_t msirange);
  115. /**
  116. * @}
  117. */
  118. /* Exported functions --------------------------------------------------------*/
  119. /** @defgroup RCC_Exported_Functions RCC Exported Functions
  120. * @{
  121. */
  122. /** @defgroup RCC_Exported_Functions_Group1 Initialization and de-initialization functions
  123. * @brief Initialization and Configuration functions
  124. *
  125. @verbatim
  126. ===============================================================================
  127. ##### Initialization and de-initialization functions #####
  128. ===============================================================================
  129. [..]
  130. This section provides functions allowing to configure the internal and external oscillators
  131. (HSE, HSI, LSE, MSI, LSI, PLL, CSS and MCO) and the System busses clocks (SYSCLK, AHB, APB1
  132. and APB2).
  133. [..] Internal/external clock and PLL configuration
  134. (+) HSI (high-speed internal): 16 MHz factory-trimmed RC used directly or through
  135. the PLL as System clock source.
  136. (+) MSI (Mutiple Speed Internal): Its frequency is software trimmable from 100KHZ to 48MHZ.
  137. It can be used to generate the clock for the USB OTG FS (48 MHz).
  138. The number of flash wait states is automatically adjusted when MSI range is updated with
  139. HAL_RCC_OscConfig() and the MSI is used as System clock source.
  140. (+) LSI (low-speed internal): 32 KHz low consumption RC used as IWDG and/or RTC
  141. clock source.
  142. (+) HSE (high-speed external): 4 to 48 MHz crystal oscillator used directly or
  143. through the PLL as System clock source. Can be used also optionally as RTC clock source.
  144. (+) LSE (low-speed external): 32.768 KHz oscillator used optionally as RTC clock source.
  145. (+) PLL (clocked by HSI, HSE or MSI) providing up to three independent output clocks:
  146. (++) The first output is used to generate the high speed system clock (up to 80MHz).
  147. (++) The second output is used to generate the clock for the USB OTG FS (48 MHz),
  148. the random analog generator (<=48 MHz) and the SDMMC1 (<= 48 MHz).
  149. (++) The third output is used to generate an accurate clock to achieve
  150. high-quality audio performance on SAI interface.
  151. (+) PLLSAI1 (clocked by HSI, HSE or MSI) providing up to three independent output clocks:
  152. (++) The first output is used to generate SAR ADC1 clock.
  153. (++) The second output is used to generate the clock for the USB OTG FS (48 MHz),
  154. the random analog generator (<=48 MHz) and the SDMMC1 (<= 48 MHz).
  155. (++) The Third output is used to generate an accurate clock to achieve
  156. high-quality audio performance on SAI interface.
  157. (+) PLLSAI2 (clocked by HSI , HSE or MSI) providing up to two independent output clocks:
  158. (++) The first output is used to generate SAR ADC2 clock.
  159. (++) The second output is used to generate an accurate clock to achieve
  160. high-quality audio performance on SAI interface.
  161. (+) CSS (Clock security system): once enabled, if a HSE clock failure occurs
  162. (HSE used directly or through PLL as System clock source), the System clock
  163. is automatically switched to HSI and an interrupt is generated if enabled.
  164. The interrupt is linked to the Cortex-M4 NMI (Non-Maskable Interrupt)
  165. exception vector.
  166. (+) MCO (microcontroller clock output): used to output MSI, LSI, HSI, LSE, HSE or
  167. main PLL clock (through a configurable prescaler) on PA8 pin.
  168. [..] System, AHB and APB busses clocks configuration
  169. (+) Several clock sources can be used to drive the System clock (SYSCLK): MSI, HSI,
  170. HSE and main PLL.
  171. The AHB clock (HCLK) is derived from System clock through configurable
  172. prescaler and used to clock the CPU, memory and peripherals mapped
  173. on AHB bus (DMA, GPIO...). APB1 (PCLK1) and APB2 (PCLK2) clocks are derived
  174. from AHB clock through configurable prescalers and used to clock
  175. the peripherals mapped on these busses. You can use
  176. "HAL_RCC_GetSysClockFreq()" function to retrieve the frequencies of these clocks.
  177. -@- All the peripheral clocks are derived from the System clock (SYSCLK) except:
  178. (+@) SAI: the SAI clock can be derived either from a specific PLL (PLLSAI1) or (PLLSAI2) or
  179. from an external clock mapped on the SAI_CKIN pin.
  180. You have to use HAL_RCCEx_PeriphCLKConfig() function to configure this clock.
  181. (+@) RTC: the RTC clock can be derived either from the LSI, LSE or HSE clock
  182. divided by 2 to 31.
  183. You have to use __HAL_RCC_RTC_ENABLE() and HAL_RCCEx_PeriphCLKConfig() function
  184. to configure this clock.
  185. (+@) USB OTG FS, SDMMC1 and RNG: USB OTG FS requires a frequency equal to 48 MHz
  186. to work correctly, while the SDMMC1 and RNG peripherals require a frequency
  187. equal or lower than to 48 MHz. This clock is derived of the main PLL or PLLSAI1
  188. through PLLQ divider. You have to enable the peripheral clock and use
  189. HAL_RCCEx_PeriphCLKConfig() function to configure this clock.
  190. (+@) IWDG clock which is always the LSI clock.
  191. (+) The maximum frequency of the SYSCLK, HCLK, PCLK1 and PCLK2 is 80 MHz.
  192. The clock source frequency should be adapted depending on the device voltage range
  193. as listed in the Reference Manual "Clock source frequency versus voltage scaling" chapter.
  194. @endverbatim
  195. Table 1. HCLK clock frequency.
  196. +-------------------------------------------------------+
  197. | Latency | HCLK clock frequency (MHz) |
  198. | |-------------------------------------|
  199. | | voltage range 1 | voltage range 2 |
  200. | | 1.2 V | 1.0 V |
  201. |-----------------|------------------|------------------|
  202. |0WS(1 CPU cycles)| 0 < HCLK <= 16 | 0 < HCLK <= 6 |
  203. |-----------------|------------------|------------------|
  204. |1WS(2 CPU cycles)| 16 < HCLK <= 32 | 6 < HCLK <= 12 |
  205. |-----------------|------------------|------------------|
  206. |2WS(3 CPU cycles)| 32 < HCLK <= 48 | 12 < HCLK <= 18 |
  207. |-----------------|------------------|------------------|
  208. |3WS(4 CPU cycles)| 48 < HCLK <= 64 | 18 < HCLK <= 26 |
  209. |-----------------|------------------|------------------|
  210. |4WS(5 CPU cycles)| 64 < HCLK <= 80 | 18 < HCLK <= 26 |
  211. +-------------------------------------------------------+
  212. * @{
  213. */
  214. /**
  215. * @brief Reset the RCC clock configuration to the default reset state.
  216. * @note The default reset state of the clock configuration is given below:
  217. * - MSI ON and used as system clock source
  218. * - HSE, HSI, PLL, PLLSAI1 and PLLISAI2 OFF
  219. * - AHB, APB1 and APB2 prescaler set to 1.
  220. * - CSS, MCO1 OFF
  221. * - All interrupts disabled
  222. * @note This function doesn't modify the configuration of the
  223. * - Peripheral clocks
  224. * - LSI, LSE and RTC clocks
  225. * @retval None
  226. */
  227. void HAL_RCC_DeInit(void)
  228. {
  229. /* Set MSION bit */
  230. SET_BIT(RCC->CR, RCC_CR_MSION);
  231. /* Insure MSIRDY bit is set before writing default MSIRANGE value */
  232. while(READ_BIT(RCC->CR, RCC_CR_MSIRDY) == RESET) { __NOP(); }
  233. /* Set MSIRANGE default value */
  234. MODIFY_REG(RCC->CR, RCC_CR_MSIRANGE, RCC_MSIRANGE_6);
  235. /* Reset CFGR register (MSI is selected as system clock source) */
  236. CLEAR_REG(RCC->CFGR);
  237. /* Reset HSION, HSIKERON, HSIASFS, HSEON, HSECSSON, PLLON, PLLSAIxON bits */
  238. CLEAR_BIT(RCC->CR, RCC_CR_HSEON | RCC_CR_HSION | RCC_CR_HSIKERON| RCC_CR_HSIASFS | RCC_CR_PLLON | RCC_CR_PLLSAI1ON | RCC_CR_PLLSAI2ON);
  239. /* Reset PLLCFGR register */
  240. CLEAR_REG(RCC->PLLCFGR);
  241. SET_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN_4 );
  242. /* Reset PLLSAI1CFGR register */
  243. CLEAR_REG(RCC->PLLSAI1CFGR);
  244. SET_BIT(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLSAI1N_4 );
  245. /* Reset PLLSAI2CFGR register */
  246. CLEAR_REG(RCC->PLLSAI2CFGR);
  247. SET_BIT(RCC->PLLSAI2CFGR, RCC_PLLSAI2CFGR_PLLSAI2N_4 );
  248. /* Reset HSEBYP bit */
  249. CLEAR_BIT(RCC->CR, RCC_CR_HSEBYP);
  250. /* Disable all interrupts */
  251. CLEAR_REG(RCC->CIER);
  252. /* Update the SystemCoreClock global variable */
  253. SystemCoreClock = MSI_VALUE;
  254. }
  255. /**
  256. * @brief Initialize the RCC Oscillators according to the specified parameters in the
  257. * RCC_OscInitTypeDef.
  258. * @param RCC_OscInitStruct pointer to an RCC_OscInitTypeDef structure that
  259. * contains the configuration information for the RCC Oscillators.
  260. * @note The PLL is not disabled when used as system clock.
  261. * @retval HAL status
  262. */
  263. HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  264. {
  265. uint32_t tickstart = 0;
  266. /* Check the parameters */
  267. assert_param(RCC_OscInitStruct != NULL);
  268. assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct->OscillatorType));
  269. /*----------------------------- MSI Configuration --------------------------*/
  270. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_MSI) == RCC_OSCILLATORTYPE_MSI)
  271. {
  272. /* Check the parameters */
  273. assert_param(IS_RCC_MSI(RCC_OscInitStruct->MSIState));
  274. assert_param(IS_RCC_MSICALIBRATION_VALUE(RCC_OscInitStruct->MSICalibrationValue));
  275. assert_param(IS_RCC_MSI_CLOCK_RANGE(RCC_OscInitStruct->MSIClockRange));
  276. /* When the MSI is used as system clock it will not be disabled */
  277. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_MSI) )
  278. {
  279. if((READ_BIT(RCC->CR, RCC_CR_MSIRDY) != RESET) && (RCC_OscInitStruct->MSIState == RCC_MSI_OFF))
  280. {
  281. return HAL_ERROR;
  282. }
  283. /* Otherwise, just the calibration and MSI range change are allowed */
  284. else
  285. {
  286. /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
  287. must be correctly programmed according to the frequency of the CPU clock
  288. (HCLK) and the supply voltage of the device. */
  289. if(RCC_OscInitStruct->MSIClockRange > __HAL_RCC_GET_MSI_RANGE())
  290. {
  291. /* First increase number of wait states update if necessary */
  292. if(RCC_SetFlashLatencyFromMSIRange(RCC_OscInitStruct->MSIClockRange) != HAL_OK)
  293. {
  294. return HAL_ERROR;
  295. }
  296. /* Selects the Multiple Speed oscillator (MSI) clock range .*/
  297. __HAL_RCC_MSI_RANGE_CONFIG(RCC_OscInitStruct->MSIClockRange);
  298. /* Adjusts the Multiple Speed oscillator (MSI) calibration value.*/
  299. __HAL_RCC_MSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->MSICalibrationValue);
  300. }
  301. else
  302. {
  303. /* Else, keep current flash latency while decreasing applies */
  304. /* Selects the Multiple Speed oscillator (MSI) clock range .*/
  305. __HAL_RCC_MSI_RANGE_CONFIG(RCC_OscInitStruct->MSIClockRange);
  306. /* Adjusts the Multiple Speed oscillator (MSI) calibration value.*/
  307. __HAL_RCC_MSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->MSICalibrationValue);
  308. /* Decrease number of wait states update if necessary */
  309. if(RCC_SetFlashLatencyFromMSIRange(RCC_OscInitStruct->MSIClockRange) != HAL_OK)
  310. {
  311. return HAL_ERROR;
  312. }
  313. }
  314. /* Update the SystemCoreClock global variable */
  315. SystemCoreClock = HAL_RCC_GetSysClockFreq() >> AHBPrescTable[(RCC->CFGR & RCC_CFGR_HPRE)>> POSITION_VAL(RCC_CFGR_HPRE)];
  316. /* Configure the source of time base considering new system clocks settings*/
  317. HAL_InitTick (TICK_INT_PRIORITY);
  318. }
  319. }
  320. else
  321. {
  322. /* Check the MSI State */
  323. if(RCC_OscInitStruct->MSIState != RCC_MSI_OFF)
  324. {
  325. /* Enable the Internal High Speed oscillator (MSI). */
  326. __HAL_RCC_MSI_ENABLE();
  327. /* Get timeout */
  328. tickstart = HAL_GetTick();
  329. /* Wait till MSI is ready */
  330. while(READ_BIT(RCC->CR, RCC_CR_MSIRDY) == RESET)
  331. {
  332. if((HAL_GetTick() - tickstart) > MSI_TIMEOUT_VALUE)
  333. {
  334. return HAL_TIMEOUT;
  335. }
  336. }
  337. /* Selects the Multiple Speed oscillator (MSI) clock range .*/
  338. __HAL_RCC_MSI_RANGE_CONFIG(RCC_OscInitStruct->MSIClockRange);
  339. /* Adjusts the Multiple Speed oscillator (MSI) calibration value.*/
  340. __HAL_RCC_MSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->MSICalibrationValue);
  341. }
  342. else
  343. {
  344. /* Disable the Internal High Speed oscillator (MSI). */
  345. __HAL_RCC_MSI_DISABLE();
  346. /* Get timeout */
  347. tickstart = HAL_GetTick();
  348. /* Wait till MSI is ready */
  349. while(READ_BIT(RCC->CR, RCC_CR_MSIRDY) != RESET)
  350. {
  351. if((HAL_GetTick() - tickstart) > MSI_TIMEOUT_VALUE)
  352. {
  353. return HAL_TIMEOUT;
  354. }
  355. }
  356. }
  357. }
  358. }
  359. /*------------------------------- HSE Configuration ------------------------*/
  360. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE)
  361. {
  362. /* Check the parameters */
  363. assert_param(IS_RCC_HSE(RCC_OscInitStruct->HSEState));
  364. /* When the HSE is used as system clock or clock source for PLL in these cases it is not allowed to be disabled */
  365. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSE) ||
  366. ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL) && (__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_HSE)))
  367. {
  368. if((READ_BIT(RCC->CR, RCC_CR_HSERDY) != RESET) && (RCC_OscInitStruct->HSEState == RCC_HSE_OFF))
  369. {
  370. return HAL_ERROR;
  371. }
  372. }
  373. else
  374. {
  375. /* Reset HSEON and HSEBYP bits before configuring the HSE --------------*/
  376. __HAL_RCC_HSE_CONFIG(RCC_HSE_OFF);
  377. /* Get Start Tick*/
  378. tickstart = HAL_GetTick();
  379. /* Wait till HSE is disabled */
  380. while(READ_BIT(RCC->CR, RCC_CR_HSERDY) != RESET)
  381. {
  382. if((HAL_GetTick() - tickstart) > HSE_TIMEOUT_VALUE)
  383. {
  384. return HAL_TIMEOUT;
  385. }
  386. }
  387. /* Set the new HSE configuration ---------------------------------------*/
  388. __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct->HSEState);
  389. /* Check the HSE State */
  390. if(RCC_OscInitStruct->HSEState != RCC_HSE_OFF)
  391. {
  392. /* Get Start Tick*/
  393. tickstart = HAL_GetTick();
  394. /* Wait till HSE is ready */
  395. while(READ_BIT(RCC->CR, RCC_CR_HSERDY) == RESET)
  396. {
  397. if((HAL_GetTick() - tickstart) > HSE_TIMEOUT_VALUE)
  398. {
  399. return HAL_TIMEOUT;
  400. }
  401. }
  402. }
  403. else
  404. {
  405. /* Get Start Tick*/
  406. tickstart = HAL_GetTick();
  407. /* Wait till HSE is disabled */
  408. while(READ_BIT(RCC->CR, RCC_CR_HSERDY) != RESET)
  409. {
  410. if((HAL_GetTick() - tickstart) > HSE_TIMEOUT_VALUE)
  411. {
  412. return HAL_TIMEOUT;
  413. }
  414. }
  415. }
  416. }
  417. }
  418. /*----------------------------- HSI Configuration --------------------------*/
  419. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI)
  420. {
  421. /* Check the parameters */
  422. assert_param(IS_RCC_HSI(RCC_OscInitStruct->HSIState));
  423. assert_param(IS_RCC_HSI_CALIBRATION_VALUE(RCC_OscInitStruct->HSICalibrationValue));
  424. /* Check if HSI is used as system clock or as PLL source when PLL is selected as system clock */
  425. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSI) ||
  426. ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL) && (__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_HSI)))
  427. {
  428. /* When HSI is used as system clock it will not be disabled */
  429. if((READ_BIT(RCC->CR, RCC_CR_HSIRDY) != RESET) && (RCC_OscInitStruct->HSIState == RCC_HSI_OFF))
  430. {
  431. return HAL_ERROR;
  432. }
  433. /* Otherwise, just the calibration is allowed */
  434. else
  435. {
  436. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  437. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  438. }
  439. }
  440. else
  441. {
  442. /* Check the HSI State */
  443. if(RCC_OscInitStruct->HSIState != RCC_HSI_OFF)
  444. {
  445. /* Enable the Internal High Speed oscillator (HSI). */
  446. __HAL_RCC_HSI_ENABLE();
  447. /* Get Start Tick*/
  448. tickstart = HAL_GetTick();
  449. /* Wait till HSI is ready */
  450. while(READ_BIT(RCC->CR, RCC_CR_HSIRDY) == RESET)
  451. {
  452. if((HAL_GetTick() - tickstart) > HSI_TIMEOUT_VALUE)
  453. {
  454. return HAL_TIMEOUT;
  455. }
  456. }
  457. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  458. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  459. }
  460. else
  461. {
  462. /* Disable the Internal High Speed oscillator (HSI). */
  463. __HAL_RCC_HSI_DISABLE();
  464. /* Get Start Tick*/
  465. tickstart = HAL_GetTick();
  466. /* Wait till HSI is disabled */
  467. while(READ_BIT(RCC->CR, RCC_CR_HSIRDY) != RESET)
  468. {
  469. if((HAL_GetTick() - tickstart) > HSI_TIMEOUT_VALUE)
  470. {
  471. return HAL_TIMEOUT;
  472. }
  473. }
  474. }
  475. }
  476. }
  477. /*------------------------------ LSI Configuration -------------------------*/
  478. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI)
  479. {
  480. /* Check the parameters */
  481. assert_param(IS_RCC_LSI(RCC_OscInitStruct->LSIState));
  482. /* Check the LSI State */
  483. if(RCC_OscInitStruct->LSIState != RCC_LSI_OFF)
  484. {
  485. /* Enable the Internal Low Speed oscillator (LSI). */
  486. __HAL_RCC_LSI_ENABLE();
  487. /* Get Start Tick*/
  488. tickstart = HAL_GetTick();
  489. /* Wait till LSI is ready */
  490. while(READ_BIT(RCC->CSR, RCC_CSR_LSIRDY) == RESET)
  491. {
  492. if((HAL_GetTick() - tickstart) > LSI_TIMEOUT_VALUE)
  493. {
  494. return HAL_TIMEOUT;
  495. }
  496. }
  497. }
  498. else
  499. {
  500. /* Disable the Internal Low Speed oscillator (LSI). */
  501. __HAL_RCC_LSI_DISABLE();
  502. /* Get Start Tick*/
  503. tickstart = HAL_GetTick();
  504. /* Wait till LSI is disabled */
  505. while(READ_BIT(RCC->CSR, RCC_CSR_LSIRDY) != RESET)
  506. {
  507. if((HAL_GetTick() - tickstart) > LSI_TIMEOUT_VALUE)
  508. {
  509. return HAL_TIMEOUT;
  510. }
  511. }
  512. }
  513. }
  514. /*------------------------------ LSE Configuration -------------------------*/
  515. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE)
  516. {
  517. FlagStatus pwrclkchanged = RESET;
  518. /* Check the parameters */
  519. assert_param(IS_RCC_LSE(RCC_OscInitStruct->LSEState));
  520. /* Update LSE configuration in Backup Domain control register */
  521. /* Requires to enable write access to Backup Domain of necessary */
  522. if(HAL_IS_BIT_CLR(RCC->APB1ENR1, RCC_APB1ENR1_PWREN))
  523. {
  524. __HAL_RCC_PWR_CLK_ENABLE();
  525. pwrclkchanged = SET;
  526. }
  527. if(HAL_IS_BIT_CLR(PWR->CR1, PWR_CR1_DBP))
  528. {
  529. /* Enable write access to Backup domain */
  530. SET_BIT(PWR->CR1, PWR_CR1_DBP);
  531. /* Wait for Backup domain Write protection disable */
  532. tickstart = HAL_GetTick();
  533. while(HAL_IS_BIT_CLR(PWR->CR1, PWR_CR1_DBP))
  534. {
  535. if((HAL_GetTick() - tickstart) > RCC_DBP_TIMEOUT_VALUE)
  536. {
  537. return HAL_TIMEOUT;
  538. }
  539. }
  540. }
  541. /* Set the new LSE configuration -----------------------------------------*/
  542. __HAL_RCC_LSE_CONFIG(RCC_OscInitStruct->LSEState);
  543. /* Check the LSE State */
  544. if(RCC_OscInitStruct->LSEState != RCC_LSE_OFF)
  545. {
  546. /* Get Start Tick*/
  547. tickstart = HAL_GetTick();
  548. /* Wait till LSE is ready */
  549. while(READ_BIT(RCC->BDCR, RCC_BDCR_LSERDY) == RESET)
  550. {
  551. if((HAL_GetTick() - tickstart) > RCC_LSE_TIMEOUT_VALUE)
  552. {
  553. return HAL_TIMEOUT;
  554. }
  555. }
  556. }
  557. else
  558. {
  559. /* Get Start Tick*/
  560. tickstart = HAL_GetTick();
  561. /* Wait till LSE is disabled */
  562. while(READ_BIT(RCC->BDCR, RCC_BDCR_LSERDY) != RESET)
  563. {
  564. if((HAL_GetTick() - tickstart) > RCC_LSE_TIMEOUT_VALUE)
  565. {
  566. return HAL_TIMEOUT;
  567. }
  568. }
  569. }
  570. /* Restore clock configuration if changed */
  571. if(pwrclkchanged == SET)
  572. {
  573. __HAL_RCC_PWR_CLK_DISABLE();
  574. }
  575. }
  576. /*-------------------------------- PLL Configuration -----------------------*/
  577. /* Check the parameters */
  578. assert_param(IS_RCC_PLL(RCC_OscInitStruct->PLL.PLLState));
  579. if(RCC_OscInitStruct->PLL.PLLState != RCC_PLL_NONE)
  580. {
  581. /* Check if the PLL is used as system clock or not */
  582. if(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_PLL)
  583. {
  584. if(RCC_OscInitStruct->PLL.PLLState == RCC_PLL_ON)
  585. {
  586. /* Check the parameters */
  587. assert_param(IS_RCC_PLLSOURCE(RCC_OscInitStruct->PLL.PLLSource));
  588. assert_param(IS_RCC_PLLM_VALUE(RCC_OscInitStruct->PLL.PLLM));
  589. assert_param(IS_RCC_PLLN_VALUE(RCC_OscInitStruct->PLL.PLLN));
  590. assert_param(IS_RCC_PLLP_VALUE(RCC_OscInitStruct->PLL.PLLP));
  591. assert_param(IS_RCC_PLLQ_VALUE(RCC_OscInitStruct->PLL.PLLQ));
  592. assert_param(IS_RCC_PLLR_VALUE(RCC_OscInitStruct->PLL.PLLR));
  593. /* Disable the main PLL. */
  594. __HAL_RCC_PLL_DISABLE();
  595. /* Get Start Tick*/
  596. tickstart = HAL_GetTick();
  597. /* Wait till PLL is ready */
  598. while(READ_BIT(RCC->CR, RCC_CR_PLLRDY) != RESET)
  599. {
  600. if((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  601. {
  602. return HAL_TIMEOUT;
  603. }
  604. }
  605. /* Configure the main PLL clock source, multiplication and division factors. */
  606. __HAL_RCC_PLL_CONFIG(RCC_OscInitStruct->PLL.PLLSource,
  607. RCC_OscInitStruct->PLL.PLLM,
  608. RCC_OscInitStruct->PLL.PLLN,
  609. RCC_OscInitStruct->PLL.PLLP,
  610. RCC_OscInitStruct->PLL.PLLQ,
  611. RCC_OscInitStruct->PLL.PLLR);
  612. /* Enable the main PLL. */
  613. __HAL_RCC_PLL_ENABLE();
  614. /* Enable PLL System Clock output. */
  615. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL_SYSCLK);
  616. /* Get Start Tick*/
  617. tickstart = HAL_GetTick();
  618. /* Wait till PLL is ready */
  619. while(READ_BIT(RCC->CR, RCC_CR_PLLRDY) == RESET)
  620. {
  621. if((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  622. {
  623. return HAL_TIMEOUT;
  624. }
  625. }
  626. }
  627. else
  628. {
  629. /* Disable the main PLL. */
  630. __HAL_RCC_PLL_DISABLE();
  631. /* Disable all PLL outputs to save power */
  632. MODIFY_REG(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC, PLLSOURCE_NONE);
  633. __HAL_RCC_PLLCLKOUT_DISABLE(RCC_PLL_SYSCLK | RCC_PLL_48M1CLK | RCC_PLL_SAI3CLK);
  634. /* Get Start Tick*/
  635. tickstart = HAL_GetTick();
  636. /* Wait till PLL is disabled */
  637. while(READ_BIT(RCC->CR, RCC_CR_PLLRDY) != RESET)
  638. {
  639. if((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  640. {
  641. return HAL_TIMEOUT;
  642. }
  643. }
  644. }
  645. }
  646. else
  647. {
  648. return HAL_ERROR;
  649. }
  650. }
  651. return HAL_OK;
  652. }
  653. /**
  654. * @brief Initialize the CPU, AHB and APB busses clocks according to the specified
  655. * parameters in the RCC_ClkInitStruct.
  656. * @param RCC_ClkInitStruct pointer to an RCC_OscInitTypeDef structure that
  657. * contains the configuration information for the RCC peripheral.
  658. * @param FLatency FLASH Latency
  659. * This parameter can be one of the following values:
  660. * @arg FLASH_LATENCY_0 FLASH 0 Latency cycle
  661. * @arg FLASH_LATENCY_1 FLASH 1 Latency cycle
  662. * @arg FLASH_LATENCY_2 FLASH 2 Latency cycle
  663. * @arg FLASH_LATENCY_3 FLASH 3 Latency cycle
  664. * @arg FLASH_LATENCY_4 FLASH 4 Latency cycle
  665. *
  666. * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency
  667. * and updated by HAL_RCC_GetHCLKFreq() function called within this function
  668. *
  669. * @note The MSI is used by default as system clock source after
  670. * startup from Reset, wake-up from STANDBY mode. After restart from Reset,
  671. * the MSI frequency is set to its default value 4 MHz.
  672. *
  673. * @note The HSI can be selected as system clock source after
  674. * from STOP modes or in case of failure of the HSE used directly or indirectly
  675. * as system clock (if the Clock Security System CSS is enabled).
  676. *
  677. * @note A switch from one clock source to another occurs only if the target
  678. * clock source is ready (clock stable after startup delay or PLL locked).
  679. * If a clock source which is not yet ready is selected, the switch will
  680. * occur when the clock source is ready.
  681. *
  682. * @note You can use HAL_RCC_GetClockConfig() function to know which clock is
  683. * currently used as system clock source.
  684. *
  685. * @note Depending on the device voltage range, the software has to set correctly
  686. * HPRE[3:0] bits to ensure that HCLK not exceed the maximum allowed frequency
  687. * (for more details refer to section above "Initialization/de-initialization functions")
  688. * @retval None
  689. */
  690. HAL_StatusTypeDef HAL_RCC_ClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t FLatency)
  691. {
  692. uint32_t tickstart = 0;
  693. /* Check the parameters */
  694. assert_param(RCC_ClkInitStruct != NULL);
  695. assert_param(IS_RCC_CLOCKTYPE(RCC_ClkInitStruct->ClockType));
  696. assert_param(IS_FLASH_LATENCY(FLatency));
  697. /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
  698. must be correctly programmed according to the frequency of the CPU clock
  699. (HCLK) and the supply voltage of the device. */
  700. /* Increasing the number of wait states because of higher CPU frequency */
  701. if(FLatency > (FLASH->ACR & FLASH_ACR_LATENCY))
  702. {
  703. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  704. __HAL_FLASH_SET_LATENCY(FLatency);
  705. /* Check that the new number of wait states is taken into account to access the Flash
  706. memory by reading the FLASH_ACR register */
  707. if((FLASH->ACR & FLASH_ACR_LATENCY) != FLatency)
  708. {
  709. return HAL_ERROR;
  710. }
  711. }
  712. /*-------------------------- HCLK Configuration --------------------------*/
  713. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)
  714. {
  715. assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
  716. MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
  717. }
  718. /*------------------------- SYSCLK Configuration ---------------------------*/
  719. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_SYSCLK) == RCC_CLOCKTYPE_SYSCLK)
  720. {
  721. assert_param(IS_RCC_SYSCLKSOURCE(RCC_ClkInitStruct->SYSCLKSource));
  722. /* HSE is selected as System Clock Source */
  723. if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
  724. {
  725. /* Check the HSE ready flag */
  726. if(READ_BIT(RCC->CR, RCC_CR_HSERDY) == RESET)
  727. {
  728. return HAL_ERROR;
  729. }
  730. }
  731. /* PLL is selected as System Clock Source */
  732. else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
  733. {
  734. /* Check the PLL ready flag */
  735. if(READ_BIT(RCC->CR, RCC_CR_PLLRDY) == RESET)
  736. {
  737. return HAL_ERROR;
  738. }
  739. }
  740. /* MSI is selected as System Clock Source */
  741. else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_MSI)
  742. {
  743. /* Check the MSI ready flag */
  744. if(READ_BIT(RCC->CR, RCC_CR_MSIRDY) == RESET)
  745. {
  746. return HAL_ERROR;
  747. }
  748. }
  749. /* HSI is selected as System Clock Source */
  750. else
  751. {
  752. /* Check the HSI ready flag */
  753. if(READ_BIT(RCC->CR, RCC_CR_HSIRDY) == RESET)
  754. {
  755. return HAL_ERROR;
  756. }
  757. }
  758. MODIFY_REG(RCC->CFGR, RCC_CFGR_SW, RCC_ClkInitStruct->SYSCLKSource);
  759. /* Get Start Tick*/
  760. tickstart = HAL_GetTick();
  761. if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
  762. {
  763. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_HSE)
  764. {
  765. if((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
  766. {
  767. return HAL_TIMEOUT;
  768. }
  769. }
  770. }
  771. else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
  772. {
  773. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_PLL)
  774. {
  775. if((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
  776. {
  777. return HAL_TIMEOUT;
  778. }
  779. }
  780. }
  781. else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_MSI)
  782. {
  783. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_MSI)
  784. {
  785. if((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
  786. {
  787. return HAL_TIMEOUT;
  788. }
  789. }
  790. }
  791. else
  792. {
  793. while(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_HSI)
  794. {
  795. if((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
  796. {
  797. return HAL_TIMEOUT;
  798. }
  799. }
  800. }
  801. }
  802. /* Decreasing the number of wait states because of lower CPU frequency */
  803. if(FLatency < (FLASH->ACR & FLASH_ACR_LATENCY))
  804. {
  805. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  806. __HAL_FLASH_SET_LATENCY(FLatency);
  807. /* Check that the new number of wait states is taken into account to access the Flash
  808. memory by reading the FLASH_ACR register */
  809. if((FLASH->ACR & FLASH_ACR_LATENCY) != FLatency)
  810. {
  811. return HAL_ERROR;
  812. }
  813. }
  814. /*-------------------------- PCLK1 Configuration ---------------------------*/
  815. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
  816. {
  817. assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB1CLKDivider));
  818. MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE1, RCC_ClkInitStruct->APB1CLKDivider);
  819. }
  820. /*-------------------------- PCLK2 Configuration ---------------------------*/
  821. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK2) == RCC_CLOCKTYPE_PCLK2)
  822. {
  823. assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB2CLKDivider));
  824. MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE2, ((RCC_ClkInitStruct->APB2CLKDivider) << 3U));
  825. }
  826. /* Update the SystemCoreClock global variable */
  827. SystemCoreClock = HAL_RCC_GetSysClockFreq() >> AHBPrescTable[(RCC->CFGR & RCC_CFGR_HPRE)>> POSITION_VAL(RCC_CFGR_HPRE)];
  828. /* Configure the source of time base considering new system clocks settings*/
  829. HAL_InitTick (TICK_INT_PRIORITY);
  830. return HAL_OK;
  831. }
  832. /**
  833. * @}
  834. */
  835. /** @defgroup RCC_Exported_Functions_Group2 Peripheral Control functions
  836. * @brief RCC clocks control functions
  837. *
  838. @verbatim
  839. ===============================================================================
  840. ##### Peripheral Control functions #####
  841. ===============================================================================
  842. [..]
  843. This subsection provides a set of functions allowing to:
  844. (+) Ouput clock to MCO pin.
  845. (+) Retrieve current clock frequencies.
  846. (+) Enable the Clock Security System.
  847. @endverbatim
  848. * @{
  849. */
  850. /**
  851. * @brief Select the clock source to output on MCO pin(PA8).
  852. * @note PA8 should be configured in alternate function mode.
  853. * @param RCC_MCOx specifies the output direction for the clock source.
  854. * For STM32L4xx family this parameter can have only one value:
  855. * @arg @ref RCC_MCO1 Clock source to output on MCO1 pin(PA8).
  856. * @param RCC_MCOSource specifies the clock source to output.
  857. * This parameter can be one of the following values:
  858. * @arg @ref RCC_MCO1SOURCE_NOCLOCK MCO output disabled, no clock on MCO
  859. * @arg @ref RCC_MCO1SOURCE_SYSCLK system clock selected as MCO source
  860. * @arg @ref RCC_MCO1SOURCE_MSI MSI clock selected as MCO source
  861. * @arg @ref RCC_MCO1SOURCE_HSI HSI clock selected as MCO source
  862. * @arg @ref RCC_MCO1SOURCE_HSE HSE clock selected as MCO sourcee
  863. * @arg @ref RCC_MCO1SOURCE_PLLCLK main PLL clock selected as MCO source
  864. * @arg @ref RCC_MCO1SOURCE_LSI LSI clock selected as MCO source
  865. * @arg @ref RCC_MCO1SOURCE_LSE LSE clock selected as MCO source
  866. * @param RCC_MCODiv specifies the MCO prescaler.
  867. * This parameter can be one of the following values:
  868. * @arg @ref RCC_MCODIV_1 no division applied to MCO clock
  869. * @arg @ref RCC_MCODIV_2 division by 2 applied to MCO clock
  870. * @arg @ref RCC_MCODIV_4 division by 4 applied to MCO clock
  871. * @arg @ref RCC_MCODIV_8 division by 8 applied to MCO clock
  872. * @arg @ref RCC_MCODIV_16 division by 16 applied to MCO clock
  873. * @retval None
  874. */
  875. void HAL_RCC_MCOConfig( uint32_t RCC_MCOx, uint32_t RCC_MCOSource, uint32_t RCC_MCODiv)
  876. {
  877. GPIO_InitTypeDef GPIO_InitStruct;
  878. /* Check the parameters */
  879. assert_param(IS_RCC_MCO(RCC_MCOx));
  880. assert_param(IS_RCC_MCODIV(RCC_MCODiv));
  881. assert_param(IS_RCC_MCO1SOURCE(RCC_MCOSource));
  882. /* MCO Clock Enable */
  883. __MCO1_CLK_ENABLE();
  884. /* Configue the MCO1 pin in alternate function mode */
  885. GPIO_InitStruct.Pin = MCO1_PIN;
  886. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  887. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  888. GPIO_InitStruct.Pull = GPIO_NOPULL;
  889. GPIO_InitStruct.Alternate = GPIO_AF0_MCO;
  890. HAL_GPIO_Init(MCO1_GPIO_PORT, &GPIO_InitStruct);
  891. /* Mask MCOSEL[] and MCOPRE[] bits then set MCO1 clock source and prescaler */
  892. MODIFY_REG(RCC->CFGR, (RCC_CFGR_MCOSEL | RCC_CFGR_MCOPRE), (RCC_MCOSource | RCC_MCODiv ));
  893. }
  894. /**
  895. * @brief Return the SYSCLK frequency.
  896. *
  897. * @note The system frequency computed by this function is not the real
  898. * frequency in the chip. It is calculated based on the predefined
  899. * constant and the selected clock source:
  900. * @note If SYSCLK source is MSI, function returns values based on MSI
  901. * Value as defined by the MSI range.
  902. * @note If SYSCLK source is HSI, function returns values based on HSI_VALUE(*)
  903. * @note If SYSCLK source is HSE, function returns values based on HSE_VALUE(**)
  904. * @note If SYSCLK source is PLL, function returns values based on HSE_VALUE(**),
  905. * HSI_VALUE(*) or MSI Value multiplied/divided by the PLL factors.
  906. * @note (*) HSI_VALUE is a constant defined in stm32l4xx_hal_conf.h file (default value
  907. * 16 MHz) but the real value may vary depending on the variations
  908. * in voltage and temperature.
  909. * @note (**) HSE_VALUE is a constant defined in stm32l4xx_hal_conf.h file (default value
  910. * 8 MHz), user has to ensure that HSE_VALUE is same as the real
  911. * frequency of the crystal used. Otherwise, this function may
  912. * have wrong result.
  913. *
  914. * @note The result of this function could be not correct when using fractional
  915. * value for HSE crystal.
  916. *
  917. * @note This function can be used by the user application to compute the
  918. * baudrate for the communication peripherals or configure other parameters.
  919. *
  920. * @note Each time SYSCLK changes, this function must be called to update the
  921. * right SYSCLK value. Otherwise, any configuration based on this function will be incorrect.
  922. *
  923. *
  924. * @retval SYSCLK frequency
  925. */
  926. uint32_t HAL_RCC_GetSysClockFreq(void)
  927. {
  928. uint32_t msirange = 0U, pllvco = 0U, pllsource = 0U, pllr = 2U, pllm = 2U;
  929. uint32_t sysclockfreq = 0U;
  930. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_MSI) ||
  931. ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL) && (__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_MSI)))
  932. {
  933. /* MSI or PLL with MSI source used as system clock source */
  934. /* Get SYSCLK source */
  935. if(READ_BIT(RCC->CR, RCC_CR_MSIRGSEL) == RESET)
  936. { /* MSISRANGE from RCC_CSR applies */
  937. msirange = (RCC->CSR & RCC_CSR_MSISRANGE) >> POSITION_VAL(RCC_CSR_MSISRANGE);
  938. }
  939. else
  940. { /* MSIRANGE from RCC_CR applies */
  941. msirange = (RCC->CR & RCC_CR_MSIRANGE) >> POSITION_VAL(RCC_CR_MSIRANGE);
  942. }
  943. /*MSI frequency range in HZ*/
  944. msirange = MSIRangeTable[msirange];
  945. if(__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_MSI)
  946. {
  947. /* MSI used as system clock source */
  948. sysclockfreq = msirange;
  949. }
  950. }
  951. else if(__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSI)
  952. {
  953. /* HSI used as system clock source */
  954. sysclockfreq = HSI_VALUE;
  955. }
  956. else if(__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSE)
  957. {
  958. /* HSE used as system clock source */
  959. sysclockfreq = HSE_VALUE;
  960. }
  961. if(__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL)
  962. {
  963. /* PLL used as system clock source */
  964. /* PLL_VCO = (HSE_VALUE or HSI_VALUE or MSI_VALUE/ PLLM) * PLLN
  965. SYSCLK = PLL_VCO / PLLR
  966. */
  967. pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC);
  968. pllm = ((RCC->PLLCFGR & RCC_PLLCFGR_PLLM) >> POSITION_VAL(RCC_PLLCFGR_PLLM)) + 1U ;
  969. switch (pllsource)
  970. {
  971. case RCC_PLLSOURCE_HSI: /* HSI used as PLL clock source */
  972. pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> POSITION_VAL(RCC_PLLCFGR_PLLN));
  973. break;
  974. case RCC_PLLSOURCE_HSE: /* HSE used as PLL clock source */
  975. pllvco = (HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> POSITION_VAL(RCC_PLLCFGR_PLLN));
  976. break;
  977. case RCC_PLLSOURCE_MSI: /* MSI used as PLL clock source */
  978. default:
  979. pllvco = (msirange / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> POSITION_VAL(RCC_PLLCFGR_PLLN));
  980. break;
  981. }
  982. pllr = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> POSITION_VAL(RCC_PLLCFGR_PLLR)) + 1U ) * 2U;
  983. sysclockfreq = pllvco/pllr;
  984. }
  985. return sysclockfreq;
  986. }
  987. /**
  988. * @brief Return the HCLK frequency.
  989. * @note Each time HCLK changes, this function must be called to update the
  990. * right HCLK value. Otherwise, any configuration based on this function will be incorrect.
  991. *
  992. * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency.
  993. * @retval HCLK frequency in Hz
  994. */
  995. uint32_t HAL_RCC_GetHCLKFreq(void)
  996. {
  997. return SystemCoreClock;
  998. }
  999. /**
  1000. * @brief Return the PCLK1 frequency.
  1001. * @note Each time PCLK1 changes, this function must be called to update the
  1002. * right PCLK1 value. Otherwise, any configuration based on this function will be incorrect.
  1003. * @retval PCLK1 frequency in Hz
  1004. */
  1005. uint32_t HAL_RCC_GetPCLK1Freq(void)
  1006. {
  1007. /* Get HCLK source and Compute PCLK1 frequency ---------------------------*/
  1008. return (HAL_RCC_GetHCLKFreq() >> APBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE1)>> POSITION_VAL(RCC_CFGR_PPRE1)]);
  1009. }
  1010. /**
  1011. * @brief Return the PCLK2 frequency.
  1012. * @note Each time PCLK2 changes, this function must be called to update the
  1013. * right PCLK2 value. Otherwise, any configuration based on this function will be incorrect.
  1014. * @retval PCLK2 frequency in Hz
  1015. */
  1016. uint32_t HAL_RCC_GetPCLK2Freq(void)
  1017. {
  1018. /* Get HCLK source and Compute PCLK2 frequency ---------------------------*/
  1019. return (HAL_RCC_GetHCLKFreq()>> APBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE2)>> POSITION_VAL(RCC_CFGR_PPRE2)]);
  1020. }
  1021. /**
  1022. * @brief Configure the RCC_OscInitStruct according to the internal
  1023. * RCC configuration registers.
  1024. * @param RCC_OscInitStruct pointer to an RCC_OscInitTypeDef structure that
  1025. * will be configured.
  1026. * @retval None
  1027. */
  1028. void HAL_RCC_GetOscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  1029. {
  1030. /* Check the parameters */
  1031. assert_param(RCC_OscInitStruct != NULL);
  1032. /* Set all possible values for the Oscillator type parameter ---------------*/
  1033. RCC_OscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_MSI | \
  1034. RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_LSI;
  1035. /* Get the HSE configuration -----------------------------------------------*/
  1036. if((RCC->CR & RCC_CR_HSEBYP) == RCC_CR_HSEBYP)
  1037. {
  1038. RCC_OscInitStruct->HSEState = RCC_HSE_BYPASS;
  1039. }
  1040. else if((RCC->CR & RCC_CR_HSEON) == RCC_CR_HSEON)
  1041. {
  1042. RCC_OscInitStruct->HSEState = RCC_HSE_ON;
  1043. }
  1044. else
  1045. {
  1046. RCC_OscInitStruct->HSEState = RCC_HSE_OFF;
  1047. }
  1048. /* Get the MSI configuration -----------------------------------------------*/
  1049. if((RCC->CR & RCC_CR_MSION) == RCC_CR_MSION)
  1050. {
  1051. RCC_OscInitStruct->MSIState = RCC_MSI_ON;
  1052. }
  1053. else
  1054. {
  1055. RCC_OscInitStruct->MSIState = RCC_MSI_OFF;
  1056. }
  1057. RCC_OscInitStruct->MSICalibrationValue = (uint32_t)((RCC->CR & RCC_ICSCR_MSITRIM) >> POSITION_VAL(RCC_ICSCR_MSITRIM));
  1058. RCC_OscInitStruct->MSIClockRange = (uint32_t)((RCC->CR & RCC_CR_MSIRANGE) );
  1059. /* Get the HSI configuration -----------------------------------------------*/
  1060. if((RCC->CR & RCC_CR_HSION) == RCC_CR_HSION)
  1061. {
  1062. RCC_OscInitStruct->HSIState = RCC_HSI_ON;
  1063. }
  1064. else
  1065. {
  1066. RCC_OscInitStruct->HSIState = RCC_HSI_OFF;
  1067. }
  1068. RCC_OscInitStruct->HSICalibrationValue = (uint32_t)((RCC->ICSCR & RCC_ICSCR_HSITRIM) >> POSITION_VAL(RCC_ICSCR_HSITRIM));
  1069. /* Get the LSE configuration -----------------------------------------------*/
  1070. if((RCC->BDCR & RCC_BDCR_LSEBYP) == RCC_BDCR_LSEBYP)
  1071. {
  1072. RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS;
  1073. }
  1074. else if((RCC->BDCR & RCC_BDCR_LSEON) == RCC_BDCR_LSEON)
  1075. {
  1076. RCC_OscInitStruct->LSEState = RCC_LSE_ON;
  1077. }
  1078. else
  1079. {
  1080. RCC_OscInitStruct->LSEState = RCC_LSE_OFF;
  1081. }
  1082. /* Get the LSI configuration -----------------------------------------------*/
  1083. if((RCC->CSR & RCC_CSR_LSION) == RCC_CSR_LSION)
  1084. {
  1085. RCC_OscInitStruct->LSIState = RCC_LSI_ON;
  1086. }
  1087. else
  1088. {
  1089. RCC_OscInitStruct->LSIState = RCC_LSI_OFF;
  1090. }
  1091. /* Get the PLL configuration -----------------------------------------------*/
  1092. if((RCC->CR & RCC_CR_PLLON) == RCC_CR_PLLON)
  1093. {
  1094. RCC_OscInitStruct->PLL.PLLState = RCC_PLL_ON;
  1095. }
  1096. else
  1097. {
  1098. RCC_OscInitStruct->PLL.PLLState = RCC_PLL_OFF;
  1099. }
  1100. RCC_OscInitStruct->PLL.PLLSource = (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC);
  1101. RCC_OscInitStruct->PLL.PLLM = (uint32_t)(((RCC->PLLCFGR & RCC_PLLCFGR_PLLM) >> POSITION_VAL(RCC_PLLCFGR_PLLM)) + 1U);
  1102. RCC_OscInitStruct->PLL.PLLN = (uint32_t)((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> POSITION_VAL(RCC_PLLCFGR_PLLN));
  1103. RCC_OscInitStruct->PLL.PLLQ = (uint32_t)((((RCC->PLLCFGR & RCC_PLLCFGR_PLLQ) >> POSITION_VAL(RCC_PLLCFGR_PLLQ)) + 1U) << 1U);
  1104. RCC_OscInitStruct->PLL.PLLR = (uint32_t)((((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> POSITION_VAL(RCC_PLLCFGR_PLLR)) + 1U) << 1U);
  1105. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) != RESET)
  1106. {
  1107. RCC_OscInitStruct->PLL.PLLP = RCC_PLLP_DIV17;
  1108. }
  1109. else
  1110. {
  1111. RCC_OscInitStruct->PLL.PLLP = RCC_PLLP_DIV7;
  1112. }
  1113. }
  1114. /**
  1115. * @brief Configure the RCC_ClkInitStruct according to the internal
  1116. * RCC configuration registers.
  1117. * @param RCC_ClkInitStruct pointer to an RCC_ClkInitTypeDef structure that
  1118. * will be configured.
  1119. * @param pFLatency Pointer on the Flash Latency.
  1120. * @retval None
  1121. */
  1122. void HAL_RCC_GetClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t *pFLatency)
  1123. {
  1124. /* Check the parameters */
  1125. assert_param(RCC_ClkInitStruct != NULL);
  1126. assert_param(pFLatency != NULL);
  1127. /* Set all possible values for the Clock type parameter --------------------*/
  1128. RCC_ClkInitStruct->ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  1129. /* Get the SYSCLK configuration --------------------------------------------*/
  1130. RCC_ClkInitStruct->SYSCLKSource = (uint32_t)(RCC->CFGR & RCC_CFGR_SW);
  1131. /* Get the HCLK configuration ----------------------------------------------*/
  1132. RCC_ClkInitStruct->AHBCLKDivider = (uint32_t)(RCC->CFGR & RCC_CFGR_HPRE);
  1133. /* Get the APB1 configuration ----------------------------------------------*/
  1134. RCC_ClkInitStruct->APB1CLKDivider = (uint32_t)(RCC->CFGR & RCC_CFGR_PPRE1);
  1135. /* Get the APB2 configuration ----------------------------------------------*/
  1136. RCC_ClkInitStruct->APB2CLKDivider = (uint32_t)((RCC->CFGR & RCC_CFGR_PPRE2) >> 3U);
  1137. /* Get the Flash Wait State (Latency) configuration ------------------------*/
  1138. *pFLatency = (uint32_t)(FLASH->ACR & FLASH_ACR_LATENCY);
  1139. }
  1140. /**
  1141. * @brief Enable the Clock Security System.
  1142. * @note If a failure is detected on the HSE oscillator clock, this oscillator
  1143. * is automatically disabled and an interrupt is generated to inform the
  1144. * software about the failure (Clock Security System Interrupt, CSSI),
  1145. * allowing the MCU to perform rescue operations. The CSSI is linked to
  1146. * the Cortex-M4 NMI (Non-Maskable Interrupt) exception vector.
  1147. * @note The Clock Security System can only be cleared by reset.
  1148. * @retval None
  1149. */
  1150. void HAL_RCC_EnableCSS(void)
  1151. {
  1152. SET_BIT(RCC->CR, RCC_CR_CSSON) ;
  1153. }
  1154. /**
  1155. * @brief Handle the RCC Clock Security System interrupt request.
  1156. * @note This API should be called under the NMI_Handler().
  1157. * @retval None
  1158. */
  1159. void HAL_RCC_NMI_IRQHandler(void)
  1160. {
  1161. /* Check RCC CSSF interrupt flag */
  1162. if(__HAL_RCC_GET_IT(RCC_IT_CSS))
  1163. {
  1164. /* RCC Clock Security System interrupt user callback */
  1165. HAL_RCC_CSSCallback();
  1166. /* Clear RCC CSS pending bit */
  1167. __HAL_RCC_CLEAR_IT(RCC_IT_CSS);
  1168. }
  1169. }
  1170. /**
  1171. * @brief RCC Clock Security System interrupt callback.
  1172. * @retval none
  1173. */
  1174. __weak void HAL_RCC_CSSCallback(void)
  1175. {
  1176. /* NOTE : This function should not be modified, when the callback is needed,
  1177. the HAL_RCC_CSSCallback should be implemented in the user file
  1178. */
  1179. }
  1180. /**
  1181. * @}
  1182. */
  1183. /**
  1184. * @}
  1185. */
  1186. /* Private function prototypes -----------------------------------------------*/
  1187. /** @addtogroup RCC_Private_Functions
  1188. * @{
  1189. */
  1190. /**
  1191. * @brief Update number of Flash wait states in line with MSI range and current
  1192. voltage range.
  1193. * @param msirange MSI range value from RCC_MSIRANGE_0 to RCC_MSIRANGE_11
  1194. * @retval HAL status
  1195. */
  1196. static HAL_StatusTypeDef RCC_SetFlashLatencyFromMSIRange(uint32_t msirange)
  1197. {
  1198. uint32_t vos = 0;
  1199. uint32_t latency = FLASH_LATENCY_0; /* default value 0WS */
  1200. if(__HAL_RCC_PWR_IS_CLK_ENABLED())
  1201. {
  1202. vos = HAL_PWREx_GetVoltageRange();
  1203. }
  1204. else
  1205. {
  1206. __HAL_RCC_PWR_CLK_ENABLE();
  1207. vos = HAL_PWREx_GetVoltageRange();
  1208. __HAL_RCC_PWR_CLK_DISABLE();
  1209. }
  1210. if(vos == PWR_REGULATOR_VOLTAGE_SCALE1)
  1211. {
  1212. if(msirange > RCC_MSIRANGE_8)
  1213. {
  1214. /* MSI > 16Mhz */
  1215. if(msirange > RCC_MSIRANGE_10)
  1216. {
  1217. /* MSI 48Mhz */
  1218. latency = FLASH_LATENCY_2; /* 2WS */
  1219. }
  1220. else
  1221. {
  1222. /* MSI 24Mhz or 32Mhz */
  1223. latency = FLASH_LATENCY_1; /* 1WS */
  1224. }
  1225. }
  1226. /* else MSI <= 16Mhz default FLASH_LATENCY_0 0WS */
  1227. }
  1228. else
  1229. {
  1230. if(msirange > RCC_MSIRANGE_8)
  1231. {
  1232. /* MSI > 16Mhz */
  1233. latency = FLASH_LATENCY_3; /* 3WS */
  1234. }
  1235. else
  1236. {
  1237. if(msirange == RCC_MSIRANGE_8)
  1238. {
  1239. /* MSI 16Mhz */
  1240. latency = FLASH_LATENCY_2; /* 2WS */
  1241. }
  1242. else if(msirange == RCC_MSIRANGE_7)
  1243. {
  1244. /* MSI 8Mhz */
  1245. latency = FLASH_LATENCY_1; /* 1WS */
  1246. }
  1247. /* else MSI < 8Mhz default FLASH_LATENCY_0 0WS */
  1248. }
  1249. }
  1250. __HAL_FLASH_SET_LATENCY(latency);
  1251. /* Check that the new number of wait states is taken into account to access the Flash
  1252. memory by reading the FLASH_ACR register */
  1253. if((FLASH->ACR & FLASH_ACR_LATENCY) != latency)
  1254. {
  1255. return HAL_ERROR;
  1256. }
  1257. return HAL_OK;
  1258. }
  1259. /**
  1260. * @}
  1261. */
  1262. #endif /* HAL_RCC_MODULE_ENABLED */
  1263. /**
  1264. * @}
  1265. */
  1266. /**
  1267. * @}
  1268. */
  1269. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/