驱动代码
#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/mod_devicetable.h>
#include <linux/fs.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
struct resource *res;
unsigned int irqno;
struct cdev *cdev;
int major;
struct class *cls;
struct device *dev;
struct gpio_desc *gpiono1;
struct gpio_desc *gpiono2;
struct gpio_desc *gpiono3;
int mycdev_open(struct inode *inode, struct file *file)
{
return 0;
}
int mycdev_release(struct inode *inode, struct file *file)
{
return 0;
}
long mycdev__ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
switch (cmd)
{
case _IOW('l', 1, int): // 开灯
switch (arg)
{
case 1: // LED1
gpiod_set_value(gpiono1,1);
break;
case 2:
gpiod_set_value(gpiono2,1);
break;
case 3:
gpiod_set_value(gpiono3,1);
break;
}
break;
case _IOW('l', 0, int): // 关灯
switch (arg)
{
case 1: // LED1
gpiod_set_value(gpiono1,0);
break;
case 2:
gpiod_set_value(gpiono2,0);
break;
case 3:
gpiod_set_value(gpiono3,0);
break;
}
break;
}
return 0;
return 0;
}
// 操作方法结构体指针
struct file_operations fops = {
.open = mycdev_open,
.unlocked_ioctl = mycdev__ioctl,
.release = mycdev_release};
int pdrv_probe(struct platform_device *pdev)
{
major = register_chrdev(0, "mychrdev", &fops);
if (major < 0)
{
printk("字符设备驱动注册失败\n");
return 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++)
{
dev = device_create(cls, NULL, MKDEV(major, i), NULL, "myled%d", i);
if (IS_ERR(dev))
{
printk("向上提交设备节点失败\n");
return -PTR_ERR(dev);
}
}
printk("向上提交设备节点成功\n");
gpiono1 = gpiod_get_from_of_node(pdev->dev.of_node, "led1-gpio", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono1))
{
printk("解析GPIO信息失败\n");
return -PTR_ERR(gpiono1);
}
gpiono2 = gpiod_get_from_of_node(pdev->dev.of_node, "led2-gpio", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono2))
{
printk("解析GPIO信息失败\n");
return -PTR_ERR(gpiono2);
}
gpiono3 = gpiod_get_from_of_node(pdev->dev.of_node, "led3-gpio", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono3))
{
printk("解析GPIO信息失败\n");
return -PTR_ERR(gpiono3);
}
return 0;
}
int pdrv_remove(struct platform_device *pdev)
{
// 销毁设备节点信息
int i;
for (i = 0; i < 3; i++)
{
device_destroy(cls, MKDEV(major, i));
}
// 销毁目录
class_destroy(cls);
// 注销字符设备驱动
unregister_chrdev(major, "mychrdev");
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
// 构建设备树匹配表
struct of_device_id oftable[] = {
{.compatible = "hqyj,myled"},
{},
};
// 1.分配驱动信息对象
struct platform_driver pdrv = {
.probe = pdrv_probe,
.remove = pdrv_remove,
.driver = {
.name = "ccc",
.of_match_table = oftable,
},
};
module_platform_driver(pdrv);
MODULE_LICENSE("GPL");
应用程序
#include<stdlib.h>
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include<unistd.h>
#include<string.h>
int main(int argc, char const *argv[])
{
char buf[128]={0};
int a,b;
int fd=open("/dev/myled0",O_RDWR);
if(fd<0)
{
printf("打开设备文件失败\n");
exit(-1);
}
while(1)
{
//从终端读取
printf("请输入要实现的功能 ");
printf("0(关灯) 1(开灯)\n");
printf("请输入>");
scanf("%d",&a);
printf("请选择要控制的灯:1(LED1)2(LED2) 3(LED3)\n");
printf("请输入>");
scanf("%d",&b);
switch(a)
{
case 1:
ioctl(fd,_IOW('l', 1,int),b);
break;
case 0:
ioctl(fd,_IOW('l', 0,int),b);
break;
}
}
close(fd);
return 0;
}