一. 简介
前面一篇文章实现了 按键的字符设备驱动代码框架,文章地址如下:
Linux按键输入实验-按键的字符设备驱动代码框架-CSDN博客
本文在 字符设备驱动框架实现的基础上,加入按键GPIO的初始化功能。
二. Linux按键输入实验-按键的GPIO初始化
1. 驱动代码完善
这里在前面按键的字符设备驱动代码框架的基础上,添加按键的GPIO初始化代码。
打开 11_key工程代码,key.c文件添加 GPIO初始化代码后如下:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>#define KEY_CNT 1
#define KEY_NAME "key"
#define INVALID_VALUE 0x00 //按键未按下值
#define VALID_VALUE 0x01 //按键按下值 static int key_open(struct inode *inode, struct file *filp);
ssize_t key_read(struct file * filp, char __user * buf, size_t count, loff_t * ppos);
int key_close(struct inode * inode, struct file * filp);//按键的设备结构体
struct key_dev{dev_t devid; //主设备号+次设备号int major; //主设备号int minor; //次设备号struct cdev key_cdev;struct class* key_class;struct device* device;struct device_node * dev_node;//设备节点int gpio_number;//gpio编号atomic_t key_value; //按键值//这里使用原子变量作为按键值类型
};struct key_dev key;static const struct file_operations key_fops = {.open = key_open,.owner = THIS_MODULE,.read = key_read,.release = key_close,
};/* 打开设备函数*/
static int key_open(struct inode *inode, struct file *filp)
{filp->private_data = &key;return 0;
}/* 从设备中读取数据函数*/
ssize_t key_read(struct file * filp, char __user * buf, size_t count, loff_t * ppos)
{int ret = 0, value = 0;struct key_dev* dev = filp->private_data;if(0 == gpio_get_value(dev->gpio_number)) //按键被按下{while(!gpio_get_value(dev->gpio_number));atomic_set(&dev->key_value, VALID_VALUE);} else //按键未按下{atomic_set(&dev->key_value, INVALID_VALUE);}value = atomic_read(&dev->key_value);ret = copy_to_user(buf, &value, sizeof(value));if(ret < 0){printk("kernel send to app for data failed!\n");return -1;}return 0;
}/* 关闭设备函数 */
int key_close(struct inode * inode, struct file * filp)
{return 0;
}/*按键的GPIO初始化函数*/
static int key_gpio_init(struct key_dev* dev)
{int ret = 0;/*1. 获取按键的设备节点*/dev->dev_node = of_find_node_by_path("/key");if(NULL == dev->dev_node){printk("find device node failed!\n");ret = -EINVAL;goto find_devnode;}/*2. 获取GPIO编号 */dev->gpio_number = of_get_named_gpio(dev->dev_node, "key-gpio", 0);if((dev->gpio_number) < 0){printk("get device number failed!\n");ret = -EINVAL;goto get_dev_number;}/*3.请求IO */ret = gpio_request(dev->gpio_number, "key");if(ret < 0){printk("gpio_request failed!\n");goto gpio_request;}/*4. 设置GPIO为输入功能*/ret = gpio_direction_input(dev->gpio_number);if(ret < 0){printk("set input io\n");goto set_input_io;}return 0;set_input_io:gpio_free(dev->gpio_number);
gpio_request:
get_dev_number:
find_devnode:return ret;
}/*模块入口函数*/
static int __init key_init(void)
{ int ret = 0;key.major = 0;//按键值初始化atomic_set(&key.key_value, INVALID_VALUE);/*1. 申请设备号*/if(key.major) //如果给出主设备号,则注册设备号{key.devid = MKDEV(key.major, 0);ret = register_chrdev_region(key.devid, KEY_CNT, KEY_NAME); }else //否则申请设备号{ret = alloc_chrdev_region(&key.devid, 0, KEY_CNT, KEY_NAME);key.major = MAJOR(key.devid);key.minor = MINOR(key.devid);}printk("major: %d minor: %d\n", key.major,key.minor);if(ret < 0){printk("devid apply failed!\n");goto devid_failed;}/*2. 设备初始化,添加设备*/key.key_cdev.owner = THIS_MODULE;cdev_init(&key.key_cdev, &key_fops);ret = cdev_add(&key.key_cdev, key.devid, KEY_CNT);if(ret < 0){printk("cdev_add failed!\n");goto cdev_init_failed;}/*3. 自动创建设备节点*/key.key_class = class_create(THIS_MODULE, KEY_NAME);if (IS_ERR(key.key_class)) {printk(KERN_ERR "class_create failed!\n");ret = PTR_ERR(key.key_class);goto class_create_failed;}key.device = device_create(key.key_class, NULL, key.devid, NULL, KEY_NAME);if (IS_ERR(key.device)) {printk(KERN_ERR "class_create failed!\n");ret = PTR_ERR(key.device);goto device_create_failed;}/*4. 按键的GPIO初始化*/key_gpio_init(&key);return 0;device_create_failed:class_destroy(key.key_class);
class_create_failed: cdev_del(&key.key_cdev);
cdev_init_failed:unregister_chrdev_region(key.devid, KEY_CNT);
devid_failed:return ret;
} /*模块出口函数*/
static void __exit key_exit(void)
{/*1. 释放设备编号 */gpio_free(key.gpio_number);/*2. 删除设备*/cdev_del(&key.key_cdev);/*3. 注销设备号 */unregister_chrdev_region(key.devid, KEY_CNT);/*4. 摧毁设备/摧毁类*/device_destroy(key.key_class, key.devid);class_destroy(key.key_class);
}/*模块入口与出口*/
module_init(key_init);
module_exit(key_exit);
MODULE_LICENSE("GPL"); //驱动License
MODULE_AUTHOR("WeiWuXian"); //作者
2. 编译驱动
对上面驱动代码进行编译:
wangtian@wangtian-virtual-machine:~/zhengdian_Linux/Linux_Drivers/11_key$ make
make -C /home/wangtian/zhengdian_Linux/linux/kernel/linux-imx-rel_imx_4.1.15_2.1.0_ga M=/home/wangtian/zhengdian_Linux/Linux_Drivers/11_key modules
make[1]: 进入目录“/home/wangtian/zhengdian_Linux/linux/kernel/linux-imx-rel_imx_4.1.15_2.1.0_ga”CC [M] /home/wangtian/zhengdian_Linux/Linux_Drivers/11_key/key.oBuilding modules, stage 2.MODPOST 1 modulesCC /home/wangtian/zhengdian_Linux/Linux_Drivers/11_key/key.mod.oLD [M] /home/wangtian/zhengdian_Linux/Linux_Drivers/11_key/key.ko
make[1]: 离开目录“/home/wangtian/zhengdian_Linux/linux/kernel/linux-imx-rel_imx_4.1.15_2.1.0_ga”
wangtian@wangtian-virtual-machine:~/zhengdian_Linux/Linux_Drivers/11_key$
可以看出,驱动模块可以正常编译。
三. 编写测试应用程序
按键驱动程序的测试,需要用到应用程序,这里编写测试(应用)程序。可以参考前面实验中的测试程序 led_app.c的实现,这里创建 key_app.c文件。
key.app.c文件代码完成后如下:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>#define INVALID_VALUE 0x00 //按键未按下值
#define VALID_VALUE 0x01 //按键按下值 /*
*测试按键是否按下
* 运行命令: ./key_app /dev/key
*/
int main(int argc, char* argv[])
{int fd = 0;char * device_name = NULL;int key_value = 0;if(argc != 2){printf("main's param number error!\n");return -1;}device_name = argv[1];fd = open(device_name, O_RDWR);if(fd < 0){printf("open led device failed!\n");return -1;}while(1){read(fd, &key_value, sizeof(key_value));if(VALID_VALUE == key_value) //按键被按下{printf("Key0 Press: key_value: %d\n", key_value);}}close(fd);return 0;
}
编译测试程序:
ubuntu进入 11_key工程目录下,对测试程序进行编译:
wangtian@wangtian-virtual-machine:~/zhengdian_Linux/Linux_Drivers/11_key$ arm-linux-gnueabihf-gcc key_app.c -o key_app
测试程序也可以编译通过。接下来就是对驱动模块进行测试,测试按键功能是否可用。