LED驱动程序框架

news2024/9/26 3:31:00

1. 字符设备驱动程序框架

在这里插入图片描述

2. 基于分层思想的LED驱动

2.1 把驱动拆分为通用的框架和具体的硬件操作

把驱动拆分为通用的框架(leddrv.c)、具体的硬件操作(board_X.c):
如图:
在这里插入图片描述

以面向对象的思想,改进代码

抽象出一个结构体:
在这里插入图片描述
每个单板相关的board_X.c实现自己的led_operations结构体,供上层的leddrv.c调用
在这里插入图片描述

2.2 驱动框架程序

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

#ifndef _LED_OPR_H
#define _LED_OPR_H

//把LED的操作抽象为这个结构体
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_opr.h的实现

#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"

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

/* 控制LED, which-哪个LED, status:1-亮,0-灭 */
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;
}

//含有led相关操作函数的结构体
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

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


module_init(led_init);
module_exit(led_exit);

MODULE_LICENSE("GPL");

测试程序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;
}

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_roc-rk3399-pc/linux-4.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
100ask_led-y := leddrv.o board_demo.o
obj-m	+= 100ask_led.o

在ubuntu系统上使用交叉编译工具链编译之后,将.ko文件和测试程序拷贝到网络文件系统中

cp 100ask_led.ko ledtest /home/book/nfs_rootfs/

启动开发板,将虚拟机中ubuntu系统的网络文件系统目录挂载到开发板上,192.168.3.54为ubuntu系统的ip

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

在 /mnt 目录下,装载驱动程序:

insmod 100ask_led.ko

在 /mnt 目录下,查看是否有该驱动程序:

cat  /proc/devices

在 /mnt 目录下,查看内核中加载的驱动程序:

lsmod
#或
lsmod | grep 100ask_led

在 /mnt 目录下,查看是否有主设备节点

ls  /dev/100ask_led*  -l

在 /mnt 目录下,执行测试程序:

./ledtest /dev/100ask_led0 on

在 /mnt 目录下,卸载驱动程序

rmmod 100ask_led

查看打印信息,在 /mnt 目录下:

dmesg

3. 具体单板

IMX6ULL开发板对应的board_demo.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 <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                               ;

/* 初始化LED, which-哪个LED */   
static int board_demo_led_init (int which){
    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;
}

在芯片手册中确定的寄存器地址被称为物理地址,在Linux内核中无法直接使用。需要使用内核提供的ioremap把物理地址映射为虚拟地址,使用虚拟地址。

ioremap函数的使用:
原型:

void __

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/504774.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

数据库管理-第七十三期 最近(20230509)

数据库管理 2023-05-09 第七十三期 最近1 证书2 EMCC 13.5.0.143 破百总结 第七十三期 最近 五一前后&#xff0c;除了X8那台的故障以外&#xff0c;还是做了或者探索了一些其他的东西。 1 证书 在五一假期的最后一天&#xff0c;还是在家通过线上的方式通过了1Z0-902&#…

Springboot +Flowable,三种常见网关的使用(排他、并行、包容网关)(一)

一.简介 Flowable 中常用的网关主要有三种类型&#xff0c;分别是&#xff1a; 排他网关并行网关包容网关 下面来说下这三种的网关的概念和用法。 二.排他网关 排他网关&#xff0c;也叫互斥网关&#xff0c;截图如下&#xff1a; 排他网关有一个入口&#xff0c;多个有效…

亚马逊云科技发力医疗与生命科学行业,加速数字化创新

2023年4月27日&#xff0c;亚马逊云科技医疗与生命科学行业峰会召开&#xff0c;会议汇聚了业界专家和思想领袖&#xff0c;共同探讨行业数字化转型和创新之道。作为全球医疗及生命科学行业云计算引领者&#xff0c;亚马逊云科技将围绕数据、算力和行业用户体验三大需求发力&am…

MySQL百万数据深度分页优化思路分析

业务场景 一般在项目开发中会有很多的统计数据需要进行上报分析&#xff0c;一般在分析过后会在后台展示出来给运营和产品进行分页查看&#xff0c;最常见的一种就是根据日期进行筛选。这种统计数据随着时间的推移数据量会慢慢的变大&#xff0c;达到百万、千万条数据只是时间…

UWA发布 | Unity手游性能蓝皮书

最新2022年度Unity手游蓝皮书出炉&#xff01;此次发布分析了2022年1月至2023年3月期间&#xff0c;游戏行业使用Unity引擎进行手游开发过程中及游戏上线后的性能表现&#xff0c;从测试机型分布、引擎各模块开销、内存占用等方面进行汇总分析&#xff0c;反映了Unity手游行业的…

Flink dataStream,如何开窗,如何进行窗口内计算

目录 开窗方式 windowAll() window() 窗口类型 基于时间 基于数量 开窗后的处理函数 全量聚合函数&#xff08;也叫窗口函数&#xff09; 增量聚合函数 增量聚合函数携带一个全量聚合函数 开窗方式 windowAll() 对于没有keyBy的数据流 window() 对于KeyBy后的数据…

交工技术文档表格-SH3503-2001

(阀门试验记录)(管道补偿器安装记录)(管道组成件校验性检查记录)(SHA级管道管螺纹、密封面加工记录)(高压、SHA级管道弯管加工记录)(管道静电接地测试记录)管道系统安装检查与压力试验记录)管道系统泄露性与真空试验记录)(管道吹洗、清洗脱脂记录)(给排水压力管道强度及严密试验…

685页40万字某省市场监管智慧应用一体化项目(word可编辑)

1.2.3.1 数字XX公共能力建设现状 1.2.3.1.1 数字XX通用基础应用平台现状 通用基础应用平台提供具有共性特征的跨部门、跨层级业务应用&#xff0c;与本项目有关的平台包括某省网上办事大厅、某省政务服务 APP 统一平台&#xff08;X政通 APP&#xff09;、某省公共信用信息平…

一次SQL的完整处理流程

流程&#xff1a; 1. 客户端到连接器是通过TCP/IP SSL通信协议连接的&#xff0c; 2.连接器验证MySQL权限信息是否正常&#xff0c;连接量是否正常&#xff0c;长时间没有连接服务器会自动断开等等 &#xff1b; 3. 然后到解析器&#xff0c;通过客户端发过来的sql进行语法解析…

电力系统储能调峰、调频模型研究(Matlab代码实现)

&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5; &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜密&#xff0c;逻辑清晰&#xff0c;为了方便读者。 ⛳️座右铭&a…

FS2114恒流模式的PWM升压IC,内置过温、关断、欠压、过流保护

FS2114是升压DC-DC转换器。其内置0.2Ω功率MOSFET的PWM电路&#xff0c;使该稳压器具有高效率。内部补偿网络还可以程度地 减少了6个外部元件的数量。 0.6V精密基准电压&#xff0c;内部软启动功能可以减低浪涌电流。 FS2114采用SOT23-6L封装&#xff0c;为应用节省空 间PCB。…

手机录屏怎么操作?有哪些好用的方法

在现代科技的时代&#xff0c;手机录屏已经成为了常见的操作。这项技术允许我们在手机上录制视频并分享给他人。但是&#xff0c;很多人可能并不知道如何进行手机录屏。下面我们将介绍手机录屏的操作方法和一些值得推荐的工具。 手机录屏操作方法 对于iOS用户&#xff0c;可以…

Java设计模式-适配器模式

适配器模式&#xff08;Adapter Pattern&#xff09;是一种常见的设计模式&#xff0c;它主要用于在不改变现有系统结构的情况下&#xff0c;将一个类的接口转换成客户端所期望的另一个接口。在本文中&#xff0c;我们将介绍适配器模式的基本概念、实现方法以及优缺点&#xff…

FS4056是一款完整的单节锂离子电池采用恒定电流/恒定电压线性充电器IC

FS4056是一款完整的单节锂离子电池采用恒定电流/恒定电压线性充电器IC。其底部带有散热片的 ESOP8/DIP8 封装与较少的外部元件数目使得 FS4056成为便携式应用的理想选择。FS4056可以适合 USB 电源和适配器电源工作。   由于采用了内部 PMOSFET 架构&#xff0c;加上防倒充电路…

CM201-1-YS_当贝纯净桌面-线刷固件包

CM201-1-YS_当贝纯净桌面-线刷固件包-内有教程 特点&#xff1a; 1、适用于对应型号的电视盒子刷机&#xff1b; 2、开放原厂固件屏蔽的市场安装和u盘安装apk&#xff1b; 3、修改dns&#xff0c;三网通用&#xff1b; 4、大量精简内置的没用的软件&#xff0c;运行速度提…

连接型CRM助力医疗企业把“成本中心”变成“利润中心”

在市场竞争日益加剧的情形下&#xff0c;企业获客成本大幅上涨&#xff0c;存量客户的维护和开发开始被重视&#xff0c;售后服务部门的职责在企业中发挥的价值越来越大。因为企业售后服务不仅能帮助客户解决问题的部门&#xff0c;还是客户与企业沟通的桥梁&#xff0c;将客户…

Linux——进程间通信(System V共享内存)

目录 共享内存示意图(理解共享内存的关键) shmget函数 第一个参数 ftok函数 使用ftok打印key值 第二个参数 第三个参数 返回值 打印shmget 再谈key值 举例理解key值 共享内存的过程 创建共享内存 关联共享内存 去除关联 ​编辑 使用共享内存通信 删除共享内存…

关于财会〔2020〕6 号文件解读,本地化部署+区块链或成为新趋势?

2020年3月&#xff0c;财政部、国家档案局发布了《关于规范电子会计凭证报销入账归档的通知》财会〔2020〕6 号&#xff08;以下简称“通知”&#xff09;&#xff0c;对于电子会计凭证的范围、法律效力、适用条件都做出了进一步规范。这可以看作是财会档案管理由纸质化迈向全面…

每日一博 - 闲聊“突发流量”的应急之道

文章目录 概述思路 概述 面对“突发流量”的情况,我会采取以下应急措施: 扩容现有资源。这是最直接和最常用的方法。可以通过增加CPU、内存、节点等来扩容。典型案例是双11等大促期间,阿里会大规模扩容幕布等系统以应对流量激增。横向扩展,增加更多服务器或节点。通过增加服务…

【Unity-UGUI控件全面解析】| Scrollbar 滚动条组件详解

🎬【Unity-UGUI控件全面解析】| Scrollbar 滚动条组件详解一、组件介绍二、组件属性面板三、代码操作组件四、组件常用方法示例4.1 监听开关事件4.2 充当 进度条/血条 使用💯总结🎬 博客主页:https://xiaoy.blog.csdn.net 🎥 本文由 呆呆敲代码的小Y 原创,首发于 CSD…