源代码
#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/of_irq.h>
#include<linux/interrupt.h>
#include "myled.h"
/*myplatform{
compatible="hqyj,platform";
reg=<0x12345678,0x14>;
interrupt-parent=<&gpiof>;//引入中断的上一级节点
interrupts=<9 0>;//连接管脚,0表示缺省状态
led1 =<&gpioe 10 0>;
};*/
struct resource *res;
int irqno;
struct gpio_desc *gpiono1;
struct gpio_desc *gpiono2;
struct gpio_desc *gpiono3;
struct gpio_desc *fan;
#define CNAME "myled"
struct class *cls;
struct device *dev;
int major;
//分配一个driver对象并且初始化
//匹配设备成功后执行
irqreturn_t irq1_handler(int irqno, void *arg)
{
printk("key1 interrupt\n");
gpiod_set_value(fan,!gpiod_get_value(fan));
return IRQ_HANDLED;
}
long myled_ioctl(struct file *file, unsigned int cmd, unsigned long args)
{
int ret,whitch;
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 = {
.unlocked_ioctl = myled_ioctl,
};
int pdrv_probe(struct platform_device *pdev)
{
//1.注册字符设备驱动
major = register_chrdev(0,CNAME,&fops);
if(major < 0) //2.判断返回值
{
printk("register chrdev is error\n");
}
//3.打印主设备号
printk("register chrdev major=%d\n",major);
//向上层提交目录信息
cls=class_create(THIS_MODULE,"myled");
if(IS_ERR(cls))
{
printk("向上提交目录失败\n");
PTR_ERR(cls);
}
printk("向上层提交目录信息成功\n");
//向上层提交设备信息
dev=device_create(cls,NULL,MKDEV(major,0),NULL,"myled");
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;
}
printk("MEM_start_addr=%x,irqno=%d\n",res->start,irqno);
//根据设备信息解析出GPIO编号
gpiono1=gpiod_get_from_of_node(pdev->dev.of_node,"led1",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(gpiono1))
{
printk("申请gpio编号失败\n");
return PTR_ERR(gpiono1);
}
gpiono2=gpiod_get_from_of_node(pdev->dev.of_node,"led2",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(gpiono2))
{
printk("申请gpio编号2失败\n");
return PTR_ERR(gpiono2);
}
gpiono3=gpiod_get_from_of_node(pdev->dev.of_node,"led3",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(gpiono3))
{
printk("申请gpio编号3失败\n");
return PTR_ERR(gpiono3);
}
fan=gpiod_get_from_of_node(pdev->dev.of_node,"fan",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(fan))
{
printk("申请gpio编号4失败\n");
return PTR_ERR(fan);
}
//根据设备信息解析出软中断号
irqno=irq_of_parse_and_map(pdev->dev.of_node,0);
if(!irqno)
{
printk("获取软中断号失败\n");
return -ENXIO;
}
printk("解析设备树获取软中断号成功\n");
//注册中断
request_irq(irqno,irq1_handler,IRQF_TRIGGER_FALLING,"key1",NULL);
return 0;
}
//和设备分离时执行
int pdrv_remove(struct platform_device *pdev)
{
//关灯
gpiod_set_value(gpiono1,0);
//释放gpio编号
gpiod_put(gpiono1);
gpiod_set_value(gpiono2,0);
//释放gpio编号
gpiod_put(gpiono2);
gpiod_set_value(gpiono3,0);
//释放gpio编号
gpiod_put(gpiono3);
gpiod_set_value(fan,0);
//释放gpio编号
gpiod_put(fan);
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
构建设备树匹配的表。使用compatible匹配
struct of_device_id oftable[]={
{.compatible="hqyj,platform",},
{},
};//名字表的构建
MODULE_DEVICE_TABLE(of,oftable);
//定义驱动对象
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 "myled.h"
int main(int argc,const char * argv[])
{
char buf[128] = {0};
int fd = -1;
int whitch;
fd = open("/dev/myled",O_RDWR);
if(fd == -1)
{
perror("open is error\n");
return -1;
}
while(1)
{
printf("1\n");
whitch = LED1;
printf("2\n");
ioctl(fd,LED_ON,&whitch);
sleep(1);
ioctl(fd,LED_OFF,&whitch);
sleep(1);
printf("3\n");
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);
sleep(1);
}
close(fd);
return 0;
}
头文件
#ifndef __HEAD_H__
#define __HEAD_H__
//封装寄存器结体
typedef struct{
volatile unsigned int MODER;
volatile unsigned int OTYPER;
volatile unsigned int OSPEEDR;
volatile unsigned int PUPDR;
volatile unsigned int IDR;
volatile unsigned int ODR;
volatile unsigned int BSRR;
}gpio_t;
#define PHY_RCC_ADDR 0X50000A28
#define PHY_LED1_ADDR 0X50006000
#define PHY_LED2_ADDR 0X50007000
#define PHY_LED3_ADDR 0X50006000
#define LED_ON _IOW('a',1,int)//开灯
#define LED_OFF _IOW('a',0,int)//关灯
enum
{
LED1,
LED2,
LED3,
};
#endif
结果