利用platform总线驱动实现在不影响流水灯运行时,按键使风扇启动关闭。

  • Post author:
  • Post category:其他


准备好关于paltform总线驱动参量的初始化后,利用驱动端一键注册宏完成platform的API;在与设备匹配后执行的prode函数中执行对,设备节点的注册;中断号与gpio编号的申请;在与设备分离时执行的remove函数中完成对申请资源的释放,例如注销中断,gpio号,设备节点等;在ioctl函数中获取用户层传送的信息,按对应逻辑执行并完成用户层的流水灯逻辑。

需要现在设备树的内核源码,定义相关设备节点,并且需要控制引脚的位置,大致如下:

myplatform{
    compilte = "hqyj,hello2";
    interrupt-parent = <&gpiof>;
    interrupts = <9 0>;
    devs = <&gpioe 10 0>,<&gpiof 10 0>,<&gpioe 8 0>,<&gpioe 9 0>;    
};

驱动文件:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/mod_devicetable.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/interrupt.h>
#include <linux/of_irq.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/slab.h>

//创建设备节点,便于应用层对设备文件进行读写
struct cdev *cdev;
dev_t devno;
struct class *cls;
struct device *device;
unsigned int minor = 0;
unsigned int major = 0;

//解析设备树,管理gpio子系统
int irqno;
struct gpio_desc *gpiono[4];

//platform名字表
struct of_device_id oftable[]=
{
	{.compatible="hqyj,hello1"},
	{.compatible="hqyj,hello2"},
	{.compatible="hqyj,hello3"},
	{},
};

//ioctl灯的状态宏
enum{
	LED1,
	LED2,
	LED3,
};
#define LED_ON _IOW('a',1,int)
#define LED_OFF _IOW('a',0,int)

irqreturn_t irq_handler(int irq,void *arg)
{
	gpiod_set_value(gpiono[3],!gpiod_get_value(gpiono[3]));
	return IRQ_HANDLED;	
}

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 __user *ubuf, size_t size, loff_t *loff)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}
ssize_t mycdev_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loff)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}
int mycdev_close (struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}

long mycdev_ioctl(struct file *file,unsigned int cmd,unsigned long args)
{
	int which;
    int ret;

    switch(cmd)
    {
    case LED_ON:
        ret = copy_from_user(&which,(void*)args,sizeof(int));
        if(ret)
        {
            printk("copy from user is error\n");
            return -EIO;
        }
        switch (which)
        {
            case LED1:
                gpiod_set_value(gpiono[0],1);   
                break;
            case LED2:
                gpiod_set_value(gpiono[1],1);   
                break;
            case LED3:
                gpiod_set_value(gpiono[2],1);   
                break;
        }
    break;
    case LED_OFF:
        ret = copy_from_user(&which,(void*)args,sizeof(int));
        if(ret)
        {
            printk("copy from user is error\n");
            return -EIO;
        }
        switch (which)
        {
            case LED1:
                gpiod_set_value(gpiono[0],0);   
				break;
            case LED2:
                gpiod_set_value(gpiono[1],0);   
                break;
            case LED3:
                gpiod_set_value(gpiono[2],0);   
                break;
        }
        break; 
	}
	return 0;
}

const struct file_operations fops = {
    .open = mycdev_open,
    .read = mycdev_read,
    .write = mycdev_write,
	.unlocked_ioctl = mycdev_ioctl,
    .release = mycdev_close,
};
int pdrv_prode(struct platform_device *pdev)
{
	int i;
	int ret;
	printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);

	//1.分配对象
	cdev = cdev_alloc();
	if(NULL == cdev)
	{
		printk("分配对象失败\n");
		ret= -ENOMEM;
		goto ERR1;
	}
	printk("分配对象成功\n");
	//2.对象初始化
	cdev_init(cdev,&fops);
	//3.设备资源的申请
	ret = alloc_chrdev_region(&devno,minor,3,"mycdev");
	if(ret)
	{
		printk("动态申请设备好失败\n");
		goto ERR2;
	}
	major = MAJOR(devno);
	minor = MINOR(devno);

	//4.注册
	ret = cdev_add(cdev,devno,3);
    if(ret)
	{
		printk("驱动注册内核失败\n");
		goto ERR3;
	}
	//5.提交目录
	cls = class_create(THIS_MODULE,"mycdev");
	if(IS_ERR(cls))
	{
		printk("向上提交目录失败\n");
		goto ERR4;
	}
	//6.提交设备节点信息,需要提交3次节点信息
	for(i = 0;i < 3;i++)
	{
		device = device_create(cls,NULL,MKDEV(major,i),NULL,"mycdev%d",i);
		if(IS_ERR(device))
		{
			printk("向上提交节点信息失败\n");
			goto ERR5;
		}
	}

	irqno = platform_get_irq(pdev,0);
	if(irqno < 0)
	{
		printk("由IRQ类型获取设备信息失败\n");	
		return -ENODATA;	
	}
	printk("IRQ类型资源数值为:%d\n",irqno);
	ret = request_irq(irqno,irq_handler,IRQF_TRIGGER_FALLING,"myirq1",NULL);
	if(ret)
	{
		printk("注册中断号失败\n");	
		return -EIO;
 	}

	for(i = 0;i < 4;i++)
	{
		gpiono[i] = gpiod_get_from_of_node(pdev->dev.of_node,"devs",i,GPIOD_OUT_LOW,NULL);
		if(IS_ERR(gpiono[i]))
		{
			printk("获取gpio编号%d失败\n",i);
			return PTR_ERR(gpiono[i]);		
		}
	}
	printk("获取gpio编号成功\n");
    return 0;	
ERR5://
	for(--i;i>=0;i--)
	{
		device_destroy(cls,MKDEV(major,i));
	}
	class_destroy(cls);

ERR4://
	cdev_del(cdev);

ERR3://
    unregister_chrdev_region(MKDEV(major,minor),3);
ERR2://
    kfree(cdev);
ERR1://对象空间申请失败
	return ret;	
}
int pdrv_remove(struct platform_device *pdev)
{
	int i;
	printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
	for(i = 0;i < 4;i++)
	{
		gpiod_set_value(gpiono[i],0);
		gpiod_put(gpiono[i]);
	}
	free_irq(irqno,NULL);

	//1.销毁节点信息
	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);
    return 0;		
}
struct platform_driver pdrv={
	.probe = pdrv_prode,
	.remove = pdrv_remove,
	.driver = {
		.name = "xin",
		.of_match_table = oftable,
	},

};

 
module_platform_driver(pdrv);
MODULE_LICENSE("GPL");

用户层文件:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/ioctl.h>
enum{
	LED1,
	LED2,
	LED3,
};
#define LED_ON _IOW('a',1,int)
#define LED_OFF _IOW('a',0,int)
int main(int argc,const char * argv[])
{
    int which;
    int fd = -1;
    fd = open("/dev/mycdev0",O_RDWR);
    if(fd == -1)
    {
        perror("open is error\n");
        return -1;
    }

    while(1)
    {
        which = LED1;
        ioctl(fd,LED_ON,&which);
        sleep(1);
        ioctl(fd,LED_OFF,&which);

        which = LED2;
        ioctl(fd,LED_ON,&which);
        sleep(1);
        ioctl(fd,LED_OFF,&which);

        which = LED3;
        ioctl(fd,LED_ON,&which);
        sleep(1);
        ioctl(fd,LED_OFF,&which);
    }
    close(fd);
    return 0;
}



版权声明:本文为Huge_Whale原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。