c - 使用 armstrong 在 beagleboard-xm 上进行定时器 C 编程

标签 c linux timer beagleboard

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

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

但是我想念一些东西。我需要一些内核操作吗? 我在 beagleboard-xm 上安装了 native gcc 编译器,在 Windows 7 上安装了带有 Code Sourcery 的交叉编译器,我可以构建简单的程序来操作 LED,但两个编译器都无法识别教程中使用的 header :

#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

关于c - 使用 armstrong 在 beagleboard-xm 上进行定时器 C 编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16900238/

相关文章:

c - Windows 7中的Geany IDE-执行C程序时出错

C 中的编译错误只是因为缩进?

linux - 如何替换 Linux 输出中的列数据 - 可能使用 awk sed 等

php - 如何使倒数计时器在页面刷新时不重置

linux - 在 Linux 中使用计时器停止内核线程

c - Mongodb-c-driver 工作后出现段错误

c++ - 使用信号中止功能实现

linux - 为什么waitpid(2)可以指定非子进程?

VB.NET 在自己的 "New"语句中的 lambda 表达式中使用自定义计时器 - 可以吗?

c++ - 如何在 gcc 中使用 Intel 的数学函数库?