pyb.UART.rst 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. .. currentmodule:: pyb
  2. .. _pyb.UART:
  3. class UART -- duplex serial communication bus
  4. =============================================
  5. UART implements the standard UART/USART duplex serial communications protocol. At
  6. the physical level it consists of 2 lines: RX and TX. The unit of communication
  7. is a character (not to be confused with a string character) which can be 8 or 9
  8. bits wide.
  9. UART objects can be created and initialised using::
  10. from pyb import UART
  11. uart = UART(1, 9600) # init with given baudrate
  12. uart.init(9600, bits=8, parity=None, stop=1) # init with given parameters
  13. Bits can be 7, 8 or 9. Parity can be None, 0 (even) or 1 (odd). Stop can be 1 or 2.
  14. *Note:* with parity=None, only 8 and 9 bits are supported. With parity enabled,
  15. only 7 and 8 bits are supported.
  16. A UART object acts like a `stream` object and reading and writing is done
  17. using the standard stream methods::
  18. uart.read(10) # read 10 characters, returns a bytes object
  19. uart.read() # read all available characters
  20. uart.readline() # read a line
  21. uart.readinto(buf) # read and store into the given buffer
  22. uart.write('abc') # write the 3 characters
  23. Individual characters can be read/written using::
  24. uart.readchar() # read 1 character and returns it as an integer
  25. uart.writechar(42) # write 1 character
  26. To check if there is anything to be read, use::
  27. uart.any() # returns the number of characters waiting
  28. *Note:* The stream functions ``read``, ``write``, etc. are new in MicroPython v1.3.4.
  29. Earlier versions use ``uart.send`` and ``uart.recv``.
  30. Constructors
  31. ------------
  32. .. class:: pyb.UART(bus, ...)
  33. Construct a UART object on the given bus. ``bus`` can be 1-6, or 'XA', 'XB', 'YA', or 'YB'.
  34. With no additional parameters, the UART object is created but not
  35. initialised (it has the settings from the last initialisation of
  36. the bus, if any). If extra arguments are given, the bus is initialised.
  37. See ``init`` for parameters of initialisation.
  38. The physical pins of the UART busses are:
  39. - ``UART(4)`` is on ``XA``: ``(TX, RX) = (X1, X2) = (PA0, PA1)``
  40. - ``UART(1)`` is on ``XB``: ``(TX, RX) = (X9, X10) = (PB6, PB7)``
  41. - ``UART(6)`` is on ``YA``: ``(TX, RX) = (Y1, Y2) = (PC6, PC7)``
  42. - ``UART(3)`` is on ``YB``: ``(TX, RX) = (Y9, Y10) = (PB10, PB11)``
  43. - ``UART(2)`` is on: ``(TX, RX) = (X3, X4) = (PA2, PA3)``
  44. The Pyboard Lite supports UART(1), UART(2) and UART(6) only. Pins are as above except:
  45. - ``UART(2)`` is on: ``(TX, RX) = (X1, X2) = (PA2, PA3)``
  46. Methods
  47. -------
  48. .. method:: UART.init(baudrate, bits=8, parity=None, stop=1, \*, timeout=1000, flow=0, timeout_char=0, read_buf_len=64)
  49. Initialise the UART bus with the given parameters:
  50. - ``baudrate`` is the clock rate.
  51. - ``bits`` is the number of bits per character, 7, 8 or 9.
  52. - ``parity`` is the parity, ``None``, 0 (even) or 1 (odd).
  53. - ``stop`` is the number of stop bits, 1 or 2.
  54. - ``flow`` sets the flow control type. Can be 0, ``UART.RTS``, ``UART.CTS``
  55. or ``UART.RTS | UART.CTS``.
  56. - ``timeout`` is the timeout in milliseconds to wait for writing/reading the first character.
  57. - ``timeout_char`` is the timeout in milliseconds to wait between characters while writing or reading.
  58. - ``read_buf_len`` is the character length of the read buffer (0 to disable).
  59. This method will raise an exception if the baudrate could not be set within
  60. 5% of the desired value. The minimum baudrate is dictated by the frequency
  61. of the bus that the UART is on; UART(1) and UART(6) are APB2, the rest are on
  62. APB1. The default bus frequencies give a minimum baudrate of 1300 for
  63. UART(1) and UART(6) and 650 for the others. Use :func:`pyb.freq <pyb.freq>`
  64. to reduce the bus frequencies to get lower baudrates.
  65. *Note:* with parity=None, only 8 and 9 bits are supported. With parity enabled,
  66. only 7 and 8 bits are supported.
  67. .. method:: UART.deinit()
  68. Turn off the UART bus.
  69. .. method:: UART.any()
  70. Returns the number of bytes waiting (may be 0).
  71. .. method:: UART.read([nbytes])
  72. Read characters. If ``nbytes`` is specified then read at most that many bytes.
  73. If ``nbytes`` are available in the buffer, returns immediately, otherwise returns
  74. when sufficient characters arrive or the timeout elapses.
  75. If ``nbytes`` is not given then the method reads as much data as possible. It
  76. returns after the timeout has elapsed.
  77. *Note:* for 9 bit characters each character takes two bytes, ``nbytes`` must
  78. be even, and the number of characters is ``nbytes/2``.
  79. Return value: a bytes object containing the bytes read in. Returns ``None``
  80. on timeout.
  81. .. method:: UART.readchar()
  82. Receive a single character on the bus.
  83. Return value: The character read, as an integer. Returns -1 on timeout.
  84. .. method:: UART.readinto(buf[, nbytes])
  85. Read bytes into the ``buf``. If ``nbytes`` is specified then read at most
  86. that many bytes. Otherwise, read at most ``len(buf)`` bytes.
  87. Return value: number of bytes read and stored into ``buf`` or ``None`` on
  88. timeout.
  89. .. method:: UART.readline()
  90. Read a line, ending in a newline character. If such a line exists, return is
  91. immediate. If the timeout elapses, all available data is returned regardless
  92. of whether a newline exists.
  93. Return value: the line read or ``None`` on timeout if no data is available.
  94. .. method:: UART.write(buf)
  95. Write the buffer of bytes to the bus. If characters are 7 or 8 bits wide
  96. then each byte is one character. If characters are 9 bits wide then two
  97. bytes are used for each character (little endian), and ``buf`` must contain
  98. an even number of bytes.
  99. Return value: number of bytes written. If a timeout occurs and no bytes
  100. were written returns ``None``.
  101. .. method:: UART.writechar(char)
  102. Write a single character on the bus. ``char`` is an integer to write.
  103. Return value: ``None``. See note below if CTS flow control is used.
  104. .. method:: UART.sendbreak()
  105. Send a break condition on the bus. This drives the bus low for a duration
  106. of 13 bits.
  107. Return value: ``None``.
  108. Constants
  109. ---------
  110. .. data:: UART.RTS
  111. UART.CTS
  112. to select the flow control type.
  113. Flow Control
  114. ------------
  115. On Pyboards V1 and V1.1 ``UART(2)`` and ``UART(3)`` support RTS/CTS hardware flow control
  116. using the following pins:
  117. - ``UART(2)`` is on: ``(TX, RX, nRTS, nCTS) = (X3, X4, X2, X1) = (PA2, PA3, PA1, PA0)``
  118. - ``UART(3)`` is on :``(TX, RX, nRTS, nCTS) = (Y9, Y10, Y7, Y6) = (PB10, PB11, PB14, PB13)``
  119. On the Pyboard Lite only ``UART(2)`` supports flow control on these pins:
  120. ``(TX, RX, nRTS, nCTS) = (X1, X2, X4, X3) = (PA2, PA3, PA1, PA0)``
  121. In the following paragraphs the term "target" refers to the device connected to
  122. the UART.
  123. When the UART's ``init()`` method is called with ``flow`` set to one or both of
  124. ``UART.RTS`` and ``UART.CTS`` the relevant flow control pins are configured.
  125. ``nRTS`` is an active low output, ``nCTS`` is an active low input with pullup
  126. enabled. To achieve flow control the Pyboard's ``nCTS`` signal should be connected
  127. to the target's ``nRTS`` and the Pyboard's ``nRTS`` to the target's ``nCTS``.
  128. CTS: target controls Pyboard transmitter
  129. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  130. If CTS flow control is enabled the write behaviour is as follows:
  131. If the Pyboard's ``UART.write(buf)`` method is called, transmission will stall for
  132. any periods when ``nCTS`` is ``False``. This will result in a timeout if the entire
  133. buffer was not transmitted in the timeout period. The method returns the number of
  134. bytes written, enabling the user to write the remainder of the data if required. In
  135. the event of a timeout, a character will remain in the UART pending ``nCTS``. The
  136. number of bytes composing this character will be included in the return value.
  137. If ``UART.writechar()`` is called when ``nCTS`` is ``False`` the method will time
  138. out unless the target asserts ``nCTS`` in time. If it times out ``OSError 116``
  139. will be raised. The character will be transmitted as soon as the target asserts ``nCTS``.
  140. RTS: Pyboard controls target's transmitter
  141. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  142. If RTS flow control is enabled, behaviour is as follows:
  143. If buffered input is used (``read_buf_len`` > 0), incoming characters are buffered.
  144. If the buffer becomes full, the next character to arrive will cause ``nRTS`` to go
  145. ``False``: the target should cease transmission. ``nRTS`` will go ``True`` when
  146. characters are read from the buffer.
  147. Note that the ``any()`` method returns the number of bytes in the buffer. Assume a
  148. buffer length of ``N`` bytes. If the buffer becomes full, and another character arrives,
  149. ``nRTS`` will be set False, and ``any()`` will return the count ``N``. When
  150. characters are read the additional character will be placed in the buffer and will
  151. be included in the result of a subsequent ``any()`` call.
  152. If buffered input is not used (``read_buf_len`` == 0) the arrival of a character will
  153. cause ``nRTS`` to go ``False`` until the character is read.