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

2024-08-31 04:04

本文主要是介绍Linux驱动开发基础(sr04超声波模块),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

所学来自百问网

目录

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 */}elseprintf("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.88all:make -C $(KERN_DIR) M=`pwd` modules $(CROSS_COMPILE)gcc -o sr04_test sr04_test.c
clean:make -C $(KERN_DIR) M=`pwd` modules cleanrm -rf modules.order  sr04_test# 参考内核源码drivers/char/ipmi/Makefile
# 要想把a.c, b.c编译成ab.ko, 可以这样指定:
# ab-y := a.o b.o
# obj-m += ab.oobj-m += sr04_drv.o

4.4 实验效果

这篇关于Linux驱动开发基础(sr04超声波模块)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1122834

相关文章

基于 Cursor 开发 Spring Boot 项目详细攻略

《基于Cursor开发SpringBoot项目详细攻略》Cursor是集成GPT4、Claude3.5等LLM的VSCode类AI编程工具,支持SpringBoot项目开发全流程,涵盖环境配... 目录cursor是什么?基于 Cursor 开发 Spring Boot 项目完整指南1. 环境准备2. 创建

防止Linux rm命令误操作的多场景防护方案与实践

《防止Linuxrm命令误操作的多场景防护方案与实践》在Linux系统中,rm命令是删除文件和目录的高效工具,但一旦误操作,如执行rm-rf/或rm-rf/*,极易导致系统数据灾难,本文针对不同场景... 目录引言理解 rm 命令及误操作风险rm 命令基础常见误操作案例防护方案使用 rm编程 别名及安全删除

Linux下MySQL数据库定时备份脚本与Crontab配置教学

《Linux下MySQL数据库定时备份脚本与Crontab配置教学》在生产环境中,数据库是核心资产之一,定期备份数据库可以有效防止意外数据丢失,本文将分享一份MySQL定时备份脚本,并讲解如何通过cr... 目录备份脚本详解脚本功能说明授权与可执行权限使用 Crontab 定时执行编辑 Crontab添加定

Python中logging模块用法示例总结

《Python中logging模块用法示例总结》在Python中logging模块是一个强大的日志记录工具,它允许用户将程序运行期间产生的日志信息输出到控制台或者写入到文件中,:本文主要介绍Pyt... 目录前言一. 基本使用1. 五种日志等级2.  设置报告等级3. 自定义格式4. C语言风格的格式化方法

SpringBoot 多环境开发实战(从配置、管理与控制)

《SpringBoot多环境开发实战(从配置、管理与控制)》本文详解SpringBoot多环境配置,涵盖单文件YAML、多文件模式、MavenProfile分组及激活策略,通过优先级控制灵活切换环境... 目录一、多环境开发基础(单文件 YAML 版)(一)配置原理与优势(二)实操示例二、多环境开发多文件版

使用docker搭建嵌入式Linux开发环境

《使用docker搭建嵌入式Linux开发环境》本文主要介绍了使用docker搭建嵌入式Linux开发环境,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录1、前言2、安装docker3、编写容器管理脚本4、创建容器1、前言在日常开发全志、rk等不同

从基础到高级详解Python数值格式化输出的完全指南

《从基础到高级详解Python数值格式化输出的完全指南》在数据分析、金融计算和科学报告领域,数值格式化是提升可读性和专业性的关键技术,本文将深入解析Python中数值格式化输出的相关方法,感兴趣的小伙... 目录引言:数值格式化的核心价值一、基础格式化方法1.1 三种核心格式化方式对比1.2 基础格式化示例

redis-sentinel基础概念及部署流程

《redis-sentinel基础概念及部署流程》RedisSentinel是Redis的高可用解决方案,通过监控主从节点、自动故障转移、通知机制及配置提供,实现集群故障恢复与服务持续可用,核心组件包... 目录一. 引言二. 核心功能三. 核心组件四. 故障转移流程五. 服务部署六. sentinel部署

Python 基于http.server模块实现简单http服务的代码举例

《Python基于http.server模块实现简单http服务的代码举例》Pythonhttp.server模块通过继承BaseHTTPRequestHandler处理HTTP请求,使用Threa... 目录测试环境代码实现相关介绍模块简介类及相关函数简介参考链接测试环境win11专业版python

Python实战之SEO优化自动化工具开发指南

《Python实战之SEO优化自动化工具开发指南》在数字化营销时代,搜索引擎优化(SEO)已成为网站获取流量的重要手段,本文将带您使用Python开发一套完整的SEO自动化工具,需要的可以了解下... 目录前言项目概述技术栈选择核心模块实现1. 关键词研究模块2. 网站技术seo检测模块3. 内容优化分析模