pyb.I2C.rst 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. .. currentmodule:: pyb
  2. .. _pyb.I2C:
  3. class I2C -- a two-wire serial protocol
  4. =======================================
  5. I2C is a two-wire protocol for communicating between devices. At the physical
  6. level it consists of 2 wires: SCL and SDA, the clock and data lines respectively.
  7. I2C objects are created attached to a specific bus. They can be initialised
  8. when created, or initialised later on.
  9. Example::
  10. from pyb import I2C
  11. i2c = I2C(1) # create on bus 1
  12. i2c = I2C(1, I2C.MASTER) # create and init as a master
  13. i2c.init(I2C.MASTER, baudrate=20000) # init as a master
  14. i2c.init(I2C.SLAVE, addr=0x42) # init as a slave with given address
  15. i2c.deinit() # turn off the peripheral
  16. Printing the i2c object gives you information about its configuration.
  17. The basic methods are send and recv::
  18. i2c.send('abc') # send 3 bytes
  19. i2c.send(0x42) # send a single byte, given by the number
  20. data = i2c.recv(3) # receive 3 bytes
  21. To receive inplace, first create a bytearray::
  22. data = bytearray(3) # create a buffer
  23. i2c.recv(data) # receive 3 bytes, writing them into data
  24. You can specify a timeout (in ms)::
  25. i2c.send(b'123', timeout=2000) # timeout after 2 seconds
  26. A master must specify the recipient's address::
  27. i2c.init(I2C.MASTER)
  28. i2c.send('123', 0x42) # send 3 bytes to slave with address 0x42
  29. i2c.send(b'456', addr=0x42) # keyword for address
  30. Master also has other methods::
  31. i2c.is_ready(0x42) # check if slave 0x42 is ready
  32. i2c.scan() # scan for slaves on the bus, returning
  33. # a list of valid addresses
  34. i2c.mem_read(3, 0x42, 2) # read 3 bytes from memory of slave 0x42,
  35. # starting at address 2 in the slave
  36. i2c.mem_write('abc', 0x42, 2, timeout=1000) # write 'abc' (3 bytes) to memory of slave 0x42
  37. # starting at address 2 in the slave, timeout after 1 second
  38. Constructors
  39. ------------
  40. .. class:: pyb.I2C(bus, ...)
  41. Construct an I2C object on the given bus. ``bus`` can be 1 or 2, 'X' or
  42. 'Y'. With no additional parameters, the I2C object is created but not
  43. initialised (it has the settings from the last initialisation of
  44. the bus, if any). If extra arguments are given, the bus is initialised.
  45. See ``init`` for parameters of initialisation.
  46. The physical pins of the I2C busses on Pyboards V1.0 and V1.1 are:
  47. - ``I2C(1)`` is on the X position: ``(SCL, SDA) = (X9, X10) = (PB6, PB7)``
  48. - ``I2C(2)`` is on the Y position: ``(SCL, SDA) = (Y9, Y10) = (PB10, PB11)``
  49. On the Pyboard Lite:
  50. - ``I2C(1)`` is on the X position: ``(SCL, SDA) = (X9, X10) = (PB6, PB7)``
  51. - ``I2C(3)`` is on the Y position: ``(SCL, SDA) = (Y9, Y10) = (PA8, PB8)``
  52. Calling the constructor with 'X' or 'Y' enables portability between Pyboard
  53. types.
  54. Methods
  55. -------
  56. .. method:: I2C.deinit()
  57. Turn off the I2C bus.
  58. .. method:: I2C.init(mode, \*, addr=0x12, baudrate=400000, gencall=False, dma=False)
  59. Initialise the I2C bus with the given parameters:
  60. - ``mode`` must be either ``I2C.MASTER`` or ``I2C.SLAVE``
  61. - ``addr`` is the 7-bit address (only sensible for a slave)
  62. - ``baudrate`` is the SCL clock rate (only sensible for a master)
  63. - ``gencall`` is whether to support general call mode
  64. - ``dma`` is whether to allow the use of DMA for the I2C transfers (note
  65. that DMA transfers have more precise timing but currently do not handle bus
  66. errors properly)
  67. .. method:: I2C.is_ready(addr)
  68. Check if an I2C device responds to the given address. Only valid when in master mode.
  69. .. method:: I2C.mem_read(data, addr, memaddr, \*, timeout=5000, addr_size=8)
  70. Read from the memory of an I2C device:
  71. - ``data`` can be an integer (number of bytes to read) or a buffer to read into
  72. - ``addr`` is the I2C device address
  73. - ``memaddr`` is the memory location within the I2C device
  74. - ``timeout`` is the timeout in milliseconds to wait for the read
  75. - ``addr_size`` selects width of memaddr: 8 or 16 bits
  76. Returns the read data.
  77. This is only valid in master mode.
  78. .. method:: I2C.mem_write(data, addr, memaddr, \*, timeout=5000, addr_size=8)
  79. Write to the memory of an I2C device:
  80. - ``data`` can be an integer or a buffer to write from
  81. - ``addr`` is the I2C device address
  82. - ``memaddr`` is the memory location within the I2C device
  83. - ``timeout`` is the timeout in milliseconds to wait for the write
  84. - ``addr_size`` selects width of memaddr: 8 or 16 bits
  85. Returns ``None``.
  86. This is only valid in master mode.
  87. .. method:: I2C.recv(recv, addr=0x00, \*, timeout=5000)
  88. Receive data on the bus:
  89. - ``recv`` can be an integer, which is the number of bytes to receive,
  90. or a mutable buffer, which will be filled with received bytes
  91. - ``addr`` is the address to receive from (only required in master mode)
  92. - ``timeout`` is the timeout in milliseconds to wait for the receive
  93. Return value: if ``recv`` is an integer then a new buffer of the bytes received,
  94. otherwise the same buffer that was passed in to ``recv``.
  95. .. method:: I2C.send(send, addr=0x00, \*, timeout=5000)
  96. Send data on the bus:
  97. - ``send`` is the data to send (an integer to send, or a buffer object)
  98. - ``addr`` is the address to send to (only required in master mode)
  99. - ``timeout`` is the timeout in milliseconds to wait for the send
  100. Return value: ``None``.
  101. .. method:: I2C.scan()
  102. Scan all I2C addresses from 0x01 to 0x7f and return a list of those that respond.
  103. Only valid when in master mode.
  104. Constants
  105. ---------
  106. .. data:: I2C.MASTER
  107. for initialising the bus to master mode
  108. .. data:: I2C.SLAVE
  109. for initialising the bus to slave mode