驱动程序
#include <linux/init.h>
#include <linux/module.h>
#include<linux/of.h>
#include<linux/of_gpio.h>
#include<linux/gpio.h>
#include<linux/timer.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/device.h>
#include <linux/wait.h>
#include <linux/uaccess.h>
#include <linux/poll.h>
#include <linux/cdev.h>
#include <linux/slab.h>
struct device_node *dnode;
struct gpio_desc *gpiono;
struct gpio_desc *gpiono2;
struct gpio_desc *gpiono3;
struct timer_list mytimer;
struct cdev *cdev;
unsigned int major=0;
unsigned int minor=0;
dev_t devno;
struct class *cls;
struct device *dev;
char kbuf[128] = {0};
void mytimer_function(struct timer_list *timer)
{
printk("hello,world\n");
//再次启用定时器
mod_timer(timer,jiffies+HZ*5);
}
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__);
int ret=copy_from_user(kbuf,ubuf,size);
if(kbuf[0]=='1')
{
gpiod_set_value(gpiono,!gpiod_get_value(gpiono));
}
else if(kbuf[0]=='2')
{
gpiod_set_value(gpiono2,!gpiod_get_value(gpiono2));
}
else if(kbuf[0]=='3')
{
gpiod_set_value(gpiono3,!gpiod_get_value(gpiono3));
}
if(ret)
{
printk("error\n");
return -EIO;
}
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,
.release=mycdev_close,
};
static int __init mycdev_init(void)
{
//1.申请一个对象空间cdev_alloc
int ret;
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=alloc_chrdev_region(&devno,minor,3,"mychrdev");
if(ret)
{
printk("动态申请设备号失败\n");
goto out2;
}
major=MAJOR(devno);//根据设备号获取主设备号
minor=MINOR(devno);//根据设备号获取次设备号
}
else
{
ret=register_chrdev_region(MKDEV(major,minor),3,"mychrdev");
if(ret)
{
printk("静态指定设备号失败\n");
goto out2;
}
}
printk("设备号申请成功\n");
//4.注册驱动对象 cdev_add
ret=cdev_add(cdev,MKDEV(major,minor),3);
if(ret)
{
printk("注册字符设备驱动对象失败\n");
goto out3;
}
printk("注册字符设备驱动对象成功\n");
//5.向上提交目录 class_create
cls=class_create(THIS_MODULE,"mychrdev");
if(IS_ERR(cls))
{
printk("向上提交目录失败\n");
goto out4;
}
printk("向上提交目录成功\n");
//6.向上提交设备节点信息 device_create
int i;
for(i=0;i<3;i++)
{
dev=device_create(cls,NULL,MKDEV(major,i),NULL,"mycdev%d",i);
if(IS_ERR(dev))
{
printk("向上提交设备节点失败\n");
goto out5;
}
}
printk("向上提交设备节点信息成功\n");
//解析设备树节点信息
dnode=of_find_node_by_path("/myled");
if(dnode==NULL)
{
printk("解析设备树节点失败\n");
return -ENXIO;
}
//获取LED1 GPIO编号
//获取led1 gpio编号
gpiono=gpiod_get_from_of_node(dnode,"led1-gpio",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(gpiono))
{
printk("申请gpio信息失败\n");
return -PTR_ERR(gpiono);
}
gpiono2=gpiod_get_from_of_node(dnode,"led2-gpio",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(gpiono2))
{
printk("申请gpio信息失败\n");
return -PTR_ERR(gpiono2);
}
gpiono3=gpiod_get_from_of_node(dnode,"led3-gpio",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(gpiono3))
{
printk("申请gpio信息失败\n");
return -PTR_ERR(gpiono3);
}
//初始化定时器对象
timer_setup(&mytimer,mytimer_function,0);
mytimer.expires=jiffies+HZ*5;//定时1s;
//注册定时器
add_timer(&mytimer);
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)
{
del_timer(&mytimer);
int i;
for(i=0;i<3;i++)
{
device_destroy(cls,MKDEV(major,i));
}
//2.销毁目录
class_destroy(cls);
//3.注销字符设备驱动对象
cdev_del(cdev);
//4.释放设备号
unregister_chrdev_region(MKDEV(major,minor),3);
//5.释放申请到的字符设备驱动对象空间
kfree(cdev);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
应用程序
#include<stdlib.h>
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<unistd.h>
#include<string.h>
int main(int argc, char const *argv[])
{
char buf[128]={0};
int fd=open("/dev/mycdev0",O_RDWR);
if(fd<0)
{
printf("打开设备文件失败\n");
exit(-1);
}
while(1)
{
//从终端读取
printf("请输入两个字符\n");
printf("第一个字符:1(LED1) 2(LED2) 3(LED3)\n");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
//向设备文件中写
write(fd,buf,sizeof(buf));
}
close(fd);
return 0;
}
现象