所学来自百问网
目录
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