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

相关文章

PyQt5 GUI 开发的基础知识

《PyQt5GUI开发的基础知识》Qt是一个跨平台的C++图形用户界面开发框架,支持GUI和非GUI程序开发,本文介绍了使用PyQt5进行界面开发的基础知识,包括创建简单窗口、常用控件、窗口属性设... 目录简介第一个PyQt程序最常用的三个功能模块控件QPushButton(按钮)控件QLable(纯文本

Linux系统中查询JDK安装目录的几种常用方法

《Linux系统中查询JDK安装目录的几种常用方法》:本文主要介绍Linux系统中查询JDK安装目录的几种常用方法,方法分别是通过update-alternatives、Java命令、环境变量及目... 目录方法 1:通过update-alternatives查询(推荐)方法 2:检查所有已安装的 JDK方

Linux系统之lvcreate命令使用解读

《Linux系统之lvcreate命令使用解读》lvcreate是LVM中创建逻辑卷的核心命令,支持线性、条带化、RAID、镜像、快照、瘦池和缓存池等多种类型,实现灵活存储资源管理,需注意空间分配、R... 目录lvcreate命令详解一、命令概述二、语法格式三、核心功能四、选项详解五、使用示例1. 创建逻

Linux下在线安装启动VNC教程

《Linux下在线安装启动VNC教程》本文指导在CentOS7上在线安装VNC,包含安装、配置密码、启动/停止、清理重启步骤及注意事项,强调需安装VNC桌面以避免黑屏,并解决端口冲突和目录权限问题... 目录描述安装VNC安装 VNC 桌面可能遇到的问题总结描js述linux中的VNC就类似于Window

linux下shell脚本启动jar包实现过程

《linux下shell脚本启动jar包实现过程》确保APP_NAME和LOG_FILE位于目录内,首次启动前需手动创建log文件夹,否则报错,此为个人经验,供参考,欢迎支持脚本之家... 目录linux下shell脚本启动jar包样例1样例2总结linux下shell脚本启动jar包样例1#!/bin

Linux之platform平台设备驱动详解

《Linux之platform平台设备驱动详解》Linux设备驱动模型中,Platform总线作为虚拟总线统一管理无物理总线依赖的嵌入式设备,通过platform_driver和platform_de... 目录platform驱动注册platform设备注册设备树Platform驱动和设备的关系总结在 l

linux批量替换文件内容的实现方式

《linux批量替换文件内容的实现方式》本文总结了Linux中批量替换文件内容的几种方法,包括使用sed替换文件夹内所有文件、单个文件内容及逐行字符串,强调使用反引号和绝对路径,并分享个人经验供参考... 目录一、linux批量替换文件内容 二、替换文件内所有匹配的字符串 三、替换每一行中全部str1为st

基于Python开发一个图像水印批量添加工具

《基于Python开发一个图像水印批量添加工具》在当今数字化内容爆炸式增长的时代,图像版权保护已成为创作者和企业的核心需求,本方案将详细介绍一个基于PythonPIL库的工业级图像水印解决方案,有需要... 目录一、系统架构设计1.1 整体处理流程1.2 类结构设计(扩展版本)二、核心算法深入解析2.1 自

Linux进程CPU绑定优化与实践过程

《Linux进程CPU绑定优化与实践过程》Linux支持进程绑定至特定CPU核心,通过sched_setaffinity系统调用和taskset工具实现,优化缓存效率与上下文切换,提升多核计算性能,适... 目录1. 多核处理器及并行计算概念1.1 多核处理器架构概述1.2 并行计算的含义及重要性1.3 并

Linux线程之线程的创建、属性、回收、退出、取消方式

《Linux线程之线程的创建、属性、回收、退出、取消方式》文章总结了线程管理核心知识:线程号唯一、创建方式、属性设置(如分离状态与栈大小)、回收机制(join/detach)、退出方法(返回/pthr... 目录1. 线程号2. 线程的创建3. 线程属性4. 线程的回收5. 线程的退出6. 线程的取消7.