.h
#ifndef __HEAD_H__
#define __HEAD_H__
// 封装开灯关灯功能码 PE10 PF10 PE8
#define LED_ON _IOW('l', 1, int)
#define LED_OFF _IOW('l', 0, int)
#endif
应用程序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include "head.h"
int main(int argc, char const *argv[])
{
int led_a, led_b;
char buf[128] = {0};
int fd_led = open("/dev/mycdev", O_RDWR);
if (fd_led < 0)
{
printf("打开设备文件失败\n");
exit(-1);
}
while (1)
{
printf("请输入控制的灯:1(LED1) 2(LED2) 3(LED3)>>> ");
scanf("%d", &led_a);
printf("请输入字符:0(关灯) 1(开灯)>>> ");
scanf("%d", &led_b);
switch (led_b)
{
case 1:
ioctl(fd_led, LED_ON, &led_a); // 开灯
break;
case 0:
ioctl(fd_led, LED_OFF, &led_a); // 关灯
break;
default:
puts("输入错误请重新输入\n");
break;
}
}
close(fd_led);
return 0;
}
驱动程序
#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/gpio.h>
#include "head.h"
struct resource *res;
unsigned int irqno;
struct gpio_desc *gpiono1;
struct gpio_desc *gpiono2;
struct gpio_desc *gpiono3;
struct device_node *dnode;
int major;
struct class *cls;
struct device *dev;
int mycdev_open(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
int which;
int ret = copy_from_user(&which, (void *)arg, 4);
if (ret)
{
printk("copy_from_user failed\n");
return ret;
}
switch (cmd)
{
case LED_ON: // 开灯
switch (which)
{
case 1:
// 开灯
gpiod_set_value(gpiono1, 1);
break;
case 2:
gpiod_set_value(gpiono2, 1);
break;
case 3:
gpiod_set_value(gpiono3, 1);
break;
}
break;
case LED_OFF: // 关灯
switch (which)
{
case 1:
// 灭灯
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;
}
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,
.unlocked_ioctl = mycdev_ioctl,
.release = mycdev_close,
};
// 定义相关成员函数
// probe函数用于匹配设备成功后执行
int pdrv_probe(struct platform_device *pdev) // 当和设备匹配成功之后执行probe
{
// 字符设备驱动注册
major = register_chrdev(0, "mycdev", &fops);
if (major < 0)
{
printk("字符设备驱动注册失败\n");
return major;
}
printk("字符设备驱动注册成功:major=%d\n", major);
// 向上提交目录
cls = class_create(THIS_MODULE, "mycdev");
if (IS_ERR(cls))
{
printk("向上提交目录失败\n");
return -PTR_ERR(cls);
}
printk("向上提交目录成功\n");
// 向上提交设备节点信息
dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "mycdev");
if (IS_ERR(dev))
{
printk("向上提交设备节点失败\n");
return -PTR_ERR(dev);
}
printk("向上提交设备节点成功\n");
gpiono1 = gpiod_get_from_of_node(pdev->dev.of_node, "led1", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono1))
{
printk("GPIO信息解析失败\n");
return -PTR_ERR(gpiono1);
}
printk("GPIO信息解析成功\n");
gpiono2 = gpiod_get_from_of_node(pdev->dev.of_node, "led2", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono2))
{
printk("GPIO信息解析失败\n");
return -PTR_ERR(gpiono2);
}
printk("GPIO信息解析成功\n");
gpiono3 = gpiod_get_from_of_node(pdev->dev.of_node, "led3", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono3))
{
printk("GPIO信息解析失败\n");
return -PTR_ERR(gpiono3);
}
printk("GPIO信息解析成功\n");
return 0;
}
// remove用于和设备分离时执行
int pdrv_remove(struct platform_device *pdev) // 当设备和驱动分离时执行remove
{
// 释放GPIO编号
gpiod_put(gpiono1);
gpiod_put(gpiono2);
gpiod_put(gpiono3);
// 销毁设备节点信息
device_destroy(cls, MKDEV(major, 0));
// 销毁目录空间
class_destroy(cls);
// 注销字符设备驱动
unregister_chrdev(major, "mycdev");
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 = "aaaaa",
.of_match_table = oftable, // 指定设备树匹配表的首地址
},
};
// 一键注册宏,代替入口出口
module_platform_driver(pdrv);
MODULE_LICENSE("GPL");