一、启动文件
.global _start //全局变量
_start:
//进入SVC模式
mrs r0, cpsr //将特殊寄存器 CPSR 里面的数据复制到 R0 中
bic r0, r0, #0x1f //00011111 将 r0 的低 5 位清零,也就是 cpsr 的 M0~M4
orr r0, r0, #0x13 //00010011 r0 或上 0x13,表示使用 SVC 模式
msr cpsr, r0 //将 r0 的数据写入到 cpsr_c 中
ldr sp, =0x80200000 //设置栈指针
b main //跳转到main函数
二、main.h
#ifndef __MAIN_H
#define __MAIN_H
#define uint8_t unsigned char
//<IMX6ULL参考手册.pdf>
/*
* CCM相关寄存器地址 699页
*/
#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)
/*
* IOMUX相关寄存器地址 MUX复用选择:1571页 PAD电气特性选择:1793页
*/
#define SW_MUX_GPIO1_IO03 *((volatile unsigned int *)0X020E0068)
#define SW_PAD_GPIO1_IO03 *((volatile unsigned int *)0X020E02F4)
/*
* GPIO1相关寄存器地址
*/
#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)
union GPIO_PAD
{
struct
{
unsigned char SRE : 1; //0--低速切换 1--高速切换 (高低电平切换速度,即上升沿和下降沿建立时间)
unsigned char Reserved3 : 2;
unsigned char DSE : 3; /* 0--关闭输出功能 1--R0驱动能力({260Ω,3V3},{150Ω,1V8},{240Ω,DDR})
2--R0/2 3--R0/3 4--R0/4 5--R0/5 6--R0/6 7--R0/7 */
unsigned char SPEED : 2; //0--低速50MHz 1/2--中速100MHz 3--高速200MHz (引脚速度)
unsigned char Reserved2 : 3;
unsigned char ODE : 1; //0--关闭开漏 1--开启开漏
unsigned char PKE : 1; //0--关闭Keeper 1--开启Keeper
unsigned char PUE : 1; //0--Keeper(IO供电NVCC_xxx关闭后保持状态) 1--Pull(拉着,不锁定状态)
unsigned char PUS : 2; //0--100K下拉电阻 1--47K上拉电阻 2--100K上拉电阻 3--22K上拉电阻
unsigned char HYS : 1; //0--关闭HYS 1--开启HYS
unsigned char Reserved1 : 7;
unsigned char Reserved;
} bitdata;
unsigned int PAD_Val;
unsigned char buff[4];
};
#endif // !__MAIN_H
三、main.c
#include "main.h"
//使能I.MX6U所有外设时钟
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;
}
//初始化LED对应的GPIO
void led_init(void)
{
//1、初始化IO复用
SW_MUX_GPIO1_IO03 = 0x05; //0101 GPIO1_IO03 of instance: gpio1
//2、配置IO属性
union GPIO_PAD gpio1_3;
gpio1_3.bitdata.HYS = 0; //输出引脚无需磁滞
gpio1_3.bitdata.PUS = 0; //下拉
gpio1_3.bitdata.PUE = 0; //保持
gpio1_3.bitdata.PKE = 1; //使能保持
gpio1_3.bitdata.ODE = 0; //关闭开漏
gpio1_3.bitdata.SPEED = 1; //100MHz
gpio1_3.bitdata.DSE = 6; //R0/6
gpio1_3.bitdata.SRE = 0; //低速变化
SW_PAD_GPIO1_IO03 = gpio1_3.PAD_Val;
//3、初始化GPIO 0--输入 1--输出
GPIO1_GDIR |= (1 << 3); /* GPIO1_IO03设置为输出 */
/* 4、设置GPIO1_IO03输出低电平,打开LED0 */
GPIO1_DR &= ~(1 << 3);
}
//LED控制 0--低电平 1--高电平
void led_control(uint8_t opt)
{
if (opt == 1)
{
GPIO1_DR |= (1 << 3); //将 GPIO1_DR 的 bit3 置 1
}
else if (opt == 0)
{
GPIO1_DR &= ~(1 << 3); //将 GPIO1_DR 的 bit3 清零
}
}
void delay_us(volatile unsigned int n)
{
while (n--)
{
}
}
void delay_ms(volatile unsigned int n)
{
while (n--)
{
delay_us(0x7ff);
}
}
int main(void)
{
clk_enable();
led_init();
while (1)
{
/* code */
led_control(1);
delay_ms(500);
led_control(0);
delay_ms(500);
}
return 0;
}
四、Makefile
objs := start.o main.o
ledc.bin:$(objs)
arm-linux-gnueabihf-ld -Timx6ul.lds -o ledc.elf $^
arm-linux-gnueabihf-objcopy -O binary -S ledc.elf $@
arm-linux-gnueabihf-objdump -D -m arm ledc.elf > ledc.dis
%.o:%.s
arm-linux-gnueabihf-gcc -Wall -nostdlib -c -O2 -o $@ $<
%.o:%.S
arm-linux-gnueabihf-gcc -Wall -nostdlib -c -O2 -o $@ $<
%.o:%.c
arm-linux-gnueabihf-gcc -Wall -nostdlib -c -O2 -o $@ $<
clean:
rm -rf *.o ledc.bin ledc.elf ledc.dis
四、imx6ul.lds
SECTIONS{
. = 0X87800000;
.text :
{
start.o
main.o
*(.text)
}
.rodata ALIGN(4) : {*(.rodata*)}
.data ALIGN(4) : { *(.data) }
__bss_start = .;
.bss ALIGN(4) : { *(.bss) *(COMMON) }
__bss_end = .;
}