效果图:
驱动代码:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include<linux/mod_devicetable.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include<linux/fs.h>
#include<linux/io.h>
#include<linux/device.h>
#define LED_ON _IOW('1',1,int)
#define LED_OFF _IOW('1',0,int)
struct resource *res;
unsigned int irqno;
struct gpio_desc *gpiono[3];
int major;
struct class *cls;
struct device *led_dev;
long myled_ioctl(struct file * file, unsigned int cmd, unsigned long arg)
{
switch (arg)
{
case 1:
switch (cmd)
{
case LED_ON:
gpiod_set_value(gpiono[0], 1);
break;
case LED_OFF:
gpiod_set_value(gpiono[0], 0);
break;
}
break;
case 2:
switch (cmd)
{
case LED_ON:
gpiod_set_value(gpiono[1], 1);
break;
case LED_OFF:
gpiod_set_value(gpiono[1], 0);
break;
}
break;
case 3:
switch (cmd)
{
case LED_ON:
gpiod_set_value(gpiono[2], 1);
break;
case LED_OFF:
gpiod_set_value(gpiono[2], 0);
break;
}
break;
}
return 0;
}
struct file_operations fops={
.unlocked_ioctl=myled_ioctl,
};
//封装probe函数,当设备和驱动匹配成功之后执行
int pdrv_probe(struct platform_device *dev)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
//获取mem类型的资源
res=platform_get_resource(dev, IORESOURCE_MEM, 0);
if(res == NULL)
{
printk("获取MEM类型资源失败\n");
return -EFAULT;
}
printk("获取MEM类型资源成功\n");
//获取中断类型的资源
irqno=platform_get_irq(dev, 0);
if(res == NULL)
{
printk("获取中断类型资源失败\n");
return irqno;
}
printk("获取中断类型资源成功\n");
//字符设备驱动注册
major=register_chrdev(0,"mychrdev",&fops);
if(major<0)
{
printk("字符设备驱动注册失败\n");
return major;
}
printk("字符设备驱动注册成功:major=%d\n",major);
//向上提交目录
cls=class_create(THIS_MODULE,"mychrdev");
if(IS_ERR(cls))
{
printk("向上提交目录失败\n");
return -PTR_ERR(cls);
}
printk("向上提交目录成功\n");
//向上提交设备节点信息
int i;//向上提交三次设备节点信息
for(i=0;i<3;i++)
{
led_dev=device_create(cls,NULL,MKDEV(major,i),NULL,"myled%d",i);
if(IS_ERR(led_dev))
{
printk("向上提交设备节点失败\n");
return -PTR_ERR(led_dev);
}
}
printk("向上提交设备节点成功\n");
//解析LED1的gpio编号
gpiono[0]=gpiod_get_from_of_node(dev->dev.of_node, "led1-gpio", 0, GPIOD_OUT_LOW, NULL);
if(gpiono[0] == NULL)
{
printk("解析LED1的gpio编号失败\n");
return -ENXIO;
}
printk("解析LED1的gpio编号成功\n");
//解析LED2的gpio编号
gpiono[1]=gpiod_get_from_of_node(dev->dev.of_node, "led2-gpio", 0, GPIOD_OUT_LOW, NULL);
if(gpiono[1] == NULL)
{
printk("解析LED2的gpio编号失败\n");
return -ENXIO;
}
printk("解析LED2的gpio编号成功\n");
//解析LED3的gpio编号
gpiono[2]=gpiod_get_from_of_node(dev->dev.of_node, "led3-gpio", 0, GPIOD_OUT_LOW, NULL);
if(gpiono[2] == NULL)
{
printk("解析LED3的gpio编号失败\n");
return -ENXIO;
}
printk("解析LED3的gpio编号成功\n");
return 0;
}
//封装remove函数,用于驱动和设备卸载时执行
int pdrv_remove(struct platform_device *dev)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
int i;
for(i=0; i<3; i++)
{
gpiod_put(gpiono[i]);
}
//注销字符设备驱动
unregister_chrdev(major,"myled");
return 0;
}
//构建用于匹配的设备树表
struct of_device_id device_table[]={
{ .compatible="hqyj,myplatform",},
{},
};
//分配驱动对象并初始化
struct platform_driver pdrv={
.probe=pdrv_probe,
.remove=pdrv_remove,
.driver={
.name="bbbbb" ,
.of_match_table=device_table,
},
};
//一键注册宏
module_platform_driver(pdrv);
MODULE_LICENSE("GPL");
应用程序:
#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>
//构建功能码
#define LED_ON _IOW('1',1,int)
#define LED_OFF _IOW('1',0,int)
int main(int argc, char const *argv[])
{
int a,b;
int fd=open("/dev/myled0", O_RDWR);
if(fd < 0)
{
printf("打开设备文件失败\n");
return -1;
}
while(1)
{
printf("想要控制哪盏灯\n");
printf("1(LED1) 2(LED2) 3(LED3)\n");
printf("请输入>");
scanf("%d", &a);
printf("请输入要实现的功能\n");
printf("0(关灯) 1(开灯)\n");
printf("请输入>");
scanf(" %d", &b);
getchar();
switch (b)
{
case 0:
ioctl(fd, LED_OFF, a);
break;
case 1:
ioctl(fd, LED_ON, a);
break;
}
}
close(fd);
return 0;
}