From 708e5a98132dfa5bb9d550057934439c9c2421ff Mon Sep 17 00:00:00 2001 From: sushe_Ubuntu Date: Tue, 16 Jan 2024 22:46:38 +0800 Subject: [PATCH 1/4] =?UTF-8?q?key=E5=AE=9E=E9=AA=8C=E5=BB=BA=E7=AB=8B?= =?UTF-8?q?=E5=A5=BD=E4=BA=86=E6=96=87=E4=BB=B6=E5=A4=B9=E5=92=8C=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E9=87=8D=E5=91=BD=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: sushe_Ubuntu --- 11_key/.vscode/c_cpp_properties.json | 21 +++++++++ 11_key/.vscode/settings.json | 21 +++++++++ 11_key/11_key.code-workspace | 11 +++++ 11_key/Makefile | 13 ++++++ 11_key/key.c | 17 +++++++ 11_key/keyApp.c | 68 ++++++++++++++++++++++++++++ 6 files changed, 151 insertions(+) create mode 100644 11_key/.vscode/c_cpp_properties.json create mode 100644 11_key/.vscode/settings.json create mode 100644 11_key/11_key.code-workspace create mode 100644 11_key/Makefile create mode 100644 11_key/key.c create mode 100644 11_key/keyApp.c diff --git a/11_key/.vscode/c_cpp_properties.json b/11_key/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..7ee3d5b --- /dev/null +++ b/11_key/.vscode/c_cpp_properties.json @@ -0,0 +1,21 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/**", + "/home/youxiu/linux/linux-imx-rel_imx_4.1.15_2.1.0_ga/include/**", + "/home/youxiu/linux/linux-imx-rel_imx_4.1.15_2.1.0_ga/arch/arm/include/**", + "/home/youxiu/linux/linux-imx-rel_imx_4.1.15_2.1.0_ga/arch/arm/include/generated/**", + "/home/youxiu/linux/linux-imx-rel_imx_4.1.15_2.1.0_ga/drivers/**" + ], + "defines": [], + "compilerPath": "/usr/bin/clang", + "cStandard": "c11", + "cppStandard": "c++17", + "intelliSenseMode": "clang-x64" + } + ], + "version": 4 +} + diff --git a/11_key/.vscode/settings.json b/11_key/.vscode/settings.json new file mode 100644 index 0000000..1a2a04a --- /dev/null +++ b/11_key/.vscode/settings.json @@ -0,0 +1,21 @@ +{ + "search.exclude": { + "**/node_modules": true, + "**/bower_components": true, + "**/*.o":true, + "**/*.su":true, + "**/*.cmd":true, + "Documentation":true, + }, + "files.exclude": { + "**/.git": true, + "**/.svn": true, + "**/.hg": true, + "**/CVS": true, + "**/.DS_Store": true, + "**/*.o":true, + "**/*.su":true, + "**/*.cmd":true, + "Documentation":true, + } +} \ No newline at end of file diff --git a/11_key/11_key.code-workspace b/11_key/11_key.code-workspace new file mode 100644 index 0000000..53a2b99 --- /dev/null +++ b/11_key/11_key.code-workspace @@ -0,0 +1,11 @@ +{ + "folders": [ + { + "path": "." + } + ], + "settings": { + + } + } +} \ No newline at end of file diff --git a/11_key/Makefile b/11_key/Makefile new file mode 100644 index 0000000..c8ee05b --- /dev/null +++ b/11_key/Makefile @@ -0,0 +1,13 @@ +KERNELDIR := /home/youxiu/linux/linux-imx-rel_imx_4.1.15_2.1.0_ga +CURRENT_PATH := $(shell pwd) +obj-m := key.o + +build: kernel_modules + +kernel_modules: + $(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) modules + +clean: + $(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) clean + + diff --git a/11_key/key.c b/11_key/key.c new file mode 100644 index 0000000..45e6548 --- /dev/null +++ b/11_key/key.c @@ -0,0 +1,17 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + diff --git a/11_key/keyApp.c b/11_key/keyApp.c new file mode 100644 index 0000000..90c1eb7 --- /dev/null +++ b/11_key/keyApp.c @@ -0,0 +1,68 @@ +#include "stdio.h" +#include "unistd.h" +#include "sys/types.h" +#include "sys/stat.h" +#include "fcntl.h" +#include "string.h" +#include "stdlib.h" + +#define LEDOFF 0 /* LED关 */ +#define LEDPON 1 /* LED开 */ + +int main(int argc,char *argv[]) +{ + int fd,retvalue; + char *filename; + unsigned char ledstate[1]; + u_int8_t cnt = 0; + + if (argc !=3) + { + printf("Error Usage!\r\n"); + return -1; + } + + filename = argv[1]; + + /* 打开驱动文件 */ + fd = open(filename,O_RDWR); + if (fd < 0) + { + printf("Can't open file %s\r\n",filename); + return -1; + } + + ledstate[0] = atoi(argv[2]); + retvalue = write(fd,ledstate,sizeof(ledstate)); + if (retvalue < 0) + { + printf("LED Control failed!\r\n"); + close(fd); + return -1; + } + + + + while (1) + { + sleep(5); + cnt++; + printf("App runing times: %d\r\n",cnt); + if (cnt >=5) + { + break; + } + + } + printf("App runing finished!\r\n"); + retvalue = close(fd); + if (retvalue < 0) + { + printf("Can't close file %s \r\n",filename); + return -1; + } + + return 0; +} + + -- Gitee From a55ae4c400840aca0371bbf9b65bec5888900e31 Mon Sep 17 00:00:00 2001 From: sushe_Ubuntu Date: Wed, 17 Jan 2024 22:05:27 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E5=86=99=E5=A5=BD=E4=BA=86=E9=A9=B1?= =?UTF-8?q?=E5=8A=A8=E5=9F=BA=E6=9C=AC=E6=A1=86=E6=9E=B6=E5=92=8Cinit()?= =?UTF-8?q?=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: sushe_Ubuntu --- 11_key/key.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/11_key/key.c b/11_key/key.c index 45e6548..0aeceb6 100644 --- a/11_key/key.c +++ b/11_key/key.c @@ -15,3 +15,91 @@ #include #include +#define KEY_CNT 1 /* 设备号个数 */ +#define KEY_NAME "key" /* 名字 */ + +/* 定义按键值 */ +#define KEY0VALUE 0xF0; /* 按键值*/ +#define INVAKEY 0x00 /* 无效的按键值 */ + + +/* key设备结构体 */ +struct key_dev{ + dev_t devid; /* 设备号 */ + struct cdev cedv; /* cdev */ + struct class *class; /* 类 */ + struct device *device; /* 设备 */ + int major; /* 主设备号 */ + int minor; /* 次设备号 */ + struct device_node *nd; /* 设备节点 */ + int key_gpio; + atomic_t keyvalue; /* 按键值*/ +}; + +struct key_dev keydev; + + +static int key_open(struct inode *inode, struct file *filp) +{ + +} + + +static ssize_t key_read(struct file *filp, char __user *buf, + size_t cnt,loff_t *offt) +{ + +} + +static struct file_operations key_fops = { + .owner = THIS_MODULE, + .open = key_open, + .read = key_read, +}; + +static __init mykey_init(void) +{ + /* 初始化源自变量 */ + atomic_set(&keydev.keyvalue,INVAKEY); + /* 注册字符设备驱动 */ + /* 1、创建设备号 */ + if(keydev.major){ /* 如果定义了设备号 */ + keydev.devid = MKDEV(keydev.major,0); + register_chrdev_regin(keydev.devid,KEY_CNT,KEY_NAME); + }else { /* 没有定义设备号 */ + alloc_chrdev_regin(&keydev.devid,0,KEY_CNT,KEY_NAME); + keydev.major = MAJOR(keydev.devid); /* 获取分配的主设备号 */ + keydev.minor = MINOR(keydev.devid); /* 获取分配的次设备号 */ + } + + /* 2、初始化cdev */ + keydev.cedv.owner = THIS_MODULE; + cdev_init(&keydev.cedv,&key_fops); + + /* 3、添加一个cdev */ + cdev_add(&keydev.cdev,keydev.devid,KEY_CNT); + + /* 4、创建类 */ + keydev.class = class_create(THIS_MODULE,KEY_NAME); + if(IS_ERR(keydev.class)){ + return PTR_ERR(keydev.class); + } + + /* 5、创建设备 */ + keydev.device = device_create(keydev.class,NULL,keydev.devid,NULL,KEY_NAME); + if(IS_ERR(keydev.device)){ + return PTR_ERR(keydev.device); + } + + return 0; +} + +static __exit mykey_exit(void) +{ + +} + +module_init(mykey_init); +module_exit(mykey_exit); +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("zhangchaoliang"); -- Gitee From 2feb4bfd6db203e832830eb52850377f9e927967 Mon Sep 17 00:00:00 2001 From: sushe_Ubuntu Date: Thu, 18 Jan 2024 22:56:35 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E5=AE=8C=E6=88=90key.c=E9=A9=B1=E5=8A=A8?= =?UTF-8?q?=EF=BC=8C=E6=8E=A5=E4=B8=8B=E9=9C=80=E8=A6=81=E6=9D=A5=E5=AE=8C?= =?UTF-8?q?=E6=88=90keyApp.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: sushe_Ubuntu --- 11_key/key.c | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/11_key/key.c b/11_key/key.c index 0aeceb6..71431ea 100644 --- a/11_key/key.c +++ b/11_key/key.c @@ -41,14 +41,41 @@ struct key_dev keydev; static int key_open(struct inode *inode, struct file *filp) { + filp.private_data = &keydev; + keydev.nd = of_find_node_by_path("/key"); + if(keydev.nd == NULL){ + return -EINVAL; + } -} + keydev.key_gpio = of_get_named_gpio(keydev.nd, "key-gpio",0); + if(keydev.key_gpio < 0) { + printk("can't get key0\r\n"); + return -EINVAL; + } + printk("key_gpio = %d\r\n", keydev.key_gpio); + /* 初始化key所使用的IO */ + gpio_request(keydev.key_gpio, "key0"); + gpio_direction_input(keydev.key_gpio); + return 0; +} static ssize_t key_read(struct file *filp, char __user *buf, size_t cnt,loff_t *offt) { - + int ret = 0; + unsigned char value; + struct key_dev *dev = filp->private_data; + if(gpio_get_value(dev->key_gpio) == 0){ + while(!gpio_get_value(dev->key_gpio)) + ; + atomic_set(&dev.keyvalue,KEY0VALUE); + }else{ + atomic_set(&dev.keyvalue,INVAKEY); + } + value = atomic_long_read(&dev->keyvalue); + ret = copy_to_user(buf,&value,sizeof(value)); + return ret; } static struct file_operations key_fops = { @@ -96,7 +123,13 @@ static __init mykey_init(void) static __exit mykey_exit(void) { + //注销字符设备驱动 + gpio_free(keydev.key_gpio); + cdev_dev(&keydev.cdev); + unregister_chrdev_region(keydev.devid,KEY_CNT); + device_destroy(keydev.class,keydev.devid); + class_destroy(keydev.class); } module_init(mykey_init); -- Gitee From a9a7d51678ab7b6c7d4683b3437509f5366beac8 Mon Sep 17 00:00:00 2001 From: sushe_Ubuntu Date: Mon, 11 Mar 2024 12:46:04 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E5=AE=8C=E6=88=90keyApp=EF=BC=8C=E5=B9=B6?= =?UTF-8?q?=E4=B8=94=E5=AE=9E=E9=AA=8C=E6=88=90=E5=8A=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: sushe_Ubuntu --- 11_key/11_key.code-workspace | 3 +- 11_key/key.c | 22 ++++---- 11_key/keyApp.c | 99 +++++++++++++++--------------------- 3 files changed, 51 insertions(+), 73 deletions(-) diff --git a/11_key/11_key.code-workspace b/11_key/11_key.code-workspace index 53a2b99..f5bceea 100644 --- a/11_key/11_key.code-workspace +++ b/11_key/11_key.code-workspace @@ -4,8 +4,7 @@ "path": "." } ], - "settings": { + "settings":{ } - } } \ No newline at end of file diff --git a/11_key/key.c b/11_key/key.c index 71431ea..d8dc4c4 100644 --- a/11_key/key.c +++ b/11_key/key.c @@ -19,14 +19,14 @@ #define KEY_NAME "key" /* 名字 */ /* 定义按键值 */ -#define KEY0VALUE 0xF0; /* 按键值*/ +#define KEY0VALUE 0xF0 /* 按键值*/ #define INVAKEY 0x00 /* 无效的按键值 */ /* key设备结构体 */ struct key_dev{ dev_t devid; /* 设备号 */ - struct cdev cedv; /* cdev */ + struct cdev cdev; /* cdev */ struct class *class; /* 类 */ struct device *device; /* 设备 */ int major; /* 主设备号 */ @@ -38,10 +38,9 @@ struct key_dev{ struct key_dev keydev; - static int key_open(struct inode *inode, struct file *filp) { - filp.private_data = &keydev; + filp->private_data = &keydev; keydev.nd = of_find_node_by_path("/key"); if(keydev.nd == NULL){ return -EINVAL; @@ -69,9 +68,9 @@ static ssize_t key_read(struct file *filp, char __user *buf, if(gpio_get_value(dev->key_gpio) == 0){ while(!gpio_get_value(dev->key_gpio)) ; - atomic_set(&dev.keyvalue,KEY0VALUE); + atomic_set(&dev->keyvalue,KEY0VALUE); }else{ - atomic_set(&dev.keyvalue,INVAKEY); + atomic_set(&dev->keyvalue,INVAKEY); } value = atomic_long_read(&dev->keyvalue); ret = copy_to_user(buf,&value,sizeof(value)); @@ -92,20 +91,19 @@ static __init mykey_init(void) /* 1、创建设备号 */ if(keydev.major){ /* 如果定义了设备号 */ keydev.devid = MKDEV(keydev.major,0); - register_chrdev_regin(keydev.devid,KEY_CNT,KEY_NAME); + register_chrdev_region(keydev.devid,KEY_CNT,KEY_NAME); }else { /* 没有定义设备号 */ - alloc_chrdev_regin(&keydev.devid,0,KEY_CNT,KEY_NAME); + alloc_chrdev_region(&keydev.devid,0,KEY_CNT,KEY_NAME); keydev.major = MAJOR(keydev.devid); /* 获取分配的主设备号 */ keydev.minor = MINOR(keydev.devid); /* 获取分配的次设备号 */ } /* 2、初始化cdev */ - keydev.cedv.owner = THIS_MODULE; - cdev_init(&keydev.cedv,&key_fops); + keydev.cdev.owner = THIS_MODULE; + cdev_init(&keydev.cdev,&key_fops); /* 3、添加一个cdev */ cdev_add(&keydev.cdev,keydev.devid,KEY_CNT); - /* 4、创建类 */ keydev.class = class_create(THIS_MODULE,KEY_NAME); if(IS_ERR(keydev.class)){ @@ -125,7 +123,7 @@ static __exit mykey_exit(void) { //注销字符设备驱动 gpio_free(keydev.key_gpio); - cdev_dev(&keydev.cdev); + cdev_del(&keydev.cdev); unregister_chrdev_region(keydev.devid,KEY_CNT); device_destroy(keydev.class,keydev.devid); diff --git a/11_key/keyApp.c b/11_key/keyApp.c index 90c1eb7..52d7b5a 100644 --- a/11_key/keyApp.c +++ b/11_key/keyApp.c @@ -3,66 +3,47 @@ #include "sys/types.h" #include "sys/stat.h" #include "fcntl.h" -#include "string.h" #include "stdlib.h" +#include "string.h" -#define LEDOFF 0 /* LED关 */ -#define LEDPON 1 /* LED开 */ - -int main(int argc,char *argv[]) -{ - int fd,retvalue; - char *filename; - unsigned char ledstate[1]; - u_int8_t cnt = 0; - - if (argc !=3) - { - printf("Error Usage!\r\n"); - return -1; - } - - filename = argv[1]; - - /* 打开驱动文件 */ - fd = open(filename,O_RDWR); - if (fd < 0) - { - printf("Can't open file %s\r\n",filename); - return -1; - } - - ledstate[0] = atoi(argv[2]); - retvalue = write(fd,ledstate,sizeof(ledstate)); - if (retvalue < 0) - { - printf("LED Control failed!\r\n"); - close(fd); - return -1; - } - - - while (1) - { - sleep(5); - cnt++; - printf("App runing times: %d\r\n",cnt); - if (cnt >=5) - { - break; - } - - } - printf("App runing finished!\r\n"); - retvalue = close(fd); - if (retvalue < 0) - { - printf("Can't close file %s \r\n",filename); - return -1; - } - - return 0; +/* 定义按键值 */ +#define KEY0VALUE 0xF0 +#define INVAKEY 0x00 + + +/* + +*/ +int main(int argc, char *argv[]){ + int fd, ret; + char *filename; + unsigned char keyvalue; + if (argc != 2) { + printf("Error Usage!\r\n"); + return -1; + } + + filename = argv[1]; + 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) { + printf("KEY0 Press,value = %#x\r\n",keyvalue);// 按下 + } + } + + ret = close(fd); // close文件 + if(ret <0){ + printf("file %s close failed\r\n",argv[1]); + return -1; + } + + return 0; } - - -- Gitee