1、新建工程
在虚拟机中打开vscode软件,新建一个LEDC的文件夹,并保存工作区。
在LEDC文件夹中新建start.s
main.c
main.h
三个文件
srart.s
.global _start
_start:mrs r0, cpsrbic r0, r0, #0x1f orr r0, r0, #0x13 msr cpsr, r0 ldr sp, =0X80200000 b main
main.c
#include "main.h"
void clk_enable(void)
{CCM_CCGR0 = 0xffffffff;CCM_CCGR1 = 0xffffffff;CCM_CCGR2 = 0xffffffff;CCM_CCGR3 = 0xffffffff;CCM_CCGR4 = 0xffffffff;CCM_CCGR5 = 0xffffffff;CCM_CCGR6 = 0xffffffff;
}
void led_init(void)
{SW_MUX_GPIO1_IO03 = 0x5; SW_PAD_GPIO1_IO03 = 0X10B0; GPIO1_GDIR = 0X0000008; GPIO1_DR = 0X0;
}
void led_on(void)
{GPIO1_DR &&#61; ~(1<<3);
}
void led_off(void)
{GPIO1_DR |&#61; (1<<3);
}
void delay_short(volatile unsigned int n)
{while(n--){}
}
void delay(volatile unsigned int n)
{while(n--){delay_short(0x7ff);}
}
int main(void)
{clk_enable(); led_init(); while(1) { led_off(); delay(500); led_on(); delay(500); }return 0;
}
main.h
#ifndef __MAIN_H
#define __MAIN_H
#define CCM_CCGR0 *((volatile unsigned int *)0X020C4068)
#define CCM_CCGR1 *((volatile unsigned int *)0X020C406C)#define CCM_CCGR2 *((volatile unsigned int *)0X020C4070)
#define CCM_CCGR3 *((volatile unsigned int *)0X020C4074)
#define CCM_CCGR4 *((volatile unsigned int *)0X020C4078)
#define CCM_CCGR5 *((volatile unsigned int *)0X020C407C)
#define CCM_CCGR6 *((volatile unsigned int *)0X020C4080)
#define SW_MUX_GPIO1_IO03 *((volatile unsigned int *)0X020E0068)
#define SW_PAD_GPIO1_IO03 *((volatile unsigned int *)0X020E02F4)
#define GPIO1_DR *((volatile unsigned int *)0X0209C000)
#define GPIO1_GDIR *((volatile unsigned int *)0X0209C004)
#define GPIO1_PSR *((volatile unsigned int *)0X0209C008)
#define GPIO1_ICR1 *((volatile unsigned int *)0X0209C00C)
#define GPIO1_ICR2 *((volatile unsigned int *)0X0209C010)
#define GPIO1_IMR *((volatile unsigned int *)0X0209C014)
#define GPIO1_ISR *((volatile unsigned int *)0X0209C018)
#define GPIO1_EDGE_SEL *((volatile unsigned int *)0X0209C01C)#endif
2、编写Makefile
objs :&#61; start.o main.oledc.bin : $(objs)arm-linux-gnueabihf-ld -Ttext 0X87800000 $^ -o ledc.elfarm-linux-gnueabihf-objcopy -O binary -S ledc.elf $&#64;arm-linux-gnueabihf-objdump -D -m arm ledc.elf > ledc.dis%.o:%.carm-linux-gnueabihf-gcc -Wall -nostdlib -c -o $&#64; $< %.o:%.sarm-linux-gnueabihf-gcc -Wall -nostdlib -c -o $&#64; $<clean:rm -rf *.o ledc.bin ledc.elf ledc.dis
3、编译代码
编写好Makefile文件后就可以在终端输入make
指令来编译代码
编译没有错误后&#xff0c;会生成ledc.bin
ledc.dis
ledc.elf
main.o start.o
五个文件。
4、烧录程序
这里选择SD卡的模式。
首先要将imxdownload
文件复制到工程文件LEDC
中。你可以用命令行复制&#xff0c;也可以用鼠标右键复制都可以。如下图所示&#xff1a;
cp ../1_leds/imxdownload ./
将SD卡插入电脑
要保证ubuntu正确识别到SD卡。
然后使用ls /dev/sd* -l
查看SD卡设备
ls /dev/sd* -l
可以判断出SD卡设备为sdb
然后使用./imxdownload led.bin /dev/sdb
命令向SD卡烧录ledc.bin
文件。
./imxdownload ledc.bin /dev/sdb
烧录完成&#xff0c;拔掉SD卡&#xff0c;将SD卡插入到开发板中&#xff0c;接入电源&#xff0c;按下复位按键。之后就可以观察到LED每隔一秒钟闪烁一次。