如下要求
a.应用程序通过阻塞的io模型来读取number变量的值b.number是内核驱动中的一个变量
c.number的值随着按键按下而改变(按键中断)
例如number=O按下按键number=1 ,再次按下按键number=Od .在按下按键的时候需要同时将led1的状态取反
e .驱动中需要编写字符设备驱动
f.驱动中需要自动创建设备节点
g.这个驱动需要的所有设备信息放在设备树的同一个节点中
添加的设备树节点
myopt{
led1=<&gpioe 10 0>;
interrupts-extended=<&gpiof 9 0>;
};
驱动代码
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include <linux/gpio.h>
int major; // 主设备号
struct class *cls;
struct device *dev;
// 定义一个等待队列头
wait_queue_head_t wq_head;
unsigned int condition = 0;
struct device_node *dnode;
unsigned int myirq; // 中断号
struct gpio_desc *gpiono; // GPIO对象
char cbuf[128] = "";
int number;
// 中断处理函数
irqreturn_t myirq_handeler(int myirq, void *arg)
{
int statue;
condition = 1;
wake_up_interruptible(&wq_head);
// 获取当前LED1的状态
statue = gpiod_get_value(gpiono);
if (statue == 1) // 如果为亮灯,则熄灭
{
gpiod_set_value(gpiono, 0);
number = 1;
}
else if (statue == 0) // 如果为灭灯,则点亮
{
gpiod_set_value(gpiono, 1);
number = 0;
}
return IRQ_HANDLED;
}
// 封装操作方法
int my_open(struct inode *inode, struct file *file)
{
return 0;
}
ssize_t my_read(struct file *file, char *ubuf, size_t size, loff_t *lof)
{
int ret;
wait_event_interruptible(wq_head, condition);
printk("从内核向用户拷贝内容\n");
if(number == 1)
cbuf[0] = '1';
else if(number == 0)
cbuf[0] = '0';
ret = copy_to_user(ubuf, cbuf, sizeof(cbuf));
if (ret < 0)
{
printk("copy_to_user failed\n");
return -1;
}
condition = 0;
return 0;
}
int my_close(struct inode *inode, struct file *file)
{
return 0;
}
// 操作方法结构体定义并初始化
struct file_operations fops =
{
.open = my_open,
.read = my_read,
.release = my_close,
};
static int __init mycdev_init(void)
{
int ret;
// 初始化等待队列
init_waitqueue_head(&wq_head);
// 注册设备驱动
major = register_chrdev(0, "myopt", &fops);
if (major < 0)
{
printk("注册设备驱动失败\n");
return -1;
}
printk("注册设备驱动成功\n");
// 向上提交设备节点目录
cls = class_create(THIS_MODULE, "myopt");
if (IS_ERR(cls))
{
printk("向上提交设备节点目录失败\n");
ret = -PTR_ERR(cls);
goto out1;
}
printk("向上提交设备节点目录成功\n");
// 向上提交设备节点信息
dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "myopt0");
if (IS_ERR(dev))
{
printk("向上提交设备节点信息失败\n");
ret = -PTR_ERR(dev);
goto out2;
}
printk("向上提交设备节点信息成功\n");
// 解析设备树节点信息
dnode = of_find_node_by_name(NULL, "myopt");
if (dnode == NULL)
{
printk("解析相关设备树节点信息失败\n");
ret = -1;
goto out3;
}
printk("解析相关设备树节点信息成功\n");
// 从设备树节点中解析出GPIO对象
gpiono = gpiod_get_from_of_node(dnode, "led1", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono))
{
printk("从设备树节点中解析出GPIO对象失败\n");
ret = -PTR_ERR(gpiono);
goto out3;
}
printk("从设备树节点中解析出GPIO对象成功\n");
// 获取中断号
myirq = irq_of_parse_and_map(dnode, 0);
if (!myirq)
{
printk("获取中断号失败\n");
ret = -1;
goto out4;
}
printk("获取中断号成功\n");
// 注册中断号
ret = request_irq(myirq, myirq_handeler, IRQF_TRIGGER_FALLING, "key", NULL);
if (ret < 0)
{
printk("注册中断号失败\n");
ret = -1;
goto out4;
}
printk("注册中断号成功\n");
return 0;
out4:
gpiod_put(gpiono);
out3:
device_destroy(cls, MKDEV(major, 0));
out2:
class_destroy(cls);
out1:
unregister_chrdev(major, "myopt");
return ret;
}
static void __exit mycdev_exit(void)
{
// 释放中断号
free_irq(myirq, NULL);
// 释放gpio编号
gpiod_put(gpiono);
// 注销设备信息
device_destroy(cls, MKDEV(major, 0));
// 注销设备目录
class_destroy(cls);
// 注销设备驱动
unregister_chrdev(major, "myopt");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
测试的应用程序代码
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
char buf[128] = "";
int main(int argc, const char *argv[])
{
unsigned int number;
int fd = open("/dev/myopt0",O_RDWR);
if(fd < 0)
{
printf("打开设备文件失败\n");
return -1;
}
printf("打开设备文件成功\n");
while(1)
{
printf("准备读取\n");
read(fd,buf,sizeof(buf));
printf("读取完毕\n");
printf("number = %c\n",buf[0]);
}
close(fd);
return 0;
}