mycdev.c
#include <linux/init.h>
#include <linux/module.h>
#include<linux/fs.h>
#include<linux/device.h>
#include<linux/of.h>
#include<linux/of_gpio.h>
#include<linux/of_irq.h>
#include<linux/interrupt.h>
#include<linux/cdev.h>
#include<linux/uaccess.h>
#include<linux/slab.h>
#include"mycdev.h"
/*myleds{
led1=<&gpioe 10 0>;
led2=<&gpiof 10 0>;
led3=<&gpioe 8 0>;
};*/
/*myirqs{
compatible = "hqyj,irq";
interrupt-parent = <&gpiof>;
interrupts = <9 0>, <7 0>, <8 0>;
};*/
struct cdev *cdev;
dev_t devno;
int major;
struct class *cls;
struct device *dev;
struct device_node *dnode;
struct device_node *fs_dnode;
int gpiono1,gpiono2,gpiono3,gpiono4;
struct device_node *dnode_key;
int irqno;
//中断 处理函数
irqreturn_t irq1_handler(int irqno, void *arg)
{
gpio_set_value(gpiono4,!gpio_get_value(gpiono4));
return IRQ_HANDLED;
}
//定义操作方法结构体内的函数
//open
int mycdev_open(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
//read
ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *off)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
//write
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *off)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
long myled_ioctl(struct file *file,unsigned int cmd,unsigned long args)
{
int whitch;
int ret;
switch(cmd)
{
case LED_ON:
ret = copy_from_user(&whitch,(void*)args,sizeof(int));
if(ret)
{
printk("copy from user is error\n");
return -EIO;
}
switch(whitch)
{
case 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(&whitch,(void*)args,sizeof(int));
if(ret)
{
printk("copy from user is error\n");
return -EIO;
}
switch(whitch)
{
case LED1:
gpio_set_value(gpiono1, 0);
break;
case LED2:
gpio_set_value(gpiono2, 0);
break;
case LED3:
gpio_set_value(gpiono3, 0);
break;
}
break;
}
return 0;
}
//close
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 = myled_ioctl,
.release=mycdev_close,
};
//入口函数
static int __init mycdev_init(void)
{
int i;
//进行字符设备驱动的注册
major=register_chrdev(0,"mycdev",&fops);
if(major<0)
{
printk("字符设备驱动注册失败\n");
return major;
}
printk("字符设备驱动注册成功,major=%d\n",major);
//向上提交目录
cls=class_create(THIS_MODULE,"mycdev");
if(IS_ERR(cls))//指针指向预留空间,函数调用失败
{
printk("向上提交目录失败\n");
return PTR_ERR(cls);
}
printk("向上提交目录成功\n");
//向上提交设备节点
for(i=0;i<3;i++)
{
dev=device_create(cls,NULL,MKDEV(major,i),NULL,"mycdev%d",i);
if(IS_ERR(dev))//指针指向预留空间,函数调用失败
{
printk("向上提交目录失败\n");
return PTR_ERR(dev);
}
}
printk("设备节点向上提交成功\n");
//解析myleds设备树节点
dnode=of_find_node_by_name(NULL,"myleds");
if(dnode==NULL)
{
printk("解析设备树节点失败\n");
return -EIO;
}
printk("解析设备树节点成功\n");
//根据设备树节点解析gpio编号
gpiono1 = of_get_named_gpio(dnode,"led1",0);
gpiono2 = of_get_named_gpio(dnode,"led2",0);
gpiono3 = of_get_named_gpio(dnode,"led3",0);
gpiono4 = of_get_named_gpio(dnode,"fs",0);
//申请gpio编号
gpio_request(gpiono1,NULL);
gpio_request(gpiono2,NULL);
gpio_request(gpiono3,NULL);
gpio_request(gpiono3,NULL);
gpio_request(gpiono4,NULL);
//设置gpio为输出并且初始化数值为0
gpio_direction_output(gpiono1,0);
gpio_direction_output(gpiono2,0);
gpio_direction_output(gpiono3,0);
gpio_direction_output(gpiono4,0);
//解析myirqs设备树节点
dnode_key=of_find_node_by_name(NULL,"myirqs");
if(dnode==NULL)
{
printk("解析设备树节点失败\n");
return -EIO;
}
printk("解析设备树节点成功\n");
//根据设备树节点解析出软中断号
irqno=irq_of_parse_and_map(dnode_key,0);
if(!irqno)
{
printk("获取软中断号失败\n");
return -ENXIO;
}
printk("解析设备树获取软中断号成功\n");
//注册中断
request_irq(irqno,irq1_handler,IRQF_TRIGGER_FALLING,"myirq1",NULL);
return 0;
}
//出口函数
static void __exit mycdev_exit(void)
{
int i;
free_irq(irqno,NULL);
gpio_free(gpiono1);
gpio_free(gpiono2);
gpio_free(gpiono3);
gpio_free(gpiono4);
for(i=0;i<3;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");
text.c
#include<stdio.h>
#include<sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include<string.h>
#include <errno.h>
#include "mycdev.h"
int main(int argc, char const *argv[])
{
char buf[128]={0};
int whitch;
int fd=open("/dev/mycdev0",O_RDWR);
if(fd==-1)
{
perror("open is error\n");
return -1;
}
while(1)
{
whitch = LED1;
ioctl(fd,LED_ON,&whitch);
sleep(1);
ioctl(fd,LED_OFF,&whitch);
sleep(1);
whitch = LED2;
ioctl(fd,LED_ON,&whitch);
sleep(1);
ioctl(fd,LED_OFF,&whitch);
sleep(1);
whitch = LED3;
ioctl(fd,LED_ON,&whitch);
sleep(1);
ioctl(fd,LED_OFF,&whitch);
sleep(1);
}
close(fd);
return 0;
}
.h
#ifndef __MYCDEV_H__
#define __MYCDEV_H__
//LED功能码
#define LED_ON _IOW('a',1,int)
#define LED_OFF _IOW('a',0,int)
enum{
LED1,
LED2,
LED3,
};
#endif
效果如图所示: