【问题标题】:Timer C programming on beagleboard-xm with armstrong使用 armstrong 在 beagleboard-xm 上进行定时器 C 编程
【发布时间】:2013-06-03 15:21:58
【问题描述】:

下午好。 我的目标是编写一个简单的 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);
    }

}

【问题讨论】:

  • 安装内核头文件,如果这不能解决您的问题,请安装内核源代码。
  • 在您的代码中,您反复打开和关闭“/sys/class/gpio/gpio157/direction”文件。在 while() 之外打开该文件并在 while() 之后关闭它会快得多。然后在里面,你可以写它..每秒。但是,即便如此.. 请查看我对下面关于 setitimer/getitimer 链接的回答的评论。你应该使用它而不是 sleep()

标签: c linux timer beagleboard


【解决方案1】:

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

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

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

Linux Device Drivers, Third Edition

【讨论】:

  • 我添加了用于从用户空间操作 GPIO 引脚的程序。如您所见,我使用睡眠,因为我不知道如何请求定时器和编写中断例程。谢谢
  • 使用睡眠不是一个好主意。有一些计时器 API 允许您在 Linux 中使用计时器。像 setitimer/getitimer。见:man7.org/linux/man-pages/man2/setitimer.2.html
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-27
相关资源
最近更新 更多