基于GPIO子系统编写cdLED驱动,编写应用程序进行测试
设置定时器,5秒钟打印一次hello world
驱动程序
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "head.h"
int major; //字符设备驱动注册主设备号
struct class *cls; //用于向上提交目录
struct device *dev; //用于向上提交设备节点
//unsigned int gpiono;
struct gpio_desc *gpiono[3];
struct timer_list mytimer; //分配定时器对象
//封装操作方法
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)
{
printk("%s:%s:%d\n", __FILE__,__func__,__LINE__);
return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{
printk("%s:%s:%d\n", __FILE__,__func__,__LINE__);
return 0;
}
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
printk("%s:%s:%d\n", __FILE__,__func__,__LINE__);
int which;
int ret = copy_from_user(&which, (void*)arg,4);
if(ret)
{
printk("copy_from_user failed\n");
return -EIO;
}
switch(cmd)
{
case LED_ON:
switch(which)
{
case 1:
gpiod_set_value(gpiono[0],1);
break;
case 2:
gpiod_set_value(gpiono[1], 1);
break;
case 3:
gpiod_set_value(gpiono[2], 1);
break;
}
break;
case LED_OFF:
switch(which)
{
case 1:
gpiod_set_value(gpiono[0],0);
break;
case 2:
gpiod_set_value(gpiono[1], 0);
break;
case 3:
gpiod_set_value(gpiono[2], 0);
break;
}
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,
.unlocked_ioctl = mycdev_ioctl,
.release = mycdev_close,
};
//定时器处理函数
void mytimer_function(struct timer_list *timer)
{
printk("hello world\n");
//再次启用定时器
mod_timer(timer,jiffies+5*HZ);
}
static int __init mycdev_init(void)
{
//字符设备驱动注册
major = register_chrdev(0, "mycdev",&fops);
if(major < 0)
{
printk("字符设备驱动注册失败\n");
return major;
}
/****************************udev创建设备节点的原************************/
//向上提交目录
cls = class_create(THIS_MODULE, "mycdev");
if(IS_ERR(cls))
{
printk("向上提交目录失败\n");
return -PTR_ERR(cls);
}
//向上提交设备节点信息
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");
return -PTR_ERR(dev);
}
}
printk("向上提交设备节点成功\n");
/******************************************************************/
//解析设备树节点信息
dnode = of_find_node_by_path("/myled");
if(dnode == NULL)
{
printk("解析设备树节点失败\n");
return -ENXIO;
}
//新版GPIO子系统接口 在设备树节点中解析出GPIO对象,并向内核申请
for(i=0; i<3; i++)
{
gpiono[i] = gpiod_get_from_of_node(dnode, "led-gpios", i, GPIOD_OUT_LOW, NULL);
if(IS_ERR(gpiono[i]))
{
printk("申请gpio信息失败\n");
return -PTR_ERR(gpiono[i]);
}
}
//初始化定时器对象
timer_setup(&mytimer, mytimer_function, 0);
mytimer.expires=jiffies+5*HZ;
//注册定时器并启用
add_timer(&mytimer);
return 0;
}
static void __exit mycdev_exit(void)
{
//注销定时器
del_timer(&mytimer);
int i;
for(i=0; i<3; i++)
{
gpiod_set_value(gpiono[i], 0);
//释放gpio编号
gpiod_put(gpiono[i]);
//销毁设备节点信息
device_destroy(cls, MKDEV(major, i));
}
//销毁目录
class_destroy(cls);
//注销字符设备驱动
unregister_chrdev(major, "mycdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
应用程序
#include
#include
#include
#include
#include
#include
#include
#include
#include "head.h"
int main(int argc, char const *argv[])
{
int fd = open("/dev/myled0", O_RDWR);
if(fd < 0)
{
printf("打开设备文件失败\n");
exit(-1);
}1
while(1)
{
int a;
printf("请输入1(开灯)0(关灯)>>");
scanf("%d", &a);
int b;
printf("请输入 1(LED1) 2(LED2) 3(LED3)>>");
scanf("%d", &b);
switch(a)
{
case 1:
ioctl(fd, LED_ON,&b);
break;
case 0:
ioctl(fd, LED_OFF,&b);
break;
}
}
close(fd);
return 0;
}
head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#define LED_ON _IOW('l', 1, int)
#define LED_OFF _IOW('l',0, int)
#endif
运行效果