基于gpio子系统完成led驱动程序编写,在应用层程序中控制led的亮灭
应用层程序
#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 "myled.h"
int main(int argc,const char * argv[])
{
int fd=open("/dev/myled0",O_RDWR);
if(fd<0)
{
perror("open");
return -1;
}
int led,status;
while(1)
{
printf("请选择灯:1(led1),2(led2),3(led3)>>");
scanf("%d",&led);
printf("请选择灯的亮灭:1(亮),0(灭)>>");
scanf("%d",&status);
if(status==1)
{
ioctl(fd,LED_ON,&led);
}
else if(status==0)
{
ioctl(fd,LED_OFF,&led);
}
}
close(fd);
return 0;
}
驱动程序
#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/poll.h>
#include <linux/of.h>
#include <linux/cdev.h>
#include <linux/slab.h>
#include <linux/of_gpio.h>
#include "myled.h"
struct device_node *node;
struct gpio_desc *led1;
struct gpio_desc *led2;
struct gpio_desc *led3;
struct cdev *cdev;
dev_t devnum;
int res,i,major;
struct class *cls;
struct device *dev;
//定义自己的open,read,write,ioctl,release函数
int myopen(struct inode *inode,struct file *file)
{
return 0;
}
ssize_t myread(struct file *file,char *user,size_t size,loff_t *loff)
{
return 0;
}
ssize_t mywrite(struct file *file,const char *user,size_t size,loff_t *loff)
{
return 0;
}
long myioctl(struct file *file,unsigned int cmd,unsigned long arg)
{
int led;
//接收用户空间传递的数据
copy_from_user(&led,(void *)arg,sizeof(int));
//判断接收到的数据,执行相应操作
switch (led)
{
case 1:
if(cmd==LED_ON){
gpiod_set_value(led1,1);
}else{
gpiod_set_value(led1,0);
}
break;
case 2:
if(cmd==LED_ON){
gpiod_set_value(led2,1);
}else{
gpiod_set_value(led2,0);
}
break;
case 3:
if(cmd==LED_ON){
gpiod_set_value(led3,1);
}else{
gpiod_set_value(led3,0);
}
break;
default:
break;
}
return 0;
}
int myrelease(struct inode *inode,struct file *file)
{
return 0;
}
//定义操作方法结构体并初始化
struct file_operations fops=
{
.open=myopen,
.read=myread,
.write=mywrite,
.unlocked_ioctl=myioctl,
.release=myrelease,
};
static int __init mycdev_init(void)
{
//根据路径解析设备树节点
node=of_find_node_by_path("/myleds");
if(node==NULL)
{
printk("解析失败\n");
return -1;
}
/*******新版gpio子系统*******/
//获取gpio编号,注册,设置为输出模式,并初始化为低电平
led1=gpiod_get_from_of_node(node,"led1",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(led1))
{
printk("gpio相关操作设置失败\n");
return PTR_ERR(led1);
}
led2=gpiod_get_from_of_node(node,"led2",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(led2))
{
printk("gpio相关操作设置失败\n");
return PTR_ERR(led2);
}
led3=gpiod_get_from_of_node(node,"led3",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(led3))
{
printk("gpio相关操作设置失败\n");
return PTR_ERR(led3);
}
/*************分布注册字符设备驱动**************/
//给字符设备驱动对象申请空间
cdev=cdev_alloc();
if(cdev==NULL)
{
printk("空间申请失败\n");
res=-ENOMEM;
goto ERR1;
}
//初始化字符设备驱动对象
cdev_init(cdev,&fops);
//动态申请设备号
res=alloc_chrdev_region(&devnum,0,3,"myled");
if(res)
{
printk("设备号申请失败\n");
res=-1;
goto ERR2;
}
major=MAJOR(devnum);
//将字符设备驱动对象注册到内核
res=cdev_add(cdev,devnum,3);
if(res)
{
printk("注册失败\n");
res=-1;
goto ERR3;
}
/****************创建设备节点********************/
//向上提交目录信息
cls=class_create(THIS_MODULE,"myled");
if(cls==NULL)
{
printk("目录信息提交失败\n");
res=-1;
goto ERR4;
}
//向上提交设备节点信息
for(i=0;i<3;i++)
{
dev=device_create(cls,NULL,MKDEV(major,i),NULL,"myled%d",i);
if(IS_ERR(dev))
{
printk("设备节点信息提交失败\n");
res=PTR_ERR(dev);
goto ERR5;
}
}
return 0;
ERR5:
for(--i;i>=0;i--)
{
device_destroy(cls,devnum);
}
class_destroy(cls);
ERR4:
cdev_del(cdev);
ERR3:
unregister_chrdev_region(devnum,3);
ERR2:
kfree(cdev);
ERR1:
return res;
}
static void __exit mycdev_exit(void)
{
//释放gpio资源
gpiod_set_value(led1,0);
gpiod_put(led1);
gpiod_set_value(led2,0);
gpiod_put(led2);
gpiod_set_value(led3,0);
gpiod_put(led3);
//销毁设备节点
for (i = 0; i < 3; i++)
{
device_destroy(cls,MKDEV(major,i));
}
//销毁目录
class_destroy(cls);
//注销字符设备驱动
cdev_del(cdev);
//释放设备号
unregister_chrdev_region(devnum,3);
//释放内存空间
kfree(cdev);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
头文件
#ifndef __MYLED_H__
#define __MYLED_H__
//led亮灭的功能码
#define LED_ON _IOW('c',1,int)
#define LED_OFF _IOW('c',0,int)
#endif