开发环境
Ubuntu虚拟机
正点原子Linux-mini开发板
芯片I.MX6UL
内核Cortex-A7
参考手册
开发流程
新建
- 新建ledc文件夹
- 新建start.S, main.c, main.h文件
start.S还是汇编语言,用来初始化C语言环境,比如初始化DDR、设置堆栈指针SP等
编写
start.S ```css .global _start / 全局标号 / /*
- 描述: _start函数,程序从此函数开始执行,此函数主要功能是设置C
运行环境。 */ _start:
/ 进入SVC模式 / mrs r0, cpsr bic r0, r0, #0x1f / 将r0的低5位清零,也就是cpsr的M0~M4 / orr r0, r0, #0x13 / r0或上0x13,表示使用SVC模式 / msr cpsr, r0 / 将r0 的数据写入到cpsr_c中 /
ldr sp, =0X80200000 / 设置栈指针 / b main / 跳转到main函数 /
main.h
```c
#ifndef __MAIN_H
#define __MAIN_H
/*
* CCM相关寄存器地址
*/
#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相关寄存器地址
*/
#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)
#endif
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)
{
/* 1、初始化IO复用, 复用为GPIO1_IO03 */
SW_MUX_GPIO1_IO03 = 0x5;
/* 2、配置GPIO1_IO03的IO属性 */
SW_PAD_GPIO1_IO03 = 0x10B0;
/* 3、初始化GPIO, GPIO1_IO03设置为输出 */
GPIO1_GDIR = 0x0000008;
/* 4、设置GPIO1_IO03输出低电平,打开LED0 */
GPIO1_DR = 0x0;
}
void led_on(void)
{
GPIO1_DR &= ~(1<<3);
}
void led_off(void)
{
GPIO1_DR |= (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);
}
}
Makefile
objs := start.o main.o
ledc.bin:$(objs)
arm-linux-gnueabihf-ld -Ttext 0X87800000 -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 -o $@ $<
%.o:%.S
arm-linux-gnueabihf-gcc -Wall -nostdlib -c -o $@ $<
%.o:%.c
arm-linux-gnueabihf-gcc -Wall -nostdlib -c -o $@ $<
clean:
rm -rf *.o ledc.bin ledc.elf ledc.dis
烧录
chmod 777 imxdownload //给予 imxdownoad可执行权限,一次即可
./imxdownload ledc.bin /dev/sdd //下载到 SD卡