NXP

KSDKV1.2 学习

2019-07-12 12:10发布

这里写图片描述 先用project generator 产生一个工程 打开之候main函数 int main(void) { // Configure board specific pin muxing hardware_init(); // Initialize UART terminal dbg_uart_init(); PRINTF(" Running the myProject1 project. "); for (;;) // Forever loop { __asm("NOP"); } } 先看hardware_init()函数 void hardware_init(void) { /* enable clock for PORTs */ CLOCK_SYS_EnablePortClock(PORTA_IDX); CLOCK_SYS_EnablePortClock(PORTB_IDX); CLOCK_SYS_EnablePortClock(PORTC_IDX); CLOCK_SYS_EnablePortClock(PORTD_IDX); CLOCK_SYS_EnablePortClock(PORTE_IDX); /* Init board clock */ BOARD_ClockInit(); } 前面几句是打开PORTx的clock Gate
BOARD_ClockInit()定义如下 /* Initialize clock. */ void BOARD_ClockInit(void) { /* Set allowed power mode, allow all. */ SMC_HAL_SetProtection(SMC, kAllowPowerModeAll); // Enable SMC_PMPROT AVLP,ALLS,AVLLS /* Setup board clock source. */ // Setup OSC0 if used. // Configure OSC0 pin mux. PORT_HAL_SetMuxMode(EXTAL0_PORT, EXTAL0_PIN, EXTAL0_PINMUX);//EXTAL0_PORT=PORTA,EXTAL0_PIN=18,EXTAL0_PINMUX=kPortPinDisabled,*set PTA18 as EXTAL0* PORT_HAL_SetMuxMode(XTAL0_PORT, XTAL0_PIN, XTAL0_PINMUX);//*set PTA19 as XTAL0* BOARD_InitOsc0(); // Setup RTC external clock if used. #if RTC_XTAL_FREQ // If RTC_CLKIN is connected, need to set pin mux. Another way for // RTC clock is set RTC_OSC_ENABLE_CONFIG to use OSC0, please check // reference manual for datails. PORT_HAL_SetMuxMode(RTC_CLKIN_PORT, RTC_CLKIN_PIN, RTC_CLKIN_PINMUX); #endif BOARD_InitRtcOsc(); /* Set system clock configuration. */ #if (CLOCK_INIT_CONFIG == CLOCK_VLPR) CLOCK_SYS_SetConfiguration(&g_defaultClockConfigVlpr); #else CLOCK_SYS_SetConfiguration(&g_defaultClockConfigRun); #endif } BOARD_InitOsc0()函数的定义如下: // OSC0 configuration. osc_user_config_t osc0Config = { .freq = OSC0_XTAL_FREQ, .hgo = MCG_HGO0, .range = MCG_RANGE0, .erefs = MCG_EREFS0, .enableCapacitor2p = OSC0_SC2P_ENABLE_CONFIG, .enableCapacitor4p = OSC0_SC4P_ENABLE_CONFIG, .enableCapacitor8p = OSC0_SC8P_ENABLE_CONFIG, .enableCapacitor16p = OSC0_SC16P_ENABLE_CONFIG, }; CLOCK_SYS_OscInit(0U, &osc0Config); BOARD_InitOsc0()这个函数用来配置OSCx_CR寄存器和MCG_C2寄存器
这里写图片描述 这里写图片描述 最核心的是调用CLOCK_SYS_SetConfiguration(&g_defaultClockConfigRun);函数,
它的参数为: const clock_manager_user_config_t g_defaultClockConfigRun = { .mcgConfig = { .mcg_mode = kMcgModePEE, // Work in PEE mode. .irclkEnable = true, // MCGIRCLK enable. .irclkEnableInStop = false, // MCGIRCLK disable in STOP mode. .ircs = kMcgIrcSlow, // Select IRC32k. .fcrdiv = 0U, // FCRDIV is 0. .frdiv = 3U, .drs = kMcgDcoRangeSelLow, // Low frequency range .dmx32 = kMcgDmx32Default, // DCO has a default range of 25% .pll0EnableInFllMode = false, // PLL0 disable .pll0EnableInStop = false, // PLL0 disalbe in STOP mode .prdiv0 = 0x1U, .vdiv0 = 0x0U, }, .simConfig = { .pllFllSel = kClockPllFllSelPll, // PLLFLLSEL select PLL. .er32kSrc = kClockEr32kSrcLpo, // ERCLK32K selection, use LPO. .outdiv1 = 1U, .outdiv4 = 3U, }, .oscerConfig = { .enable = true, // OSCERCLK enable. .enableInStop = false, // OSCERCLK disable in STOP mode. } };