Linux 驱动——Button驱动1
button_drv.c驱动文件:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <asm/io.h> //包含iomap函数iounmap函数
#include <asm/uaccess.h> //包含copy_from_user函数
#include <linux/device.h> //包含class类相关的处理函数
#include <linux/fs.h> //包含file_operations结构体
#define DRIVER_NAME "button_drv"
#define DEVICE_NAME "button_dev"
int major;
static struct class *buttondrv_class;
static struct class_device *buttondrv_class_dev;
volatile unsigned long *gpfcon = NULL;
volatile unsigned long *gpfdat = NULL;
volatile unsigned long *gpgcon = NULL;
volatile unsigned long *gpgdat = NULL;
static int button_drv_open(struct inode *inode, struct file *file)
{
*gpfcon &= ~((0x3<<(0*2)) | (0x3<<(2*2)));
*gpgcon &= ~((0x3<<(3*2)) | (0x3<<(11*2)));
return 0;
}
static int button_drv_read(struct file * file, char __user * userbuf, size_t count, loff_t * off)
{
int ret;
unsigned char keyVal[4];
unsigned long keyVals;
if(count!=sizeof(keyVal))
{
printk("button_drv_read error 1 \n");
}
keyVals = *gpfdat;
keyVal[0] = ((keyVals>>0) & 1);
keyVal[1] = ((keyVals>>2) & 1);
keyVals = *gpgdat;
keyVal[2] = ((keyVals>>3) & 1);
keyVal[3] = ((keyVals>>11) & 1);
ret = copy_to_user(userbuf, keyVal, sizeof(keyVal));
if(ret<0)
{
printk("button_drv_read error 2 \n");
}
return sizeof(keyVal);
}
static struct file_operations button_drv_fops = {
.owner = THIS_MODULE,
.open = button_drv_open,
.read = button_drv_read,
};
static int button_drv_init(void)
{
major = register_chrdev(0, DRIVER_NAME, &button_drv_fops);
buttondrv_class = class_create(THIS_MODULE, DEVICE_NAME);
buttondrv_class_dev = class_device_create(buttondrv_class, NULL, MKDEV(major, 0), NULL, DEVICE_NAME);
gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
gpfdat = gpfcon + 1;
gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16);
gpgdat = gpgcon + 1;
return 0;
}
static void button_drv_exit(void)
{
unregister_chrdev(major, DRIVER_NAME);
class_device_unregister(buttondrv_class_dev);
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;
int val;
char *filename;
unsigned char keyVal[4];
filename = argv[1];
fd = open(filename, O_RDWR);
if(fd<0)
{
printf("can not open \n");
}
while(1)
{
read(fd, keyVal, sizeof(keyVal));
if(!keyVal[0] || !keyVal[1] || !keyVal[2] || !keyVal[3])
{
printf("key pressed %d %d %d %d \n", keyVal[0], keyVal[1], keyVal[2], keyVal[3]);
}
}
return 0;
}
编译生成button_drv.ko文件和button_app文件,运行:./button_app /dev/button_dev
Linux 驱动——Button驱动1的更多相关文章
- 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驱动2
button_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/f ...
- 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 ...
随机推荐
- SQLyog 连接主机的时候出现错误
这个 连接主机的时候出现: 这个时候打开cmd 找到mysql 的bin目录 输入 mysqld -nt -remove 即可 直接登录,注意这里的d代表的是服务端 接下来就可以不用输入密码直 ...
- java Arrays常用方法
1. 简介 Arrays类包含用于操作数组的各种方法(例如排序和搜索).此类还包含一个静态工厂,允许将数组视为列表. 如果指定的数组引用为null,则此类中的方法都抛出NullPointerExcep ...
- 八大排序算法——归并排序(动图演示 思路分析 实例代码java 复杂度分析)
一.动图演示 二.思路分析 归并排序就是递归得将原始数组递归对半分隔,直到不能再分(只剩下一个元素)后,开始从最小的数组向上归并排序 1. 向上归并排序的时候,需要一个暂存数组用来排序, 2. 将 ...
- django虚拟环境安装
虚拟环境主要是防止不同版本的模块之间的冲突,维护多个项目的时候这个非常重要. 虚拟环境的安装 sudo apt install python-virtualenv 虚拟环境安装成功后,直接创建一个虚拟 ...
- ldap 导出、导入ldif数据
ldap 导出.导入ldif数据有如下方式: 1.dsadm(速度快,需要停止ldap实例) 2.dsconf(速度慢,需要保持ldap实例开启) windows导出.导入需要加上参数--unsecu ...
- 网页布局中页面内容不足一屏时页脚footer固定底部
方法一:给html.body都设置100%的高度,确定body下内容设置min-height有效,然后设置主体部分min-height为100%,此时若没有header.footer则刚好完美占满全屏 ...
- Rhino学习教程——1.1
在Rhino的官网下载好Rhino5.0版本后(Rhino官网会提供下载方式,官网是http://www.xuexiniu.com),双击桌面快捷键,就会出现Rhino的界面(我已经自定义过界面了). ...
- Ubuntu16.04下安装sublime text3
通过ppa安装,打开终端,输入以下命令: sudo add-apt-repository ppa:webupd8team/sublime-text-3 sudo apt-get update sudo ...
- Mysql 源码:关于innodb中两次写的探索
转载自:http://www.cnblogs.com/bamboos/p/3553703.html?utm_source=tuicool&utm_medium=referral 两次写可以说是 ...
- # -*- coding: utf-8 -*-
-- coding: utf-8 -- import scrapy from jobscrawler_qianchengwuyou.items import JobscrawlerQianchengw ...