通过platform阻塞IO来驱动按键控制LED灯的亮灭
a .应用程序通过阻塞的io模型来读取number变量的值
b.number是内核驱动中的一个变量
c .number的值随着按键按下而改变(按键中断)例如number=0按下按键number=1 ,再次按下按键number=0
d .在按下按键的时候需要同时将led1的状态取反
e.驱动中需要编写字符设备驱动
f.驱动中需要自动创建设备节点
g.这个驱动需要的所有设备信息放在设备树的同一个节点中
添加的设备树节点
myplatform{
compatible = "hqyj,myplatform";
reg=<0X12345678 0X400>;
interrupt-parent=<&gpiof>;
interrupts=<9 0>;
led1=<&gpioe 10 0>;
实验现象
pdev.c
#include
#include
#include
void pdev_release(struct device *dev)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
}
struct resource res[]={
[0]={
.start=0X12345678,
.end=0X12345678+59,
.flags=IORESOURCE_MEM,
},
[1]={
.start=71,
.end=71,
.flags=IORESOURCE_IRQ,
},
};
struct platform_device pdev={
.name="aaaaa",
.id=PLATFORM_DEVID_AUTO,
.dev={
.release=pdev_release,
},
.num_resources=ARRAY_SIZE(res),
.resource=res,
};
static int __init mycdev_init(void)
{
platform_device_register(&pdev);
return 0;
}
static void __exit mycdev_exit(void)
{
platform_device_unregister(&pdev);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
pdrv.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
struct resource *res;
unsigned int irqno;
struct gpio_desc *gpiono;
struct cdev *cdev;
unsigned int major = 500;
unsigned int minor = 0;
dev_t devno;
struct class *cls;
struct device *dev;
wait_queue_head_t wq_head;
unsigned int condition = 0;
unsigned int number;
struct device_node *dnode;
irqreturn_t myirq_handler(int irqno, void *dev_id)
{
if (number == 0)
{
number = 1;
gpiod_set_value(gpiono, 1);
}
else
{
number = 0;
gpiod_set_value(gpiono, 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;
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
if (size > sizeof(number))
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 ret;
}
condition = 0;
return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{
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,
};
int pdrv_probe(struct platform_device *pdev)
{
int ret, i;
init_waitqueue_head(&wq_head);
cdev = cdev_alloc();
if (cdev == NULL)
{
printk("申请字符设备驱动对象空间失败\n");
ret = -EFAULT;
goto out1;
}
printk("字符设备驱动对象申请成功\n");
cdev_init(cdev, &fops);
if (major > 0)
{
ret = register_chrdev_region(MKDEV(major, minor), 3, "myplatform");
if (ret)
{
printk("静态指定设备号失败\n");
goto out2;
}
}
else
{
ret = alloc_chrdev_region(&devno, minor, 3, "myplatform");
if (ret)
{
printk("动态申请设备号失败\n");
goto out2;
}
major = MAJOR(devno);
minor = MINOR(devno);
}
printk("申请设备号成功\n");
ret = cdev_add(cdev, MKDEV(major, minor), 3);
if (ret)
{
printk("注册字符设备驱动对象失败\n");
goto out3;
}
printk("注册字符设备驱动对象成功\n");
cls = class_create(THIS_MODULE, "myplatform");
if (IS_ERR(cls))
{
printk("向上提交目录失败\n");
ret = -PTR_ERR(cls);
goto out4;
}
printk("向上提交目录成功\n");
for (i = 0; i < 3; i++)
{
dev = device_create(cls, NULL, MKDEV(major, i), NULL, "myplatform%d", i);
if (IS_ERR(dev))
{
printk("向上提交节点信息失败\n");
ret = -PTR_ERR(dev);
goto out5;
}
}
printk("向上提交设备节点信息成功\n");
dnode = of_find_node_by_name(NULL, "myplatform");
if (dnode == NULL)
{
printk("解析设备树节点失败\n");
return -ENXIO;
}
printk("解析设备树节点成功\n");
irqno = irq_of_parse_and_map(dnode, 0);
if (!irqno)
{
printk("软中断号获取失败\n");
return -ENOMEM;
}
printk("软中断号获取成功 irqno = %d\n", irqno);
ret = request_irq(irqno, myirq_handler, IRQF_TRIGGER_FALLING, "myplatform1", NULL);
if (ret)
{
printk("注册驱动失败\n");
return ret;
}
printk("key1中断注册成功\n");
gpiono = gpiod_get_from_of_node(dnode,"led1",0,GPIOD_OUT_LOW,NULL);
if (IS_ERR(gpiono))
{
printk("申请gpio失败\n");
return -PTR_ERR(gpiono);
}
gpiod_set_value(gpiono, 0);
return 0;
out5:
for (--i; i >= 0; i--)
{
device_destroy(cls, MKDEV(major, i));
}
class_destroy(cls);
out4:
cdev_del(cdev);
out3:
unregister_chrdev_region(MKDEV(major, minor), 3);
out2:
kfree(cdev);
out1:
return ret;
}
int pdrv_remove(struct platform_device *pdev)
{
int i;
gpiod_put(gpiono);
for (i = 0; i < 3; i++)
{
device_destroy(cls, MKDEV(major, i));
}
class_destroy(cls);
cdev_del(cdev);
unregister_chrdev_region(MKDEV(major, minor), 3);
kfree(cdev);
return 0;
}
struct of_device_id oftable[]={
{.compatible="hqyj,myled",},
{},
};
struct platform_driver pdrv={
.probe=pdrv_probe,
.remove=pdrv_remove,
.driver={
.name="aaaaa",
.of_match_table=oftable,
},
};
module_platform_driver(pdrv);
MODULE_LICENSE("GPL");
test1.c
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char const *argv[])
{
unsigned int number;
int fd = open("/dev/myplatform0", 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;
}