machine.I2C.rst 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. .. currentmodule:: machine
  2. .. _machine.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. Printing the I2C object gives you information about its configuration.
  10. Example usage::
  11. from machine import I2C
  12. i2c = I2C(freq=400000) # create I2C peripheral at frequency of 400kHz
  13. # depending on the port, extra parameters may be required
  14. # to select the peripheral and/or pins to use
  15. i2c.scan() # scan for slaves, returning a list of 7-bit addresses
  16. i2c.writeto(42, b'123') # write 3 bytes to slave with 7-bit address 42
  17. i2c.readfrom(42, 4) # read 4 bytes from slave with 7-bit address 42
  18. i2c.readfrom_mem(42, 8, 3) # read 3 bytes from memory of slave 42,
  19. # starting at memory-address 8 in the slave
  20. i2c.writeto_mem(42, 2, b'\x10') # write 1 byte to memory of slave 42
  21. # starting at address 2 in the slave
  22. Constructors
  23. ------------
  24. .. class:: I2C(id=-1, \*, scl, sda, freq=400000)
  25. Construct and return a new I2C object using the following parameters:
  26. - *id* identifies a particular I2C peripheral. The default
  27. value of -1 selects a software implementation of I2C which can
  28. work (in most cases) with arbitrary pins for SCL and SDA.
  29. If *id* is -1 then *scl* and *sda* must be specified. Other
  30. allowed values for *id* depend on the particular port/board,
  31. and specifying *scl* and *sda* may or may not be required or
  32. allowed in this case.
  33. - *scl* should be a pin object specifying the pin to use for SCL.
  34. - *sda* should be a pin object specifying the pin to use for SDA.
  35. - *freq* should be an integer which sets the maximum frequency
  36. for SCL.
  37. General Methods
  38. ---------------
  39. .. method:: I2C.init(scl, sda, \*, freq=400000)
  40. Initialise the I2C bus with the given arguments:
  41. - *scl* is a pin object for the SCL line
  42. - *sda* is a pin object for the SDA line
  43. - *freq* is the SCL clock rate
  44. .. method:: I2C.deinit()
  45. Turn off the I2C bus.
  46. Availability: WiPy.
  47. .. method:: I2C.scan()
  48. Scan all I2C addresses between 0x08 and 0x77 inclusive and return a list of
  49. those that respond. A device responds if it pulls the SDA line low after
  50. its address (including a write bit) is sent on the bus.
  51. Primitive I2C operations
  52. ------------------------
  53. The following methods implement the primitive I2C master bus operations and can
  54. be combined to make any I2C transaction. They are provided if you need more
  55. control over the bus, otherwise the standard methods (see below) can be used.
  56. .. method:: I2C.start()
  57. Generate a START condition on the bus (SDA transitions to low while SCL is high).
  58. Availability: ESP8266.
  59. .. method:: I2C.stop()
  60. Generate a STOP condition on the bus (SDA transitions to high while SCL is high).
  61. Availability: ESP8266.
  62. .. method:: I2C.readinto(buf, nack=True)
  63. Reads bytes from the bus and stores them into *buf*. The number of bytes
  64. read is the length of *buf*. An ACK will be sent on the bus after
  65. receiving all but the last byte. After the last byte is received, if *nack*
  66. is true then a NACK will be sent, otherwise an ACK will be sent (and in this
  67. case the slave assumes more bytes are going to be read in a later call).
  68. Availability: ESP8266.
  69. .. method:: I2C.write(buf)
  70. Write the bytes from *buf* to the bus. Checks that an ACK is received
  71. after each byte and stops transmitting the remaining bytes if a NACK is
  72. received. The function returns the number of ACKs that were received.
  73. Availability: ESP8266.
  74. Standard bus operations
  75. -----------------------
  76. The following methods implement the standard I2C master read and write
  77. operations that target a given slave device.
  78. .. method:: I2C.readfrom(addr, nbytes, stop=True)
  79. Read *nbytes* from the slave specified by *addr*.
  80. If *stop* is true then a STOP condition is generated at the end of the transfer.
  81. Returns a `bytes` object with the data read.
  82. .. method:: I2C.readfrom_into(addr, buf, stop=True)
  83. Read into *buf* from the slave specified by *addr*.
  84. The number of bytes read will be the length of *buf*.
  85. If *stop* is true then a STOP condition is generated at the end of the transfer.
  86. The method returns ``None``.
  87. .. method:: I2C.writeto(addr, buf, stop=True)
  88. Write the bytes from *buf* to the slave specified by *addr*. If a
  89. NACK is received following the write of a byte from *buf* then the
  90. remaining bytes are not sent. If *stop* is true then a STOP condition is
  91. generated at the end of the transfer, even if a NACK is received.
  92. The function returns the number of ACKs that were received.
  93. Memory operations
  94. -----------------
  95. Some I2C devices act as a memory device (or set of registers) that can be read
  96. from and written to. In this case there are two addresses associated with an
  97. I2C transaction: the slave address and the memory address. The following
  98. methods are convenience functions to communicate with such devices.
  99. .. method:: I2C.readfrom_mem(addr, memaddr, nbytes, \*, addrsize=8)
  100. Read *nbytes* from the slave specified by *addr* starting from the memory
  101. address specified by *memaddr*.
  102. The argument *addrsize* specifies the address size in bits.
  103. Returns a `bytes` object with the data read.
  104. .. method:: I2C.readfrom_mem_into(addr, memaddr, buf, \*, addrsize=8)
  105. Read into *buf* from the slave specified by *addr* starting from the
  106. memory address specified by *memaddr*. The number of bytes read is the
  107. length of *buf*.
  108. The argument *addrsize* specifies the address size in bits (on ESP8266
  109. this argument is not recognised and the address size is always 8 bits).
  110. The method returns ``None``.
  111. .. method:: I2C.writeto_mem(addr, memaddr, buf, \*, addrsize=8)
  112. Write *buf* to the slave specified by *addr* starting from the
  113. memory address specified by *memaddr*.
  114. The argument *addrsize* specifies the address size in bits (on ESP8266
  115. this argument is not recognised and the address size is always 8 bits).
  116. The method returns ``None``.