韦东山嵌入式学习---app读取按键值框架

一.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

这里还没有具体硬件操作,主要是搭建一个框架 下一篇写具体硬件操作

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
韦东山老师为啥要录升级版嵌入视频?200x年左右,嵌入Linux在全世界、在中国刚刚兴起。我记得我2005年进入中兴时,全部门的人正在努力学习Linux。在2008年,我写了一本书《嵌入Linux应用开发完全手册》。它的大概内容是:裸机、U-boot、Linux内核、Linux设备驱动。那时还没有这样讲解整个系统的书,芯片厂家Linux开发包也还不完善,从bootloader到内核,再到设备驱动都不完善。有全系统开发能力的人也很少。于是这书也就恰逢其时,变成了畅销书。我也根据这个思路录制了视频:裸机、U-boot、Linux内核、Linux设备驱动。收获些许名声,带领很多人进入Linux世界。11年过去了,嵌入Linux世界发生了翻天覆地的变化① 基本系统能用芯片厂家都会提供完整的U-boot、Linux内核、芯片上硬件资源的驱动。方案厂家会做一些定制,比如加上某个WIFI模块,会添加这个WIFI模块的驱动。你可以使用厂家的原始方案,或是使用/借鉴方案商的方案,做出一个“能用”的产品。② 基础驱动弱化;高级驱动专业化基础的驱动,比如GPIO、UART、SPI、I2C、LCD、MMC等,有了太多的书籍、视频、示例代码,修修改改总是可以用的。很多所谓的驱动工程师,实际上就是“调参工程师”。我们群里有名的火哥,提出了一个概念:这些驱动就起一个“hardware enable”的作用。高级的驱动,比如USB、PCIE、HDMI、MIPI、GPU、WIFI、蓝牙、摄像头、声卡。体系非常复杂,很少有人能讲清楚,很多时候只是一笔带过。配置一下应用层工具就了事,能用就成。这些高级驱动,工作中需要专门的人来负责,非常专业。他们是某一块的专家,比如摄像头专家、音频专家。③ 项目为王你到一个公司,目的是把产品做出来,会涉及APP到内核到驱动全流程。中小公司玩不起华为中兴的配置,需要的是全面手。大公司里,只负责很小很小一块的镙丝钉,位置也不太稳固啊。所以,如果你不是立志成为某方面的专家,那就做一个全栈工程师吧。④ 调试很重要都说代码是3分写7分调,各种调试调优技术,可以为你的升职加薪加一把火。基于上述4点,我录制的全新视频将有这些特点:1. 快速入门,2. 实战项目,3. 驱动大全,4. 专题,5. 授人以渔,6. 要做任务另外,我们会使用多款芯片同时录制,先讲通用的原理,再单独讲各个板子的操作。这些芯片涵盖主流芯片公司的主流芯片,让你学习工作无缝对接。1.快速入门入门讲究的是快速,入门之后再慢慢深入,特别是对于急着找工作的学生,对于业余时间挑灯夜读的工作了的人,一定要快!再从裸机、U-boot、内核、驱动这样的路线学习就不适合了,时间就拉得太长了。搞不好学了后面忘了前面。并且实际工作中并不需要你去弄懂U-boot,会用就行:U-boot比驱动还复杂。讲哪些内容?怎么讲呢?混着讲比如先讲LED APP,知道APP怎么调用驱动,再讲LED硬件原理和裸机,最后讲驱动的编写。这样可以快速掌握嵌入Linux的整套开发流程,不必像以前那样光学习裸机就花上1、2个月。而里面的裸机课程,也会让你在掌握硬件操作的同时,把单片机也学会了。讲基础技能中断、休眠-唤醒、异步通知、阻塞、内存映射等等机制,会配合驱动和APP来讲解。这些技能是嵌入Linux开发的基础。而这些驱动,只会涉及LED、按制、LCD等几个驱动。掌握了这些输入、输出的驱动和对应的APP后,你已经具备基本的开发能力了。讲配置我们从厂家、从方案公司基本上都可以拿到一套完整的开发环境,怎么去配置它?需要懂shell和python等配置脚本。效果效率优先以前我都是现场写代码、现场写文档,字写得慢,降低了学习效率。这次,效果与效率统一考虑,不再追求所有东西都现场写。容易的地方可先写好代码文档,难的地方现场写。2.实战项目会讲解这样的涉及linux网关/服务器相关项目(不限于,请多提建议):                   定位为:快速掌握项目开发经验,丰满简历。涉及的每一部分都会讲,比如如果涉及蓝牙,在这里只会讲怎么使用,让你能写出程序;如果要深入,可以看后面的蓝牙专题。3. 驱动大全包括基础驱动、高级驱动。这些驱动都是独立成章,深入讲解。虽然基础驱动弱化了,但是作为Linux系统开发人员,这是必备技能,并且从驱动去理解内核是一个好方法。在讲解这些驱动时,会把驱动的运行环境,比如内核调度,进程线程等概念也讲出来,这样就可以搭建一个知识体系。没有这些知识体系的话,对驱动的理解就太肤浅了,等于在Linux框架下写裸机,一叶障目,不见泰山。定位为:工具、字典,用到再学习。4. 专题想深入学习的任何内容,都可独立为专题。比如U-boot专题、内核内存管理专题、systemtap调试专题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值