通过platform总线驱动框架实现GPIO子系统的LED驱动,编写应用程序测试
pdrv.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/mod_devicetable.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/interrupt.h>
// 功能码
#define LED_ON _IOW('l', 1, int)
#define LED_OFF _IOW('l', 0, int)
// 设备号
dev_t devno;
struct fasync_struct *fapp;
unsigned int minor = 0;
unsigned char kbuf[128] = {0};
unsigned int condition = 0;
struct cdev *cdev; // 字符设备驱动对象
struct class *cls; // 设备节点目录
struct device *dev;
#if 1
unsigned int major = 0;
#else
unsigned int major = 400;
#endif
// 定义一个GPIO编号数组
struct gpio_desc *gpiono[3] = {0};
struct resource *res;
// 定义一个数组存储中断号
unsigned int irqno[3];
// 封装结构体当中的操作方法
int mycdev_open(struct inode *inde, 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)
{
unsigned long ret;
// 向用户空间读取拷贝
if (size > sizeof(kbuf))
{
size = sizeof(kbuf);
}
ret = copy_to_user(ubuf, kbuf, size);
if (ret) // 拷贝失败
{
printk("copy_to_user filed\n");
return ret;
}
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{
unsigned long ret;
// 从用户空间写入数据
if (size > sizeof(kbuf))
{
size = sizeof(kbuf);
}
ret = copy_from_user(kbuf, ubuf, size);
if (ret) // 拷贝失败
{
printk("copy_from_user filed\n");
return ret;
}
// 像读取数据的进程中发送SIGIO信号
kill_fasync(&fapp, SIGIO, POLL_IN);
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
// ioctl函数
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
unsigned int whitch, ret;
// 保存第三个参数到whtich
ret = copy_from_user(&whitch, (void *)arg, sizeof(int));
if (ret)
{
printk("copy_from_user failed\n");
return -1;
}
switch (cmd)
{
case LED_ON:
switch (whitch)
{
case 1:
printk("LED1开灯\n");
// 设置管脚输出高电平(点灯)
gpiod_set_value(gpiono[whitch - 1], 1);
break;
case 2:
printk("LED2开灯\n");
gpiod_set_value(gpiono[whitch - 1], 1);
break;
case 3:
printk("LED3开灯\n");
gpiod_set_value(gpiono[whitch - 1], 1);
break;
}
break;
case LED_OFF:
switch (whitch)
{
case 1:
printk("LED1关灯\n");
gpiod_set_value(gpiono[whitch - 1], 0);
break;
case 2:
printk("LED2关灯\n");
gpiod_set_value(gpiono[whitch - 1], 0);
break;
case 3:
printk("LED3关灯\n");
gpiod_set_value(gpiono[whitch - 1], 0);
break;
}
break;
}
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,
.unlocked_ioctl = mycdev_ioctl,
.release = mycdev_close,
};
// 中断处理函数
irqreturn_t myirq_handler(int irqno, void *dev)
{
int whitch = (int)dev;
switch (whitch)
{
case 0:
printk("LED1开灯\n");
// 设置管脚输出高电平(电平)
gpiod_set_value(gpiono[whitch], 0);
break;
case 1:
printk("LED2开灯\n");
// 设置管脚输出高电平(电平)
gpiod_set_value(gpiono[whitch], 0);
break;
case 2:
printk("LED3开灯\n");
// 设置管脚输出高电平(电平)
gpiod_set_value(gpiono[whitch], 0);
break;
}
return IRQ_HANDLED;
}
// probe函数用于匹配设备成功后执行
int pdrv_probe(struct platform_device *pdev) // 当设备匹配成功后执行probe
{
int i, ret;
// 实例化字符设备驱动
// 1.实例化字符设备驱动对象
cdev = cdev_alloc();
if (cdev == NULL)
{
ret = -ENOMEM;
goto OUT1;
}
// 2部分初始化字符设备驱动对象
cdev_init(cdev, &fops);
// 3申请设备号
if (major == 0) // 动态申请设备号
{
ret = alloc_chrdev_region(&devno, minor, 3, "mycdev");
if (ret)
{
printk("动态申请设备号失败\n");
goto OUT2;
}
major = MAJOR(devno);
minor = MINOR(devno);
}
else // 静态指定设备号
{
devno = MKDEV(major, 0);
ret = register_chrdev_region(devno, 3, "mydev");
if (ret)
{
printk("静态设备号指定失败\n");
goto OUT2;
}
}
printk("设备号申请成功\n");
// 4将字符设备驱动对象注册进内核
ret = cdev_add(cdev, devno, 3);
if (ret)
{
printk("注册进内核字符设备失败\n");
goto OUT3;
}
printk("注册进内核字符设备成功\n");
// 5.自动创建设备节点
cls = class_create(THIS_MODULE, "mydev");
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, "mycdev_%d", i);
if (IS_ERR(dev))
{
printk("向上提交设备节点信息失败\n");
ret = PTR_ERR(dev);
goto OUT5;
}
}
printk("自动创建设备节点成功\n");
// 解析MEM类型的资源
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (res == NULL)
{
printk("资源解析失败%d\n", __LINE__);
return -ENOMEM;
}
// 解析中断类型的资源
for (i = 0; i < 3; i++)
{
irqno[i] = platform_get_irq(pdev, i);
if (irqno[i] < 0)
{
printk("资源解析失败%d\n", __LINE__);
return -ENOMEM;
}
printk("mem:%x irq:%d\n", res->start, irqno[i]);
// 注册中断
ret = request_irq(irqno[i], myirq_handler, IRQF_TRIGGER_FALLING, "key", (void *)i);
if (ret < 0)
{
printk("中断注册失败\n");
return ret;
}
}
printk("中断号注册成功\n");
// 解析出GPIO编号并申请
gpiono[0] = gpiod_get_from_of_node(pdev->dev.of_node, "led1", 0, GPIOD_OUT_LOW, NULL);
gpiono[1] = gpiod_get_from_of_node(pdev->dev.of_node, "led2", 0, GPIOD_OUT_LOW, NULL);
gpiono[2] = gpiod_get_from_of_node(pdev->dev.of_node, "led3", 0, GPIOD_OUT_LOW, NULL);
for (i = 0; i < 3; i++)
{
if (IS_ERR(gpiono[i]))
{
printk("GPIO信息解析失败\n");
return -PTR_ERR(gpiono);
}
printk("GPIO信息解析成功\n");
// 亮灯
gpiod_set_value(gpiono[i], 1);
}
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(devno, 3);
OUT2:
kfree(cdev);
OUT1:
return ret;
}
// remove用于和设备分离时执行
int pdrv_remove(struct platform_device *pdev) // 当设备和驱动分离时执行remove
{
int i;
// 灭灯
for(i=0;i<3;i++)
{
gpiod_set_value(gpiono[i], 0);
// 释放GPIO编号
gpiod_put(gpiono[i]);
}
//销毁设备节点信息
for(i=0;i<3;i++)
{
device_destroy(cls,MKDEV(major,i));
}
//销毁目录
class_destroy(cls);
//将字符设备驱动注销
cdev_del(cdev);
unregister_chrdev_region(devno,3);
kfree(cdev);
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
// 在驱动中创建一个用于设备树匹配的表
struct of_device_id oftable[] = {
{
.compatible = "hqyj,myplatform",
},
{
.compatible = "hqyj,myplatform1",
},
{
.compatible = "hqyj,myplatform2",
},
{}, // 防止越界访问
};
// 分配对象并初始化
struct platform_driver pdrv = {
.probe = pdrv_probe,
.remove = pdrv_remove,
.driver = {
.name = "aaa",
.of_match_table = oftable, // 指定设备树匹配表的首地址
},
};
// 一建注册宏
module_platform_driver(pdrv);
MODULE_LICENSE("GPL");
main.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
//功能码
#define LED_ON _IOW('l', 1, int)
#define LED_OFF _IOW('l', 0, int)
int main(int argc, char const *argv[])
{
unsigned int ans, addr;
char buf[128] = {0};
int fd = open("/dev/mycdev_0", O_RDWR);
if(fd < 0)
{
printf("设备文件打开失败\n");
exit(-1);
}
while(1)
{
//从终端读取
printf("请输入进行进行的操作: 1(开灯) 0(关灯) >");
scanf("%d", &ans);
if(1 == ans)
{
printf("请输入要操作的灯:1(LED1) 2(LED2) 3(LED3)>");
scanf("%d", &addr);
ioctl(fd, LED_ON, &addr);
}
else
{
printf("请输入要操作的灯:1(LED1) 2(LED2) 3(LED3)>");
scanf("%d", &addr);
ioctl(fd, LED_OFF, &addr);
}
}
close(fd);
return 0;
}
结果: