基于gpio子系统编写LED灯的驱动,编写应用程序测试:
head.h:
#ifndef __HEAD_H__
#define __HEAD_H__
typedef struct{
unsigned int MODER;
unsigned int OTYPER;
unsigned int OSPEEDR;
unsigned int PUPDR;
unsigned int IDR;
unsigned int ODR;
}gpio_t;
#define LED1 1
#define LED2 2
#define LED3 3
//构建LED开关的功能码,不添加ioctl第三个参数
#define LED_ON _IOW('l',1,int)
#define LED_OFF _IOW('l',0,int)
#endif
led.c:
#include <linux/init.h>
#include <linux/module.h>
#include<linux/fs.h>
#include<linux/io.h>
#include<linux/device.h>
#include<linux/uaccess.h>
#include<linux/slab.h>
#include<linux/of.h>
#include<linux/of_gpio.h>
#include<linux/gpio.h>
#include"head.h"
/*myled{
led1-gpio=<&gpioe 10 0>; //10表示使用的gpioe第几个引脚 0表示gpio默认属性
led2-gpio=<&gpiof 10 0>;
led3-gpio=<&gpioe 8 0>;
} */
struct cdev *cdev;
char kbuf[128]={0};
unsigned int major=0;
unsigned int minor=0;
dev_t devno;
module_param(major,uint,0664); //方便在命令行传递major值
struct class *cls;
struct device *dev;
struct device_node *done; //指向解析成功的设备树节点信息
unsigned int gpiono; //保存解析成功的GPIO编号
int mycdev_open(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *lof)
{
int ret;
if(size>sizeof(kbuf));
size=sizeof(kbuf);
ret=copy_to_user(ubuf,kbuf,size);
if(ret)
{
printk("copy_to_user err\n");
return -EIO;
}
return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{
int ret;
if(size>sizeof(kbuf))
size=sizeof(kbuf);
//从用户拷贝
ret=copy_from_user(kbuf,ubuf,size);
if(ret)
{
printk("copy_from_user err\n");
return -EIO;
}
return 0;
}
long mycdev_ioctl(struct file *file,unsigned int cmd,unsigned long arg)
{
int ret,led;
switch(cmd)
{
case LED_ON:
ret = copy_from_user(&led,(void *)arg, sizeof(int));
if(ret)
{
printk("copy error\n");
return -EIO;
}
switch(led)
{
case LED1:
//点亮led1
gpio_set_value(gpiono1,1);
break;
case LED2:
gpio_set_value(gpiono2,1);
break;
case LED3:
gpio_set_value(gpiono3,1);
break;
}
break;
case LED_OFF:
ret = copy_from_user(&led, (void *)arg, sizeof(int));
if(ret)
{
printk("copy error\n");
return -EIO;
}
switch(led)
{
case LED1:
//点亮led1
gpio_set_value(gpiono1,0);
break;
case LED2:
gpio_set_value(gpiono2,0);
break;
case LED3:
gpio_set_value(gpiono3,0);
break;
}
break;
default:
printk("cmd error\n");
break;
}
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,
.ioctl=mycdev_ioctl,
.close=mycdev_close,
};
static int __init mycdev_init(void)
{
int ret;
cdev = cdev_alloc(); //申请一个字符设备驱动对象空间
if(cdev==NULL)
{
printk("字符设备驱动对象申请空间失败\n");
ret = -EFAULT;
goto out1;
}
printk("申请对象空间成功\n");
cdev_init(cdev, &fops); //字符设备驱动对象初始化
if(major>0) //静态指定设备号
{
ret=register_chrdev_region(MKDEV(major,minor),3,"myled");
if(ret)
{
printk("静态申请设备号失败\n");
goto out2;
}
}
else if(major==0) //动态申请设备号
{
ret=alloc_chrdev_region(&devno,minor,3,"myled");
if(ret)
{
printk("动态申请设备号失败\n");
goto out2;
}
major=MAJOR(devno); //获取主设备号
minor=MINOR(devno); //获取次设备号
}
printk("申请设备号成功\n");
//注册字符设备驱动对象
ret=cdev_add(cdev,MKDEV(major,minor),3);
if(ret)
{
printk("注册字符设备驱动对象失败\n");
goto out3;
}
printk("注册字符设备驱动对象成功\n");
//向上提交目录信息
cls=class_create(THIS_MODULE,"myled");
if(IS_ERR(cls))
{
printk("向上提交目录失败\n");
ret=-PTR_ERR(cls);
goto out4;
}
printk("向上提交目录成功\n");
//向上提交设备节点信息
int i;
for(i=0;i<3;i++)
{
dev=device_create(cls,NULL,MKDEV(major,i),NULL,"myled%d",i);
if(IS_ERR(dev))
{
printk("向上提交设备节点信息失败\n");
ret=-PTR_ERR(dev);
goto out5;
}
}
printk("向上提交设备信息成功\n");
//解析led的设备树节点
donde=of_find_node_by_path("/myled");
if(dnode==NULL)
{
printk("解析设备树节点失败\n");
return -ENXIO;
}
printk("解析GPIO信息成功\n");
//获取GPIO编号
gpiono1=of_get_named_gpio(dnode,"led1-gpio",0);
gpiono2=of_get_named_gpio(dnode,"led2-gpio",0);
gpiono3=of_get_named_gpio(dnode,"led3-gpio",0);
if(gpiono1<0)
{
printk("GPIO编号解析失败\n");
}
printk("gpio1编号解析成功\n");
if(gpiono2<0)
{
printk("GPIO编号解析失败\n");
}
printk("gpio2编号解析成功\n");
if(gpiono3<0)
{
printk("GPIO编号解析失败\n");
}
printk("gpio3编号解析成功\n");
//申请GPIO编号
int ret=gpio_request(gpiono1,NULL);
if(ret)
{
printk("gpiono1 error\n");
return 0;
}
ret=gpio_request(gpiono2,NULL);
if(ret)
{
printk("gpiono2 error\n");
return 0;
}
ret=gpio_request(gpiono3,NULL);
if(ret)
{
printk("gpiono3 error\n");
return 0;
}
//设置输出模式
gpio_direction_output(gpiono1,0);
gpio_direction_output(gpiono2,0);
gpio_direction_output(gpiono3,0);
return 0;
out5:
//释放前一次提交成功的设备信息
for(--i;i>=0;i--)
{
device_destroy(cls,MKDEV(major,i));
}
class_destroy(cls);//释放目录
out4:
cdev_del(cdev);
out3:
unregister_chrdev_region(MKDEV(major,minor),3);
out2:
kfree(cdev);
out1:
return ret;
}
static void __exit mycdev_exit(void)
{
//释放节点信息
int i;
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);
//释放设备号
gpio_free(gpiono1);
gpio_free(gpiono2);
gpio_free(gpiono3);
}
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 <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include "head.h"
int main(int argc, const char *argv[])
{
cahr buf[128] = {};
int led;
int fd1,fd2,fd3;
fd1=open("/dev/my_led1",O_RDWR);
if(fd1<0)
{
printf("open led1 error\n");
return -1;
}
printf("open my_led1 success\n");
fd2=open("/dev/my_led2",O_RDWR);
if(fd1<0)
{
printf("open led2 error\n");
return -1;
}
printf("open my_led2 success\n");
fd3=open("/dev/my_led3",O_RDWR);
if(fd1<0)
{
printf("open led3 error\n");
return -1;
}
printf("open my_led3 success\n");
while(1)
{
printf("请输入灯亮灭 0(LED_OFF) 1(LED_ON)\n");
fgers(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
if(buf[0] == '1')
{
printf("请输入要控制哪个灯亮 1(LED1) 2(LED2) 3(LED3)\n");
scanf(" %d",&led);
if(led == 1)
{
ioctl(fd1,LED_ON,&led);
}
if(led == 2)
{
ioctl(fd2,LED_ON,&led);
}
if(led == 3)
{
ioctl(fd3,LED_ON,&led);
}
}
else
{
printf("请输入要控制哪个灯亮 1(LED1) 2(LED2) 3(LED3)\n");
scanf(" %d",&led);
if(led == 1)
{
ioctl(fd1,LED_OFF,&led);
}
if(led == 2)
{
ioctl(fd2,LED_OFF,&led);
}
if(led == 3)
{
ioctl(fd3,LED_OFF,&led);
}
}
}
close(fd1);
close(fd2);
close(fd3);
return 0;
}
Makefile:
modname?=mychrdev
arch?=arm
ifeq ($(arch),arm)
KERNELDIR:=/home/ubuntu/FSMP1A/linux-stm32mp-5.10.61-stm32mp-r2-r0/linux-5.10.61 #用于编译生成ARM架构的模块
else
KERNELDIR :=/lib/modules/$(shell uname -r )/build #用于生产x86架构的模块
endif
#定义变量保存模块化编译的文件路径
PWD:=$(shell pwd)
all:
#M=$(PWD)指定模块化编译的路径
make -C $(KERNELDIR) M=$(PWD) modules
clean: #编译清除
make -C $(KERNELDIR) M=$(PWD) clean
obj-m:=$(modname).o #指定将demo.o独立连接生产内核模块文件