head.h
#ifndef __HEAD_H__
#define __HEAD_H__
//封装GPIO组寄存器结构体
typedef struct{
unsigned int MODER;
unsigned int OTYPER;
unsigned int OSPEEDR;
unsigned int PUPDR;
unsigned int IDR;
unsigned int ODR;
}gpio_t;
#define PHY_GPIOE_ADDR 0X50006000
#define PHY_GPIOF_ADDR 0X50007000
#define PHY_RCCAHB4_ADDR 0X50000A28 //使能GPIO章节
#define LED_ON _IOW('l',1,int)
#define LED_OFF _IOW('l',0,int)
#endif
test.c
#include<stdlib.h>
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<unistd.h>
#include<string.h>
#include <sys/ioctl.h>
#include"head.h"
int main(int argc,char const *argv)
{
char buf[128]={0};
int a,b;
//通过open函数打开字符驱动文件得到对应文件描述符后,用户层调用open,该字符设备驱动则也会调用对应的.open成员函数
int fd=open("/dev/myleds",O_RDWR);
if(fd<0)
{
printf("打开设备文件失败\n");
exit(-1);
}
while(1)
{
printf("请输入要操控的设备:1(Led1灯) 2(Led2灯) 3(Led3灯)\n");
scanf("%d",&a);
printf("请输入: 1(亮、响) 0(灭/不响)\n");
scanf("%d",&b);
switch (a)
{
case 1:
if(b==1)
ioctl(fd,LED_ON,&a);
else if(b==0)
ioctl(fd,LED_OFF,&a);
break;
case 2:
if(b==1)
ioctl(fd,LED_ON,&a);
else if(b==0)
ioctl(fd,LED_OFF,&a);
break;
case 3:
if(b==1)
ioctl(fd,LED_ON,&a);
else if(b==0)
ioctl(fd,LED_OFF,&a);
break;
default:
printf("输入有误\n");
return 0;
}
}
close(fd);
return 0;
}
pdrv.c
#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/poll.h>
#include <linux/slab.h>
#include <linux/cdev.h>
#include "head.h"
struct resource *res;
unsigned int irqno;
struct gpio_desc *gpiono[3];
//存储获得的设备号
unsigned int major=0;
unsigned int minor=0;
//存储最终设备号
dev_t devno;
//驱动设备对象指针
struct cdev *cdev;
//指向申请的向上提交目录和设备文件信息
struct class *cls;
struct device *dev;
//编写设备驱动中的mycdev_ioctl给操作方法结构体变量赋值
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
int ret2;
int which;
//注意:!!!!!想使用arg要先强转赋值给另一个变量
ret2=copy_from_user(&which,(void *)arg,sizeof(int));
switch(which)
{
case 1:
switch(cmd)
{
case LED_ON:
gpiod_set_value(gpiono[0],1);//开灯,第二个参数填1,高电平,0低电平
break;
case LED_OFF:
gpiod_set_value(gpiono[0],0);//开灯,第二个参数填1,高电平,0低电平
break;
}
break;
case 2:
switch(cmd)
{
case LED_ON:
gpiod_set_value(gpiono[1],1);//开灯,第二个参数填1,高电平,0低电平
break;
case LED_OFF:
gpiod_set_value(gpiono[1],0);//开灯,第二个参数填1,高电平,0低电平
break;
}
break;
case 3:
switch(cmd)
{
case LED_ON:
gpiod_set_value(gpiono[2],1);//开灯,第二个参数填1,高电平,0低电平
break;
case LED_OFF:
gpiod_set_value(gpiono[2],0);//开灯,第二个参数填1,高电平,0低电平
break;
}
break;
}
return 0;
}
int mycdev_open(struct inode *inode, struct file *file)
{
return 0;
}
ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *iof)
{
return 0;
}
//struct file是每一个打开了的文件就会有的结构体
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *iof)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
struct file_operations fops={
.open=mycdev_open,
.read=mycdev_read,
.write=mycdev_write,
.unlocked_ioctl=mycdev_ioctl,
.release=mycdev_close,
};
//定义相关成员函数
//probe函数用于匹配设备成功后进行执行,也可完成初始化相关工作
int pdrv_probe(struct platform_device *pdev)//当和设备匹配成功之后执行probe
{
int ret;
//1.分配字符设备驱动对象空间
cdev=cdev_alloc();
if(cdev==NULL)
{
printk("分配字符设备驱动对象失败\n");
ret=-EFAULT;
goto LOOP1;
}
printk("分配对象空间成功\n");
//2.字符驱动对象初始化
cdev_init(cdev,&fops);
//3.申请设备号
//静态指定设备号
if(major>0)
{
ret=register_chrdev_region(MKDEV(major,minor),1,"myleds");
if(ret)
{
printk("静态指定设备号失败\n");
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
goto LOOP2;
}
}
else if(major==0) //动态申请设备号
{
ret=alloc_chrdev_region(&devno,minor,1,"myleds");
if(ret)
{
printk("动态申请设备号失败\n");
goto LOOP2;
}
major=MAJOR(devno);
minor=MINOR(devno);
}
printk("申请设备号成功\n");
//4.添加字符设备驱动对象注册进内核
ret=cdev_add(cdev,MKDEV(major,minor),1);
if(ret)
{
printk("字符设备驱动对象注册失败\n");
goto LOOP3;
}
printk("添加字符设备驱动对象注册进内核成功\n");
//向上提交目录(给udev机制使用,从而能在/dev下创建设备文件)
cls=class_create(THIS_MODULE,"myleds");
if(IS_ERR(cls))
{
printk("向上提交目录失败\n");
return -PTR_ERR(cls);
goto LOOP4;
}
printk("向上提交目录成功\n");
//向上提交设备节点信息(给udev机制使用,从而能在/dev下创建设备文件)
dev=device_create(cls,NULL,MKDEV(major,0),NULL,"myleds");
if(IS_ERR(dev))
{
printk("向上提交设备节点失败\n");
ret= -PTR_ERR(dev);
goto LOOP5;
}
printk("向上提交设备节点成功\n");
//解析MEM类型的资源 此处用不上,用于留作记录笔记使用
res= platform_get_resource(pdev,IORESOURCE_MEM,0);
if(res==NULL)
{
printk("解析资源失败%d\n",__LINE__);
return -ENOMEM;
}
//解析中断类型的资源
irqno=platform_get_irq(pdev,0);
if(irqno<0)
{
printk("解析资源失败%d\n",__LINE__);
return -ENOMEM;
}
//解析GPIO编号并申请
int i;
for(i=0;i<3;i++)
{
gpiono[i]=gpiod_get_from_of_node(pdev->dev.of_node,"interrupts-extended",i,GPIOD_OUT_LOW,NULL);
if(IS_ERR(gpiono[i]))
{
printk("GPIO信息解析失败\n");
return -PTR_ERR(gpiono[i]);
}
printk("GPIO信息解析成功\n");
}
printk("mem:%x irq:%d\n",res->start,irqno);
return 0;
LOOP5:
//释放已经申请的设备节点信息
device_destroy(cls,MKDEV(major,0));
//释放目录空间
class_destroy(cls);
LOOP4:
//注销字符设备驱动对象
cdev_del(cdev);
LOOP3:
//释放设备号
unregister_chrdev_region(MKDEV(major,minor),1);
LOOP2:
kfree(cdev);
LOOP1:
return ret;
}
//remove用于和设备分离时进行执行
int pdrv_remove(struct platform_device *pdev)//当设备和驱动分离时执行remove
{
int i;
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
for(i=0;i<3;i++)
{
gpiod_put(gpiono[i]);
}
//销毁设备节点信息
device_destroy(cls,MKDEV(major,0));
//销毁目录空间
class_destroy(cls);
//注销字符设备驱动对象
cdev_del(cdev);
//释放设备号
unregister_chrdev_region(MKDEV(major,minor),1);
//释放对象空间
kfree(cdev);
return 0;
}
//在驱动中创建一个用于设备树匹配的表
struct of_device_id oftable[]={
{.compatible="hqyj,myplatform",},
{.compatible="hqyj,myplatform1",},
{.compatible="hqyj,myplatform2",},
{},
};
//分配对象并初始化s
struct platform_driver pdrv={
.probe=pdrv_probe,
.remove=pdrv_remove,
.driver={
.name="aaaa",
.of_match_table=oftable,//指定设备树匹配表的首地址
},
};
//一键注册宏,代替入口出口
module_platform_driver(pdrv);
MODULE_LICENSE("GPL");