要求
代码
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
int main(int argc,const char * argv[])
{
int fd;
int status;
if ((fd = open("/dev/my_irq_led1", O_RDWR)) == -1)
{
printf("打开设备失败\n");
exit(-1);
}
while (1)
{
read(fd,&status,sizeof(status));//阻塞读取数据
printf("Number:%d\n",status);
}
close(fd);
return 0;
}
#include <linux/init.h>
#include <linux/module.h>
#include <linux/mod_devicetable.h>
#include <linux/uaccess.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
/*
my_irq_led1{
compatible = "test,my_irq_led1"; //用于获取节点
reg = <0x12345678 0x14>;
interrupt-parent = <&gpiof>; //引用父节点
interrupts = <9 0>; //这个节点引入的中断管脚
led1=<&gpioe 10 0>;
};
*/
#define CNAME "my_irq_led1"
int major;
// 向上申请的结构体目录信息
struct class *cls;
// 向上申请的结构体节点信息
struct device *dev;
wait_queue_head_t wq;
unsigned int condition=0;
unsigned int status=0;
struct device_node *node;
struct gpio_desc *desc;
unsigned int irqnum;
//LED中断函数
irqreturn_t irq_led_handle(int irq, void *dev)
{
//获取LED的状态赋值给status
status = gpiod_get_value(desc);
//状态取反
status = !status;
//设置LED的状态
gpiod_set_value(desc,status);
//表示当前数据准备好
condition=1;
//唤醒休眠进程
wake_up_interruptible(&wq);
return IRQ_HANDLED;
}
//打开LED
int irq_led_open(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
//读数据
ssize_t irq_led_read(struct file *file,char __user *ubuf, size_t size, loff_t *offs)
{
int ret;
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
if(file->f_flags & O_NONBLOCK){
//非阻塞
return -EINVAL;
}else{
//阻塞
ret = wait_event_interruptible(wq,condition);
if(ret < 0){
printk("接收信号失败\n");
return ret;
}
}
//将数据拷贝到用户空间
if(size > sizeof(status)) size = sizeof(status);
ret = copy_to_user(ubuf,(void *)&status,size);
if(ret){
printk("数据拷贝失败\n");
return -EIO;
}
printk("数据拷贝成功\n");
//4.将条件清零
condition = 0;
return size;
}
//关闭
int irq_led_close(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
struct file_operations fops = {
.open = irq_led_open,
.read = irq_led_read,
.release = irq_led_close,
};
int pdrv_probe(struct platform_device *pdev)
{
int ret;
//1.注册字符设备驱动
major = register_chrdev(0,CNAME,&fops);
if(major < 0){
printk("注册字符设备失败\n");
ret = major;
goto ERR1;
}
printk("注册字符设备成功\n");
//2.创建设备节点
// 向上提交目录信息
cls = class_create(THIS_MODULE,CNAME);
if(IS_ERR(cls)){
printk("向上提交设备目录信息失败\n");
ret = PTR_ERR(cls);
goto ERR2;
}
printk("向上提交设备目录信息成功\n");
// 向上提交设备信息
dev = device_create(cls,NULL,MKDEV(major,0),NULL,CNAME);
if(IS_ERR(dev)){
printk("向上提交设备信息失败\n");
ret = PTR_ERR(dev);
goto ERR3;
}
printk("向上提交设备信息成功\n");
//3.初始化等待队列头
init_waitqueue_head(&wq);
//4.解析设备树节点,申请使用的中断和led
node = of_find_node_by_path("/irq_led");
if(node == NULL){
printk("解析设备树失败");
ret = -EAGAIN;
goto ERR4;
}
printk("解析设备树成功");
//获取GPIO结构体信息
desc = gpiod_get_from_of_node(node,"led1",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(desc)){
printk("获取GPIO失败\n");
ret = PTR_ERR(desc);
goto ERR4;
}
//获取软中断号
irqnum = irq_of_parse_and_map(node,0);
if(irqnum == 0){
printk("获取中断号失败\n");
ret = -EAGAIN;
goto ERR5;
}
printk("获取中断号成功\n");
//注册中断
ret = request_irq(irqnum,irq_led_handle,IRQF_TRIGGER_FALLING,CNAME,NULL);
if(ret){
printk("注册中断号失败\n");
goto ERR5;
}
printk("注册中断号成功\n");
return 0;
ERR5:
gpiod_put(desc);
ERR4:
device_destroy(cls,MKDEV(major,0));
ERR3:
class_destroy(cls);
ERR2:
unregister_chrdev(major,CNAME);
ERR1:
return ret;
}
//remove设备和驱动分离时执行
int pdrv_remove(struct platform_device *pdev)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
free_irq(irqnum,NULL);
device_destroy(cls,MKDEV(major,0));
class_destroy(cls);
unregister_chrdev(major,CNAME);
//释放GPIO信息
gpiod_put(desc);
return 0;
}
struct of_device_id oftable[] = {
{.compatible = "test,my_irq_led1"},
{},
};
struct platform_driver pdrv = {
.probe = pdrv_probe,
.remove = pdrv_remove,
.driver = {
.name = "ONE",
.of_match_table = oftable,
},
};
module_platform_driver(pdrv);
MODULE_LICENSE("GPL");
串口显示
实验现象