代码:
homework.h
#ifndef __HOMEWORK_H__
#define __HOMEWORK_H__
#define LED1_ON _IO('L',(0x1<<1))
#define LED1_OFF _IO('L',(0x1<<2))
#define LED2_ON _IO('L',(0x1<<3))
#define LED2_OFF _IO('L',(0x1<<4))
#define LED3_ON _IO('L',(0x1<<5))
#define LED3_OFF _IO('L',(0x1<<6))
#endif
部分设备树
myleds{
led1=<&gpioe 10 0>;
led2=<&gpiof 10 0>;
led3=<&gpioe 8 0>;
};
homework.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/device/class.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/gpio/consumer.h>
#include "homework.h"
#define COUNT (3) // 设备文件数目
struct gpio_desc *gpionum[COUNT]; // gpio结构体
int myopen(struct inode *inode, struct file *file){printk("%s.\n", __func__);return 0;}
int myclose(struct inode *inode, struct file *file){printk("%s.\n", __func__);return 0;}
long myioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
printk("%s.\n", __func__);
switch(MINOR(file->f_inode->i_rdev))
{
case 0:
switch(cmd)
{
case LED1_ON:
printk("LED1_ON.\n");
gpiod_set_value(gpionum[0], 1);
break;
case LED1_OFF:
printk("LED1_OFF.\n");
gpiod_set_value(gpionum[0], 0);
break;
default:
printk("传入cmd错误.\n");
break;
}
break;
case 1:
switch(cmd)
{
case LED2_ON:
printk("LED2_ON.\n");
gpiod_set_value(gpionum[1], 1);
break;
case LED2_OFF:
printk("LED2_OFF.\n");
gpiod_set_value(gpionum[1], 0);
break;
default:
printk("传入cmd错误.\n");
break;
}
break;
case 2:
switch(cmd)
{
case LED3_ON:
printk("LED3_ON.\n");
gpiod_set_value(gpionum[2], 1);
break;
case LED3_OFF:
printk("LED3_OFF.\n");
gpiod_set_value(gpionum[2], 0);
break;
default:
printk("传入cmd错误.\n");
break;
}
break;
default:
printk("传入minor错误.\n");
break;
}
return 0;
}
#define CNAME "myleds" // 驱动名
const struct file_operations fop = {
.open = myopen,
.release = myclose,
.unlocked_ioctl = myioctl,
}; // 文件操作方法结构体
int major = -1; // 主设备号
#define DIRNAME "myleds" // 目录名
struct class *cls = NULL; // 目录信息结构体
char *fname[COUNT] = {"myled1", "myled2", "myled3"}; // 设备文件名
struct device *dev = NULL; // 设备文件信息结构体
#define TNODE "myleds" // 设备树结点名
struct device_node *dnode = NULL; // 解析到的结点
char *tname[COUNT] = {"led1", "led2", "led3"}; // 结点的键名
static int __init myled_init(void)
{
int ret = -1; //返回值
int i = 0; // 循环量
// 1. 注册字符设备驱动[cdev_alloc >>> cdev_init >>> alloc/register_chrdev_region >>> cdev_add]
if((major = register_chrdev(0, CNAME, &fop)) < 0)
{
printk("注册字符设备驱动[失败].\n");
ret = major;
goto ERR1;
}
printk("注册字符设备驱动[成功].\n");
// 2. 向上提交目录信息
if(IS_ERR(cls = class_create(THIS_MODULE, DIRNAME)))
{
printk("向上提交目录信息[失败].\n");
ret = PTR_ERR(cls);
goto ERR2;
}
printk("向上提交目录信息[成功].\n");
// 3. 向上提交设备文件信息
for(i = 0; i < COUNT; i++)
{
if(IS_ERR(dev = device_create(cls, NULL, MKDEV(major, i), NULL, fname[i])))
{
printk("向上提交设备文件信息[失败].\n");
goto ERR3;
return PTR_ERR(dev);
}
}
printk("向上提交设备文件信息[成功].\n");
// 1. 解析设备树结点
if(NULL == (dnode = of_find_node_by_name(NULL, TNODE)))
{
printk("解析设备树结点[失败].\n");
return -ENODATA;
}
printk("解析设备树结点[成功].\n");
// 2. 获取gpio编号
for(i = 0; i < COUNT; i++)
{
if(IS_ERR(gpionum[i] = gpiod_get_from_of_node(dnode, tname[i], 0, GPIOD_OUT_LOW, NULL)))
{
printk("获取gpio编号[失败].\n");
ret = -PTR_ERR(gpionum[i]);
goto ERR4;
}
}
printk("获取gpio编号[成功].\n");
// 3. 测试
for(i = 0; i < COUNT; i++)
{
gpiod_set_value(gpionum[i], 1);
}
printk("测试完成.\n");
return 0;
ERR4:
for(--i; i >= 0; i--)
{
gpiod_put(gpionum[i]);
}
i = COUNT;
ERR3:
for(--i; i >= 0; i--)
{
device_destroy(cls, MKDEV(major, i));
}
class_destroy(cls);
ERR2:
unregister_chrdev(major, CNAME);
ERR1:
return ret;
}
static void __exit myled_exit(void)
{
int i = 0;// 循环量
// 灭灯+释放编号
for(i = 0; i < COUNT; i++)
{
gpiod_set_value(gpionum[i], 0);
gpiod_put(gpionum[i]);
}
// 3. 移除设备文件信息
for(i = 0; i < COUNT; i++)
{
device_destroy(cls, MKDEV(major, i));
}
// 2. 移除目录信息
class_destroy(cls);
// 1. 注销字符设备驱动
unregister_chrdev(major, CNAME);
return ;
}
module_init(myled_init);
module_exit(myled_exit);
MODULE_LICENSE("GPL");
Makefile
KERNDIR:=/home/hades/linux-5.10.61
obj-m:=homework.o
PWD:=$(shell pwd)
all:
make -C $(KERNDIR) M=$(PWD) modules
clean:
make -C $(KERNDIR) M=$(PWD) clean
testForLed.c
#include <fcntl.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <unistd.h>
#include "homework.h"
int main(int argc, const char *argv[])
{
int fds[3] = {0};
char *devname[3] = {"/dev/myled1", "/dev/myled2", "/dev/myled3"};
for(int i = 0; i < 3; i++)
{
if(-1 == (fds[i] = open(devname[i], O_RDWR)))
{
perror("open");
return -1;
}
}
printf("文件打开完毕.\n");
char cmd = 0;
while(1)
{
puts("MENU");
puts("0. QUIT");
puts("1. LED1_ON");
puts("2. LED2_ON");
puts("3. LED3_ON");
puts("4. LED1_OFF");
puts("5. LED2_OFF");
puts("6. LED3_OFF");
scanf("%c", &cmd);
printf("您输入的是:%c\n", cmd);
switch(cmd)
{
case '0':
for(int i = 0; i < 3; i++)
{
close(fds[i]);
}
return 0;
case '1':
ioctl(fds[0], LED1_ON);
break;
case '2':
ioctl(fds[1], LED2_ON);
break;
case '3':
ioctl(fds[2], LED3_ON);
break;
case '4':
ioctl(fds[0], LED1_OFF);
break;
case '5':
ioctl(fds[1], LED2_OFF);
break;
case '6':
ioctl(fds[2], LED3_OFF);
break;
default:
printf("请输入正确的cmd.\n");
break;
}
getchar();
puts("键入回车继续-");
while(getchar() != 10);
system("clear");
}
return 0;
}
运行截图: