Skip to content

Commit 01edc0e

Browse files
authored
Fix and add details to Serial doc (#7521)
Fixes #7484 Clarify blocking case for write() Add flush() method. Missing ), clarifications
1 parent 683b8e6 commit 01edc0e

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

doc/reference.rst

+29-11
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,31 @@ milliseconds is not recommended.
151151
Serial
152152
------
153153

154-
``Serial`` object works much the same way as on a regular Arduino. Apart
155-
from hardware FIFO (128 bytes for TX and RX) ``Serial`` has
156-
additional 256-byte TX and RX buffers. Both transmit and receive is
157-
interrupt-driven. Write and read functions only block the sketch
158-
execution when the respective FIFO/buffers are full/empty. Note that
159-
the length of additional 256-bit buffer can be customized.
154+
The ``Serial`` object works much the same way as on a regular Arduino. Apart
155+
from the hardware FIFO (128 bytes for TX and RX), ``Serial`` has an
156+
additional customizable 256-byte RX buffer. The size of this software buffer can
157+
be changed by the user. It is suggested to use a bigger size at higher receive speeds.
158+
159+
The ``::setRxBufferSize(size_t size)`` method changes the RX buffer size as needed. This
160+
should be called before ``::begin()``. The size argument should be at least large enough
161+
to hold all data received before reading.
162+
163+
For transmit-only operation, the 256-byte RX buffer can be switched off to save RAM by
164+
passing mode SERIAL_TX_ONLY to Serial.begin(). Other modes are SERIAL_RX_ONLY and
165+
SERIAL_FULL (the default).
166+
167+
Receive is interrupt-driven, but transmit polls and busy-waits. Blocking behavior is as follows:
168+
The ``::write()`` call does not block if the number of bytes fits in the current space available
169+
in the TX FIFO. The call blocks if the TX FIFO is full and waits until there is room before
170+
writing more bytes into it, until all bytes are written. In other words, when the call returns,
171+
all bytes have been written to the TX FIFO, but that doesn't mean that all bytes have been sent
172+
out through the serial line yet.
173+
The ``::read()`` call doesn't block, not even if there are no bytes available for reading.
174+
The ``::readBytes()`` call blocks until the number of bytes read complies with the number of
175+
bytes required by the argument passed in.
176+
The ``::flush()`` call blocks waiting for the TX FIFO to be empty before returning. It is
177+
recommended to call this to make sure all bytes have been sent before doing configuration changes
178+
on the serial port (e.g. changing baudrate) or doing a board reset.
160179

161180
``Serial`` uses UART0, which is mapped to pins GPIO1 (TX) and GPIO3
162181
(RX). Serial may be remapped to GPIO15 (TX) and GPIO13 (RX) by calling
@@ -180,14 +199,13 @@ instead, call ``Serial1.setDebugOutput(true)``.
180199
You also need to use ``Serial.setDebugOutput(true)`` to enable output
181200
from ``printf()`` function.
182201

183-
The method ``Serial.setRxBufferSize(size_t size)`` allows to define the
184-
receiving buffer depth. The default value is 256.
185-
186202
Both ``Serial`` and ``Serial1`` objects support 5, 6, 7, 8 data bits,
187203
odd (O), even (E), and no (N) parity, and 1 or 2 stop bits. To set the
188204
desired mode, call ``Serial.begin(baudrate, SERIAL_8N1)``,
189205
``Serial.begin(baudrate, SERIAL_6E2)``, etc.
190-
206+
Default configuration mode is SERIAL_8N1. Possibilities are SERIAL_[5678][NEO][12].
207+
Example: ``SERIAL_8N1`` means 8bits No parity 1 stop bit.
208+
191209
A new method has been implemented on both ``Serial`` and ``Serial1`` to
192210
get current baud rate setting. To get the current baud rate, call
193211
``Serial.baudRate()``, ``Serial1.baudRate()``. Return a ``int`` of
@@ -206,7 +224,7 @@ current speed. For example
206224
207225
| ``Serial`` and ``Serial1`` objects are both instances of the
208226
``HardwareSerial`` class.
209-
| I've done this also for official ESP8266 `Software
227+
| This is also done for official ESP8266 `Software
210228
Serial <libraries.rst#softwareserial>`__
211229
library, see this `pull
212230
request <https://github.com/plerup/espsoftwareserial/pull/22>`__.

0 commit comments

Comments
 (0)