驱动代码
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/platform_device.h>
#include <linux/mod_devicetable.h>
#include <linux/of_gpio.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
unsigned int condition = 0;
//定义一个等待队列头
wait_queue_head_t wq_head;
unsigned int major = 0;
struct class *cls;
struct device *dev;
int number = 0;
struct resource *res;
unsigned int irqno;
struct gpio_desc *gpiono;
//定义中断处理函数
irqreturn_t key_handler(int irq,void *dev)
{
gpiod_set_value(gpiono,!gpiod_get_value(gpiono));
if (number == 0)
{
number = 1;
condition = 1;
wake_up_interruptible(&wq_head);
}
else
{
number = 0;
condition = 1;
wake_up_interruptible(&wq_head);
}
return IRQ_HANDLED;
}
//封装操作方法
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;
//判断IO方式
if (file ->f_flags&O_NONBLOCK)
{
}
else
{
wait_event_interruptible(wq_head,condition);
}
ret = copy_to_user(ubuf,&number,size);
if (ret)
{
printk("copy_to_user err\n");
return -EIO;
}
condition = 0;//下一次硬件数据,没有就绪
return 0;
}
ssize_t mycdev_write(struct file *file,const char*ubuf,size_t size,loff_t *lof)
{
int ret;
ret =copy_from_user(&number,ubuf,size);
if (ret)
{
printk("copy_from_user err\n");
return -EIO;
}
condition = 1;
wake_up_interruptible(&wq_head);
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,
};
//封装probe函数
int pdrv_probe(struct platform_device *pdev)
{
init_waitqueue_head(&wq_head);
//字符设备注册
major = register_chrdev(0,"mychrdev",&fops);
{
if (major < 0)
{
printk("字符设备驱动注册失败\n");
return major;
}
printk("字符设备驱动注册成功:major=%d\n",major);
}
//获取中断类型的资源
//中断号
irqno = platform_get_irq(pdev,0);
if (irqno < 0)
{
printk("获取中断类型的资源失败\n");
return -ENXIO;
}
printk("irq资源%d\n",irqno);
//设备树匹配成功后,设备树节点指针可以通过pdev->dev.of_node获取
//基于设备树节点信息获取gpio_desc对象指针
gpiono=gpiod_get_from_of_node(pdev->dev.of_node,"led1-gpio",0,GPIOD_OUT_LOW,NULL);
if (IS_ERR(gpiono))
{
printk("解析GPIO管脚信息失败\n");
return -ENXIO;
}
printk("解析GPIO管脚信息成功\n");
//注册按键中断
int ret = request_irq(irqno,key_handler,IRQF_TRIGGER_FALLING,"key_int",NULL);
if (ret < 0)
{
printk("注册按键中断失败\n");
return ret;
}
printk("注册按键中断成功\n");
//向上提交目录信息
cls = class_create(THIS_MODULE,"myled");
if (IS_ERR(cls))
{
printk("向上提交目录失败\n");
return -PTR_ERR(cls);
}
printk("向上提交目录成功\n");
dev = device_create(cls,NULL,MKDEV(major,0),NULL,"myled0");
if(IS_ERR(dev))
{
printk("向上提交设备节点信息失败\n");
return -PTR_ERR(dev);
}
printk("向上提交设备节点信息成功\n");
return 0;
}
//封装remove函数
int pdrv_remove(struct platform_device *pdev)
{
//释放GPIO信息
gpiod_put(gpiono);
//销毁目录
class_destroy(cls);
unregister_chrdev(major, "myled");
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
//构建设备树匹配表
struct of_device_id oftable[] =
{
{.compatible = "hqyj,myplatform"},
{},//防止数组越界
};
//定义驱动信息对象并初始化
struct platform_driver pdrv = {
.probe = pdrv_probe,
.remove = pdrv_remove,
.driver = {
.name = "bbbbb",
.of_match_table=oftable,//用于设备树匹配
},
};
//一键注册宏
module_platform_driver(pdrv);
MODULE_LICENSE("GPL");
应用代码
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
int main(int argc, char const *argv[])
{
int number;
int fd = open("/dev/myled0",O_RDWR);
if (fd < 0)
{
printf("打开设备文件失败\n");
exit(-1);
}
while (1)
{
read(fd,&number,sizeof(number));
printf("%d\n",number);
}
close(fd);
return 0;
}