【正点原子I.MX6U-MINI驱动篇】5、 LED驱动程序框架:从分层到分离(非常重要)

一、字符设备驱动程序框架

字符设备驱动程序框架

图中驱动层访问硬件外设寄存器依靠的是ioremap函数去映射到寄存器地址,然后开始控制寄存器。那么该如何编写驱动程序?

  • 确定主设备号,也可以让内核分配;
  • 定义自己的fle_operations结构体,这是核心;
  • 实现对应的drv_open/drv_read/drv_write等函数,填入file_operations结构体:
  • file_operations结构体告诉内核:通过register_chrdev函数;
  • 谁来注册驱动程序?需要一个入口函数:安装驱动程序时,就会去调用这个入口函数:
  • 有入口函数就应该有出口函数:卸载驱动程序是,出口函数调用unregister chrdev
  • 其它完善:提供设备信息,自动创建设备节点:class create,device create

二、分层思想(上下驱动和硬件分层)

LED驱动程序接口定义构思

把驱动拆分为通用的框架(leddrv.c)、具体的硬件操作(board_X.c):

拆分驱动构成通用框架

以面向对象的思想,改进代码,抽象出一个结构体:

面向对象的思想抽象出结构体

在单板文件中初始化这个结构体

#include <linux/module.h>

#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>
#include "led_opr.h"

static int board_demo_led_init (int which) /* 初始化LED, which-哪个LED */	   
{
    
    
	
	printk("%s %s line %d, led %d\n", __FILE__, __FUNCTION__, __LINE__, which);
	return 0;
}

static int board_demo_led_ctl (int which, char status) /* 控制LED, which-哪个LED, status:1-亮,0-灭 */
{
    
    
	printk("%s %s line %d, led %d, %s\n", __FILE__, __FUNCTION__, __LINE__, which, status ? "on" : "off");
	return 0;
}

static struct led_operations board_demo_led_opr = {
    
    
	.init = board_demo_led_init,
	.ctl  = board_demo_led_ctl,
};

struct led_operations *get_board_led_opr(void)
{
    
    
	return &board_demo_led_opr;
}

每个单板相关的board_X.c实现自己的led_operations结构体,供上层的leddrv.c调用:

构建实现自己的结构体

2.1、编写代码

2.1.1 驱动程序

驱动程序分为上下两层:leddrv.cboard_demo.cleddrv.c负责注册file_operations结构体,它的open/write成员会调用board_demo.c中提供的硬件led_opr中的对应函数。

1、把LED的操作抽象出一个led_operations结构体首先看看led_opr.h,它定义了一个led_operations结构体,把LED的操作抽象为这个结构体:

#ifndef _LED_OPR_H
#define _LED_OPR_H

struct led_operations {
    
    
	int (*init) (int which); /* 初始化LED, which-哪个LED */       
	int (*ctl) (int which, char status); /* 控制LED, which-哪个LED, status:1-亮,0-灭 */
};

struct led_operations *get_board_led_opr(void);

#endif

2、驱动程序的上层:file_operations结构体上层是leddrv.c,它的核心是file_operations结构体,首先看看入口函数:

#include <linux/module.h>

#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>

#include "led_opr.h"

#define LED_NUM 2

/* 1. 确定主设备号                                                                 */
static int major = 0;
static struct class *led_class;
struct led_operations *p_led_opr;


#define MIN(a, b) (a < b ? a : b)

/* 3. 实现对应的open/read/write等函数,填入file_operations结构体                   */
static ssize_t led_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
{
    
    
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	return 0;
}

/* write(fd, &val, 1); */
static ssize_t led_drv_write (struct file *file, const char __user *buf, size_t size, loff_t *offset)
{
    
    
	int err;
	char status;
	struct inode *inode = file_inode(file);
	int minor = iminor(inode);
	
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	err = copy_from_user(&status, buf, 1);

	/* 根据次设备号和status控制LED */
	p_led_opr->ctl(minor, status);
	
	return 1;
}

static int led_drv_open (struct inode *node, struct file *file)
{
    
    
	int minor = iminor(node);
	
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	/* 根据次设备号初始化LED */
	p_led_opr->init(minor);
	
	return 0;
}

static int led_drv_close (struct inode *node, struct file *file)
{
    
    
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	return 0;
}

/* 2. 定义自己的file_operations结构体                                              */
static struct file_operations led_drv = {
    
    
	.owner	 = THIS_MODULE,
	.open    = led_drv_open,
	.read    = led_drv_read,
	.write   = led_drv_write,
	.release = led_drv_close,
};

/* 4. 把file_operations结构体告诉内核:注册驱动程序                                */
/* 5. 谁来注册驱动程序啊?得有一个入口函数:安装驱动程序时,就会去调用这个入口函数 */
static int __init led_init(void)
{
    
    
	int err;
	int i;
	
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	major = register_chrdev(0, "100ask_led", &led_drv);  /* /dev/led */


	led_class = class_create(THIS_MODULE, "100ask_led_class");
	err = PTR_ERR(led_class);
	if (IS_ERR(led_class)) {
    
    
		printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
		unregister_chrdev(major, "100ask_led");
		return -1;
	}

	for (i = 0; i < LED_NUM; i++)
		device_create(led_class, NULL, MKDEV(major, i), NULL, "100ask_led%d", i); /* /dev/100ask_led0,1,... */

	p_led_opr = get_board_led_opr();
	
	return 0;
}

/* 6. 有入口函数就应该有出口函数:卸载驱动程序时,就会去调用这个出口函数           */
static void __exit led_exit(void)
{
    
    
	int i;
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);

	for (i = 0; i < LED_NUM; i++)
		device_destroy(led_class, MKDEV(major, i)); /* /dev/100ask_led0,1,... */

	device_destroy(led_class, MKDEV(major, 0));
	class_destroy(led_class);
	unregister_chrdev(major, "100ask_led");
}

/* 7. 其他完善:提供设备信息,自动创建设备节点                                     */

module_init(led_init);
module_exit(led_exit);

MODULE_LICENSE("GPL");

其中第102行

p_led_opr = get_board_led_opr();

从底层硬件相关的代码board_demo.c中获得led_operaions结构体。

2.1.2 测试程序编写

测试程序为ledtest.c:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

/*
 * ./ledtest /dev/100ask_led0 on
 * ./ledtest /dev/100ask_led0 off
 */
int main(int argc, char **argv)
{
    
    
	int fd;
	char status;
	
	/* 1. 判断参数 */
	if (argc != 3) 
	{
    
    
		printf("Usage: %s <dev> <on | off>\n", argv[0]);
		return -1;
	}

	/* 2. 打开文件 */
	fd = open(argv[1], O_RDWR);
	if (fd == -1)
	{
    
    
		printf("can not open file %s\n", argv[1]);
		return -1;
	}

	/* 3. 写文件 */
	if (0 == strcmp(argv[2], "on"))
	{
    
    
		status = 1;
		write(fd, &status, 1);
	}
	else
	{
    
    
		status = 0;
		write(fd, &status, 1);
	}
	
	close(fd);
	
	return 0;
}

2.1.3 Makefile编写

KERN_DIR = /home/zhiguoxin/linux/IMX6ULL/linux-imx-rel_imx_4.1.15_2.1.0_ga_alientek
CURRENT_PATH := $(shell pwd)

all:
	$(MAKE) -C $(KERN_DIR) M=$(CURRENT_PATH) modules
	arm-linux-gnueabihf-gcc -o ledtest ledtest.c 

clean:
	$(MAKE) -C $(KERN_DIR) M=$(CURRENT_PATH) modules clean
	rm -rf modules.order
	rm -f ledtest
	
led_drv-y := leddrv.o board_demo.o
obj-m	+= led_drv.o
  • 第1行,KERNELDIR表示Linux内核源码目录,使用绝对路径,大家根据自己的实际情况填写。这种方式适用于嵌入式开发的交叉编译,KERN_DIR目录中包含了内核驱动模块所需要的各种头文件及依赖。
  • 第2行,CURRENT_PATH表示当前路径,直接通过运行pwd命令来获取当前所处路径。
  • 第5行,具体的编译命令,后面的modules表示编译模块,-C表示将当前的工作目录切换到指定目录中,也就是KERNERLDIR目录执行makefile。M表示模块源码目录,make modules命令中加入M=dir以后程序会自动到指定的dir目录中读取模块的源码并将其编译为.ko文件。
  • 第6行,使用交叉编译工具链将ledtest.c编译成可以在arm板子上运行的ledtest可执行文件。
  • 第9行,清除modules。
  • 第10行,清除modules.order。
  • 第11行,清除ledtest。
  • 第13、14行,要想把leddrv.c,board_demo.c编译成led_drv.ko。

2.1.4 源码目录

源码目录

2.2、具体单板的LED驱动程序

上面的board_demo.c文件中的board_demo_led_init和board_demo_led_ctl函数并没有实现

我现在使用的是im6ull板子,我现在就只需要在里面添加相应的操作即可,这样我们的驱动文件就不需要做任何的修改

#include <linux/module.h>

#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>
#include <asm/io.h>

#include "led_opr.h"

static volatile unsigned int *CCM_CCGR1                              ;
static volatile unsigned int *IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3;
static volatile unsigned int *GPIO5_GDIR                             ;
static volatile unsigned int *GPIO5_DR                               ;

static int board_demo_led_init (int which) /* 初始化LED, which-哪个LED */       
{
    
    
    unsigned int val;

    //printk("%s %s line %d, led %d\n", __FILE__, __FUNCTION__, __LINE__, which);
    if (which == 0)
    {
    
    
        if (!CCM_CCGR1)
        {
    
    
            CCM_CCGR1                               = ioremap(0x20C406C, 4);
            IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 = ioremap(0x2290014, 4);
            GPIO5_GDIR                              = ioremap(0x020AC000 + 0x4, 4);
            GPIO5_DR                                = ioremap(0x020AC000 + 0, 4);
        }
        
        /* GPIO5_IO03 */
        /* a. 使能GPIO5
         * set CCM to enable GPIO5
         * CCM_CCGR1[CG15] 0x20C406C
         * bit[31:30] = 0b11
         */
        *CCM_CCGR1 |= (3<<30);
        
        /* b. 设置GPIO5_IO03用于GPIO
         * set IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3
         *      to configure GPIO5_IO03 as GPIO
         * IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3  0x2290014
         * bit[3:0] = 0b0101 alt5
         */
        val = *IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3;
        val &= ~(0xf);
        val |= (5);
        *IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 = val;
        
        
        /* b. 设置GPIO5_IO03作为output引脚
         * set GPIO5_GDIR to configure GPIO5_IO03 as output
         * GPIO5_GDIR  0x020AC000 + 0x4
         * bit[3] = 0b1
         */
        *GPIO5_GDIR |= (1<<3);
    }
    
    return 0;
}

static int board_demo_led_ctl (int which, char status) /* 控制LED, which-哪个LED, status:1-亮,0-灭 */
{
    
    
    //printk("%s %s line %d, led %d, %s\n", __FILE__, __FUNCTION__, __LINE__, which, status ? "on" : "off");
    if (which == 0)
    {
    
    
        if (status) /* on: output 0*/
        {
    
    
            /* d. 设置GPIO5_DR输出低电平
             * set GPIO5_DR to configure GPIO5_IO03 output 0
             * GPIO5_DR 0x020AC000 + 0
             * bit[3] = 0b0
             */
            *GPIO5_DR &= ~(1<<3);
        }
        else  /* off: output 1*/
        {
    
    
            /* e. 设置GPIO5_IO3输出高电平
             * set GPIO5_DR to configure GPIO5_IO03 output 1
             * GPIO5_DR 0x020AC000 + 0
             * bit[3] = 0b1
             */ 
            *GPIO5_DR |= (1<<3);
        }
    
    }
    return 0;
}

static struct led_operations board_demo_led_opr = {
    
    
    .num  = 1,
    .init = board_demo_led_init,
    .ctl  = board_demo_led_ctl,
};

struct led_operations *get_board_led_opr(void)
{
    
    
    return &board_demo_led_opr;
}

三、分离思想(上中下三层)

3.1 面向对象

字符设备驱动程序抽象出一个file_operations结构体;我们写的程序针对硬件部分抽象出led_operations结构体。

3.2 驱动分层

上下分层,比如我们前面第二节写的LED驱动程序就分为2层:

  • 1.上层实现硬件无关的操作,比如注册字符设备驱动:leddrv.c
  • 2.下层实现硬件相关的操作,比如board A.c实现单板A的LED操作

led 的分层

3.3 驱动分离

还能不能改进?分离
在board A.c中,实现了一个led operations,为LED引脚实现了初始化函数、控制函数:

static struct led_operations board_demo_led_opr = {
    
    
	.num  = 1,
	.init = board_demo_led_init,
	.ctl  = board_demo_led_ctl,
};

如果硬件上更换一个引脚来控制LED怎么办?你要去修改上面结构体中的init、ctl函数。

实际情况是,每一款芯片它的GPI0操作都是类似的。比如:GPIO1_3、GPIO5_4这2个引脚接到LED:

  • GPIO1_3属于第1组,即GPIO1

    • 有方向寄存器DIR、数据寄存器DR等,基础地址是addr base addr gpio1
    • 设置为output引脚:修改GPIO1的DIR寄存器的bit3
    • 设置输出电平:修改GPIO1的DR寄存器的bit3
  • GPIO5_4属于第5组,即GPIO5

    • 有方向寄存器DIR、数据寄存器DR等,基础地址是addr base addr gpio5
    • 设置为output引脚:修改GPIO5的DIR寄存器的bit4
    • 设置输出电平:修改GPIO5的DR寄存器的bit4。

既然引脚操作那么有规律,并且这是跟主芯片相关的,那可以针对该芯片写出比较通用的硬件操作代码。

比如board A.c使用芯片chipY,那就可以写出:chipY_gpio.c,它实现芯片Y的GPIO操作,适用于芯片Y的所有GPIO引脚。

使用时,我们只需要在board_A_led.c中指定使用哪一个引脚即可。程序结构如下:

程序结构

以面向对象的思想,在board_A_led.c中实现led resouce结构体,它定义“资源”一要用哪一个引脚。
chipY_gpio.c中仍是实现led_operations结构体,它要写得更完善,支持所有GPIO

3.3、编写代码

3.3.1 驱动程序

程序仍分为上下结构:上层leddrv.c向内核注册file operations结构体;下层chip_demo_gpio.c提供led_operations结构体来操作硬件。

下层的代码分为2个:chip_demo_gpio.c实现通用的GPI0操作,board A led.c指定使用哪个GPIO,即“资源”。

led_resource.h中定义了led_resource结构体,用来描述GPIO:


board_A_led.c 指定使用哪个GPIO,它实现一个led_resource结构体,
并提供访问函数:


chip_demo_gpio.c中,首先获得board_A_led.c实现的led_resource结构体,然后再进行其他操作,请看下面第 26 行:

3.3.2 chip_demo_gpio.c

#include <linux/gfp.h>
#include "led_opr.h"
#include "led_resource.h"

static struct led_resource *led_rsc;
static int board_demo_led_init (int which) /* 初始化LED, which-哪个LED */	   
{
    
    	
	//printk("%s %s line %d, led %d\n", __FILE__, __FUNCTION__, __LINE__, which);
	if (!led_rsc)
	{
    
    
		led_rsc = get_led_resouce();
	}
	
	printk("init gpio: group %d, pin %d\n", GROUP(led_rsc->pin), PIN(led_rsc->pin));
	switch(GROUP(led_rsc->pin))
	{
    
    
		case 0:
		{
    
    
			printk("init pin of group 0 ...\n");
			break;
		}
		case 1:
		{
    
    
			printk("init pin of group 1 ...\n");
			break;
		}
		case 2:
		{
    
    
			printk("init pin of group 2 ...\n");
			break;
		}
		case 3:
		{
    
    
			printk("init pin of group 3 ...\n");
			break;
		}
	}
	
	return 0;
}

static int board_demo_led_ctl (int which, char status) /* 控制LED, which-哪个LED, status:1-亮,0-灭 */
{
    
    
	//printk("%s %s line %d, led %d, %s\n", __FILE__, __FUNCTION__, __LINE__, which, status ? "on" : "off");
	printk("set led %s: group %d, pin %d\n", status ? "on" : "off", GROUP(led_rsc->pin), PIN(led_rsc->pin));

	switch(GROUP(led_rsc->pin))
	{
    
    
		case 0:
		{
    
    
			printk("set pin of group 0 ...\n");
			break;
		}
		case 1:
		{
    
    
			printk("set pin of group 1 ...\n");
			break;
		}
		case 2:
		{
    
    
			printk("set pin of group 2 ...\n");
			break;
		}
		case 3:
		{
    
    
			printk("set pin of group 3 ...\n");
			break;
		}
	}

	return 0;
}

static struct led_operations board_demo_led_opr = {
    
    
	.init = board_demo_led_init,
	.ctl  = board_demo_led_ctl,
};

struct led_operations *get_board_led_opr(void)
{
    
    
	return &board_demo_led_opr;
}

3.3.2 board_A_led.c

#include "led_resource.h"

static struct led_resource board_A_led = {
    
    
	.pin = GROUP_PIN(3,1),
};

struct led_resource *get_led_resouce(void)
{
    
    
	return &board_A_led;
}

3.3.2 lde_resource.h

#ifndef _LED_RESOURCE_H
#define _LED_RESOURCE_H

/* GPIO3_0 */
/* bit[31:16] = group */
/* bit[15:0]  = which pin */
#define GROUP(x) (x>>16)
#define PIN(x)   (x&0xFFFF)
#define GROUP_PIN(g,p) ((g<<16) | (p))

struct led_resource {
    
    
	int pin;
};

struct led_resource *get_led_resouce(void);

#endif

猜你喜欢

转载自blog.csdn.net/qq_39400113/article/details/128035669