STM32L4xx_HAL_Driver  1.14.0
Initialization and de-initialization functions

Initialization and Configuration functions. More...

Functions

HAL_StatusTypeDef HAL_RCC_DeInit (void)
 Reset the RCC clock configuration to the default reset state. More...
 
HAL_StatusTypeDef HAL_RCC_OscConfig (RCC_OscInitTypeDef *RCC_OscInitStruct)
 Initialize the RCC Oscillators according to the specified parameters in the RCC_OscInitTypeDef. More...
 
HAL_StatusTypeDef HAL_RCC_ClockConfig (RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t FLatency)
 Initialize the CPU, AHB and APB busses clocks according to the specified parameters in the RCC_ClkInitStruct. More...
 

Detailed Description

Initialization and Configuration functions.

===============================================================================
          ##### Initialization and de-initialization functions #####
===============================================================================
   [..]
     This section provides functions allowing to configure the internal and external oscillators
     (HSE, HSI, LSE, MSI, LSI, PLL, CSS and MCO) and the System busses clocks (SYSCLK, AHB, APB1
      and APB2).

   [..] Internal/external clock and PLL configuration
        (+) HSI (high-speed internal): 16 MHz factory-trimmed RC used directly or through
            the PLL as System clock source.

        (+) MSI (Mutiple Speed Internal): Its frequency is software trimmable from 100KHZ to 48MHZ.
            It can be used to generate the clock for the USB OTG FS (48 MHz).
            The number of flash wait states is automatically adjusted when MSI range is updated with
            HAL_RCC_OscConfig() and the MSI is used as System clock source.

        (+) LSI (low-speed internal): 32 KHz low consumption RC used as IWDG and/or RTC
            clock source.

        (+) HSE (high-speed external): 4 to 48 MHz crystal oscillator used directly or
            through the PLL as System clock source. Can be used also optionally as RTC clock source.

        (+) LSE (low-speed external): 32.768 KHz oscillator used optionally as RTC clock source.

        (+) PLL (clocked by HSI, HSE or MSI) providing up to three independent output clocks:
          (++) The first output is used to generate the high speed system clock (up to 80MHz).
          (++) The second output is used to generate the clock for the USB OTG FS (48 MHz),
               the random analog generator (<=48 MHz) and the SDMMC1 (<= 48 MHz).
          (++) The third output is used to generate an accurate clock to achieve
               high-quality audio performance on SAI interface.

        (+) PLLSAI1 (clocked by HSI, HSE or MSI) providing up to three independent output clocks:
          (++) The first output is used to generate SAR ADC1 clock.
          (++) The second output is used to generate the clock for the USB OTG FS (48 MHz),
               the random analog generator (<=48 MHz) and the SDMMC1 (<= 48 MHz).
          (++) The Third output is used to generate an accurate clock to achieve
               high-quality audio performance on SAI interface.

        (+) PLLSAI2 (clocked by HSI, HSE or MSI) providing up to two independent output clocks:
          (++) The first output is used to generate SAR ADC2 clock.
          (++) The second  output is used to generate an accurate clock to achieve
               high-quality audio performance on SAI interface.

        (+) CSS (Clock security system): once enabled, if a HSE clock failure occurs
           (HSE used directly or through PLL as System clock source), the System clock
            is automatically switched to HSI and an interrupt is generated if enabled.
            The interrupt is linked to the Cortex-M4 NMI (Non-Maskable Interrupt)
            exception vector.

        (+) MCO (microcontroller clock output): used to output MSI, LSI, HSI, LSE, HSE or
            main PLL clock (through a configurable prescaler) on PA8 pin.

   [..] System, AHB and APB busses clocks configuration
        (+) Several clock sources can be used to drive the System clock (SYSCLK): MSI, HSI,
            HSE and main PLL.
            The AHB clock (HCLK) is derived from System clock through configurable
            prescaler and used to clock the CPU, memory and peripherals mapped
            on AHB bus (DMA, GPIO...). APB1 (PCLK1) and APB2 (PCLK2) clocks are derived
            from AHB clock through configurable prescalers and used to clock
            the peripherals mapped on these busses. You can use
            "HAL_RCC_GetSysClockFreq()" function to retrieve the frequencies of these clocks.

        -@- All the peripheral clocks are derived from the System clock (SYSCLK) except:

          (+@) SAI: the SAI clock can be derived either from a specific PLL (PLLSAI1) or (PLLSAI2) or
               from an external clock mapped on the SAI_CKIN pin.
               You have to use HAL_RCCEx_PeriphCLKConfig() function to configure this clock.
          (+@) RTC: the RTC clock can be derived either from the LSI, LSE or HSE clock
               divided by 2 to 31.
               You have to use __HAL_RCC_RTC_ENABLE() and HAL_RCCEx_PeriphCLKConfig() function
               to configure this clock.
          (+@) USB OTG FS, SDMMC1 and RNG: USB OTG FS requires a frequency equal to 48 MHz
               to work correctly, while the SDMMC1 and RNG peripherals require a frequency
               equal or lower than to 48 MHz. This clock is derived of the main PLL or PLLSAI1
               through PLLQ divider. You have to enable the peripheral clock and use
               HAL_RCCEx_PeriphCLKConfig() function to configure this clock.
          (+@) IWDG clock which is always the LSI clock.


        (+) The maximum frequency of the SYSCLK, HCLK, PCLK1 and PCLK2 is 80 MHz.
            The clock source frequency should be adapted depending on the device voltage range
            as listed in the Reference Manual "Clock source frequency versus voltage scaling" chapter.
     Table 1. HCLK clock frequency for STM32L4Rx/STM32L4Sx devices
     +--------------------------------------------------------+
     | Latency         |     HCLK clock frequency (MHz)       |
     |                 |--------------------------------------|
     |                 |  voltage range 1  | voltage range 2  |
     |                 |       1.2 V       |     1.0 V        |
     |-----------------|-------------------|------------------|
     |0WS(1 CPU cycles)|   0 < HCLK <= 20  |  0 < HCLK <= 8   |
     |-----------------|-------------------|------------------|
     |1WS(2 CPU cycles)|  20 < HCLK <= 40  |  8 < HCLK <= 16  |
     |-----------------|-------------------|------------------|
     |2WS(3 CPU cycles)|  40 < HCLK <= 60  | 16 < HCLK <= 26  |
     |-----------------|-------------------|------------------|
     |3WS(4 CPU cycles)|  60 < HCLK <= 80  | 16 < HCLK <= 26  |
     |-----------------|-------------------|------------------|
     |4WS(5 CPU cycles)|  80 < HCLK <= 100 | 16 < HCLK <= 26  |
     |-----------------|-------------------|------------------|
     |5WS(6 CPU cycles)| 100 < HCLK <= 120 | 16 < HCLK <= 26  |
     +--------------------------------------------------------+

     Table 2. HCLK clock frequency for other STM32L4 devices
     +-------------------------------------------------------+
     | Latency         |    HCLK clock frequency (MHz)       |
     |                 |-------------------------------------|
     |                 | voltage range 1  | voltage range 2  |
     |                 |      1.2 V       |     1.0 V        |
     |-----------------|------------------|------------------|
     |0WS(1 CPU cycles)|  0 < HCLK <= 16  |  0 < HCLK <= 6   |
     |-----------------|------------------|------------------|
     |1WS(2 CPU cycles)| 16 < HCLK <= 32  |  6 < HCLK <= 12  |
     |-----------------|------------------|------------------|
     |2WS(3 CPU cycles)| 32 < HCLK <= 48  | 12 < HCLK <= 18  |
     |-----------------|------------------|------------------|
     |3WS(4 CPU cycles)| 48 < HCLK <= 64  | 18 < HCLK <= 26  |
     |-----------------|------------------|------------------|
     |4WS(5 CPU cycles)| 64 < HCLK <= 80  | 18 < HCLK <= 26  |
     +-------------------------------------------------------+

Function Documentation

◆ HAL_RCC_ClockConfig()

HAL_StatusTypeDef HAL_RCC_ClockConfig ( RCC_ClkInitTypeDef RCC_ClkInitStruct,
uint32_t  FLatency 
)

Initialize the CPU, AHB and APB busses clocks according to the specified parameters in the RCC_ClkInitStruct.

Parameters
RCC_ClkInitStructpointer to an RCC_OscInitTypeDef structure that contains the configuration information for the RCC peripheral.
FLatencyFLASH Latency This parameter can be one of the following values:
  • FLASH_LATENCY_0 FLASH 0 Latency cycle
  • FLASH_LATENCY_1 FLASH 1 Latency cycle
  • FLASH_LATENCY_2 FLASH 2 Latency cycles
  • FLASH_LATENCY_3 FLASH 3 Latency cycles
  • FLASH_LATENCY_4 FLASH 4 Latency cycles
Note
The SystemCoreClock CMSIS variable is used to store System Clock Frequency and updated by HAL_RCC_GetHCLKFreq() function called within this function
The MSI is used by default as system clock source after startup from Reset, wake-up from STANDBY mode. After restart from Reset, the MSI frequency is set to its default value 4 MHz.
The HSI can be selected as system clock source after from STOP modes or in case of failure of the HSE used directly or indirectly as system clock (if the Clock Security System CSS is enabled).
A switch from one clock source to another occurs only if the target clock source is ready (clock stable after startup delay or PLL locked). If a clock source which is not yet ready is selected, the switch will occur when the clock source is ready.
You can use HAL_RCC_GetClockConfig() function to know which clock is currently used as system clock source.
Depending on the device voltage range, the software has to set correctly HPRE[3:0] bits to ensure that HCLK not exceed the maximum allowed frequency (for more details refer to section above "Initialization/de-initialization functions")
Return values
None

Definition at line 1055 of file stm32l4xx_hal_rcc.c.

1056 {
1057  uint32_t tickstart;
1058 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1059  uint32_t hpre = RCC_SYSCLK_DIV1;
1060 #endif
1061  HAL_StatusTypeDef status;
1062 
1063  /* Check Null pointer */
1064  if(RCC_ClkInitStruct == NULL)
1065  {
1066  return HAL_ERROR;
1067  }
1068 
1069  /* Check the parameters */
1070  assert_param(IS_RCC_CLOCKTYPE(RCC_ClkInitStruct->ClockType));
1071  assert_param(IS_FLASH_LATENCY(FLatency));
1072 
1073  /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
1074  must be correctly programmed according to the frequency of the CPU clock
1075  (HCLK) and the supply voltage of the device. */
1076 
1077  /* Increasing the number of wait states because of higher CPU frequency */
1078  if(FLatency > __HAL_FLASH_GET_LATENCY())
1079  {
1080  /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
1081  __HAL_FLASH_SET_LATENCY(FLatency);
1082 
1083  /* Check that the new number of wait states is taken into account to access the Flash
1084  memory by reading the FLASH_ACR register */
1085  if(__HAL_FLASH_GET_LATENCY() != FLatency)
1086  {
1087  return HAL_ERROR;
1088  }
1089  }
1090 
1091  /*------------------------- SYSCLK Configuration ---------------------------*/
1092  if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_SYSCLK) == RCC_CLOCKTYPE_SYSCLK)
1093  {
1094  assert_param(IS_RCC_SYSCLKSOURCE(RCC_ClkInitStruct->SYSCLKSource));
1095 
1096  /* PLL is selected as System Clock Source */
1097  if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
1098  {
1099  /* Check the PLL ready flag */
1100  if(READ_BIT(RCC->CR, RCC_CR_PLLRDY) == 0U)
1101  {
1102  return HAL_ERROR;
1103  }
1104 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1105  /* Undershoot management when selection PLL as SYSCLK source and frequency above 80Mhz */
1106  /* Compute target PLL output frequency */
1107  if(RCC_GetSysClockFreqFromPLLSource() > 80000000U)
1108  {
1109  if(READ_BIT(RCC->CFGR, RCC_CFGR_HPRE) == RCC_SYSCLK_DIV1)
1110  {
1111  /* Intermediate step with HCLK prescaler 2 necessary before to go over 80Mhz */
1112  MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_SYSCLK_DIV2);
1113  hpre = RCC_SYSCLK_DIV2;
1114  }
1115  else if((((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK) && (RCC_ClkInitStruct->AHBCLKDivider == RCC_SYSCLK_DIV1))
1116  {
1117  /* Intermediate step with HCLK prescaler 2 necessary before to go over 80Mhz */
1118  MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_SYSCLK_DIV2);
1119  hpre = RCC_SYSCLK_DIV2;
1120  }
1121  else
1122  {
1123  /* nothing to do */
1124  }
1125  }
1126 #endif
1127  }
1128  else
1129  {
1130  /* HSE is selected as System Clock Source */
1131  if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
1132  {
1133  /* Check the HSE ready flag */
1134  if(READ_BIT(RCC->CR, RCC_CR_HSERDY) == 0U)
1135  {
1136  return HAL_ERROR;
1137  }
1138  }
1139  /* MSI is selected as System Clock Source */
1140  else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_MSI)
1141  {
1142  /* Check the MSI ready flag */
1143  if(READ_BIT(RCC->CR, RCC_CR_MSIRDY) == 0U)
1144  {
1145  return HAL_ERROR;
1146  }
1147  }
1148  /* HSI is selected as System Clock Source */
1149  else
1150  {
1151  /* Check the HSI ready flag */
1152  if(READ_BIT(RCC->CR, RCC_CR_HSIRDY) == 0U)
1153  {
1154  return HAL_ERROR;
1155  }
1156  }
1157 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1158  /* Overshoot management when going down from PLL as SYSCLK source and frequency above 80Mhz */
1159  if(HAL_RCC_GetSysClockFreq() > 80000000U)
1160  {
1161  /* Intermediate step with HCLK prescaler 2 necessary before to go under 80Mhz */
1162  MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_SYSCLK_DIV2);
1163  hpre = RCC_SYSCLK_DIV2;
1164  }
1165 #endif
1166 
1167  }
1168 
1169  MODIFY_REG(RCC->CFGR, RCC_CFGR_SW, RCC_ClkInitStruct->SYSCLKSource);
1170 
1171  /* Get Start Tick*/
1172  tickstart = HAL_GetTick();
1173 
1174  while(__HAL_RCC_GET_SYSCLK_SOURCE() != (RCC_ClkInitStruct->SYSCLKSource << RCC_CFGR_SWS_Pos))
1175  {
1176  if((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
1177  {
1178  return HAL_TIMEOUT;
1179  }
1180  }
1181  }
1182 
1183  /*-------------------------- HCLK Configuration --------------------------*/
1184  if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)
1185  {
1186  assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
1187  MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
1188  }
1189 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1190  else
1191  {
1192  /* Is intermediate HCLK prescaler 2 applied internally, complete with HCLK prescaler 1 */
1193  if(hpre == RCC_SYSCLK_DIV2)
1194  {
1195  MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_SYSCLK_DIV1);
1196  }
1197  }
1198 #endif
1199 
1200  /* Decreasing the number of wait states because of lower CPU frequency */
1201  if(FLatency < __HAL_FLASH_GET_LATENCY())
1202  {
1203  /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
1204  __HAL_FLASH_SET_LATENCY(FLatency);
1205 
1206  /* Check that the new number of wait states is taken into account to access the Flash
1207  memory by reading the FLASH_ACR register */
1208  if(__HAL_FLASH_GET_LATENCY() != FLatency)
1209  {
1210  return HAL_ERROR;
1211  }
1212  }
1213 
1214  /*-------------------------- PCLK1 Configuration ---------------------------*/
1215  if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
1216  {
1217  assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB1CLKDivider));
1218  MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE1, RCC_ClkInitStruct->APB1CLKDivider);
1219  }
1220 
1221  /*-------------------------- PCLK2 Configuration ---------------------------*/
1222  if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK2) == RCC_CLOCKTYPE_PCLK2)
1223  {
1224  assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB2CLKDivider));
1225  MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE2, ((RCC_ClkInitStruct->APB2CLKDivider) << 3U));
1226  }
1227 
1228  /* Update the SystemCoreClock global variable */
1229  SystemCoreClock = HAL_RCC_GetSysClockFreq() >> (AHBPrescTable[READ_BIT(RCC->CFGR, RCC_CFGR_HPRE) >> RCC_CFGR_HPRE_Pos] & 0x1FU);
1230 
1231  /* Configure the source of time base considering new system clocks settings*/
1232  status = HAL_InitTick(uwTickPrio);
1233 
1234  return status;
1235 }
uint32_t HAL_GetTick(void)
Provide a tick value in millisecond.
HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
This function configures the source of the time base: The time source is configured to have 1ms time ...
static uint32_t RCC_GetSysClockFreqFromPLLSource(void)
Compute SYSCLK frequency based on PLL SYSCLK source.
uint32_t HAL_RCC_GetSysClockFreq(void)
Return the SYSCLK frequency.
MODIFY_REG(hrtc->Instance->CR, RTC_CR_WUCKSEL,(uint32_t) WakeUpClock)
uint32_t uwTickPrio
Definition: stm32l4xx_hal.c:91
assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock))

◆ HAL_RCC_DeInit()

HAL_StatusTypeDef HAL_RCC_DeInit ( void  )

Reset the RCC clock configuration to the default reset state.

Note
The default reset state of the clock configuration is given below:
  • MSI ON and used as system clock source
  • HSE, HSI, PLL, PLLSAI1 and PLLSAI2 OFF
  • AHB, APB1 and APB2 prescalers set to 1.
  • CSS, MCO1 OFF
  • All interrupts disabled
  • All interrupt and reset flags cleared
This function does not modify the configuration of the
  • Peripheral clock sources
  • LSI, LSE and RTC clocks (Backup domain)
Return values
HALstatus

Definition at line 264 of file stm32l4xx_hal_rcc.c.

265 {
266  uint32_t tickstart;
267 
268  /* Reset to default System clock */
269  /* Set MSION bit */
270  SET_BIT(RCC->CR, RCC_CR_MSION);
271 
272  /* Insure MSIRDY bit is set before writing default MSIRANGE value */
273  /* Get start tick */
274  tickstart = HAL_GetTick();
275 
276  /* Wait till MSI is ready */
277  while(READ_BIT(RCC->CR, RCC_CR_MSIRDY) == 0U)
278  {
279  if((HAL_GetTick() - tickstart) > MSI_TIMEOUT_VALUE)
280  {
281  return HAL_TIMEOUT;
282  }
283  }
284 
285  /* Set MSIRANGE default value */
286  MODIFY_REG(RCC->CR, RCC_CR_MSIRANGE, RCC_MSIRANGE_6);
287 
288  /* Reset CFGR register (MSI is selected as system clock source) */
289  CLEAR_REG(RCC->CFGR);
290 
291  /* Update the SystemCoreClock global variable for MSI as system clock source */
292  SystemCoreClock = MSI_VALUE;
293 
294  /* Configure the source of time base considering new system clock settings */
296  {
297  return HAL_ERROR;
298  }
299 
300  /* Insure MSI selected as system clock source */
301  /* Get start tick */
302  tickstart = HAL_GetTick();
303 
304  /* Wait till system clock source is ready */
305  while(READ_BIT(RCC->CFGR, RCC_CFGR_SWS) != RCC_CFGR_SWS_MSI)
306  {
307  if((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
308  {
309  return HAL_TIMEOUT;
310  }
311  }
312 
313  /* Reset HSION, HSIKERON, HSIASFS, HSEON, HSECSSON, PLLON, PLLSAIxON bits */
314 #if defined(RCC_PLLSAI2_SUPPORT)
315 
316  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);
317 
318 #elif defined(RCC_PLLSAI1_SUPPORT)
319 
320  CLEAR_BIT(RCC->CR, RCC_CR_HSEON | RCC_CR_HSION | RCC_CR_HSIKERON| RCC_CR_HSIASFS | RCC_CR_PLLON | RCC_CR_PLLSAI1ON);
321 
322 #else
323 
324  CLEAR_BIT(RCC->CR, RCC_CR_HSEON | RCC_CR_HSION | RCC_CR_HSIKERON| RCC_CR_HSIASFS | RCC_CR_PLLON);
325 
326 #endif /* RCC_PLLSAI2_SUPPORT */
327 
328  /* Insure PLLRDY, PLLSAI1RDY and PLLSAI2RDY (if present) are reset */
329  /* Get start tick */
330  tickstart = HAL_GetTick();
331 
332 #if defined(RCC_PLLSAI2_SUPPORT)
333 
334  while(READ_BIT(RCC->CR, RCC_CR_PLLRDY | RCC_CR_PLLSAI1RDY | RCC_CR_PLLSAI2RDY) != 0U)
335 
336 #elif defined(RCC_PLLSAI1_SUPPORT)
337 
338  while(READ_BIT(RCC->CR, RCC_CR_PLLRDY | RCC_CR_PLLSAI1RDY) != 0U)
339 
340 #else
341 
342  while(READ_BIT(RCC->CR, RCC_CR_PLLRDY) != 0U)
343 
344 #endif
345  {
346  if((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
347  {
348  return HAL_TIMEOUT;
349  }
350  }
351 
352  /* Reset PLLCFGR register */
353  CLEAR_REG(RCC->PLLCFGR);
354  SET_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN_4 );
355 
356 #if defined(RCC_PLLSAI1_SUPPORT)
357 
358  /* Reset PLLSAI1CFGR register */
359  CLEAR_REG(RCC->PLLSAI1CFGR);
360  SET_BIT(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLSAI1N_4 );
361 
362 #endif /* RCC_PLLSAI1_SUPPORT */
363 
364 #if defined(RCC_PLLSAI2_SUPPORT)
365 
366  /* Reset PLLSAI2CFGR register */
367  CLEAR_REG(RCC->PLLSAI2CFGR);
368  SET_BIT(RCC->PLLSAI2CFGR, RCC_PLLSAI2CFGR_PLLSAI2N_4 );
369 
370 #endif /* RCC_PLLSAI2_SUPPORT */
371 
372  /* Reset HSEBYP bit */
373  CLEAR_BIT(RCC->CR, RCC_CR_HSEBYP);
374 
375  /* Disable all interrupts */
376  CLEAR_REG(RCC->CIER);
377 
378  /* Clear all interrupt flags */
379  WRITE_REG(RCC->CICR, 0xFFFFFFFFU);
380 
381  /* Clear all reset flags */
382  SET_BIT(RCC->CSR, RCC_CSR_RMVF);
383 
384  return HAL_OK;
385 }
uint32_t HAL_GetTick(void)
Provide a tick value in millisecond.
CLEAR_BIT(hrtc->Instance->CR, RTC_CR_WUTE)
return HAL_OK
HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
This function configures the source of the time base: The time source is configured to have 1ms time ...
MODIFY_REG(hrtc->Instance->CR, RTC_CR_WUCKSEL,(uint32_t) WakeUpClock)
uint32_t uwTickPrio
Definition: stm32l4xx_hal.c:91

◆ HAL_RCC_OscConfig()

HAL_StatusTypeDef HAL_RCC_OscConfig ( RCC_OscInitTypeDef RCC_OscInitStruct)

Initialize the RCC Oscillators according to the specified parameters in the RCC_OscInitTypeDef.

Parameters
RCC_OscInitStructpointer to an RCC_OscInitTypeDef structure that contains the configuration information for the RCC Oscillators.
Note
The PLL is not disabled when used as system clock.
Transitions LSE Bypass to LSE On and LSE On to LSE Bypass are not supported by this macro. User should request a transition to LSE Off first and then LSE On or LSE Bypass.
Transition HSE Bypass to HSE On and HSE On to HSE Bypass are not supported by this macro. User should request a transition to HSE Off first and then HSE On or HSE Bypass.
Return values
HALstatus

Definition at line 401 of file stm32l4xx_hal_rcc.c.

402 {
403  uint32_t tickstart;
404  HAL_StatusTypeDef status;
405  uint32_t sysclk_source, pll_config;
406 
407  /* Check Null pointer */
408  if(RCC_OscInitStruct == NULL)
409  {
410  return HAL_ERROR;
411  }
412 
413  /* Check the parameters */
414  assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct->OscillatorType));
415 
416  sysclk_source = __HAL_RCC_GET_SYSCLK_SOURCE();
417  pll_config = __HAL_RCC_GET_PLL_OSCSOURCE();
418 
419  /*----------------------------- MSI Configuration --------------------------*/
420  if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_MSI) == RCC_OSCILLATORTYPE_MSI)
421  {
422  /* Check the parameters */
423  assert_param(IS_RCC_MSI(RCC_OscInitStruct->MSIState));
424  assert_param(IS_RCC_MSICALIBRATION_VALUE(RCC_OscInitStruct->MSICalibrationValue));
425  assert_param(IS_RCC_MSI_CLOCK_RANGE(RCC_OscInitStruct->MSIClockRange));
426 
427  /* Check if MSI is used as system clock or as PLL source when PLL is selected as system clock */
428  if((sysclk_source == RCC_CFGR_SWS_MSI) ||
429  ((sysclk_source == RCC_CFGR_SWS_PLL) && (pll_config == RCC_PLLSOURCE_MSI)))
430  {
431  if((READ_BIT(RCC->CR, RCC_CR_MSIRDY) != 0U) && (RCC_OscInitStruct->MSIState == RCC_MSI_OFF))
432  {
433  return HAL_ERROR;
434  }
435 
436  /* Otherwise, just the calibration and MSI range change are allowed */
437  else
438  {
439  /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
440  must be correctly programmed according to the frequency of the CPU clock
441  (HCLK) and the supply voltage of the device. */
442  if(RCC_OscInitStruct->MSIClockRange > __HAL_RCC_GET_MSI_RANGE())
443  {
444  /* First increase number of wait states update if necessary */
445  if(RCC_SetFlashLatencyFromMSIRange(RCC_OscInitStruct->MSIClockRange) != HAL_OK)
446  {
447  return HAL_ERROR;
448  }
449 
450  /* Selects the Multiple Speed oscillator (MSI) clock range .*/
451  __HAL_RCC_MSI_RANGE_CONFIG(RCC_OscInitStruct->MSIClockRange);
452  /* Adjusts the Multiple Speed oscillator (MSI) calibration value.*/
453  __HAL_RCC_MSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->MSICalibrationValue);
454  }
455  else
456  {
457  /* Else, keep current flash latency while decreasing applies */
458  /* Selects the Multiple Speed oscillator (MSI) clock range .*/
459  __HAL_RCC_MSI_RANGE_CONFIG(RCC_OscInitStruct->MSIClockRange);
460  /* Adjusts the Multiple Speed oscillator (MSI) calibration value.*/
461  __HAL_RCC_MSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->MSICalibrationValue);
462 
463  /* Decrease number of wait states update if necessary */
464  if(RCC_SetFlashLatencyFromMSIRange(RCC_OscInitStruct->MSIClockRange) != HAL_OK)
465  {
466  return HAL_ERROR;
467  }
468  }
469 
470  /* Update the SystemCoreClock global variable */
471  SystemCoreClock = HAL_RCC_GetSysClockFreq() >> (AHBPrescTable[READ_BIT(RCC->CFGR, RCC_CFGR_HPRE) >> RCC_CFGR_HPRE_Pos] & 0x1FU);
472 
473  /* Configure the source of time base considering new system clocks settings*/
474  status = HAL_InitTick(uwTickPrio);
475  if(status != HAL_OK)
476  {
477  return status;
478  }
479  }
480  }
481  else
482  {
483  /* Check the MSI State */
484  if(RCC_OscInitStruct->MSIState != RCC_MSI_OFF)
485  {
486  /* Enable the Internal High Speed oscillator (MSI). */
487  __HAL_RCC_MSI_ENABLE();
488 
489  /* Get timeout */
490  tickstart = HAL_GetTick();
491 
492  /* Wait till MSI is ready */
493  while(READ_BIT(RCC->CR, RCC_CR_MSIRDY) == 0U)
494  {
495  if((HAL_GetTick() - tickstart) > MSI_TIMEOUT_VALUE)
496  {
497  return HAL_TIMEOUT;
498  }
499  }
500  /* Selects the Multiple Speed oscillator (MSI) clock range .*/
501  __HAL_RCC_MSI_RANGE_CONFIG(RCC_OscInitStruct->MSIClockRange);
502  /* Adjusts the Multiple Speed oscillator (MSI) calibration value.*/
503  __HAL_RCC_MSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->MSICalibrationValue);
504 
505  }
506  else
507  {
508  /* Disable the Internal High Speed oscillator (MSI). */
509  __HAL_RCC_MSI_DISABLE();
510 
511  /* Get timeout */
512  tickstart = HAL_GetTick();
513 
514  /* Wait till MSI is ready */
515  while(READ_BIT(RCC->CR, RCC_CR_MSIRDY) != 0U)
516  {
517  if((HAL_GetTick() - tickstart) > MSI_TIMEOUT_VALUE)
518  {
519  return HAL_TIMEOUT;
520  }
521  }
522  }
523  }
524  }
525  /*------------------------------- HSE Configuration ------------------------*/
526  if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE)
527  {
528  /* Check the parameters */
529  assert_param(IS_RCC_HSE(RCC_OscInitStruct->HSEState));
530 
531  /* When the HSE is used as system clock or clock source for PLL in these cases it is not allowed to be disabled */
532  if((sysclk_source == RCC_CFGR_SWS_HSE) ||
533  ((sysclk_source == RCC_CFGR_SWS_PLL) && (pll_config == RCC_PLLSOURCE_HSE)))
534  {
535  if((READ_BIT(RCC->CR, RCC_CR_HSERDY) != 0U) && (RCC_OscInitStruct->HSEState == RCC_HSE_OFF))
536  {
537  return HAL_ERROR;
538  }
539  }
540  else
541  {
542  /* Set the new HSE configuration ---------------------------------------*/
543  __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct->HSEState);
544 
545  /* Check the HSE State */
546  if(RCC_OscInitStruct->HSEState != RCC_HSE_OFF)
547  {
548  /* Get Start Tick*/
549  tickstart = HAL_GetTick();
550 
551  /* Wait till HSE is ready */
552  while(READ_BIT(RCC->CR, RCC_CR_HSERDY) == 0U)
553  {
554  if((HAL_GetTick() - tickstart) > HSE_TIMEOUT_VALUE)
555  {
556  return HAL_TIMEOUT;
557  }
558  }
559  }
560  else
561  {
562  /* Get Start Tick*/
563  tickstart = HAL_GetTick();
564 
565  /* Wait till HSE is disabled */
566  while(READ_BIT(RCC->CR, RCC_CR_HSERDY) != 0U)
567  {
568  if((HAL_GetTick() - tickstart) > HSE_TIMEOUT_VALUE)
569  {
570  return HAL_TIMEOUT;
571  }
572  }
573  }
574  }
575  }
576  /*----------------------------- HSI Configuration --------------------------*/
577  if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI)
578  {
579  /* Check the parameters */
580  assert_param(IS_RCC_HSI(RCC_OscInitStruct->HSIState));
581  assert_param(IS_RCC_HSI_CALIBRATION_VALUE(RCC_OscInitStruct->HSICalibrationValue));
582 
583  /* Check if HSI is used as system clock or as PLL source when PLL is selected as system clock */
584  if((sysclk_source == RCC_CFGR_SWS_HSI) ||
585  ((sysclk_source == RCC_CFGR_SWS_PLL) && (pll_config == RCC_PLLSOURCE_HSI)))
586  {
587  /* When HSI is used as system clock it will not be disabled */
588  if((READ_BIT(RCC->CR, RCC_CR_HSIRDY) != 0U) && (RCC_OscInitStruct->HSIState == RCC_HSI_OFF))
589  {
590  return HAL_ERROR;
591  }
592  /* Otherwise, just the calibration is allowed */
593  else
594  {
595  /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
596  __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
597  }
598  }
599  else
600  {
601  /* Check the HSI State */
602  if(RCC_OscInitStruct->HSIState != RCC_HSI_OFF)
603  {
604  /* Enable the Internal High Speed oscillator (HSI). */
605  __HAL_RCC_HSI_ENABLE();
606 
607  /* Get Start Tick*/
608  tickstart = HAL_GetTick();
609 
610  /* Wait till HSI is ready */
611  while(READ_BIT(RCC->CR, RCC_CR_HSIRDY) == 0U)
612  {
613  if((HAL_GetTick() - tickstart) > HSI_TIMEOUT_VALUE)
614  {
615  return HAL_TIMEOUT;
616  }
617  }
618 
619  /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
620  __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
621  }
622  else
623  {
624  /* Disable the Internal High Speed oscillator (HSI). */
625  __HAL_RCC_HSI_DISABLE();
626 
627  /* Get Start Tick*/
628  tickstart = HAL_GetTick();
629 
630  /* Wait till HSI is disabled */
631  while(READ_BIT(RCC->CR, RCC_CR_HSIRDY) != 0U)
632  {
633  if((HAL_GetTick() - tickstart) > HSI_TIMEOUT_VALUE)
634  {
635  return HAL_TIMEOUT;
636  }
637  }
638  }
639  }
640  }
641  /*------------------------------ LSI Configuration -------------------------*/
642  if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI)
643  {
644  /* Check the parameters */
645  assert_param(IS_RCC_LSI(RCC_OscInitStruct->LSIState));
646 
647  /* Check the LSI State */
648  if(RCC_OscInitStruct->LSIState != RCC_LSI_OFF)
649  {
650 #if defined(RCC_CSR_LSIPREDIV)
651  uint32_t csr_temp = RCC->CSR;
652 
653  /* Check LSI division factor */
654  assert_param(IS_RCC_LSIDIV(RCC_OscInitStruct->LSIDiv));
655 
656  if (RCC_OscInitStruct->LSIDiv != (csr_temp & RCC_CSR_LSIPREDIV))
657  {
658  if (((csr_temp & RCC_CSR_LSIRDY) == RCC_CSR_LSIRDY) && \
659  ((csr_temp & RCC_CSR_LSION) != RCC_CSR_LSION))
660  {
661  /* If LSIRDY is set while LSION is not enabled,
662  LSIPREDIV can't be updated */
663  return HAL_ERROR;
664  }
665 
666  /* Turn off LSI before changing RCC_CSR_LSIPREDIV */
667  if ((csr_temp & RCC_CSR_LSION) == RCC_CSR_LSION)
668  {
669  __HAL_RCC_LSI_DISABLE();
670 
671  /* Get Start Tick*/
672  tickstart = HAL_GetTick();
673 
674  /* Wait till LSI is disabled */
675  while(READ_BIT(RCC->CSR, RCC_CSR_LSIRDY) != 0U)
676  {
677  if((HAL_GetTick() - tickstart) > LSI_TIMEOUT_VALUE)
678  {
679  return HAL_TIMEOUT;
680  }
681  }
682  }
683 
684  /* Set LSI division factor */
685  MODIFY_REG(RCC->CSR, RCC_CSR_LSIPREDIV, RCC_OscInitStruct->LSIDiv);
686  }
687 #endif /* RCC_CSR_LSIPREDIV */
688 
689  /* Enable the Internal Low Speed oscillator (LSI). */
690  __HAL_RCC_LSI_ENABLE();
691 
692  /* Get Start Tick*/
693  tickstart = HAL_GetTick();
694 
695  /* Wait till LSI is ready */
696  while(READ_BIT(RCC->CSR, RCC_CSR_LSIRDY) == 0U)
697  {
698  if((HAL_GetTick() - tickstart) > LSI_TIMEOUT_VALUE)
699  {
700  return HAL_TIMEOUT;
701  }
702  }
703  }
704  else
705  {
706  /* Disable the Internal Low Speed oscillator (LSI). */
707  __HAL_RCC_LSI_DISABLE();
708 
709  /* Get Start Tick*/
710  tickstart = HAL_GetTick();
711 
712  /* Wait till LSI is disabled */
713  while(READ_BIT(RCC->CSR, RCC_CSR_LSIRDY) != 0U)
714  {
715  if((HAL_GetTick() - tickstart) > LSI_TIMEOUT_VALUE)
716  {
717  return HAL_TIMEOUT;
718  }
719  }
720  }
721  }
722  /*------------------------------ LSE Configuration -------------------------*/
723  if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE)
724  {
725  FlagStatus pwrclkchanged = RESET;
726 
727  /* Check the parameters */
728  assert_param(IS_RCC_LSE(RCC_OscInitStruct->LSEState));
729 
730  /* Update LSE configuration in Backup Domain control register */
731  /* Requires to enable write access to Backup Domain of necessary */
732  if(HAL_IS_BIT_CLR(RCC->APB1ENR1, RCC_APB1ENR1_PWREN))
733  {
734  __HAL_RCC_PWR_CLK_ENABLE();
735  pwrclkchanged = SET;
736  }
737 
738  if(HAL_IS_BIT_CLR(PWR->CR1, PWR_CR1_DBP))
739  {
740  /* Enable write access to Backup domain */
741  SET_BIT(PWR->CR1, PWR_CR1_DBP);
742 
743  /* Wait for Backup domain Write protection disable */
744  tickstart = HAL_GetTick();
745 
746  while(HAL_IS_BIT_CLR(PWR->CR1, PWR_CR1_DBP))
747  {
748  if((HAL_GetTick() - tickstart) > RCC_DBP_TIMEOUT_VALUE)
749  {
750  return HAL_TIMEOUT;
751  }
752  }
753  }
754 
755  /* Set the new LSE configuration -----------------------------------------*/
756 #if defined(RCC_BDCR_LSESYSDIS)
757  if((RCC_OscInitStruct->LSEState & RCC_BDCR_LSEON) != 0U)
758  {
759  /* Set LSESYSDIS bit according to LSE propagation option (enabled or disabled) */
760  MODIFY_REG(RCC->BDCR, RCC_BDCR_LSESYSDIS, (RCC_OscInitStruct->LSEState & RCC_BDCR_LSESYSDIS));
761 
762  if((RCC_OscInitStruct->LSEState & RCC_BDCR_LSEBYP) != 0U)
763  {
764  /* LSE oscillator bypass enable */
765  SET_BIT(RCC->BDCR, RCC_BDCR_LSEBYP);
766  SET_BIT(RCC->BDCR, RCC_BDCR_LSEON);
767  }
768  else
769  {
770  /* LSE oscillator enable */
771  SET_BIT(RCC->BDCR, RCC_BDCR_LSEON);
772  }
773  }
774  else
775  {
776  CLEAR_BIT(RCC->BDCR, RCC_BDCR_LSEON);
777  CLEAR_BIT(RCC->BDCR, RCC_BDCR_LSEBYP);
778  }
779 #else
780  __HAL_RCC_LSE_CONFIG(RCC_OscInitStruct->LSEState);
781 #endif /* RCC_BDCR_LSESYSDIS */
782 
783  /* Check the LSE State */
784  if(RCC_OscInitStruct->LSEState != RCC_LSE_OFF)
785  {
786  /* Get Start Tick*/
787  tickstart = HAL_GetTick();
788 
789  /* Wait till LSE is ready */
790  while(READ_BIT(RCC->BDCR, RCC_BDCR_LSERDY) == 0U)
791  {
792  if((HAL_GetTick() - tickstart) > RCC_LSE_TIMEOUT_VALUE)
793  {
794  return HAL_TIMEOUT;
795  }
796  }
797  }
798  else
799  {
800  /* Get Start Tick*/
801  tickstart = HAL_GetTick();
802 
803  /* Wait till LSE is disabled */
804  while(READ_BIT(RCC->BDCR, RCC_BDCR_LSERDY) != 0U)
805  {
806  if((HAL_GetTick() - tickstart) > RCC_LSE_TIMEOUT_VALUE)
807  {
808  return HAL_TIMEOUT;
809  }
810  }
811 
812 #if defined(RCC_BDCR_LSESYSDIS)
813  /* By default, stop disabling LSE propagation */
814  CLEAR_BIT(RCC->BDCR, RCC_BDCR_LSESYSDIS);
815 #endif /* RCC_BDCR_LSESYSDIS */
816  }
817 
818  /* Restore clock configuration if changed */
819  if(pwrclkchanged == SET)
820  {
821  __HAL_RCC_PWR_CLK_DISABLE();
822  }
823  }
824 #if defined(RCC_HSI48_SUPPORT)
825  /*------------------------------ HSI48 Configuration -----------------------*/
826  if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI48) == RCC_OSCILLATORTYPE_HSI48)
827  {
828  /* Check the parameters */
829  assert_param(IS_RCC_HSI48(RCC_OscInitStruct->HSI48State));
830 
831  /* Check the LSI State */
832  if(RCC_OscInitStruct->HSI48State != RCC_HSI48_OFF)
833  {
834  /* Enable the Internal Low Speed oscillator (HSI48). */
835  __HAL_RCC_HSI48_ENABLE();
836 
837  /* Get Start Tick*/
838  tickstart = HAL_GetTick();
839 
840  /* Wait till HSI48 is ready */
841  while(READ_BIT(RCC->CRRCR, RCC_CRRCR_HSI48RDY) == 0U)
842  {
843  if((HAL_GetTick() - tickstart) > HSI48_TIMEOUT_VALUE)
844  {
845  return HAL_TIMEOUT;
846  }
847  }
848  }
849  else
850  {
851  /* Disable the Internal Low Speed oscillator (HSI48). */
852  __HAL_RCC_HSI48_DISABLE();
853 
854  /* Get Start Tick*/
855  tickstart = HAL_GetTick();
856 
857  /* Wait till HSI48 is disabled */
858  while(READ_BIT(RCC->CRRCR, RCC_CRRCR_HSI48RDY) != 0U)
859  {
860  if((HAL_GetTick() - tickstart) > HSI48_TIMEOUT_VALUE)
861  {
862  return HAL_TIMEOUT;
863  }
864  }
865  }
866  }
867 #endif /* RCC_HSI48_SUPPORT */
868  /*-------------------------------- PLL Configuration -----------------------*/
869  /* Check the parameters */
870  assert_param(IS_RCC_PLL(RCC_OscInitStruct->PLL.PLLState));
871 
872  if(RCC_OscInitStruct->PLL.PLLState != RCC_PLL_NONE)
873  {
874  /* Check if the PLL is used as system clock or not */
875  if(sysclk_source != RCC_CFGR_SWS_PLL)
876  {
877  if(RCC_OscInitStruct->PLL.PLLState == RCC_PLL_ON)
878  {
879  /* Check the parameters */
880  assert_param(IS_RCC_PLLSOURCE(RCC_OscInitStruct->PLL.PLLSource));
881  assert_param(IS_RCC_PLLM_VALUE(RCC_OscInitStruct->PLL.PLLM));
882  assert_param(IS_RCC_PLLN_VALUE(RCC_OscInitStruct->PLL.PLLN));
883 #if defined(RCC_PLLP_SUPPORT)
884  assert_param(IS_RCC_PLLP_VALUE(RCC_OscInitStruct->PLL.PLLP));
885 #endif /* RCC_PLLP_SUPPORT */
886  assert_param(IS_RCC_PLLQ_VALUE(RCC_OscInitStruct->PLL.PLLQ));
887  assert_param(IS_RCC_PLLR_VALUE(RCC_OscInitStruct->PLL.PLLR));
888 
889  /* Disable the main PLL. */
890  __HAL_RCC_PLL_DISABLE();
891 
892  /* Get Start Tick*/
893  tickstart = HAL_GetTick();
894 
895  /* Wait till PLL is ready */
896  while(READ_BIT(RCC->CR, RCC_CR_PLLRDY) != 0U)
897  {
898  if((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
899  {
900  return HAL_TIMEOUT;
901  }
902  }
903 
904  /* Configure the main PLL clock source, multiplication and division factors. */
905  __HAL_RCC_PLL_CONFIG(RCC_OscInitStruct->PLL.PLLSource,
906  RCC_OscInitStruct->PLL.PLLM,
907  RCC_OscInitStruct->PLL.PLLN,
908 #if defined(RCC_PLLP_SUPPORT)
909  RCC_OscInitStruct->PLL.PLLP,
910 #endif
911  RCC_OscInitStruct->PLL.PLLQ,
912  RCC_OscInitStruct->PLL.PLLR);
913 
914  /* Enable the main PLL. */
915  __HAL_RCC_PLL_ENABLE();
916 
917  /* Enable PLL System Clock output. */
918  __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL_SYSCLK);
919 
920  /* Get Start Tick*/
921  tickstart = HAL_GetTick();
922 
923  /* Wait till PLL is ready */
924  while(READ_BIT(RCC->CR, RCC_CR_PLLRDY) == 0U)
925  {
926  if((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
927  {
928  return HAL_TIMEOUT;
929  }
930  }
931  }
932  else
933  {
934  /* Disable the main PLL. */
935  __HAL_RCC_PLL_DISABLE();
936 
937  /* Disable all PLL outputs to save power if no PLLs on */
938 #if defined(RCC_PLLSAI1_SUPPORT) && defined(RCC_CR_PLLSAI2RDY)
939  if(READ_BIT(RCC->CR, (RCC_CR_PLLSAI1RDY | RCC_CR_PLLSAI2RDY)) == 0U)
940  {
941  MODIFY_REG(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC, RCC_PLLSOURCE_NONE);
942  }
943 #elif defined(RCC_PLLSAI1_SUPPORT)
944  if(READ_BIT(RCC->CR, RCC_CR_PLLSAI1RDY) == 0U)
945  {
946  MODIFY_REG(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC, RCC_PLLSOURCE_NONE);
947  }
948 #else
949  MODIFY_REG(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC, RCC_PLLSOURCE_NONE);
950 #endif /* RCC_PLLSAI1_SUPPORT && RCC_CR_PLLSAI2RDY */
951 
952 #if defined(RCC_PLLSAI2_SUPPORT)
953  __HAL_RCC_PLLCLKOUT_DISABLE(RCC_PLL_SYSCLK | RCC_PLL_48M1CLK | RCC_PLL_SAI3CLK);
954 #elif defined(RCC_PLLSAI1_SUPPORT)
955  __HAL_RCC_PLLCLKOUT_DISABLE(RCC_PLL_SYSCLK | RCC_PLL_48M1CLK | RCC_PLL_SAI2CLK);
956 #else
957  __HAL_RCC_PLLCLKOUT_DISABLE(RCC_PLL_SYSCLK | RCC_PLL_48M1CLK);
958 #endif /* RCC_PLLSAI2_SUPPORT */
959 
960  /* Get Start Tick*/
961  tickstart = HAL_GetTick();
962 
963  /* Wait till PLL is disabled */
964  while(READ_BIT(RCC->CR, RCC_CR_PLLRDY) != 0U)
965  {
966  if((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
967  {
968  return HAL_TIMEOUT;
969  }
970  }
971  }
972  }
973  else
974  {
975  /* Check if there is a request to disable the PLL used as System clock source */
976  if((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_OFF)
977  {
978  return HAL_ERROR;
979  }
980  else
981  {
982  pll_config = RCC->PLLCFGR;
983  /* Do not return HAL_ERROR if request repeats the current configuration */
984  if((READ_BIT(pll_config, RCC_PLLCFGR_PLLSRC) != RCC_OscInitStruct->PLL.PLLSource) ||
985  (READ_BIT(pll_config, RCC_PLLCFGR_PLLM) != ((RCC_OscInitStruct->PLL.PLLM - 1U) << RCC_PLLCFGR_PLLM_Pos)) ||
986  (READ_BIT(pll_config, RCC_PLLCFGR_PLLN) != (RCC_OscInitStruct->PLL.PLLN << RCC_PLLCFGR_PLLN_Pos)) ||
987 #if defined(RCC_PLLP_SUPPORT)
988 #if defined(RCC_PLLP_DIV_2_31_SUPPORT)
989  (READ_BIT(pll_config, RCC_PLLCFGR_PLLPDIV) != (RCC_OscInitStruct->PLL.PLLP << RCC_PLLCFGR_PLLPDIV_Pos)) ||
990 #else
991  (READ_BIT(pll_config, RCC_PLLCFGR_PLLP) != ((RCC_OscInitStruct->PLL.PLLP == RCC_PLLP_DIV7) ? 0U : 1U)) ||
992 #endif
993 #endif
994  (READ_BIT(pll_config, RCC_PLLCFGR_PLLQ) != ((((RCC_OscInitStruct->PLL.PLLQ) >> 1U) - 1U) << RCC_PLLCFGR_PLLQ_Pos)) ||
995  (READ_BIT(pll_config, RCC_PLLCFGR_PLLR) != ((((RCC_OscInitStruct->PLL.PLLR) >> 1U) - 1U) << RCC_PLLCFGR_PLLR_Pos)))
996  {
997  return HAL_ERROR;
998  }
999  }
1000  }
1001  }
1002  return HAL_OK;
1003 }
uint32_t HAL_GetTick(void)
Provide a tick value in millisecond.
CLEAR_BIT(hrtc->Instance->CR, RTC_CR_WUTE)
RCC_PLLInitTypeDef PLL
return HAL_OK
static HAL_StatusTypeDef RCC_SetFlashLatencyFromMSIRange(uint32_t msirange)
Update number of Flash wait states in line with MSI range and current voltage range.
HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
This function configures the source of the time base: The time source is configured to have 1ms time ...
ADC handle Structure definition.
uint32_t HAL_RCC_GetSysClockFreq(void)
Return the SYSCLK frequency.
MODIFY_REG(hrtc->Instance->CR, RTC_CR_WUCKSEL,(uint32_t) WakeUpClock)
uint32_t uwTickPrio
Definition: stm32l4xx_hal.c:91
assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock))