一、编写代码
1、在bsp文件夹添加bsp_key.h和bap_key.c文件,并编写代码;
2、main.c中添加bsp_key.h头文件,并编写代码;
3、修改.vscode文件,添加key头文件路径;
4、修改Makefile文件,更改目标文件,并添加头文件路径;
5、编译、烧写文件。
二、加上清除BSS段,代码不运行
从 key.dis 文件可以看出,_bss_start = 0x87800291。
对于32位的SOC来说,一般访问是以4个字节访问的。0x0 . 0x4 . 0x8 . 0xc . … 访问的最后一个字节都是0,4,8,c,不可能是 1 这样不能被4除尽的数。因此会从0x87800290开始清除BSS段。然而0x87800290不要属于BSS段。所以我们需要对 _bss_start 进行4字节对齐。按照4字节对齐原理,_bss_start = 0x87800294。所以需要设置_bss_start为4字节对齐。设置在连接脚本 imx6u.lds中,方法为:
原文件为:
SECTIONS{
. = 0x87800000 ;
.text :
{
obj/start.o
*(.text)
}
.rodata ALIGN(4) : {*(.rodata)}
.data ALIGN(4) : {*(.data)}
__bss_start = .;
.bss ALIGN(4) : {*(.bss) *(COMMON)}
__bss_end = .;
}
设置以后为:
SECTIONS{
. = 0x87800000 ;
.text :
{
obj/start.o
*(.text)
}
.rodata ALIGN(4) : {*(.rodata)}
.data ALIGN(4) : {*(.data)}
. = ALIGN(4);
__bss_start = .;
.bss ALIGN(4) : {*(.bss) *(COMMON)}
__bss_end = .;
}
添加了:
. = ALIGN(4);
这是将定位计数器 “.” 设置为4字节。
设置以后重新编译,查看 key.dis 文件,则为:
此时程序运行没有问题了。