void init_rcc(void) {
/************* 初始化外部时钟 ****************/
RCCP -> CR.HSEON = 0 ;
RCCP -> CR.HSEBYP = 0 ;
RCCP -> CR.HSERDY = 0 ;
RCCP -> CR.HSEON = 1 ;
while(!(RCCP -> CR.HSERDY));
/************** 外部总线配置 ***************/
RCCP -> CFGR.HPRE = 0 ; //AHB预分频
RCCP -> CFGR.PPRE2 = 0 ; //高速APB预分频(APB2)
RCCP -> CFGR.PPRE1 = 4 ; //低速APB预分频(APB1)
RCCP -> CFGR.ADCPRE = 2 ; /* ADCCLK = PCLK2/6 ADC转换速率:72M/6 = 12MHZ */
/************** FLASH存储设置 ***************/
FLASH -> ACR &= ((u32)0x00000038); //清零某些位 /* Flash 2 wait state */
FLASHP -> ACR.LATENCY = 2; //72mhz,2周期延时
FLASHP -> ACR.PRFTBE = 1; //预取缓冲区使能
/************** PLL时钟设置 ***************/
RCCP -> CFGR.PLLXTPRE = 0 ; //HSE作为时钟输入,不分频
RCCP -> CFGR.PLLSRC = 1 ; //PLL时钟源选择
RCCP -> CFGR.PLLMUL = 4 ; //PLL倍频
/************** PLL时钟使能 ***************/
RCCP -> CR.PLLON = 1 ;
while(!(RCCP -> CR.PLLRDY));
/************** 系统时钟切换 ***************/
RCCP -> CFGR.SW = 0 ;
RCCP -> CFGR.SW = 2 ;
while((RCCP -> CFGR.SWS) != 2);
/************** 外设时钟使能 ***************/
RCC -> APB2ENR |= IOPA_EN | IOPB_EN | IOPC_EN | IOPD_EN | IOPE_EN
| USART1_EN;
RCC -> APB1ENR = TIM1_EN | TIM2_EN | TIM3_EN | TIM4_EN | TIM5_EN
| TIM6_EN | TIM7_EN;
}
/************* APB2时钟使能 ****************/
#define AFIO_EN (uint32_t)1 << 0
#define IOPA_EN (uint32_t)1 << 2
#define IOPB_EN (uint32_t)1 << 3
#define IOPC_EN (uint32_t)1 << 4
#define IOPD_EN (uint32_t)1 << 5
#define IOPE_EN (uint32_t)1 << 6
#define IOPF_EN (uint32_t)1 << 7
#define IOPG_EN (uint32_t)1 << 8
#define ADC1_EN (uint32_t)1 << 9
#define ADC2_EN (uint32_t)1 << 10
#define TIM1_EN (uint32_t)1 << 11
#define SPI1_EN (uint32_t)1 << 12
#define TIM8_EN (uint32_t)1 << 13
#define USART1_EN (uint32_t)1 << 14
#define ADC3_EN (uint32_t)1 << 15
/************* APB1时钟使能 ****************/
#define TIM2_EN (uint32_t)0x01
#define TIM3_EN (uint32_t)0x02
#define TIM4_EN (uint32_t)0x04
#define TIM5_EN (uint32_t)0x08
#define TIM6_EN (uint32_t)0x10
#define TIM7_EN (uint32_t)0x20
#define WWDG_EN (uint32_t)0x800
#define SPI2_EN (uint32_t)0x4000
#define SPI3_EN (uint32_t)0x8000
#define USART2_EN (uint32_t)0x20000
#define USART3_EN (uint32_t)0x40000
#define USART4_EN (uint32_t)0x80000
#define USART5_EN (uint32_t)0x100000
#define I2C1_EN (uint32_t)0x200000
#define I2C2_EN (uint32_t)0x400000
#define USB_EN (uint32_t)0x800000
#define CAN_EN (uint32_t)0x2000000
#define BKP_EN (uint32_t)0x8000000
#define PWR_EN (uint32_t)0x10000000
#define DAC_EN (uint32_t)0x20000000
/************* APB时钟使能 ****************/
#define DMA1_EN (uint32_t)1 << 0
#define DMA2_EN (uint32_t)1 << 1
#define SRAM_EN (uint32_t)1 << 2
#define FLITF_EN (uint32_t)1 << 4
#define CRC_EN (uint32_t)1 << 6
#define FSMC_EN (uint32_t)1 << 8
#define SDIO_EN (uint32_t)1 << 10
阅读(2255) | 评论(0) | 转发(0) |