自己编写驱动,应用层程序,在应用层通过ioctl控制LED灯流水,当按键KEY1按下,让风扇转动(使用platform总线模型)
头文件
#ifndef __MYLED_H__
#define __MYLED_H__
#define LED_1 _IOW('a',1,int)
#define LED_2 _IOW('a',2,int)
#define LED_3 _IOW('a',3,int)
#endif
内核源文件
#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/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/wait.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include "myled.h"
struct resource *res;
int irqno;
struct cdev *cdev;
dev_t devno;
unsigned int minor=0;
#if 1
unsigned int major =0; //动态申请主设备号
#else
unsigned int major=500; //静态指定主设备号
#endif
struct class *cls;
struct device *dev;
struct gpio_desc *gpiono[4];
char *LED[4]={"led1","led2","led3","fans"};
//ioctl接收应用层数据
long myled_ioctl (struct file *file, unsigned int cmd, unsigned long args)
{
//1.判断cmd switch(cmd)
//2.判断操作哪盏灯进行点亮
switch(cmd){
case LED_1:
gpiod_set_value(gpiono[0],!gpiod_get_value(gpiono[0]));
break;
case LED_2:
gpiod_set_value(gpiono[1],!gpiod_get_value(gpiono[1]));
break;
case LED_3:
gpiod_set_value(gpiono[2],!gpiod_get_value(gpiono[2]));
break;
}
return 0;
}
//中断处理函数
irqreturn_t irq1_handler(int irqno,void *arg){
printk("irq succeed\n");
gpiod_set_value(gpiono[3],!gpiod_get_value(gpiono[3]));
return IRQ_HANDLED;
}
//操作数
const struct file_operations fops={
.unlocked_ioctl = myled_ioctl,
};
//分配对象并且初始化
//probe函数
int pdrv_probe(struct platform_device *pdev){
int ret,i;
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
//分配对象
cdev = cdev_alloc();
if(cdev == NULL){
printk("分配对象空间失败\n");
ret=-ENOMEM;
goto ERR1;
}
printk("分配对象空间成功\n");
//对象初始化
cdev_init(cdev,&fops);
//申请设备号
if(major==0) //动态申请
{
ret=alloc_chrdev_region(&devno,minor,3,"mydev");
if(ret){
printk("静态申请设备号失败\n");
goto ERR2;
}
major=MAJOR(devno); //获取主设备号
minor=MINOR(devno); //获取次设备号
}else if(major>0){ //静态指定主设备号
ret=register_chrdev_region(MKDEV(major,minor),3,"mydev");
if(ret){
printk("静态申请设备号失败\n");
goto ERR2;
}
}
//注册对象
ret=cdev_add(cdev,MKDEV(major,minor),3);
if(ret){
printk("驱动对象注册进内核失败\n");
goto ERR3;
}
printk("驱动对象注册进内核成功\n");
//向上提交目录
cls=class_create(THIS_MODULE,"mydev");
if(IS_ERR(cls)){
printk("向上提交目录失败\n");
goto ERR4;
}
printk("向上提交目录成功\n");
//向上提交设备节点
for(i=0;i<3;i++){
dev=device_create(cls,NULL,MKDEV(major,i),NULL,"mydev%d",i);
if(IS_ERR(dev)){
printk("向上提交节点信息失败\n");
goto ERR5;
}
}
printk("向上提交节点信息成功\n");
printk("register chrdev major=%d\n",major);
//获取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;
}
printk("mem类型资源数值为:%x\n",res->start);
printk("中断类型的资源数值为%d\n",irqno);
//根据设备树结点解析gpio编号
for(i=0;i<4;i++){
gpiono[i]=gpiod_get_from_of_node(pdev->dev.of_node,LED[i],0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(gpiono[i])){
printk("解析gpio[%d]编号失败\n",i);
return PTR_ERR(gpiono[i]);
}
printk("解析gpio[%d]编号成功\n",i);
}
//注册中断
request_irq(irqno,irq1_handler,IRQF_TRIGGER_FALLING,"keyirq",NULL);
return 0;
ERR5:
//将前面提交成功的设备信息销毁
for(--i;i>=0;i--){
device_destroy(cls,MKDEV(major,i));
}
//销毁目录
class_destroy(cls);
ERR4:
cdev_del(cdev);
ERR3:
unregister_chrdev_region(MKDEV(major,minor),3);
ERR2:
kfree(cdev);
ERR1:
return ret;
}
//remove函数
int pdrv_remove(struct platform_device *pdev){
int i;
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
for(i=0;i<4;i++){
gpiod_set_value(gpiono[i],0);
gpiod_put(gpiono[i]);
}
//注销中断
free_irq(irqno,NULL);
for(i=0;i<3;i++){
device_destroy(cls,MKDEV(major,i));
}
class_destroy(cls);
cdev_del(cdev);
unregister_chrdev_region(MKDEV(major,minor),3);
kfree(cdev);
return 0;
}
struct of_device_id oftable[]={
{.compatible="hqyj,hello2"},
{},
};
struct platform_driver pdrv={
.probe=pdrv_probe,
.remove=pdrv_remove,
.driver={
.name="aaaaa",
.of_match_table=oftable,
},
};
//一键注册宏
module_platform_driver(pdrv);
MODULE_LICENSE("GPL");
应用层源文件
#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 "myled.h"
int main(int argc,const char * argv[])
{
int fd = -1;
fd = open("/dev/mydev0",O_RDWR);
if(fd == -1)
{
perror("open is error\n");
return -1;
}
while(1)
{
ioctl(fd,LED_1);
sleep(1);
ioctl(fd,LED_1);
sleep(1);
ioctl(fd,LED_2);
sleep(1);
ioctl(fd,LED_2);
sleep(1);
ioctl(fd,LED_3);
sleep(1);
ioctl(fd,LED_3);
sleep(1);
}
close(fd);
return 0;
}
Makefile
#定义一个存放架构变量
ARCH ?= X86
#定义一个存放文件名变量
modname ?= demo
ifeq ($(ARCH),arm)
#定义一个变量,存放linux内核源码目录,生成ARM架构
KERNEDIR:=/home/ubuntu/linux-5.10.61
else
#定义一个变量,存放ubuntu的linux内核源码目录,生成X86架构
KERNEDIR:=/lib/modules/$(shell uname -r)/build
endif
#定义一个变量,开启一个终端,执行pwd命令
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