📄 stm32f2xx_rcc.c
字号:
* @brief Enables or disables the Internal High Speed oscillator (HSI).
* @note The HSI is stopped by hardware when entering STOP and STANDBY modes.
* It is used (enabled by hardware) as system clock source after startup
* from Reset, wakeup from STOP and STANDBY mode, or in case of failure
* of the HSE used directly or indirectly as system clock (if the Clock
* Security System CSS is enabled).
* @note HSI can not be stopped if it is used as system clock source. In this case,
* you have to select another source of the system clock then stop the HSI.
* @note After enabling the HSI, the application software should wait on HSIRDY
* flag to be set indicating that HSI clock is stable and can be used as
* system clock source.
* @param NewState: new state of the HSI.
* This parameter can be: ENABLE or DISABLE.
* @note When the HSI is stopped, HSIRDY flag goes low after 6 HSI oscillator
* clock cycles.
* @retval None
*/
void RCC_HSICmd(FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_FUNCTIONAL_STATE(NewState));
*(__IO uint32_t *) CR_HSION_BB = (uint32_t)NewState;
}
/**
* @brief Configures the External Low Speed oscillator (LSE).
* @note As the LSE is in the Backup domain and write access is denied to
* this domain after reset, you have to enable write access using
* PWR_BackupAccessCmd(ENABLE) function before to configure the LSE
* (to be done once after reset).
* @note After enabling the LSE (RCC_LSE_ON or RCC_LSE_Bypass), the application
* software should wait on LSERDY flag to be set indicating that LSE clock
* is stable and can be used to clock the RTC.
* @param RCC_LSE: specifies the new state of the LSE.
* This parameter can be one of the following values:
* @arg RCC_LSE_OFF: turn OFF the LSE oscillator, LSERDY flag goes low after
* 6 LSE oscillator clock cycles.
* @arg RCC_LSE_ON: turn ON the LSE oscillator
* @arg RCC_LSE_Bypass: LSE oscillator bypassed with external clock
* @retval None
*/
void RCC_LSEConfig(uint8_t RCC_LSE)
{
/* Check the parameters */
assert_param(IS_RCC_LSE(RCC_LSE));
/* Reset LSEON and LSEBYP bits before configuring the LSE ------------------*/
/* Reset LSEON bit */
*(__IO uint8_t *) BDCR_ADDRESS = RCC_LSE_OFF;
/* Reset LSEBYP bit */
*(__IO uint8_t *) BDCR_ADDRESS = RCC_LSE_OFF;
/* Configure LSE (RCC_LSE_OFF is already covered by the code section above) */
switch (RCC_LSE)
{
case RCC_LSE_ON:
/* Set LSEON bit */
*(__IO uint8_t *) BDCR_ADDRESS = RCC_LSE_ON;
break;
case RCC_LSE_Bypass:
/* Set LSEBYP and LSEON bits */
*(__IO uint8_t *) BDCR_ADDRESS = RCC_LSE_Bypass | RCC_LSE_ON;
break;
default:
break;
}
}
/**
* @brief Enables or disables the Internal Low Speed oscillator (LSI).
* @note After enabling the LSI, the application software should wait on
* LSIRDY flag to be set indicating that LSI clock is stable and can
* be used to clock the IWDG and/or the RTC.
* @note LSI can not be disabled if the IWDG is running.
* @param NewState: new state of the LSI.
* This parameter can be: ENABLE or DISABLE.
* @note When the LSI is stopped, LSIRDY flag goes low after 6 LSI oscillator
* clock cycles.
* @retval None
*/
void RCC_LSICmd(FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_FUNCTIONAL_STATE(NewState));
*(__IO uint32_t *) CSR_LSION_BB = (uint32_t)NewState;
}
/**
* @brief Configures the main PLL clock source, multiplication and division factors.
* @note This function must be used only when the main PLL is disabled.
*
* @param RCC_PLLSource: specifies the PLL entry clock source.
* This parameter can be one of the following values:
* @arg RCC_PLLSource_HSI: HSI oscillator clock selected as PLL clock entry
* @arg RCC_PLLSource_HSE: HSE oscillator clock selected as PLL clock entry
* @note This clock source (RCC_PLLSource) is common for the main PLL and PLLI2S.
*
* @param PLLM: specifies the division factor for PLL VCO input clock
* This parameter must be a number between 0 and 63.
* @note You have to set the PLLM parameter correctly to ensure that the VCO input
* frequency ranges from 1 to 2 MHz. It is recommended to select a frequency
* of 2 MHz to limit PLL jitter.
*
* @param PLLN: specifies the multiplication factor for PLL VCO output clock
* This parameter must be a number between 192 and 432.
* @note You have to set the PLLN parameter correctly to ensure that the VCO
* output frequency is between 192 and 432 MHz.
*
* @param PLLP: specifies the division factor for main system clock (SYSCLK)
* This parameter must be a number in the range {2, 4, 6, or 8}.
* @note You have to set the PLLP parameter correctly to not exceed 120 MHz on
* the System clock frequency.
*
* @param PLLQ: specifies the division factor for OTG FS, SDIO and RNG clocks
* This parameter must be a number between 4 and 15.
* @note If the USB OTG FS is used in your application, you have to set the
* PLLQ parameter correctly to have 48 MHz clock for the USB. However,
* the SDIO and RNG need a frequency lower than or equal to 48 MHz to work
* correctly.
*
* @retval None
*/
void RCC_PLLConfig(uint32_t RCC_PLLSource, uint32_t PLLM, uint32_t PLLN, uint32_t PLLP, uint32_t PLLQ)
{
/* Check the parameters */
assert_param(IS_RCC_PLL_SOURCE(RCC_PLLSource));
assert_param(IS_RCC_PLLM_VALUE(PLLM));
assert_param(IS_RCC_PLLN_VALUE(PLLN));
assert_param(IS_RCC_PLLP_VALUE(PLLP));
assert_param(IS_RCC_PLLQ_VALUE(PLLQ));
RCC->PLLCFGR = PLLM | (PLLN << 6) | (((PLLP >> 1) -1) << 16) | (RCC_PLLSource) |
(PLLQ << 24);
}
/**
* @brief Enables or disables the main PLL.
* @note After enabling the main PLL, the application software should wait on
* PLLRDY flag to be set indicating that PLL clock is stable and can
* be used as system clock source.
* @note The main PLL can not be disabled if it is used as system clock source
* @note The main PLL is disabled by hardware when entering STOP and STANDBY modes.
* @param NewState: new state of the main PLL. This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void RCC_PLLCmd(FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_FUNCTIONAL_STATE(NewState));
*(__IO uint32_t *) CR_PLLON_BB = (uint32_t)NewState;
}
/**
* @brief Configures the PLLI2S clock multiplication and division factors.
*
* @note PLLI2S is available only in Silicon RevisionB and RevisionY.
* @note This function must be used only when the PLLI2S is disabled.
* @note PLLI2S clock source is common with the main PLL (configured in
* RCC_PLLConfig function )
*
* @param PLLI2SN: specifies the multiplication factor for PLLI2S VCO output clock
* This parameter must be a number between 192 and 432.
* @note You have to set the PLLI2SN parameter correctly to ensure that the VCO
* output frequency is between 192 and 432 MHz.
*
* @param PLLI2SR: specifies the division factor for I2S clock
* This parameter must be a number between 2 and 7.
* @note You have to set the PLLI2SR parameter correctly to not exceed 192 MHz
* on the I2S clock frequency.
*
* @retval None
*/
void RCC_PLLI2SConfig(uint32_t PLLI2SN, uint32_t PLLI2SR)
{
/* Check the parameters */
assert_param(IS_RCC_PLLI2SN_VALUE(PLLI2SN));
assert_param(IS_RCC_PLLI2SR_VALUE(PLLI2SR));
RCC->PLLI2SCFGR = (PLLI2SN << 6) | (PLLI2SR << 28);
}
/**
* @brief Enables or disables the PLLI2S.
* @note PLLI2S is available only in RevisionB and RevisionY
* @note The PLLI2S is disabled by hardware when entering STOP and STANDBY modes.
* @param NewState: new state of the PLLI2S. This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void RCC_PLLI2SCmd(FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_FUNCTIONAL_STATE(NewState));
*(__IO uint32_t *) CR_PLLI2SON_BB = (uint32_t)NewState;
}
/**
* @brief Enables or disables the Clock Security System.
* @note If a failure is detected on the HSE oscillator clock, this oscillator
* is automatically disabled and an interrupt is generated to inform the
* software about the failure (Clock Security System Interrupt, CSSI),
* allowing the MCU to perform rescue operations. The CSSI is linked to
* the Cortex-M3 NMI (Non-Maskable Interrupt) exception vector.
* @param NewState: new state of the Clock Security System.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void RCC_ClockSecuritySystemCmd(FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_FUNCTIONAL_STATE(NewState));
*(__IO uint32_t *) CR_CSSON_BB = (uint32_t)NewState;
}
/**
* @brief Selects the clock source to output on MCO1 pin(PA8).
* @note PA8 should be configured in alternate function mode.
* @param RCC_MCO1Source: specifies the clock source to output.
* This parameter can be one of the following values:
* @arg RCC_MCO1Source_HSI: HSI clock selected as MCO1 source
* @arg RCC_MCO1Source_LSE: LSE clock selected as MCO1 source
* @arg RCC_MCO1Source_HSE: HSE clock selected as MCO1 source
* @arg RCC_MCO1Source_PLLCLK: main PLL clock selected as MCO1 source
* @param RCC_MCO1Div: specifies the MCO1 prescaler.
* This parameter can be one of the following values:
* @arg RCC_MCO1Div_1: no division applied to MCO1 clock
* @arg RCC_MCO1Div_2: division by 2 applied to MCO1 clock
* @arg RCC_MCO1Div_3: division by 3 applied to MCO1 clock
* @arg RCC_MCO1Div_4: division by 4 applied to MCO1 clock
* @arg RCC_MCO1Div_5: division by 5 applied to MCO1 clock
* @retval None
*/
void RCC_MCO1Config(uint32_t RCC_MCO1Source, uint32_t RCC_MCO1Div)
{
uint32_t tmpreg = 0;
/* Check the parameters */
assert_param(IS_RCC_MCO1SOURCE(RCC_MCO1Source));
assert_param(IS_RCC_MCO1DIV(RCC_MCO1Div));
tmpreg = RCC->CFGR;
/* Clear MCO1[1:0] and MCO1PRE[2:0] bits */
tmpreg &= CFGR_MCO1_RESET_MASK;
/* Select MCO1 clock source and prescaler */
tmpreg |= RCC_MCO1Source | RCC_MCO1Div;
/* Store the new value */
RCC->CFGR = tmpreg;
}
/**
* @brief Selects the clock source to output on MCO2 pin(PC9).
* @note PC9 should be configured in alternate function mode.
* @param RCC_MCO2Source: specifies the clock source to output.
* This parameter can be one of the following values:
* @arg RCC_MCO2Source_SYSCLK: System clock (SYSCLK) selected as MCO2 source
* @arg RCC_MCO2Source_PLLI2SCLK: PLLI2S clock selected as MCO2 source
* @arg RCC_MCO2Source_HSE: HSE clock selected as MCO2 source
* @arg RCC_MCO2Source_PLLCLK: main PLL clock selected as MCO2 source
* @param RCC_MCO2Div: specifies the MCO2 prescaler.
* This parameter can be one of the following values:
* @arg RCC_MCO2Div_1: no division applied to MCO2 clock
* @arg RCC_MCO2Div_2: division by 2 applied to MCO2 clock
* @arg RCC_MCO2Div_3: division by 3 applied to MCO2 clock
* @arg RCC_MCO2Div_4: division by 4 applied to MCO2 clock
* @arg RCC_MCO2Div_5: division by 5 applied to MCO2 clock
* @retval None
*/
void RCC_MCO2Config(uint32_t RCC_MCO2Source, uint32_t RCC_MCO2Div)
{
uint32_t tmpreg = 0;
/* Check the parameters */
assert_param(IS_RCC_MCO2SOURCE(RCC_MCO2Source));
assert_param(IS_RCC_MCO2DIV(RCC_MCO2Div));
tmpreg = RCC->CFGR;
/* Clear MCO2 and MCO2PRE[2:0] bits */
tmpreg &= CFGR_MCO2_RESET_MASK;
/* Select MCO2 clock source and prescaler */
tmpreg |= RCC_MCO2Source | RCC_MCO2Div;
/* Store the new value */
RCC->CFGR = tmpreg;
}
/**
* @}
*/
/** @defgroup RCC_Group2 System AHB and APB busses clocks configuration functions
* @brief System, AHB and APB busses clocks configuration functions
*
@verbatim
===============================================================================
System, AHB and APB busses clocks configuration functions
===============================================================================
This section provide functions allowing to configure the System, AHB, APB1 and
APB2 busses clocks.
1. Several clock sources can be used to drive the System clock (SYSCLK): HSI,
HSE and PLL.
The AHB clock (HCLK) is derived from System clock through configurable prescaler
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -