中断子系统用驱动控制led实验
驱动代码
#include <linux/init.h>
#include <linux/module.h>#include<linux/interrupt.h>
#include<linux/gpio.h>
#include<linux/timer.h>#include<linux/of.h>
#include<linux/of_irq.h>
#include<linux/of_gpio.h>/*myirq{compatible = "hqyj,myirq";interrupt-parent=<&gpiof>;interrupts=<9 0>,<7 0>,<8 0>;led1-gpio=<&gpioe 10 0>;led2-gpio=<&gpiof 10 0>;led3-gpio=<&gpioe 8 0>;};*/struct device_node *dnode;struct gpio_desc *lednode1;struct gpio_desc *lednode2;struct gpio_desc *lednode3;unsigned int irq_id1;unsigned int irq_id2;unsigned int irq_id3;//中断处理函数irqreturn_t myirq_handle1(int irq,void *dev){//反转电平gpiod_set_value(lednode1,!gpiod_get_value(lednode1));//防止其他灯亮gpiod_set_value(lednode2,0);gpiod_set_value(lednode3,0);return IRQ_HANDLED;}irqreturn_t myirq_handle2(int irq,void *dev){//反转电平gpiod_set_value(lednode2,!gpiod_get_value(lednode2));//防止其他灯亮gpiod_set_value(lednode1,0);gpiod_set_value(lednode3,0);return IRQ_HANDLED;}irqreturn_t myirq_handle3(int irq,void *dev){//反转电平gpiod_set_value(lednode3,!gpiod_get_value(lednode3));//防止其他灯亮gpiod_set_value(lednode1,0);gpiod_set_value(lednode2,0);return IRQ_HANDLED;}static int __init mycdev_init(void)
{//根据compatible解析设备节点dnode=of_find_compatible_node(NULL,NULL,"hqyj,myirq");if(NULL==dnode){printk("of_find_compatible_node error\n");return -ENXIO;}printk("解析设备节点成功\n");//设备树节点解析lednode1=gpiod_get_from_of_node(dnode,"led1-gpio",0,GPIOD_OUT_LOW,NULL);if(lednode1==NULL){printk("设备树节点解析失败\n");return ENXIO;}//设备树节点解析lednode2=gpiod_get_from_of_node(dnode,"led2-gpio",0,GPIOD_OUT_LOW,NULL);if(lednode2==NULL){printk("设备树节点解析失败\n");return ENXIO;}//设备树节点解析lednode3=gpiod_get_from_of_node(dnode,"led3-gpio",0,GPIOD_OUT_LOW,NULL);if(lednode3==NULL){printk("设备树节点解析失败\n");return ENXIO;}//解析按键1的软中断irq_id1=irq_of_parse_and_map(dnode,0);if(!irq_id1){printk("解析软中断失败\n");return -ENXIO;}//解析按键2的软中断irq_id2=irq_of_parse_and_map(dnode,1);if(!irq_id2){printk("解析软中断失败\n");return -ENXIO;}//解析按键3的软中断irq_id3=irq_of_parse_and_map(dnode,2);if(!irq_id3){printk("解析软中断失败\n");return -ENXIO;}printk("解析软中断成功\n");//注册按键1请求中断int ret=request_irq(irq_id1,myirq_handle1,IRQF_TRIGGER_FALLING,"key1",(void*)1); if(ret){printk("注册按键请求中断失败\n");return ret;}//注册按键2请求中断ret=request_irq(irq_id2,myirq_handle2,IRQF_TRIGGER_FALLING,"key2",(void*)2); if(ret){printk("注册按键请求中断失败\n");return ret;}//注册按键3请求中断ret=request_irq(irq_id3,myirq_handle3,IRQF_TRIGGER_FALLING,"key3",(void*)3); if(ret){printk("注册按键请求中断失败\n");return ret;}printk("注册按键请求中断成功\n");return 0;
}
static void __exit mycdev_exit(void)
{ //关灯gpiod_set_value(lednode1,0);gpiod_set_value(lednode2,0);gpiod_set_value(lednode3,0);//清空中断号free_irq(irq_id1,(void*)1);free_irq(irq_id2,(void*)2);free_irq(irq_id3,(void*)3);//释放设备树节点空间gpiod_put(lednode1);gpiod_put(lednode2);gpiod_put(lednode3);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
串口led点灯实验(驱动+应用层)
驱动代码
#include <linux/init.h>
#include <linux/module.h>#include<linux/fs.h>
#include<linux/io.h>
#include<linux/device.h>#include<linux/gpio.h>
#include<linux/of.h>
#include<linux/of_gpio.h>#include"head.h"int major;
char kbuf[128]={0};struct class *cls;
struct device *dev;struct device_node *dnode;struct gpio_desc *lednode1;
struct gpio_desc *lednode2;
struct gpio_desc *lednode3;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 *lof)
{printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);unsigned long ret;//向用户空间读取拷贝if(size>sizeof(kbuf))//用户空间期待读取的大小内核满足不了,那就给内核支持的最大大小size=sizeof(kbuf);ret=copy_to_user(ubuf,kbuf,size);if(ret)//拷贝失败{printk("copy_to_user filed\n");return ret;}return 0;
}ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{unsigned long ret;//从用户空间读取数据if(size>sizeof(kbuf))//用户空间期待读取的大小内核满足不了,那就给内核支持的最大大小size=sizeof(kbuf);ret=copy_from_user(kbuf,ubuf,size);if(ret)//拷贝失败{printk("copy_to_user filed\n");return ret;}return 0;
}long mycdev_ioctl (struct file * file, unsigned int cmd, unsigned long arg)
{switch (cmd){case LED_OFF:switch (arg){case 1:gpiod_set_value(lednode1,0);break;case 2:gpiod_set_value(lednode2,0);break;case 3:gpiod_set_value(lednode3,0);break;}break;case LED_ON:switch (arg){case 1:gpiod_set_value(lednode1,1);break;case 2:gpiod_set_value(lednode2,1);break;case 3:gpiod_set_value(lednode3,1);break;}break;default:break;}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,.unlocked_ioctl=mycdev_ioctl,.release=mycdev_close,
};static int __init mycdev_init(void)
{//字符设备驱动注册major=register_chrdev(0,"mychrdev",&fops);if(major<0){printk("字符设备驱动注册失败\n");return major;}printk("字符设备驱动注册成功:major=%d\n",major);//向上提交目录cls=class_create(THIS_MODULE,"mychrdev");if(IS_ERR(cls)){printk("向上提交目录失败\n");return -PTR_ERR(cls);}printk("向上提交目录成功\n");int i;//向上提交三次设备节点信息for(i=0;i<3;i++){dev=device_create(cls,NULL,MKDEV(major,i),NULL,"myled%d",i);if(IS_ERR(dev)){printk("向上提交设备节点失败\n");return -PTR_ERR(dev);}}printk("向上提交设备节点成功\n");dnode=of_find_compatible_node(NULL,NULL,"hqyj,myirq");if(NULL==dnode){printk("of_find_compatible_node error\n");return -ENXIO;}printk("解析设备节点成功\n");//设备树节点解析lednode1=gpiod_get_from_of_node(dnode,"led1-gpio",0,GPIOD_OUT_LOW,NULL);if(lednode1==NULL){printk("设备树节点解析失败\n");return ENXIO;}//设备树节点解析lednode2=gpiod_get_from_of_node(dnode,"led2-gpio",0,GPIOD_OUT_LOW,NULL);if(lednode2==NULL){printk("设备树节点解析失败\n");return ENXIO;}//设备树节点解析lednode3=gpiod_get_from_of_node(dnode,"led3-gpio",0,GPIOD_OUT_LOW,NULL);if(lednode3==NULL){printk("设备树节点解析失败\n");return ENXIO;}printk("设备树节点解析成功\n");return 0;
}
static void __exit mycdev_exit(void)
{ //关灯gpiod_set_value(lednode1,0);gpiod_set_value(lednode2,0);gpiod_set_value(lednode3,0);//释放设备树节点空间gpiod_put(lednode1);gpiod_put(lednode2);gpiod_put(lednode3);int i;for(i=0;i<3;i++){device_destroy(cls,MKDEV(major,i));}//销毁目录class_destroy(cls);//注销字符设备驱动unregister_chrdev(major,"mychrdev");}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
头文件
#ifndef __HEAD_H__
#define __HEAD_H__#define LED_ON _IOW('l',1,int)
#define LED_OFF _IOW('l',0,int)
#endif
应用层
#include<stdio.h>
#include<stdlib.h>
#include<string.h>#include<fcntl.h>
#include<unistd.h>#include<sys/types.h>
#include<sys/stat.h>
#include<sys/ioctl.h>#include"head.h"int main(int argc, char const *argv[])
{int fd;fd=open("/dev/myled0",O_RDWR);if(fd<0){perror("open error:");return -1;}int a;int b;
while (1)
{out:printf("选择要实现的功能\n");printf("请输入0关灯 1开灯\n");scanf("%d",&a);printf("LED 1,LED 2 LED 3");printf("请输入:");scanf("%d",&b);switch (a){case 0:ioctl(fd,LED_OFF,b); break;case 1:ioctl(fd,LED_ON,b); break;default:printf("输入错误请重新输入\n");goto out;break;}
}return 0;
}
中断运用驱动控制led实验
串口led点灯实验结果