machine.UART.rst 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. .. currentmodule:: machine
  2. .. _machine.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 machine 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. Supported parameters differ on a board:
  14. Pyboard: Bits can be 7, 8 or 9. Stop can be 1 or 2. With *parity=None*,
  15. only 8 and 9 bits are supported. With parity enabled, only 7 and 8 bits
  16. are supported.
  17. WiPy/CC3200: Bits can be 5, 6, 7, 8. Stop can be 1 or 2.
  18. A UART object acts like a `stream` object and reading and writing is done
  19. using the standard stream methods::
  20. uart.read(10) # read 10 characters, returns a bytes object
  21. uart.read() # read all available characters
  22. uart.readline() # read a line
  23. uart.readinto(buf) # read and store into the given buffer
  24. uart.write('abc') # write the 3 characters
  25. Constructors
  26. ------------
  27. .. class:: UART(id, ...)
  28. Construct a UART object of the given id.
  29. Methods
  30. -------
  31. .. method:: UART.init(baudrate=9600, bits=8, parity=None, stop=1, \*, ...)
  32. Initialise the UART bus with the given parameters:
  33. - *baudrate* is the clock rate.
  34. - *bits* is the number of bits per character, 7, 8 or 9.
  35. - *parity* is the parity, ``None``, 0 (even) or 1 (odd).
  36. - *stop* is the number of stop bits, 1 or 2.
  37. Additional keyword-only parameters that may be supported by a port are:
  38. - *tx* specifies the TX pin to use.
  39. - *rx* specifies the RX pin to use.
  40. On the WiPy only the following keyword-only parameter is supported:
  41. - *pins* is a 4 or 2 item list indicating the TX, RX, RTS and CTS pins (in that order).
  42. Any of the pins can be None if one wants the UART to operate with limited functionality.
  43. If the RTS pin is given the the RX pin must be given as well. The same applies to CTS.
  44. When no pins are given, then the default set of TX and RX pins is taken, and hardware
  45. flow control will be disabled. If *pins* is ``None``, no pin assignment will be made.
  46. .. method:: UART.deinit()
  47. Turn off the UART bus.
  48. .. method:: UART.any()
  49. Returns an integer counting the number of characters that can be read without
  50. blocking. It will return 0 if there are no characters available and a positive
  51. number if there are characters. The method may return 1 even if there is more
  52. than one character available for reading.
  53. For more sophisticated querying of available characters use select.poll::
  54. poll = select.poll()
  55. poll.register(uart, select.POLLIN)
  56. poll.poll(timeout)
  57. .. method:: UART.read([nbytes])
  58. Read characters. If ``nbytes`` is specified then read at most that many bytes,
  59. otherwise read as much data as possible.
  60. Return value: a bytes object containing the bytes read in. Returns ``None``
  61. on timeout.
  62. .. method:: UART.readinto(buf[, nbytes])
  63. Read bytes into the ``buf``. If ``nbytes`` is specified then read at most
  64. that many bytes. Otherwise, read at most ``len(buf)`` bytes.
  65. Return value: number of bytes read and stored into ``buf`` or ``None`` on
  66. timeout.
  67. .. method:: UART.readline()
  68. Read a line, ending in a newline character.
  69. Return value: the line read or ``None`` on timeout.
  70. .. method:: UART.write(buf)
  71. Write the buffer of bytes to the bus.
  72. Return value: number of bytes written or ``None`` on timeout.
  73. .. method:: UART.sendbreak()
  74. Send a break condition on the bus. This drives the bus low for a duration
  75. longer than required for a normal transmission of a character.
  76. .. method:: UART.irq(trigger, priority=1, handler=None, wake=machine.IDLE)
  77. Create a callback to be triggered when data is received on the UART.
  78. - *trigger* can only be ``UART.RX_ANY``
  79. - *priority* level of the interrupt. Can take values in the range 1-7.
  80. Higher values represent higher priorities.
  81. - *handler* an optional function to be called when new characters arrive.
  82. - *wake* can only be ``machine.IDLE``.
  83. .. note::
  84. The handler will be called whenever any of the following two conditions are met:
  85. - 8 new characters have been received.
  86. - At least 1 new character is waiting in the Rx buffer and the Rx line has been
  87. silent for the duration of 1 complete frame.
  88. This means that when the handler function is called there will be between 1 to 8
  89. characters waiting.
  90. Returns an irq object.
  91. Availability: WiPy.
  92. Constants
  93. ---------
  94. .. data:: UART.RX_ANY
  95. IRQ trigger sources
  96. Availability: WiPy.