通过platform总线驱动实现
a.应用程序通过阻塞的io模型来读取number变量的值
b.number是内核驱动中的一个变量
c.number的值随着按键按下而改变(按键中断) 例如number=0 按下按键number=1 ,再次按下按键number=0
d.在按下按键的时候需要同时将led1的状态取反
e.驱动中需要编写字符设备驱动
f.驱动中需要自动创建设备节点
g.这个驱动需要的所有设备信息放在设备树的同一个节点中
底层驱动程序:pdrv.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "myled.h"
#include
#include
#include
#include
#include
struct resource *res;
int irqno;
struct gpio_desc *desc;
struct gpio_desc *desc1;
wait_queue_head_t head;
unsigned int condition;
unsigned char number;
struct cdev *cdev;
struct class* cls;
struct device* dev;
dev_t dev1;
unsigned int major;
unsigned int minor;
//操作方法结构体
ssize_t mycdev_read(struct file *file, char __user *ubuf, size_t size, loff_t *loff)
{
int ret;
if(file->f_flags&O_NONBLOCK)
{
return -EINVAL;
}else
{
ret=wait_event_interruptible(head,condition);
if(ret)
{
printk("接收阻塞,休眠\n");
return ret;
}
}
if(size>sizeof(char))
size=sizeof(char);
ret=copy_to_user(ubuf,&number,size);
if(ret)
{
printk("数据从内核向用户拷贝失败\n");
return -EIO;
}
condition=0;
return size;
}
ssize_t mycdev_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loff)
{
return 0;
}
int mycdev_open(struct inode *inode, struct file *file)
{
return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
return 0;
}
struct file_operations fops=
{
.open=mycdev_open,
.read=mycdev_read,
.write=mycdev_write,
.release=mycdev_close,
};
//中断处理函数
irqreturn_t irq_handler(int irq,void *dev)
{
gpiod_set_value(desc,!gpiod_get_value(desc));
if(number=='0')
{
number='1';
condition=1;
wake_up_interruptible(&head);
}else
{
number='0';
condition=1;
wake_up_interruptible(&head);
}
return IRQ_HANDLED;
}
//platfom总线设置
int pdrv_probe(struct platform_device *pdr)
{
int ret;
printk("%s:%d\n",__FILE__,__LINE__);
res=platform_get_resource(pdr,IORESOURCE_MEM,0);
if(res==NULL)
{
printk("获取资源失败\n");
return -ENODATA;
}
irqno=platform_get_irq(pdr,0);
if(irqno<0)
{
printk("获取资源失败\n");
return irqno;
}
printk("%#x %d\n",res->start,irqno);
//获取gpio编号
desc=gpiod_get_from_of_node(pdr->dev.of_node,"led1",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(desc))
{
printk("获取GPIO编号失败\n");
return PTR_ERR(desc);
}
//注册软中断号
ret=request_irq(irqno,irq_handler,IRQF_TRIGGER_FALLING,"key2——inte",NULL);
if(ret)
{
printk("注册中断号失败\n");
return ret;
}
printk("注册中断号成功\n");
condition=0;
init_waitqueue_head(&head);
return 0;
}
int pdrv_remove(struct platform_device *pdrv)
{
free_irq(irqno,NULL);
gpiod_set_value(desc,0);
gpiod_put(desc);
printk("%s:%d\n",__FILE__,__LINE__);
return 0;
}
//定义compatible表
struct of_device_id oftable[]=
{
{.compatible="hqyj,hello2"},
{},
};
MODULE_DEVICE_TABLE(of,oftable);
struct platform_driver pdrv={
.probe=pdrv_probe,
.remove=pdrv_remove,
.driver={
.name="test1",
.of_match_table=oftable,
},
};
static int __init mycdev_init(void)
{
int ret,i;
//注册设备驱动
//1,分配设备驱动对象
cdev=cdev_alloc();
if(NULL==cdev)
{
printk("申请驱动对象空间失败\n");
ret = -ENOMEM;
goto ERR1;
//return -ENOMEM;
}
//2,初始化对象
cdev_init(cdev,&fops);
//3,设备号的申请
ret=alloc_chrdev_region(&dev1,0,3,"LED");
if(ret != 0)
{
printk("申请设备号失败\n");
goto ERR2;
}
major = MAJOR(dev1);
minor = MINOR(dev1);
printk("动态设备号申请成功\n");
//4,注册字符设备驱动
ret=cdev_add(cdev,dev1,3);
if(ret !=0)
{
printk("注册设备驱动失败\n");
goto ERR3;
}
printk("注册字符设备驱动成功:%d\n",dev1);
//5,自动创建设备节点
//创建目录
cls=class_create(THIS_MODULE,"LED");
if(IS_ERR(cls))
{
printk("向上提交目录失败\n");
goto ERR4;
}
printk("向上提交目录成功\n");
//创建设备节点 3次
for(i=0;i<3;i++)
{
dev=device_create(cls,NULL,MKDEV(major,i),NULL,"myled%d",i);
if(IS_ERR(dev))
{
printk("向上提交节点失败\n");
ret = PTR_ERR(dev);
goto ERR5;
}
}
printk("向上提交节点成功\n");
platform_driver_register(&pdrv);
return 0;
ERR5:
for(--i;i>=0;i--)
{
device_destroy(cls,MKDEV(major,i));
}
class_destroy(cls);
ERR4:
cdev_del(cdev);
ERR3:
unregister_chrdev_region(dev1,3);
ERR2:
kfree(cdev);
ERR1:
return ret;
}
static void __exit mycdev_exit(void)
{
int i;
platform_driver_unregister(&pdrv);
for(i=0;i<3;i++)
{
device_destroy(cls,MKDEV(major,i));
}
//销毁目录
class_destroy(cls);
//2,注销字符设备驱动
cdev_del(cdev);
//3,释放设备号
unregister_chrdev_region(dev1,3);
//4,释放动态申请的空间
kfree(cdev);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
应用层程序
#include
#include
#include
#include
#include
#include
#include
#include
#include "myled.h"
#include
#include
#include
#include
#include
int main(int argc, char const *argv[])
{
int fd;
char number;
fd=open("/dev/myled1",O_RDWR);
if(fd<0)
{
printf("打开platform总线失败\n");
exit(-1);
}
while(1)
{
int ret;
ret=read(fd,&number,sizeof(char));
if(0==ret)
{
printf("读取失败\n");
break;
}
printf("number=%c\n",number);
}
close(fd);
return 0;
}
效果: