LED驱动(原始架构优化:分层/分离)——STM32MP157

文章目录

  • 优化思想:分层
    • Demo的LED驱动程序
      • led_opr.h
      • board_demo.c
      • leddrv.c
      • ledtest.c
      • Makefile
      • 编译测试
    • STM32MP157的LED驱动程序
      • board_stm32mp157.c
      • leddrv.c
      • led_opr.h
      • Makefiel
      • 编译测试
  • 优化思想:分离
    • Demo的LED驱动程序
      • led_resource.h
      • board_A_led.c
      • chip_demo_gpio.c
      • Makefile
      • 编译测试
  • 总结:
    • 面向对象
    • 分层
    • 分离

优化思想:分层

使用分层的思想,使得LED 驱动能支持多个板子

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

file_operations 结构体定义如下:
在这里插入图片描述

每个单板相关的 board_X.c 实现自己的 led_operations 结构体,供上层的 leddrv.c 调用
LED驱动(原始架构优化:分层/分离)——STM32MP157_第1张图片

Demo的LED驱动程序

实现功能:

  • 通过board_demo.c里面的write函数实现点灯和关灯的信息打印

led_opr.h

声明结构体:led_operations

#ifndef _LED_OPR_H
#define _LED_OPR_H

//声明结构体:led_operations 
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

board_demo.c

定义结构体:led_operations

demo程序不操作具体硬件,只返回打印信息;即应用层调用write函数,最终会调用单板的board_demo_led_ctl 函数

#include 

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#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;
}

leddrv.c

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

可以定义多个设备节点,在入口函数时创建多个设备节点,出口函数销毁多个设备节点

添加获取次设备号代码:能够根据用户输入的设备节点的名字获取次设备号

#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);//获取inode结构体
	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, "my_led", &led_drv);  /* /dev/led */


	led_class = class_create(THIS_MODULE, "my_led_class");
	err = PTR_ERR(led_class);
	if (IS_ERR(led_class)) {
		printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
		unregister_chrdev(major, "my_led");
		return -1;
	}
	//根据次设备号打开多个设备节点
	for (i = 0; i < LED_NUM; i++)
		device_create(led_class, NULL, MKDEV(major, i), NULL, "my_led%d", i); /* /dev/my_led0,1,... */

	//从底层硬件相关的代码 board_demo.c 中获得 led_operaions结构体
	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/my_led0,1,... */

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


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

module_init(led_init);
module_exit(led_exit);

MODULE_LICENSE("GPL");

ledtest.c


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

/*
 * ./ledtest /dev/my_led0 on
 * ./ledtest /dev/my_led0 off
 */
int main(int argc, char **argv)
{
	int fd;
	char status;//作为开灯与关灯的变量,1表示开灯,0表示关灯
	
	/* 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;
}



Makefile


# 1. 使用不同的开发板内核时, 一定要修改KERN_DIR
# 2. KERN_DIR中的内核要事先配置、编译, 为了能编译内核, 要先设置下列环境变量:
# 2.1 ARCH,          比如: export ARCH=arm64
# 2.2 CROSS_COMPILE, 比如: export CROSS_COMPILE=aarch64-linux-gnu-
# 2.3 PATH,          比如: export PATH=$PATH:/home/book/100ask_stm32mp157_pro-sdk/ToolChain/arm-buildroot-linux-gnueabihf_sdk-buildroot/bin 
# 注意: 不同的开发板不同的编译器上述3个环境变量不一定相同,
#       请参考各开发板的高级用户使用手册

KERN_DIR = /home/book/100ask_stm32mp157_pro-sdk/Linux-5.4

all:
	make -C $(KERN_DIR) M=`pwd` modules 
	$(CROSS_COMPILE)gcc -o ledtest ledtest.c 

clean:
	make -C $(KERN_DIR) M=`pwd` modules clean
	rm -rf modules.order
	rm -f ledtest

# 参考内核源码drivers/char/ipmi/Makefile
# 要想把a.c, b.c编译成ab.ko, 可以这样指定:
# ab-y := a.o b.o
# obj-m += ab.o

# leddrv.c board_demo.c 编译成 my_led.ko
my_led-y := leddrv.o board_demo.o
obj-m	+= my_led.o

编译测试

在Makefile文件目录下执行make指令,此时,目录下有编译好的内核模块my_led.ko和可执行程序ledtest ,移植到开发板上

insmod my_led.ko  //挂载驱动程序
echo "7 4 1 7" > /proc/sys/kernel/printk 
./ledtest /dev/my_led0 on // 点灯
./ledtest /dev/my_led0 off  //熄灭

执行结果:

[root@100ask:~]#./ledtest /dev/my_led0 on 
[13463.176987] /home/book/source/02_led_drv/01_led_drv_template/leddrv.c led_drv_open line 58
[13463.197877] /home/book/source/02_led_drv/01_led_drv_template/board_demo.c board_demo_led_init line 22, led 0
[13463.216232] /home/book/source/02_led_drv/01_led_drv_template/leddrv.c led_drv_write line 45
[13463.232889] /home/book/source/02_led_drv/01_led_drv_template/board_demo.c board_demo_led_ctl line 28, led 0, on // 可以看到这句“on ”打印
[13463.247977] /home/book/source/02_led_drv/01_led_drv_template/leddrv.c led_drv_close line 67

[root@100ask:~]#./ledtest /dev/my_led0 off 
[13464.540637] /home/book/source/02_led_drv/01_led_drv_template/leddrv.c led_drv_open line 58
[13464.554380] /home/book/source/02_led_drv/01_led_drv_template/board_demo.c board_demo_led_init line 22, led 0
[13464.569671] /home/book/source/02_led_drv/01_led_drv_template/leddrv.c led_drv_write line 45
[13464.580615] /home/book/source/02_led_drv/01_led_drv_template/board_demo.c board_demo_led_ctl line 28, led 0, off // 可以看到这句“off ”打印
[13464.593397] /home/book/source/02_led_drv/01_led_drv_template/leddrv.c led_drv_close line 67

STM32MP157的LED驱动程序

LED的操作主要是先写框架,再写硬件操作的代码

根据原始框架中编写的GPIO操作的方法,操作指定寄存器,完成指定功能

参考博文:LED驱动(原始架构)——STM32MP157

将上一章Demo的LED驱动程序中的文件替换为以下几个文件,其他不变

board_stm32mp157.c

将上面的board_demo.c 修改为board_stm32mp157.c

#include 

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

#include "led_opr.h"

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

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

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

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

//初始化 LED 引脚:使能、设置为GPIO 模式、设置为输出引脚
static int board_demo_led_init (int which) /* 初始化LED, which-哪个LED */	   
{
	//映射之前先判断,防止多次映射
	if (!RCC_PLL4CR)
	{
		// RCC_PLL4CR地址:0x50000000 + 0x894
		RCC_PLL4CR = (volatile unsigned int *)ioremap(0x50000000 + 0x894, 4);
		
		// RCC_MP_AHB4ENSETR 地址:0x50000000 + 0xA28
		RCC_MP_AHB4ENSETR = (volatile unsigned int *)ioremap(0x50000000 + 0xA28, 4);
		
		// GPIOA_MODER 地址:0x50002000 + 0x00
		GPIOA_MODER = (volatile unsigned int *)ioremap(0x50002000 + 0x00, 4);
		
		// GPIOA_BSRR 地址: 0x50002000 + 0x18
		GPIOA_BSRR = (volatile unsigned int *)ioremap(0x50002000 + 0x18, 4);
	}

	if (which == 0)
	{
		/* enalbe PLL4, it is clock source for all gpio */
		*RCC_PLL4CR |= (1<<0);
		while ((*RCC_PLL4CR & (1<<1)) == 0);
		
		/* enable gpioA */
		*RCC_MP_AHB4ENSETR |= (1<<0);
		
		/*
		 * configure gpa10 as gpio
		 * configure gpio as output 
		 */
		*GPIOA_MODER &= ~(3<<20);
		*GPIOA_MODER |= (1<<20);
	}

	return 0;
}

static int board_demo_led_ctl (int which, char status) /* 控制LED, which-哪个LED, status:1-亮,0-灭 */
{
	if (which == 0)
	{
		/* to set gpio register: out 1/0 */
		if (status)
		{
			/* set gpa10 to let led on */
			*GPIOA_BSRR = (1<<26);
		}
		else
		{
			/* set gpa10 to let led off */
			*GPIOA_BSRR = (1<<10);
		}
	}
	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;
}

leddrv.c

为了leddrv的通用性,将次设备号的个数放在单板代码中然后读取,而不是在该文件中定义次设备号个数

#include 

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

#include "led_opr.h"


/* 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, "my_led", &led_drv);  /* /dev/led */


	led_class = class_create(THIS_MODULE, "my_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;
	}

	p_led_opr = get_board_led_opr();

	for (i = 0; i < p_led_opr->num; i++)
		device_create(led_class, NULL, MKDEV(major, i), NULL, "my_led%d", i); /* /dev/my_led0,1,... */

	
	return 0;
}

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

	for (i = 0; i < p_led_opr->num; i++)
		device_destroy(led_class, MKDEV(major, i)); /* /dev/my_led0,1,... */

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


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

module_init(led_init);
module_exit(led_exit);

MODULE_LICENSE("GPL");

led_opr.h

声明结构体:led_operations

#ifndef _LED_OPR_H
#define _LED_OPR_H

struct led_operations {
	int num;//次设备号
	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


Makefiel


# 1. 使用不同的开发板内核时, 一定要修改KERN_DIR
# 2. KERN_DIR中的内核要事先配置、编译, 为了能编译内核, 要先设置下列环境变量:
# 2.1 ARCH,          比如: export ARCH=arm64
# 2.2 CROSS_COMPILE, 比如: export CROSS_COMPILE=aarch64-linux-gnu-
# 2.3 PATH,          比如: export PATH=$PATH:/home/book/100ask_roc-rk3399-pc/ToolChain-6.3.1/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin 
# 注意: 不同的开发板不同的编译器上述3个环境变量不一定相同,
#       请参考各开发板的高级用户使用手册

KERN_DIR = /home/book/100ask_stm32mp157_pro-sdk/Linux-5.4

all:
	make -C $(KERN_DIR) M=`pwd` modules 
	$(CROSS_COMPILE)gcc -o ledtest ledtest.c 

clean:
	make -C $(KERN_DIR) M=`pwd` modules clean
	rm -rf modules.order
	rm -f ledtest

# 参考内核源码drivers/char/ipmi/Makefile
# 要想把a.c, b.c编译成ab.ko, 可以这样指定:
# ab-y := a.o b.o
# obj-m += ab.o

# leddrv.c board_demo.c 编译成 my_led.ko
my_led-y := leddrv.o board_stm32mp157.o
obj-m	+= my_led.o

编译测试

在Makefile文件目录下执行make指令,此时,目录下有编译好的内核模块my_led.ko和可执行程序ledtest ,移植到开发板上

insmod my_led.ko
echo none > /sys/class/leds/heartbeat/trigger // 关闭心跳灯
./ledtest /dev/my_led0 on
./ledtest /dev/my_led0 off

优化思想:分离

目的:写出比较通用的硬件操作代码

程序结构:

LED驱动(原始架构优化:分层/分离)——STM32MP157_第2张图片

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

Demo的LED驱动程序

led_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))//组合引脚信息

//定义了 led_resource 结构体,用来描述 GPIO:
struct led_resource {
	int pin;//该变量里面组合了引脚的信息
};

struct led_resource *get_led_resouce(void);

#endif

board_A_led.c

运行设备节点,会打开指定的第几组的第几个引脚,引脚的定义在文件:board_A_led中

#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;
}

chip_demo_gpio.c

简单的实现驱动函数的调用

#include 

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#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();//首先获得 board_A_led.c 实现的 led_resource结构体,然后再进行其他操作
	}
	
	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;
}


Makefile


# 1. 使用不同的开发板内核时, 一定要修改KERN_DIR
# 2. KERN_DIR中的内核要事先配置、编译, 为了能编译内核, 要先设置下列环境变量:
# 2.1 ARCH,          比如: export ARCH=arm64
# 2.2 CROSS_COMPILE, 比如: export CROSS_COMPILE=aarch64-linux-gnu-
# 2.3 PATH,         比如: export PATH=$PATH:/home/book/100ask_stm32mp157_pro-sdk/ToolChain/arm-buildroot-linux-gnueabihf_sdk-buildroot/bin 
# 注意: 不同的开发板不同的编译器上述3个环境变量不一定相同,
#       请参考各开发板的高级用户使用手册

KERN_DIR = /home/book/100ask_stm32mp157_pro-sdk/Linux-5.4

all:
	make -C $(KERN_DIR) M=`pwd` modules 
	$(CROSS_COMPILE)gcc -o ledtest ledtest.c 

clean:
	make -C $(KERN_DIR) M=`pwd` modules clean
	rm -rf modules.order
	rm -f ledtest

# 参考内核源码drivers/char/ipmi/Makefile
# 要想把a.c, b.c编译成ab.ko, 可以这样指定:
# ab-y := a.o b.o
# obj-m += ab.o

# leddrv.c board_demo.c 编译成 100ask.ko
my_led-y := leddrv.o chip_demo_gpio.o board_A_led.o
obj-m	+= my_led.o

编译测试

在Makefile文件目录下执行make指令,此时,目录下有编译好的内核模块my_led.ko和可执行程序ledtest ,移植到开发板上

insmod my_led.ko  //挂载驱动程序
echo "7 4 1 7" > /proc/sys/kernel/printk 
./ledtest /dev/my_led0 on // 点灯
./ledtest /dev/my_led0 off  //熄灭

总结:

驱动设思想:面向对象,分层,分离

在linux内核里面,面向对象即用一个结构体来表示对象

面向对象

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

分层

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

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

分离

程序仍分为上下结构:

  • 上层 leddrv.c 向内核注册 file_operations 结构体;
  • 下层 chip_demo_gpio.c 提供 led_operations 结构体来操作硬件。
    • 下层的代码分为 2 个:chip_demo_gpio.c 实现通用的 GPIO 操作,
    • board_A_led.c 指定使用哪个 GPIO,即“资源”

你可能感兴趣的:(架构,stm32,嵌入式硬件)