上篇文章中我绕过Eclipse开发环境与交叉工具链。直接在DragonBoard 410c的终端上编写了程序并成功编译与运行,在终端输出!!hello字符,这意味着我们已经能基本控制DragonBoard 410c了。
但是各位应该还是对上一篇文章所做的事情不怎么感冒,毕竟仅仅是输出一个hello world而已,并没有什么新奇的效果。那么我们本文将直接对DragonBoard 410c的硬件进行控制,在Linux系统中调用DragonBoard 410c自带的LED模块并使其进行闪烁。如果你听到这里已经迫不及待了,那就和我进入到下面的课程吧。
这一部分大家可以查看上一章的教程配置debian并运行:
Linux系统中的LED处理在所有驱动控制中是最简单的形式, LED class允许开发者在用户空间对LED进行操作。 所有的LED显示与/sys/class/leds/文件夹下。我们可以在这个文件夹下控制LED的属性。 举例来说,LED的最大亮度取决于文件夹下的max_brightness文件。 该文件可以设置LED的亮度。只不过很多硬件中所带的LED没有亮度控制所以一般该文件都设置为非零值就OK。因此本文中我们就可以在C代码中直接引用该文件夹下的LED文件属性对LED进行直接控制。
在这个类下还包括了很多有意思的特性,比如LED的trigger事件。 一个trigger是led事件中的内核源。 Trigger既可以很简单也可以无比复杂。Trigger可以支持ide-disk、nand-disk以及shapsl-charge等,此处与本文内容无关,只是提及一下,如果打击有兴趣,可以在这个链接进行详细的学习。
/* ============================================================================ Name : BlinkyLED.c Author : ZhouJunyu Version : 0.0.1 Copyright : Your copyright notice Description : Simple Hardware access example that blinks user-LED4 ============================================================================ */
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
//定义控制LED的路劲
#define LED4 "/sys/class/leds/apq8016-sbc\:green\:user3/brightness"
int main( void ){
//获得对LED的亮度控制文件权限
int led4_fd = open( LED4, O_WRONLY);
if(led4_fd < 0){
printf("Could not open File: %s", LED4);
return 0;
}
int i;
for( i=0; i<10;i++){
//LED闪烁程序
write( led4_fd, "1", 2 ); //Turning the LED ON by writing 1 into the brightness file
sleep( 1 );
write( led4_fd, "0", 2 ); //Turning the LED OFF by writing 0 into the brightness file
sleep( 1 );
}
close(led4_fd);
}
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#define LED4 "/sys/class/leds/apq8016-sbc\:green\:user3/brightness"
int led4_fd = open( LED4, O_WRONLY);
if(led4_fd < 0){
printf("Could not open File: %s", LED4);
return 0;
}
int i;
for( i=0; i<10;i++){
//LED闪烁程序
write( led4_fd, "1", 2 ); //Turning the LED ON by writing 1 into the brightness file
sleep( 1 );
write( led4_fd, "0", 2 ); //Turning the LED OFF by writing 0 into the brightness file
sleep( 1 );
}
close(led4_fd);
接下来使用第一篇文章的方法在DragonBoard 410c中进行编译:
Gcc BlinkyLED –o hello
Chmod u+x BlinkyLED
Sudo ./hello
那么DragonBoard 410c就会按照固定的频率进行开关LED十次
开始运行
开始闪烁
程序退出
本次我们通过编写linux系统下最简单的LED控制,了解了嵌入式Linux系统中控制硬件的具体方式。当然,嵌入式中包含的硬件与模块千奇百怪,而且纷繁复杂。越到后面需要了解的内容越多,难度也越大。
欢迎各位经常与我交流DragonBoard 410c的开发心得