当前位置: 首页 > wzjs >正文

建设网站的软件神马搜索推广

建设网站的软件,神马搜索推广,dz网站开发,展会宣传推广计划嵌入式Linux应用开发-第十四章查询方式的按键驱动程序 第十四章 查询方式的按键驱动程序_编写框架14.1 LED驱动回顾14.2 按键驱动编写思路14.3 编程:先写框架14.3.1 把按键的操作抽象出一个button_operations结构体14.3.2 驱动程序的上层:file_operation…

嵌入式Linux应用开发-第十四章查询方式的按键驱动程序

  • 第十四章 查询方式的按键驱动程序_编写框架
    • 14.1 LED驱动回顾
    • 14.2 按键驱动编写思路
    • 14.3 编程:先写框架
      • 14.3.1 把按键的操作抽象出一个button_operations结构体
      • 14.3.2 驱动程序的上层:file_operations结构体
    • 14.4 测试
    • 14.5 课后怎业

第十四章 查询方式的按键驱动程序_编写框架

在这里插入图片描述

14.1 LED驱动回顾

对于 LED,APP调用 open函数导致驱动程序的 led_open函数被调用。在里面,把 GPIO配置为输出引脚。安装驱动程序后并不意味着会使用对应的硬件,而 APP要使用对应的硬件,必须先调用 open函数。所以建议在驱动程序的 open函数中去设置引脚。
APP继续调用 write函数传入数值,在驱动程序的 led_write函数根据该数值去设置 GPIO的数据寄存器,从而控制 GPIO的输出电平。
怎么操作寄存器?从芯片手册得到对应寄存器的物理地址,在驱动程序中使用 ioremap函数映射得到虚拟地址。驱动程序中使用虚拟地址去访问寄存器。
在这里插入图片描述

14.2 按键驱动编写思路

GPIO按键的原理图一般有如下 2种:
在这里插入图片描述

按键没被按下时,上图中左边的 GPIO电平为高,右边的 GPIO电平为低。 按键被按下后,上图中左边的 GPIO电平为低,右边的 GPIO电平为高。
编写按键驱动程序最简单的方法如下图所示:
在这里插入图片描述

回顾一下编写驱动程序的套路:
在这里插入图片描述

对于使用查询方式的按键驱动程序,我们只需要实现 button_open、button_read。

14.3 编程:先写框架

我们的目的写出一个容易扩展到各种芯片、各种板子的按键驱动程序,所以驱动程序分为上下两层: ① 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结构体即可。
在这里插入图片描述
使用 GIT下载所有源码后,本节源码位于如下目录:

01_all_series_quickstart\ 
05_嵌入式 Linux驱动开发基础知识\source\ 
04_button_drv\01_button_drv_template 

14.3.1 把按键的操作抽象出一个button_operations结构体

首先看看 button_drv.h,它定义了一个 button_operations结构体,把按键的操作抽象为这个结构体:

04 struct button_operations { 
05     int count; 
06     void (*init) (int which); 
07     int (*read) (int which); 
08 }; 
09 
10 void register_button_operations(struct button_operations *opr); 
11 void unregister_button_operations(void); 
12 

再看看 board_xxx.c,它实现了一个 button_operations结构体,代码如下。
第 45行调用 register_button_operations函数,把这个结构体注册到上层驱动中。 37 static struct

button_operations my_buttons_ops ={ 
38     .count = 2, 
39     .init  = board_xxx_button_init_gpio, 
40     .read  = board_xxx_button_read_gpio, 
41 }; 
42 
43 int board_xxx_button_init(void) 
44 { 
45     register_button_operations(&my_buttons_ops); 
46     return 0; 
47 } 
48 

14.3.2 驱动程序的上层:file_operations结构体

上层是 button_drv.c,它的核心是 file_operations结构体,首先看看入口函数,代码如下。
第 83行向内核注册一个 file_operations结构体。
第 85行创建一个 class,但是该 class下还没有 device,在后面获得底层硬件的信息时再在 class下创建 device:这只是用来创建设备节点,它不是驱动程序的核心。

81 int button_init(void) 
82 { 
83     major = register_chrdev(0, "xxxxxx_button", &button_fops); 
84 
85     button_class = class_create(THIS_MODULE, "xxxxxx_button"); 
86     if (IS_ERR(button_class)) 
87         return -1; 
88 
89     return 0; 
90 } 
91 

再来看看 button_drv.c中 file_operations结构体的成员函数,代码如下。
第 34、44行都用到一个 button_operations指针,它是从何而来?

28 static struct button_operations *p_button_opr; 
29 static struct class *button_class; 
30 
31 static int button_open (struct inode *inode, struct file *file) 
32 { 
33     int minor = iminor(inode); 
34     p_button_opr->init(minor); 
35     return 0; 
36 } 
37 
38 static ssize_t button_read (struct file *file, char __user *buf, size_t size, loff_t *off) 
39 { 
40     unsigned int minor = iminor(file_inode(file)); 
41     char level; 
42     int err; 
43 
44     level = p_button_opr->read(minor); 
45     err = copy_to_user(buf, &level, 1); 
46     return 1; 
47 } 
48 
49 
50 static struct file_operations button_fops = { 51     .open = button_open, 
52     .read = button_read, 
53 }; 

上面第 34、44行都用到一个 button_operations指针,来自于底层硬件相关的代码。
底层代码调用 register_button_operations函数,向上提供这个结构体指针。
register_button_operations函数代码如下,它还根据底层提供的 button_operations调用
device_create,这是创建设备节点(第 62行)。

55 void register_button_operations(struct button_operations *opr) 
56 { 
57     int i; 
58 
59     p_button_opr = opr; 
60     for (i = 0; i < opr->count; i++) 
61     { 
62         device_create(button_class, NULL, MKDEV(major, i), NULL, "xxxxxx_button%d", i); 
63     } 
64 } 
65 

14.4 测试

这只是一个示例程序,还没有真正操作硬件。测试程序操作驱动程序时,只会导致驱动程序中打印信息。
首先设置交叉工具链,修改驱动 Makefile中内核的源码路径,编译驱动和测试程序。
启动开发板后,通过 NFS访问编译好驱动程序、测试程序,就可以在开发板上如下操作了:

# insmod button_drv.ko   // 装载驱动程序 
[  435.276713] button_drv: loading out-of-tree module taints kernel. 
# insmod board_xxx.ko 
# ls /dev/xxxxxx_button* -l     // 查看设备节点 
crw-------    1 root     root      236,   0 Jan 18 08:57 /dev/xxxxxx_button0 
crw-------    1 root     root      236,   1 Jan 18 08:57 /dev/xxxxxx_button1 
# ./button_test /dev/xxxxxx_button0    // 读按键 
[  450.886180] /home/book/source/04_button_drv/01_button_drv_template/board_xxx.c board_xxx_button_init_gpio 28, init gpio for button 0 [ 
450.910915] 
/home/book/source/04_button_drv/01_button_drv_template/board_xxx.c 
board_xxx_button_read_gpio 33, read gpio for button 0 
get button : 1    // 得到数据 

14.5 课后怎业

合并 LED、BUTTON框架驱动程序:01_led_drv_template、01_button_drv_template,合并为:
gpio_drv_template

http://www.dtcms.com/wzjs/287375.html

相关文章:

  • av做爰免费网站徐州百度seo排名优化
  • 商城网站一般建设的宽度谷歌seo教程
  • 珠海网站建设王道下拉強b2b平台是什么意思
  • 买了域名怎么做网站搜索引擎营销例子
  • 怎样做免费外贸网站免费推广的网站平台
  • 成都市住房和城乡建设局网站百度推广怎么收费标准
  • 邢台信息港最新招聘信息seo网站优化论文
  • 广州番禺网站制作公司域名注册官网
  • 北海做网站网站建设网络营销自学网站
  • adobe可以做网站吗市场调研分析
  • 南宁网站制作哪家好免费拓客软件哪个好用
  • 滨江网站开发软文代写文案
  • 用jsp怎么做的购物网站滨州网站seo
  • 网站如何做后台清理优化大师
  • 怎么做才能设计出好的网站网站优化及推广
  • 免费认证网站最近一周新闻大事
  • 福州网站设计要多少钱seo什么意思简单来说
  • java网站建设书籍广告投放公司
  • web网页设计图片seo快速整站上排名教程
  • html5网站制作教程手机百度高级搜索入口
  • 站长统计向日葵app下载5118数据分析平台
  • 做网站要需要多少钱展示型网站有哪些
  • 外贸seo网站开发百度推广代理查询
  • 陕西建筑工程网网站页面的优化
  • 400网站推广关键词排名推广公司
  • 免费网站建设绑定域名域名注册服务机构
  • 山东省春季高考网站建设试题个人做外贸怎样起步
  • 浙江网站建设报价全免费建立自己的网站
  • wordpress 网店seo优化排名易下拉用法
  • 网站上的logo怎么做肇庆网站建设