1.LED闪烁实验
参考野火官方例程
头文件:
#include "fsl_debug_console.h"
#include "board.h"
#include "pin_mux.h"
#include "clock_config.h"
#include "./led/bsp_led.h"
初始化开发板引脚:
BOARD_BootClockRUN();
void BOARD_BootClockRUN(void)
{
/* Init RTC OSC clock frequency. */
CLOCK_SetRtcXtalFreq(BOARD_XTAL32K_CLK_HZ);
/* Set XTAL 24MHz clock frequency. */
CLOCK_SetXtalFreq(BOARD_XTAL0_CLK_HZ);
/* Setting PeriphClk2Mux and PeriphMux to provide stable clock before PLLs are initialed */
CLOCK_SetMux(kCLOCK_PeriphClk2Mux, 0x1); /* Set PERIPH_CLK2 MUX to OSC */
CLOCK_SetMux(kCLOCK_PeriphMux, 0x1); /* Set PERIPH_CLK MUX to PERIPH_CLK2 */
DCDC->REG3 = (DCDC->REG3 & (~DCDC_REG3_TRG_MASK)) | DCDC_REG3_TRG(0x12);
#ifndef SKIP_SYSCLK_INIT
/* Init System PLL (PLL2). */
CLOCK_InitSysPll(&sysPllConfig_BOARD_BootClockRUN);
CLOCK_InitSysPfd(kCLOCK_Pfd2, 22);
CLOCK_InitSysPfd(kCLOCK_Pfd3, 22);
#endif
#ifndef XIP_EXTERNAL_FLASH
/* Init Usb1 PLL (PLL3). */
CLOCK_InitUsb1Pll(&usb1PllConfig_BOARD_BootClockRUN);
CLOCK_InitUsb1Pfd(kCLOCK_Pfd3, 18);
#endif
CLOCK_InitEnetPll(&enetPllConfig_BOARD_BootClockRUN);
/* Set AHB_PODF. */
CLOCK_SetDiv(kCLOCK_AhbDiv, 0x0);
/* Set ARM_PODF. */
CLOCK_SetDiv(kCLOCK_ArmDiv, 0x0);
/* Set IPG_PODF. */
CLOCK_SetDiv(kCLOCK_IpgDiv, 0x3);
/* Set preperiph clock source. */
CLOCK_SetMux(kCLOCK_PrePeriphMux, 0x3);
/* Set periph clock source. */
CLOCK_SetMux(kCLOCK_PeriphMux, 0x0);
/* Set PERIPH_CLK2_PODF. */
CLOCK_SetDiv(kCLOCK_PeriphClk2Div, 0x0);
/* Set periph clock2 clock source. */
CLOCK_SetMux(kCLOCK_PeriphClk2Mux, 0x0);
/* Set PERCLK_PODF. */
CLOCK_SetDiv(kCLOCK_PerclkDiv, 0x1);
/* Set per clock source. */
CLOCK_SetMux(kCLOCK_PerclkMux, 0x0);
/* Set USDHC1_PODF. */
CLOCK_SetDiv(kCLOCK_Usdhc1Div, 0x2);
/* Set Usdhc1 clock source. */
CLOCK_SetMux(kCLOCK_Usdhc1Mux, 0x0);
/* Set USDHC2_PODF. */
CLOCK_SetDiv(kCLOCK_Usdhc2Div, 0x2);
/* Set Usdhc2 clock source. */
CLOCK_SetMux(kCLOCK_Usdhc2Mux, 0x0);
#ifndef SKIP_SYSCLK_INIT
/* Set Semc alt clock source. */
CLOCK_SetMux(kCLOCK_SemcAltMux, 0x0);
/* Set SEMC_PODF. */
CLOCK_SetDiv(kCLOCK_SemcDiv, 0x1);
/* Set Semc clock source. */
CLOCK_SetMux(kCLOCK_SemcMux, 0x1);
#endif
#ifndef XIP_EXTERNAL_FLASH
/* Set FLEXSPI_PODF. */
CLOCK_SetDiv(kCLOCK_FlexspiDiv, 0x1);
/* Set Flexspi clock source. */
CLOCK_SetMux(kCLOCK_FlexspiMux, 0x0);
#endif
/* Set LPSPI_PODF. */
CLOCK_SetDiv(kCLOCK_LpspiDiv, 0x4);
/* Set Lpspi clock source. */
CLOCK_SetMux(kCLOCK_LpspiMux, 0x2);
/* Set TRACE_PODF. */
CLOCK_SetDiv(kCLOCK_TraceDiv, 0x2);
/* Set Trace clock source. */
CLOCK_SetMux(kCLOCK_TraceMux, 0x2);
/* Set SAI1_CLK_PRED. */
CLOCK_SetDiv(kCLOCK_Sai1PreDiv, 0x3);
/* Set SAI1_CLK_PODF. */
CLOCK_SetDiv(kCLOCK_Sai1Div, 0x1);
/* Set Sai1 clock source. */
CLOCK_SetMux(kCLOCK_Sai1Mux, 0x0);
/* Set SAI2_CLK_PRED. */
CLOCK_SetDiv(kCLOCK_Sai2PreDiv, 0x3);
/* Set SAI2_CLK_PODF. */
CLOCK_SetDiv(kCLOCK_Sai2Div, 0x1);
/* Set Sai2 clock source. */
CLOCK_SetMux(kCLOCK_Sai2Mux, 0x0);
/* Set SAI3_CLK_PRED. */
CLOCK_SetDiv(kCLOCK_Sai3PreDiv, 0x3);
/* Set SAI3_CLK_PODF. */
CLOCK_SetDiv(kCLOCK_Sai3Div, 0x1);
/* Set Sai3 clock source. */
CLOCK_SetMux(kCLOCK_Sai3Mux, 0x0);
/* Set LPI2C_CLK_PODF. */
CLOCK_SetDiv(kCLOCK_Lpi2cDiv, 0x0);
/* Set Lpi2c clock source. */
CLOCK_SetMux(kCLOCK_Lpi2cMux, 0x0);
/* Set CAN_CLK_PODF. */
CLOCK_SetDiv(kCLOCK_CanDiv, 0x1);
/* Set Can clock source. */
CLOCK_SetMux(kCLOCK_CanMux, 0x2);
/* Set SPDIF0_CLK_PRED. */
CLOCK_SetDiv(kCLOCK_Spdif0PreDiv, 0x1);
/* Set SPDIF0_CLK_PODF. */
CLOCK_SetDiv(kCLOCK_Spdif0Div, 0x7);
/* Set Spdif clock source. */
CLOCK_SetMux(kCLOCK_SpdifMux, 0x3);
/* Set FLEXIO1_CLK_PRED. */
CLOCK_SetDiv(kCLOCK_Flexio1PreDiv, 0x1);
/* Set FLEXIO1_CLK_PODF. */
CLOCK_SetDiv(kCLOCK_Flexio1Div, 0x7);
/* Set Flexio1 clock source. */
CLOCK_SetMux(kCLOCK_Flexio1Mux, 0x3);
/* Set Pll3 sw clock source. */
CLOCK_SetMux(kCLOCK_Pll3SwMux, 0x0);
/* Set UART_CLK_PODF. */
CLOCK_SetDiv(kCLOCK_UartDiv, 0x0);
/* Set Uart clock source. */
CLOCK_SetMux(kCLOCK_UartMux, 0x0);
SystemCoreClockUpdate();
}
延迟函数:
void delay(uint32_t count)
{
volatile uint32_t i = 0;
for (i = 0; i < count; ++i)
{
__asm("NOP");
}
}
GPIO配置:
/* 所有引脚均使用这个pad函数*/
#define LED_PAD_CONFIG_DATA (SRE_0_SLOW_SLEW_RATE| \
DSE_6_R0_6| \
SPEED_2_MEDIUM_100MHz| \
ODE_0_OPEN_DRAIN_DISABLED| \
PKE_0_PULL_KEEPER_DISABLED| \
PUE_0_KEEPER_SELECTED| \
PUS_0_100K_OHM_PULL_DOWN| \
HYS_0_HYSTERESIS_DISABLED)
/* 配置说明 : */
/* 转换速率: 转换速率慢
驱动强度: R0/6
带宽配置 : medium(100MHz)
开漏配置: 关闭
拉/保持器配置: 关闭
拉/保持器选择: 保持器(上面已关闭,配置无效)
上拉/下拉选择: 100K欧姆下拉(上面已关闭,配置无效)
滞回器配置: 关闭 */
//GPIO初始化函数
void LED_GPIO_Config(void)
{
LED_IOMUXC_CONFIG();
LED_PAD_CONFIG();
LED_MODE_CONFIG();
}
static void LED_IOMUXC_CONFIG(void)
{
//开启对应led口端口复用功能
IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B1_08_GPIO1_IO24,0U);
}
static void LED_PAD_CONFIG(void)
{
//设置管脚属性
IOMUXC_SetPinConfig(IOMUXC_GPIO_AD_B1_08_GPIO1_IO24,LED_PAD_CONFIG_DATA);
}
static void LED_MODE_CONFIG(void)
{
//GPIO模式设置
gpio_pin_config_t LED_Blue;
LED_Blue.direction = kGPIO_DigitalOutput;//数字输出
LED_Blue.outputLogic = 1;//高电平
LED_Blue.interruptMode = kGPIO_NoIntmode;//禁用中断
//GPIO初始化
GPIO_PinInit(GPIO1,24U,&LED_Blue);
}
main函数:
int main(void)
{
LED_GPIO_Config();
while(1)
{
digitalHi(GPIO1,24U);//LED亮
delay(70000000);
digitalLo(GPIO1,24U);//LED灭
delay(70000000);
digitalToggle(GPIO1,24U);//led状态反转
delay(70000000);
}
}
好了,LED亮起来了。