设备树配置
key{compatible = "alientek,key";pinctrl-0 = <&key_gpio>;pinctrl-names = "alientek,key";key-gpio = <&gpio3 RK_PC5 GPIO_ACTIVE_HIGH>;status = "okay";};
配置信息方便后面直接引用:
// Narnat 2025-6-14key-gpios{/omit-if-no-ref/key_gpio: key-pin {rockchip,pins=<3 RK_PC5 RK_FUNC_GPIO &pcfg_pull_none>;};};
注册节点,将信息写入设备,方便后续驱动读取设备
驱动代码
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <linux/semaphore.h>
#include <asm/uaccess.h>
#include <asm/io.h>#define KEY_CNT 1 // 设备号个数
#define KEY_NAME "key" // 名字#define KEY0VALUE 0xf0 // 按键值
#define INVAKEY 0x00 // 无效按键值/* 设备结构体 */
struct key_dev{dev_t devid; // 设备号struct cdev cdev; // cdevstruct class* class; // 类struct device* device; // 设备int major; // 主设备号int minor; // 次设备号struct device_node* nd; // 设备节点int key_gpio; // key所使用的gpio编号atomic_t keyvalue; // 按键值
};static struct key_dev keydev; // 按键值static int keyio_init(void){int ret;const char* str;/*设置LED所使用GPIO*/// 1.获取节点keydevkeydev.nd = of_find_node_by_path("/key");if(keydev.nd == NULL){printk("keydev node not find! \r\n");return -1;}// 2.读取status属性ret = of_property_read_string(keydev.nd, "status", &str);if(ret < 0) return -1;if(strcmp(str, "okay")) return -1;// 3.获取compatible属性并对比ret = of_property_read_string(keydev.nd, "compatible", &str);if(ret < 0){printk("failed to get compatible\n");return -1;}if(strcmp(str, "alientek,key")){printk("compatible match failed\n");return -1;}// 4.获取gpio属性,得到kEY编号keydev.key_gpio = of_get_named_gpio(keydev.nd, "key-gpio", 0);if(keydev.key_gpio < 0){printk("cant get key-gpio \n");return -1;}printk("key-gpio num= %d \n", keydev.key_gpio);// 5.向gpio子系统申请使用gpioret = gpio_request(keydev.key_gpio, "KEY0");if(ret){printk("failed to request key-gpio \n");return ret;}// 6.设置gpio输入模式ret = gpio_direction_input(keydev.key_gpio);if(ret < 0){printk("cant set gpio \n");return ret;}return 0;
}static int key_open(struct inode* inode, struct file* filp){int ret = 0;filp->private_data = &keydev;ret = keyio_init();if(ret < 0) return ret;return 0;
}static ssize_t key_read(struct file* filp, char __user* buf, size_t cnt, loff_t* offt){int ret = 0;int value;struct key_dev* dev = filp->private_data;if(gpio_get_value(dev->key_gpio) == 1){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 ssize_t key_write(struct file* filp, const char __user* buf, size_t cnt, loff_t* offt){return 0;
}static int key_release(struct inode* inode, struct file* filp){struct key_dev* dev = filp->private_data;gpio_free(dev->key_gpio);return 0;
}
// 设备操作函数
static struct file_operations key_fops = {.owner = THIS_MODULE,.open = key_open,.read = key_read,.write = key_write,.release = key_release,
};/* 驱动入口 */
static int __init mykey_init(void){int ret;// 1.初始化原子变量keydev.keyvalue = (atomic_t)ATOMIC_INIT(0);// 2.初始值设置为INVAKEYatomic_set(&keydev.keyvalue, INVAKEY);/*注册字符驱动*/// 1.创建设备号if(keydev.major){ // 定义了设备号keydev.devid = MKDEV(keydev.major, 0);ret = register_chrdev_region(keydev.devid, KEY_CNT, KEY_NAME);if(ret < 0){printk("cannt register %s char driver \n", KEY_NAME);return -1;}}else{ // 未定义设备号ret = alloc_chrdev_region(&keydev.devid, 0, KEY_CNT, KEY_NAME); // 申请设备号if(ret < 0){printk("%s couldnot alloc_chrdev_region \n", KEY_NAME);return -1;}keydev.major = MAJOR(keydev.devid); // 分配主设备号keydev.minor = MINOR(keydev.devid); // 分配次设备}printk("keydev major=%d, minor=%d \r\n", keydev.major, keydev.minor);// 2.初始化cdevkeydev.cdev.owner = THIS_MODULE;cdev_init(&keydev.cdev, &key_fops);// 3.添加一个cdevret = cdev_add(&keydev.cdev, keydev.devid, KEY_CNT);if(ret < 0) goto del_unregister;// 4.创建类keydev.class = class_create(THIS_MODULE, KEY_NAME);if(IS_ERR(keydev.class)) goto del_cdev;// 5.创建设备keydev.device = device_create(keydev.class, NULL, keydev.devid, NULL, KEY_NAME);if(IS_ERR(keydev.device)) goto destroy_class; return 0;
destroy_class:device_destroy(keydev.class, keydev.devid);
del_cdev:cdev_del(&keydev.cdev);
del_unregister:unregister_chrdev_region(keydev.devid, KEY_CNT);return -1;
}/* 驱动出口 */
static void __exit mykey_exit(void){// 注销字符设备驱动cdev_del(&keydev.cdev); // 删除cdevunregister_chrdev_region(keydev.devid, KEY_CNT); // 注销设备号device_destroy(keydev.class, keydev.devid);class_destroy(keydev.class);
}module_init(mykey_init);
module_exit(mykey_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Narnat");
补全read函数,应用层调用read命令后读取到驱动发送的数据
应用层:
#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"
/***************************************************************
Copyright © ALIENTEK Co., Ltd. 1998-2029. All rights reserved.
文件名 : keyApp.c
作者 : 正点原子Linux团队
版本 : V1.0
描述 : 按键输入测试应用程序
其他 : 无
使用方法 :./keyApp /dev/key
论坛 : www.openedv.com
日志 : 初版V1.0 2021/01/5 正点原子Linux团队创建
***************************************************************//* 定义按键值 */
#define KEY0VALUE 0XF0
#define INVAKEY 0X00/** @description : main主程序* @param - argc : argv数组元素个数* @param - argv : 具体参数* @return : 0 成功;其他 失败*/
int main(int argc, char *argv[])
{int fd, ret;char *filename;int keyvalue;if(argc != 2){printf("Error Usage!\r\n");return -1;}filename = argv[1];/* 打开key驱动 */fd = open(filename, O_RDWR);if(fd < 0){printf("file %s open failed!\r\n", argv[1]);return -1;}/* 循环读取按键值数据! */while(1) {read(fd, &keyvalue, sizeof(keyvalue));if (keyvalue == KEY0VALUE) { /* KEY0 */printf("KEY0 Press, value = %#X\r\n", keyvalue); /* 按下 */}}ret= close(fd); /* 关闭文件 */if(ret < 0){printf("file %s close failed!\r\n", argv[1]);return -1;}return 0;
}
效果: