STM32F4xx_HAL_Driver  1.8.3
stm32f4xx_hal_rcc.c
Go to the documentation of this file.
1 
66 /* Includes ------------------------------------------------------------------*/
67 #include "stm32f4xx_hal.h"
68 
78 #ifdef HAL_RCC_MODULE_ENABLED
79 
80 /* Private typedef -----------------------------------------------------------*/
81 /* Private define ------------------------------------------------------------*/
86 /* Private macro -------------------------------------------------------------*/
87 #define __MCO1_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
88 #define MCO1_GPIO_PORT GPIOA
89 #define MCO1_PIN GPIO_PIN_8
90 
91 #define __MCO2_CLK_ENABLE() __HAL_RCC_GPIOC_CLK_ENABLE()
92 #define MCO2_GPIO_PORT GPIOC
93 #define MCO2_PIN GPIO_PIN_9
98 /* Private variables ---------------------------------------------------------*/
105 /* Private function prototypes -----------------------------------------------*/
106 /* Private functions ---------------------------------------------------------*/
107 
200 __weak HAL_StatusTypeDef HAL_RCC_DeInit(void)
201 {
202  return HAL_OK;
203 }
204 
219 __weak HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
220 {
221  uint32_t tickstart;
222  uint32_t pll_config;
223  /* Check Null pointer */
224  if (RCC_OscInitStruct == NULL)
225  {
226  return HAL_ERROR;
227  }
228 
229  /* Check the parameters */
230  assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct->OscillatorType));
231  /*------------------------------- HSE Configuration ------------------------*/
232  if (((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE)
233  {
234  /* Check the parameters */
235  assert_param(IS_RCC_HSE(RCC_OscInitStruct->HSEState));
236  /* When the HSE is used as system clock or clock source for PLL in these cases HSE will not disabled */
237  if ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSE) || \
238  ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL) && ((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLCFGR_PLLSRC_HSE)))
239  {
240  if ((__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET) && (RCC_OscInitStruct->HSEState == RCC_HSE_OFF))
241  {
242  return HAL_ERROR;
243  }
244  }
245  else
246  {
247  /* Set the new HSE configuration ---------------------------------------*/
248  __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct->HSEState);
249 
250  /* Check the HSE State */
251  if ((RCC_OscInitStruct->HSEState) != RCC_HSE_OFF)
252  {
253  /* Get Start Tick */
254  tickstart = HAL_GetTick();
255 
256  /* Wait till HSE is ready */
257  while (__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)
258  {
259  if ((HAL_GetTick() - tickstart) > HSE_TIMEOUT_VALUE)
260  {
261  return HAL_TIMEOUT;
262  }
263  }
264  }
265  else
266  {
267  /* Get Start Tick */
268  tickstart = HAL_GetTick();
269 
270  /* Wait till HSE is bypassed or disabled */
271  while (__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET)
272  {
273  if ((HAL_GetTick() - tickstart) > HSE_TIMEOUT_VALUE)
274  {
275  return HAL_TIMEOUT;
276  }
277  }
278  }
279  }
280  }
281  /*----------------------------- HSI Configuration --------------------------*/
282  if (((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI)
283  {
284  /* Check the parameters */
285  assert_param(IS_RCC_HSI(RCC_OscInitStruct->HSIState));
286  assert_param(IS_RCC_CALIBRATION_VALUE(RCC_OscInitStruct->HSICalibrationValue));
287 
288  /* Check if HSI is used as system clock or as PLL source when PLL is selected as system clock */
289  if ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSI) || \
290  ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL) && ((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLCFGR_PLLSRC_HSI)))
291  {
292  /* When HSI is used as system clock it will not disabled */
293  if ((__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET) && (RCC_OscInitStruct->HSIState != RCC_HSI_ON))
294  {
295  return HAL_ERROR;
296  }
297  /* Otherwise, just the calibration is allowed */
298  else
299  {
300  /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
301  __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
302  }
303  }
304  else
305  {
306  /* Check the HSI State */
307  if ((RCC_OscInitStruct->HSIState) != RCC_HSI_OFF)
308  {
309  /* Enable the Internal High Speed oscillator (HSI). */
310  __HAL_RCC_HSI_ENABLE();
311 
312  /* Get Start Tick*/
313  tickstart = HAL_GetTick();
314 
315  /* Wait till HSI is ready */
316  while (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET)
317  {
318  if ((HAL_GetTick() - tickstart) > HSI_TIMEOUT_VALUE)
319  {
320  return HAL_TIMEOUT;
321  }
322  }
323 
324  /* Adjusts the Internal High Speed oscillator (HSI) calibration value. */
325  __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
326  }
327  else
328  {
329  /* Disable the Internal High Speed oscillator (HSI). */
330  __HAL_RCC_HSI_DISABLE();
331 
332  /* Get Start Tick*/
333  tickstart = HAL_GetTick();
334 
335  /* Wait till HSI is ready */
336  while (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET)
337  {
338  if ((HAL_GetTick() - tickstart) > HSI_TIMEOUT_VALUE)
339  {
340  return HAL_TIMEOUT;
341  }
342  }
343  }
344  }
345  }
346  /*------------------------------ LSI Configuration -------------------------*/
347  if (((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI)
348  {
349  /* Check the parameters */
350  assert_param(IS_RCC_LSI(RCC_OscInitStruct->LSIState));
351 
352  /* Check the LSI State */
353  if ((RCC_OscInitStruct->LSIState) != RCC_LSI_OFF)
354  {
355  /* Enable the Internal Low Speed oscillator (LSI). */
356  __HAL_RCC_LSI_ENABLE();
357 
358  /* Get Start Tick*/
359  tickstart = HAL_GetTick();
360 
361  /* Wait till LSI is ready */
362  while (__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) == RESET)
363  {
364  if ((HAL_GetTick() - tickstart) > LSI_TIMEOUT_VALUE)
365  {
366  return HAL_TIMEOUT;
367  }
368  }
369  }
370  else
371  {
372  /* Disable the Internal Low Speed oscillator (LSI). */
373  __HAL_RCC_LSI_DISABLE();
374 
375  /* Get Start Tick */
376  tickstart = HAL_GetTick();
377 
378  /* Wait till LSI is ready */
379  while (__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) != RESET)
380  {
381  if ((HAL_GetTick() - tickstart) > LSI_TIMEOUT_VALUE)
382  {
383  return HAL_TIMEOUT;
384  }
385  }
386  }
387  }
388  /*------------------------------ LSE Configuration -------------------------*/
389  if (((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE)
390  {
391  FlagStatus pwrclkchanged = RESET;
392 
393  /* Check the parameters */
394  assert_param(IS_RCC_LSE(RCC_OscInitStruct->LSEState));
395 
396  /* Update LSE configuration in Backup Domain control register */
397  /* Requires to enable write access to Backup Domain of necessary */
398  if (__HAL_RCC_PWR_IS_CLK_DISABLED())
399  {
400  __HAL_RCC_PWR_CLK_ENABLE();
401  pwrclkchanged = SET;
402  }
403 
404  if (HAL_IS_BIT_CLR(PWR->CR, PWR_CR_DBP))
405  {
406  /* Enable write access to Backup domain */
407  SET_BIT(PWR->CR, PWR_CR_DBP);
408 
409  /* Wait for Backup domain Write protection disable */
410  tickstart = HAL_GetTick();
411 
412  while (HAL_IS_BIT_CLR(PWR->CR, PWR_CR_DBP))
413  {
414  if ((HAL_GetTick() - tickstart) > RCC_DBP_TIMEOUT_VALUE)
415  {
416  return HAL_TIMEOUT;
417  }
418  }
419  }
420 
421  /* Set the new LSE configuration -----------------------------------------*/
422  __HAL_RCC_LSE_CONFIG(RCC_OscInitStruct->LSEState);
423  /* Check the LSE State */
424  if ((RCC_OscInitStruct->LSEState) != RCC_LSE_OFF)
425  {
426  /* Get Start Tick*/
427  tickstart = HAL_GetTick();
428 
429  /* Wait till LSE is ready */
430  while (__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
431  {
432  if ((HAL_GetTick() - tickstart) > RCC_LSE_TIMEOUT_VALUE)
433  {
434  return HAL_TIMEOUT;
435  }
436  }
437  }
438  else
439  {
440  /* Get Start Tick */
441  tickstart = HAL_GetTick();
442 
443  /* Wait till LSE is ready */
444  while (__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) != RESET)
445  {
446  if ((HAL_GetTick() - tickstart) > RCC_LSE_TIMEOUT_VALUE)
447  {
448  return HAL_TIMEOUT;
449  }
450  }
451  }
452 
453  /* Restore clock configuration if changed */
454  if (pwrclkchanged == SET)
455  {
456  __HAL_RCC_PWR_CLK_DISABLE();
457  }
458  }
459  /*-------------------------------- PLL Configuration -----------------------*/
460  /* Check the parameters */
461  assert_param(IS_RCC_PLL(RCC_OscInitStruct->PLL.PLLState));
462  if ((RCC_OscInitStruct->PLL.PLLState) != RCC_PLL_NONE)
463  {
464  /* Check if the PLL is used as system clock or not */
465  if (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_PLL)
466  {
467  if ((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_ON)
468  {
469  /* Check the parameters */
470  assert_param(IS_RCC_PLLSOURCE(RCC_OscInitStruct->PLL.PLLSource));
471  assert_param(IS_RCC_PLLM_VALUE(RCC_OscInitStruct->PLL.PLLM));
472  assert_param(IS_RCC_PLLN_VALUE(RCC_OscInitStruct->PLL.PLLN));
473  assert_param(IS_RCC_PLLP_VALUE(RCC_OscInitStruct->PLL.PLLP));
474  assert_param(IS_RCC_PLLQ_VALUE(RCC_OscInitStruct->PLL.PLLQ));
475 
476  /* Disable the main PLL. */
477  __HAL_RCC_PLL_DISABLE();
478 
479  /* Get Start Tick */
480  tickstart = HAL_GetTick();
481 
482  /* Wait till PLL is disabled */
483  while (__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
484  {
485  if ((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
486  {
487  return HAL_TIMEOUT;
488  }
489  }
490 
491  /* Configure the main PLL clock source, multiplication and division factors. */
492  WRITE_REG(RCC->PLLCFGR, (RCC_OscInitStruct->PLL.PLLSource | \
493  RCC_OscInitStruct->PLL.PLLM | \
494  (RCC_OscInitStruct->PLL.PLLN << RCC_PLLCFGR_PLLN_Pos) | \
495  (((RCC_OscInitStruct->PLL.PLLP >> 1U) - 1U) << RCC_PLLCFGR_PLLP_Pos) | \
496  (RCC_OscInitStruct->PLL.PLLQ << RCC_PLLCFGR_PLLQ_Pos)));
497  /* Enable the main PLL. */
498  __HAL_RCC_PLL_ENABLE();
499 
500  /* Get Start Tick */
501  tickstart = HAL_GetTick();
502 
503  /* Wait till PLL is ready */
504  while (__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
505  {
506  if ((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
507  {
508  return HAL_TIMEOUT;
509  }
510  }
511  }
512  else
513  {
514  /* Disable the main PLL. */
515  __HAL_RCC_PLL_DISABLE();
516 
517  /* Get Start Tick */
518  tickstart = HAL_GetTick();
519 
520  /* Wait till PLL is disabled */
521  while (__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
522  {
523  if ((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
524  {
525  return HAL_TIMEOUT;
526  }
527  }
528  }
529  }
530  else
531  {
532  /* Check if there is a request to disable the PLL used as System clock source */
533  if ((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_OFF)
534  {
535  return HAL_ERROR;
536  }
537  else
538  {
539  /* Do not return HAL_ERROR if request repeats the current configuration */
540  pll_config = RCC->PLLCFGR;
541 #if defined (RCC_PLLCFGR_PLLR)
542  if (((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_OFF) ||
543  (READ_BIT(pll_config, RCC_PLLCFGR_PLLSRC) != RCC_OscInitStruct->PLL.PLLSource) ||
544  (READ_BIT(pll_config, RCC_PLLCFGR_PLLM) != (RCC_OscInitStruct->PLL.PLLM) << RCC_PLLCFGR_PLLM_Pos) ||
545  (READ_BIT(pll_config, RCC_PLLCFGR_PLLN) != (RCC_OscInitStruct->PLL.PLLN) << RCC_PLLCFGR_PLLN_Pos) ||
546  (READ_BIT(pll_config, RCC_PLLCFGR_PLLP) != (((RCC_OscInitStruct->PLL.PLLP >> 1U) - 1U)) << RCC_PLLCFGR_PLLP_Pos) ||
547  (READ_BIT(pll_config, RCC_PLLCFGR_PLLQ) != (RCC_OscInitStruct->PLL.PLLQ << RCC_PLLCFGR_PLLQ_Pos)) ||
548  (READ_BIT(pll_config, RCC_PLLCFGR_PLLR) != (RCC_OscInitStruct->PLL.PLLR << RCC_PLLCFGR_PLLR_Pos)))
549 #else
550  if (((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_OFF) ||
551  (READ_BIT(pll_config, RCC_PLLCFGR_PLLSRC) != RCC_OscInitStruct->PLL.PLLSource) ||
552  (READ_BIT(pll_config, RCC_PLLCFGR_PLLM) != (RCC_OscInitStruct->PLL.PLLM) << RCC_PLLCFGR_PLLM_Pos) ||
553  (READ_BIT(pll_config, RCC_PLLCFGR_PLLN) != (RCC_OscInitStruct->PLL.PLLN) << RCC_PLLCFGR_PLLN_Pos) ||
554  (READ_BIT(pll_config, RCC_PLLCFGR_PLLP) != (((RCC_OscInitStruct->PLL.PLLP >> 1U) - 1U)) << RCC_PLLCFGR_PLLP_Pos) ||
555  (READ_BIT(pll_config, RCC_PLLCFGR_PLLQ) != (RCC_OscInitStruct->PLL.PLLQ << RCC_PLLCFGR_PLLQ_Pos)))
556 #endif /* RCC_PLLCFGR_PLLR */
557  {
558  return HAL_ERROR;
559  }
560  }
561  }
562  }
563  return HAL_OK;
564 }
565 
591 HAL_StatusTypeDef HAL_RCC_ClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t FLatency)
592 {
593  uint32_t tickstart;
594 
595  /* Check Null pointer */
596  if (RCC_ClkInitStruct == NULL)
597  {
598  return HAL_ERROR;
599  }
600 
601  /* Check the parameters */
602  assert_param(IS_RCC_CLOCKTYPE(RCC_ClkInitStruct->ClockType));
603  assert_param(IS_FLASH_LATENCY(FLatency));
604 
605  /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
606  must be correctly programmed according to the frequency of the CPU clock
607  (HCLK) and the supply voltage of the device. */
608 
609  /* Increasing the number of wait states because of higher CPU frequency */
610  if (FLatency > __HAL_FLASH_GET_LATENCY())
611  {
612  /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
613  __HAL_FLASH_SET_LATENCY(FLatency);
614 
615  /* Check that the new number of wait states is taken into account to access the Flash
616  memory by reading the FLASH_ACR register */
617  if (__HAL_FLASH_GET_LATENCY() != FLatency)
618  {
619  return HAL_ERROR;
620  }
621  }
622 
623  /*-------------------------- HCLK Configuration --------------------------*/
624  if (((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)
625  {
626  /* Set the highest APBx dividers in order to ensure that we do not go through
627  a non-spec phase whatever we decrease or increase HCLK. */
628  if (((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
629  {
630  MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE1, RCC_HCLK_DIV16);
631  }
632 
633  if (((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK2) == RCC_CLOCKTYPE_PCLK2)
634  {
635  MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE2, (RCC_HCLK_DIV16 << 3));
636  }
637 
638  assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
639  MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
640  }
641 
642  /*------------------------- SYSCLK Configuration ---------------------------*/
643  if (((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_SYSCLK) == RCC_CLOCKTYPE_SYSCLK)
644  {
645  assert_param(IS_RCC_SYSCLKSOURCE(RCC_ClkInitStruct->SYSCLKSource));
646 
647  /* HSE is selected as System Clock Source */
648  if (RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
649  {
650  /* Check the HSE ready flag */
651  if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)
652  {
653  return HAL_ERROR;
654  }
655  }
656  /* PLL is selected as System Clock Source */
657  else if ((RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK) ||
658  (RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLRCLK))
659  {
660  /* Check the PLL ready flag */
661  if (__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
662  {
663  return HAL_ERROR;
664  }
665  }
666  /* HSI is selected as System Clock Source */
667  else
668  {
669  /* Check the HSI ready flag */
670  if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET)
671  {
672  return HAL_ERROR;
673  }
674  }
675 
676  __HAL_RCC_SYSCLK_CONFIG(RCC_ClkInitStruct->SYSCLKSource);
677 
678  /* Get Start Tick */
679  tickstart = HAL_GetTick();
680 
681  while (__HAL_RCC_GET_SYSCLK_SOURCE() != (RCC_ClkInitStruct->SYSCLKSource << RCC_CFGR_SWS_Pos))
682  {
683  if ((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
684  {
685  return HAL_TIMEOUT;
686  }
687  }
688  }
689 
690  /* Decreasing the number of wait states because of lower CPU frequency */
691  if (FLatency < __HAL_FLASH_GET_LATENCY())
692  {
693  /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
694  __HAL_FLASH_SET_LATENCY(FLatency);
695 
696  /* Check that the new number of wait states is taken into account to access the Flash
697  memory by reading the FLASH_ACR register */
698  if (__HAL_FLASH_GET_LATENCY() != FLatency)
699  {
700  return HAL_ERROR;
701  }
702  }
703 
704  /*-------------------------- PCLK1 Configuration ---------------------------*/
705  if (((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
706  {
707  assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB1CLKDivider));
708  MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE1, RCC_ClkInitStruct->APB1CLKDivider);
709  }
710 
711  /*-------------------------- PCLK2 Configuration ---------------------------*/
712  if (((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK2) == RCC_CLOCKTYPE_PCLK2)
713  {
714  assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB2CLKDivider));
715  MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE2, ((RCC_ClkInitStruct->APB2CLKDivider) << 3U));
716  }
717 
718  /* Update the SystemCoreClock global variable */
719  SystemCoreClock = HAL_RCC_GetSysClockFreq() >> AHBPrescTable[(RCC->CFGR & RCC_CFGR_HPRE) >> RCC_CFGR_HPRE_Pos];
720 
721  /* Configure the source of time base considering new system clocks settings */
722  HAL_InitTick(uwTickPrio);
723 
724  return HAL_OK;
725 }
726 
775 void HAL_RCC_MCOConfig(uint32_t RCC_MCOx, uint32_t RCC_MCOSource, uint32_t RCC_MCODiv)
776 {
777  GPIO_InitTypeDef GPIO_InitStruct;
778  /* Check the parameters */
779  assert_param(IS_RCC_MCO(RCC_MCOx));
780  assert_param(IS_RCC_MCODIV(RCC_MCODiv));
781  /* RCC_MCO1 */
782  if (RCC_MCOx == RCC_MCO1)
783  {
784  assert_param(IS_RCC_MCO1SOURCE(RCC_MCOSource));
785 
786  /* MCO1 Clock Enable */
787  __MCO1_CLK_ENABLE();
788 
789  /* Configure the MCO1 pin in alternate function mode */
790  GPIO_InitStruct.Pin = MCO1_PIN;
791  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
792  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
793  GPIO_InitStruct.Pull = GPIO_NOPULL;
794  GPIO_InitStruct.Alternate = GPIO_AF0_MCO;
795  HAL_GPIO_Init(MCO1_GPIO_PORT, &GPIO_InitStruct);
796 
797  /* Mask MCO1 and MCO1PRE[2:0] bits then Select MCO1 clock source and prescaler */
798  MODIFY_REG(RCC->CFGR, (RCC_CFGR_MCO1 | RCC_CFGR_MCO1PRE), (RCC_MCOSource | RCC_MCODiv));
799 
800  /* This RCC MCO1 enable feature is available only on STM32F410xx devices */
801 #if defined(RCC_CFGR_MCO1EN)
802  __HAL_RCC_MCO1_ENABLE();
803 #endif /* RCC_CFGR_MCO1EN */
804  }
805 #if defined(RCC_CFGR_MCO2)
806  else
807  {
808  assert_param(IS_RCC_MCO2SOURCE(RCC_MCOSource));
809 
810  /* MCO2 Clock Enable */
811  __MCO2_CLK_ENABLE();
812 
813  /* Configure the MCO2 pin in alternate function mode */
814  GPIO_InitStruct.Pin = MCO2_PIN;
815  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
816  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
817  GPIO_InitStruct.Pull = GPIO_NOPULL;
818  GPIO_InitStruct.Alternate = GPIO_AF0_MCO;
819  HAL_GPIO_Init(MCO2_GPIO_PORT, &GPIO_InitStruct);
820 
821  /* Mask MCO2 and MCO2PRE[2:0] bits then Select MCO2 clock source and prescaler */
822  MODIFY_REG(RCC->CFGR, (RCC_CFGR_MCO2 | RCC_CFGR_MCO2PRE), (RCC_MCOSource | (RCC_MCODiv << 3U)));
823 
824  /* This RCC MCO2 enable feature is available only on STM32F410Rx devices */
825 #if defined(RCC_CFGR_MCO2EN)
826  __HAL_RCC_MCO2_ENABLE();
827 #endif /* RCC_CFGR_MCO2EN */
828  }
829 #endif /* RCC_CFGR_MCO2 */
830 }
831 
842 {
843  *(__IO uint32_t *) RCC_CR_CSSON_BB = (uint32_t)ENABLE;
844 }
845 
851 {
852  *(__IO uint32_t *) RCC_CR_CSSON_BB = (uint32_t)DISABLE;
853 }
854 
885 __weak uint32_t HAL_RCC_GetSysClockFreq(void)
886 {
887  uint32_t pllm = 0U;
888  uint32_t pllvco = 0U;
889  uint32_t pllp = 0U;
890  uint32_t sysclockfreq = 0U;
891 
892  /* Get SYSCLK source -------------------------------------------------------*/
893  switch (RCC->CFGR & RCC_CFGR_SWS)
894  {
895  case RCC_CFGR_SWS_HSI: /* HSI used as system clock source */
896  {
897  sysclockfreq = HSI_VALUE;
898  break;
899  }
900  case RCC_CFGR_SWS_HSE: /* HSE used as system clock source */
901  {
902  sysclockfreq = HSE_VALUE;
903  break;
904  }
905  case RCC_CFGR_SWS_PLL: /* PLL used as system clock source */
906  {
907  /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM) * PLLN
908  SYSCLK = PLL_VCO / PLLP */
909  pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
910  if (__HAL_RCC_GET_PLL_OSCSOURCE() != RCC_PLLSOURCE_HSI)
911  {
912  /* HSE used as PLL clock source */
913  pllvco = (uint32_t)((((uint64_t) HSE_VALUE * ((uint64_t)((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos)))) / (uint64_t)pllm);
914  }
915  else
916  {
917  /* HSI used as PLL clock source */
918  pllvco = (uint32_t)((((uint64_t) HSI_VALUE * ((uint64_t)((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos)))) / (uint64_t)pllm);
919  }
920  pllp = ((((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >> RCC_PLLCFGR_PLLP_Pos) + 1U) * 2U);
921 
922  sysclockfreq = pllvco / pllp;
923  break;
924  }
925  default:
926  {
927  sysclockfreq = HSI_VALUE;
928  break;
929  }
930  }
931  return sysclockfreq;
932 }
933 
943 uint32_t HAL_RCC_GetHCLKFreq(void)
944 {
945  return SystemCoreClock;
946 }
947 
954 uint32_t HAL_RCC_GetPCLK1Freq(void)
955 {
956  /* Get HCLK source and Compute PCLK1 frequency ---------------------------*/
957  return (HAL_RCC_GetHCLKFreq() >> APBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE1) >> RCC_CFGR_PPRE1_Pos]);
958 }
959 
966 uint32_t HAL_RCC_GetPCLK2Freq(void)
967 {
968  /* Get HCLK source and Compute PCLK2 frequency ---------------------------*/
969  return (HAL_RCC_GetHCLKFreq() >> APBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE2) >> RCC_CFGR_PPRE2_Pos]);
970 }
971 
979 __weak void HAL_RCC_GetOscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
980 {
981  /* Set all possible values for the Oscillator type parameter ---------------*/
982  RCC_OscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_LSI;
983 
984  /* Get the HSE configuration -----------------------------------------------*/
985  if ((RCC->CR & RCC_CR_HSEBYP) == RCC_CR_HSEBYP)
986  {
987  RCC_OscInitStruct->HSEState = RCC_HSE_BYPASS;
988  }
989  else if ((RCC->CR & RCC_CR_HSEON) == RCC_CR_HSEON)
990  {
991  RCC_OscInitStruct->HSEState = RCC_HSE_ON;
992  }
993  else
994  {
995  RCC_OscInitStruct->HSEState = RCC_HSE_OFF;
996  }
997 
998  /* Get the HSI configuration -----------------------------------------------*/
999  if ((RCC->CR & RCC_CR_HSION) == RCC_CR_HSION)
1000  {
1001  RCC_OscInitStruct->HSIState = RCC_HSI_ON;
1002  }
1003  else
1004  {
1005  RCC_OscInitStruct->HSIState = RCC_HSI_OFF;
1006  }
1007 
1008  RCC_OscInitStruct->HSICalibrationValue = (uint32_t)((RCC->CR & RCC_CR_HSITRIM) >> RCC_CR_HSITRIM_Pos);
1009 
1010  /* Get the LSE configuration -----------------------------------------------*/
1011  if ((RCC->BDCR & RCC_BDCR_LSEBYP) == RCC_BDCR_LSEBYP)
1012  {
1013  RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS;
1014  }
1015  else if ((RCC->BDCR & RCC_BDCR_LSEON) == RCC_BDCR_LSEON)
1016  {
1017  RCC_OscInitStruct->LSEState = RCC_LSE_ON;
1018  }
1019  else
1020  {
1021  RCC_OscInitStruct->LSEState = RCC_LSE_OFF;
1022  }
1023 
1024  /* Get the LSI configuration -----------------------------------------------*/
1025  if ((RCC->CSR & RCC_CSR_LSION) == RCC_CSR_LSION)
1026  {
1027  RCC_OscInitStruct->LSIState = RCC_LSI_ON;
1028  }
1029  else
1030  {
1031  RCC_OscInitStruct->LSIState = RCC_LSI_OFF;
1032  }
1033 
1034  /* Get the PLL configuration -----------------------------------------------*/
1035  if ((RCC->CR & RCC_CR_PLLON) == RCC_CR_PLLON)
1036  {
1037  RCC_OscInitStruct->PLL.PLLState = RCC_PLL_ON;
1038  }
1039  else
1040  {
1041  RCC_OscInitStruct->PLL.PLLState = RCC_PLL_OFF;
1042  }
1043  RCC_OscInitStruct->PLL.PLLSource = (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC);
1044  RCC_OscInitStruct->PLL.PLLM = (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM);
1045  RCC_OscInitStruct->PLL.PLLN = (uint32_t)((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos);
1046  RCC_OscInitStruct->PLL.PLLP = (uint32_t)((((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) + RCC_PLLCFGR_PLLP_0) << 1U) >> RCC_PLLCFGR_PLLP_Pos);
1047  RCC_OscInitStruct->PLL.PLLQ = (uint32_t)((RCC->PLLCFGR & RCC_PLLCFGR_PLLQ) >> RCC_PLLCFGR_PLLQ_Pos);
1048 }
1049 
1058 void HAL_RCC_GetClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t *pFLatency)
1059 {
1060  /* Set all possible values for the Clock type parameter --------------------*/
1061  RCC_ClkInitStruct->ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
1062 
1063  /* Get the SYSCLK configuration --------------------------------------------*/
1064  RCC_ClkInitStruct->SYSCLKSource = (uint32_t)(RCC->CFGR & RCC_CFGR_SW);
1065 
1066  /* Get the HCLK configuration ----------------------------------------------*/
1067  RCC_ClkInitStruct->AHBCLKDivider = (uint32_t)(RCC->CFGR & RCC_CFGR_HPRE);
1068 
1069  /* Get the APB1 configuration ----------------------------------------------*/
1070  RCC_ClkInitStruct->APB1CLKDivider = (uint32_t)(RCC->CFGR & RCC_CFGR_PPRE1);
1071 
1072  /* Get the APB2 configuration ----------------------------------------------*/
1073  RCC_ClkInitStruct->APB2CLKDivider = (uint32_t)((RCC->CFGR & RCC_CFGR_PPRE2) >> 3U);
1074 
1075  /* Get the Flash Wait State (Latency) configuration ------------------------*/
1076  *pFLatency = (uint32_t)(FLASH->ACR & FLASH_ACR_LATENCY);
1077 }
1078 
1085 {
1086  /* Check RCC CSSF flag */
1087  if (__HAL_RCC_GET_IT(RCC_IT_CSS))
1088  {
1089  /* RCC Clock Security System interrupt user callback */
1091 
1092  /* Clear RCC CSS pending bit */
1093  __HAL_RCC_CLEAR_IT(RCC_IT_CSS);
1094  }
1095 }
1096 
1101 __weak void HAL_RCC_CSSCallback(void)
1102 {
1103  /* NOTE : This function Should not be modified, when the callback is needed,
1104  the HAL_RCC_CSSCallback could be implemented in the user file
1105  */
1106 }
1107 
1116 #endif /* HAL_RCC_MODULE_ENABLED */
void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
Initializes the GPIOx peripheral according to the specified parameters in the GPIO_Init.
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 ...
uint32_t HAL_GetTick(void)
Provides a tick value in millisecond.
HAL_StatusTypeDef HAL_RCC_DeInit(void)
Resets the RCC clock configuration to the default reset state.
HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
Initializes the RCC Oscillators according to the specified parameters in the RCC_OscInitTypeDef.
HAL_StatusTypeDef HAL_RCC_ClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t FLatency)
Initializes the CPU, AHB and APB busses clocks according to the specified parameters in the RCC_ClkIn...
void HAL_RCC_NMI_IRQHandler(void)
This function handles the RCC CSS interrupt request.
uint32_t HAL_RCC_GetHCLKFreq(void)
Returns the HCLK frequency.
void HAL_RCC_GetOscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
Configures the RCC_OscInitStruct according to the internal RCC configuration registers.
void HAL_RCC_MCOConfig(uint32_t RCC_MCOx, uint32_t RCC_MCOSource, uint32_t RCC_MCODiv)
Selects the clock source to output on MCO1 pin(PA8) or on MCO2 pin(PC9).
void HAL_RCC_EnableCSS(void)
Enables the Clock Security System.
uint32_t HAL_RCC_GetSysClockFreq(void)
Returns the SYSCLK frequency.
uint32_t HAL_RCC_GetPCLK1Freq(void)
Returns the PCLK1 frequency.
uint32_t HAL_RCC_GetPCLK2Freq(void)
Returns the PCLK2 frequency.
void HAL_RCC_GetClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t *pFLatency)
Configures the RCC_ClkInitStruct according to the internal RCC configuration registers.
void HAL_RCC_DisableCSS(void)
Disables the Clock Security System.
void HAL_RCC_CSSCallback(void)
RCC Clock Security System interrupt callback.
RCC_PLLInitTypeDef PLL
RCC System, AHB and APB busses clock configuration structure definition.
RCC Internal/External Oscillator (HSE, HSI, LSE and LSI) configuration structure definition.
This file contains all the functions prototypes for the HAL module driver.