内核版本:linux2.6.22.6 硬件平台:JZ2440

驱动源码 final_key.c :

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/irq.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#include <linux/poll.h> static struct class *key_int_class;
static struct class_device *key_int_class_device; volatile unsigned long *GPFCON=NULL;
volatile unsigned long *GPFDAT=NULL;
volatile unsigned long *GPGCON=NULL;
volatile unsigned long *GPGDAT=NULL; static struct fasync_struct fasync_key; //定义一个 fsync 结构体 struct pin_desc
{
unsigned int pin;
unsigned int key_val;
}; static unsigned char key_val;
static volatile int ev_press=; static struct pin_desc pin_desc_array[]={{S3C2410_GPF0,0x01},{S3C2410_GPF2,0X02},{S3C2410_GPG3,0x03},{S3C2410_GPG11,0x04}}; static DECLARE_WAIT_QUEUE_HEAD(wait_key); //定义一个信号量
static DECLARE_MUTEX(key_lock); //定义定时器队列的结构体
static struct timer_list key_timer; static struct pin_desc *desc_id; static irqreturn_t key_handler(int irq, void *dev_id)
{
desc_id = (struct pin_desc *)dev_id;
mod_timer(&key_timer,jiffies+HZ/);
return IRQ_RETVAL(IRQ_HANDLED);
} static int key_drv_open(struct inode *inode,struct file *file)
{
//获取信号量
down(&key_lock); request_irq(IRQ_EINT0, key_handler,IRQT_BOTHEDGE,"KEY1", &pin_desc_array[]);
request_irq(IRQ_EINT2, key_handler,IRQT_BOTHEDGE,"KEY2", &pin_desc_array[]);
request_irq(IRQ_EINT11, key_handler,IRQT_BOTHEDGE,"KEY3",&pin_desc_array[]);
request_irq(IRQ_EINT19, key_handler,IRQT_BOTHEDGE,"KEY4",&pin_desc_array[]); return ;
} ssize_t key_drv_read(struct file *file,const char __user *buf,size_t count,loff_t *ppos)
{
if (count != )
return -EINVAL; wait_event_interruptible(wait_key,ev_press);
ev_press=;
copy_to_user(buf,&key_val,); return ;
} static int key_drv_close(struct inode *inode,struct file *file)
{
free_irq(IRQ_EINT0, &pin_desc_array[]);
free_irq(IRQ_EINT2, &pin_desc_array[]);
free_irq(IRQ_EINT11, &pin_desc_array[]);
free_irq(IRQ_EINT19, &pin_desc_array[]); //释放信号量
up(&key_lock);
return ;
} unsigned int key_drv_poll(struct file *file,poll_table *wait)
{
unsigned int mask=;
poll_wait(file,&wait_key,wait); if(ev_press) mask |= POLLIN | POLLRDNORM; return mask;
} static int init_fasync(int fd,struct file *file,int on) // 初始化FASYNC (异步通知)结构体
{
printk("init fasync struct...\n");
return fasync_helper(fd,file,on,&fasync_key);
} static struct file_operations key_drv_mode=
{
.owner = THIS_MODULE,
.open = key_drv_open,
.read = key_drv_read,
.release = key_drv_close,
.poll = key_drv_poll,
.fasync = init_fasync,
}; static void key_timer_function(unsigned long data)
{
struct pin_desc *pindesc = desc_id;
unsigned int pinval=; if(!pindesc) return ; pinval = s3c2410_gpio_getpin(pindesc->pin); if(pinval) key_val = 0x08 | pindesc->key_val;
else key_val = pindesc->key_val; kill_fasync(&fasync_key,SIGIO,POLL_IN); //发生中断后 向结构体里的PID进程 发送 SIGIO 信号 wake_up_interruptible(&wait_key);
ev_press = ;
} int major=;
static int key_drv_init(void)
{
//初始化定时器
init_timer(&key_timer);
key_timer.function = key_timer_function;
add_timer(&key_timer); major = register_chrdev(,"final_key",&key_drv_mode); // /proc/devices key_int_class = class_create(THIS_MODULE,"key_class");
key_int_class_device = class_device_create(key_int_class,NULL,MKDEV(major,),NULL,"final_key"); // /dev/key_int_drv GPFCON=(volatile unsigned long *)ioremap(0x56000050,);
GPFDAT=GPFCON+;
GPGCON=(volatile unsigned long *)ioremap(0x56000060,);
GPGDAT=GPGCON+; return ;
} static void key_drv_exit(void)
{
unregister_chrdev(major,"final_key"); class_device_unregister(key_int_class_device);
class_destroy(key_int_class);
iounmap(GPFCON);
iounmap(GPGCON); } module_init(key_drv_init);
module_exit(key_drv_exit);
MODULE_LICENSE("GPL");

测试应用程序 final_key_test.c :

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h> int fd=; void act_fun(void) // 捕获信号响应函数
{
unsigned char key_val=;
read(fd,&key_val,);
printf("key_val= %d\n",key_val);
} int main(int argc,char **argv)
{
unsigned char key_val=;
int oflags=; int ret;
struct pollfd fds[]; fd = open("/dev/final_key",O_RDWR); if(fd <)
{
printf("error: can't open device :/dev/final_key\n");
return -;
} // signal(SIGIO,act_fun); //捕获信号 // fcntl(fd,F_SETOWN,getpid()); //应用程 序用 fcntl 告诉fd驱动 当前应用程序的PID
// oflags = fcntl(fd,F_GETFL); // 获取 fd驱动的 状态旗标
// fcntl(fd,F_SETFL,oflags|FASYNC);//更新oflags while()
{
read(fd,&key_val,);
printf("key_val= %d\n",key_val);
// sleep(5);
}
return ;
}

Makefile文件:

KER_DIR=/work/systems/kernel/linux-/linux-2.6.22.6

all:
make -C $(KER_DIR) M=`pwd` modules clean:
make -C $(KER_DIR) M=`pwd` modules clean
rm -fr moudles.order obj-m +=final_key.o

集齐所有机制的按键控制LED驱动的更多相关文章

  1. 按键控制led驱动

    内核版本:linux2.6.22.6 硬件平台:JZ2440 驱动源码 key_drv.c : #include<linux/module.h> #include<linux/ker ...

  2. 51单片机-独立按键控制led矩阵的左移和右移

    51单片机学习 独立按键 控制led灯光矩阵的左移和右移 开发板采用的是普中的A2学习开发板,具体的代码如下: typedef unsigned int u16; void delay(u16 tim ...

  3. 外部按键 控制 LED 中断 (参考 http://www.oschina.net/question/565065_115196?sort=time )

    转帖: http://www.oschina.net/question/565065_115196?sort=time 实验目的: mini2440开发板上有6个按键,将其中的前4个按键设为外部中断方 ...

  4. CC2530学习路线-基础实验-GPIO 按键控制LED灯亮灭(2)

    目录 1.前期预备知识 1.1 新大陆Zigbee模块按键电路图 1.2 CC2530相关寄存器 1.3 CC2530中断走向图 1.4 使用C语言为51单片机编写中断程序 1.5 *函数指针 2. ...

  5. 基于Arduino的按键控制LED实验

    I/O 口的意思即为INPUT 接口和OUTPUT 接口,到目前为止我们设计的小灯实验都还只是应用到Arduino 的I/O 口的输出功能,这个实验我们来尝试一下使用Arduino的I/O 口的输入功 ...

  6. 【嵌入式】——arm裸机开发 step by step 之 按键控制 LED 和 蜂鸣器

    一.arm-9 TQ2440 key.h #ifndef __KEY_H__ #define __KEY_H__ #define GPFCON (*(volatile unsigned long *) ...

  7. 按键控制LED灯-ESP32中断处理

    #include <driver/gpio.h> #include <esp_task_wdt.h> #include <freertos/FreeRTOS.h> ...

  8. 独立按键控制led灯

    #include "regx51.h"typedef unsigned int u16; void delay_us(u16 time){ while(time--){} }voi ...

  9. 安卓控制LED驱动编写

    开发平台 * 芯灵思SinlinxA33开发板 淘宝店铺: https://sinlinx.taobao.com/ 嵌入式linux 开发板交流 QQ:641395230 打开Android Stud ...

随机推荐

  1. 聊聊模板方法模式,装饰器模式以及AOP

    在软件系统设计的时候,我们需要把一个大的系统按照业务功能进行拆分,做到高内聚.低耦合. 但是呢,拆分之后会产生一些通用性的东西,比如日志,安全,事务,性能统计等,这些非功能性需求,横跨多个模块.最lo ...

  2. CSS让页面平滑滚动

    我们以往实现平滑滚动往往用的是jQuery, 如实现平滑回到顶部,就写如下代码: $('.js_go_to_top').click(function () { $(".js_scroll_a ...

  3. Mysql系列一:SQL入门

    csdn博客搬迁 连接数据库:1.在dos窗口下,进入数据库的安装目录的bin目录下,使用mysqld命令启动数据库服务,或者在计算机的服务里面启动mysql服务2.另外打开一个dos窗口,进入数据库 ...

  4. Java如何计数替换字符串中第一次出现的子字符串?

    在Java编程中,如何拆分正则表达式和字符串? 以下示例演示如何使用Matcher类的replaceFirst()方法替换字符中指定的子字符串的首次出现. package com.yiibai; im ...

  5. [UI] 06 - jQuery

    前言 From : http://www.runoob.com/jquery/jquery-intro.html Ref: jQuery 实例 一.什么是 jQuery ? jQuery是一个Java ...

  6. 【SQLSERVER】How to check current pool size

    SELECT des.program_name , des.login_name , des.host_name , COUNT(des.session_id) [Connections] FROM ...

  7. Android ListView只加载当前屏幕内的图片(解决list滑动时加载卡顿)

    最近在做ListView分页显示,其中包括图片 和文字(先下载解析文字内容,再异步加载图片)发现每次点击下一页后,文字内容加载完毕,马上向下滑动,由于这时后台在用线程池异步下载图片,我每页有20条,也 ...

  8. websql的使用/phonegap操作数据库 sqlite

    对websql的常用操作进行了一个封装,项目是基于phonegap的,不过phonegap默认已经集成了数据库操作的插件,所以无需再配置什么,直接用就可以了: /** *数据库操作辅助类,定义对象.数 ...

  9. 深度学习之windows安装tensorflow

    1. 安装python3.5 2. 下载tensorflow-1.1.0rc2-cp35-cp35m-win_amd64.whl 3. pip install tensorflow-1.1.0rc2- ...

  10. vs2017更新后web项目部分后台代码类没有颜色,也没有自动提示输入功能

    vs2017有的版本更新后默认.net framework框架是.net framework4.6.1,将项目的.net framework框架更改为4.6.1,颜色和自动提示出现