設備樹配置
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;
}
效果: