编写驱动,应用层程序,在应用层通过ioctl控制LED灯流水,当按键KEY1按下,让风扇转动
mydev.h:
#ifndef __MYLED_H__
#define __MYLED_H__
#define LED_ON _IOW('a',1,int)
#define LED_OFF _IOW('a',0,int)
enum{
LED1,
LED2,
LED3,
};
#endif
mydev.c:
#include <linux/init.h>
#include <linux/module.h>
#include<linux/fs.h>
#include<linux/uaccess.h>
#include<linux/io.h>
#include<linux/device.h>
#include<linux/poll.h>
#include <linux/of.h>
#include <linux/timer.h>
#include <linux/of_gpio.h>
#include<linux/interrupt.h>
#include<linux/of_irq.h>
#include "mydev.h"
int major;
int irqno;
struct class *cls;
struct device *dev;
char kbuf[128]={0};
struct device_node *dnode;
struct device_node *dnode1;
struct gpio_desc* gpiono1;
struct gpio_desc* gpiono2;
struct gpio_desc* gpiono3;
struct gpio_desc* gpiono4;
irqreturn_t irq1_handler(int irqno, void *arg)
{
gpiod_set_value(gpiono4,!gpiod_get_value(gpiono4));
return IRQ_HANDLED;
}
int myled_open(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
//read
ssize_t myled_read(struct file *file, char *ubuf, size_t size, loff_t *off)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
//write
ssize_t myled_write(struct file *file, const char *ubuf, size_t size, loff_t *off)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
//close
int myled_close(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
long myled_ioctl(struct file *file,unsigned int cmd,unsigned long args)
{
//1.判断cmd switch(cmd)
//2.判断操作哪盏灯进行点亮 copy_from_user
int whitch;
int ret;
switch(cmd)
{
case LED_ON:
ret = copy_from_user(&whitch,(void*)args,sizeof(int));
if(ret)
{
printk("copy from user is error\n");
return -EIO;
}
switch (whitch)
{
case LED1:
gpiod_set_value(gpiono1,1);
break;
case LED2:
gpiod_set_value(gpiono2,1);
break;
case LED3:
gpiod_set_value(gpiono3,1);
break;
}
break;
case LED_OFF:
ret = copy_from_user(&whitch,(void*)args,sizeof(int));
if(ret)
{
printk("copy from user is error\n");
return -EIO;
}
switch (whitch)
{
case LED1:
gpiod_set_value(gpiono1,0);
break;
case LED2:
gpiod_set_value(gpiono2,0);
break;
case LED3:
gpiod_set_value(gpiono3,0);
break;
}
break;
}
return 0;
}
const struct file_operations fops = {
.open = myled_open,
.read = myled_read,
.write = myled_write,
.unlocked_ioctl = myled_ioctl,
.release = myled_close,
};
static int __init mycdev_init(void)
{
int i;
//1.注册字符设备驱动
major = register_chrdev(0,"mycdev",&fops);
if(major < 0) //2.判断返回值
{
printk("register chrdev is error\n");
}
//3.打印主设备号
printk("register chrdev major=%d\n",major);
//4.向上提交目录
cls=class_create(THIS_MODULE,"mycdev");
if(IS_ERR(cls))//指针指向预留空间,函数调用失败
{
printk("向上提交目录失败\n");
return PTR_ERR(cls);
}
printk("向上提交目录成功\n");
//向上提交设备节点
for(i=0;i<3;i++)
{
dev=device_create(cls,NULL,MKDEV(major,i),NULL,"mycdev%d",i);
if(IS_ERR(dev))//指针指向预留空间,函数调用失败
{
printk("向上提交目录失败\n");
return PTR_ERR(dev);
}
}
printk("设备节点向上提交成功\n");
//解析设备树节点
dnode=of_find_node_by_name(NULL,"myleds");
if(dnode==NULL)
{
printk("解析设备树节点失败\n");
return -EIO;
}
printk("解析设备树节点成功\n");
//根据设备树节点解析gpio编号
gpiono1=gpiod_get_from_of_node(dnode,"led1",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(gpiono1))
{
printk("解析gpio编号失败\n");
return -EIO;
}
gpiono2=gpiod_get_from_of_node(dnode,"led2",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(gpiono2))
{
printk("解析gpio编号失败\n");
return -EIO;
}
gpiono3=gpiod_get_from_of_node(dnode,"led3",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(gpiono3))
{
printk("解析gpio编号失败\n");
return -EIO;
}
gpiono4=gpiod_get_from_of_node(dnode,"fan",0,GPIOD_OUT_HIGH,NULL);
if(gpiono4<0)
{
printk("解析gpio编号失败\n");
return -EIO;
}
//解析设备节点
dnode1=of_find_node_by_name(NULL,"myirqs");
if(dnode1==NULL)
{
printk("解析设备树节点失败\n");
return -EIO;
}
printk("解析设备树节点成功\n");
//根据设备树节点解析出软中断号
irqno=irq_of_parse_and_map(dnode1,0);
if(!irqno)
{
printk("获取软中断号失败\n");
return -ENXIO;
}
printk("解析设备树获取软中断号成功\n");
//注册中断
request_irq(irqno,irq1_handler,IRQF_TRIGGER_FALLING,"myirq1",NULL);
return 0;
}
static void __exit mycdev_exit(void)
{
//字符设备驱动的注销
unregister_chrdev(major,"mycdev");
gpiod_set_value(gpiono1,0);
gpiod_put(gpiono1);
gpiod_set_value(gpiono2,0);
gpiod_put(gpiono2);
gpiod_set_value(gpiono3,0);
gpiod_put(gpiono3);
gpiod_set_value(gpiono4,0);
gpiod_put(gpiono4);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
test.c:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/ioctl.h>
#include "mydev.h"
int main(int argc,const char * argv[])
{
char buf[128] = {0};
int whitch;
int fd = -1;
fd = open("/dev/mycdev0",O_RDWR);
if(fd == -1)
{
perror("open is error\n");
return -1;
}
while(1)
{
whitch = LED1;
ioctl(fd,LED_ON,&whitch);
sleep(1);
ioctl(fd,LED_OFF,&whitch);
sleep(1);
whitch = LED2;
ioctl(fd,LED_ON,&whitch);
sleep(1);
ioctl(fd,LED_OFF,&whitch);
sleep(1);
whitch = LED3;
ioctl(fd,LED_ON,&whitch);
sleep(1);
ioctl(fd,LED_OFF,&whitch);
}
close(fd);
return 0;
}
Makefile:
ARCH ?=x86
modname ?= mydev
ifeq ($(ARCH),arm)
#定义一个变量,存放linux内核源码目录,arm架构
KERNEDIR:=/home/ubuntu/linux-5.10.61
#x86架构
else
KERNEDIR:=/lib/modules/$(shell uname -r)/build
#定义一个变量,开启一个终端,执行pwd命令
endif
PWD:=$(shell pwd)
all:
@#-C:跳转到内核顶层目录下,读取内核顶层目录下的Makefile文件
@#在内核源码顶层目录下执行:make M=$(shell pwd) modules
@#M=$(shell pwd):回到当前目录下,只编译当前目录下的文件
@#make modules:采用模块化方式进行编译
make -C $(KERNEDIR) M=$(shell pwd) modules
clean:
make -C $(KERNEDIR) M=$(shell pwd) clean
#指定模块化方式编译的文件
obj-m:=$(modname).o