驱动代码
#include <linux/init.h>
#include <linux/module.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/platform_device.h>
#include <linux/mod_devicetable.h>
#define LED_ON _IOW('l', 1, int)
#define LED_OFF _IOW('l', 0, int)
int major;
char kbuf[128] = {0};
struct class *cls;
struct device *device;
struct gpio_desc *gpiono1;
struct gpio_desc *gpiono2;
struct gpio_desc *gpiono3;
struct device_node *dnode;
struct resource *res;
unsigned int irqno;
///
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)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
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;
}
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_to_user filed\n");
return ret;
}
return 0;
}
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
switch (cmd)
{
case LED_ON:
switch (arg)
{
case 1:
gpiod_set_value(gpiono1, 1); // led1输出高电平
break;
case 2:
gpiod_set_value(gpiono2, 1); // led2输出高电平
break;
case 3:
gpiod_set_value(gpiono3, 1); // led2输出高电平
break;
}
break;
case LED_OFF:
switch (arg)
{
case 1:
gpiod_set_value(gpiono1, 0); // led1输出低电平
break;
case 2:
gpiod_set_value(gpiono2, 0); // led2输出低电平
break;
case 3:
gpiod_set_value(gpiono3, 0); // led3输出高电平
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,
};
///
// 封装probe函数,当设备和驱动匹配成功之后执行
int pdrv_probe(struct platform_device *dev)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
// 字符设备驱动注册
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++)
{
device = device_create(cls, NULL, MKDEV(major, i), NULL, "myled%d", i);
if (IS_ERR(device))
{
printk("向上提交设备节点失败\n");
return -PTR_ERR(device);
}
}
printk("向上提交设备节点成功\n");
// 解析LED灯设备树节点
dnode = of_find_node_by_path("/leds");
if (dnode == NULL)
{
printk("解析设备树节点失败\n");
return -ENXIO;
}
printk("解析设备树节点成功\n");
// 解析LED1的gpio编号
gpiono1 = gpiod_get_from_of_node(dnode, "led1-gpio", 0, GPIOD_OUT_LOW, NULL);
if (gpiono1 == NULL)
{
printk("解析gpio1编号失败\n");
return -ENXIO;
}
printk("解析gpio1编号成功\n");
// 解析LED2的gpio编号
gpiono2 = gpiod_get_from_of_node(dnode, "led2-gpio", 0, GPIOD_OUT_LOW, NULL);
if (gpiono2 == NULL)
{
printk("解析gpio2编号失败\n");
return -ENXIO;
}
printk("解析gpio2编号成功\n");
// 解析LED3的gpio编号
gpiono3 = gpiod_get_from_of_node(dnode, "led3-gpio", 0, GPIOD_OUT_LOW, NULL);
if (gpiono3 == NULL)
{
printk("解析gpio3编号失败\n");
return -ENXIO;
}
printk("解析gpio3编号成功\n");
return 0;
}
// 封装remove函数,用于驱动和设备卸载时执行
int pdrv_remove(struct platform_device *dev)
{
// 释放gpio编号
gpiod_put(gpiono1);
gpiod_put(gpiono2);
gpiod_put(gpiono3);
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
// 构建用于匹配的设备树表
struct of_device_id oftable[] = {
{
.compatible = "hqyj,myplatform",
},
{/* end node */}, // 防止数组越界
};
// 分配驱动对象并初始化
struct platform_driver pdrv = {
.probe = pdrv_probe,
.remove = pdrv_remove,
.driver = {
.name = "bbbbb",
.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 <fcntl.h>
#include<unistd.h>
#include<string.h>
#include<sys/ioctl.h>
#include "myled.h"
//创建功能码
#define LED_ON _IOW('l',1,int)
#define LED_OFF _IOW('l',0,int)
int main(int argc, char const *argv[])
{
int a,b;
int fd=open("/dev/myled0",O_RDWR);
if(fd<0)
{
printf("打开设备文件失败\n");
exit(-1);
}
while(1)
{
//从终端读取
printf("请输入要实现的功能\n");
printf("0(关灯) 1(开灯)\n");
printf("请输入>");
scanf("%d",&a);
printf("请输入要控制的灯\n");
printf("1(LED1) 2(LED2) 3(LED3)\n");
printf("请输入>");
scanf("%d",&b);
switch(a)
{
case 1:
ioctl(fd,LED_ON,b);
break;
case 0:
ioctl(fd,LED_OFF,b);
break;
}
}
close(fd);
return 0;
}
头文件:
#ifndef __HEAD_H__
#define __HEAD_H__
typedef struct{
unsigned int MODER;
unsigned int OTYPER;
unsigned int OSPEEDR;
unsigned int PUPDR;
unsigned int IDR;
unsigned int ODR;
}gpio_t;
#define PHY_LED1_ADDR 0X50006000
#define PHY_LED2_ADDR 0X50007000
#define PHY_LED3_ADDR 0X50006000
#define PHY_RCC_ADDR 0X50000A28
//构建功能码
#define LED_ON _IO('l',1)
#define LED_OFF _IO('l',0)
#endif
效果图: