test.c
#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 num;
int fd = open("/dev/mychrdev", O_RDWR);
if (fd < 0)
{
printf("打开设备文件失败\n");
exit(-1);
}
while (1)
{
read(fd,&num,sizeof(num));//读取数据
printf("num = %d\n",num);
}
close(fd);
return 0;
}
mychrdev.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <linux/timer.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include <linux/cdev.h>
#include <linux/slab.h>
struct cdev *cdev;
unsigned int major = 0;
unsigned int minor = 0; // 次设备号的起始值
dev_t devno;
struct class *cls;
struct device *dev;
char kbuf[128] = "";
struct device_node *dnode;
unsigned int gpiono;
unsigned int irqno;
int num = 0;
// 定义等待队列头
wait_queue_head_t wq_head;
unsigned int condition = 0;
// 中断处理函数
irqreturn_t myirq_handler(int irqno, void *dev_id)
{
gpio_set_value(gpiono,!gpio_get_value(gpiono));
num = !num;
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;
if (sizeof(num) < size)
size = sizeof(num);
wait_event_interruptible(wq_head, condition); // 将进程切换为休眠
ret = copy_to_user(ubuf,&num, size);
if (ret)
{
printk("copy_to_user failed\n");
return -EIO;
}
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
condition = 0; // 表示下一次硬件数据没有准备好
return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{
int ret;
if (sizeof(kbuf) < size)
size = sizeof(kbuf);
ret = copy_from_user(kbuf, ubuf, size);
if (ret)
{
printk("copy_from_user filed\n");
return -EIO;
}
condition = 1; // 表示硬件数据就绪
wake_up_interruptible(&wq_head); // 唤醒休眠的进程
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
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,
};
static int __init mycdev_init(void)
{
int ret;
// 初始化等待队列头
init_waitqueue_head(&wq_head);
// 1.分配字符设备驱动对象空间 cdev_alloc
cdev = cdev_alloc();
if (cdev == NULL)
{
printk("申请字符设备驱动对象空间失败\n");
ret = -EFAULT;
goto out1;
}
printk("字符设备驱动对象申请成功\n");
// 2.字符设备驱动对象部分初始化 cdev_init
cdev_init(cdev, &fops);
// 3.申请设备号 register_chrdev_region/alloc_chrdev_region
if (major > 0) // 静态申请设备号
{
ret = register_chrdev_region(MKDEV(major, minor), 1, "myled");
if (ret)
{
printk("静态指定设备号失败\n");
goto out2;
}
}
else // 动态申请设备号
{
ret = alloc_chrdev_region(&devno, minor, 1, "myled");
if (ret)
{
printk("动态申请设备号失败\n");
goto out2;
}
major = MAJOR(devno); // 根据设备号得到主设备号
minor = MINOR(devno); // 根据设备号得到次设备号
}
printk("申请设备号成功\n");
// 4.注册字符设备驱动对象 cdev_add()
ret = cdev_add(cdev, MKDEV(major, minor), 1);
if (ret)
{
printk("注册字符设备驱动对象失败\n");
goto out3;
}
printk("注册字符设备驱动对象成功\n");
// 5.向上提交目录
cls = class_create(THIS_MODULE, "mychrdev");
if (IS_ERR(cls))
{
printk("向上提交目录失败\n");
ret = -PTR_ERR(cls);
goto out4;
}
printk("向上提交目录成功\n");
// 6.向上提交设备节点信息
// 为灯创建设备文件
dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "mychrdev");
if (IS_ERR(dev))
{
printk("向上提交设备节点信息失败\n");
return -PTR_ERR(dev);
goto out5;
}
// 解析设备树节点
dnode = of_find_node_by_name(NULL, "mychrdev");
if (dnode == NULL)
{
printk("解析设备树节点失败\n");
return -ENXIO;
}
printk("解析设备树节点成功\n");
// 根据设备树节点解析led1gpio编号
gpiono = of_get_named_gpio(dnode, "led1", 0);
if (gpiono < 0)
{
printk("gpio编号解析失败\n");
}
printk("解析gpio编号成功gpiono=%d\n", gpiono);
// 申请gpio编号
ret = gpio_request(gpiono, NULL);
if (ret < 0)
{
printk("申请gpio编号失败\n");
return ret;
}
// led1对应的gpio的管脚为输出
gpio_direction_output(gpiono, 0);
// 设置管脚输出高电平,亮灯
gpio_set_value(gpiono, 1);
// 获取软中断号
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, "key1", NULL);
if (ret)
{
printk("注册驱动失败\n");
return ret;
}
printk("key1中断注册成功\n");
return 0;
out5:
// 销毁上面提交的设备信息
device_destroy(cls, MKDEV(major, 0));
class_destroy(cls);
out4:
cdev_del(cdev);
out3:
unregister_chrdev_region(MKDEV(major, minor), 3);
out2:
kfree(cdev);
out1:
return ret;
}
static void __exit mycdev_exit(void)
{
// 灭灯
gpio_set_value(gpiono, 0);
// 释放gpio编号
gpio_free(gpiono);
// 注销中断
free_irq(irqno, NULL);
// 销毁设备节点信息
device_destroy(cls, MKDEV(major, 0));
// 销毁目录信息
class_destroy(cls);
// 销毁字符设备驱动
// 字符设备驱动的注销
unregister_chrdev(major, "mychrdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
实验现象