Linux 驱动——Button驱动2
button_drv.c驱动文件:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <asm/io.h> //包含iomap函数iounmap函数
#include <asm/uaccess.h> //包含copy_from_user函数
#include <linux/device.h> //包含类相关的处理函数
#include <asm/arch/regs-gpio.h> //包含S3C2410_GPF0等相关的
#include <linux/irq.h> //包含IRQ_HANDLED、IRQ_TYPE_EDGE_RISING
#include <asm-arm/irq.h> //包含IRQT_BOTHEDGE触发类型
#include <linux/interrupt.h>request_irq、free_irq函数
#include <linux/delay.h>
#include <asm/hardware.h>
#define DRIVER_NAME "button_drv"
#define DEVICE_NAME "button_dev"
int major;
volatile unsigned long *gpfcon;
volatile unsigned long *gpfdat;
volatile unsigned long *gpgcon;
volatile unsigned long *gpgdat;
struct class *buttondrv_class;
struct class_device *buttondrv_class_device;
unsigned int key_val;
unsigned int ev_press = 0;
DECLARE_WAIT_QUEUE_HEAD(button_waitq); //注册等待队列button_waitq
struct pin_desc{
unsigned int pin;
unsigned int key_val;
};
struct pin_desc pins_desc[4] = {
{S3C2410_GPF0, 0x01},
{S3C2410_GPF2, 0x02},
{S3C2410_GPG3, 0x03},
{S3C2410_GPG11, 0x04},
};
irqreturn_t buttons_irq(int irq, void *dev_id) //按键中断服务函数
{
unsigned int pin_val;
struct pin_desc *pin_desc = (struct pin_desc *)dev_id;
pin_val = s3c2410_gpio_getpin(pin_desc->pin);
if(pin_val)
{
key_val = 0x80 | pin_desc->key_val;
}
else
{
key_val = pin_desc->key_val;
}
wake_up_interruptible(&button_waitq); //唤醒休眠的进程, 即调用read函数的进程
ev_press = 1;
return IRQ_HANDLED;
}
int button_drv_open(struct inode *inode, struct file *file)
{
int ret;
ret = request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "S1", (void*)&pins_desc[0]); //注册一个外部中断S1, 双边沿触发
//dev_id为&pins_desc[0]
if(ret)
{
printk("open failed 1 \n");
return -1;
}
ret = request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "S2", (void*)&pins_desc[1]);
if(ret)
{
printk("open failed 2 \n");
return -1;
}
ret = request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "S3", (void*)&pins_desc[2]);
if(ret)
{
printk("open fail 3 \n");
return -1;
}
ret = request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "S4", (void*)&pins_desc[3]);
if(ret)
{
printk("open fail 4 \n");
return -1;
}
return 0;
}
ssize_t button_drv_read(struct file *file, char __user *userbuf, size_t count, loff_t *off)
{
int ret;
wait_event_interruptible(button_waitq, ev_press); //上层应用调用read函数时, ev_press为0, 就将button_waitq加入等待队列, 并释放cpu进入睡眠状态
//当有按键按下时, 中断处理函数唤醒休眠进程, 将ev_press置为1,当上层应用再次调用read函数时
//就会不进入睡眠状态而直接读取按键值
ret = copy_to_user(userbuf, &key_val, 1); //将取得的键值传递给上层应用
if(ret)
{
printk("copy eror \n");
return -1;
}
ev_press = 0; //按键值读取后再次将ev_press置0, 这样上层再次调用时就又会进入睡眠,直到中断再次唤醒
return 1;
}
int button_drv_close(struct inode *inode, struct file *file)
{
free_irq(IRQ_EINT0, (void*)&pins_desc[0]); //释放中断, 根据IRQEINT0找到irq_desc结构, 根据pins_desc[0]找到irq_desc->action结构
free_irq(IRQ_EINT2, (void*)&pins_desc[1]);
free_irq(IRQ_EINT11, (void*)&pins_desc[2]);
free_irq(IRQ_EINT19, (void*)&pins_desc[3]);
return 0;
}
struct file_operations button_drv_fops = {
.owner = THIS_MODULE,
.open = button_drv_open,
.read = button_drv_read,
.release = button_drv_close,
};
int __init button_drv_init(void)
{
major = register_chrdev(0, DRIVER_NAME, &button_drv_fops);
if(major<0)
{
printk("fail 1 button_drv \n");
}
buttondrv_class = class_create(THIS_MODULE, DEVICE_NAME);
if(buttondrv_class<0)
{
printk("fail 2 button_dev \n");
}
buttondrv_class_device = class_device_create(buttondrv_class, NULL, MKDEV(major,0), NULL, DEVICE_NAME);
if(buttondrv_class_device<0)
{
printk("fail 3 button_dev \n");
}
gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
gpfdat = gpfcon + 1;
gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16);
gpgdat = gpgcon + 1;
return 0;
}
void __exit button_drv_exit(void)
{
unregister_chrdev(major, DEVICE_NAME);
class_device_unregister(buttondrv_class_device);
class_destroy(buttondrv_class);
iounmap(gpfcon);
iounmap(gpgcon);
}
module_init(button_drv_init);
module_exit(button_drv_exit);
MODULE_LICENSE("GPL");
Makefile文件:
obj-m += button_drv.o
KERN_DIR = /work/system/linux-2.6.22.6
all:
make -C $(KERN_DIR) M=`pwd` modules
clean:
rm -rf *.o *.ko *.order *.symvers *.mod.c
button_app.c文件:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main(int argc, char **argv)
{
int fd;
char *filename;
unsigned char keyVal;
filename = argv[1];
fd = open(filename, O_RDWR);
if(fd<0)
{
printf("can not open \n");
}
while(1)
{
read(fd, &keyVal, 1);
printf("keyVal: %x \n",keyVal);
}
return 0;
}
编译生成button_drv.ko文件和button_app文件,运行:./button_app /dev/button_dev
Linux 驱动——Button驱动2的更多相关文章
- Linux 驱动——Button驱动7(Timer)消抖
button_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/f ...
- Linux 驱动——Button驱动6(mutex、NBLOCK、O_NONBLOCK)互斥信号量、阻塞、非阻塞
button_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/f ...
- Linux 驱动——Button驱动5(atomic)原子量
button_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/f ...
- Linux 驱动——Button驱动4(fasync)异步通知
button_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/f ...
- Linux 驱动——Button驱动3(poll机制)
button_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/f ...
- Linux 驱动——Button驱动1
button_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/i ...
- Linux GPIO键盘驱动开发记录_OMAPL138
Linux GPIO键盘驱动开发记录_OMAPL138 Linux基本配置完毕了,这几天开始着手Linux驱动的开发,从一个最简单的键盘驱动开始,逐步的了解开发驱动的过程有哪些.看了一下Linux3. ...
- linux块设备驱动之实例
1.注册:向内核注册个块设备驱动,其实就是用主设备号告诉内核这个代表块设备驱动 sbull_major = register_blkdev(sbull_major, "sbull&quo ...
- Linux 视频设备驱动V4L2最常用的控制命令
http://blog.csdn.net/shaolyh/article/details/6583226 Linux 视频设备驱动V4L2最常用的控制命令使用说明(1.02) 命令 功能 VIDIOC ...
随机推荐
- 数模美赛准备——我的第一个LaTex文档
软件下载地址:清华大学镜像网站(点击下载) https://mirrors.tuna.tsinghua.edu.cn/ctex/legacy/2.9/ 上述网站页面 根据个人需求下载不同的版本安装完成 ...
- UnicodeEncodeError: 'gbk' codec can't encode character '\xbb' in position 30633: illegal multibyte sequence
import urllib.request def load_baidu(): url = "https://www.baidu.com/" header = {"Use ...
- 缺少的文件是 ..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props。
报错信息: 严重性 代码 说明 项目级别 文件 行 禁止显示状态 工具错误 这台计算机上缺少此项目引用的 NuGet 程序包.使用“NuGet 程序包还原”可下载这些程序包.有关更多信息,请参见 ht ...
- Python3浮点型(float)运算结果不正确处理办法
一.问题说明 以前对浮点数运行没有没有太在意.昨天同事要求把百分比结果保存到文件上,然后就以保存1位小数的形式给他保存了. 但是今天同事运行时问能不能统一以一位小数的形式保存,当时觉得很奇怪昨天就是以 ...
- javascript 中的number
大家都知道javascript中有五种简单数据类型,number,string,boolean,null,undefined,复杂数据类型是object.本文主要记录下number类型的一些可能不太常 ...
- GET 和 POST 请求的优缺点和误区
Get和Post在面试中一般都会问到,一般的区别:(1)post更安全(不会作为url的一部分,不会被缓存.保存在服务器日志.以及浏览器浏览记录中)(2)post发送的数据更大(get有url长度限制 ...
- thinkphp5和thinkphp3.2.3中URL重写出现No input file specified
查询后解决办法是打开public目录下的.htaccess文件,把:RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L] 改为:RewriteRule ^(.*)$ i ...
- mysql插入中文报错的问题
报错:1366, "Incorrect string value: '\\xE6\\xB7\\xB1\\xE5\\x85\\xA5...' for column ' 由于公司原因之前一直在使 ...
- erlang并发编程(二)
补充-------erlang并发编程 Pid =spawn(fun()-> do_sth() end). 进程监视: Ref = monitor(process, Pid)靠抛异常来终结进程 ...
- SQL-54 查找排除当前最大、最小salary之后的员工的平均工资avg_salary。
题目描述 查找排除当前最大.最小salary之后的员工的平均工资avg_salary.CREATE TABLE `salaries` ( `emp_no` int(11) NOT NULL,`sala ...