From 496cfd944d4a189719f5af31e762e7662c015b4c Mon Sep 17 00:00:00 2001 From: D301_Ubuntu <1395769145@qq.com> Date: Thu, 4 Jan 2024 14:52:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BA=92=E6=96=A5=E4=BD=93=E5=AE=9E=E9=AA=8C?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E7=BC=96=E8=AF=91=E6=88=90=E5=8A=9F=EF=BC=8C?= =?UTF-8?q?=E7=AD=89=E5=BE=85=E4=B8=8B=E8=BD=BD=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> --- 10_mutex/.vscode/c_cpp_properties.json | 21 +++ 10_mutex/.vscode/settings.json | 21 +++ 10_mutex/10_mutex.code-workspace | 11 ++ 10_mutex/Makefile | 13 ++ 10_mutex/mutex.c | 171 +++++++++++++++++++++++++ 10_mutex/mutexApp.c | 68 ++++++++++ 6 files changed, 305 insertions(+) create mode 100644 10_mutex/.vscode/c_cpp_properties.json create mode 100644 10_mutex/.vscode/settings.json create mode 100644 10_mutex/10_mutex.code-workspace create mode 100644 10_mutex/Makefile create mode 100644 10_mutex/mutex.c create mode 100644 10_mutex/mutexApp.c diff --git a/10_mutex/.vscode/c_cpp_properties.json b/10_mutex/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..7ee3d5b --- /dev/null +++ b/10_mutex/.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/10_mutex/.vscode/settings.json b/10_mutex/.vscode/settings.json new file mode 100644 index 0000000..1a2a04a --- /dev/null +++ b/10_mutex/.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/10_mutex/10_mutex.code-workspace b/10_mutex/10_mutex.code-workspace new file mode 100644 index 0000000..53a2b99 --- /dev/null +++ b/10_mutex/10_mutex.code-workspace @@ -0,0 +1,11 @@ +{ + "folders": [ + { + "path": "." + } + ], + "settings": { + + } + } +} \ No newline at end of file diff --git a/10_mutex/Makefile b/10_mutex/Makefile new file mode 100644 index 0000000..387fb0d --- /dev/null +++ b/10_mutex/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 := mutex.o + +build: kernel_modules + +kernel_modules: + $(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) modules + +clean: + $(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) clean + + diff --git a/10_mutex/mutex.c b/10_mutex/mutex.c new file mode 100644 index 0000000..10ad141 --- /dev/null +++ b/10_mutex/mutex.c @@ -0,0 +1,171 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define GPIOLED_CNT 1 /* 设备号个数 */ +#define GPIOLED_NAME "gpioled" /* 名字 */ +#define LEDOFF 1 /* 关灯 */ +#define LEDON 0 /* 开灯 */ + + + +struct gpioled_dev +{ + dev_t devid; /* 设备号 */ + struct cdev cdev; /* cdev */ + struct class *class; /* 类 */ + struct device *device; /* 设备 */ + int major; /* 主设备号 */ + int minor; /* 次设备号 */ + struct device_node *nd; /* 设备节点 */ + int led_gpio; /* led所使用的GPIO编号 */ + int dev_status; /* 设备状态,0:设备未使用;>0:设备已经被使用 */ + struct mutex lock; /* 互斥锁 */ +}; + + +struct gpioled_dev gpioled; /* led设备 */ + + +static int led_open(struct inode *inode, struct file *filp) +{ + filp->private_data = &gpioled; + if(mutex_lock_interruptible(&gpioled.lock)){ + return -ERESTARTSYS; + } +#if 0 + mutex_lock(&gpioled.lock); /* 上锁 */ +#endif + return 0; +} + +static ssize_t led_read(struct file *filp, const char __user *buf,size_t cnt,loff_t *offt) +{ + return 0; +} + + +static ssize_t led_write(struct file *filp, const char __user *buf,size_t cnt,loff_t *offt) +{ + int retval; + unsigned char databuf[1]; + unsigned char ledstate; + + struct gpioled_dev *dev = filp->private_data; + + retval = copy_from_user(databuf,buf,cnt); + + ledstate = databuf[0]; + + if(ledstate == LEDOFF){ + gpio_set_value(dev->led_gpio,0); + }else if(ledstate == LEDON){ + gpio_set_value(dev->led_gpio,1); + } + return retval; +} + +static int led_release(struct inode *inode, struct file *filp) +{ + struct gpioled_dev *dev = filp->private_data; + mutex_unlock(&dev->lock); /* 解锁 */ + return 0; +} + +/* 设备操作函数 */ +static struct file_operations gpioled_fops = { + .owner = THIS_MODULE, + .open = led_open, + .read = led_read, + .write = led_write, + .release = led_release +}; + +static int __init led_init(void) +{ + int ret = 0; + + mutex_init(&gpioled.lock); /* 初始化互斥锁 */ + /* 设置LED所使用的GPIO */ + /* 1、获取设备节点 gpioled*/ + gpioled.nd = of_find_node_by_path("/gpioled"); + if (gpioled.nd != NULL) + { + printk("gpioled node has been found!\r\n"); + } + + /* 2、 */ + gpioled.led_gpio = of_get_named_gpio(gpioled.nd,"led-gpio",0); + ret = gpio_direction_output(gpioled.led_gpio,1); + + + /* 注册字符设备驱动 */ + /* 1、创建设备号 */ + if(gpioled.major){ + gpioled.devid = MKDEV(gpioled.major,0); + register_chrdev_region(gpioled.devid,GPIOLED_CNT,GPIOLED_NAME); + }else{ + alloc_chrdev_region(&gpioled.devid,0,GPIOLED_CNT,GPIOLED_NAME); /* 申请设备号 */ + gpioled.major = MAJOR(gpioled.devid); /* 获取分配的主设备号 */ + gpioled.minor = MINOR(gpioled.devid); /* 获取分配的次设备号 */ + } + + printk("gpioled major = %d, minor = %d \r\n",gpioled.major,gpioled.minor); + + + /* 2、初始化 */ + gpioled.cdev.owner = THIS_MODULE; + cdev_init(&gpioled.cdev,&gpioled_fops); + + /* 3、添加一个cdev */ + cdev_add(&gpioled.cdev,gpioled.devid,GPIOLED_CNT); + + /* 创建类 */ + gpioled.class = class_create(THIS_MODULE,GPIOLED_NAME); + if(IS_ERR(gpioled.class)){ + return PTR_ERR(gpioled.class); + } + + /* 5、创建设备 */ + gpioled.device = device_create(gpioled.class,NULL,gpioled.devid,NULL,GPIOLED_NAME); + + if(IS_ERR(gpioled.device)) { + return PTR_ERR(gpioled.device); + } + + return 0; +} + +static void __exit led_exit(void) +{ + /* 注销字符设备驱动 */ + cdev_del(&gpioled.cdev); + unregister_chrdev_region(gpioled.devid,GPIOLED_CNT); + + device_destroy(gpioled.class,gpioled.devid); + class_destroy(gpioled.class); +} + + + + + + +module_init(led_init); +module_exit(led_exit); +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("chaoliang"); diff --git a/10_mutex/mutexApp.c b/10_mutex/mutexApp.c new file mode 100644 index 0000000..90c1eb7 --- /dev/null +++ b/10_mutex/mutexApp.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