本帖最后由 gjianw217 于 2015-8-22 02:24 编辑
前面进行了OK210试用体验的入门篇介绍,算是初步入门,分别包含:
【OK210试用体验】的第一篇:开箱验板
【OK210试用体验】的第二篇:板载资源
【OK210试用体验】的第三篇:开发环境(软件安装,开发环境,烧写系统)
【OK210试用体验】的第四篇:编程入门(NFS登录,驱动入门)
前一篇介绍了
功能篇的字符驱动之LED灯,今天是功能篇的第二篇:
字符驱动之Key按键的控制,本节主要分3部分:硬件分析,软件基础,驱动编程。
一、硬件分析
在【OK210试用体验】的第二篇:板载资源中,简单分析了Key按键的功能和作用。其实对Key的操作,和对LED的操作是一相对的,都是对GPIO的最基本操作,也是入门操作。Key是对GPIO的输入进行操作,LED是对GPIO的输出进行操作。
首先从OK210的底板原理图中可知,OK210的5个按键,通过核心板接到了S5PV210的XEINT3-7引脚上,如下图,
接着从用户手册的第103页中得知,这些引脚没有复用功能,默认为GPI,即通用端口输入,如下图,其实在用户手册的92而,有这么一句话:,想必大家都知道其什么意思了。
GPH0,1,2,3: 32 in/out port - Key pad, External Wake-up (up-to 32-bit). (GPH* groups are in Alive region)
也就是说,要对5个Key的操作,就是对XEINT3-7这5个引脚的控制。
二、软件基础
1 字符设备驱动
字符驱动模块,可以简单的总结为如下图所示:包括对设备文件的创建,字符驱动的注册以及文件操作的编写等。
其实个人理解,对字符驱动的编写,就是对
struct file_operations 结构体的填充,该结构体定义在linux/fs.h头文件中,在2.6.35.7内核中,定义为如下形式:
- struct file_operations {
- struct module *owner;
- loff_t (*llseek) (struct file *, loff_t, int);
- ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
- ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
- ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
- ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
- int (*readdir) (struct file *, void *, filldir_t);
- unsigned int (*poll) (struct file *, struct poll_table_struct *);
- int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
- long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
- long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
- int (*mmap) (struct file *, struct vm_area_struct *);
- int (*open) (struct inode *, struct file *);
- int (*flush) (struct file *, fl_owner_t id);
- int (*release) (struct inode *, struct file *);
- int (*fsync) (struct file *, int datasync);
- int (*aio_fsync) (struct kiocb *, int datasync);
- int (*fasync) (int, struct file *, int);
- int (*lock) (struct file *, int, struct file_lock *);
- ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
- unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
- int (*check_flags)(int);
- int (*flock) (struct file *, int, struct file_lock *);
- ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
- ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
- int (*setlease)(struct file *, long, struct file_lock **);
- };
本节将会用到
open,read,poll,fasync,release这5个函数,下面将会一一介绍。
2 驱动中断
驱动程序中进行中断处理涉及到的最基本的内核API,主要用于申请和释放中断,在linux/sched.h里声明,其原型为:
int request_irq(unsigned int irq,
void (*handler)(int irq, void *dev_id, struct pt_regs *regs
),
unsigned long irqflags,
const char * devname,
void *dev_id);
irq是要申请的硬件中断号;handler是向系统登记的中断处理函数。这是一个回调函数,中断发生时,系统调用这个函数,传入的参数包括硬件中断号,device id,寄存器值。dev_id就是下面的request_irq时传递给系统的参数dev_id。irqflags是中断处理的一些属性。比较重要的有SA_INTERRUPT,标明中断处理程序是快速处理程序(设置SA_INTERRUPT)还是慢速处理程序(不设置SA_INTERRUPT)。快速处理程序被调用时屏蔽所有中断,慢速处理程序不屏蔽。还有一个SA_SHIRQ属性,设置了以后运行多个设备共享中断。dev_id在中断共享时会用到。一般设置为这个设备的 device结构本身或者NULL。中断处理程序可以用dev_id找到相应的控制这个中断的设备,或者用irq2dev_map找到中断对应的设备。
void free_irq(unsigned int irq,void *dev_id);
三、驱动编程
(一)、本驱动程序分别实现了对按键的简单读(read)操作,支持轮询机制(poll)和支持异步机制(fasync)。
1简单读(read)操作
简单的读操作的工作原理是:当应用程序读取键值时,会调用按键驱动程序的read函数,而实现的read函数检测完读取长度后没有直接读取键值而是等待按键消息,如果没有按键,程序会进入休眠状态,这样可以节省大量的CPU,而当按下按键时硬件会产生中断,程序自动进入中断处理函数,在中断处理函数中,驱动程序读取键值存入全局变量并激活read函数中等待的消息,应用程序被迅速唤醒并通过read函数读取键值,如此,完成了获取键值的工作。测试程序见TestButtons1()。
2支持轮询机制(poll)
上面的实现的按键驱动程序有个弊端,如果不按键,应用程序将会永远阻塞在这里,幸运的是,linux内核提供了poll机制,可以设置超时等待时间,如果在这个时间内读取到键值则正常返回,反之则超时退出。测试程序见TestButtons2()。
3支持异步机制(fasync)
很多情况下,程序在等待按键期间需要处理其它任务而不是在这里空等,这时,就需要采用异步模式了。所谓异步模式,实际上是采用消息机制,即当驱动程序检测到按键后发送消息给应用程序,应用程序接收到消息后再去读取键值。与前面的两种模式相比,最大的不同在于异步方式是驱动告诉应用程序来读而不是应用程序主动去读。测试程序见TestButtons3()。
下面是按键采用异步机制的测试结果:
(二)程序代码
1驱动程序:
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- static dev_t devno;
- static struct cdev cdev;
- static struct class* my_buttons_class;
- static struct device* my_buttons_device;
- static wait_queue_head_t my_button_waitq;
- static struct fasync_struct *my_button_fasync;
- static volatile int pressed = 0;
- static unsigned char key_val;
- struct key_desc{
- unsigned int pin;
- unsigned char value;
- char* name;
- };
- static struct key_desc my_key_descs[] = {
- [0] = {
- .pin = S5PV210_GPH0(3),
- .value = 0x01,
- .name ="Key1",
- },
- [1] = {
- .pin = S5PV210_GPH0(4),
- .value = 0x02,
- .name ="Key2",
- },
- [2] = {
- .pin = S5PV210_GPH0(5),
- .value = 0x03,
- .name ="Key3",
- },
- [3] = {
- .pin = S5PV210_GPH0(6),
- .value = 0x04,
- .name ="Key4",
- },
- [4] = {
- .pin = S5PV210_GPH0(7),
- .value = 0x05,
- .name ="Key5",
- },
- };
- #define BUTTONS_NUM ARRAY_SIZE(my_key_descs)
- static irqreturn_t my_buttons_irq(int irq, void *dev_id){
- volatile struct key_desc *key = (volatile struct key_desc *)dev_id;
- if(gpio_get_value(key->pin)){
- key_val = key->value|0x80;
- }
- else{
- key_val = key->value;
- }
- pressed = 1;
- wake_up_interruptible(&my_button_waitq);
- kill_fasync(&my_button_fasync, SIGIO, POLL_IN); //用kill_fasync函数告诉应用程序,有数据可读了
- return IRQ_RETVAL(IRQ_HANDLED);
- }
- static int my_buttons_open(struct inode *inode, struct file *file){
- int ret;
- ret = request_irq( IRQ_EINT3, my_buttons_irq, IRQ_TYPE_EDGE_BOTH, "key1", &my_key_descs[0]);
- if(ret)
- return ret;
- ret = request_irq( IRQ_EINT4, my_buttons_irq, IRQ_TYPE_EDGE_BOTH, "key2", &my_key_descs[1]);
- if(ret)
- return ret;
- ret = request_irq( IRQ_EINT5, my_buttons_irq, IRQ_TYPE_EDGE_BOTH, "key3", &my_key_descs[2]);
- if(ret)
- return ret;
- ret = request_irq( IRQ_EINT6, my_buttons_irq, IRQ_TYPE_EDGE_BOTH, "key4", &my_key_descs[3]);
- if(ret)
- return ret;
- ret = request_irq( IRQ_EINT7, my_buttons_irq, IRQ_TYPE_EDGE_BOTH, "key5", &my_key_descs[4]);
- if(ret)
- return ret;
- return 0;
- }
- static ssize_t my_buttons_read(struct file * file, char __user *data, size_t count, loff_t *loff){
- if(count != 1){
- printk(KERN_ERR "The driver can only give one key value once!n");
- return -ENOMEM;
- }
- wait_event_interruptible(my_button_waitq, pressed);
- pressed = 0;
- if(copy_to_user(data, &key_val, 1)){
- printk(KERN_ERR "The driver can not copy the data to user area!n");
- return -ENOMEM;
- }
- return 0;
- }
- static unsigned my_buttons_poll(struct file *file, poll_table *wait){
- unsigned int mask = 0;
- poll_wait(file, &my_button_waitq, wait);
- if (pressed)
- mask |= POLLIN | POLLRDNORM;
- return mask;
- }
- static int my_buttons_fasync(int fd, struct file * file, int on){
- return fasync_helper(fd, file, on, &my_button_fasync);
- }
- static int my_buttons_close(struct inode *inode, struct file *file){
- free_irq(IRQ_EINT3, &my_key_descs[0]);
- free_irq(IRQ_EINT4, &my_key_descs[1]);
- free_irq(IRQ_EINT5, &my_key_descs[2]);
- free_irq(IRQ_EINT6, &my_key_descs[3]);
- free_irq(IRQ_EINT7, &my_key_descs[4]);
- return 0;
- }
- struct file_operations my_buttons_ops = {
- .open = my_buttons_open,
- .read = my_buttons_read,
- .poll = my_buttons_poll,
- .fasync = my_buttons_fasync,
- .release = my_buttons_close,
- };
- int my_buttons_init(void){
- int ret;
- cdev_init(&cdev, &my_buttons_ops);
- cdev.owner = THIS_MODULE;
- ret = alloc_chrdev_region(&devno, 0, 1, "buttons");
- if(ret){
- printk(KERN_ERR "alloc char device region faild!n");
- return ret;
- }
- ret = cdev_add(&cdev, devno, 1);
- if(ret){
- printk(KERN_ERR "add char device faild!n");
- goto add_error;
- }
- my_buttons_class = class_create(THIS_MODULE, "buttonsdrv");
- if(IS_ERR(my_buttons_class)){
- printk(KERN_ERR "create class error!n");
- goto class_error;
- }
- my_buttons_device = device_create(my_buttons_class, NULL, devno, NULL, "buttons");
- if(IS_ERR(my_buttons_device)){
- printk(KERN_ERR "create buttons device error!n");
- goto device_error;
- }
- init_waitqueue_head(&my_button_waitq);
- return 0;
- device_error:
- class_destroy(my_buttons_class);
- class_error:
- cdev_del(&cdev);
- add_error:
- unregister_chrdev_region(devno,1);
- return -ENODEV;
- }
- void my_buttons_exit(void){
- device_destroy(my_buttons_class, devno);
- class_destroy(my_buttons_class);
- cdev_del(&cdev);
- unregister_chrdev_region(devno, 1);
- }
- module_init(my_buttons_init);
- module_exit(my_buttons_exit);
- MODULE_LICENSE("GPL");
- MODULE_AUTHOR("gjianw217@163.com");
- MODULE_DESCRIPTION("Buttons' Driver");
2应用程序:
- #include
- #include
- #include
- #include
- #include
- #include
- void TestButtons1()
- {
- int fd = open("/dev/buttons", O_RDWR);
- if(fd < 0){
- printf("open error");;
- return 0;
- }
- unsigned char key;
- while(1){
- read(fd, &key, 1);
- printf("The key = %xn", key);
- }
- close(fd);
- }
- void TestButtons2()
- {
- int fd;
- unsigned char key_val;
- int ret;
- struct pollfd fds[1];
- fd = open("/dev/buttons", O_RDWR);
- if (fd < 0){
- printf("can't open!n");
- }
- fds[0].fd = fd;
- fds[0].events = POLLIN;
- while (1){
- ret = poll(fds, 1, 5000);
- if (ret == 0){
- printf("time outn");
- }
- else{
- read(fd, &key_val, 1);
- printf("key_val = 0x%xn", key_val);
- }
- }
- }
- int asycfd;
- void my_signal_fun(int signum)
- {
- unsigned char key_val;
- read(asycfd, &key_val, 1);
- printf("key_val: 0x%xn", key_val);
- }
- void TestButtons3()
- {
- unsigned char key_val;
- int ret;
- int Oflags;
- signal(SIGIO, my_signal_fun);
- asycfd = open("/dev/buttons", O_RDWR | O_NONBLOCK);
- if (asycfd < 0){
- printf("can't open!n");
- return -1;
- }
- fcntl(asycfd, F_SETOWN, getpid());
- Oflags = fcntl(asycfd, F_GETFL);
- fcntl(asycfd, F_SETFL, Oflags | FASYNC);
- int rest;
- while (1){
- printf("Hellon");
- while(rest = sleep(50)){
- sleep(rest);
- }
- }
- }
- int main()
- {
- //TestButtons1();
- // TestButtons2();
- TestButtons3();
- }
3Makefile:
- #buttons Makefile
- ARCH=arm
- CROSS_COMPILE=/home/ok210/arm-2009q3/bin/arm-none-linux-gnueabi-
- APP_COMPILE=/home/ok210/arm-2009q3/bin/arm-none-linux-gnueabi-
- #obj-m := app-drv.o
- obj-m := keys-drv.o
- #KDIR := /path/to/kernel/linux/
- KDIR := /home/ok210/android-kernel-samsung-dev/
- PWD := $(shell pwd)
- default:
- make -C $(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
- app:keys-app.c
- $(APP_COMPILE)gcc -o app keys-app.c
- clean:
- $(MAKE) -C $(KDIR) M=$(PWD) clean
欢迎大家关注本人的微信公众号【口袋物联】,微信号为koudaiwulian。