4

下午好。我的目标是编写一个简单的 c 程序,在我的 beagleboard-xm 上运行,并每 100 毫秒在 gpio 引脚上打开一个 LED。我想使用定时器中断来实现这一点。我正在尝试遵循本教程

http://www.kunen.org/uC/beagle/omap_dmtimer.html

但我想念一些东西。我需要一些内核操作吗?我在 beagleboard-xm 上安装了本机 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);
    }

}
4

1 回答 1

1

如果您希望能够从用户空间操作 GPIO 引脚,那么您必须构建一个内核驱动程序/模块来为您执行此操作。然后您可以通过 ioctl、proc 或其他内核 API 向您的驱动程序发送消息以操作 GPIO 引脚。

本教程看起来像一个内核驱动程序示例。您无法使用这些标头构建常规用户空间程序。您需要构建一个示例“测试驱动程序”或执行我上面所说的操作。

网上有大量关于内核驱动程序的资源。这是你应该开始的一个。

Linux 设备驱动程序,第三版

于 2013-06-03T15:29:01.743 回答