先用project generator 产生一个工程
打开之候main函数
int main(void)
{
hardware_init();
dbg_uart_init();
PRINTF("
Running the myProject1 project.
");
for (;;)
{
__asm("NOP");
}
}
先看hardware_init()函数
void hardware_init(void)
{
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);
BOARD_ClockInit();
}
前面几句是打开PORTx的clock Gate
BOARD_ClockInit()定义如下
void BOARD_ClockInit(void)
{
SMC_HAL_SetProtection(SMC, kAllowPowerModeAll);
PORT_HAL_SetMuxMode(EXTAL0_PORT, EXTAL0_PIN, EXTAL0_PINMUX);
PORT_HAL_SetMuxMode(XTAL0_PORT, XTAL0_PIN, XTAL0_PINMUX);
BOARD_InitOsc0();
#if RTC_XTAL_FREQ
PORT_HAL_SetMuxMode(RTC_CLKIN_PORT, RTC_CLKIN_PIN, RTC_CLKIN_PINMUX);
#endif
BOARD_InitRtcOsc();
#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.
}
}