1.在内核模块中启用定时器,定时1s,让led1 一秒亮、一秒灭
2.基于gpio子系统完成LED灯驱动的注册,应用程序测试
1.mychrdev_timer.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/device.h>
#include <linux/slab.h>
#include<linux/cdev.h>
#include <linux/uaccess.h>
#include<linux/of.h>
#include<linux/gpio.h>
#include<linux/of_gpio.h>
#include <linux/timer.h>
struct cdev *cdev;
unsigned int major=500;//主设备号的起始值
unsigned int minor=0;//次设备号的起始值
dev_t devno;//设备号
struct class *cls;//用于保存目录信息
struct device *dev;//用于保存设备节点信息
struct device_node *dnode;//设备树节点结构体
struct gpio_desc *gpiono;//gpio属性结构体
//分配一个定时器对象
struct timer_list mytimer;
//定义操作方法结构体变量并赋值
struct file_operations fops =
{
};
void mytimer_func(struct timer_list *timer)
{
printk("定时器处理函数执行\n");
if(gpiod_get_value(gpiono)==0)
{
//设置管脚输出高电平,亮灯
gpiod_set_value(gpiono,1);
}else{
//设置管脚输出低电平,灭灯;
gpiod_set_value(gpiono,0);
}
//再次启动定时器
mod_timer(timer,jiffies+HZ);
}
static int __init mycdev_init(void)
{
int ret;
//1.分配字符设备驱动对象空间 cdev_alloc
cdev=cdev_alloc();
if(cdev==NULL)
{
printk("申请字符设备驱动对象空间失败\n");
ret=-EFAULT;
goto out1;
}
printk("字符设备驱动对象申请成功\n");
//2.字符设备驱动对象部分初始化 cdev_init
cdev_init(cdev,&fops);
//3.申请设备号 register_chrdev_region/alloc_chrdev_region
if(major>0)//静态申请设备号
{
ret=register_chrdev_region(MKDEV(major,minor),1,"mychrdev");
if(ret)
{
printk("静态指定设备号失败\n");
goto out2;
}
}
else//动态申请设备号
{
ret=alloc_chrdev_region(&devno,minor,1,"mychrdev");
if(ret)
{
printk("动态申请设备号失败\n");
goto out2;
}
major=MAJOR(devno);//根据设备号得到主设备号
minor=MINOR(devno);//根据设备号得到次设备号
}
printk("申请设备号成功\n");
//4.注册字符设备驱动对象 cdev_add()
ret=cdev_add(cdev,MKDEV(major,minor),1);
if(ret)
{
printk("注册字符设备驱动对象失败\n");
goto out3;
}
printk("注册字符设备驱动对象成功\n");
//5.向上提交目录
cls=class_create(THIS_MODULE,"mychrdev");
if(IS_ERR(cls))
{
printk("向上提交目录失败\n");
ret=-PTR_ERR(cls);
goto out4;
}
printk("向上提交目录成功\n");
//6.向上提交设备节点
dev=device_create(cls,NULL,MKDEV(major,minor),NULL,"mychrdev");
if(IS_ERR(dev))
{
printk("向上提交节点信息失败\n");
ret=-PTR_ERR(dev);
goto out5;
}
printk("向上提交设备节点信息成功\n");
//解析led灯的设备树节点
dnode=of_find_node_by_path("/myleds");
if(dnode==NULL)
{
printk("解析设备树节点失败\n");
return -ENXIO;
}
printk("解析设备树节点成功\n");
//根据设备树节点解析led1 gpio结构体并向内核注册
gpiono=gpiod_get_from_of_node(dnode,"led1",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(gpiono))
{
printk("申请gpio失败\n");
return -PTR_ERR(gpiono);
}
mytimer.expires=jiffies+HZ;
timer_setup(&mytimer,mytimer_func,0);
add_timer(&mytimer);
return 0;
out5:
//销毁上面提交的设备信息
device_destroy(cls,MKDEV(major,minor));
class_destroy(cls);
out4:
cdev_del(cdev);
out3:
unregister_chrdev_region(MKDEV(major,minor),1);
out2:
kfree(cdev);
out1:
return ret;
}
static void __exit mycdev_exit(void)
{
//灭灯
gpiod_set_value(gpiono,0);
//注销gpio信息
gpiod_put(gpiono);
//1.销毁设备节点信息
device_destroy(cls,MKDEV(major,minor));
//2.销毁目录信息
class_destroy(cls);
//3.注销对象 cdev_del()
cdev_del(cdev);
//4.释放设备号 unregister_chrdev_region()
unregister_chrdev_region(MKDEV(major,minor),1);
//5.释放对象空间 kfree()
kfree(cdev);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
2 test.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
int main(int argc, char const *argv[])
{
int fd1,fd2,fd3;
char a,b;
char buf[10]={0};
while(1)
{
printf("请输入两个字符\n");
printf("请选择要操作的灯1(LED1) 2(LED2) 3(LED3) 4(EXIT)\n");
scanf("%c",&a);
getchar();
if(a == '4')
return 0;
switch(a)
{
case '1':
fd1 = open("/dev/myled1",O_RDWR);
if (fd1 < 0)
{
printf("打开设备文件失败\n");
exit(-1);
}
break;
case '2':
fd2 = open("/dev/myled2",O_RDWR);
if (fd2 < 0)
{
printf("打开设备文件失败\n");
exit(-1);
}
break;
case '3':
fd3 = open("/dev/myled3",O_RDWR);
if (fd3 < 0)
{
printf("打开设备文件失败\n");
exit(-1);
}
break;
}
//从终端读取
printf("第二个字符:0(关灯) 1(开灯)\n");
scanf("%c",&b);
getchar();
buf[0]=a;
buf[1]=b;
switch(a)
{
case '1':
write(fd1,buf,sizeof(buf));
break;
case '2':
write(fd2,buf,sizeof(buf));
break;
case '3':
write(fd3,buf,sizeof(buf));
break;
}
memset(buf,0,sizeof(buf));
}
close(fd1);
close(fd2);
close(fd3);
return 0;
}
led.c
#include <linux/init.h>
#include <linux/module.h>
#include<linux/fs.h>
#include<linux/device.h>
#include<linux/cdev.h>
#include<linux/slab.h>
#include <linux/io.h>
#include<linux/of.h>
#include<linux/gpio.h>
#include<linux/of_gpio.h>
struct cdev *cdev;//字符设备驱动对象空间首地址
unsigned int major=200;//主设备号
unsigned int minor=0;//次设备号的起始值
dev_t devno;//设备号变量
char kbuf[128]={0};
struct class *cls;//存放向上提交目录的返回值
struct device *dev;//存放向上提交设备节点信息结构体
struct device_node *dnode;
struct gpio_desc *gpiono1;
struct gpio_desc *gpiono2;
struct gpio_desc *gpiono3;
int mycdev_open(struct inode *inode, struct file *file)
{
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)
{
int ret;
if(sizeof(kbuf)<size)//判断用户空间的需求是否能被驱动满足,满足不了就给能给的最好的
size=sizeof(kbuf);
ret=copy_from_user(kbuf,ubuf,size);
if(ret)
{
printk("copy_from_user filed\n");
return -EIO;
}
switch(kbuf[0]){
case '1'://LED1
if(kbuf[1]=='0')//关灯
gpiod_set_value(gpiono1,0);
else//开灯
gpiod_set_value(gpiono1,1);
break;
case '2'://LED2
if(kbuf[1]=='0')//关灯
gpiod_set_value(gpiono2,0);
else//开灯
gpiod_set_value(gpiono2,1);
break;
case '3'://LED3
if(kbuf[1]=='0')//关灯
gpiod_set_value(gpiono3,0);
else//开灯
gpiod_set_value(gpiono3,1);
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,
.write = mycdev_write,
.release = mycdev_close,
};
static int __init mycdev_init(void)
{
int ret,i;
//1.分配字符设备驱动对象空间 cdev_alloc
cdev = cdev_alloc();
if (!cdev) {
printk(KERN_ALERT "cdev_alloc failed\n");
return -ENOMEM;
}
printk("字符设备驱动对象空间申请成功\n");
//2.字符设备驱动对象部分初始化 cdev_init
cdev_init(cdev, &fops);
//3.申请设备号 register_chrdev_region/alloc_chrdev_region
if(major>0)//静态申请设备号
{
ret=register_chrdev_region(MKDEV(major,minor),3,"myled");
if(ret)
{
printk("静态指定设备号失败\n");
goto out2;
}
}
else//动态申请设备号
{
ret=alloc_chrdev_region(&devno,minor,3,"myled");
if(ret)
{
printk("动态申请设备号失败\n");
goto out2;
}
major=MAJOR(devno);//根据设备号得到主设备号
minor=MINOR(devno);//根据设备号得到次设备号
}
printk("申请设备号成功\n");
//4.注册字符设备驱动对象 cdev_add()
ret=cdev_add(cdev,MKDEV(major,minor),3);
if(ret)
{
printk("注册字符设备驱动对象失败\n");
goto out3;
}
printk("注册字符设备驱动对象成功\n");
//5.向上提交目录
cls=class_create(THIS_MODULE,"myled");
if(IS_ERR(cls))
{
printk("向上提交目录失败\n");
ret=-PTR_ERR(cls);
goto out4;
}
printk("向上提交目录成功\n");
//6.向上提交设备节点
for(i=0;i<3;i++)
{
dev=device_create(cls,NULL,MKDEV(major,i),NULL,"myled%d",i+1);
if(IS_ERR(dev))
{
printk("向上提交节点信息失败\n");
ret=-PTR_ERR(dev);
goto out5;
}
}
printk("向上提交设备节点信息成功\n");
//解析led灯的设备树节点
dnode=of_find_node_by_path("/myleds");
if(dnode==NULL)
{
printk("解析设备树节点失败\n");
return -ENXIO;
}
printk("解析设备树节点成功\n");
//根据设备树节点解析led1 gpio结构体并向内核注册
gpiono1=gpiod_get_from_of_node(dnode,"led1",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(gpiono1))
{
printk("申请gpio失败\n");
return -PTR_ERR(gpiono1);
}
//根据设备树节点解析led2 gpio结构体并向内核注册
gpiono2=gpiod_get_from_of_node(dnode,"led2",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(gpiono2))
{
printk("申请gpio失败\n");
return -PTR_ERR(gpiono2);
}
//根据设备树节点解析led3 gpio结构体并向内核注册
gpiono3=gpiod_get_from_of_node(dnode,"led3",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(gpiono3))
{
printk("申请gpio失败\n");
return -PTR_ERR(gpiono3);
}
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),1);
out2:
kfree(cdev);
out1:
return ret;
}
static void __exit mycdev_exit(void)
{
//灭灯,注销gpio信息
int i;
gpiod_set_value(gpiono1,0);
gpiod_put(gpiono1);
gpiod_set_value(gpiono2,0);
gpiod_put(gpiono2);
gpiod_set_value(gpiono3,0);
gpiod_put(gpiono3);
//1.销毁设备信息 device_destroy
for(i=0;i<3;i++)
{
device_destroy(cls,MKDEV(major,i));
}
//2.销毁目录 class_destroy
class_destroy(cls);
//3.注销对象 cdev_del()
cdev_del(cdev);
//4.释放设备号 unregister_chrdev_region()
unregister_chrdev_region(MKDEV(major,minor),3);
//5.释放对象空间 kfree()
kfree(cdev);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");