[Linux_IMX6ULL驱动开发]-基础驱动

 驱动的含义

如何理解嵌入式的驱动呢,我个人认为,驱动就是嵌入式上层应用操控底层硬件的桥梁。因为上层应用是在用户态,是无法直接操控底层的硬件的。我们需要利用系统调用(open、read、write等),进入内核态,通过打开对应的设备节点,通过read、write等通过编写的驱动函数来操控设备节点。

如何编写驱动

总的来说,驱动编写的大体步骤如下所示:

1、确定驱动的主设备号

2、定义自己的file_operation结构体,这个结构体的成员包含了很多的函数指针

3、我们需要在驱动文件中实现对应的函数,传入结构体中

4、编写一个驱动入口函数(对应的,也需要一个驱动卸载函数)

5、在驱动入口函数中,把file_operation结构体注册到内核当中、创建节点(class)、创建设备(相应的在驱动卸载函数中定义结构体从内核中卸载、节点、设备的卸载方法)

6、使用如下两个宏分别修饰入口函数和出口函数

7、使用 MODULE_LICENSE("GPL"); 遵守GPL协议,否则无法使用

基于如上步骤,我们进行以下操作

首先,我们需要三个文件,一个作为底层驱动文件,一个是上层APP文件,一个是Makefile

驱动文件hello_driver.c
上层应用文件hello_drv.c
Makefile

刚开始我们可能不知道到底要包含什么头文件,我们可以学习Linux内核中的文件来进行参考,我们可以打开 Linux-4.9.88\drivers\char\misc.c ,把里面的头文件拷贝过来使用。

首先我们需要定义一个全局变量作为驱动的设备号,然后定义一个file_operation结构体。需要注意,这两个变量都是全局变量,因为需要被多个函数使用。

file_operation结构体需要多个函数指针成员,在这里,我们定义四个函数,把函数指针赋值给结构体成员

其中需要注意的是,驱动和上层直接读写是需要通过两个函数来进行的,分别是 copy_to_user 和  copy_from_user,前者用于驱动中读的驱动函数,后者用于驱动中写的函数

同时,结构体成员函数的形参,返回值必须严格遵守一样的原则,否则会报错

#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>/* 确定主设备号 */
static int major = 0;
/* 缓存数组 */
static char kernal_buf[1024];/* 数据超过1024,限制为1024 */
#define data_num(a,b) ( (a) < (b) ? (a) : (b) )/* 定义函数入口地址 */
static int hello_drv_open (struct inode *node, struct file *file)
{printk("%s %s %d \n",__FILE__,__FUNCTION__,__LINE__);return 0;
}static ssize_t hello_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
{int return_size;printk("%s %s %d \n",__FILE__,__FUNCTION__,__LINE__);return_size = copy_to_user(buf, kernal_buf, data_num(1024,size));return return_size;}
static ssize_t hello_drv_write (struct file *file, const char __user *buf, size_t size, loff_t *offset)
{int return_size;printk("%s %s %d \n",__FILE__,__FUNCTION__,__LINE__);return_size = copy_from_user(kernal_buf, buf, data_num(1024,size));return return_size;}
static int hello_drv_rease (struct inode *node, struct file *file)
{printk("%s %s %d \n",__FILE__,__FUNCTION__,__LINE__);return 0;
}/* 定义文件结构体读,写,打开,卸载
*/
static struct file_operations hello_driver = {.owner = THIS_MODULE,.open = hello_drv_open,.read = hello_drv_read,.write = hello_drv_write,.release = hello_drv_rease,
};

当我们为file_operation结构体的成员指定了对应的函数指针后,我们需要指定一个入口函数以及一个出口函数,并且在入口函数中,把file_operation注册到内核、节点的创建和设备的创建,在出口函数中完成上述三个的卸载(节点需要另外创建一个全局变量,struct class类型)

/* 节点的定义 全局变量 */
static struct class *hello_class;/* 入口函数 */
static int __init hello_init(void)
{int err;printk("%s %s %d \n",__FILE__,__FUNCTION__,__LINE__);/* 注册结构体到内核后,返回主设备号 */major = register_chrdev(0, "hello", &hello_driver);//创建节点 /dev/hellohello_class = class_create(THIS_MODULE, "hello_class");err = PTR_ERR(hello_class);if (IS_ERR(hello_class)){printk("%s %s %d \n",__FILE__,__FUNCTION__,__LINE__);/* 创建失败的话摧毁内核中的hello结构体 */unregister_chrdev( major, "hello");return -1;}/* 创建了节点后,需要创建设备 */	device_create(hello_class, NULL, MKDEV(major, 0), NULL, "hello"); return 1;
}/* 出口函数 */
static void __exit hello_exit(void)
{printk("%s %s %d \n",__FILE__,__FUNCTION__,__LINE__);/* 把device卸载 */device_destroy(hello_class, MKDEV(major, 0));/* 把class卸载 */class_destroy(hello_class);/* 把file_operation从内核中卸载 */unregister_chrdev( major, "hello");}

当写好了入口函数和出口函数后,还需通过两个宏声明,否则系统不知道这两个函数分别是入口函数和出口函数

/* 需要用某些宏表示上述两个函数分别是入口函数和出口函数 */
module_init(hello_init);
module_exit(hello_exit);

这就是一个驱动的具体框架了,整体完整代码如下

#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>/*  流程1.file_operation结构体,实现内部对应函数2.注册结构体到内核,同时使用宏声明入口和出口函数,指引进入3.创建节点,让上层应用函数可以打开 /dev/...,节点class创建完毕后,创建deviceclass提供了一种更高层次的设备抽象,而device则代表了具体的硬件设备*//* 确定主设备号 */
static int major = 0;
/* 缓存数组 */
static char kernal_buf[1024];
/* 节点的定义 */
static struct class *hello_class;/* 读多少的宏定义 */
#define data_num(a,b) ( (a) < (b) ? (a) : (b) )/* 定义函数入口地址 */
static int hello_drv_open (struct inode *node, struct file *file)
{printk("%s %s %d \n",__FILE__,__FUNCTION__,__LINE__);return 0;
}static ssize_t hello_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
{int return_size;printk("%s %s %d \n",__FILE__,__FUNCTION__,__LINE__);return_size = copy_to_user(buf, kernal_buf, data_num(1024,size));return return_size;}
static ssize_t hello_drv_write (struct file *file, const char __user *buf, size_t size, loff_t *offset)
{int return_size;printk("%s %s %d \n",__FILE__,__FUNCTION__,__LINE__);return_size = copy_from_user(kernal_buf, buf, data_num(1024,size));return return_size;}
static int hello_drv_rease (struct inode *node, struct file *file)
{printk("%s %s %d \n",__FILE__,__FUNCTION__,__LINE__);return 0;
}/* 定义文件结构体读,写,打开,卸载
*/
static struct file_operations hello_driver = {.owner = THIS_MODULE,.open = hello_drv_open,.read = hello_drv_read,.write = hello_drv_write,.release = hello_drv_rease,
};/* 把结构体注册到内核为了能够把该结构体注册到内核需要init函数
*/
static int __init hello_init(void)
{int err;printk("%s %s %d \n",__FILE__,__FUNCTION__,__LINE__);/* 注册结构体到内核后,返回主设备号 */major = register_chrdev(0, "hello", &hello_driver);//创建节点 /dev/hellohello_class = class_create(THIS_MODULE, "hello_class");err = PTR_ERR(hello_class);if (IS_ERR(hello_class)){printk("%s %s %d \n",__FILE__,__FUNCTION__,__LINE__);/* 创建失败的话摧毁内核中的hello结构体 */unregister_chrdev( major, "hello");return -1;}/* 创建了节点后,需要创建设备 */	device_create(hello_class, NULL, MKDEV(major, 0), NULL, "hello"); return 1;
}/* 有注册函数就有卸载函数 */
static void __exit hello_exit(void)
{printk("%s %s %d \n",__FILE__,__FUNCTION__,__LINE__);/* 把device卸载 */device_destroy(hello_class, MKDEV(major, 0));/* 把class卸载 */class_destroy(hello_class);/* 把file_operation从内核中卸载 */unregister_chrdev( major, "hello");}/* 需要用某些宏表示上述两个函数分别是入口函数和出口函数 */
module_init(hello_init);
module_exit(hello_exit);/* 遵循GPL协议 */
MODULE_LICENSE("GPL");

如上,驱动程序hello_driver.c就完成了 ,在这里我们通过上层应用来打开驱动节点,然后往里面写入数据,然后在从里面读取数据。应用的代码如下


#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>/** ./hello_drv_test -w abc* ./hello_drv_test -r*/
int main(int argc, char **argv)
{int fd;char buf[1024];int len;/* 1. 判断参数 */if (argc < 2) {printf("Usage: %s -w <string>\n", argv[0]);printf("       %s -r\n", argv[0]);return -1;}/* 2. 打开文件 */fd = open("/dev/hello", O_RDWR);if (fd == -1){printf("can not open file /dev/hello\n");return -1;}/* 3. 写文件或读文件 */if ((0 == strcmp(argv[1], "-w")) && (argc == 3)){len = strlen(argv[2]) + 1;len = len < 1024 ? len : 1024;write(fd, argv[2], len);}else{len = read(fd, buf, 1024);		buf[1023] = '\0';printf("APP read : %s\n", buf);}close(fd);return 0;
}

 同时,我们还需要编写Makefile,Makefile和具体的解析如下所示

1、KERN_DIR = /home/book/100ask_imx6ull-sdk/Linux-4.9.88

        定义了KERN_DIR变量,指向了内核的目录

2、all:

        标记了Makefile的第一个目标,执行make的时候执行

3、make -C $(KERN_DIR) M=`pwd` modules 

        -C $(KERN_DIR):这是make的一个选项,用于改变到另一个目录并读取那里的Makefile。这告诉make工具首先进入这个目录,并在那里查找Makefile。

        M=`pwd` modules:M的意思是指定模块源代码的的位置,当指定了module作为目标后,就是告诉系统想要构建内核模块。内核构建系统会查找当前目录(由M变量指定)中的模块源代码,并生成相应的模块文件(通常是.ko文件)。

4、$(CROSS_COMPILE)gcc -o hello_drv_test hello_drv_test.c 

        CROSS_COMPILE是环境变量,这列的意思是使用交叉编译器编译hello_drv_test.c 生成hello_drv_test.o。如果不存在交叉编译器会使用gcc

5、obj-m    += hello_driver.o

        这行告诉内核构建系统hello_driver.o是一个要构建的对象文件(即内核模块)


KERN_DIR = /home/book/100ask_imx6ull-sdk/Linux-4.9.88all:make -C $(KERN_DIR) M=`pwd` modules $(CROSS_COMPILE)gcc -o hello_drv_test hello_drv_test.c clean:make -C $(KERN_DIR) M=`pwd` modules cleanrm -rf modules.orderrm -f hello_drv_testobj-m	+= hello_driver.o

驱动的安装、卸载和现象

当我们在服务器上面编译完成后,会生成如下几个文件

我们通过挂载,把这两个文件挂载到开发板上

当前挂载的目录下存在 hello_driver.ko  hello_drv_test这两个文件。

首先,我们需要安装驱动,使用 insmod + 驱动名 ,来安装驱动

(lsmod也可以查看安装的驱动程序)

如上图,驱动程序成功的安装了

在这里我们使用应用文件写入驱动程序,再从中读出

当我们不使用驱动的时候,使用 rmmod+驱动名 卸载

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

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

相关文章

synchronized的使用方式

1、修饰实例方法 public synchronized void A(){} 这个时候锁的是当前的实例对象。多线程操作同一个实例的实例方法时&#xff0c;才会阻塞。 2、修饰静态方法 public synchronized static void A(){}这个时候锁的是整个类下所有的实例对象&#xff0c;静态方法是存在于方法…

自然语言处理:大模型LLM论文整理

LLMs 九层妖塔 地址&#xff1a;https://github.com/km1994/LLMsNineStoryDemonTower LLMs 千面郎君 地址&#xff1a;https://github.com/km1994/LLMs_interview_notes LLMs 论文学习笔记&#xff1a;https://gitee.com/km601/llms_paper NLP 百面百搭 地址&#xff1a;htt…

os模块篇(三)

文章目录 os.putenv(key, value, /)os.setegid(egid, /)os.seteuid(euid, /)os.setgid(gid, /)os.setgroups(groups, /)os.setns(fd, nstype0)os.setpgrp()os.setpgid(pid, pgrp, /)os.setpriority(which, who, priority) os.putenv(key, value, /) os.putenv(key, value) 是 …

看懂Spring和Spring Boot的区别与联系

一、概述 Spring和Spring Boot是Java应用程序开发中最受欢迎的框架之一。Spring提供了一个全功能的、开放式源代码的Java应用程序框架&#xff0c;可以帮助开发人员在基于Java的代码中快速编写而不必关心底层技术实现。而Spring Boot则是基于Spring框架之上的快速开发框架&…

Java编程实战:疫情物资分配系统的设计与实现

✍✍计算机编程指导师 ⭐⭐个人介绍&#xff1a;自己非常喜欢研究技术问题&#xff01;专业做Java、Python、微信小程序、安卓、大数据、爬虫、Golang、大屏等实战项目。 ⛽⛽实战项目&#xff1a;有源码或者技术上的问题欢迎在评论区一起讨论交流&#xff01; ⚡⚡ Java实战 |…

小程序运营秘籍,5步打造爆款!

在数字化的浪潮中&#xff0c;小程序以其轻便、快捷的特性迅速走红&#xff0c;成为连接用户与服务的重要桥梁。然而&#xff0c;一个成功的小程序离不开精细化的运营策略。如何做运营&#xff0c;才能让小程序在激烈的市场竞争中脱颖而出&#xff1f;以下是几个关键步骤的解析…

RocketMQ笔记(三)SpringBoot整合RocketMQ发送单向消息

目录 一、简介1.1、特点1.2、场景 二、Maven依赖三、application配置四、生产者4.1、测试类4.2、运行结果 一、简介 RocketMQ 提供了一种单向发送消息的方法&#xff0c;在这种模式下&#xff0c;生产者只负责尽快地发送消息&#xff0c;而不需要关心消息是否被Broker接收&…

Opencv相机的读取、拍摄、保存

Opencv相机的读取、拍摄、保存 以下是OpenCV相机的读取、拍摄和保存的基本步骤&#xff1a; 1、读取相机数据 要从相机中读取数据&#xff0c;可以使用cv2.VideoCapture()函数。该函数接受两个参数&#xff0c;即相机的索引和视频捕获模式。例如&#xff0c;要从名为“camera…

Qt 富文本处理 (字体颜色大小加粗等)

Qt中支持HTML的控件有textEdit 、label 、textBrowser 。 接口&#xff1a;setHtml("Qt"); toHtml(). 文本样式设置 : 可分字设置 &#xff0c;主要使用QTextCharFormat类进行文本样式设置。 示例&#xff1a; QTextCharFormat fmt; //粗体 fmt.setFontWeight…

在 IntelliJ IDEA 中部署 Java 项目的详细步骤

在进行 Java 项目的部署前&#xff0c;确保你已经完成了以下准备工作&#xff1a; 项目开发完成&#xff1a;确保 Java 项目已经经过完整的开发&#xff0c;并且通过了相关的测试。依赖管理&#xff1a;确认项目中所有的依赖项都已经正确配置&#xff0c;并且可以被正常引入。…

蓝桥杯备考随手记: 求最大公约数

1. 最大公约数的定义 最大公约数指的是两个或多个整数中能同时整除它们的最大正整数。最大公约数也可以被称为最大公因数、最大公因子或者简称为GCD&#xff08;Greatest Common Divisor&#xff09;。例如&#xff0c;对于整数12和18来说&#xff0c;它们的最大公约数是6。 …

查找算法及查找常用数据结构总结

1.顺序表查找 基本方法&#xff1a; 设查找表以一维数组来存储&#xff0c;要求在此表中查找出关键字的值为x的元素的位置&#xff0c;若查找成功&#xff0c;则返回其位置&#xff08;即下标&#xff09;&#xff0c;否则&#xff0c;返回一个表示元素不存在的下标&#xff0…

VMware虚拟机共享主机v2rayN

目录 &#x1f33c;前言 &#x1f33c;解释 &#x1f6a9;操作 1&#xff09;VMware -- 虚拟网络编辑器 2&#xff09;VMware -- 网络适配器 3&#xff09;主机 IP 地址 4&#xff09;v2rayN 代理端口 5&#xff09;VMware -- 网络代理(Network proxy) &#x1f382;结…

红黑树介绍及插入操作的实现

&#x1f389;个人名片&#xff1a; &#x1f43c;作者简介&#xff1a;一名乐于分享在学习道路上收获的大二在校生 &#x1f648;个人主页&#x1f389;&#xff1a;GOTXX &#x1f43c;个人WeChat&#xff1a;ILXOXVJE &#x1f43c;本文由GOTXX原创&#xff0c;首发CSDN&…

轻松集成所有大模型——一站式大模型应用开发框架Promptulate

&#x1f496; 作者简介&#xff1a;大家好&#xff0c;我是Zeeland&#xff0c;大模型创业、LangChain Top Contributor、算法工程师、Promptulate founder、Python开发者。&#x1f4dd; CSDN主页&#xff1a;Zeeland&#x1f525;&#x1f4e3; 我的博客&#xff1a;Zeeland…

windows平台虚拟机安装

windows平台虚拟机安装 1. 安装VMwareWorkstationPro 1.1 软件下载 官网下载 官网 百度网盘下载 版本 VMwareWorkstationPro16 链接&#xff1a;https://pan.baidu.com/s/1LidMxoM9e4a4CANixyRoyg?pwd1157 提取码&#xff1a;1157 1.2 软件安装 软件安装注意事项 软件…

代码学习第32天---动态规划

随想录日记part32 t i m e &#xff1a; time&#xff1a; time&#xff1a; 2024.03.30 主要内容&#xff1a;今天开始要学习动态规划的相关知识了&#xff0c;今天的内容主要涉及两个方面&#xff1a; 不同路径 &#xff1b; 不同路径 II。 62.不同路径 63. 不同路径 II 动态…

接口和抽象类的区别【大白话Java面试题】

接口和抽象类的区别 大白话回答 1、接口是行为的抽象&#xff0c;是一种行为的规范&#xff0c;接口是like a 的关系&#xff1b;抽象是对类的抽象&#xff0c;是一种模板设计&#xff0c;抽象类是is a 的关系。 2、接口没有构造方法&#xff0c;而抽象类有构造方法&#xff0…

爬虫框架Scrapy从创建到使用

scrapy框架安装命令 1.需要安装python 链接: link 2.scrapy安装命令 python -m pip install Scrapy3. 创建爬虫项目 scrapy startproject 项目名称4.创建爬虫文件 scrapy genspider 爬虫名 域名5.爬虫运行 scrapy crawl 爬虫名scrapy 项目目录介绍 PaC #项目文件名称P…

Canvas实现圆点动画

示例效果图&#xff1a; 话不多说直接上代码&#xff1a; <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><t…