- #include
- #include
- #include
- #include
- #include
- #include
- #include "device.h"
- int main(int argc,const char * argv[])
- {
- int i;
- int fd[6]={0};
- char *p[6]={"/dev/myled0","/dev/myled1","/dev/myled2","/dev/myled3","/dev/myled4","/dev/myled5"};
- for(i=0;i<6;i++)
- {
- fd[i] = open(p[i],O_RDWR);
- if(-1 == fd[i])
- {
- printf("open is error\n");
- return -1;
- }
- }
- while(1)
- {
- ioctl(fd[0],LED0_ON);
- sleep(1);
- ioctl(fd[0],LED0_OFF);
- ioctl(fd[1],LED3_ON);
- sleep(1);
- ioctl(fd[1],LED3_OFF);
- ioctl(fd[2],LED5_ON);
- sleep(1);
- ioctl(fd[2],LED5_OFF);
- ioctl(fd[3],LED4_ON);
- sleep(1);
- ioctl(fd[3],LED4_OFF);
- ioctl(fd[4],LED2_ON);
- sleep(1);
- ioctl(fd[4],LED2_OFF);
- ioctl(fd[5],LED1_ON);
- sleep(1);
- ioctl(fd[5],LED1_OFF);
- }
- return 0;
- }
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include "device.h"
- #define CNAME "myleds"
- struct device_node *node;
- struct gpio_desc *gpio[6];
- int ret,i;
- struct cdev *cdev;
- dev_t dev;
- int minor=0;
- int major;
- int count=6;
- struct device* device;
- struct class* cls;
- char *p[6]={"myled0","myled1","myled2","myled3","myled4","myled5"};
- int myc_open(struct inode *node, struct file *file)
- {
- return 0;
- }
- long myc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
- {
- if(cmd == LED0_ON)
- {
- gpiod_set_value(gpio[0],1);
- }else if(cmd == LED0_OFF)
- {
- gpiod_set_value(gpio[0],0);
- }
- if(cmd == LED1_ON)
- {
- gpiod_set_value(gpio[1],1);
- }else if(cmd == LED1_OFF)
- {
- gpiod_set_value(gpio[1],0);
- }
- if(cmd == LED2_ON)
- {
- gpiod_set_value(gpio[2],1);
- }else if(cmd == LED2_OFF)
- {
- gpiod_set_value(gpio[2],0);
- }
- if(cmd == LED3_ON)
- {
- gpiod_set_value(gpio[3],1);
- }else if(cmd == LED3_OFF)
- {
- gpiod_set_value(gpio[3],0);
- }
- if(cmd == LED4_ON)
- {
- gpiod_set_value(gpio[4],1);
- }else if(cmd == LED4_OFF)
- {
- gpiod_set_value(gpio[4],0);
- }
- if(cmd == LED5_ON)
- {
- gpiod_set_value(gpio[5],1);
- }else if(cmd == LED5_OFF)
- {
- gpiod_set_value(gpio[5],0);
- }
- return 0;
- }
- int myc_close(struct inode *node, struct file *file)
- {
- return 0;
- }
- const struct file_operations fops = {
- .open = myc_open,
- .release = myc_close,
- .unlocked_ioctl = myc_ioctl,
- };
- static int __init mycdev_init(void)
- {
- //分配字符设备驱动
- cdev = cdev_alloc();
- if(cdev == NULL)
- {
- printk("内存分配失败\n");
- goto ERR1;
- }
- printk("内存分配成功\n");
- //初始化设备驱动
- cdev_init(cdev,&fops);
- //动态申请设备号
- ret = alloc_chrdev_region(&dev,minor,count,CNAME);
- if(ret<0)
- {
- printk("动态申请设备号失败\n");
- goto ERR2;
- }
- printk("动态申请设备号成功\n");
- //注册字符设备驱动
- ret = cdev_add(cdev,dev,count);
- if(ret<0)
- {
- printk("注册字符设备驱动失败\n");
- goto ERR3;
- }
- printk("注册字符设备驱动成功\n");
- //5.自动创建设备节点
- //向上层提交目录信息
- cls = class_create(THIS_MODULE,CNAME);
- if(IS_ERR(cls))
- {
- printk("class create is failed\n");
- ret = PTR_ERR(cls);
- goto ERR4;
- }
- //向上层提交设备节点
- //自动申请
- major = MAJOR(dev);
- for(i=0;i
- {
- device = device_create(cls,NULL,MKDEV(major,i),NULL,"myled%d",i);
- if(IS_ERR(device))
- {
- printk("device create is error\n");
- ret = PTR_ERR(device);
- goto ERR5;
- }
- }
- node = of_find_node_by_path("/myleds");
- if(node == NULL)
- {
- printk("通过路径解析设备树节点失败\n");
- return -EFAULT;
- }
- printk("成功解析到设备树节点\n");
-
- for(i=0;i<6;i++)
- {
- gpio[i] = gpiod_get_from_of_node(node,p[i],0,GPIOD_OUT_LOW,NULL);
- if(IS_ERR(gpio[i]))
- {
- printk("gpio编号获取失败\n");
- return PTR_ERR(gpio[i]);
- }
- printk("gipo[%d]编号获取成功\n",i);
-
- //设置引脚为输出模式
- ret = gpiod_direction_output(gpio[i],0);
- if(ret<0)
- {
- printk("输出模式设置成功\n");
- return ret;
- }
- //初始化
- gpiod_set_value(gpio[i],0);
- }
- return 0;
- ERR5:
- for(--i;i>=0;i--)
- {
- device_destroy(cls,MKDEV(major,i));
- }
- class_destroy(cls);
- ERR4:
- cdev_del(cdev);
- ERR3:
- unregister_chrdev_region(dev,count);
- ERR2:
- kfree(cdev);
- ERR1:
- return -EIO;
- }
- static void __exit mycdev_exit(void)
- {
- for(i=0;i<6;i++)
- {
- gpiod_set_value(gpio[i],0);
- //释放
- gpiod_put(gpio[i]);
- }
- for(i=0;i
- {
- device_destroy(cls,MKDEV(major,i));
- }
- //2.销毁目录信息
- class_destroy(cls);
- //3.驱动的注销
- cdev_del(cdev);
- //4.销毁设备号
- unregister_chrdev_region(dev,count);
- //5.释放cdev结构体
- kfree(cdev);
- }
- module_init(mycdev_init);
- module_exit(mycdev_exit);
- MODULE_LICENSE("GPL");
-
- #ifndef __DEVICE_H__
- #define __DEVICE_H__
- #define LED0_ON _IOW('a',1,int)
- #define LED0_OFF _IOW('a',0,int)
- #define LED1_ON _IOW('a',1,int)
- #define LED1_OFF _IOW('a',0,int)
- #define LED2_ON _IOW('a',1,int)
- #define LED2_OFF _IOW('a',0,int)
- #define LED3_ON _IOW('a',1,int)
- #define LED3_OFF _IOW('a',0,int)
- #define LED4_ON _IOW('a',1,int)
- #define LED4_OFF _IOW('a',0,int)
- #define LED5_ON _IOW('a',1,int)
- #define LED5_OFF _IOW('a',0,int)
- #endif
现象实现:
-
相关阅读:
华为浏览器,Chrome的平替,插件无缝连接
罗素《为什么我不是基督徒》的后果
Python每日一练 04
互联网安全面临的全新挑战
form-create表单生成器简单使用
wxPython 4.2.0 发布
卷积层计算量(FLOPS)和参数量的计算
babili-webpack-plugin编译后的代码v+‘‘===1,会被压缩成字符串v1
计算机网络数据链路层知识总结
Leetcode1805-字符串中不同整数的数目
-
原文地址:https://blog.csdn.net/qq_58538047/article/details/128025248