作业:
让主程序正常进行3个灯的流水,按下按键1让蜂鸣器响,按键2让马达震动
#include<linux/init.h>
#include<linux/module.h>
#include<linux/of_gpio.h>
#include<linux/gpio.h>
#include<linux/of.h>
#include<linux/of_irq.h>
#include<linux/interrupt.h>
#include<linux/delay.h>
struct device_node* node1;
struct device_node* node2;
struct gpio_desc* desc1;
struct gpio_desc* desc2;
struct gpio_desc* desc3;
struct gpio_desc* desc4;
struct gpio_desc* desc5;
struct timer_list mytimer1;
unsigned int irqno[2];
char* irqname[2]={"key1_int","key2_int"};
void timer_function1(struct timer_list* timer)
{
gpiod_set_value(desc1,1);
mdelay(500);
gpiod_set_value(desc1,0);
mdelay(1000);
gpiod_set_value(desc2,1);
mdelay(500);
gpiod_set_value(desc2,0);
mdelay(1000);
gpiod_set_value(desc3,1);
mdelay(500);
gpiod_set_value(desc3,0);
mod_timer(&mytimer1,jiffies+HZ);
}
irqreturn_t irq_handler(int irqno,void* dev)
{
switch((int)dev)
{
case 0:
gpiod_set_value(desc4,1);
break;
case 1:
gpiod_set_value(desc5,1);
break;
}
return IRQ_HANDLED;
}
static int __init mycdev_init(void)
{
int ret,i;
node1=of_find_node_by_name(NULL,"myleds");
if(node1==NULL)
{
printk("获取设备树节点信息失败\n");
return -ENODATA;
}
node2=of_find_node_by_name(NULL,"myirq");
if(node2==NULL)
{
printk("获取设备树节点信息失败\n");
return -ENODATA;
}
printk("获取设备树节点信息成功\n");
desc1=gpiod_get_from_of_node(node1,"led1",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(desc1))
{
printk("获取gpio编号失败\n");
return PTR_ERR(desc1);
}
desc2=gpiod_get_from_of_node(node1,"led2",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(desc2))
{
printk("获取gpio编号失败\n");
return PTR_ERR(desc2);
}
desc3=gpiod_get_from_of_node(node1,"led3",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(desc3))
{
printk("获取gpio编号失败\n");
return PTR_ERR(desc3);
}
desc4=gpiod_get_from_of_node(node1,"bee",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(desc4))
{
printk("获取gpio编号失败\n");
return PTR_ERR(desc4);
}
desc5=gpiod_get_from_of_node(node1,"horse",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(desc5))
{
printk("获取gpio编号失败\n");
return PTR_ERR(desc5);
}
mytimer1.expires=jiffies+HZ;
timer_setup(&mytimer1,timer_function1,0);
//将定时器对象添加到内核并启用定时器
add_timer(&mytimer1);
for(i=0;i<ARRAY_SIZE(irqno);i++)
{
irqno[i]=irq_of_parse_and_map(node2,i);
if(irqno[i]==0)
{
printk("获取软中断号失败\n");
return -EINVAL;
}
ret=request_irq(irqno[i],irq_handler,IRQF_TRIGGER_FALLING,irqname[i],(void*)i);
if(ret)
{
printk("中断注册失败\n");
return ret;
}
}
return 0;
}
static void __exit mycdev_exit(void)
{
int i;
for(i=0;i<ARRAY_SIZE(irqno);i++)
{
free_irq(irqno[i],(void*)i);
}
del_timer(&mytimer1);
del_timer(&mytimer2);
del_timer(&mytimer3);
gpiod_set_value(desc1,0);
gpiod_set_value(desc2,0);
gpiod_set_value(desc3,0);
gpiod_set_value(desc4,0);
gpiod_set_value(desc5,0);
gpiod_put(desc1);
gpiod_put(desc2);
gpiod_put(desc3);
gpiod_put(desc4);
gpiod_put(desc5);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");