应用程序代码
#include<stdio.h>
#include<string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main (int argc, const char *argv[])
{
char buf[128] = "";
int fd = open("/dev/led0",O_RDWR);
if(fd < 0)
{
printf("打开文件失败\n");
return -1;
}
while(1)
{
memset(buf,0,sizeof(buf));
read(fd,buf,sizeof(buf));
printf("%s\n",buf);
}
close(fd);
return 0;
}
驱动代码
#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/gpio.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
struct cdev *cdev;
unsigned int irqnum;
unsigned int major;
unsigned int minor;
dev_t devnum;
struct class *cls;
struct device *dev;
struct device_node *dnode;
//gpio结构体
struct gpio_desc *gpionum;
//定义等待队列头
wait_queue_head_t wq_head;
unsigned int number = 0;
char kbuf[128] = "";
unsigned int condition=0;
//中断处理函数
irqreturn_t myirq_handler(int irqnum,void *dev_id)
{
number = !number;
gpiod_set_value(gpionum,!gpiod_get_value(gpionum));
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 __user *ubuf, size_t size, loff_t * lof)
{
int ret;
if(sizeof(kbuf) < size)
{
size = sizeof(kbuf);
}
wait_event_interruptible(wq_head,condition);//将进程切换为休眠
ret = copy_to_user(ubuf,kbuf,size);
if(ret)
{
printk("copy_tO_user filed\n");
return -EIO;
}
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
condition=0;//表示下一次硬件数据没有准备好
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,
.release = mycdev_close,
};
static int __init mycdev_init(void)
{
int ret;
//初始化等待队列头
init_waitqueue_head(&wq_head);
//申请字符设备对象空间
cdev = cdev_alloc();
if(cdev == NULL)
{
printk("为字符设备对象申请空间失败\n");
return -EIO;
}
printk("为字符设备对象申请空间成功\n");
//初始化字符设备对象
cdev_init(cdev,&fops);
//申请设备号
//动态申请
ret = alloc_chrdev_region(&devnum,0,1,"myled");
if(ret < 0)
{
printk("led灯申请设备号失败\n");
ret = -EFAULT;
return ret;
}
major = MAJOR(devnum);
minor = MINOR(devnum);
printk("led灯申请设备号成功,major = %d\n",major);
//注册字符设备驱动
ret = cdev_add(cdev,MKDEV(major,minor),1);
if(ret < 0)
{
printk("注册字符设备驱动失败");
return -EFAULT;
}
printk("注册字符设备驱动成功");
//向上提交目录
cls = class_create(THIS_MODULE,"myled");
if(IS_ERR(cls))
{
printk("向上提交目录失败\n");
ret = -PTR_ERR(cls);
return ret;
}
printk("向上提交目录成功\n");
dev = device_create(cls,NULL,MKDEV(major,minor),NULL,"led0");
if(IS_ERR(dev))
{
printk("向上提交节点信息失败\n");
ret = -PTR_ERR(dev);
return ret;
}
printk("向上提交节点信息成功\n");
//解析设备树节点信息
dnode = of_find_node_by_name(NULL,"mydevs");
if(dnode == NULL)
{
printk("解析设备树节点信息失败\n");
return -ENXIO;
}
printk("解析设备树节点信息成功\n");
//根据设备树节点解析led1 gpio结构体并向内核注册
gpionum = gpiod_get_from_of_node(dnode,"led1",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(gpionum))
{
printk("申请gpio失败\n");
return -PTR_ERR(gpionum);
}
printk("申请gpio成功\n");
//默认灭灯
gpiod_set_value(gpionum,1);
//获取软中断号
irqnum = irq_of_parse_and_map(dnode,0);
if(!irqnum)
{
printk("软中断号获取失败\n");
return -ENOMEM;
}
printk("软中断号获取成功,irqnum = %d\n",irqnum);
//注册中断
ret = request_irq(irqnum,myirq_handler,IRQF_TRIGGER_FALLING,"key1",NULL);
if(ret)
{
printk("注册中断失败\n");
return ret;
}
printk("key1注册中断成功\n");
return 0;
}
static void __exit mycdev_exit(void)
{
//注销中断
free_irq(irqnum,NULL);
//灭灯
gpiod_set_value(gpionum,0);
//注销gpio信息
gpiod_put(gpionum);
//释放设备树节点信息
device_destroy(cls,MKDEV(major,minor));
class_destroy(cls);
//注销字符设备驱动
cdev_del(cdev);
//释放申请的设备号
unregister_chrdev_region(MKDEV(major,minor),1);
//释放字符驱动对象空间
kfree(cdev);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");