使用platform总线编写驱动,应用层程序,在应用层通过ioctl控制LED灯流水,当按键KEY1按下,让风扇转动
head.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/of.h>
#include <linux/of_gpio.h>
#include<linux/interrupt.h>
#include<linux/of_irq.h>
#include <linux/platform_device.h>
#include <linux/mod_devicetable.h>
#include "mydev.h"
struct resource *res;
int irqno;
int major;
struct class *cls;
struct device *dev;
int i;
char kbuf[128]={0};
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;
}
//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,
.unlocked_ioctl = myled_ioctl,
.release = myled_close,
};
//分配对象并且初始化
//probe函数
int pdrv_probe(struct platform_device *pdev)
{
//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");
//获取mem类型设备资源
res=platform_get_resource(pdev,IORESOURCE_MEM,0);
if(res==NULL)
{
printk("获取设备资源失败\n");
return -ENODATA;
}
//获取中断资源
irqno=platform_get_irq(pdev,0);
if(irqno<0)
{
printk("获取中断资源失败\n");
return -ENODATA;
}
//注册中断
request_irq(irqno,irq1_handler,IRQF_TRIGGER_FALLING,"myirq1",NULL);
gpiono1=gpiod_get_from_of_node(pdev->dev.of_node,"led1",0, GPIOD_OUT_LOW,NULL);
if(IS_ERR(gpiono1))
{
printk("解析gpio编号失败\n");
return -EIO;
}
gpiono2=gpiod_get_from_of_node(pdev->dev.of_node,"led2",0, GPIOD_OUT_LOW,NULL);
if(IS_ERR(gpiono2))
{
printk("解析gpio编号失败\n");
return -EIO;
}
gpiono3=gpiod_get_from_of_node(pdev->dev.of_node,"led3",0, GPIOD_OUT_LOW,NULL);
if(IS_ERR(gpiono3))
{
printk("解析gpio编号失败\n");
return -EIO;
}
gpiono4=gpiod_get_from_of_node(pdev->dev.of_node,"fan",0, GPIOD_OUT_HIGH,NULL);
if(gpiono4<0)
{
printk("解析gpio编号失败\n");
return -EIO;
}
return 0;
}
//remove函数
int pdrv_remove(struct platform_device *pdev)
{
unregister_chrdev(major,"mycdev");
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
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);
return 0;
}
//构建名字表
struct of_device_id oftable[]=
{
{.compatible="hqyj,platform"},
{},
};
struct platform_driver pdrv={
.probe=pdrv_probe,
.remove=pdrv_remove,
.driver={
.name="aaaaa",
.of_match_table=oftable,//设置设备树匹配
},
};
//一件注册宏
module_platform_driver(pdrv);
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
运行效果: