【嵌入式Linux】嵌入式Linux驱动开发基础知识之驱动设计的思想:面向对象/分层/分离

文章目录

  • 前言
  • 1、分离设计
    • 驱动程序分析---程序分层
      • 通用驱动程序---面向对象
      • 个性化驱动程序---分离
    • APP 程序分析

前言

韦东山嵌入式Linux驱动开发基础知识学习笔记
文章中大多内容来自韦东山老师的文档,还有部分个人根据自己需求补充的内容
视频教程地址:https://www.bilibili.com/video/BV14f4y1Q7ti

上一章在驱动中引入面向对象和分层的设计思想
在驱动里简单来说面向对象就是抽象一个结构体作为设备的类,然后将结构体成员作为对象
而分层设计思想简单来说就是将 内核相关的操作和硬件强相关操作分离 达到内核相关操作通用,硬件相关操作个性化目的
本节将介绍分离思想
【嵌入式Linux】嵌入式Linux驱动开发基础知识之LED驱动框架–面向对象、分层设计思想

1、分离设计

【嵌入式Linux】嵌入式Linux驱动开发基础知识之驱动设计的思想:面向对象/分层/分离_第1张图片

▲面向对象、分层、分离思想体现

将使能、操作GPIO的流程抽象成统一的接口,LED的init可以去调用这些接口,其中添加struct led_resource结构体,其中保存了LED需要的资源即GPIO的group & pin,实现初始化。一系列操作相当于封装了GPIO的寄存器相关操作供设备使用

驱动程序分析—程序分层

通用驱动程序—面向对象

led_opr.h:包含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-灭 */
	int (*exit) (void);
};

struct led_operations *get_board_led_opr(void);

#endif

leddrv.c:给APP提供接口

#include 

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#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, "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__);

	p_led_opr->exit();

	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");

个性化驱动程序—分离

board_A_led:包含LED所需要的资源信息

#include "led_resource.h"

static struct led_resource led_resources = {
	.num = 2,
	/* PA10 green */
	.pin[0] = GROUP_PIN(0,10),
	/* PG8 yellow */
	.pin[1] = GROUP_PIN(6,8),
};

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

chip_demo_gpio.c:包含GPIO相关操作、使能函数,LED设备对象从这里获得接口

#include 

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include "led_opr.h"
#include "led_resource.h"

/* registers */
// RCC_PLL4CR地址:0x50000000 + 0x894
static volatile unsigned int *RCC_PLL4CR;

// RCC_MP_AHB4ENSETR 地址:0x50000000 + 0xA28
static volatile unsigned int *RCC_MP_AHB4ENSETR;

// GPIOA_MODER 地址:0x50002000 + 0x00
static volatile unsigned int *GPIOA_MODER;

// GPIOA_BSRR 地址: 0x50002000 + 0x18
static volatile unsigned int *GPIOA_BSRR;

// GPIOG_MODER 地址:0x50008000 + 0x00
static volatile unsigned int *GPIOG_MODER;

// GPIOG_BSRR 地址: 0x50008000 + 0x18
static volatile unsigned int *GPIOG_BSRR;
/* registers end */

struct led_resource *led_rsc;

static int board_demo_led_init (int which) /* 初始化LED, which-哪个LED */	   
{
	if (!led_rsc)
	{
		led_rsc = get_led_resouce();
	}
	/* 寄存器映射到变量 */
	if (!RCC_PLL4CR)
	{
		// RCC_PLL4CR地址:0x50000000 + 0x894
		RCC_PLL4CR = ioremap(0x50000000 + 0x894, 4);
		
		// RCC_MP_AHB4ENSETR 地址:0x50000000 + 0xA28
		RCC_MP_AHB4ENSETR = ioremap(0x50000000 + 0xA28, 4);
		
		// GPIOA_MODER 地址:0x50002000 + 0x00
		GPIOA_MODER = ioremap(0x50002000 + 0x00, 4);
		
		// GPIOA_BSRR 地址: 0x50002000 + 0x18
		GPIOA_BSRR = ioremap(0x50002000 + 0x18, 4);

		// GPIOG_MODER 地址:0x50008000 + 0x00
		GPIOG_MODER = ioremap(0x50008000 + 0x00, 4);

		// GPIOG_BSRR 地址: 0x50008000 + 0x18
		GPIOG_BSRR = ioremap(0x50008000 + 0x18, 4);
	}

	switch(GROUP(led_rsc->pin[which]))
	{
		case 0:
		{
			printk("init pin of group A ...\n");
			/* enalbe PLL4, it is clock source for all gpio */
			*RCC_PLL4CR |= (1<<0);
			while ((*RCC_PLL4CR & (1<<1)) == 0);
			
			/* enable gpio */
			*RCC_MP_AHB4ENSETR |= (1<<GROUP(led_rsc->pin[which]));
			
			/*
			* configure gpio as output 
			*/
			*GPIOA_MODER &= ~(3 << (PIN(led_rsc->pin[which])*2) );
			*GPIOA_MODER |= (1<< (PIN(led_rsc->pin[which])*2) );
			/* ... */
			break;
		}
		case 6:
		{
			printk("init pin of group G ...\n");
			/* enalbe PLL4, it is clock source for all gpio */
			*RCC_PLL4CR |= (1<<0);
			while ((*RCC_PLL4CR & (1<<1)) == 0);
			
			/* enable gpio */
			*RCC_MP_AHB4ENSETR |= (1<<GROUP(led_rsc->pin[which]));

			/*
			* configure gpg8 as gpio
			* configure gpio as output 
			*/
			*GPIOG_MODER &= ~((PIN(led_rsc->pin[which])*2));
			*GPIOG_MODER |= ((PIN(led_rsc->pin[which])*2));
			break;
		}
		default:
			printk("not support %d\n", led_rsc->pin[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");
	printk("set led %s: group %d, pin %d\n", status ? "on" : "off", GROUP(led_rsc->pin[which]), PIN(led_rsc->pin[which]));

	switch(GROUP(led_rsc->pin[which]))
	{
		case 0:
		{
			printk("set pin of group A ...\n");
			*GPIOA_BSRR = ( 1<< (16*status + PIN(led_rsc->pin[which])));
			break;
		}
		case 6:
		{
			printk("set pin of group G ...\n");
			*GPIOG_BSRR = ( 1<< (16*status + PIN(led_rsc->pin[which])));
			break;
		}
		default:
		printk("not support %d\n", led_rsc->pin[which]);
	}

	return 0;
}

static int board_demo_led_exit (void)
{
	if(RCC_PLL4CR)
	{
		iounmap(RCC_PLL4CR);
		iounmap(RCC_MP_AHB4ENSETR);
		iounmap(GPIOA_MODER);
		iounmap(GPIOA_BSRR);
		iounmap(GPIOG_MODER);
		iounmap(GPIOG_BSRR);
	}

	return 0;
}

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

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

APP 程序分析

ledtest.c


#include 
#include 
#include 
#include 
#include 
#include 

/*
 * ./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  \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;
}

你可能感兴趣的:(#,嵌入式Linux,linux,STM32MP157,驱动开发)