一,前言
今天玩一下US100超声波传感器。5年前玩寻路小车的时候用过,已经忘记了。先看了下它的原理,除了支持gpio还支持uart的。给它输入0x55命令可以获取2个字节的距离mm信息。那么我就用下串口子系统。基于console的其实一直在用,但是我都没了解过tty的框架。正好网上了解下,资料比较多,我就不多说。配置完设置树,使用8250底层uart驱动及tty驱动,所以驱动不用自己写,配置完设备树,写了read/write APP就能用了。工程已上传我的gitee工程13。
二,设备树修改
a.先用PC串口调试助手使用了下US100。验证器件正常。GND我就连接了一根。
image.png
b.看了下datasheet及原理图,决定使用UART1。
am335x-bone-common.dtsi修改下
uart1_pins: pinmux_uart1_pins {
pinctrl-single,pins = <
AM33XX_PADCONF(AM335X_PIN_UART1_RXD, PIN_INPUT_PULLUP, MUX_MODE0)
AM33XX_PADCONF(AM335X_PIN_UART1_TXD, PIN_OUTPUT_PULLDOWN, MUX_MODE0)
>;
};
&uart1 {
pinctrl-names = "default";
pinctrl-0 = <&uart1_pins>;
status = "okay";
};
image.png
三,遇到的问题
1. 到底注册到哪个设备了,那么多tty。
log信息可以看出注册到了ttyS1
[ 1.013776] Serial: 8250/16550 driver, 6 ports, IRQ sharing enabled
[ 1.019091] 44e09000.serial: ttyS0 at MMIO 0x44e09000 (irq = 29, base_baud = 3000000) is a 8250
[ 1.814064] printk: console [ttyS0] enabled
[ 1.820211] 48022000.serial: ttyS1 at MMIO 0x48022000 (irq = 35, base_baud = 3000000) is a 8250
设备描述中也可以看到更详细的内容。
# pwd
/sys/bus/platform/drivers/omap8250/48022000.serial
# ls -al
total 0
drwxr-xr-x 5 root root 0 Jan 1 00:02 .
drwxr-xr-x 4 root root 0 Jan 1 00:02 ..
lrwxrwxrwx 1 root root 0 Jan 1 00:02 driver -> ../../../../../../../bus/platform/drivers/omap8250
-rw-r--r-- 1 root root 4096 Jan 1 00:02 driver_override
-r--r--r-- 1 root root 4096 Jan 1 00:02 modalias
lrwxrwxrwx 1 root root 0 Jan 1 00:02 of_node -> ../../../../../../../firmware/devicetree/base/ocp/interconnect@48000000/segment@0/target-module@22000/serial@0
drwxr-xr-x 2 root root 0 Jan 1 00:02 power
lrwxrwxrwx 1 root root 0 Jan 1 00:02 subsystem -> ../../../../../../../bus/platform
drwxr-xr-x 3 root root 0 Jan 1 00:02 tty
-rw-r--r-- 1 root root 4096 Jan 1 00:02 uevent
drwxr-xr-x 3 root root 0 Jan 1 00:02 wakeup
# cd tty
# ls
ttyS1
2. write一直阻塞。
我的app是先write 0x55,然后read 2字节,结果write就阻塞了。怀疑线反了,反接后依然write 阻塞。于是用示波器,看了下波形幅度值不到5。所以将usb上电改成了电源上电。问题解决。示波器发送0x55截图如下,
另外,我用echo -e -n "\x55" > /dev/ttyS1截图也是一样的。并且由于发送成功,所以我截取rx脚,接收也成功。接收2字节如下图
image.png
3. read一直阻塞
示波器都已经显示read有正常的波形,但是read一直被阻塞。我怀疑杜邦线断了,用万用表测量线正常。难道是没有进入中断。我之前看框架的时候ftrace也跟踪过,但是ftrace内容太多了,因为uart0也是用8250,所以我区分不了。那么从/proc/interrupts来查看中断。如下可以看出35号中断一开始是没有的,运行了us程序后,35号中断出现。最后我只能看源码,当然最快的速度是网上找下框架资料,比如接收中断是哪个函数,然后又会调用哪里。接着找到了线索,他描述的接收中断居然是从console输入作为接收中断的,然后echo回显到console。那么我立即想到一个问题,我不用console的,是否就不能唤醒read中。所以是否有些设置bypass console。本来想看了document的帮助,因为我觉得linux的串口驱动不会有bug的,先网上搜索下,立即得到答案。原来需要设置raw模块的串口通信。在raw模式下最后运行app,获取结果正确。
# cat /proc/interrupts
CPU0
16: 618 INTC 68 Level gp_timer
18: 0 INTC 3 Level arm-pmu
19: 8 INTC 12 Level 49000000.edma_ccint
21: 0 INTC 14 Level 49000000.edma_ccerrint
28: 0 INTC 96 Level 44e07000.gpio
29: 108 INTC 72 Level 44e09000.serial
30: 146 INTC 70 Level 44e0b000.i2c
43: 0 INTC 98 Level 4804c000.gpio
44: 37 INTC 64 Level mmc0
46: 0 INTC 30 Level 4819c000.i2c
47: 0 INTC 32 Level 481ac000.gpio
48: 0 INTC 62 Level 481ae000.gpio
49: 47 INTC 28 Level mmc1
53: 1191 INTC 41 Level 4a100000.ethernet
54: 689 INTC 42 Level 4a100000.ethernet
57: 0 44e07000.gpio 6 Edge 48060000.mmc cd
58: 0 INTC 7 Level tps65217-irq
IPI0: 0 CPU wakeup interrupts
IPI1: 0 Timer broadcast interrupts
IPI2: 0 Rescheduling interrupts
IPI3: 0 Function call interrupts
IPI4: 0 CPU stop interrupts
IPI5: 0 IRQ work interrupts
IPI6: 0 completion interrupts
Err: 0
# cd /usr/study/
#
# ./us &
# ok
# cat /proc/interrupts
CPU0
16: 1012 INTC 68 Level gp_timer
18: 0 INTC 3 Level arm-pmu
19: 8 INTC 12 Level 49000000.edma_ccint
21: 0 INTC 14 Level 49000000.edma_ccerrint
28: 0 INTC 96 Level 44e07000.gpio
29: 247 INTC 72 Level 44e09000.serial
30: 146 INTC 70 Level 44e0b000.i2c
35: 0 INTC 73 Level 48022000.serial
43: 0 INTC 98 Level 4804c000.gpio
44: 37 INTC 64 Level mmc0
46: 0 INTC 30 Level 4819c000.i2c
47: 0 INTC 32 Level 481ac000.gpio
48: 0 INTC 62 Level 481ae000.gpio
49: 47 INTC 28 Level mmc1
53: 1373 INTC 41 Level 4a100000.ethernet
54: 764 INTC 42 Level 4a100000.ethernet
57: 0 44e07000.gpio 6 Edge 48060000.mmc cd
58: 0 INTC 7 Level tps65217-irq
IPI0: 0 CPU wakeup interrupts
IPI1: 0 Timer broadcast interrupts
IPI2: 0 Rescheduling interrupts
IPI3: 0 Function call interrupts
IPI4: 0 CPU stop interrupts
IPI5: 0 IRQ work interrupts
IPI6: 0 completion interrupts
Err: 0
四 测试结果正确
不同的障碍物距离,显示的结果不同。
# stty -F /dev/ttyS1 9600 raw
# ./us
ok
write 0x55
distance is 91
write 0x55
distance is 45
write 0x55
distance is 198
write 0x55
distance is 120
write 0x55
distance is 1579
五,参考网址
a.http://blog.chinaunix.net/uid-27717694-id-3493611.html
b.https://blog.csdn.net/shipinsky/article/details/82177143
c.https://blog.csdn.net/lhl_blog/article/details/82254056
网友评论