📄 stm32f0xx_rcc.c
字号:
tmpreg &= ~RCC_CFGR_PPRE;
/* Set PPRE[2:0] bits according to RCC_HCLK value */
tmpreg |= RCC_HCLK;
/* Store the new value */
RCC->CFGR = tmpreg;
}
/**
* @brief Configures the ADC clock (ADCCLK).
* @param RCC_ADCCLK: defines the ADC clock source. This clock is derived
* from the HSI14 or APB clock (PCLK).
* This parameter can be one of the following values:
* @arg RCC_ADCCLK_HSI14: ADC clock = HSI14 (14MHz)
* @arg RCC_ADCCLK_PCLK_Div2: ADC clock = PCLK/2
* @arg RCC_ADCCLK_PCLK_Div4: ADC clock = PCLK/4
* @retval None
*/
void RCC_ADCCLKConfig(uint32_t RCC_ADCCLK)
{
/* Check the parameters */
assert_param(IS_RCC_ADCCLK(RCC_ADCCLK));
/* Clear ADCPRE bit */
RCC->CFGR &= ~RCC_CFGR_ADCPRE;
/* Set ADCPRE bits according to RCC_PCLK value */
RCC->CFGR |= RCC_ADCCLK & 0xFFFF;
/* Clear ADCSW bit */
RCC->CFGR3 &= ~RCC_CFGR3_ADCSW;
/* Set ADCSW bits according to RCC_ADCCLK value */
RCC->CFGR3 |= RCC_ADCCLK >> 16;
}
/**
* @brief Configures the CEC clock (CECCLK).
* @param RCC_CECCLK: defines the CEC clock source. This clock is derived
* from the HSI or LSE clock.
* This parameter can be one of the following values:
* @arg RCC_CECCLK_HSI_Div244: CEC clock = HSI/244 (32768Hz)
* @arg RCC_CECCLK_LSE: CEC clock = LSE
* @retval None
*/
void RCC_CECCLKConfig(uint32_t RCC_CECCLK)
{
/* Check the parameters */
assert_param(IS_RCC_CECCLK(RCC_CECCLK));
/* Clear CECSW bit */
RCC->CFGR3 &= ~RCC_CFGR3_CECSW;
/* Set CECSW bits according to RCC_CECCLK value */
RCC->CFGR3 |= RCC_CECCLK;
}
/**
* @brief Configures the I2C1 clock (I2C1CLK).
* @param RCC_I2CCLK: defines the I2C1 clock source. This clock is derived
* from the HSI or System clock.
* This parameter can be one of the following values:
* @arg RCC_I2C1CLK_HSI: I2C1 clock = HSI
* @arg RCC_I2C1CLK_SYSCLK: I2C1 clock = System Clock
* @retval None
*/
void RCC_I2CCLKConfig(uint32_t RCC_I2CCLK)
{
/* Check the parameters */
assert_param(IS_RCC_I2CCLK(RCC_I2CCLK));
/* Clear I2CSW bit */
RCC->CFGR3 &= ~RCC_CFGR3_I2C1SW;
/* Set I2CSW bits according to RCC_I2CCLK value */
RCC->CFGR3 |= RCC_I2CCLK;
}
/**
* @brief Configures the USART1 clock (USART1CLK).
* @param RCC_USARTCLK: defines the USART1 clock source. This clock is derived
* from the HSI or System clock.
* This parameter can be one of the following values:
* @arg RCC_USART1CLK_PCLK: USART1 clock = APB Clock (PCLK)
* @arg RCC_USART1CLK_SYSCLK: USART1 clock = System Clock
* @arg RCC_USART1CLK_LSE: USART1 clock = LSE Clock
* @arg RCC_USART1CLK_HSI: USART1 clock = HSI Clock
* @retval None
*/
void RCC_USARTCLKConfig(uint32_t RCC_USARTCLK)
{
/* Check the parameters */
assert_param(IS_RCC_USARTCLK(RCC_USARTCLK));
/* Clear USARTSW[1:0] bit */
RCC->CFGR3 &= ~RCC_CFGR3_USART1SW;
/* Set USARTSW bits according to RCC_USARTCLK value */
RCC->CFGR3 |= RCC_USARTCLK;
}
/**
* @brief Returns the frequencies of the System, AHB and APB busses clocks.
* @note The frequency returned by this function is not the real frequency
* in the chip. It is calculated based on the predefined constant and
* the source selected by RCC_SYSCLKConfig():
*
* @note If SYSCLK source is HSI, function returns constant HSI_VALUE(*)
*
* @note If SYSCLK source is HSE, function returns constant HSE_VALUE(**)
*
* @note If SYSCLK source is PLL, function returns constant HSE_VALUE(**)
* or HSI_VALUE(*) multiplied by the PLL factors.
*
* (*) HSI_VALUE is a constant defined in stm32f0xx.h file (default value
* 8 MHz) but the real value may vary depending on the variations
* in voltage and temperature, refer to RCC_AdjustHSICalibrationValue().
*
* (**) HSE_VALUE is a constant defined in stm32f0xx.h file (default value
* 8 MHz), user has to ensure that HSE_VALUE is same as the real
* frequency of the crystal used. Otherwise, this function may
* return wrong result.
*
* - The result of this function could be not correct when using fractional
* value for HSE crystal.
*
* @param RCC_Clocks: pointer to a RCC_ClocksTypeDef structure which will hold
* the clocks frequencies.
*
* @note This function can be used by the user application to compute the
* baudrate for the communication peripherals or configure other parameters.
* @note Each time SYSCLK, HCLK and/or PCLK clock changes, this function
* must be called to update the structure's field. Otherwise, any
* configuration based on this function will be incorrect.
*
* @retval None
*/
void RCC_GetClocksFreq(RCC_ClocksTypeDef* RCC_Clocks)
{
uint32_t tmp = 0, pllmull = 0, pllsource = 0, prediv1factor = 0, presc = 0;
/* Get SYSCLK source -------------------------------------------------------*/
tmp = RCC->CFGR & RCC_CFGR_SWS;
switch (tmp)
{
case 0x00: /* HSI used as system clock */
RCC_Clocks->SYSCLK_Frequency = HSI_VALUE;
break;
case 0x04: /* HSE used as system clock */
RCC_Clocks->SYSCLK_Frequency = HSE_VALUE;
break;
case 0x08: /* PLL used as system clock */
/* Get PLL clock source and multiplication factor ----------------------*/
pllmull = RCC->CFGR & RCC_CFGR_PLLMULL;
pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
pllmull = ( pllmull >> 18) + 2;
if (pllsource == 0x00)
{
/* HSI oscillator clock divided by 2 selected as PLL clock entry */
RCC_Clocks->SYSCLK_Frequency = (HSI_VALUE >> 1) * pllmull;
}
else
{
prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1;
/* HSE oscillator clock selected as PREDIV1 clock entry */
RCC_Clocks->SYSCLK_Frequency = (HSE_VALUE / prediv1factor) * pllmull;
}
break;
default: /* HSI used as system clock */
RCC_Clocks->SYSCLK_Frequency = HSI_VALUE;
break;
}
/* Compute HCLK, PCLK clocks frequencies -----------------------------------*/
/* Get HCLK prescaler */
tmp = RCC->CFGR & RCC_CFGR_HPRE;
tmp = tmp >> 4;
presc = APBAHBPrescTable[tmp];
/* HCLK clock frequency */
RCC_Clocks->HCLK_Frequency = RCC_Clocks->SYSCLK_Frequency >> presc;
/* Get PCLK prescaler */
tmp = RCC->CFGR & RCC_CFGR_PPRE;
tmp = tmp >> 8;
presc = APBAHBPrescTable[tmp];
/* PCLK clock frequency */
RCC_Clocks->PCLK_Frequency = RCC_Clocks->HCLK_Frequency >> presc;
/* ADCCLK clock frequency */
if((RCC->CFGR3 & RCC_CFGR3_ADCSW) != RCC_CFGR3_ADCSW)
{
/* ADC Clock is HSI14 Osc. */
RCC_Clocks->ADCCLK_Frequency = HSI14_VALUE;
}
else
{
if((RCC->CFGR & RCC_CFGR_ADCPRE) != RCC_CFGR_ADCPRE)
{
/* ADC Clock is derived from PCLK/2 */
RCC_Clocks->ADCCLK_Frequency = RCC_Clocks->PCLK_Frequency >> 1;
}
else
{
/* ADC Clock is derived from PCLK/4 */
RCC_Clocks->ADCCLK_Frequency = RCC_Clocks->PCLK_Frequency >> 2;
}
}
/* CECCLK clock frequency */
if((RCC->CFGR3 & RCC_CFGR3_CECSW) != RCC_CFGR3_CECSW)
{
/* CEC Clock is HSI/256 */
RCC_Clocks->CECCLK_Frequency = HSI_VALUE / 244;
}
else
{
/* CECC Clock is LSE Osc. */
RCC_Clocks->CECCLK_Frequency = LSE_VALUE;
}
/* I2C1CLK clock frequency */
if((RCC->CFGR3 & RCC_CFGR3_I2C1SW) != RCC_CFGR3_I2C1SW)
{
/* I2C1 Clock is HSI Osc. */
RCC_Clocks->I2C1CLK_Frequency = HSI_VALUE;
}
else
{
/* I2C1 Clock is System Clock */
RCC_Clocks->I2C1CLK_Frequency = RCC_Clocks->SYSCLK_Frequency;
}
/* USART1CLK clock frequency */
if((RCC->CFGR3 & RCC_CFGR3_USART1SW) == 0x0)
{
/* USART1 Clock is PCLK */
RCC_Clocks->USART1CLK_Frequency = RCC_Clocks->PCLK_Frequency;
}
else if((RCC->CFGR3 & RCC_CFGR3_USART1SW) == RCC_CFGR3_USART1SW_0)
{
/* USART1 Clock is System Clock */
RCC_Clocks->USART1CLK_Frequency = RCC_Clocks->SYSCLK_Frequency;
}
else if((RCC->CFGR3 & RCC_CFGR3_USART1SW) == RCC_CFGR3_USART1SW_1)
{
/* USART1 Clock is LSE Osc. */
RCC_Clocks->USART1CLK_Frequency = LSE_VALUE;
}
else if((RCC->CFGR3 & RCC_CFGR3_USART1SW) == RCC_CFGR3_USART1SW)
{
/* USART1 Clock is HSI Osc. */
RCC_Clocks->USART1CLK_Frequency = HSI_VALUE;
}
}
/**
* @}
*/
/** @defgroup RCC_Group3 Peripheral clocks configuration functions
* @brief Peripheral clocks configuration functions
*
@verbatim
===============================================================================
#####Peripheral clocks configuration functions #####
===============================================================================
[..] This section provide functions allowing to configure the Peripheral clocks.
(#) The RTC clock which is derived from the LSE, LSI or HSE_Div32 (HSE
divided by 32).
(#) After restart from Reset or wakeup from STANDBY, all peripherals are off
except internal SRAM, Flash and SWD. Before to start using a peripheral you
have to enable its interface clock. You can do this using RCC_AHBPeriphClockCmd(),
RCC_APB2PeriphClockCmd() and RCC_APB1PeriphClockCmd() functions.
(#) To reset the peripherals configuration (to the default state after device reset)
you can use RCC_AHBPeriphResetCmd(), RCC_APB2PeriphResetCmd() and
RCC_APB1PeriphResetCmd() functions.
@endverbatim
* @{
*/
/**
* @brief Configures the RTC clock (RTCCLK).
* @note As the RTC clock configuration bits are 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 RTC clock source (to be done once after reset).
* @note Once the RTC clock is configured it can't be changed unless the RTC
* is reset using RCC_BackupResetCmd function, or by a Power On Reset (POR)
*
* @param RCC_RTCCLKSource: specifies the RTC clock source.
* This parameter can be one of the following values:
* @arg RCC_RTCCLKSource_LSE: LSE selected as RTC clock
* @arg RCC_RTCCLKSource_LSI: LSI selected as RTC clock
* @arg RCC_RTCCLKSource_HSE_Div32: HSE divided by 32 selected as RTC clock
*
* @note If the LSE or LSI is used as RTC clock source, the RTC continues to
* work in STOP and STANDBY modes, and can be used as wakeup source.
* However, when the HSE clock is used as RTC clock source, the RTC
* cannot be used in STOP and STANDBY modes.
*
* @note The maximum input clock frequency for RTC is 2MHz (when using HSE as
* RTC clock source).
*
* @retval None
*/
void RCC_RTCCLKConfig(uint32_t RCC_RTCCLKSource)
{
/* Check the parameters */
assert_param(IS_RCC_RTCCLK_SOURCE(RCC_RTCCLKSource));
/* Select the RTC clock source */
RCC->BDCR |= RCC_RTCCLKSource;
}
/**
* @brief Enables or disables the RTC clock.
* @note This function must be used only after the RTC clock source was selected
* using the RCC_RTCCLKConfig function.
* @param NewState: new state of the RTC clock.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void RCC_RTCCLKCmd(FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
RCC->BDCR |= RCC_BDCR_RTCEN;
}
else
{
RCC->BDCR &= ~RCC_BDCR_RTCEN;
}
}
/**
* @brief Forces or releases the Backup domain reset.
* @note This function resets the RTC peripheral (including the backup registers)
* and the RTC clock source selection in RCC_BDCR register.
* @param NewState: new state of the Backup domain reset.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void RCC_BackupResetCmd(FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
RCC->BDCR |= RCC_BDCR_BDRST;
}
else
{
RCC->BDCR &= ~RCC_BDCR_BDRST;
}
}
/**
* @brief Enables or disables the AHB peripheral clock.
* @note After reset, the peripheral clock (used for registers read/write access)
* is disabled and the application software has to enable this clock before
* using it.
* @param RCC_AHBPeriph: specifies the AHB peripheral to gates its clock.
* This parameter can be any combination of the following values:
* @arg RCC_AHBPeriph_GPIOA: GPIOA clock
* @arg RCC_AHBPeriph_GPIOB: GPIOB clock
* @arg RCC_AHBPeriph_GPIOC: GPIOC clock
* @arg RCC_AHBPeriph_GPIOD: GPIOD clock
* @arg RCC_AHBPeriph_GPIOF: GPIOF clock
* @arg RCC_AHBPeriph_TS: TS clock
* @arg RCC_AHBPeriph_CRC: CRC clock
* @arg RCC_AHBPeriph_FLITF: (has effect only when the Flash memory is in power down mode)
* @arg RCC_AHBPeriph_SRAM: SRAM clock
* @arg RCC_AHBPeriph_DMA1: DMA1 clock
* @param NewState: new state of the specified peripheral clock.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void RCC_AHBPeriphClockCmd(uint32_t RCC_AHBPeriph, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_RCC_AHB_PERIPH(RCC_AHBPeriph));
assert_param(IS_FUNCTIONAL_STATE(NewState));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -