简介 / Introduction
UART 流量控制是一种让慢速和快速设备通过 UART 相互通信而不会丢失数据的方法。
考虑两个设备通过 UART 通信的情况。发送器 T 正在向接收器 R 发送一长串字节。R 是一个比 T 慢的设备,在某些时候 R 无法跟上。它需要对数据进行一些处理或清空一些缓冲区,然后才能继续接收数据。
R 需要告诉 T 暂时停止传输。这就是流量控制的作用。 流量控制提供额外的信令,通知发送器应该停止(暂停)或开始(恢复)传输。
目前存在几种形式的流量控制。例如,硬件流量控制使用额外的导线,这些导线上的逻辑电平决定发送器是继续发送数据还是停止发送。在软件流量控制中,通过正常数据线发送特殊字符来启动或停止传输。
UART Flow Control is a method for slow and fast devices to communicate with each other over UART without the risk of losing data.
Consider the case where two units are communicating over UART. A transmitter T is sending a long stream of bytes to a receiver R. R is a slower device than T, and at some point R cannot keep up. It needs to either do some processing on the data or empty some buffers before it can keep receiving data.
R needs to tell T to stop transmitting for a while. This is where flow control comes in. Flow control provides extra signaling to inform the transmitter that it should stop (pause) or start (resume) the transmission.
Several forms of flow control exist. For example, hardware flow control uses extra wires, where the logic level on these wires define whether the transmitter should keep sending data or stop. With software flow control, special characters are sent over the normal data lines to start or stop the transmission.
流控协议 / Flow Control Protocols
下面来介绍实现流控的常用方法。
-
UART Hardware Flow Control / 硬件流控
使用硬件流量控制(也称为 RTS/CTS 流量控制)时,除了数据线外,还需要两条额外的导线。它们分别称为 RTS(请求发送)和 CTS(清除发送)。这两条线在两台设备之间交叉耦合,因此一台设备上的 RTS 连接到远程设备上的 CTS,反之亦然。每台设备都将使用其 RTS 输出是否准备好接受新数据,并读取 CTS 查看是否允许向另一台设备发送数据。
只要设备准备好接受更多数据,它就会保持 RTS 线路的有效状态。设备会在其接收缓冲区满之前的一段时间清除RTS有效状态。线路上和其他设备发送寄存器中可能仍有数据,即使在 RTS 终止后仍需接收。其他设备必须尊重流量控制信号并暂停传输,直到 RTS 信号再次有效。
With hardware flow control (also called RTS/CTS flow control), two extra wires are needed in addition to the data lines. They are called RTS (Request to Send) and CTS (Clear to Send). These wires are cross-coupled between the two devices, so RTS on one device is connected to CTS on the remote device and vice versa. Each device will use its RTS to output if it is ready to accept new data and read CTS to see if it is allowed to send data to the other device.
As long as a device is ready to accept more data, it will keep the RTS line asserted. It will deassert RTS some time before its receive buffer is full. There might still be data on the line and in the other device transmit registers which has to be received even after RTS has been deasserted. The other device is required to respect the flow control signal and pause the transmission until RTS is again asserted.
流量控制是双向的,这意味着两个设备都可以请求停止传输。 如果其中一个设备无需请求停止传输(即它的速度足够快,总是能接收到数据),则另一个设备上的 CTS 信号可以绑定到有效状态的逻辑电平。这样,快速设备上的RTS引脚就可以用于其他功能。
The flow control is bidirectional, meaning both devices can request a halt in transmission. If one of the devices never has to request a stop in transmission (i.e. it is fast enough to always receive data), the CTS signal on the other device can be tied to the asserted logic level. The RTS pin on the fast device can thus be freed up to other functions.
如下实际波形表示了硬件流控的时序:
1,本地设备将RTS信号拉低,是为有效,然后对端设备发送数据,本地设备的RX信号线上接收到数据。
2,当本地设备不想接收数据了,就将RTS信号拉高,然后对端设备发送完缓冲区里的数据后,就不再发送数据了。
Hardware Flow Control Example: After a certain time the receiver deasserts RTS and the transmitter responds by pausing transmission.
-
Legacy Hardware Flow Control / 早期硬件流控
在谈论硬件流量控制时,一个容易引起混淆的问题是,不同的协议使用相同的名称。硬件流量控制有时指另一种流量控制方法。在本文中,我们将把第二种方法称为传统硬件流控制,以区别于上面的硬件流控制中讨论的类型。之所以使用传统这个名称,是因为这种方法实际上是实现流量控制的早期方法。
A point of confusion when talking about hardware flow control is that the same names are used for different protocols. Hardware Flow Control sometimes refer to another method for flow control. In this document we shall refer to this second method as legacy Hardware Flow Control to differentiate it from the type discussed in above-mentioned Hardware Flow Control. The name legacy is used because this method was actually the early method of implementing flow control.
传统硬件流量控制仍然使用名为 RTS 和 CTS 的两条额外导线,但功能略有不同。在这种方案中,流量控制是单向的,存在主/从关系。传统上,主站和从站被称为 DTE(数据终端设备)和 DCE(数据通信设备)。当主设备要向从设备传输数据时,它会使能RTS线路的信号。从属设备通过使能CTS信号作出响应。然后就可以进行传输,直到从属设备取消CTS有效信号,表示需要暂时停止传输。当主站完成整个信息的传输后,它将取消RTS有效信号。
Legacy Hardware Flow Control still uses two extra wires named RTS and CTS, but the functionality is slightly different. In this scheme the flow control is unidirectional and there is a master/slave relationship (traditionally the master and slave are referred to as DTE (Data Terminal Equipment) and DCE (Data Communications Equipment)). When the master wants to transmit data to the slave it asserts the RTS line. The slave responds by asserting CTS. Transmission can then occur until the slave deasserts CTS, indicating that it needs a temporary halt in transmission. When the master has finished transmitting the entire message it will deassert RTS.
传统RTS / CTS 流量控制是 RS232 标准的另一种流量控制机制。它利用 RS232 连接器上的两个引脚 RTS(请求发送)和 CTS(清除发送)。这两条线允许接收器和发送器相互提醒对方自己的状态。
发送器使能 RTS信号,会导致接收器产生中断,即 "嘿,我能发送一些数据吗?“
如果接收器可以接收数据,则会使能其 CTS信号,即 "可以开始发送了"。
RTS / CTS Flow Control is another flow control mechanism that is part of the RS232 standard. It makes use of two further pins on the RS232 connector, RTS (Request to Send) and CTS (Clear to Send). These two lines allow the receiver and the transmitter to alert each other to their state.
A transmitter raises its RTS line, which causes an interrupt on the receiver, I.e. – “Hey can I send some data”? If the receiver is in a position to receive the data it will assert its CTS line, I.e. – “Yes you can start sending”.
-
UART Software Flow Control / UART软件流控
软件流量控制不使用额外的导线。只需要 3 根导线(RX、TX 和 GND)。 通过发送特殊的流量控制字符来启动和停止传输。流量控制字符通过正常的 TX 和 RX 线路发送。流量控制字符通常是 ASCII 码 XON 和 XOFF(0x11 和 0x13)。如果设备 A 向设备 B 发送 XOFF,则表示 B 应停止向 A 传输数据,直到 B 收到来自 A 的 XON 字符。
Software flow control does not use extra wires. Only 3 wires are required (RX, TX, and GND). Transmission is started and stopped by sending special flow control characters. The flow control characters are sent over the normal TX and RX lines. The flow control characters are typically the ASCII codes XON and XOFF (0x11 and 0x13). If device A sends XOFF to device B it means that B should halt transmission to A until B receives an XON character from A.
其他说明
1,UART驱动的支持
使用USB-to-UART bridge设备,在PC上打开串口工具,和外设通过UART进行通讯。进行软件或硬件流控的话,需要串口驱动支持才行。
需要确认外设的UART 驱动程序是否支持UART 硬件流量控制(CTS/RTS),以及是否支持软件流量控制(XON/XOFF)。
如果UART 驱动程序支持硬件流控,将使能RTS信号以开始接收新数据,并在接收完成后取消RTS有效状态。此外,UART 驱动程序还将在发送数据前读取 CTS信号,以确定是否允许向其他设备发送数据。
The UART driver will assert RTS to start to receive new data, and deassert RTS after receiving is complete. Also the UART driver will read CTS before sending data to see if it is allowed to send data to the other device.
2,关于硬件流控RTS/CTS信号有效时的电平
在使用了硬件流控的芯片手册上,标注的是低有效。
这样如果不使用硬件流控的时候,直接将CTS接地或者不接悬空即可,更加方便一些。
RTS的话,应该不用管,但也可以接地或不接。
参考:
1,SILICON LABS
AN0059.0: UART Flow Control
https://www.silabs.com/documents/public/application-notes/an0059.0-uart-flow-control.pdf
2,brainboxes
What is RTS / CTS Hardware Flow Control? - Brainboxes