【2023 DigiKey大赛参与奖】BeagleBone_Green开发板基于Ibserialport进行串口开发
[复制链接]
本文转自本人公众号文章
发板基于Ibserialport进行串口开发
一. 前言
串口是嵌入式开发最常用的通讯接口,需要经常使用,一个好的串口开发库可以比自己直接操作设备文件读写,更方便简单高效。这里分享一款比较好的跨平台的串口库libserialport以及在本开发板上的应用实例。
二. 使能串口
查看设备树
debian@BeagleBone:~$ ls /lib/firmware/BB-UART*
/lib/firmware/BB-UART1-00A0.dtbo
/lib/firmware/BB-UART1-RTSCTS-00A0.dtbo
/lib/firmware/BB-UART2-00A0.dtbo
/lib/firmware/BB-UART2-00A1.dtbo
/lib/firmware/BB-UART2-RTSCTS-00A0.dtbo
/lib/firmware/BB-UART3-00A0.dtbo
/lib/firmware/BB-UART4-00A0.dtbo
/lib/firmware/BB-UART4-E-INK.dtbo
/lib/firmware/BB-UART4-RS485-00A0.dtbo
/lib/firmware/BB-UART4-RTSCTS-00A0.dtbo
/lib/firmware/BB-UART5-00A0.dtbo
/lib/firmware/BB-UART5-RTSCTS-00A0.dtbo
debian@BeagleBone:~$
查看设备树内容
cat /lib/firmware/BB-UART1-00A0.dtbo
sudo nano /boot/uEnv.txt添加如下内容使能设备树,重启
# UART 1
uboot_overlay_addr0=/lib/firmware/BB-UART1-00A0.dtbo
# UART 2
uboot_overlay_addr1=/lib/firmware/BB-UART2-00A0.dtbo
# UART 4
uboot_overlay_addr2=/lib/firmware/BB-UART4-00A0.dtbo
# UART 5
uboot_overlay_addr3=/lib/firmware/BB-UART5-00A0.dtbo
# UART 3 (only TX). Note that in "uboot_overlay_addrX", the X need not be = UART id
uboot_overlay_addr4=/lib/firmware/BB-UART3-00A0.dtbo
三. 使用Ibserialport源码构建应用
下载代码
git clone git://sigrok.org/libserialport
位于libserialport同级
复制测试代码
cp libserialport/examples/send_receive.c .
新建config.h文件可以是空文件
需要文件
libserialport.h
libserialport_internal.h
serialport.c
timing.c
linux.c
linux_termios.c
linux_termios.h
编译
gcc libserialport/serialport.c libserialport/timing.c libserialport/linux.c libserialport/linux_termios.c send_receive.c -o send_receive -Ilibserialport/ -I. -DSP_PRIV=
运行测试
sudo cat /proc/tty/drivers
debian@BeagleBone:~$ sudo cat /proc/tty/drivers
/dev/tty /dev/tty 5 0 system:/dev/tty
/dev/console /dev/console 5 1 system:console
/dev/ptmx /dev/ptmx 5 2 system
/dev/vc/0 /dev/vc/0 4 0 system:vtmaster
g_serial /dev/ttyGS 242 0-7 serial
serial /dev/ttyS 4 64-69 serial
pty_slave /dev/pts 136 0-1048575 pty:slave
pty_master /dev/ptm 128 0-1048575 pty:master
unknown /dev/tty 4 1-63 console
debian@BeagleBone:~$
对应
debian@BeagleBone:~$ ls /dev/ttyS
ttyS0 ttyS1 ttyS2 ttyS3 ttyS4 ttyS5
我们使用UART4测试,短接UART4的RXD和TXD
debian@BeagleBone:~$ ./send_receive /dev/ttyS4
Looking for port /dev/ttyS4.
Opening port.
Setting port to 9600 8N1, no flow control.
Sending 'Hello!' (6 bytes) on port /dev/ttyS4.
Sent 6 bytes successfully.
Receiving 6 bytes on port /dev/ttyS4.
Received 6 bytes successfully.
Received 'Hello!'.
debian@BeagleBone:~$
四. 总结
基于libserial提供的阻塞和非阻塞的接口,可以快速的进行串口应用的开发。
|