Cubieboard2裸机开发之(三)C语言操作LED

前言

前面通过汇编语言点亮LED,代码虽然简单,但并不是很直观。这次使用熟悉的C语言来控制LED,但是需要注意的地方有两点,第一,要想使用C语言,首先需要在调用C语言代码之前设置好堆栈;第二,调用C语言函数时,是需要相对跳转还是绝对地址跳转,还是两者都可以,这就需要知道代码是否运行在链接地址处,是位置无关的还是位置有关的。从前面分析可以知道,我们的代码是运行在链接地址处的,因此可以用直接进行函数的调用。

一、目的

使用C语言的方式操作板载LED。

二、源代码说明

start.S文件。首先禁止CPU的IRQ和FIQ,设置为管理模式,然后设置堆栈指针,最后调用C语言的main函数。

 /*
* (C) Copyright conan liang <lknlfy@163.com>
*
*/ /* global entry point */
.globl _start
_start: b reset reset:
/* disable IRQ & FIQ, set the cpu to SVC32 mode */
mrs r0, cpsr
and r1, r0, #0x1f
teq r1, #0x1a
bicne r0, r0, #0x1f
orrne r0, r0, #0x13
orr r0, r0, #0xc0
msr cpsr, r0
/* setup stack, so we can call C code */
ldr sp, =( * )
/* call main function */
bl main
loop:
b loop

main.c文件。首先初始化LED所在IO管脚,设置为输出功能,并且输出低电平,即一开始两个LED是熄灭的。

 #include "led.h"

 /* just for test */
static void delay(void)
{
unsigned int i; for (i = ; i < ; i++);
} /* C code entry point */
int main(void)
{
/* init PIO */
led_init(); while () {
/* two LEDs on */
set_led_on();
delay();
/* two LEDs off */
set_led_off();
delay();
} return ;
}

led.c文件。LED驱动程序,一个初始化函数,一个使两个LED同时点亮函数,一个同时使两个LED同时熄灭函数。

 #include "led.h"
#include "io.h" /* set two LEDs on */
void set_led_on(void)
{
unsigned int tmp; /* PH20 and PH21 output 1 */
tmp = readl(PH_DAT);
tmp |= (0x1 << );
tmp |= (0x1 << );
writel(tmp, PH_DAT);
} /* set two LEDs off */
void set_led_off(void)
{
unsigned int tmp; /* PH20 and PH21 output 0 */
tmp = readl(PH_DAT);
tmp &= ~(0x1 << );
tmp &= ~(0x1 << );
writel(tmp, PH_DAT);
} /* init PIO */
void led_init(void)
{
unsigned int tmp; /* set PH20 and PH21 output */
tmp = readl(PH_CFG2);
tmp &= ~(0x7 << );
tmp &= ~(0x7 << );
tmp |= (0x1 << );
tmp |= (0x1 << );
writel(tmp, PH_CFG2);
/* set PH20 and PH21 output 0 */
tmp = readl(PH_DAT);
tmp &= ~(0x1 << );
tmp &= ~(0x1 << );
writel(tmp, PH_DAT);
}

三、验证

使用arm-linux-gnueabihf工具编译后生成led_c.b文件,再使用mksunxiboot工具在led_c.b文件前面加上一个头部,最终生成led_c.bin文件,使用以下命令将led_c.bin文件烧写到TF中:

#sudo dd if=./led_c.bin of=/dev/sdb bs=1024 seek=8

将TF卡插入Cubieboard2,上电即可看到两个LED同时闪烁。效果不好用图片展示,因此就不上图了。

上一篇:ORA-12516 TNS监听程序找不到符合协议堆栈要求的可用处理程序


下一篇:[Locked] Binary Tree Upside Down