HC-SR04是一款常见的超声波测距传感器,它可以提供2cm-400cm的非接触式测距功能,精度达到3mm,基本可以满足一般测距需求。该款模块的两个IO口,其中trig为触发信号输入,echo为回响信号输出。接下来说说调试方法。
1. dts设备注册
修改 kernel/arch/arm64/boot/dts/rockchip/rk3308-firefly.dtsi 该文件,增加以下资源描述:
sr04_test: sr04_test {
compatible= "ultrasonic";
trig-gpios = <&gpio0 RK_PA5 GPIO_ACTIVE_HIGH>;
echo-gpios = <&gpio0 RK_PC4 GPIO_ACTIVE_HIGH>;
status = "okay";
};
2. 驱动编写,笔者在iio子系统下进行驱动编写,在路径 kernel/drivers/iio/下创建ultrasonic/,参考这款传感器的规格书,下面是部分驱动代码的实现:
信号触发
ret = gpio_direction_output(sr04->trig, 0);
if (ret)
goto err;
udelay(2);
ret = gpio_direction_output(sr04->trig, 1);
if (ret)
goto err;
udelay(15);
ret = gpio_direction_output(sr04->trig, 0);
if (ret)
goto err;
中断处理
static irqreturn_t sr04_handle_irq(int irq, void *data)
{
struct iio_dev *iio = data;
struct sr04 *sr04 = iio_priv(iio);
current_echo_value[sr04->number] = gpio_get_value(sr04->echo);
if (current_echo_value[sr04->number] != last_echo_value[sr04->number]) {
if(sr04_flag[sr04->number]==0){
do_gettimeofday(&start[sr04->number]);
sr04_flag[sr04->number] = 1;
last_echo_value[sr04->number] = current_echo_value[sr04->number];
}
else{
do_gettimeofday(&end[sr04->number]);
time_lag[sr04->number] = (end[sr04->number].tv_usec-start[sr04->number].tv_usec);
sr04_flag[sr04->number] = 0;
last_echo_value[sr04->number] = current_echo_value[sr04->number];
}
ev_press = 1;
wake_up_interruptible(&sr04_waitq);
}
return IRQ_HANDLED;
}
完整的驱动文件可见后面附件。
3. 完成驱动代码编写后,分别修改kernel/drivers/iio/Kconfig b/kernel/drivers/iio/Kconfig和kernel/drivers/iio/Makefile b/kernel/drivers/iio/Makefile
diff --git a/kernel/drivers/iio/Kconfig b/kernel/drivers/iio/Kconfig
index 66792e7..07fdf91 100644
--- a/kernel/drivers/iio/Kconfig
+++ b/kernel/drivers/iio/Kconfig
@@ -53,6 +53,7 @@ source "drivers/iio/dac/Kconfig"
source "drivers/iio/frequency/Kconfig"
source "drivers/iio/gyro/Kconfig"
source "drivers/iio/humidity/Kconfig"
+source "drivers/iio/ultrasonic/Kconfig"
source "drivers/iio/imu/Kconfig"
source "drivers/iio/light/Kconfig"
source "drivers/iio/magnetometer/Kconfig"
diff --git a/kernel/drivers/iio/Makefile b/kernel/drivers/iio/Makefile
index aeca726..c409a89 100644
--- a/kernel/drivers/iio/Makefile
+++ b/kernel/drivers/iio/Makefile
@@ -19,6 +19,7 @@ obj-y += dac/
obj-y += gyro/
obj-y += frequency/
obj-y += humidity/
+obj-y += ultrasonic/
obj-y += imu/
obj-y += light/
cd kernel/
make menuconfig
输入上面的命令,进入图形选择界面,/ 搜索sr04,选择、保存、退出。
--- a/kernel/arch/arm64/configs/firefly-rk3308_linux_defconfig
+++ b/kernel/arch/arm64/configs/firefly-rk3308_linux_defconfig
@@ -251,6 +251,7 @@ CONFIG_IIO=y
CONFIG_IIO_BUFFER=y
CONFIG_IIO_KFIFO_BUF=y
CONFIG_IIO_TRIGGER=y
+CONFIG_SR04=y
CONFIG_ROCKCHIP_SARADC=y
CONFIG_PWM=y
CONFIG_PWM_ROCKCHIP=y
配置好后,参考官方维基http://wiki.t-firefly.com/ROC-RK ... html#bian-yi-kernel进行编译即可。
编译烧录完成,可以找到我们挂载的设备节点
# cd /sys/bus/iio/devices/iio:device1
# ls
dev in_temp_input name of_node power subsystem uevent
进入Firefly开发者社区,可下载驱动文件。
———————————————
更多信息请关注Firefly公众微信号fireflytee:
———————————————
Firefly官网:http://www.t-firefly.com
Firefly开源社区:http://developer.t-firefly.com