@@ -40,7 +40,8 @@ using the standard stream methods::
4040
4141 To check if there is anything to be read, use::
4242
43- uart.any() # returns True if any characters waiting
43+ uart.any() # returns the number of characters waiting
44+
4445
4546 *Note: * The stream functions ``read ``, ``write ``, etc. are new in MicroPython v1.3.4.
4647 Earlier versions use ``uart.send `` and ``uart.recv ``.
@@ -57,7 +58,7 @@ Constructors
5758 initialised (it has the settings from the last initialisation of
5859 the bus, if any). If extra arguments are given, the bus is initialised.
5960 See ``init `` for parameters of initialisation.
60-
61+
6162 The physical pins of the UART busses are:
6263
6364 - ``UART(4) `` is on ``XA ``: ``(TX, RX) = (X1, X2) = (PA0, PA1) ``
@@ -66,20 +67,24 @@ Constructors
6667 - ``UART(3) `` is on ``YB ``: ``(TX, RX) = (Y9, Y10) = (PB10, PB11) ``
6768 - ``UART(2) `` is on: ``(TX, RX) = (X3, X4) = (PA2, PA3) ``
6869
70+ The Pyboard Lite supports UART(1), UART(2) and UART(6) only. Pins are as above except:
71+
72+ - ``UART(2) `` is on: ``(TX, RX) = (X1, X2) = (PA2, PA3) ``
73+
6974Methods
7075-------
7176
7277.. only :: port_pyboard
7378
74- .. method :: uart.init(baudrate, bits=8, parity=None, stop=1, \*, timeout=1000, flow=None , timeout_char=0, read_buf_len=64)
79+ .. method :: uart.init(baudrate, bits=8, parity=None, stop=1, \*, timeout=1000, flow=0 , timeout_char=0, read_buf_len=64)
7580
7681 Initialise the UART bus with the given parameters:
7782
7883 - ``baudrate `` is the clock rate.
7984 - ``bits `` is the number of bits per character, 7, 8 or 9.
8085 - ``parity `` is the parity, ``None ``, 0 (even) or 1 (odd).
8186 - ``stop `` is the number of stop bits, 1 or 2.
82- - ``flow `` sets the flow control type. Can be None , ``UART.RTS ``, ``UART.CTS ``
87+ - ``flow `` sets the flow control type. Can be 0 , ``UART.RTS ``, ``UART.CTS ``
8388 or ``UART.RTS | UART.CTS ``.
8489 - ``timeout `` is the timeout in milliseconds to wait for the first character.
8590 - ``timeout_char `` is the timeout in milliseconds to wait between characters.
@@ -103,16 +108,18 @@ Methods
103108
104109 .. method :: uart.any()
105110
106- Returns the number of characters waiting (may be 0).
111+ Returns the number of bytes waiting (may be 0).
107112
108113 .. method :: uart.writechar(char)
109114
110115 Write a single character on the bus. ``char `` is an integer to write.
111- Return value: ``None ``.
116+ Return value: ``None ``. See note below if CTS flow control is used.
112117
113118.. method :: uart.read([nbytes])
114119
115120 Read characters. If ``nbytes `` is specified then read at most that many bytes.
121+ If ``nbytes `` are available in the buffer, returns immediately, otherwise returns
122+ when sufficient characters arrive or the timeout elapses.
116123
117124 .. only :: port_pyboard
118125
@@ -124,9 +131,9 @@ Methods
124131
125132.. method :: uart.readall()
126133
127- Read as much data as possible.
134+ Read as much data as possible. Returns after the timeout has elapsed.
128135
129- Return value: a bytes object or ``None `` on timeout.
136+ Return value: a bytes object or ``None `` if timeout prevents any data being read .
130137
131138.. method :: uart.readchar()
132139
@@ -144,9 +151,11 @@ Methods
144151
145152.. method :: uart.readline()
146153
147- Read a line, ending in a newline character.
154+ Read a line, ending in a newline character. If such a line exists, return is
155+ immediate. If the timeout elapses, all available data is returned regardless
156+ of whether a newline exists.
148157
149- Return value: the line read or ``None `` on timeout.
158+ Return value: the line read or ``None `` on timeout if no data is available .
150159
151160.. method :: uart.write(buf)
152161
@@ -157,7 +166,8 @@ Methods
157166 bytes are used for each character (little endian), and ``buf `` must contain
158167 an even number of bytes.
159168
160- Return value: number of bytes written or ``None `` on timeout.
169+ Return value: number of bytes written. If a timeout occurs and no bytes
170+ were written returns ``None ``.
161171
162172.. method :: uart.sendbreak()
163173
@@ -173,4 +183,63 @@ Constants
173183 .. data :: UART.RTS
174184 .. data :: UART.CTS
175185
176- to select the flow control type
186+ to select the flow control type.
187+
188+ Flow Control
189+ ------------
190+
191+ .. only :: port_pyboard
192+
193+ On Pyboards V1 and V1.1 ``UART(2) `` and ``UART(3) `` support RTS/CTS hardware flow control
194+ using the following pins:
195+
196+ - ``UART(2) `` is on: ``(TX, RX, nRTS, nCTS) = (X3, X4, X2, X1) = (PA2, PA3, PA1, PA0) ``
197+ - ``UART(3) `` is on :``(TX, RX, nRTS, nCTS) = (Y9, Y10, Y7, Y6) = (PB10, PB11, PB14, PB13) ``
198+
199+ On the Pyboard Lite only ``UART(2) `` supports flow control on these pins:
200+
201+ ``(TX, RX, nRTS, nCTS) = (X1, X2, X4, X3) = (PA2, PA3, PA1, PA0) ``
202+
203+ In the following paragraphs the term "target" refers to the device connected to
204+ the UART.
205+
206+ When the UART's ``init() `` method is called with ``flow `` set to one or both of
207+ ``UART.RTS `` and ``UART.CTS `` the relevant flow control pins are configured.
208+ ``nRTS `` is an active low output, ``nCTS `` is an active low input with pullup
209+ enabled. To achieve flow control the Pyboard's ``nCTS `` signal should be connected
210+ to the target's ``nRTS `` and the Pyboard's ``nRTS `` to the target's ``nCTS ``.
211+
212+ CTS: target controls Pyboard transmitter
213+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
214+
215+ If CTS flow control is enabled the write behaviour is as follows:
216+
217+ If the Pyboard's ``uart.write(buf) `` method is called, transmission will stall for
218+ any periods when ``nCTS `` is ``False ``. This will result in a timeout if the entire
219+ buffer was not transmitted in the timeout period. The method returns the number of
220+ bytes written, enabling the user to write the remainder of the data if required. In
221+ the event of a timeout, a character will remain in the UART pending ``nCTS ``. The
222+ number of bytes composing this character will be included in the return value.
223+
224+ If ``uart.writechar() `` is called when ``nCTS `` is ``False `` the method will time
225+ out unless the target asserts ``nCTS `` in time. If it times out ``OSError 116 ``
226+ will be raised. The character will be transmitted as soon as the target asserts ``nCTS ``.
227+
228+ RTS: Pyboard controls target's transmitter
229+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
230+
231+ If RTS flow control is enabled, behaviour is as follows:
232+
233+ If buffered input is used (``read_buf_len `` > 0), incoming characters are buffered.
234+ If the buffer becomes full, the next character to arrive will cause ``nRTS `` to go
235+ ``False ``: the target should cease transmission. ``nRTS `` will go ``True `` when
236+ characters are read from the buffer.
237+
238+ Note that the ``any() `` method returns the number of bytes in the buffer. Assume a
239+ buffer length of ``N `` bytes. If the buffer becomes full, and another character arrives,
240+ ``nRTS `` will be set False, and ``any() `` will return the count ``N ``. When
241+ characters are read the additional character will be placed in the buffer and will
242+ be included in the result of a subsequent ``any() `` call.
243+
244+ If buffered input is not used (``read_buf_len `` == 0) the arrival of a character will
245+ cause ``nRTS `` to go ``False `` until the character is read.
0 commit comments