下午好。
我的目标是编写一个简单的C程序,该程序在我的beagleboard-xm上运行,每100毫秒打开一个gpio引脚上的led灯。我想使用计时器中断来实现这一点。
我正在尝试按照本教程
http://www.kunen.org/uC/beagle/omap_dmtimer.html
但是我很想念我需要一些内核操作吗?
我已经在beagleboard-xm上安装了 native gcc编译器,并在Windows 7上安装了带有Code Sourcery的交叉编译器,我可以构建简单的程序来操作led,但是两个编译器都无法识别本教程中使用的头文件:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/clk.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
#include <asm/io.h>
#include <mach/dmtimer.h>
#include <linux/types.h>
任何建议将不胜感激。
提前致谢
在这里,我发布了我用于GPIO操作的代码
#include <stdio.h>
#include<signal.h>
#include<unistd.h>
void sig_handler(int signo) {
if (signo == SIGINT) {
FILE *fp;
if ((fp = fopen("/sys/class/gpio/gpio157/direction", "w")) == NULL ) {
exit(1);
} else {
fprintf(fp, "low");
fclose(fp);
}
fp = fopen("/sys/class/gpio/unexport", "w");
fprintf(fp, "157");
fclose(fp);
printf("Closing and cleaning \n");
}
}
void main() {
FILE *fp;
printf("\n*************************************\n"
"* Welcome to PIN Blink program *\n"
"* ....blinking pin 22 on port GPIO *\n"
"* ....rate of 1 Hz............ *\n"
"**************************************\n");
if (signal(SIGINT, sig_handler) == SIG_ERR )
printf("\ncan't catch SIGINT\n");
fp = fopen("/sys/class/gpio/export", "w");
fprintf(fp, "157");
fclose(fp);
printf("...export file accessed, new pin now accessible\n");
while (1) {
if ((fp = fopen("/sys/class/gpio/gpio157/direction", "w")) == NULL ) {
printf("Error \n");
exit(1);
}
fprintf(fp, "high");
fclose(fp);
sleep(1);
if ((fp = fopen("/sys/class/gpio/gpio157/direction", "w")) == NULL ) {
printf("Error \n");
exit(1);
}
fprintf(fp, "low");
fclose(fp);
sleep(1);
}
}
最佳答案
如果要能够从用户空间操纵GPIO引脚,则必须构建一个内核驱动程序/模块才能为您完成此操作。然后,您可以通过ioctl,proc或其他内核API向驱动程序发送消息,以操纵GPIO引脚。
该教程看起来像一个内核驱动程序示例。您不能使用这些 header 构建常规的用户空间程序。您只需要构建一个示例“测试驱动程序”或做我上面所说的。
在线上有大量有关内核驱动程序的资源。您应该从这里开始。
Linux Device Drivers, Third Edition