Linux驱动开发基础(sr04超声波模块)

news2024/9/25 5:22:27

所学来自百问网

目录

1. SR04 超声波简介

2. 硬件设计

3. 软件设计

4. 示例代码

4.1 驱动代码

4.1.1 轮询模式

4.1.2 中断模式

4.3 应用程序

4.4 Makefile

4.5 实验效果


1. SR04 超声波简介

超声波测距模块是利用超声波来测距。模块先发送超声波,然后接收反射回来的超声波,由反射经历的时间和声音的传播速度340m/s,计算得出距离。

SR04 是一款常见的超声波传感器,模块自动发送8个40KHz的方波,自动检测是否有信号返回,用户只需提供一个触发信号,随后检测回响信号的时间长短即可。

SR04 采用5V电压,静态电流小于2mA,感应角度最大约15度,探测距离约2cm-450cm。

2. 硬件设计

SR04 模块上面有四个引脚,分别为:VCC、Trig、Echo、GND。

* Trig是脉冲触发引脚,即控制该脚让SR04模块开始发送超声波。

* Echo是回响接收引脚,即SR04模块一旦接收到超声波的返回信号则输出回响信号,回响信号的脉冲宽度与所测距离成正比。

3. 软件设计

时序图如下:

① 触发:向Trig(脉冲触发引脚)发出一个大约10us的高电平。

② 发出超声波,接收反射信号:模块就自动发出8个40Khz的超声波,超声波遇到障碍物后反射回来,模块收到返回来的超声波。

③ 回响:模块接收到反射回来的超声波后,Echo引脚输出一个与检测距离成比例的高电平。

4. 示例代码

4.1 驱动代码

4.1.1 轮询模式

#include <linux/module.h>
#include <linux/poll.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 <linux/gpio/consumer.h>
#include <linux/platform_device.h>
#include <linux/of_gpio.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/slab.h>
#include <linux/fcntl.h>
#include <linux/timer.h>
#include <linux/workqueue.h>
#include <asm/current.h>
#include <linux/delay.h>
static int major;
static struct class* sr04_class;
static struct gpio_desc *sr04_trig;
static struct gpio_desc *sr04_echo;

static ssize_t sr04_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
{
	int us = 0;
	int err;
	unsigned long flags;
	int timeout_us = 1000000; // 超时时间

	local_irq_save(flags);

	/* 发送10us高电平    , 测量距离 2cm-450cm */
	gpiod_set_value(sr04_trig,1);
	udelay(15);
	gpiod_set_value(sr04_trig,0);

	//使用udelay来延时判断引脚电平 等待高电平
	while(!gpiod_get_value(sr04_echo) && timeout_us)
	{
		udelay(1); 
		timeout_us--;
	}
	if (!timeout_us)
	{
		local_irq_restore(flags); // 恢复中断
		return -EAGAIN;
	}
	
	timeout_us = 1000000;
	while (gpiod_get_value(sr04_echo) && timeout_us)
	{ 
		udelay(1); 
		us++; // 累加时间
		timeout_us--;
	}  

	if (!timeout_us)
	{
		local_irq_restore(flags); // 恢复中断
		return -EAGAIN;
	}
		
	local_irq_restore(flags); // 恢复中断
	err = copy_to_user(buf, &us, 4);
	if(err < 0)
	{
		printk("%s %s line %d",__FILE__,__FUNCTION__,__LINE__);
		return err;
	}
	return 4;
}

static unsigned int sr04_poll (struct file *file, struct poll_table_struct *wait)
{

	return 0;
}

static struct file_operations sr04_ops = {
	.owner = THIS_MODULE,
	.read = sr04_read,
	.poll = sr04_poll,
};


static int sr04_probe(struct platform_device *pdev)
{
    // 获取硬件资源
	sr04_trig = gpiod_get(&pdev->dev,"trig",GPIOD_OUT_LOW);
	sr04_echo = gpiod_get(&pdev->dev,"echo",GPIOD_IN);


	device_create(sr04_class, NULL, MKDEV(major, 0), NULL, "sr04");
	return 0;
}
static int sr04_remove(struct platform_device *pdev)
{
	device_destroy(sr04_class, MKDEV(major, 0));
	gpiod_put(sr04_trig);
	gpiod_put(sr04_echo);
	return 0;
}

static struct of_device_id ask100_sr04[] = 
{
	{.compatible = "100ask,sr04" },
	{},
};

static struct platform_driver sr04_dri = {
	.probe = sr04_probe,
	.remove = sr04_remove,
	.driver = {
		.name = "sr04_100ask",
		.of_match_table = ask100_sr04,
	},
};

static int __init sr04_init(void)
{
    int err;

	major = register_chrdev(0,"sr04",&sr04_ops);
	sr04_class = class_create(THIS_MODULE,"sr04_class");

	err = platform_driver_register(&sr04_dri);
	return err;
}

static void __exit sr04_exit(void)
{
	unregister_chrdev(major,"sr04");
	class_destroy(sr04_class);
	platform_driver_unregister(&sr04_dri);
}

module_init(sr04_init);
module_exit(sr04_exit);
MODULE_LICENSE("GPL");

4.1.2 中断模式

#include <linux/module.h>
#include <linux/poll.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 <linux/gpio/consumer.h>
#include <linux/platform_device.h>
#include <linux/of_gpio.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/slab.h>
#include <linux/fcntl.h>
#include <linux/timer.h>
#include <linux/workqueue.h>
#include <asm/current.h>
#include <linux/delay.h>

static int major;
static struct class* sr04_class;
static struct gpio_desc *gpio_trig;
static struct gpio_desc *gpio_echo;
static int irq;
static wait_queue_head_t* sr04_wq;
static u64 sr04_data = 0;
static ssize_t sr04_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
{

	int timeout = 0;
	gpiod_set_value(gpio_trig, 1);
	udelay(15);
	gpiod_set_value(gpio_trig, 0);

	
	timeout = wait_event_interruptible_timeout(sr04_wq, sr04_data, HZ);
	if(timeout)
	{
		copy_to_user(buf,&sr04_data,4);
		sr04_data = 0;
		return 4;
	}else
	{
		return -EAGAIN;
	}
}
static unsigned int sr04_poll (struct file *file, struct poll_table_struct *wait)
{

	return 0;
}

static struct file_operations sr04_fops = {
	.owner = THIS_MODULE,
	.read = sr04_read,
	.poll = sr04_poll,
};

// 中断处理函数
static irq_handler_t sr04_isr(int irq, void * dev_id)
{
    // 获取引脚电平
	int val = gpiod_get_value(gpio_echo);

	if(val)
	{
		sr04_data = ktime_get_ns();
	}else
	{
		sr04_data = ktime_get_ns() - sr04_data;
		
		wake_up(&sr04_wq); // 唤醒中断
	}
	return IRQ_HANDLED;
}

static int sr04_probe(struct platform_device *pdev)
{	

	gpio_trig = gpiod_get(pdev->dev, "trig", GPIOD_OUT_LOW);
	gpio_echo = gpiod_get(pdev->dev, "echo", GPIOD_IN);
    // 申请中断号
	irq = gpiod_to_irq(gpio_echo);
    // 申请中断 此处为上下边沿触发
	request_irq(irq, sr04_isr, IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, "sr04", NULL);
	
	device_create(sr04_class, NULL,  MKDEV(major, 0), "sr04", NULL);
	
	return 0;
}
static int sr04_remove(struct platform_device *pdev)
{
	gpiod_put(gpio_trig);
	gpiod_put(gpio_echo);
	free_irq(irq, NULL); // 释放中断号
	
	device_destroy(sr04_class, MKDEV(major, 0));
	return 0;
}


static struct of_device_id ask100_sr04[] = {
	{ compatible = "100ask,sr04" },
	{},

},

static struct platform_driver sr04_dri = {
	.probe = sr04_probe,
	.remove = sr04_remove,
	.driver = {
		.name = "100ask_sr04",
		.of_match_table = ask100_sr04,
	},

};

static int __init sr04_init(void)
{
	int err;
	major = register_chrdev(0, "sr04", &sr04_fops);
	sr04_class = class_create(THIS_MODULE, "sr04_class");
    // 初始化中断队列
	init_waitqueue_head(sr04_wq);
	
	err = platform_driver_register(&sr04_dri);
	return err;
}

static void __exit sr04_exit(void)
{

	unregister_chrdev(major, "sr04");
	class_destroy(sr04_class);
	platform_driver_unregister(&sr04_dri);

}


module_init(sr04_init);
module_exit(sr04_exit);
MODULE_LICENSE("GPL");

4.2 应用程序

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

/*
 * ./sr04_test /dev/sr04
 *
 */
int main(int argc, char **argv)
{
	int fd;
	int us;

	int i;
	
	/* 1. 判断参数 */
	if (argc != 2) 
	{
		printf("Usage: %s <dev>\n", argv[0]);
		return -1;
	}


	/* 2. 打开文件 */
//	fd = open(argv[1], O_RDWR | O_NONBLOCK);
	fd = open(argv[1], O_RDWR);
	if (fd == -1)
	{
		printf("can not open file %s\n", argv[1]);
		return -1;
	}


	while (1)
	{
		if (read(fd, &us, 4) == 4)
		{
			printf("get us: %d us\n", us);  /* mm */
			printf("get distance: %d mm\n", us*340/2/1000);  /* mm */
		}
		else
			printf("get distance: -1\n");
	}
	
	close(fd);
	
	return 0;
}

4.3 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_imx6ull-sdk/Linux-4.9.88

all:
	make -C $(KERN_DIR) M=`pwd` modules 
	$(CROSS_COMPILE)gcc -o sr04_test sr04_test.c
clean:
	make -C $(KERN_DIR) M=`pwd` modules clean
	rm -rf modules.order  sr04_test

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



obj-m += sr04_drv.o

4.4 实验效果

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

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

相关文章

大数据技术概述

4v特点 volume&#xff08;体量大&#xff09; velocity&#xff08;处理速度快&#xff09; variety&#xff08;数据类型多&#xff09; value&#xff08;价值密度低&#xff09; 核心设计理念 并行化 规模经济 虚拟化 分布式系统满足需求 系统架构 大数据处理流程 采集…

找论文的方法:如何找到本领域研究方向所需要的论文进行泛读和精读?

1、参考其他研究者给出的该领域的reading lists&#xff1a; 例如&#xff0c;在异配图神经网络领域&#xff1a; Awesome Resource on Graph Neural Networks With Heterophily&#xff1a;https://github.com/alexfanjn/Graph-Neural-Networks-With-Heterophily 在图对抗攻…

快速掌握GPTEngineer:用AI创建网页应用的实用教程

今天来聊聊一个非常有趣的工具——GPTEngineer。这是一个基于AI的网页开发平台&#xff0c;特别适合那些不熟悉编程但又想快速创建网页应用的人。如果你想用简单的文本描述来生成一个网站或者应用&#xff0c;GPTEngineer可能就是你需要的。我们一步步看看如何使用它。 1. 了解…

Guava Cache实现原理及最佳实践

本文内容包括Guava Cache的使用、核心机制的讲解、核心源代码的分析以及最佳实践的说明。 概要 Guava Cache是一款非常优秀本地缓存&#xff0c;使用起来非常灵活&#xff0c;功能也十分强大。Guava Cache说简单点就是一个支持LRU的ConcurrentHashMap&#xff0c;并提供了基于…

Java面试宝典-java基础08

Java面试宝典-java基础08 71、BIO、NIO、AIO有哪些应用场景72、简述一下BIO的编程流程73、NIO的三大核心部分是什么&#xff1f;74、NIO中buffer的四大属性是什么&#xff1f;75、对比一下BIO和NIO&#xff1f;76、FileChannel是做什么的&#xff1f;77、简述一下Selector选择器…

51单片机-矩阵键盘(基于LC602)

时间&#xff1a;2024.8.30 作者&#xff1a;Whappy 目的&#xff1a;手撕51&#xff08;第二遍&#xff09; 代码&#xff1a; main.c #include <REGX52.H> #include "LCD1602.h" #include "Delay.h" #include "MatrixKey.h"unsigned…

【Canvas与艺术】录王昌龄诗《从军行之四》

【成图】 【代码】 <!DOCTYPE html> <html lang"utf-8"> <meta http-equiv"Content-Type" content"text/html; charsetutf-8"/> <head><title>出塞青海长云暗雪山</title><style type"text/css&quo…

opencv实战项目十六:kmeans图像颜色聚类:

文章目录 前言K-means介绍效果 前言 在数字化时代&#xff0c;图像处理技术已成为计算机视觉领域的重要组成部分。其中&#xff0c;图像颜色聚类作为一项关键技术在众多应用场景中发挥着重要作用&#xff0c;如图像分割、物体识别、色彩调整等。K-means算法作为一种经典的聚类…

Java性能优化传奇之旅--Java万亿级性能优化之电商平台高峰时段性能大作战:策略与趋势洞察

&#x1f496;&#x1f496;&#x1f496;亲爱的朋友们&#xff0c;热烈欢迎你们来到 青云交的博客&#xff01;能与你们在此邂逅&#xff0c;我满心欢喜&#xff0c;深感无比荣幸。在这个瞬息万变的时代&#xff0c;我们每个人都在苦苦追寻一处能让心灵安然栖息的港湾。而 我的…

Redis基础知识学习(入门篇)

文章目录 五大数据结构一. String: 字符串二. Hash: 散列概念性质 三. List: 列表四. Set: 集合特点 五. Sorted Set: 有序集合 五大数据结构 一. String: 字符串 数据结构中&#xff0c;字符串要单独用一种存储结构来存储&#xff0c;称为串存储结构。这里的串指的就是字符串…

性能分析之使用 Jvisualvm dump 分析示例

一、前言 在 JMeter 入门系列中相信大家对工具使用已经没问题&#xff0c;今天开起性能测试进阶系列之 jvisualvm 工具简单学习&#xff0c;目标是通过演示 Jvisualvm 工具定位代码&#xff0c;帮助性能测试工程师直接定位代码位置&#xff0c;协助开发解决性能问题&#xff1…

Flink CDC MySQL数据同步到Doris表同步配置生成工具类

工具类 生成的配置 要同步表为&#xff1a; customer_user.tb_business_user_info express.route_push_service 请提前自行到doris中建好目标数据库&#xff0c;如果没有会报错 同步的配置文件如下&#xff1a;&#xff08;将配置内容保存为xxx.yaml文件到flink cdc提交任务&…

昇腾 Ascend 概念澄清 Host、Device、AI core、AI CPU、DVPP、AIPP、AscendCL、AscendC

昇腾 Ascend 概念澄清 Host、Device、AI core、AI CPU、DVPP、AIPP、AscendCL、AscendC flyfish Ascend C开发算子&#xff0c;偏低。 AscendCL开发应用&#xff0c;偏高。 AI core、AI CPU、DVPP都属于计算资源。 Ascend C开发的算子运行在AI Core上。 AIPP用于在AI Core上完…

TimeWheel算法介绍及在应用上的探索

作者&#xff1a;来自 vivo 互联网服务器团队- Li Fan 本文从追溯时间轮算法的出现&#xff0c;介绍了时间轮算法未出现前&#xff0c;基于队列的定时任务实现&#xff0c;以及基于队列的定时任务实现所存在的缺陷。接着我们介绍了时间轮算法的算法思想及其数据结构&#xff0c…

手撕数据结构与算法——拓扑排序

拓扑排序是图论中的一个重要概念&#xff0c;它在许多领域如任务调度、课程规划等都有广泛的应用。在这篇文章中&#xff0c;我们将探讨拓扑排序的基本概念、算法实现以及在C/C中的实现方法。 拓扑排序简介 拓扑排序是针对有向无环图&#xff08;DAG&#xff09;的一种排序算法…

二叉树(数据结构)

1.两种特殊的二叉树 1. 满二叉树 : 一棵二叉树&#xff0c;如果 每层的结点数都达到最大值&#xff0c;则这棵二叉树就是满二叉树 。也就是说&#xff0c; 如果一棵 二叉树的层数为 K &#xff0c;且结点总数是2^k-1 &#xff0c;则它就是满二叉树 。 2. 完全二叉树 : 完…

为你的LLM应用增加记忆能力

1. 记忆系统的重要性 我们都知道&#xff0c;大模型本身是无状态、无记忆的。默认情况下&#xff0c;我们向大模型发起的每次提问&#xff0c;在其内部都会被视为一次全新的调用。尽管诸如 ChatGPT 等聊天应用内置了部分记忆功能&#xff0c;可以记录用户最近几轮的聊天信息&a…

ChatTTS容器构建教程

一、模型介绍 ChatTTS 是专门为对话场景设计的文本转语音模型&#xff0c;例如 LLM 助手对话任务。它支持英文和中文两种语言。最大的模型使用了 10 万小时以上的中英文数据进行训练。在 HuggingFace 中开源的版本为 4 万小时训练且未 SFT 的版本。 ChatTTS WebUI如下&#x…

【单片机原理及应用】实验:LED循环控制

目录 一、实验目的 二、实验内容 三、实验步骤 四、记录与处理 五、思考 六、成果文件提取链接 一、实验目的 熟悉Proteus x8原理图与C51程序的联合仿真调试方法&#xff0c;掌握C51延时函数和循环控制的方法 二、实验内容 【参照图表】 &#xff08;1&#xff09;创建一…

晚宴扫码查询桌号

在现代社交活动中&#xff0c;晚宴的组织和管理越来越依赖于技术手段。为了提高晚宴的效率和参与者的体验&#xff0c;我们可以通过一个简单的扫码查询系统来实现快速查找桌号和座位号。以下是详细步骤&#xff1a; 1. 电脑端上传查询信息 访问云分组官网。 使用微信扫码登录…