韦东山嵌入式linux系列-LED 驱动程序框架

1 回顾字符设备驱动程序框架

图中驱动层访问硬件外设寄存器依靠的是 ioremap 函数去映射到寄存器地址,然后开始控制寄存器。

那么该如何编写驱动程序?

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

应用程序调用open等函数最简单的方法是驱动层也提供对应的drv_open,应用程序调用read,驱动层也提供对应的drv_read等等。需要写出驱动层的函数,为了便于管理,将这些函数放到file_operations结构体中,即第一步定义对应的file_operations结构体,并实现对应的open等程序(第一步);实现完成之后,将file_operations结构体通过register_chrdev注册到内核(第二步);然后通过入口函数调用注册函数(chrdev),安装驱动程序的时候,内核会调用入口函数,完成file_operations结构体的注册(第三步);有入口函数就有出口函数(第四步)。对于字符设备驱动程序而言,file_operations结构体是核心,每一个驱动程序都对应file_operations结构体,内核中有众多file_operations结构体,怎么才能找到对应的结构体呢?

应用程序要访问驱动程序,需要打开一个设备结点,里面有主设备号,根据设备结点的主设备号在内核中找到对应的file_operations结构体。注册结构体时足以提供主设备号,可以让内核分配;最后就是完善信息,创建类,创建设备。

2 对于 LED 驱动,我们想要什么样的接口

在open函数中配置LED的引脚为输出,在write函数中确定LED和引脚电平。

3 LED 驱动能支持多个板子的基础: 分层思想

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

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

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

有初始化函数、有控制函数,每个单板相关的 board_X.c 实现自己的 led_operations 结构体,供上层的 leddrv.c 调用

4 写代码

led.drv.c

/*************************************************************************
 > File Name: led.drv.c
 > Author: Winter
 > Created Time: Sun 07 Jul 2024 12:35:19 AM EDT
 ************************************************************************/

#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_operations.h"

#define LED_NUM 2


// 1确定主设备号,也可以让内核分配
static int major = 0;				// 让内核分配
static struct class *led_class;
struct led_operations* p_led_operations;





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

// 3 实现对应的 drv_open/drv_read/drv_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* node;
	int minor;
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	// 把用户区的数据buf拷贝到内核区status,即向写到内核status中写数据
	err = copy_from_user(&status, buf, 1);
	// 根据次设备号和status控制LED
	node = file_inode(file);
	minor = iminor(node);
	p_led_operations->ctl(minor, status);

	return 1;
}

static int led_drv_open (struct inode *node, struct file *file)
{
	int minor;
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	// 得到次设备号
	minor = iminor(node);
	
	// 根据次设备号初始化LED
	p_led_operations->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 结构体告诉内核: register_chrdev
// 5谁来注册驱动程序啊?得有一个入口函数:安装驱动程序时,就会去调用这个入口函数
static int __init led_init(void)
{
	int err, i;	
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	// 注册led_drv,返回主设备号
	major = register_chrdev(0, "winter_led", &led_drv);  /* /dev/led */
	// 创建class
	led_class = class_create(THIS_MODULE, "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_class");
		return -1;
	}
	// 创建device
	// 根据次设备号访问多个LED
//	device_create(led_class, NULL, MKDEV(major, 0), NULL, "winter_led0"); /* /dev/winter_led0 */
//	device_create(led_class, NULL, MKDEV(major, 1), NULL, "winter_led1"); /* /dev/winter_led1 */
	for (i = 0; i < LED_NUM; i++)
	{
		device_create(led_class, NULL, MKDEV(major, i), NULL, "winter_led%d", i);
	}

	// 入口函数获得结构体指针
	p_led_operations = get_board_led_operations();
	
	return 0;
}



// 6有入口函数就应该有出口函数:卸载驱动程序时,出口函数调用unregister_chrdev
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));
	}
	class_destroy(led_class);
	// 卸载
	unregister_chrdev(major, "winter_led");
}


// 7其他完善:提供设备信息,自动创建设备节点: class_create,device_create
module_init(led_init);
module_exit(led_exit);

MODULE_LICENSE("GPL");

board_demo.c

#include <linux/gfp.h>
#include "led_operations.h"

// init函数
static int board_demo_led_init(int which)
{
	printk("%s %s line %d, led %d\n", __FILE__, __FUNCTION__, __LINE__, which);
	return 0;
}

// ctl函数
static int board_demo_led_ctl(int which, char status)
{
	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_operations = {
	.init = board_demo_led_init,
	.ctl = board_demo_led_ctl,
};

// 返回结构体
struct led_operations* get_board_led_operations(void)
{
	return &board_demo_led_operations;
}


led_operations.h

#ifndef LED_OPERATIONS_H
#define LED_OPERATIONS_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_operations(void);


#endif

led_drv_test.c

/*************************************************************************
 > File Name: hello_test.c
 > Author: Winter
 > Created Time: Sun 07 Jul 2024 01:39:39 AM EDT
 ************************************************************************/

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

/*
 * ./led_drv  /dev/winter_led0 on
 * ./led_drv  /dev/winter_led0 off
 */
int main(int argc, char **argv)
{
	int fd;
	char status;

	
	/* 1. 判断参数 */
	if (argc < 2) 
	{
		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;
	}
	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_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 led_drv_test led_drv_test.c 

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

# 参考内核源码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
winter_led-y := led_drv.o board_demo.o
obj-m	+= winter_led.o

执行

make

5 测试

在开发板挂载 Ubuntu 的NFS目录

mount -t nfs -o nolock,vers=3 192.168.5.11:/home/book/nfs_rootfs/ /mnt

安装驱动

insmod winter_led.ko

执行打开灯

./led_drv_test /dev/winter_led0 on
./led_drv_test /dev/winter_led0 off

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-12 04:56:05       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-12 04:56:05       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-12 04:56:05       57 阅读
  4. Python语言-面向对象

    2024-07-12 04:56:05       68 阅读

热门阅读

  1. 【LeetCode】633. 平方数之和

    2024-07-12 04:56:05       19 阅读
  2. 深度优先算法-DFS(算法篇)

    2024-07-12 04:56:05       23 阅读
  3. C++ override关键字

    2024-07-12 04:56:05       23 阅读
  4. LeetCode 2974.最小数字游戏:排序+交换奇偶位

    2024-07-12 04:56:05       29 阅读
  5. 实现前端用户密码重置功能(有源码)

    2024-07-12 04:56:05       27 阅读
  6. [XCUITest] 处理iOS权限点击授权 有哪些权限?

    2024-07-12 04:56:05       22 阅读
  7. 周鸿祎关于2024年AI看法

    2024-07-12 04:56:05       18 阅读
  8. HJ1 字符串最后一个单词的长度

    2024-07-12 04:56:05       22 阅读
  9. 单元测试和集成测试

    2024-07-12 04:56:05       22 阅读
  10. shift 命令学习

    2024-07-12 04:56:05       24 阅读
  11. FPGA(1)--什么是布局与布线

    2024-07-12 04:56:05       24 阅读
  12. Django ORM中的F 对象

    2024-07-12 04:56:05       21 阅读