text.c
- #include
- #include
- #include
- #include
- #include
- #include
- int main(int argc, char const *argv[])
- {
- char buf[128]={0};
- int fd=open("/dev/myleds",O_RDWR);
- if(fd<0)
- {
- printf("打开设备文件失败\n");
- exit(-1);
- }
- //在终端输入数据
- while(1)
- {
- printf("请输入:0(led1关灯)1(led1开灯)2(led2开灯)3(led2关灯)4(led3开灯)5(led3开灯)>>>");
- fgets(buf,sizeof(buf),stdin);
- buf[strlen(buf)-1]='\0';
- write(fd,buf,sizeof(buf));
- }
- close(fd);
- return 0;
- }
驱动代码
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- /* myleds{
- led1=<&gpioe 10 0>;
- led2=<&gpiof 10 0>;
- led3=<&gpioe 8 0>;
- };*/
- int major;
- int gpiono;
- int gpiono2;
- int gpiono3;
- char kbuf[128] = {0};
- struct class *cls;
- struct device *dev;
- struct device_node *dnode;
-
- int mycdev_open(struct inode *inode,struct file *file)
- {
- printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
- return 0;
- }
- ssize_t mycdev_read(struct file *file, char *ubuf,size_t size, loff_t *iof)
- {
- int ret;
- //将进程2写到内核的数据传递给进程1
- if(size>sizeof(kbuf))
- size=sizeof(kbuf);
- ret=copy_to_user(ubuf,kbuf,size);
- if(ret)
- {
- printk("copy to user failed\n");
- return -EIO;
- }
- printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
- return 0;
- }
- ssize_t mycdev_write(struct file *file,const char *ubuf,size_t size,loff_t *off)
- {
- int ret;
- if(size>sizeof(kbuf))
- size=sizeof(kbuf);
- ret=copy_from_user(kbuf,ubuf,size);
- if(ret)
- {
- printk("copy from user failed\n");
- return -EIO;
- }
- if(kbuf[0]=='1')
- {
- //开灯
- gpio_set_value(gpiono,1);
- }
- else if(kbuf[0]=='0')
- {
- //关灯
- gpio_set_value(gpiono,0);
- }
- else if(kbuf[0]=='2')
- {
- gpio_set_value(gpiono2,1);
- }
- else if(kbuf[0]=='3')
- {
- //关灯
- gpio_set_value(gpiono2,0);
- }
- else if(kbuf[0]=='4')
- {
- gpio_set_value(gpiono3,1);
- }
- else if(kbuf[0]=='5')
- {
- gpio_set_value(gpiono3,0);
- }
- return 0;
- }
- int mycdev_close(struct inode *inode,struct file *file)
- {
- printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
- return 0;
- }
- //定义操作方法结构变量并初始化
- struct file_operations fops=
- {
- .open=mycdev_open,
- .read=mycdev_read,
- .write=mycdev_write,
- .release=mycdev_close,
- };
- //入口函数
- static int __init mycdev_init(void)
- {
- int ret;
- //进行字符设备驱动的注册
- major=register_chrdev(0,"myleds",&fops);
- if(major<0)
- {
- printk("字符设备驱动注册失败\n");
- return major;
- }
- printk("字符设备驱动注册成功,major=%d\n",major);
- //向上提交目lu
- cls=class_create(THIS_MODULE,"myleds");
- if(IS_ERR(cls))
- {
- printk("向上提交目錄失敗\n");
- return -PTR_ERR(cls);
- }
- printk("向上提交目录成功\n");
- //向上提交设备节点信息
- dev=device_create(cls,NULL,MKDEV(major,0),NULL,"myleds");
- if(IS_ERR(dev))
- {
- printk("向上提交设备节点失败\n");
- return -PTR_ERR(dev);
- }
- printk("向上提交设备节点成功\n");
- return 0;
-
- // 解析设备树节点
- dnode = of_find_node_by_name(NULL, "myleds");
- if (dnode == NULL)
- {
- printk("解析设备树节点失败\n");
- return -ENOMEM;
- }
- printk("解析设备树节点成功\n");
- // 根据设备树节点解析出gpio编号--led1
- gpiono = of_get_named_gpio(dnode, "led1", 0);
- if (gpiono < 0)
- {
- printk("gpio解析设备号失败\n");
- return -EIO;
- }
- // 申请gpio编号
- ret = gpio_request(gpiono, NULL);
- if (ret)
- {
- printk("GPIO编号申请失败\n");
- return -EIO;
- }
- printk("申请gpio编号成功%d\n", gpiono);
- gpiono2 = of_get_named_gpio(dnode, "led2", 0);
- if (gpiono2 < 0)
- {
- printk("gpio2解析设备号失败\n");
- return -EIO;
- }
- ret=gpio_request(gpiono2,NULL);
- if(ret)
- {
- printk("gpio2编号申请失败\n");
- return ret;
- }
- gpiono3 = of_get_named_gpio(dnode, "led3", 0);
- if (gpiono3 < 0)
- {
- printk("gpio3解析设备号失败\n");
- return -EIO;
- }
- ret=gpio_request(gpiono3,NULL);
- if(ret)
- {
- printk("gpio3编号申请失败\n");
- return ret;
- }
- printk("gpio3编号申请成功\n");
- // 设置管脚为输出
- gpio_direction_output(gpiono, 0);
- gpio_direction_output(gpiono2, 0);
- gpio_direction_output(gpiono3, 0);
- // 开灯
- //gpio_set_value(gpiono, 1);
- return 0;
- }
- static void __exit mycdev_exit(void)
- {
- // 灭灯
- gpio_set_value(gpiono, 0);
- gpio_set_value(gpiono2, 0);
- gpio_set_value(gpiono3, 0);
- // 释放gpio编号
- gpio_free(gpiono);
- gpio_free(gpiono2);
- gpio_free(gpiono3);
- //销毁设备节点信息
- device_destroy(cls,MKDEV(major,0));
- //销毁目录空间
- class_destroy(cls);
- //字符设备驱动的注销
- unregister_chrdev(major,"myleds");
- }
- module_init(mycdev_init);
- module_exit(mycdev_exit);
- MODULE_LICENSE("GPL");
- linux@linux:~/qudong/day2/