- io_interupt.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/of_irq.h>
#include <linux/of.h>
#include <linux/interrupt.h>
#include <linux/fs.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
/*
myinteruptled
{
led1=<&gpioe 10 0>;
led2=<&gpiof 10 0>;
led3=<&gpioe 8 0>;
interrupt-parent = <&gpiof>;
interrupts=<9 0>,<7 0>,<8 0>;
};
*/
struct device_node *dnode;
unsigned int irqno[3] = {0};
struct class *cls;
struct device *dev;
int major; // 用于保存主设备号
struct device_node *dnode;
struct gpio_desc *gpiono1, *gpiono2, *gpiono3;
// 分配底半部对象
struct tasklet_struct tasklet;
unsigned int number = 0;
//定义等待队列头
wait_queue_head_t wq_head;
unsigned int condition=0;
// 定义底半部处理函数
void tasklet_callback(struct tasklet_struct *t)
{
number = !number;
condition = 1;//表示硬件数据就绪
wake_up_interruptible(&wq_head);//唤醒休眠的进程
}
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)
{
int ret;
if (sizeof(number) < size)
size = sizeof(number);
wait_event_interruptible(wq_head,condition);//将进程切换为休眠
ret = copy_to_user(ubuf, &number, size);
if (ret)
{
printk("copy_to_user filed\n");
return -EIO;
}
condition = 0;//表示下一次硬件数据没有准备好
return 0;
}
// 中断处理函数
irqreturn_t myirq_handler(int irqnode, void *dev_id)
{
//KEY1控制LED1,KEY2控制LED2,KEY3控制LED3
if (irqnode == irqno[0])
{
gpiod_set_value(gpiono3, !gpiod_get_value(gpiono3));
printk("key1 interrupt\n");
}
else if (irqnode == irqno[1])
{
gpiod_set_value(gpiono2, !gpiod_get_value(gpiono2));
printk("key2 interrupt\n");
}
else if (irqnode == irqno[2])
{
gpiod_set_value(gpiono1, !gpiod_get_value(gpiono1));
printk("key3 interrupt\n");
}
tasklet_schedule(&tasklet);
return IRQ_HANDLED;
}
struct file_operations fops =
{
.open = mycdev_open,
.read = mycdev_read,
};
static int __init mycdev_init(void)
{
int ret, i;
//初始化等待队列头
init_waitqueue_head(&wq_head);
// 底半部对象初始化
tasklet_setup(&tasklet, tasklet_callback);
// 解析设备树节点********************************
dnode = of_find_node_by_path("/myinteruptled");
if (dnode == NULL)
{
printk("解析设备树节点失败\n");
return -ENXIO;
}
printk("解析设备树节点成功\n");
for (i = 0; i < 3; i++)
{
// 获取中断号*********************************
irqno[i] = irq_of_parse_and_map(dnode, i);
if (!irqno[i])
{
printk("软中断号获取失败\n");
return -ENOMEM;
}
printk("软中断号获取成功 irqno[%d] = %d\n", i, irqno[i]);
// 注册中断************************************
ret = request_irq(irqno[i], myirq_handler, IRQF_TRIGGER_FALLING, "key1", NULL);
if (ret)
{
printk("注册key%d驱动失败\n", i + 1);
for (i--; i >= 0; i--)
{
free_irq(irqno[i], NULL);
}
return ret;
}
printk("key%d中断注册成功\n", i + 1);
}
// 字符设备驱动注册
major = register_chrdev(0, "myiointrupt", &fops);
if (major < 0)
{
printk("字符设备驱动注册失败\n");
return major;
}
printk("字符设备驱动注册成功major=%d\n", major);
// 向上提交目录
cls = class_create(THIS_MODULE, "myiointrupt");
if (IS_ERR(cls))
{
printk("向上提交目录失败\n");
return -PTR_ERR(cls);
}
printk("向上提交目录成功\n");
// 向上提交设备节点信息
dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "myiointrupt");
if (IS_ERR(dev))
{
printk("向上提交设备节点信息失败\n");
return -PTR_ERR(dev);
}
printk("向上提交设备节点信息成功\n");
// 申请gpio并向内核注册
gpiono1 = gpiod_get_from_of_node(dnode, "led1", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono1))
{
printk("申请gpio失败\n");
return -PTR_ERR(gpiono1);
}
gpiono2 = gpiod_get_from_of_node(dnode, "led2", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono2))
{
gpiod_put(gpiono1);
printk("申请gpio失败\n");
return -PTR_ERR(gpiono2);
}
gpiono3 = gpiod_get_from_of_node(dnode, "led3", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono3))
{
gpiod_put(gpiono1);
gpiod_put(gpiono2);
printk("申请gpio失败\n");
return -PTR_ERR(gpiono3);
}
printk("申请gpio成功\n");
// 亮灯
gpiod_set_value(gpiono1, 1);
gpiod_set_value(gpiono2, 1);
gpiod_set_value(gpiono3, 1);
return 0;
}
static void __exit mycdev_exit(void)
{
int i;
// 注销中断****************************************
for (i = 0; i < 3; i++)
{
free_irq(irqno[i], NULL);
}
// 灭灯
gpiod_set_value(gpiono1, 0);
gpiod_set_value(gpiono2, 0);
gpiod_set_value(gpiono3, 0);
// 注销gpio信息
gpiod_put(gpiono1);
gpiod_put(gpiono2);
gpiod_put(gpiono3);
// 销毁设备节点信息
device_destroy(cls, MKDEV(major, 0));
class_destroy(cls);
// 字符设备驱动的注销
unregister_chrdev(major, "myiointrupt");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
- test.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
unsigned int number;
int main(int argc, char const *argv[])
{
int fd = open("/dev/myiointrupt", O_RDWR);
if (fd < 0)
{
printf("打开设备文件失败\n");
exit(-1);
}
while (1)
{
read(fd, &number, sizeof(number));
printf("number = %d\n", number);
}
close(fd);
return 0;
}
- 现象: