5.17 RCC 使用说明

本章说明 N32G41x 标准外设库中 RCC 驱动的文件组成与 86 个公开函数,内容包括复位、各时钟源与 PLL 的配置、系统时钟与总线预分频、外设时钟使能与复位、MCO 时钟输出、时钟安全系统以及中断与标志管理,并给出 1 个示例 RCC_ClockConfig。时钟树、各时钟源特性与寄存器定义请配合用户手册的“复位和时钟控制(RCC)”章节阅读,芯片已知问题以勘误手册为准。

5.17.1 相关文件

相对路径 作用
firmware/n32g41x_std_periph_driver/inc/n32g41x_rcc.h 定义 RCC_ClocksType 结构体与时钟源、预分频、MCO、时钟安全系统、中断与标志等宏,并按头文件中的十七个功能区段声明 86 个公开函数
firmware/n32g41x_std_periph_driver/src/n32g41x_rcc.c 实现上述 86 个函数:时钟源使能与校准、PLL 与系统时钟及总线分频配置、外设时钟使能与复位、外设与定时器时钟源选择、滤波分频、MCO 输出、时钟安全系统、EMC 与 Glitch 检测以及中断与标志管理
projects/n32g41x_EVAL/examples/RCC/ RCC 示例根目录,含 RCC_ClockConfig 1 个示例
projects/n32g41x_EVAL/examples/RCC/RCC_ClockConfig/ 该示例的工程目录组成:MDK-ARM/、EWARM/、inc/、src/、readme.txt
projects/n32g41x_EVAL/bsp/ 示例引用的板级支持文件:串口日志输出与毫秒延时

5.17.2 API 参考

搜索 API共 86 个函数

以下 86 个公开函数按 n32g41x_rcc.h 的声明顺序分为 17 组;每个条目给出功能摘要与 n32g41x_rcc.c 中该函数正上方的注释原文。

复位

RCC_DeInit

HSI 配置

RCC_EnableHsi、RCC_SetHsiCalibValue、RCC_WaitHsiStable

HSE 配置

RCC_ConfigHse、RCC_ConfigHseReadyDly、RCC_WaitHseStable

LSI 配置

RCC_EnableLsi、RCC_SetLsiCalibValue、RCC_WaitLsiStable

LSE 配置

RCC_ConfigLse、RCC_WaitLseStable、RCC_EnableLseNim、RCC_ConfigLseGlitchFilter、RCC_ConfigLseBuffOpt、RCC_ConfigLseBiasTrim、RCC_ConfigLseDrvTrim

PLL 配置

RCC_ConfigPll、RCC_EnablePll、RCC_EnablePllBypass、RCC_EnablePllSysclkDiv

系统时钟与总线预分频配置

RCC_ConfigSysclk、RCC_GetSysclkSrc、RCC_ConfigHclk、RCC_ConfigPclk1、RCC_ConfigPclk2、RCC_GetClocksFreqValue

外设时钟使能与复位

RCC_EnableAHBPeriphClk、RCC_EnableAPB1PeriphClk、RCC_EnableAPB2PeriphClk、RCC_EnableAHBPeriphReset、RCC_EnableAPB1PeriphReset、RCC_EnableAPB2PeriphReset

外设时钟源配置

RCC_ConfigAdc1mClk、RCC_ConfigAdcSysclk、RCC_ConfigI2SClk、RCC_ConfigUsart1Clk、RCC_ConfigUsart4Clk、RCC_ConfigRTCClk、RCC_ConfigLowPowerClk

定时器时钟源配置

RCC_ConfigAtim1Clk、RCC_ConfigAtim2Clk、RCC_ConfigGtim1Clk、RCC_ConfigGtim2Clk、RCC_ConfigGtim3Clk、RCC_ConfigGtim4Clk、RCC_ConfigBtim1Clk

定时器与 IO 滤波时钟分频配置

RCC_ConfigIOFilter、RCC_ConfigATIM1Filter、RCC_ConfigATIM2Filter、RCC_ConfigGTIM1Filter、RCC_ConfigGTIM2Filter、RCC_ConfigGTIM3Filter、RCC_ConfigGTIM4Filter

MCO 输出配置

RCC_ConfigMco、RCC_ConfigMcoClkPre

时钟安全系统

RCC_EnableHSEClockSecuritySystem、RCC_EnableLSEClockSecuritySystem、RCC_EnableHSECaptureRequest、RCC_EnableLSECaptureRequest

EMC 检测与保护

RCC_EnableEMCDetect、RCC_ConfigEMCDetectLevel、RCC_EnableEMCReset、RCC_EnableEMCSwitch、RCC_EnableEMCGBInt、RCC_EnableEMCGBNInt

Glitch 检测与保护

RCC_EnableGlitchDetect、RCC_ConfigGlitchDetectLevel、RCC_EnableGlitchReset、RCC_EnableGlitchSwitch、RCC_EnableGlitchInt、RCC_EnableM4LockupReset

功耗台阶配置

RCC_EnablePwrUpStep、RCC_EnablePwrDownStep、RCC_ConfigPwrUpStepWidth、RCC_ConfigPwrDownStepWidth、RCC_EnablePStepSf、RCC_PStepSfDiv、RCC_ClearPwrDownStepFlag、RCC_GetPwrDownStepFlag、RCC_GetPwrStepFsm

中断与标志管理

RCC_ConfigInt、RCC_GetIntStatus、RCC_ClrIntPendingBit、RCC_GetFlagStatus、RCC_ClearResetFlag

void RCC_DeInit(void)把 RCC 的各个寄存器复位为默认值。
源码注释
name    RCC_DeInit.
fun     Reset the RCC registers.
param   none
return  none
void RCC_EnableHsi(FunctionalState Cmd)使能内部高速时钟(HSI)。
源码注释
name    RCC_EnableHsi.
fun     Enables the Internal High Speed oscillator (HSI).
param   Cmd:
          - ENABLE
          - DISABLE
return  none
note   HSI can not be stopped if it is used directly or through the PLL as system clock.
void RCC_SetHsiCalibValue(uint16_t calibration_value)调整内部高速时钟(HSI)的校准值。
源码注释
name    RCC_SetHsiCalibValue.
fun     Adjusts the Internal High Speed oscillator (HSI) calibration value.
param   calibration_value(the calibration trimming value):
        This parameter must be a number between 0 and 0x1FF
return  none
ErrorStatus RCC_WaitHsiStable(void)等待 HSI 启动稳定。
源码注释
name    RCC_WaitHsiStable.
fun     Waits for HSI start-up.
param   none
return  ErrorStatus:
         - SUCCESS    HSI oscillator is stable and ready to use
         - ERROR      HSI oscillator not yet ready
void RCC_ConfigHse(uint32_t RCC_HSE)配置外部高速时钟(HSE)。
源码注释
name    RCC_ConfigHse.
fun     Configures the External High Speed oscillator (HSE).
param   RCC_HSE :
          - RCC_HSE_DISABLE    HSE oscillator OFF
          - RCC_HSE_ENABLE     HSE oscillator ON
          - RCC_HSE_BYPASS     HSE oscillator bypassed with external clock
return  none:
note    HSE can not be stopped if it is used directly or through the PLL as system clock
void RCC_ConfigHseReadyDly(uint32_t hse_dlydiv)配置 HSE 就绪延时(RDF 时间)。
源码注释
name    RCC_ConfigHseReadyDly.
fun     Configures the HSE RDF Time
param   hse_dlydiv(HSE RDF delay value):
         - RCC_HSERDFDIV_1MS
         - RCC_HSERDFDIV_2MS
         - RCC_HSERDFDIV_3MS
         - RCC_HSERDFDIV_4MS
         - RCC_HSERDFDLY_128HSE
return  none
ErrorStatus RCC_WaitHseStable(void)等待 HSE 启动稳定。
源码注释
name    RCC_WaitHseStable.
fun     Waits for HSE start-up.
param   none
return  ErrorStatus:
         - SUCCESS    HSE oscillator is stable and ready to use
         - ERROR      HSE oscillator not yet ready
void RCC_EnableLsi(FunctionalState Cmd)使能内部低速时钟(LSI)。
源码注释
name    RCC_EnableLsi.
fun     Enables the LSI.
param   Cmd:
          - ENABLE
          - DISABLE
return  none
void RCC_SetLsiCalibValue(uint16_t calibration_value)调整内部低速时钟(LSI)的校准值。
源码注释
name    RCC_SetLsiCalibValue.
fun     Adjusts the Internal Low Speed oscillator (LSI) calibration value.
param   calibration_value(the calibration trimming value):
        This parameter must be a number between 0 and 0x3F
return  none
ErrorStatus RCC_WaitLsiStable(void)等待 LSI 启动稳定。
源码注释
name    RCC_WaitLsiStable.
fun     Waits for LSI start-up.
param   none
return  ErrorStatus:
         - SUCCESS    LSI oscillator is stable and ready to use
         - ERROR      LSI oscillator not yet ready
void RCC_ConfigLse(uint32_t RCC_LSE)配置外部低速时钟(LSE)。
源码注释
name    RCC_ConfigLse.
fun     Configures the External Low Speed oscillator (LSE).
param   RCC_LSE :
          - RCC_LSE_DISABLE    LSE oscillator OFF
          - RCC_LSE_ENABLE     LSE oscillator ON
          - RCC_LSE_BYPASS     LSE oscillator bypassed with external clock
return  none.
note none.
ErrorStatus RCC_WaitLseStable(void)等待 LSE 启动稳定。
源码注释
name    RCC_WaitLseStable.
fun     Waits for LSE start-up.
param   none
return  ErrorStatus:
         - SUCCESS    LSE oscillator is stable and ready to use
         - ERROR      LSE oscillator not yet ready
void RCC_EnableLseNim(FunctionalState Cmd)使能 LSE 噪声抑制。
源码注释
name    RCC_EnableLseNim.
fun     LSE noise immunity enable.
param   Cmd:
          - ENABLE
          - DISABLE
return  none.
void RCC_ConfigLseGlitchFilter(uint32_t lsegliterflit)配置 LSE 毛刺滤波器。
源码注释
name    RCC_ConfigLseGlitchFilter.
fun     Configure LSE glitch filter.
param   lse glitch filter select for  ad_lse_clkout
          - RCC_LSE_GLITCHFILTER_25ns
          - RCC_LSE_GLITCHFILTER_50ns
          - RCC_LSE_GLITCHFILTER_100ns
          - RCC_LSE_GLITCHFILTER_250ns
          - 0 : no glitch filter
return  none
void RCC_ConfigLseBuffOpt(uint32_t lsebuffopt)配置 LSE 缓冲调整信号。
源码注释
name    RCC_ConfigLseBuffOpt.
fun     Configure LSE buffer trimming signal.
param   lsebuffopt (lse buffer trimming)
return  none
void RCC_ConfigLseBiasTrim(uint32_t LseBiasTrim)配置 LSE 偏置调整信号。
源码注释
name    RCC_ConfigLseBiasTrim.
fun     Configure lse bias trimming signal.
param   LseBiasTrim (lse bias trimming)
return  none
void RCC_ConfigLseDrvTrim(uint32_t lsedrvtrim)配置 LSE 驱动能力调整信号。
源码注释
name    RCC_ConfigLseDrvTrim.
fun     Configure LSE core trimming signal.
param   lsedrvtrim (lse drv trimming)
return  none
void RCC_ConfigPll(uint32_t PLL_source, uint32_t PLL_inpre, uint32_t PLL_mul, uint32_t PLL_od)配置 PLL 的时钟源与倍频系数。
源码注释
name    RCC_ConfigPll.
fun     Configures the PLL clock source and multiplication factor.
param   PLL_source(PLL entry clock source):
          - RCC_PLL_SRC_HSI         HSI oscillator clock selected as PLL clock entry
          - RCC_PLL_SRC_HSE         HSE oscillator clock selected as PLL clock entry
param   PLL_inpre(PLL input divider):
          - 1 ~ 63(for div 1 ~ div 63)
param   PLL_mul(PLL multiplication factor):
          - 2 ~ 127(for mul 2 ~ mul 127)
param   PLL_od(PLL OD):
          - RCC_PLL_OD_DIV_1
          - RCC_PLL_OD_DIV_2
return  none
note    This function must be used only when the PLL is disabled.
note    Fin frequency requirement is in the range of 8MHz ~ 32MHz,
        Fin/PLL_inpre frequency requirement is in the range of 8MHz ~ 32MHz,
        Fvco(=Fin/PLL_inpre*PLL_mul) frequency requirement is in the range of 96MHz ~ 160MHz,
        Fout(=Fvco/PLL_od) frequency requirement is in the range of 64MHz ~ 160MHz.

使用提示: 先用本函数写入时钟源与倍频参数,再用 RCC_EnablePll 使能 PLL;PLL 激活后这些参数不可再修改。

void RCC_EnablePll(FunctionalState Cmd)使能 PLL。
源码注释
name    RCC_EnablePll.
fun     Enables the PLL.
param   Cmd:
          - ENABLE
          - DISABLE
return  none
void RCC_EnablePllBypass(FunctionalState Cmd)使能 PLL 旁路。
源码注释
name    RCC_EnablePllBypass
fun     Enables the PLL Bypass.
param   Cmd:
          - ENABLE
          - DISABLE
return  none.

使用提示: 旁路模式下 PLL 输入时钟直接送到输出,切换前需确认该输入时钟已经稳定。

void RCC_EnablePllSysclkDiv(FunctionalState Cmd)配置是否把 PLL 输出二分频后送给系统时钟。
源码注释
name    RCC_EnablePllSysclkDiv.
fun     Configure whether to enable Pll Clock Output Div2 to sysclk.
param   Cmd:
          - ENABLE
          - DISABLE
return  none.

使用提示: 该位控制 PLL 输出是否二分频;使能 PLL 前需先将其清零,待 PLL 就绪后再配置。

void RCC_ConfigSysclk(uint32_t sysclk_source)配置系统时钟(SYSCLK)的来源。
源码注释
name    RCC_ConfigSysclk.
fun     Configures the system clock (SYSCLK).
param   sysclk_source(clock source used as system clock):
          - RCC_SYSCLK_SRC_HSI       HSI selected as system clock
          - RCC_SYSCLK_SRC_HSE       HSE selected as system clock
          - RCC_SYSCLK_SRC_PLL      PLL selected as system clock
return  none
uint32_t RCC_GetSysclkSrc(void)返回当前用作系统时钟的时钟源。
源码注释
name    RCC_GetSysclkSrc.
fun     Returns the clock source used as system clock.
param   none
return  (The clock source used as system clock):
          - RCC_SYSCLK_STS_HSI       HSI selected as system clock
          - RCC_SYSCLK_STS_HSE       HSE selected as system clock
          - RCC_SYSCLK_STS_PLL       PLL selected as system clock
void RCC_ConfigHclk(uint32_t sysclk_div)配置 AHB 时钟(HCLK)。
源码注释
name    RCC_ConfigHclk.
fun     Configures the AHB clock (HCLK).
param   sysclk_div(AHB clock is derived from the system clock (SYSCLK)):
          - RCC_SYSCLK_DIV1      AHB clock = SYSCLK
          - RCC_SYSCLK_DIV2      AHB clock = SYSCLK/2
          - RCC_SYSCLK_DIV4      AHB clock = SYSCLK/4
          - RCC_SYSCLK_DIV8      AHB clock = SYSCLK/8
          - RCC_SYSCLK_DIV16     AHB clock = SYSCLK/16
          - RCC_SYSCLK_DIV64     AHB clock = SYSCLK/64
          - RCC_SYSCLK_DIV128    AHB clock = SYSCLK/128
return  none
void RCC_ConfigPclk1(uint32_t RCC_HCLK)配置低速 APB 时钟(PCLK1)。
源码注释
name    RCC_ConfigPclk1.
fun     Configures the Low Speed APB clock (PCLK1).
param   RCC_HCLK(APB1 clock is derived from the AHB clock (HCLK)):
          - RCC_HCLK_DIV1     APB1 clock = HCLK
          - RCC_HCLK_DIV2     APB1 clock = HCLK/2
          - RCC_HCLK_DIV4     APB1 clock = HCLK/4
          - RCC_HCLK_DIV8     APB1 clock = HCLK/8
          - RCC_HCLK_DIV16    APB1 clock = HCLK/16
return  none
void RCC_ConfigPclk2(uint32_t RCC_HCLK)配置高速 APB 时钟(PCLK2)。
源码注释
name    RCC_ConfigPclk2.
fun     Configures the High Speed APB clock (PCLK2).
param   RCC_HCLK(APB2 clock is derived from the AHB clock (HCLK)):
          - RCC_HCLK_DIV1     APB2 clock = HCLK
          - RCC_HCLK_DIV2     APB2 clock = HCLK/2
          - RCC_HCLK_DIV4     APB2 clock = HCLK/4
          - RCC_HCLK_DIV8     APB2 clock = HCLK/8
          - RCC_HCLK_DIV16    APB2 clock = HCLK/16
return  none
void RCC_GetClocksFreqValue(RCC_ClocksType *RCC_Clocks)返回芯片各路时钟的频率。
源码注释
name    RCC_GetClocksFreqValue.
fun     Returns the frequencies of different on chip clocks.
param   RCC_Clocks pointer to a RCC_ClocksType structure which will hold
        the clocks frequencies.
return  none

使用提示: 结果按当前 RCC_CFG 配置与 HSI_VALUE、HSE_VALUE 宏计算,更换晶振后需同步更新这些宏。

void RCC_EnableAHBPeriphClk(uint32_t AHB_periph, FunctionalState Cmd)使能 AHB 外设时钟。
源码注释
name    RCC_EnableAHBPeriphClk.
fun     Enables the AHB peripheral clock.
param   AHB_periph (AHB peripheral to gates its clock):
          - RCC_AHB_PERIPH_ADC2
          - RCC_AHB_PERIPH_ADC1
          - RCC_AHB_PERIPH_CRC
          - RCC_AHB_PERIPH_GPIO
          - RCC_AHB_PERIPH_DMA
param   Cmd:
          - ENABLE
          - DISABLE
return  none
void RCC_EnableAPB1PeriphClk(uint32_t APB1_periph, FunctionalState Cmd)使能低速 APB1 外设时钟。
源码注释
name    RCC_EnableAPB1PeriphClk.
fun     Enables the Low Speed APB1 peripheral clock.
param   APB1_periph (APB1 peripheral to gates its clock):
          - RCC_APB1_PERIPH_OPAMP
          - RCC_APB1_PERIPH_CAN
          - RCC_APB1_PERIPH_I2C2
          - RCC_APB1_PERIPH_I2C1
          - RCC_APB1_PERIPH_USART3
          - RCC_APB1_PERIPH_USART2
          - RCC_APB1_PERIPH_WWDG
          - RCC_APB1_PERIPH_RTC
          - RCC_APB1_PERIPH_COMPFILT
          - RCC_APB1_PERIPH_COMP
          - RCC_APB1_PERIPH_BTIM2
          - RCC_APB1_PERIPH_BTIM1
          - RCC_APB1_PERIPH_GTIM4
          - RCC_APB1_PERIPH_GTIM3
          - RCC_APB1_PERIPH_GTIM2
          - RCC_APB1_PERIPH_GTIM1
param   Cmd:
          - ENABLE
          - DISABLE
return none.
void RCC_EnableAPB2PeriphClk(uint32_t APB2_periph, FunctionalState Cmd)使能高速 APB2 外设时钟。
源码注释
name    RCC_EnableAPB2PeriphClk.
fun     Enables the High Speed APB2 peripheral clock.
param   APB2_periph (APB2 peripheral to gates its clock):
          - RCC_APB2_PERIPH_SPI2
          - RCC_APB2_PERIPH_USART4
          - RCC_APB2_PERIPH_USART1
          - RCC_APB2_PERIPH_ATIM2
          - RCC_APB2_PERIPH_SPI1
          - RCC_APB2_PERIPH_ATIM1
param   Cmd:
          - ENABLE
          - DISABLE
return none.
void RCC_EnableAHBPeriphReset(uint32_t AHB_periph)复位 AHB 外设。
源码注释
name    RCC_EnableAHBPeriphReset.
fun     AHB peripheral reset.
param   AHB_periph specifies the AHB peripheral to reset.
          - RCC_AHB_PERIPH_ADC2
          - RCC_AHB_PERIPH_ADC1
          - RCC_AHB_PERIPH_GPIO
return none.
void RCC_EnableAPB1PeriphReset(uint32_t APB1_periph)复位低速 APB1 外设。
源码注释
name    RCC_EnableAPB1PeriphReset.
fun     Low Speed APB1 peripheral reset.
param   APB1_periph specifies the APB1 peripheral to reset.
          - RCC_APB1_PERIPH_OPAMP
          - RCC_APB1_PERIPH_CAN
          - RCC_APB1_PERIPH_I2C2
          - RCC_APB1_PERIPH_I2C1
          - RCC_APB1_PERIPH_USART3
          - RCC_APB1_PERIPH_USART2
          - RCC_APB1_PERIPH_WWDG
          - RCC_APB1_PERIPH_RTC
          - RCC_APB1_PERIPH_COMP
          - RCC_APB1_PERIPH_BTIM2
          - RCC_APB1_PERIPH_BTIM1
          - RCC_APB1_PERIPH_GTIM4
          - RCC_APB1_PERIPH_GTIM3
          - RCC_APB1_PERIPH_GTIM2
          - RCC_APB1_PERIPH_GTIM1
return none.
void RCC_EnableAPB2PeriphReset(uint32_t APB2_periph)复位高速 APB2 外设。
源码注释
name    RCC_EnableAPB2PeriphReset.
fun     High Speed APB2 peripheral reset.
param   APB2_periph specifies the APB2 peripheral to reset.
          - RCC_APB2_PERIPH_SPI2
          - RCC_APB2_PERIPH_USART4
          - RCC_APB2_PERIPH_USART1
          - RCC_APB2_PERIPH_ATIM2
          - RCC_APB2_PERIPH_SPI1
          - RCC_APB2_PERIPH_ATIM1
return none.
void RCC_ConfigAdc1mClk(uint32_t ADC1M_clksrc, uint32_t ADC1M_prescaler)配置 ADC 的 1M 时钟(ADC1MCLK)。
源码注释
name    RCC_ConfigAdc1mClk.
fun     Configures the ADCx 1M clock (ADC1MCLK).
param   ADC1M_clksrc(ADC1M clock source):
          - RCC_ADC1MCLK_SRC_HSI
          - RCC_ADC1MCLK_SRC_HSE
param   ADC1M_prescaler(ADC1M clock prescaler):
          - RCC_ADC1MCLK_DIV1
          - RCC_ADC1MCLK_DIV2
          - RCC_ADC1MCLK_DIV4
          - RCC_ADC1MCLK_DIV6
          - RCC_ADC1MCLK_DIV8
          - RCC_ADC1MCLK_DIV10
          - RCC_ADC1MCLK_DIV12
          - RCC_ADC1MCLK_DIV16
          - RCC_ADC1MCLK_DIV32
return  none
void RCC_ConfigAdcSysclk(uint32_t sclk_prescaler)配置 ADC 系统时钟(ADCSysCLK)的分频。
源码注释
name    RCC_ConfigAdcSysclk.
fun     Configures the ADCSysCLK prescaler.
param   sclk_prescaler(ADCSCLK prescaler):
         - RCC_ADCSYSCLK_DIV1     ADC clock = SYSCLK
         - RCC_ADCSYSCLK_DIV2     ADC clock = SYSCLK/2
         - RCC_ADCSYSCLK_DIV3     ADC clock = SYSCLK/3
         - RCC_ADCSYSCLK_DIV4     ADC clock = SYSCLK/4
         - RCC_ADCSYSCLK_DIV5     ADC clock = SYSCLK/5
         - RCC_ADCSYSCLK_DIV6     ADC clock = SYSCLK/6
         - RCC_ADCSYSCLK_DIV8     ADC clock = SYSCLK/8
         - RCC_ADCSYSCLK_DIV10    ADC clock = SYSCLK/10
         - RCC_ADCSYSCLK_DIV12    ADC clock = SYSCLK/12
         - RCC_ADCSYSCLK_DIV16    ADC clock = SYSCLK/16
         - RCC_ADCSYSCLK_DIV32    ADC clock = SYSCLK/32
         - RCC_ADCSYSCLK_DIV64    ADC clock = SYSCLK/64
return  none
void RCC_ConfigI2SClk(uint32_t i2s_clksrc)配置 I2S 时钟源(I2SCLK)。
源码注释
name    RCC_ConfigI2SClk.
fun     Configures the I2S clock source(I2SCLK).
param   i2s_clksrc(I2S clock source):
         - RCC_I2S_CLKSEL_SYS
         - RCC_I2S_CLKSEL_IOM
return  none
void RCC_ConfigUsart1Clk(uint32_t usart1_clksrc)配置 UART1 时钟源(UART1CLK)。
源码注释
name    RCC_ConfigUsart1Clk.
fun     Configures the UART1 clock source(UART1CLK).
param   usart1_clksrc(UART1 clock source):
         - RCC_USART1_CLKSEL_LSI
         - RCC_USART1_CLKSEL_LSE
         - RCC_USART1_CLKSEL_APB2
return  none
void RCC_ConfigUsart4Clk(uint32_t usart4_clksrc)配置 UART4 时钟源(UART4CLK)。
源码注释
name    RCC_ConfigUsart4Clk.
fun     Configures the UART4 clock source(UART4CLK).
param   usart4_clksrc(UART4 clock source):
         - RCC_USART4_CLKSEL_LSI
         - RCC_USART4_CLKSEL_LSE
         - RCC_USART4_CLKSEL_APB2
return  none
void RCC_ConfigRTCClk(uint32_t rtc_clksrc)配置 RTC 时钟源(RTCCLK)。
源码注释
name    RCC_ConfigRTCClk.
fun     Configures the RTC clock source(RTCCLK).
param   rtc_clksrc(RTC clock source):
         - RCC_RTC_CLKSEL_LSI
         - RCC_RTC_CLKSEL_LSE
         - RCC_RTC_CLKSEL_HSE_DIV128
return  none
void RCC_ConfigLowPowerClk(uint32_t lp_clksrc)配置低功耗时钟源(LPCLK)。
源码注释
name    RCC_ConfigLowPowerClk.
fun     Configures the LP clock source(LPCLK).
param   lp_clksrc(LP clock source):
         - RCC_LP_CLKSEL_LSI
         - RCC_LP_CLKSEL_LSE
return  none
void RCC_ConfigAtim1Clk(uint32_t atimer1_clksrc)配置 ATIM1 时钟源(ATIM1CLK)。
源码注释
name    RCC_ConfigAtim1Clk.
fun     Configures the ATIM1 clock source(ATIM1CLK).
param   atimer1_clksrc(ATIM1 clock source):
         - RCC_ATIM1_CLKSEL_SYS
         - RCC_ATIM1_CLKSEL_AHB
         - RCC_ATIM1_CLKSEL_PLL
return  none
void RCC_ConfigAtim2Clk(uint32_t atimer2_clksrc)配置 ATIM2 时钟源(ATIM2CLK)。
源码注释
name    RCC_ConfigAtim2Clk.
fun     Configures the ATIM2 clock source(ATIM2CLK).
param   atimer2_clksrc(ATIM2 clock source):
         - RCC_ATIM2_CLKSEL_SYS
         - RCC_ATIM2_CLKSEL_AHB
         - RCC_ATIM2_CLKSEL_PLL
return  none
void RCC_ConfigGtim1Clk(uint32_t gtimer1_clksrc)配置 GTIM1 时钟源(GTIM1CLK)。
源码注释
name    RCC_ConfigGtim1Clk.
fun     Configures the GTIM1 clock source(GTIM1CLK).
param   gtimer1_clksrc(GTIM1 clock source):
         - RCC_GTIM1_CLKSEL_SYS
         - RCC_GTIM1_CLKSEL_AHB
return  none
void RCC_ConfigGtim2Clk(uint32_t gtimer2_clksrc)配置 GTIM2 时钟源(GTIM2CLK)。
源码注释
name    RCC_ConfigGtim2Clk.
fun     Configures the GTIM2 clock source(GTIM2CLK).
param   gtimer2_clksrc(GTIM2 clock source):
         - RCC_GTIM2_CLKSEL_SYS
         - RCC_GTIM2_CLKSEL_AHB
return  none
void RCC_ConfigGtim3Clk(uint32_t gtimer3_clksrc)配置 GTIM3 时钟源(GTIM3CLK)。
源码注释
name    RCC_ConfigGtim3Clk.
fun     Configures the GTIM3 clock source(GTIM3CLK).
param   gtimer3_clksrc(GTIM3 clock source):
         - RCC_GTIM3_CLKSEL_SYS
         - RCC_GTIM3_CLKSEL_AHB
return  none
void RCC_ConfigGtim4Clk(uint32_t gtimer4_clksrc)配置 GTIM4 时钟源(GTIM4CLK)。
源码注释
name    RCC_ConfigGtim4Clk.
fun     Configures the GTIM4 clock source(GTIM4CLK).
param   gtimer4_clksrc(GTIM4 clock source):
         - RCC_GTIM4_CLKSEL_SYS
         - RCC_GTIM4_CLKSEL_AHB
return  none
void RCC_ConfigBtim1Clk(uint32_t btimer1_clksrc)配置 BTIM1 时钟源(BTIM1CLK)。
源码注释
name    RCC_ConfigBtim1Clk.
fun     Configures the BTIM1 clock source(BTIM1CLK).
param   btimer1_clksrc(BTIM1 clock source):
         - RCC_BTIM1_CLKSEL_LSI
         - RCC_BTIM1_CLKSEL_LSE
         - RCC_BTIM1_CLKSEL_APB1
return  none
void RCC_ConfigIOFilter(uint32_t ioflitclk_div)配置 GPIO 滤波器的工作速度。
源码注释
name    RCC_ConfigIOFilter.
fun     Configures the Speed of GPIO filter.
param   ioflitclk_div value:
        -0 ~ 0x3F
return  none
void RCC_ConfigATIM1Filter(uint32_t atim1flitclk_div)配置 ATIM1 滤波器的工作速度。
源码注释
name    RCC_ConfigATIM1Filter.
fun     Configures the Speed of ATIM1 filter.
param   atim1flitclk_div value:
        -0 ~ 0x1F
return  none
void RCC_ConfigATIM2Filter(uint32_t atim2flitclk_div)配置 ATIM2 滤波器的工作速度。
源码注释
name    RCC_ConfigATIM2Filter.
fun     Configures the Speed of ATIM2 filter.
param   atim2flitclk_div value:
        -0 ~ 0x1F
return  none
void RCC_ConfigGTIM1Filter(uint32_t gtim1flitclk_div)配置 GTIM1 滤波器的工作速度。
源码注释
name    RCC_ConfigGTIM1Filter.
fun     Configures the Speed of GTIM1 filter.
param   gtim1flitclk_div value:
        -0 ~ 0x1F
return  none
void RCC_ConfigGTIM2Filter(uint32_t gtim2flitclk_div)配置 GTIM2 滤波器的工作速度。
源码注释
name    RCC_ConfigGTIM2Filter.
fun     Configures the Speed of GTIM2 filter.
param   gtim2flitclk_div value:
        -0 ~ 0x1F
return  none
void RCC_ConfigGTIM3Filter(uint32_t gtim3flitclk_div)配置 GTIM3 滤波器的工作速度。
源码注释
name    RCC_ConfigGTIM3Filter.
fun     Configures the Speed of GTIM3 filter.
param   gtim3flitclk_div value:
        -0 ~ 0x1F
return  none
void RCC_ConfigGTIM4Filter(uint32_t gtim4flitclk_div)配置 GTIM4 滤波器的工作速度。
源码注释
name    RCC_ConfigGTIM4Filter.
fun     Configures the Speed of GTIM4 filter.
param   gtim4flitclk_div value:
        -0 ~ 0x1F
return  none
void RCC_ConfigMco(uint32_t MCO_source)选择从 MCO 引脚输出的时钟源。
源码注释
name   RCC_ConfigMco.
fun    Selects the clock source to output on MCO pin.
param  MCO_source(clock source to output):
         - RCC_MCO_NOCLK       No clock selected
         - RCC_MCO_LSI         LSI oscillator clock selected
         - RCC_MCO_LSE         LSE oscillator clock selected
         - RCC_MCO_SYSCLK      System clock selected
         - RCC_MCO_HSI         HSI oscillator clock selected
         - RCC_MCO_HSE         HSE oscillator clock selected
         - RCC_MCO_PLLDIV      PLL sysdiv clock selected
return  none.
void RCC_ConfigMcoClkPre(uint32_t MCO_PLL_prescaler)配置 MCO 的系统时钟与 HSI 时钟分频。
源码注释
name    RCC_ConfigMcoClkPre.
fun     Configures the MCO System and HSI clock prescaler.
param   MCO_PLL_prescaler specifies the MCO PLL clock prescaler.
        This parameter can be on of the following values:
          - RCC_MCO_CLK_DIV1
          - RCC_MCO_CLK_DIV2
          - RCC_MCO_CLK_DIV4
          - RCC_MCO_CLK_DIV8
          - RCC_MCO_CLK_DIV16
return  none.
void RCC_EnableHSEClockSecuritySystem(FunctionalState Cmd)使能 HSE 时钟安全系统。
源码注释
name    RCC_EnableHSEClockSecuritySystem.
fun     Enables the HSE Clock Security System.
param   Cmd:
          - ENABLE
          - DISABLE
return  none.
void RCC_EnableLSEClockSecuritySystem(FunctionalState Cmd)使能 LSE 时钟安全系统。
源码注释
name    RCC_EnableLSEClockSecuritySystem.
fun     Enables the LSE Clock Security System.
param   Cmd:
          - ENABLE
          - DISABLE
return  none.
void RCC_EnableHSECaptureRequest(FunctionalState Cmd)配置是否使能 HSE 定时器捕获请求。
源码注释
name    RCC_EnableHSECaptureRequest.
fun     Configure whether to enable HSE Timer Capture requests .
param   Cmd:
          - ENABLE
          - DISABLE
return  none.
void RCC_EnableLSECaptureRequest(FunctionalState Cmd)配置是否使能 LSE 定时器捕获请求。
源码注释
name    RCC_EnableLSECaptureRequest.
fun     Configure whether to enable LSE Timer Capture requests.
param   Cmd:
          - ENABLE
          - DISABLE
return  none.
void RCC_EnableEMCDetect(uint32_t EMC_type, FunctionalState Cmd)配置是否使能 EMC 检测。
源码注释
name    RCC_EnableEMCDetect.
fun     Configure whether to enable EMC detection.
param   EMC_type:
          - RCC_EMC_GB
          - RCC_EMC_GBN
param   Cmd:
          - ENABLE
          - DISABLE
return  none.
void RCC_ConfigEMCDetectLevel(uint32_t EMC_type, uint8_t level)配置 EMC 检测的电压阈值。
源码注释
name    RCC_ConfigEMCDetectLevel.
fun     Configures the voltage threshold detected by the EMC.
param   EMC_type:
          - RCC_EMC_GB
          - RCC_EMC_GBN
param   level (The input parameters must be the following values):
          - 0x0 ~ 0x3
return  none
void RCC_EnableEMCReset(uint32_t EMC_type, FunctionalState Cmd)配置 EMC 出错时是否产生复位请求。
源码注释
name    RCC_EnableEMCReset.
fun     Configure whether to generate a reset request when an EMC error occurs.
param   EMC_type:
          - RCC_EMC_GB
          - RCC_EMC_GBN
param   Cmd:
          - ENABLE
          - DISABLE
return  none.
void RCC_EnableEMCSwitch(uint32_t EMC_type, FunctionalState Cmd)配置是否使能 EMC 切换。
源码注释
name    RCC_EnableEMCSwitch.
fun     Configure whether to enable EMC switch.
param   EMC_type:
          - RCC_EMC_GB
          - RCC_EMC_GBN
param   Cmd:
          - ENABLE
          - DISABLE
return  none.
void RCC_EnableEMCGBInt(FunctionalState Cmd)配置是否使能 EMC GB。
源码注释
name    RCC_EnableEMCGBInt.
fun     Configure whether to enable EMC GB.
param   Cmd:
          - ENABLE
          - DISABLE
return  none.
void RCC_EnableEMCGBNInt(FunctionalState Cmd)配置是否使能 EMC GBN 中断。
源码注释
name    RCC_EnableEMCGBNInt.
fun     Configure whether to enable EMC GBN int.
param   Cmd:
          - ENABLE
          - DISABLE
return  none.
void RCC_EnableGlitchDetect(FunctionalState Cmd)配置是否使能 Glitch 检测。
源码注释
name    RCC_EnableGlitchDetect.
fun     Configure whether to enable Glitch.
param   Cmd:
          - ENABLE
          - DISABLE
return  none.
void RCC_ConfigGlitchDetectLevel(uint8_t level)配置 Glitch 检测的电压阈值。
源码注释
name    RCC_ConfigGlitchDetectLevel.
fun     Configures the voltage threshold detected by the Glitch.
param   level (The input parameters must be the following values):
        - 0x0 ~ 0x1F
return  none
void RCC_EnableGlitchReset(FunctionalState Cmd)配置是否使能 Glitch 复位。
源码注释
name    RCC_EnableGlitchReset.
fun     Configure whether to enable Glitch reset.
param   Cmd:
          - ENABLE
          - DISABLE
return  none.
void RCC_EnableGlitchSwitch(FunctionalState Cmd)配置是否使能 Glitch 切换。
源码注释
name    RCC_EnableGlitchSwitch.
fun     Configure whether to enable Glitch switch.
param   Cmd:
          - ENABLE
          - DISABLE
return  none.
void RCC_EnableGlitchInt(FunctionalState Cmd)配置是否使能 Glitch 中断。
源码注释
name    RCC_EnableGlitchInt.
fun     Configure whether to enable GLITCH int.
param   Cmd:
          - ENABLE
          - DISABLE
return  none.
void RCC_EnableM4LockupReset(FunctionalState Cmd)配置是否使能 M4 死锁复位。
源码注释
name    RCC_EnableM4LockupReset.
fun     Configure whether to enable M4 lockup reset.
param   Cmd:
          - ENABLE
          - DISABLE
return  none.
void RCC_EnablePwrUpStep(FunctionalState Cmd)使能上电台阶宽度。
源码注释
name    RCC_EnablePwrUpStep.
fun     Power up step width enable.
param   Cmd:
          - ENABLE
          - DISABLE
return  none.
void RCC_EnablePwrDownStep(FunctionalState Cmd)使能掉电台阶宽度。
源码注释
name    RCC_EnablePwrDownStep.
fun     Power down step width enable.
param   Cmd:
          - ENABLE
          - DISABLE
return  none.
void RCC_ConfigPwrUpStepWidth(uint32_t pwrupstepwidth)配置上电台阶宽度。
源码注释
name    RCC_ConfigPwrUpStepWidth.
fun     Configure power up step width.
param   pwrupstepwidth (power up step width, 0<pwrupstepwidth<=1023)
return  none
void RCC_ConfigPwrDownStepWidth(uint32_t pwrdownstepwidth)配置掉电台阶宽度。
源码注释
name    RCC_ConfigPwrDownStepWidth.
fun     Configure power down step width.
param   pwrdownstepwidth (power down step width, 0<pwrdownstepwidth<=1023)
return  none
void RCC_EnablePStepSf(FunctionalState Cmd)使能软件功耗台阶。
源码注释
name    RCC_EnablePStepSf.
fun     Enable the software power step.
param   Cmd:
          - ENABLE
          - DISABLE
return  none.
void RCC_PStepSfDiv(uint32_t pstepsfdiv)设置基于 PLL 时钟的软件控制省电台阶。
源码注释
name    RCC_PStepSfDiv.
fun   Software-Controlled Power-Saving Stages (Based on PLL Clock)
param   pstepsfdiv (power saving stage divider based on PLL clock):
         - RCC_PWRUP_PSTEPSFDIV_1
         - RCC_PWRUP_PSTEPSFDIV_2
         - RCC_PWRUP_PSTEPSFDIV_4
         - RCC_PWRUP_PSTEPSFDIV_8
         - RCC_PWRUP_PSTEPSFDIV_16
return  none
void RCC_ClearPwrDownStepFlag(void)清除掉电台阶标志。
源码注释
name    RCC_ClearPwrDownStepFlag.
fun     Clear pstep downstep flag.
return  none.
FlagStatus RCC_GetPwrDownStepFlag(void)获取 PLL 功耗台阶控制的掉电标志。
源码注释
name    RCC_GetPwrDownStepFlag.
fun     Get PLL Power Step Control down step flag.
return  FlagStatus:
          - SET
          - RESET
uint32_t RCC_GetPwrStepFsm(void)获取 PLL 功耗台阶控制的状态机状态。
源码注释
name    RCC_GetPwrStepFsm.
fun     Get PLL Power Step Control FSM.
return  Status:
        0  - START
        1  - UP step1
        3  - UP step2
        7  - UP step3
        15 - UP step4
        14 - STOP
        12 - DOWN step1
        8  - DOWN step2
        9  - DOWN step3
        11 - DOWN step4
void RCC_ConfigInt(uint32_t Interrupt, FunctionalState Cmd)使能指定的 RCC 中断。
源码注释
name    RCC_ConfigInt.
fun     Enables the specified RCC interrupts.
param   Interrupt(the RCC interrupt sources to be enabled):
         - RCC_ENABLE_LSESSI
         - RCC_ENABLE_PLLRDI
         - RCC_ENABLE_HSERDI
         - RCC_ENABLE_HSIRDI
         - RCC_ENABLE_LSERDI
         - RCC_ENABLE_LSIRDI
param   Cmd:
          - ENABLE
          - DISABLE
return  none
INTStatus RCC_GetIntStatus(uint32_t interrupt_flag)检查指定的 RCC 中断是否发生。
源码注释
name    RCC_GetIntStatus.
fun     Checks whether the specified RCC interrupt has occurred or not.
param   interrupt_flag(RCC interrupt source to check):
        -  RCC_INT_LSESSIF    LSE clock security system interrupt flag
        -  RCC_INT_CLKSSIF    HSE clock security system interrupt flag
        -  RCC_INT_PLLRDIF    PLL ready interrupt flag
        -  RCC_INT_HSERDIF    HSE ready interrupt flag
        -  RCC_INT_HSIRDIF    HSI ready interrupt flag
        -  RCC_INT_LSERDIF    LSE ready interrupt flag
        -  RCC_INT_LSIRDIF    LSI ready interrupt flag
return  The new state of RccInt
         - SET
         - RESET
void RCC_ClrIntPendingBit(uint32_t interrupt_clear)清除 RCC 的中断挂起位。
源码注释
name    RCC_ClrIntPendingBit.
fun     Clears the RCC's interrupt pending bits.
param   interrupt_clear specifies the interrupt pending bit to clear.
          -  RCC_CLR_LSESSIF    Clear LSE clock security system interrupt flag
          -  RCC_CLR_CLKSSIF    Clear HSE clock security system interrupt flag
          -  RCC_CLR_PLLRDIF    Clear PLL ready interrupt flag
          -  RCC_CLR_HSERDIF    Clear HSE ready interrupt flag
          -  RCC_CLR_HSIRDIF    Clear HSI ready interrupt flag
          -  RCC_CLR_LSERDIF    Clear LSE ready interrupt flag
          -  RCC_CLR_LSIRDIF    Clear LSI ready interrupt flag
return  none.

使用提示: 清除中断挂起位前先确认已读取对应状态,避免漏掉中断事件。

FlagStatus RCC_GetFlagStatus(uint8_t RCC_flag)检查指定的 RCC 标志是否置位。
源码注释
name    RCC_GetFlagStatus.
fun     Checks whether the specified RCC flag is set or not.
param   RCC_flag:
          - RCC_CTRL_FLAG_HSIRDF
          - RCC_CTRL_FLAG_HSERDF
          - RCC_CTRL_FLAG_PLLRDF
          - RCC_CTRL_FLAG_LSIRDF
          - RCC_CTRL_FLAG_LSERDF
          - RCC_CTRLSTS_FLAG_PORRSTF
          - RCC_CTRLSTS_FLAG_PINRSTF
          - RCC_CTRLSTS_FLAG_WWDGRSTF
          - RCC_CTRLSTS_FLAG_SFTRSTF
          - RCC_CTRLSTS_FLAG_IWDGRSTF
          - RCC_CTRLSTS_FLAG_GLITCHRSTF
          - RCC_CTRLSTS_FLAG_EMCGBRSTF
          - RCC_CTRLSTS_FLAG_EMCGBNRSTF
          - RCC_CTRLSTS_FLAG_LKUPRSTF
return  FlagStatus:
          - SET
          - RESET
void RCC_ClearResetFlag(void)清除 RCC 的复位标志。
源码注释
name    RCC_ClearResetFlag.
fun     Clears the RCC reset flags.
param   none
return  none
note  Clear the following flags:
          - RCC_CTRLSTS_FLAG_PORRSTF
          - RCC_CTRLSTS_FLAG_PINRSTF
          - RCC_CTRLSTS_FLAG_WWDGRSTF
          - RCC_CTRLSTS_FLAG_SFTRSTF
          - RCC_CTRLSTS_FLAG_IWDGRSTF
          - RCC_CTRLSTS_FLAG_GLITCHRSTF
          - RCC_CTRLSTS_FLAG_EMCGBRSTF
          - RCC_CTRLSTS_FLAG_EMCGBNRSTF
          - RCC_CTRLSTS_FLAG_LKUPRSTF

使用提示: 复位标志读取后应及时清除,否则无法区分下一次复位的来源。

5.17.3 Demo 示例说明

本节说明 RCC 的 1 个示例:RCC_ClockConfig 把系统时钟切换到 HSI、HSE 或 PLL 三种来源之一,并通过串口打印各时钟频率、从 MCO 引脚输出所选时钟。

5.17.3.1 RCC_ClockConfig 时钟树配置

工程目录: projects/n32g41x_EVAL/examples/RCC/RCC_ClockConfig/

复位后串口打印默认时钟频率→按编译期选择的时钟源切换系统时钟到 HSI、HSE 或 PLL→重新初始化串口并打印 SYSCLK、HCLK、PCLK1、PCLK2 频率→把 PA8 配为 MCO 复用输出并选择分频→主循环空转等待测量

项目 源码解读
功能 把系统时钟(SYSCLK)切换到 HSI、HSE 或 PLL 三种来源之一,切换后经串口打印 SYSCLK、HCLK、PCLK1、PCLK2 频率,并由 PA8 的 MCO 引脚输出所选时钟。
源码组成 src/main.c:main、PrintfClockInfo、MCO_Configuration 与 SetSysClockToHSI、SetSysClockToHSE、SetSysClockToPLL;
inc/main.h:时钟源选择与 MCO 引脚宏;
src/n32g41x_it.c、inc/n32g41x_it.h:中断服务函数;
readme.txt:功能、环境与使用说明;
MDK-ARM/、EWARM/:Keil 与 IAR 工程。
串口日志来自 bsp/src/log.c,延时来自 bsp/src/delay.c。
关键配置 时钟源:由 SYSCLK_SOURCE_SELECT 选择,默认 SYSCLK_SOURCE_PLL;
HSI 16 MHz、HSE 8 MHz,PLL 调用 SetSysClockToPLL(RCC_PLL_SRC_HSE, 72000000U),pllinpre 2、pllmul 36、plloutdiv RCC_PLL_OD_DIV_1、pllsysdiv ENABLE,得 SYSCLK 72 MHz、PCLK1 36 MHz;
MCO:引脚 PA8(GPIO_PIN_8、GPIOA),N32G412 GPIO_AF7、N32G415 GPIO_AF13,分频 HSI RCC_MCO_CLK_DIV2、HSE RCC_MCO_CLK_DIV1、PLL RCC_MCO_CLK_DIV8;
串口:TX PA9、115200。
程序与数据路径 main 先调用 PrintfClockInfo("After reset") 打印复位后的默认时钟,再按 SYSCLK_SOURCE_SELECT 调用 SetSysClockToHSI、SetSysClockToHSE 或 SetSysClockToPLL 重配时钟与总线分频,失败时打印 Clock configuration failure!;
随后 PrintfClockInfo 打印新频率,MCO_Configuration 打开 GPIO 时钟、把 PA8 配为复用推挽并设置 MCO 分频与时钟源,最后进入空 while(1)。
主要 API RCC_DeInit、RCC_EnableHsi、RCC_ConfigHse、RCC_ConfigPll、RCC_EnablePll、RCC_EnablePllSysclkDiv、RCC_ConfigHclk、RCC_ConfigPclk1、RCC_ConfigPclk2、RCC_ConfigSysclk、RCC_GetSysclkSrc、RCC_GetClocksFreqValue、RCC_ConfigMco、RCC_ConfigMcoClkPre
验证方法 复位运行后串口先打印 After reset 与默认时钟,再打印所选方式的时钟信息(PLL 方式为 HSE->PLL->SYSCLK, 72MHz 以及 SYSCLK、HCLK、PCLK1、PCLK2 四个频率值);
用示波器测量 PA8,HSI 方式为 8 MHz、HSE 方式为 8 MHz、PLL 方式为 9 MHz。
使用提示 支持 N32G412x8L7、N32G412xBL7、N32G415x8L7、N32G415xBL7;
开发环境为 KEIL MDK-ARM V5.34 或 IAR EWARM 8.50.1。
切换时钟源前需在 inc/main.h 修改 SYSCLK_SOURCE_SELECT;
PLL 方式要求 HSE_VALUE 为 8 MHz;
源码注释要求 MCO 输出不超过 20 MHz。
核心代码解读与移植

示例先由头文件的宏选定时钟来源与 MCO 引脚,再按 HSI、HSE 或 PLL 三种路径重配时钟与总线分频,最后打印频率并从 PA8 输出 MCO。

数据路径: SYSCLK_SOURCE_SELECT(inc/main.h)→ SetSysClockToHSI/SetSysClockToHSE/SetSysClockToPLL 配置时钟源、PLL 与总线分频 → RCC_ConfigSysclk 切换系统时钟 → RCC_GetClocksFreqValue → PrintfClockInfo 串口打印 → MCO_Configuration 把 PA8 配为复用输出

1. 用头文件宏集中时钟源与 MCO 的可调项。 inc/main.h 用 SYSCLK_SOURCE_SELECT 在 HSI、HSE、PLL 三种时钟源之间选择,并用一组宏给出 MCO 引脚的端口、引脚号与复用号。

inc/main.hinc/main.h
/* System clock source selection */
#define SYSCLK_SOURCE_HSI         0
#define SYSCLK_SOURCE_HSE         1
#define SYSCLK_SOURCE_PLL         2

#ifndef SYSCLK_SOURCE_SELECT
#define SYSCLK_SOURCE_SELECT      SYSCLK_SOURCE_PLL  /* Select sysclk source */
#endif

/* MCO pin definition: PA8 */
#define MCO_PIN                   GPIO_PIN_8
#define MCO_GPIO                  GPIOA

#if defined(N32G412)
#define MCO_AF                    GPIO_AF7
#elif defined(N32G415)
#define MCO_AF                    GPIO_AF13
#endif

改换时钟来源只需修改 SYSCLK_SOURCE_SELECT;换 MCO 引脚或型号时改 MCO_PIN、MCO_GPIO 与 MCO_AF,main.c 不用改动。

2. 按目标频率查表得到 PLL 参数与总线分频。 SetSysClockToPLL 用 SYS_freq 查表,为每个频率档位给出倍频、输出分频、PLLSYSDIV 与 FLASH 等待周期,再统一配置 HCLK、PCLK1、PCLK2。

src/main.csrc/main.c · SetSysClockToPLL
    switch (SYS_freq)
    {
        case 72000000:
            /* 4MHz * 36 = 144MHz(FVCO) / 1(PLLOD) /1 = 144MHz(Fpll) */
            latency   = FLASH_LATENCY_1;   /* 36MHz < SYSCLK <= 72MHz */
            pllmul    = 36;
            plloutdiv = RCC_PLL_OD_DIV_1;
            pllsysdiv = ENABLE;
            break;
        case 80000000:
            /* 4MHz * 40 = 160MHz(FVCO) / 2(PLLOD) /1 = 80MHz(Fpll) */
            latency   = FLASH_LATENCY_2;   /* 72MHz < SYSCLK <= 80MHz */
            pllmul    = 40;
            plloutdiv = RCC_PLL_OD_DIV_2;
            pllsysdiv = DISABLE;
            break;
        case 96000000:
            /* 4MHz * 48 = 192MHz(FVCO) / 2(PLLOD) /1 = 96MHz(Fpll) */
            latency   = FLASH_LATENCY_2;   /* 72MHz < SYSCLK <= 96MHz */
            pllmul    = 48;
            plloutdiv = RCC_PLL_OD_DIV_2;
            pllsysdiv = DISABLE;
            break;
        default:
            return ERROR;
    }
src/main.csrc/main.c · SetSysClockToPLL
    /* HCLK = SYSCLK */
    RCC_ConfigHclk(RCC_SYSCLK_DIV1);

    /* PCLK2 = HCLK */
    RCC_ConfigPclk2(RCC_HCLK_DIV1);

    /* PCLK1 = HCLK/2 */
    RCC_ConfigPclk1(RCC_HCLK_DIV2);

PLL 输入分频由时钟源决定:RCC_PLL_SRC_HSI 取 4、RCC_PLL_SRC_HSE 取 2;改用其它系统频率时,需同时核对 FLASH_SetLatency 的等待周期与 PCLK1、PCLK2 的分频是否满足手册的频率上限。

3. 使能 PLL 并切换到 PLL 作为系统时钟。 参数写入后依次使能 PLL、等待 PLLRDF 置位、配置 PLLSYSDIV,再把 SYSCLK 切到 PLL 并等待状态位确认切换完成,每步都用 timeout_value 计数防死等。

src/main.csrc/main.c · SetSysClockToPLL
    /* Configure PLL: source, input prescaler, multiplier, output prescaler */
    RCC_ConfigPll(PLL_src, pllinpre, pllmul, plloutdiv);

    /* Enable PLL */
    RCC_EnablePll(ENABLE);

    /* Wait till PLL is ready */
    while (RCC_GetFlagStatus(RCC_CTRL_FLAG_PLLRDF) != SET)
    {
        if ((timeout_value--) == 0)
        {
            return ERROR;
        }
    }
    
    /* Enable or Disable PLLSYSDIV (SYSCLK = FPLL / pllsysdiv) */
    RCC_EnablePllSysclkDiv(pllsysdiv);

    /* Select PLL as system clock source */
    RCC_ConfigSysclk(RCC_SYSCLK_SRC_PLL);

    /* Wait till PLL is used as system clock source */
    timeout_value = 0xFFFFFFFF;
    while (RCC_GetSysclkSrc() != RCC_SYSCLK_STS_PLL)
    {
        if ((timeout_value--) == 0)
        {
            return ERROR;
        }
    }

RCC_ConfigPll 必须在 RCC_EnablePll 之前调用,PLLSYSDIV 也要在 PLL 就绪后再配置;修改时钟源或倍频参数后要同步确认 PLL 输入频率、FVCO 与输出频率都落在手册给出的范围内。

4. 在 main 中选择分支并打印频率。 main 按 SYSCLK_SOURCE_SELECT 进入对应分支调用时钟配置函数,PLL 分支以 HSE 为源、目标 72 MHz,失败时打印提示,随后由 PrintfClockInfo 重新初始化串口并打印四个频率。

src/main.csrc/main.c · main
#elif SYSCLK_SOURCE_SELECT == SYSCLK_SOURCE_PLL
    /* Method 3: PLL as system clock (72MHz from HSE) */
    if (SetSysClockToPLL(RCC_PLL_SRC_HSE, 72000000U) == ERROR)
    {
        log_info("Clock configuration failure!\n");
    }
    PrintfClockInfo("HSE->PLL->SYSCLK, 72MHz");

    MCO_Configuration(RCC_MCO_SYSCLK, RCC_MCO_CLK_DIV8);
#endif
src/main.csrc/main.c · PrintfClockInfo
void PrintfClockInfo(const char* msg)
{
    log_init(); /* should reinit after sysclk changed */
    log_info("\n--------------------------------\n");
    log_info("%s:\n", msg);
    RCC_GetClocksFreqValue(&RCC_ClockFreq);
    log_info("SYSCLK: %d\n", RCC_ClockFreq.SysclkFreq);
    log_info("HCLK: %d\n", RCC_ClockFreq.HclkFreq);
    log_info("PCLK1: %d\n", RCC_ClockFreq.Pclk1Freq);
    log_info("PCLK2: %d\n", RCC_ClockFreq.Pclk2Freq);
    SysTick_Delay_Ms(1);/* Waiting for the log transmission to complete */
}

时钟改变后串口波特率随之变化,所以 PrintfClockInfo 每次都先调用 log_init 重新初始化;PLL 分支要求 HSE_VALUE 为 8 MHz,SetSysClockToPLL 在 HSE_VALUE 不等于 8000000 时直接返回 ERROR,换晶振时要同步修改该宏。

5. 从 PA8 输出 MCO 时钟。 MCO_Configuration 打开 GPIO 时钟,把 PA8 配为复用推挽输出,再写 MCO 分频与时钟源,示例三档分别输出 SYSCLK 的 2、1、8 分频。

src/main.csrc/main.c · MCO_Configuration
void MCO_Configuration(uint32_t MCO_source, uint32_t MCO_prescaler)
{
    GPIO_InitType GPIO_InitStructure;

    /* Enable GPIO clock */
    RCC_EnableAHBPeriphClk(RCC_AHB_PERIPH_GPIO, ENABLE);

    /* Configure MCO pin as alternate push-pull */
    GPIO_InitStruct(&GPIO_InitStructure);
    GPIO_InitStructure.Pin            = MCO_PIN;
    GPIO_InitStructure.GPIO_Mode      = GPIO_MODE_AF_PP;
    GPIO_InitStructure.GPIO_Pull      = GPIO_NO_PULL;
    GPIO_InitStructure.GPIO_Alternate = MCO_AF;
    GPIO_InitPeripheral(MCO_GPIO, &GPIO_InitStructure);

    /* Configure MCO prescaler */
    RCC_ConfigMcoClkPre(MCO_prescaler);

    /* Select MCO clock source */
    RCC_ConfigMco(MCO_source);
}

源码注释要求 MCO 输出不超过 20 MHz,改变 MCO 时钟源或分频、或改用 MCO 的其它引脚时,都要按这一限制重新核对分频。

对应源码: src/main.c、inc/main.h

5.17.4 注意与排障

场景 检查与建议 来源
系统时钟源切换与停机限制 复位后系统时钟默认为 HSI;
当时钟源被直接或经 PLL 间接用作系统时钟时该时钟源不能被停止。
切换前先确认目标时钟源的就绪位已置位,目标源未就绪时切换不会发生。
用户手册
PLL 参数配置与输入时钟切换 PLL 的时钟源、输入预分频、倍频与输出分频必须在使能 PLL 前配置,PLL 激活后这些参数不能修改;
切换 PLL 输入时钟源时,必须先使新时钟源就绪再关闭原时钟源。
用户手册
PLL 频率范围与 SYSCLK 上限 配置 PLL 分频与倍频时需满足 F_IN 与 F_IN/N 都在 8 MHz~32 MHz、F_VCO 在 96 MHz~160 MHz、F_PLL 在 64 MHz~160 MHz;
N32G412 的 SYSCLK 不超过 80 MHz,N32G415 不超过 96 MHz。
用户手册
ATIM 选用 PLL 时钟 ATIM1 或 ATIM2 的时钟源选为 PLL 时,需确保系统时钟源也为 PLL,并保证 F_PLL 不超过 144 MHz、SYSCLK 不超过 72 MHz。 用户手册
总线时钟上限 配置 RCC_CFG 的 APB2PRES、APB1PRES 时,N32G412 需确保 PCLK2 不超过 80 MHz、PCLK1 不超过 40 MHz;
N32G415 需确保 PCLK2 不超过 96 MHz、PCLK1 不超过 48 MHz。
用户手册
PLLSYSDIV 配置顺序 使能 PLL 前需先把 RCC_CFG.PLLSYSDIV 设为 0,等待 PLL 就绪后才可配置该位。 用户手册
LSE 使能与关闭时序 关闭 LSE 需等待 3 个 LSE 周期才完全关闭;
重新使能 LSE 需等待 1024 个 LSE 周期 RCC_CTRL.LSERDF 才置位。
用户手册
LSE 时钟安全系统 RCC_CTRL.LSECSSEN 一旦使能,只能通过系统复位或在检测到 LSE 故障后自动清除。 用户手册
Glitch 检测使能顺序 使能 RCC_EMCCTRL.GVDET 后需延时 10 μs 及以上,才可使能 GVRST 与 GVSW。 用户手册

N32G412、N32G415 系列勘误手册 V1.0.0(2026.6.30)的勘误列表只列出一行 A 版芯片条目,勘误链接列为空,未列出具体条目。

国民技术开发套件文档中心

按芯片系列在线查阅国民技术 MCU 开发套件的芯片资料、SDK、驱动 API 与 Demo 示例说明。

© 2026 国民技术股份有限公司 版权所有粤ICP备11069485号本站使用不含 Cookie 的匿名访问统计