From 46d0bafe1cbf8533b7a71dbe5315548237a2ecd2 Mon Sep 17 00:00:00 2001 From: sushe_Ubuntu Date: Mon, 11 Mar 2024 13:03:52 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9gitignore=E9=9C=80?= =?UTF-8?q?=E8=A6=81=E5=BF=BD=E7=95=A5=E7=9A=84=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: sushe_Ubuntu --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 43f9db7..fb086c9 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ */.beep* */.tmp* */*App +*/.vscode/* -- Gitee From 25d7c19bed496d5934b153d1efe7e407acbccfc3 Mon Sep 17 00:00:00 2001 From: sushe_Ubuntu Date: Mon, 11 Mar 2024 13:15:10 +0800 Subject: [PATCH 2/7] gitignore Signed-off-by: sushe_Ubuntu --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index fb086c9..fca577c 100644 --- a/.gitignore +++ b/.gitignore @@ -2,9 +2,10 @@ *_*/*.ko *_*/*.mod.c *_*/.*.cmd +*_*/.vscode/*.json */*.symvers */*.order */.beep* */.tmp* */*App -*/.vscode/* + -- Gitee From 65242f4b9b03f34df6344d9a3238706415c53d64 Mon Sep 17 00:00:00 2001 From: D301_Ubuntu <1395769145@qq.com> Date: Thu, 14 Mar 2024 18:07:18 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E5=88=9B=E5=BB=BA=E5=AE=9E=E9=AA=8C12=5Fti?= =?UTF-8?q?mer=E5=B9=B6=E9=87=8D=E5=91=BD=E5=90=8D=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: D301_Ubuntu <1395769145@qq.com> --- .gitignore | 1 + 12_timer/12_timer.code-workspace | 10 +++ 12_timer/Makefile | 13 +++ 12_timer/timer.c | 136 +++++++++++++++++++++++++++++++ 12_timer/timerAppp.c | 49 +++++++++++ 5 files changed, 209 insertions(+) create mode 100644 12_timer/12_timer.code-workspace create mode 100644 12_timer/Makefile create mode 100644 12_timer/timer.c create mode 100644 12_timer/timerAppp.c diff --git a/.gitignore b/.gitignore index 43f9db7..deae9cc 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ */.beep* */.tmp* */*App +*/.vscode/ diff --git a/12_timer/12_timer.code-workspace b/12_timer/12_timer.code-workspace new file mode 100644 index 0000000..f5bceea --- /dev/null +++ b/12_timer/12_timer.code-workspace @@ -0,0 +1,10 @@ +{ + "folders": [ + { + "path": "." + } + ], + "settings":{ + + } +} \ No newline at end of file diff --git a/12_timer/Makefile b/12_timer/Makefile new file mode 100644 index 0000000..fe20c16 --- /dev/null +++ b/12_timer/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 := timer.o + +build: kernel_modules + +kernel_modules: + $(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) modules + +clean: + $(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) clean + + diff --git a/12_timer/timer.c b/12_timer/timer.c new file mode 100644 index 0000000..d8dc4c4 --- /dev/null +++ b/12_timer/timer.c @@ -0,0 +1,136 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#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 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 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 = { + .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_region(keydev.devid,KEY_CNT,KEY_NAME); + }else { /* 没有定义设备号 */ + alloc_chrdev_region(&keydev.devid,0,KEY_CNT,KEY_NAME); + keydev.major = MAJOR(keydev.devid); /* 获取分配的主设备号 */ + keydev.minor = MINOR(keydev.devid); /* 获取分配的次设备号 */ + } + + /* 2、初始化cdev */ + 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)){ + 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) +{ + //注销字符设备驱动 + gpio_free(keydev.key_gpio); + cdev_del(&keydev.cdev); + unregister_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("zhangchaoliang"); diff --git a/12_timer/timerAppp.c b/12_timer/timerAppp.c new file mode 100644 index 0000000..52d7b5a --- /dev/null +++ b/12_timer/timerAppp.c @@ -0,0 +1,49 @@ +#include "stdio.h" +#include "unistd.h" +#include "sys/types.h" +#include "sys/stat.h" +#include "fcntl.h" +#include "stdlib.h" +#include "string.h" + + +/* 定义按键值 */ +#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 From 48dc9082dd07b758909b978364cd1af5d79127da Mon Sep 17 00:00:00 2001 From: sushe_Ubuntu Date: Sat, 16 Mar 2024 13:20:37 +0800 Subject: [PATCH 4/7] tianjia vscode files Signed-off-by: sushe_Ubuntu --- .gitignore | 6 ------ 12_timer/.vscode/c_cpp_properties.json | 21 +++++++++++++++++++++ 12_timer/.vscode/settings.json | 21 +++++++++++++++++++++ 3 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 12_timer/.vscode/c_cpp_properties.json create mode 100644 12_timer/.vscode/settings.json diff --git a/.gitignore b/.gitignore index 337f8ed..43f9db7 100644 --- a/.gitignore +++ b/.gitignore @@ -2,14 +2,8 @@ *_*/*.ko *_*/*.mod.c *_*/.*.cmd -*_*/.vscode/*.json */*.symvers */*.order */.beep* */.tmp* */*App -<<<<<<< HEAD - -======= -*/.vscode/ ->>>>>>> 65242f4b9b03f34df6344d9a3238706415c53d64 diff --git a/12_timer/.vscode/c_cpp_properties.json b/12_timer/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..c83cbcf --- /dev/null +++ b/12_timer/.vscode/c_cpp_properties.json @@ -0,0 +1,21 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/**", + "../../../linux-imx-rel_imx_4.1.15_2.1.0_ga/include/**", + "../../../linux-imx-rel_imx_4.1.15_2.1.0_ga/arch/arm/include/**", + "../../../linux-imx-rel_imx_4.1.15_2.1.0_ga/arch/arm/include/generated/**", + "../../../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/12_timer/.vscode/settings.json b/12_timer/.vscode/settings.json new file mode 100644 index 0000000..1a2a04a --- /dev/null +++ b/12_timer/.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 -- Gitee From ded3d6af76dbf6e15430426b84646666ba9d1381 Mon Sep 17 00:00:00 2001 From: D301_Ubuntu <1395769145@qq.com> Date: Sat, 16 Mar 2024 15:36:12 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E9=87=8D=E6=96=B0=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E5=BF=BD=E7=95=A5=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: D301_Ubuntu <1395769145@qq.com> --- .gitignore | 1 - 12_timer/.vscode/c_cpp_properties.json | 21 +++++++++++++++++++++ 12_timer/.vscode/settings.json | 21 +++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 12_timer/.vscode/c_cpp_properties.json create mode 100644 12_timer/.vscode/settings.json diff --git a/.gitignore b/.gitignore index d6b97f9..43f9db7 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,6 @@ *_*/*.ko *_*/*.mod.c *_*/.*.cmd -*_*/.vscode/*.json */*.symvers */*.order */.beep* diff --git a/12_timer/.vscode/c_cpp_properties.json b/12_timer/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..c83cbcf --- /dev/null +++ b/12_timer/.vscode/c_cpp_properties.json @@ -0,0 +1,21 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/**", + "../../../linux-imx-rel_imx_4.1.15_2.1.0_ga/include/**", + "../../../linux-imx-rel_imx_4.1.15_2.1.0_ga/arch/arm/include/**", + "../../../linux-imx-rel_imx_4.1.15_2.1.0_ga/arch/arm/include/generated/**", + "../../../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/12_timer/.vscode/settings.json b/12_timer/.vscode/settings.json new file mode 100644 index 0000000..1a2a04a --- /dev/null +++ b/12_timer/.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 -- Gitee From 217e6d5aa92baa7b23095c0cf4da4207335730eb Mon Sep 17 00:00:00 2001 From: D301_Ubuntu <1395769145@qq.com> Date: Sat, 16 Mar 2024 21:49:18 +0800 Subject: [PATCH 6/7] =?UTF-8?q?=E7=BC=96=E5=86=99=E5=AE=8C=E6=88=90?= =?UTF-8?q?=E5=AE=9E=E9=AA=8C12=5Ftimer=E5=B9=B6=E7=BC=96=E8=AF=91?= =?UTF-8?q?=E6=88=90=E5=8A=9F=EF=BC=8C=E7=AD=89=E5=BE=85=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: D301_Ubuntu <1395769145@qq.com> --- 12_timer/12_timer.code-workspace | 6 +- 12_timer/timer.c | 177 ++++++++++++++++++------------- 12_timer/timerAppp.c | 40 +++++-- 3 files changed, 142 insertions(+), 81 deletions(-) diff --git a/12_timer/12_timer.code-workspace b/12_timer/12_timer.code-workspace index f5bceea..7771164 100644 --- a/12_timer/12_timer.code-workspace +++ b/12_timer/12_timer.code-workspace @@ -4,7 +4,11 @@ "path": "." } ], - "settings":{ + "settings": { + "files.associations": { + "gpio.h": "c", + "*.tcc": "c" + } } } \ No newline at end of file diff --git a/12_timer/timer.c b/12_timer/timer.c index d8dc4c4..7a4465f 100644 --- a/12_timer/timer.c +++ b/12_timer/timer.c @@ -14,123 +14,158 @@ #include #include #include +#include -#define KEY_CNT 1 /* 设备号个数 */ -#define KEY_NAME "key" /* 名字 */ -/* 定义按键值 */ -#define KEY0VALUE 0xF0 /* 按键值*/ -#define INVAKEY 0x00 /* 无效的按键值 */ +#define TIMER_CNT 1 +#define TIMER_NAME "timer" +#define LED_ON 1 +#define LED_OFF 0 +#define CLOSE_CMD (_IO(0XEF, 0X1)) +#define OPEN_CMD (_IO(0XEF, 0X2)) +#define SETPERIOD_CMD (_IO(0XEF, 0X3)) - -/* key设备结构体 */ -struct key_dev{ +/* timer设备结构体 */ +struct timer_dev{ dev_t devid; /* 设备号 */ + int major; /* 主设备号 */ + int minor; /* 次设备号 */ struct cdev cdev; /* cdev */ struct class *class; /* 类 */ struct device *device; /* 设备 */ - int major; /* 主设备号 */ - int minor; /* 次设备号 */ struct device_node *nd; /* 设备节点 */ - int key_gpio; - atomic_t keyvalue; /* 按键值*/ + int led_gpio; + int timeperiod; + struct timer_list timer; /* 定义一个定时器 */ + spinlock_t lock; /*定义自旋锁 */ }; -struct key_dev keydev; +struct timer_dev timerdev; + -static int key_open(struct inode *inode, struct file *filp) + +static int led_init(void) { - filp->private_data = &keydev; - keydev.nd = of_find_node_by_path("/key"); - if(keydev.nd == NULL){ + int ret = 0; + timerdev.nd = of_find_node_by_path("/gpioled"); + if(timerdev.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"); + timerdev.led_gpio = of_get_named_gpio(timerdev.nd,"led-goio",0); + if(timerdev.led_gpio < 0) 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); + + /* 初始化led所使用的IO */ + gpio_request(timerdev.led_gpio,"led"); + ret = gpio_direction_output(timerdev.led_gpio, 1); + if(ret < 0) + printk("can't set gpio !!!!\r\n"); + return ret; +} +static int timer_open(struct inode *inode, struct file *filp) +{ + int ret = 0; + filp->private_data = &timerdev; + timerdev.timeperiod = 1000; + ret = led_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) + +void timer_function(unsigned long arg) { - 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); + struct timer_dev *dev = (struct timer_dev *)arg; + static int sta = 0; + sta = !sta; + gpio_set_value(dev->led_gpio, sta); + mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->timeperiod)); +} + + +static long timer_unlocked_ioclt(struct file *filp,unsigned int cmd,unsigned long arg) +{ + struct timer_dev *dev = (struct timer_dev *)filp->private_data; + unsigned int timerperiod = 0; + unsigned long flags; + switch (cmd) { + case CLOSE_CMD : + del_timer_sync(&dev->timer); + break; + case OPEN_CMD: + mod_timer(&dev->timer,jiffies + msecs_to_jiffies(dev->timeperiod)); + break; + case SETPERIOD_CMD: + spin_lock_irqsave(&dev->timer,flags); + dev->timeperiod = arg; + spin_unlock_irqrestore(&dev->timer,flags); + mod_timer(&dev->timer,jiffies + msecs_to_jiffies(dev->timeperiod)); + break; + default: + break; } - value = atomic_long_read(&dev->keyvalue); - ret = copy_to_user(buf,&value,sizeof(value)); - return ret; } -static struct file_operations key_fops = { +static struct file_operations timer_fops = { .owner = THIS_MODULE, - .open = key_open, - .read = key_read, + .open = timer_open, + .unlocked_ioctl = timer_unlocked_ioclt, }; -static __init mykey_init(void) +static __init timer_init(void) { - /* 初始化源自变量 */ - atomic_set(&keydev.keyvalue,INVAKEY); + /* 初始化自旋锁 */ + spin_lock_init(&timerdev.lock); /* 注册字符设备驱动 */ /* 1、创建设备号 */ - if(keydev.major){ /* 如果定义了设备号 */ - keydev.devid = MKDEV(keydev.major,0); - register_chrdev_region(keydev.devid,KEY_CNT,KEY_NAME); + if(timerdev.major){ /* 如果定义了设备号 */ + timerdev.devid = MKDEV(timerdev.major,0); + register_chrdev_region(timerdev.devid,TIMER_CNT,TIMER_NAME); }else { /* 没有定义设备号 */ - alloc_chrdev_region(&keydev.devid,0,KEY_CNT,KEY_NAME); - keydev.major = MAJOR(keydev.devid); /* 获取分配的主设备号 */ - keydev.minor = MINOR(keydev.devid); /* 获取分配的次设备号 */ + alloc_chrdev_region(&timerdev.devid,0,TIMER_CNT,TIMER_NAME); + timerdev.major = MAJOR(timerdev.devid); /* 获取分配的主设备号 */ + timerdev.minor = MINOR(timerdev.devid); /* 获取分配的次设备号 */ } /* 2、初始化cdev */ - keydev.cdev.owner = THIS_MODULE; - cdev_init(&keydev.cdev,&key_fops); + timerdev.cdev.owner = THIS_MODULE; + cdev_init(&timerdev.cdev,&timer_fops); /* 3、添加一个cdev */ - cdev_add(&keydev.cdev,keydev.devid,KEY_CNT); + cdev_add(&timerdev.cdev,timerdev.devid,TIMER_CNT); /* 4、创建类 */ - keydev.class = class_create(THIS_MODULE,KEY_NAME); - if(IS_ERR(keydev.class)){ - return PTR_ERR(keydev.class); + timerdev.class = class_create(THIS_MODULE,TIMER_NAME); + if(IS_ERR(timerdev.class)){ + return PTR_ERR(timerdev.class); } /* 5、创建设备 */ - keydev.device = device_create(keydev.class,NULL,keydev.devid,NULL,KEY_NAME); - if(IS_ERR(keydev.device)){ - return PTR_ERR(keydev.device); + timerdev.device = device_create(timerdev.class,NULL,timerdev.devid,NULL,TIMER_NAME); + if(IS_ERR(timerdev.device)){ + return PTR_ERR(timerdev.device); } + /* 6、初始化timer */ + init_timer(&timerdev.timer); + timerdev.timer.function = timer_function; + timerdev.timer.data = (unsigned long)&timerdev; return 0; } -static __exit mykey_exit(void) +static __exit timer_exit(void) { - //注销字符设备驱动 - gpio_free(keydev.key_gpio); - cdev_del(&keydev.cdev); - unregister_chrdev_region(keydev.devid,KEY_CNT); + gpio_free(timerdev.led_gpio); + del_timer_sync(&timerdev.timer); + // 注销字符设备驱动 + cdev_del(&timerdev.cdev); + unregister_chrdev_region(timerdev.devid,TIMER_CNT); + + class_destroy(timerdev.class); + device_destroy(timerdev.class,timerdev.devid); - device_destroy(keydev.class,keydev.devid); - class_destroy(keydev.class); } -module_init(mykey_init); -module_exit(mykey_exit); +module_init(timer_init); +module_exit(timer_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("zhangchaoliang"); diff --git a/12_timer/timerAppp.c b/12_timer/timerAppp.c index 52d7b5a..7c112ca 100644 --- a/12_timer/timerAppp.c +++ b/12_timer/timerAppp.c @@ -5,11 +5,13 @@ #include "fcntl.h" #include "stdlib.h" #include "string.h" +#include "linux/ioctl.h" +/* 定义命令 */ +#define CLOSE_CMD (_IO(0XEF, 0X1)) +#define OPEN_CMD (_IO(0XEF, 0X2)) +#define SETPERIOD_CMD (_IO(0XEF, 0X3)) -/* 定义按键值 */ -#define KEY0VALUE 0xF0 -#define INVAKEY 0x00 /* @@ -17,8 +19,11 @@ */ int main(int argc, char *argv[]){ int fd, ret; + unsigned int cmd; + unsigned int arg; char *filename; - unsigned char keyvalue; + unsigned char str[99]; + if (argc != 2) { printf("Error Usage!\r\n"); return -1; @@ -31,12 +36,29 @@ int main(int argc, char *argv[]){ return -1; } - // 循环读取按键数据! - while(1){ - read(fd,&keyvalue,sizeof(keyvalue)); - if(keyvalue == KEY0VALUE) { - printf("KEY0 Press,value = %#x\r\n",keyvalue);// 按下 + while (1) + { + printf("input CMD: "); + ret = scanf("%d", &cmd); + if(ret != 1){ + gets(str); + } + + if(cmd == 1){ + cmd = CLOSE_CMD; + } + else if(cmd == 2){ + cmd == OPEN_CMD; + } + else if(cmd == 3){ + cmd = SETPERIOD_CMD; + printf("input timer period:"); + ret = scanf("%d", &arg); + if(ret!=1){ + gets(str); + } } + ioctl(fd, cmd, arg); } ret = close(fd); // close文件 -- Gitee From 40ab1f72b3a7dee4e2dad670624eef52e996d024 Mon Sep 17 00:00:00 2001 From: sushe_Ubuntu Date: Thu, 21 Mar 2024 22:33:00 +0800 Subject: [PATCH 7/7] =?UTF-8?q?=E5=AE=9E=E9=AA=8C12Linux=E5=86=85=E6=A0=B8?= =?UTF-8?q?=E5=AE=9A=E6=97=B6=E5=99=A8=E5=AE=9E=E9=AA=8C=E5=AE=8C=E6=88=90?= =?UTF-8?q?=E5=B9=B6=E9=AA=8C=E8=AF=81=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 --- 12_timer/12_timer.code-workspace | 3 ++- 12_timer/timer.c | 35 ++++++++++++++++++-------------- 12_timer/timerAppp.c | 6 +++--- 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/12_timer/12_timer.code-workspace b/12_timer/12_timer.code-workspace index 7771164..c20f397 100644 --- a/12_timer/12_timer.code-workspace +++ b/12_timer/12_timer.code-workspace @@ -7,7 +7,8 @@ "settings": { "files.associations": { "gpio.h": "c", - "*.tcc": "c" + "*.tcc": "c", + "ioctl.h": "c" } } diff --git a/12_timer/timer.c b/12_timer/timer.c index 7a4465f..628d46d 100644 --- a/12_timer/timer.c +++ b/12_timer/timer.c @@ -46,20 +46,18 @@ struct timer_dev timerdev; static int led_init(void) { - int ret = 0; timerdev.nd = of_find_node_by_path("/gpioled"); if(timerdev.nd == NULL) return -EINVAL; - timerdev.led_gpio = of_get_named_gpio(timerdev.nd,"led-goio",0); - if(timerdev.led_gpio < 0) + timerdev.led_gpio = of_get_named_gpio(timerdev.nd,"led-gpio",0); + if(!gpio_is_valid(timerdev.led_gpio)) return -EINVAL; /* 初始化led所使用的IO */ gpio_request(timerdev.led_gpio,"led"); - ret = gpio_direction_output(timerdev.led_gpio, 1); - if(ret < 0) - printk("can't set gpio !!!!\r\n"); - return ret; + gpio_direction_output(timerdev.led_gpio, 1); + + return 0; } static int timer_open(struct inode *inode, struct file *filp) @@ -76,7 +74,7 @@ static int timer_open(struct inode *inode, struct file *filp) void timer_function(unsigned long arg) { struct timer_dev *dev = (struct timer_dev *)arg; - static int sta = 0; + static int sta = 1; sta = !sta; gpio_set_value(dev->led_gpio, sta); mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->timeperiod)); @@ -88,20 +86,24 @@ static long timer_unlocked_ioclt(struct file *filp,unsigned int cmd,unsigned lo struct timer_dev *dev = (struct timer_dev *)filp->private_data; unsigned int timerperiod = 0; unsigned long flags; - switch (cmd) { - case CLOSE_CMD : + unsigned int CMD = cmd; + switch (CMD) + { + case CLOSE_CMD: del_timer_sync(&dev->timer); break; case OPEN_CMD: - mod_timer(&dev->timer,jiffies + msecs_to_jiffies(dev->timeperiod)); + mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->timeperiod)); break; case SETPERIOD_CMD: - spin_lock_irqsave(&dev->timer,flags); + spin_lock_irqsave(&dev->lock, flags); dev->timeperiod = arg; - spin_unlock_irqrestore(&dev->timer,flags); - mod_timer(&dev->timer,jiffies + msecs_to_jiffies(dev->timeperiod)); + spin_unlock_irqrestore(&dev->lock,flags); + mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->timeperiod)); break; + default: + printk("CMD = %d\r\n", cmd); break; } } @@ -149,6 +151,9 @@ static __init timer_init(void) init_timer(&timerdev.timer); timerdev.timer.function = timer_function; timerdev.timer.data = (unsigned long)&timerdev; + timerdev.timeperiod = 1000; + timerdev.timer.expires = jiffies + msecs_to_jiffies(500); + add_timer(&timerdev.timer); return 0; } @@ -160,8 +165,8 @@ static __exit timer_exit(void) cdev_del(&timerdev.cdev); unregister_chrdev_region(timerdev.devid,TIMER_CNT); - class_destroy(timerdev.class); device_destroy(timerdev.class,timerdev.devid); + class_destroy(timerdev.class); } diff --git a/12_timer/timerAppp.c b/12_timer/timerAppp.c index 7c112ca..36382ff 100644 --- a/12_timer/timerAppp.c +++ b/12_timer/timerAppp.c @@ -5,12 +5,12 @@ #include "fcntl.h" #include "stdlib.h" #include "string.h" -#include "linux/ioctl.h" +#include "sys/ioctl.h" /* 定义命令 */ #define CLOSE_CMD (_IO(0XEF, 0X1)) #define OPEN_CMD (_IO(0XEF, 0X2)) -#define SETPERIOD_CMD (_IO(0XEF, 0X3)) +#define SETPERIOD_CMD (_IOW(0XEF, 0X3,int)) @@ -48,7 +48,7 @@ int main(int argc, char *argv[]){ cmd = CLOSE_CMD; } else if(cmd == 2){ - cmd == OPEN_CMD; + cmd = OPEN_CMD; } else if(cmd == 3){ cmd = SETPERIOD_CMD; -- Gitee