【IMX6ULL驱动开发学习】01.编写第一个hello驱动+自动创建设备节点(不涉及硬件操作)

news2024/10/6 6:05:32

目录

一、驱动程序编写流程

二、代码编写

2.1 驱动程序hello_drv.c

2.2 测试程序

2.3 编写驱动程序的Makefile

三、上机实验

3.1 NFS 挂载

3.2 测试示例


一、驱动程序编写流程

  • 构造file_operations结构体

  • 在里面填充open/read/write/ioctl成员

  • 注册file_operations结构体 int major = register_chrdev(0, "name", &fops);

  • 入口函数:调用regiister_chrdev

  • 出口函数:调用unregiister_chrdev

  • 辅助信息: class_create/class_destroy   device_create/device_destroy

总结:应用程序调用open/read/write/ioctl,驱动程序就给你提供对应的open/read/write/ioctl,只不过驱动程序的这些函数为了便于管理,故把函数放在file_operations结构体里面,通过 register_chrdev函数把结构体告诉内核,并注册字符设备驱动程序。驱动程序里面有个入口函数,相当于main函数,是装载驱动程序时调用的函数,在入口函数中注册,把结构体放到chrdevs数组里面来,出口函数中反注册,就是把结构体拿掉,在卸载驱动程序时调用的函数。  

二、代码编写

2.1 驱动程序hello_drv.c

参考 driver/char 中的程序,包含头文件,写框架,传输数据:

  • 驱动中实现 open, read, write, release, APP 调用这些函数时,都打印内核信息
  • APP 调用 write 函数时,传入的数据保存在驱动中   
  • APP 调用 read 函数时,把驱动中保存的数据返回给 APP   
  • 需要注意的是,驱动程序和应用程序之间数据传递要使用copy_from_user(hello_buf, buf, len)和copy_to_user(buf, hello_buf, len)
  • class_create和device_create这两个函数为我们创建了设备节点、主次设备号等辅助信息就不用手动创建设备节点了 mknod /dev/xyz c 245 0

  • class_create(THIS_MODULE, "hello_class"),创建类:为这个模块创建类,类名叫hello_class

  • device_create(hello_class, NULL, MKDEV(major, 0), NULL, "hello"),在类下面创建设备信息:在hello_class下创建设备,没有父亲NULL,主次设备号,无私有数据NULL,格式hello根据这些信息,系统会为我们创建设备节点,设备节点名字是/dev/hello,和上面的类名无关

  • device_destroy(hello_class, MKDEV(major, 0))销毁hello_class类下面的这个设备(由主次设备号确定)

  • class_destroy(hello_class)销毁hello_class类

#include "asm/cacheflush.h"
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/miscdevice.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/mman.h>
#include <linux/random.h>
#include <linux/init.h>
#include <linux/raw.h>
#include <linux/tty.h>
#include <linux/capability.h>
#include <linux/ptrace.h>
#include <linux/device.h>
#include <linux/highmem.h>
#include <linux/backing-dev.h>
#include <linux/shmem_fs.h>
#include <linux/splice.h>
#include <linux/pfn.h>
#include <linux/export.h>
#include <linux/io.h>
#include <linux/uio.h>
#include <linux/uaccess.h>

static struct class *hello_class;
static int major;
static unsigned char hello_buf[100];

static int hello_open (struct inode *node, struct file *filp)
{
    printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
    return 0;
}

static ssize_t hello_read (struct file *filp, char __user *buf, size_t size, loff_t *offset)
{
    unsigned long len = size > 100 ? 100 : size;
    printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
    copy_to_user(buf, hello_buf, len);
    return len;
}

static ssize_t hello_write(struct file *filp, const char __user *buf, size_t size, loff_t *offset)
{
    unsigned long len = size > 100 ? 100 : size;
    printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
    copy_from_user(hello_buf, buf, len);
    return len;
}

static int hello_release (struct inode *node, struct file *filp)
{
    printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
    return 0;
}

/* 1. create file_operations */
static const struct file_operations hello_drv = {
    .owner      = THIS_MODULE,
	.read		= hello_read,
	.write		= hello_write,
	.open		= hello_open,
    .release    = hello_release,
};


/* 2. register_chrdev */

/* 3. entry function */
static int hello_init(void)
{
    major = register_chrdev(0, "100ask_hello", &hello_drv);
    
	hello_class = class_create(THIS_MODULE, "hello_class");
	if (IS_ERR(hello_class)) {
		printk("failed to allocate class\n");
		return PTR_ERR(hello_class);
	}

    device_create(hello_class, NULL, MKDEV(major, 0), NULL, "hello");  /* /dev/hello */
       
    return 0;
}


/* 4. exit function */
static void hello_exit(void)
{    
    
    device_destroy(hello_class, MKDEV(major, 0));
    class_destroy(hello_class);
    
    unregister_chrdev(major, "100ask_hello");
}


module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");

2.2 测试程序

测试程序要实现读、写功能:

./hello_test /dev/xxx abcdef     // 把字符串“abcdeft”发给驱动程序
./hello_test /dev/xxx            // 把驱动中保存的字符串读回来

hello_drv_test.c源码如下

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

/* 写: ./hello_test /dev/xxx 100ask
 * 读: ./hello_test /dev/xxx
 */
int main(int argc, char **argv)
{
    int fd;
    int len;
    char buf[100];

    if (argc < 2)
    {
        printf("Usage: \n");
        printf("%s <dev> [string]\n", argv[0]);
        return -1;
    }

    // open
    fd = open(argv[1], O_RDWR);
    if (fd < 0)
    {
        printf("can not open file %s\n", argv[1]);
        return -1;
    }

    if (argc == 3)
    {
        // write
        len = write(fd, argv[2], strlen(argv[2])+1);
        printf("write ret = %d\n", len);
    }
    else
    {
        // read
        len = read(fd, buf, 100);
        buf[99] = '\0';
        printf("read str : %s\n", buf);
    }

    // close
    close(fd);
    return 0;
}

2.3 编写驱动程序的Makefile

驱动程序中包含了很多头文件,这些头文件来自内核,不同的 ARM 板它的某些头文件可能不同。所以编译驱动程序时,需要指定板子所用的内核的源码路径。要编译哪个文件?这也需要指定,设置 obj-m 变量即可怎么把.c 文件编译为驱动程序.ko?这要借助内核的顶层 Makefile。

本驱动程序的 Makefile 内容如下:

KERN_DIR = /home/me/Linux-4.9.88 :指定内核目录

先设置好交叉编译工具链,编译好你的板子所用的内核,然后修改 Makefile指定内核源码路径,最后即可执行make命令编译驱动程序和测试程序。 

三、上机实验

3.1 NFS 挂载

我们是在 Ubuntu 中编译程序,但是需要在 ARM 板子上测试。所以需要把程序放到 ARM 板子上。启动单板后,可以通过 NFS 挂载 Ubuntu 的某个目录,访问该目录中的程序。

ifconfig eth0 192.168.5.9                    //静态配置开发板ip地址 
mount -t nfs -o nolock,vers=3 192.168.5.11:/home/book/nfs_rootfs /mnt //挂载到开发板上的mnt目录下
echo "7 4 1 7" > /proc/sys/kernel/printk     //打开内核打印信息

 

3.2 测试示例

首先在开发板的mnt目录下查看文件是否挂载成功,当前目录下以及有了Ubuntu编译好的驱动程序和测试文件

insmod hello_drv.ko   // 安装驱动程序
ls /dev/hello -l      // 驱动程序会生成设备节点
./hello_drv_test      // 查看测试程序的用法

 显示已载入系统的模块

查看测试程序用法,并写入字符串"abcdef"后读出,测试结果如下: 

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

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

相关文章

【计算机网络】TCP协议超详细讲解

文章目录 1. TCP简介2. TCP和UDP的区别3. TCP的报文格式4. 确认应答机制5. 超时重传6. 三次握手7. 为什么两次握手不行?8. 四次挥手9. 滑动窗口10. 流量控制11. 拥塞控制12. 延时应答13. 捎带应答14. 面向字节流15. TCP的连接异常处理 1. TCP简介 TCP协议广泛应用于可靠性要求…

P1398 [NOI2013] 书法家

题目描述 输入 #1 3 13 1 1 -1 -1 1 -1 1 1 1 -1 1 1 1 1 -1 1 -1 1 -1 1 -1 1 -1 -1 1 -1 1 -1 -1 1 1 -1 1 1 1 -1 1 1 1 输出 #1 24 输入 #2 3 13 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1…

线上电影购票选座H5小程序源码开发

搭建一个线上电影购票选座H5小程序源码需要一些基本的技术和步骤。以下是一个大致的搭建过程&#xff0c;可以参考&#xff1a; 1. 确定需求和功能&#xff1a;首先要明确你想要的电影购票选座H5小程序的需求和功能&#xff0c;例如用户登录注册、电影列表展示、选座购票、订单…

Nacos服务治理—负载均衡

引入负载均衡 在消费方引入负载均衡机制&#xff0c;同时简化获取服务提供者信息的流程 Spring Cloud引入组件LoadBalance实现负载均衡 添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web<…

代码随想录第42天 | 0-1背包问题、416. 分割等和子集

0-1背包问题 问题描述&#xff1a;有n件物品和一个最多能背重量为w 的背包。第i件物品的重量是weight[i]&#xff0c;得到的价值是value[i] 。每件物品只能用一次&#xff0c;求解将哪些物品装入背包里物品价值总和最大。 二维数组解法 动态规划五部曲&#xff1a; 确定dp数…

微信小程序--原生

1&#xff1a;数据绑定 1&#xff1a;数据绑定的基本原则 2&#xff1a;在data中定义页面的数据 3&#xff1a;Mustache语法 4&#xff1a;Mustache的应用场景 1&#xff1a;常见的几种场景 2&#xff1a;动态绑定内容 3&#xff1a;动态绑定属性 4&#xff1a;三元运算 4&am…

计算机网络(8) --- IP与IP协议

计算机网络&#xff08;7&#xff09; --- UDP协议和TCP协议_哈里沃克的博客-CSDN博客UDP协议和TCP协议https://blog.csdn.net/m0_63488627/article/details/132125374?spm1001.2014.3001.5501 目录 1.IP与IP协议 IP作用 协议​编辑 2.网段划分 DHCP划分 CIDR划分 特殊…

什么是进程、线程、协程

什么是进程&#xff1f; 我们都知道计算机的核心是CPU&#xff0c;它承担了所有的计算任务&#xff1b;而操作系统是计算机的管理者&#xff0c;它负责任务的调度、资源的分配和管理&#xff0c;统领整个计算机硬件&#xff1b;应用程序则是具有某种功能的程序&#xff0c;程序…

拥抱AIGC浪潮,亚信科技将如何把握时代新增量?

去年底&#xff0c;由ChatGPT带起的AIGC浪潮以迅雷不及掩耳之势席卷全球。 当互联网技术的人口红利逐渐消退之际&#xff0c;AIGC就像打开通用人工智能大门的那把秘钥&#xff0c;加速开启数智化时代的到来。正如OpenAI CEO Sam Altman所言&#xff1a;一个全新的摩尔定律可能…

Elasticsearch:如何创建 Elasticsearch PEM 和/或 P12 证书?

你是否希望使用 SSL/TLS 证书来保护你的 Elasticsearch 部署&#xff1f; 在本文中&#xff0c;我们将指导你完成为 Elasticsearch 创建 PEM 和 P12 证书的过程。 这些证书在建立安全连接和确保 Elasticsearch 集群的完整性方面发挥着至关重要的作用。 友情提示&#xff1a;你可…

【毕业项目】自主设计HTTP

博客介绍&#xff1a;运用之前学过的各种知识 自己独立做出一个HTTP服务器 自主设计WEB服务器 背景目标描述技术特点项目定位开发环境WWW介绍 网络协议栈介绍网络协议栈整体网络协议栈细节与http相关的重要协议 HTTP背景知识补充特点uri & url & urn网址url HTTP请求和…

C#程序的启动显示方案(无窗口进程发送消息) - 开源研究系列文章

今天继续研究C#的WinForm的实例显示效果。 我们上次介绍了Winform窗体的唯一实例运行代码(见博文&#xff1a;基于C#的应用程序单例唯一运行的完美解决方案 - 开源研究系列文章 )。这就有一个问题&#xff0c;程序已经打开了&#xff0c;这时候再次运行该应用程序&#xff0c;…

学习网络编程No.1【网络基础知识】

引言&#xff1a; 北京时间&#xff1a;2023/8/4/22:40&#xff0c;天苍苍野茫茫&#xff0c;风吹造地见牛羊&#xff0c;此时心潮澎湃&#xff0c;非常开心&#xff0c;啊哈哈哈&#xff01;因为就在刚刚我们终于把系统编程方面的知识给学完了&#xff0c;啊哈哈哈&#xff0…

科大讯飞分类算法挑战赛2023的一些经验总结

引言: ResNet是he kaiming大佬的早年神作&#xff0c;当年直接刷榜各大图像分类任务。ResNet是一种残差网络&#xff0c;咱们可以把它理解为一个子网络&#xff0c;这个子网络经过堆叠可以构成一个很深的网络&#xff0c;而ResNext在其基础上&#xff0c;进行了一定修改完善&am…

RabbitMQ在CentOS下的安装

RabbitMQ的版本是3.8.2 1.环境配置&#xff1a;CentOs 7.6以上版本&#xff0c;我的版本是7.9&#xff0c;不要对yum换源&#xff0c;否则可能会安装失败。 echo "export LC_ALLen_US.UTF-8" >> /etc/profile source /etc/profile 以上命令&#xff0c;是…

分立式BUCK电路原理与制作持续更新

一、分立式BUCK电路总体原理图 下面改图包含了电压环和电流环。 二、BUCK电路与LDO的区别 LDO不适合在压差大的环境下使用&#xff0c;因为三极管因为CE极承受了压差&#xff0c;压差越大损耗的功率就越大&#xff0c;将三极管换成MOS管&#xff0c;MOS管两端的压差很小所以效…

Linux安装配置nginx+php搭建以及在docker中配置

Linux安装配置nginxphp搭建以及在docker中配置 文章目录 Linux安装配置nginxphp搭建以及在docker中配置1.nginx源码包编译环境和安装相应的依赖1.1 安装编译环境1.2 安装pcre库、zlib库和openssl库 2.安装nginx2.1 在[nginx官网](https://nginx.org/en/download.html)上获取源码…

uni-app 封装api请求

前端封装api请求 前端封装 API 请求可以提高代码的可维护性和重用性&#xff0c;同时使得 API 调用更加简洁和易用。 下面是一种常见的前端封装 API 请求的方式&#xff1a; 创建一个 API 封装模块或类&#xff1a;可以使用 JavaScript 或 TypeScript 创建一个独立的模块或类来…

解决mysql常见错误,安装mysql提示Install/Remove of the service Denied!/显示无法启动/服务名无效

​​​​​1.概述问题 1.1 在安装mysql中提示Install/Remove of the service Denied! 1.2 MySQL 服务没有加载到电脑上时&#xff0c;有以下原因&#xff1a; 1.2.1 端口被占用&#xff0c;需要更改端口&#xff0c;也可以卸载重装mysql。 1.2.2 启动 MySQL 服务是就会提示 服务…

vue3中使用vue-simple-uploader

vue-simple-uploader本身是基于vue2的&#xff0c;直接npm i vue-simple-uploader -S下载下来版本的是0.7.6。在vue3中无法使用会报错。 解决&#xff1a;使用next安装接下来要发布的版本就会下载1.0.1版本&#xff0c;即可使用vue3 npm i vue-simple-uploadernext -S 注意&…