1.设备树代码

按键实验(不带input子系统)_#include
按键实验(不带input子系统)_#include_02

2.驱动代码

#include <linux/types.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/ide.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_irq.h>
#include <asm/mach/map.h>

#define KEY_CNT 1
#define KEY_NAME "key"
#define KEY0VALUE 0xF0 /* 定义按键值 */
#define INVAKEY 0x00 /* 无效按键值 */

/* key设备结构体 */
struct key_dev{
dev_t devid; /* 设备号 */
struct cdev cdev; /* 字符设备 */
struct class *class; /* 类 */
struct device *device; /* 设备 */
int major; /* 注设备号 */
int minor; /* 次设备号 */
struct device_node *nd; /* 设备节点 */
int key_gpio; /* 设备树里面的编号 */
atomic_t keyValue; /* 按键的值,用来读取 */
};

struct key_dev key; /* 定义LED结构体 */

static int keyio_init(void)
{
int ret;
/* 1. 获取设备节点 */
key.nd = of_find_node_by_path("/key");
if( key.nd == NULL){
ret = -EINVAL;
printk("key node can not found!\r\n");
return ret;
}

/* 2. 获取设备所对应的gpio */
/* 注意第三个参数,因为只有一个索引,所以是0 */
key.key_gpio = of_get_named_gpio(key.nd, "key-gpios", 0);
if( key.key_gpio < 0) {
printk("Can't find key gpio \r\n");
ret = -EINVAL;
return ret;
}
printk("Led gpio num = %d. \r\n", key.key_gpio);

/* 3. 获取到IO之后,申请IO */
/* 作用是申请之后,就能知道该IO是否被其他函数占用 */
/* 如果申请失败,说明被战用啦 */
/* 解决方法:1.搜索所需管脚 2.在搜索复用管脚,例如 &gpio1 3 */
ret = gpio_request(key.key_gpio, "key0");
if(ret) {
printk("Failed to request the led gpio! \r\n");
ret = -EINVAL;
return ret;
}

/* 4. 使用IO */
ret = gpio_direction_input(key.key_gpio); /* 设置输出,默认高电平不点亮 */
if(ret < 0){
printk("Failed to set input or output! \r\n");
gpio_free(key.key_gpio);
return ret;
}

printk("key init! \r\n");
return 0;
}

static int key_open(struct inode *inode, struct file *filp)
{
int ret;

filp->private_data = &key;

ret = keyio_init();
if(ret < 0){
return ret;
}
return 0;
}

static int key_release(struct inode *inode, struct file *filp)
{
//struct key_dev *dev = (struct key_dev *)filp->private_data;
return 0;
}

static ssize_t key_write(struct file *filp, const char __user *buf, size_t count, loff_t *ppos)
{
return 0;
}

static ssize_t key_read(struct file *filp, char __user *buf,
size_t cnt, loff_t *offt)
{
/* 获取私有数据 */
struct key_dev *dev = (struct key_dev *)filp->private_data;
int ret = 0;
unsigned char value;

if (gpio_get_value(dev->key_gpio) == 0) { /* key0 按下 */
while(!gpio_get_value(dev->key_gpio)); /* 等待按键释放 */
atomic_set(&dev->keyValue, KEY0VALUE);
} else { /* 无效的按键值 */
atomic_set(&dev->keyValue, INVAKEY);
}

value = atomic_read(&dev->keyValue); /* 保存按键值 */
ret = copy_to_user(buf, &value, sizeof(value));
return ret;
}

/* 字符设备操作集 */
static const struct file_operations key_fops = {
.owner = THIS_MODULE,
.write = key_write,
.open = key_open,
.read = key_read,
.release = key_release,
};

/* 模块入口函数 */
static int __init myKey_init(void)
{
/* 定义一些所需变量 */
int ret = 0;
/* 初始化原子变量 */
atomic_set(&key.keyValue, INVAKEY);

/* 1. 注册字符设备驱动 */
key.major = 0;
if(key.major) {
key.devid = MKDEV(key.major, 0);
ret = register_chrdev_region(key.devid, KEY_CNT, KEY_NAME);
} else {
alloc_chrdev_region(&key.devid, 0, KEY_CNT, KEY_NAME);
key.major = MAJOR(key.devid);
key.minor = MINOR(key.devid);
}
if(ret < 0){
goto fail_devid;
}
printk("Make devid success! \r\n");
printk("major = %d, minor = %d \r\n", key.major, key.minor);

/* 2. 初始化cdev */
key.cdev.owner = THIS_MODULE;
cdev_init(&key.cdev, &key_fops);
ret = cdev_add(&key.cdev, key.devid, KEY_CNT);
if (ret < 0){
goto fail_cdev;
} else {
printk("Cdev add sucess! \r\n");
}

/* 3. 自动创建设备节点 */
key.class = class_create(THIS_MODULE, KEY_NAME);
if(IS_ERR(key.class)) {
ret = PTR_ERR(key.class);
goto fail_class;
} else {
printk("Class create sucess! \r\n");
}

key.device = device_create(key.class, NULL, key.devid, NULL, KEY_NAME);
if(IS_ERR(key.device)) {
ret = PTR_ERR(key.device);
goto fail_device;
} else {
printk("Device create sucess! \r\n");
}

return 0;

/* 错误处理 */
fail_device:
class_destroy(key.class);
fail_class:
cdev_del(&key.cdev);
fail_cdev:
unregister_chrdev_region(key.devid, KEY_CNT);
fail_devid:
return ret;
}

/* 模块出口函数 */
static void __exit myKey_exit(void)
{
/* 1. 释放设备号 */
cdev_del(&key.cdev);
/* 2. 注销设备号 */
unregister_chrdev_region(key.devid, KEY_CNT);
/* 3. 摧毁设备 */
device_destroy(key.class, key.devid);
/* 4.摧毁类 */
class_destroy(key.class);

printk("key exit! \r\n");
}

/* 模块入口和出口注册 */
module_init(myKey_init);
module_exit(myKey_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Shao Zheming");

3.应用代码

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

#define KEY0VALUE 0xF0 /* 定义按键值 */
#define INVAKEY 0x00 /* 无效按键值 */

int main(int argc, char *argv[])
{
if(argc != 2)
{
printf("Error Usage!\r\n");
return -1;
}

char *filename;
int fd, retValue;
unsigned char keyValue;

filename = argv[1];
fd = open(filename, O_RDWR);
if(fd < 0)
{
printf("file %s open failed! \r\n", filename);
return -1;
}

/* 循环读取按键值数据! */
while(1) {
read(fd, &keyValue, sizeof(keyValue));
if (keyValue == KEY0VALUE) { /* KEY0 */
printf("KEY0 Press, value = %#X\r\n", keyValue);/* 按下 */
}
}

retValue = close(fd);
if(retValue < 0){
printf("file %s close failed!\r\n", argv[1]);
}
return 0;
}

4.实验效果

![image](​​https://img20​​​按键实验(不带input子系统)_linux_03

主要是给自己看的,所以肯定会出现很多错误哈哈哈哈哈