博客
关于我
嵌入式Linux驱动学习之LED控制:基于AT91SAM9261EK
阅读量:565 次
发布时间:2019-03-09

本文共 1848 字,大约阅读时间需要 6 分钟。

基于AT91SAM9261EK开发板的嵌入式Linux LED驱动开发

为了实现对AT91SAM9261EK开发板上的两个LED灯进行控制,本文将介绍如何编写一个基于Linux的最简单LED驱动程序。

(1)编写嵌入式Linux LED驱动驱动程序需要控制PA13和PA14两个GPIO引脚。低电平状态亮,高电平状态灭。我们可以参考网上成功的驱动示例,进行适当修改。

(2)驱动程序的编写与用户程序的开发驱动程序需要遵循以下步骤编写:

  • 定义相关宏
  • 导入必要的头文件
  • 实现模块初始化与卸载函数
  • 实现文件操作接口函数
  • 注册字符设备

如下的Makefile用于编译驱动程序:

obj-m += LED_TEST.oCURRENT_PATH := $(shell pwd)ARM_LINUX_KERNEL := /home/AT91/Kernel/linux-2.6.32.2all:    $(MAKE) -C $(ARM_LINUX_KERNEL) \    SUBDIRS=$(CurrentValue) \    modulesclean:    rm -rf *.cmd *.o *.ko *.mod.c *.symvers *.order

用户程序的实现需要与驱动程序配合使用。程序将实现对LED状态的控制功能,具体代码如下:

#include 
#include
#include
#include
#define LED_DEV "/dev/SAM9261-LED_TEST"int main(int argc, char **argv) { int fd, ret, led_num, led_status; if (argc != 3 || sscanf(argv[1], "%d", &led_num) != 1 || sscanf(argv[2], "%d", &led_status) != 1) { printf("Please input correct parameters!\n"); printf("Usage:\n%s
\n", argv[0]); printf("Options:\n"); printf(" led_num - 1 for red led, 0 for blue led.\n"); printf(" led_status - 1 for ON, 0 for OFF.\n"); exit(1); } if ((led_status != 1 && led_status != 0) || (led_num != 0 && led_num != 1)) { printf("Error: The parameter value must be '0' or '1'!\n"); exit(1); } fd = open(LED_DEV, O_RDWR); if (fd < 0) { printf("Fail to open device '%s'!\n", LED_DEV); exit(1); } ret = ioctl(fd, led_status, led_num); if (ret < 0) { printf("Fail calling ioctl!\n"); exit(1); } close(fd); return 0;}

用户程序的Makefile编写如下:

all:    arm-none-linux-gnueabi-gcc LED_TEST_APP.c -o LED_APPclean:    rm -rf *.o LED_APP

编译命令为:

make

可以通过以下步骤完成开发板上的测试和应用:

  • 使用insmod LED_TEST.ko加载驱动程序。
  • 执行用户程序LED_APP 1 1(开启第一个LED)。
  • 执行LED_APP 1 0(关闭第一个LED)。
  • 执行LED_APP 0 1(开启第二个LED)。
  • 执行LED_APP 0 0(关闭第二个LED)。
  • 为了卸载驱动程序,使用rmmod LED_TEST.ko命令。

    通过以上步骤,可以实现对开发板上的两个LED灯进行简单的控制。

    转载地址:http://yzfpz.baihongyu.com/

    你可能感兴趣的文章
    private和protected能同时修饰成员变量吗_天天用注解你了解注解是怎么实现的吗?...
    查看>>
    privoxy Invalid header received from client.
    查看>>
    Privoxy代码下载
    查看>>
    Probabilistic-Programming-and-Bayesian-Methods-for-Hackers
    查看>>
    pytorch中的求和函数sum(axis=x)解释
    查看>>
    Problem F: 质心算法
    查看>>
    Problem N HDU 2612 Find a way (两次BFS求最值)
    查看>>
    Process /usr/libexec/gdu-notification-daemon was killed by signal 6 (SIGABRT)
    查看>>
    process.env.VUE_APP_BASE_API 获取不到
    查看>>
    Process.run() 和 Process.start() 之间的区别
    查看>>
    Processes
    查看>>
    Processing通过编程实现艺术设计_实现艺术和现实的交互---数据设计分析002
    查看>>
    ProcessOnLoading
    查看>>
    SpringBoot中集成screw(螺丝钉)实现数据库表结构文档生成
    查看>>
    PROFINET 模拟器使用教程
    查看>>
    Program type already present: android.support.v4.widget.EdgeEffectCompat
    查看>>
    PyTorch中文版官方教程来啦(附下载)
    查看>>
    Progress Kemp LoadMaster 远程命令执行漏洞复现(CVE-2024-1212)
    查看>>
    Project configuration is not up-to-date with pom.xml. Run Maven->Update Project
    查看>>
    Project Euler 15 Lattice paths
    查看>>