一.APP 去读取按键的 4 种方法
① 查询方式
② 休眠-唤醒方式
③ poll 方式
④ 异步通知方式
查询方式
驱动程序中构造、注册一个 file_operations 结构体,里面提供有对应的 open,read 函数。APP 调用
open 时,导致驱动中对应的 open 函数被调用,在里面配置 GPIO 为输入引脚。APP 调用 read 时,导致驱动中对应的 read 函数被调用,它读取寄存器,把引脚状态直接返回给 APP
休眠-唤醒方式
驱动程序中构造、注册一个 file_operations 结构体,里面提供有对应的 open,read 函数。
APP 调用 open 时,导致驱动中对应的 open 函数被调用,在里面配置 GPIO 为输入引脚;并且注册 GPIO的中断处理函数,APP 调用 read 时,导致驱动中对应的 read 函数被调用,如果有按键数据则直接返回给 APP;否则 APP在内核态休眠。当用户按下按键时,GPIO 中断被触发,导致驱动程序之前注册的中断服务程序被执行。它会记录按键数据,并唤醒休眠中的 APP。APP 被唤醒后继续在内核态运行,即继续执行驱动代码,把按键数据返回给 APP(的用户空间)。
poll 方式
上面的休眠-唤醒方式有个缺点:如果用户一直没操作按键,那么 APP 就会永远休眠。
我们可以给 APP 定个闹钟,这就是 poll 方式。
驱动程序中构造、注册一个 file_operations 结构体,里面提供有对应的 open,read,poll 函数。APP 调用 open 时,导致驱动中对应的 open 函数被调用,在里面配置 GPIO 为输入引脚;并且注册 GPIO
的中断处理函数。
APP 调用 poll 或 select 函数,意图是“查询”是否有数据,这 2 个函数都可以指定一个超时时间,即
在这段时间内没有数据的话就返回错误。这会导致驱动中对应的 poll 函数被调用,如果有按键数据则直接返回给 APP;否则 APP 在内核态休眠一段时间。
当用户按下按键时,GPIO 中断被触发,导致驱动程序之前注册的中断服务程序被执行。它会记录按键数据,并唤醒休眠中的 APP。
如果用户没按下按键,但是超时时间到了,内核也会唤醒 APP。
所以 APP 被唤醒有 2 种原因:用户操作了按键,超时。被唤醒的 APP 在内核态继续运行,即继续执行驱动代码,把“状态”返回给 APP(的用户空间)。
APP 得到 poll/select 函数的返回结果后,如果确认是有数据的,则再调用 read 函数,这会导致驱动
中的 read 函数被调用,这时驱动程序中含有数据,会直接返回数据。
异步通知方式
异步通知的实现原理是:内核给 APP 发信号。信号有很多种,这里发的是 SIGIO。
驱动程序中构造、注册一个 file_operations 结构体,里面提供有对应的 open,read,fasync 函数。
APP 调用 open 时,导致驱动中对应的 open 函数被调用,在里面配置 GPIO 为输入引脚;并且注册 GPIO的中断处理函数。
APP 给信号 SIGIO 注册自己的处理函数:my_signal_fun。
APP 调用 fcntl 函数,把驱动程序的 flag 改为 FASYNC,这会导致驱动程序的 fasync 函数被调用,它
只是简单记录进程 PID。
当用户按下按键时,GPIO 中断被触发,导致驱动程序之前注册的中断服务程序被执行。它会记录按键数据,然后给进程 PID 发送 SIGIO 信号。
APP 收到信号后会被打断,先执行信号处理函数:在信号处理函数中可以去调用 read 函数读取按键值。
信号处理函数返回后,APP 会继续执行原先被打断的代码
二,查询方式的按键驱动程序编写
分层
我们的目的写出一个容易扩展到各种芯片、各种板子的按键驱动程序,所以驱动程序分为上下两层:
① button_drv.c 分配/设置/注册 file_operations 结构体
起承上启下的作用,向上提供 button_open,button_read 供 APP 调用。
而这 2 个函数又会调用底层硬件提供的 p_button_opr 中的 init、read 函数操作硬件。
② board_xxx.c 分配/设置/注册 button_operations 结构体
这个结构体是我们自己抽象出来的,里面定义单板 xxx 的按键操作函数。
这样的结构易于扩展,对于不同的单板,只需要替换 board_xxx.c 提供自己的 button_operations 结
构体即可。
三,完整代码
button_drv.c
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/fcntl.h>
#include <linux/fs.h>
#include <linux/signal.h>
#include <linux/mutex.h>
#include <linux/mm.h>
#include <linux/timer.h>
#include <linux/wait.h>
#include <linux/skbuff.h>
#include <linux/proc_fs.h>
#include <linux/poll.h>
#include <linux/capi.h>
#include <linux/kernelcapi.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/moduleparam.h>
#include "button_drv.h"
static int major =0;//定义一个主设备号
static struct button_operations *p_button_opr;//由底层代码提供 .h文件
static struct class *button_class;//创建类 在入口函数中创建
static int button_open(struct inode *inode,struct file *file)
{
int minor=iminor(inode);//从节点中获得次设备号
p_button_opr->init(minor);//调用board文件中的初始化函数,底层代码调用 register_button_operations 函数,向上提供这个结构体指针。
return 0;
}
static ssize_t button_read(struct file *file,char __user *buf,size_t size,loff_t *off)//ssize_t是有符号整型,在32位机器上等同与int,在64位机器上等同与long int
{ //size_t 就是无符号型的ssize_t,也就是unsigned long/ unsigned int (在32位下)
unsigned int minor=iminor(file_inode(file));//从文件中获取节点,从节点中获得次设备号 //loff_t 为 long long 类型
int level;
int err;
level=p_button_opr->read(minor);//调用底层函数将引脚电平读出来 p_button_opr由底层硬件代码提供
err=copy_to_user(buf,&level,1);//拷贝到buf,假装用一下返回值,避免报错
return 1;
}
static struct file_operations button_fops= //定义自己的file_operations结构体,向上提供 button_open,button_read 供 APP 调用。而这 2 个函数又会调用底层硬件提供的 p_button_opr 中的 init、read 函数操作硬件。
{
.open=button_open,
.read=button_read,
};
void register_button_operations(struct button_operations *opr)
{
int i;
p_button_opr=opr;//由底层代码提供p_button_opr
for(i=0;i<opr->count;i++)//创建这个设备
{
device_create(button_class,NULL,MKDEV(major,i),NULL,"ky_button%d",i);
}
}
void unregister_button_operations(void)
{
int i;
for(i=0;i< p_button_opr->count;i++)//销毁这些设备
{
device_destroy(button_class,MKDEV(major,i));
}
}
EXPORT_SYMBOL(register_button_operations);//为了让上面两个函数给其他文件用,要导出一下
EXPORT_SYMBOL(unregister_button_operations);
int button_init(void)//入口函数
{
major=register_chrdev(0,"ky_button",&button_fops);//初始化,向内核注册自己的file_operations结构体,传入file_operations结构体的指针
button_class=class_create(THIS_MODULE,"ky_button");//创建这个类, class 下还没有 device,在后面获得底层硬件的信息时再在 class 下创建 device:这只是用来创建设备节点,它不是驱动程序的核心。
if(IS_ERR(button_class))
return -1;
return 0;
}
void button_exit(void)//出口函数
{
class_destroy(button_class);//销毁这个类
unregister_chrdev(major,"ky_button");//注销,传入主设备号和名字
}
module_init(button_init);
module_exit(button_exit);
MODULE_LICENSE("GPL");
button_drv.h
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/fcntl.h>
#include <linux/fs.h>
#include <linux/signal.h>
#include <linux/mutex.h>
#include <linux/mm.h>
#include <linux/timer.h>
#include <linux/wait.h>
#include <linux/skbuff.h>
#include <linux/proc_fs.h>
#include <linux/poll.h>
#include <linux/capi.h>
#include <linux/kernelcapi.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/moduleparam.h>
#include "button_drv.h"
static int major =0;//定义一个主设备号
static struct button_operations *p_button_opr;//由底层代码提供 .h文件
static struct class *button_class;//创建类 在入口函数中创建
static int button_open(struct inode *inode,struct file *file)
{
int minor=iminor(inode);//从节点中获得次设备号
p_button_opr->init(minor);//调用board文件中的初始化函数,底层代码调用 register_button_operations 函数,向上提供这个结构体指针。
return 0;
}
static ssize_t button_read(struct file *file,char __user *buf,size_t size,loff_t *off)//ssize_t是有符号整型,在32位机器上等同与int,在64位机器上等同与long int
{ //size_t 就是无符号型的ssize_t,也就是unsigned long/ unsigned int (在32位下)
unsigned int minor=iminor(file_inode(file));//从文件中获取节点,从节点中获得次设备号 //loff_t 为 long long 类型
int level;
int err;
level=p_button_opr->read(minor);//调用底层函数将引脚电平读出来 p_button_opr由底层硬件代码提供
err=copy_to_user(buf,&level,1);//拷贝到buf,假装用一下返回值,避免报错
return 1;
}
static struct file_operations button_fops= //定义自己的file_operations结构体,向上提供 button_open,button_read 供 APP 调用。而这 2 个函数又会调用底层硬件提供的 p_button_opr 中的 init、read 函数操作硬件。
{
.open=button_open,
.read=button_read,
};
void register_button_operations(struct button_operations *opr)
{
int i;
p_button_opr=opr;//由底层代码提供p_button_opr
for(i=0;i<opr->count;i++)//创建这个设备
{
device_create(button_class,NULL,MKDEV(major,i),NULL,"ky_button%d",i);
}
}
void unregister_button_operations(void)
{
int i;
for(i=0;i< p_button_opr->count;i++)//销毁这些设备
{
device_destroy(button_class,MKDEV(major,i));
}
}
EXPORT_SYMBOL(register_button_operations);//为了让上面两个函数给其他文件用,要导出一下
EXPORT_SYMBOL(unregister_button_operations);
int button_init(void)//入口函数
{
major=register_chrdev(0,"ky_button",&button_fops);//初始化,向内核注册自己的file_operations结构体,传入file_operations结构体的指针
button_class=class_create(THIS_MODULE,"ky_button");//创建这个类, class 下还没有 device,在后面获得底层硬件的信息时再在 class 下创建 device:这只是用来创建设备节点,它不是驱动程序的核心。
if(IS_ERR(button_class))
return -1;
return 0;
}
void button_exit(void)//出口函数
{
class_destroy(button_class);//销毁这个类
unregister_chrdev(major,"ky_button");//注销,传入主设备号和名字
}
module_init(button_init);
module_exit(button_exit);
MODULE_LICENSE("GPL");
board_xxx.c
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/fcntl.h>
#include <linux/fs.h>
#include <linux/signal.h>
#include <linux/mutex.h>
#include <linux/mm.h>
#include <linux/timer.h>
#include <linux/wait.h>
#include <linux/skbuff.h>
#include <linux/proc_fs.h>
#include <linux/poll.h>
#include <linux/capi.h>
#include <linux/kernelcapi.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/moduleparam.h>
#include "button_drv.h"
static void board_xxx_button_init_gpio (int which)
{
printk("%s %s %d, init gpio for button %d\n", __FILE__, __FUNCTION__, __LINE__, which);
}
static int board_xxx_button_read_gpio (int which)
{
printk("%s %s %d, read gpio for button %d\n", __FILE__, __FUNCTION__, __LINE__, which);
return 1;
}
static struct button_operations my_buttons_ops ={ //实现了一个 button_operations 结构体
.count = 2,
.init = board_xxx_button_init_gpio,//初始化引脚的函数
.read = board_xxx_button_read_gpio,//读引脚的函数
};
int board_xxx_button_init(void)
{
register_button_operations(&my_buttons_ops);//注册上面的结构体,底层代码调用 register_button_operations 函数,向上提供这个结构体指针。
return 0;
}
void board_xxx_button_exit(void)
{
unregister_button_operations();//销毁上面的结构体
}
module_init(board_xxx_button_init);
module_exit(board_xxx_button_exit);
MODULE_LICENSE("GPL");
button_test.c
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/fcntl.h>
#include <linux/fs.h>
#include <linux/signal.h>
#include <linux/mutex.h>
#include <linux/mm.h>
#include <linux/timer.h>
#include <linux/wait.h>
#include <linux/skbuff.h>
#include <linux/proc_fs.h>
#include <linux/poll.h>
#include <linux/capi.h>
#include <linux/kernelcapi.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/moduleparam.h>
#include "button_drv.h"
static void board_xxx_button_init_gpio (int which)
{
printk("%s %s %d, init gpio for button %d\n", __FILE__, __FUNCTION__, __LINE__, which);
}
static int board_xxx_button_read_gpio (int which)
{
printk("%s %s %d, read gpio for button %d\n", __FILE__, __FUNCTION__, __LINE__, which);
return 1;
}
static struct button_operations my_buttons_ops ={ //实现了一个 button_operations 结构体
.count = 2,
.init = board_xxx_button_init_gpio,//初始化引脚的函数
.read = board_xxx_button_read_gpio,//读引脚的函数
};
int board_xxx_button_init(void)
{
register_button_operations(&my_buttons_ops);//注册上面的结构体,底层代码调用 register_button_operations 函数,向上提供这个结构体指针。
return 0;
}
void board_xxx_button_exit(void)
{
unregister_button_operations();//销毁上面的结构体
}
module_init(board_xxx_button_init);
module_exit(board_xxx_button_exit);
MODULE_LICENSE("GPL");
Makelife
# 1. 使用不同的开发板内核时, 一定要修改KERN_DIR
# 2. KERN_DIR中的内核要事先配置、编译, 为了能编译内核, 要先设置下列环境变量:
# 2.1 ARCH, 比如: export ARCH=arm64
# 2.2 CROSS_COMPILE, 比如: export CROSS_COMPILE=aarch64-linux-gnu-
# 2.3 PATH, 比如: export PATH=$PATH:/home/book/100ask_roc-rk3399-pc/ToolChain-6.3.1/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin
# 注意: 不同的开发板不同的编译器上述3个环境变量不一定相同,
# 请参考各开发板的高级用户使用手册
KERN_DIR = /home/ky/100ask_imx6ull-sdk/Linux-4.9.88
all:
# -C选项的作用是指将当前的工作目录转移到制定的 目录,即(KERN_DIR)目录,程序到(shell pwd)当前目录查找模块源码,将其编译,生成.ko文件。
make -C $(KERN_DIR) M=`pwd` modules
$(CROSS_COMPILE)gcc -o button_test button_test.c
clean:
make -C $(KERN_DIR) M=`pwd` modules clean
rm -rf modules.order
rm -f ledtest
# 参考内核源码drivers/char/ipmi/Makefile
# 要想把a.c, b.c编译成ab.ko, 可以这样指定:
# ab-y := a.o b.o
# obj-m += ab.o
#将这两个文件编译成.ko
obj-m += button_drv.o
obj-m += board_xxx.o
这里还没有具体硬件操作,主要是搭建一个框架 下一篇写具体硬件操作