|
| 1 | +.. _machine.I2C: |
| 2 | + |
| 3 | +class I2C -- a two-wire serial protocol |
| 4 | +======================================= |
| 5 | + |
| 6 | +I2C is a two-wire protocol for communicating between devices. At the physical |
| 7 | +level it consists of 2 wires: SCL and SDA, the clock and data lines respectively. |
| 8 | + |
| 9 | +I2C objects are created attached to a specific bus. They can be initialised |
| 10 | +when created, or initialised later on. |
| 11 | + |
| 12 | +.. only:: port_wipy |
| 13 | + |
| 14 | + Example:: |
| 15 | + |
| 16 | + from machine import I2C |
| 17 | + |
| 18 | + i2c = I2C(0) # create on bus 0 |
| 19 | + i2c = I2C(0, I2C.MASTER) # create and init as a master |
| 20 | + i2c.init(I2C.MASTER, baudrate=20000) # init as a master |
| 21 | + i2c.deinit() # turn off the peripheral |
| 22 | + |
| 23 | +Printing the i2c object gives you information about its configuration. |
| 24 | + |
| 25 | +.. only:: port_wipy |
| 26 | + |
| 27 | + A master must specify the recipient's address:: |
| 28 | + |
| 29 | + i2c.init(I2C.MASTER) |
| 30 | + i2c.writeto(0x42, '123') # send 3 bytes to slave with address 0x42 |
| 31 | + i2c.writeto(addr=0x42, b'456') # keyword for address |
| 32 | + |
| 33 | + Master also has other methods:: |
| 34 | + |
| 35 | + i2c.scan() # scan for slaves on the bus, returning |
| 36 | + # a list of valid addresses |
| 37 | + i2c.readfrom_mem(0x42, 2, 3) # read 3 bytes from memory of slave 0x42, |
| 38 | + # starting at address 2 in the slave |
| 39 | + i2c.writeto_mem(0x42, 2, 'abc') # write 'abc' (3 bytes) to memory of slave 0x42 |
| 40 | + # starting at address 2 in the slave, timeout after 1 second |
| 41 | + |
| 42 | +Constructors |
| 43 | +------------ |
| 44 | + |
| 45 | +.. only:: port_wipy |
| 46 | + |
| 47 | + .. class:: machine.I2C(bus, ...) |
| 48 | + |
| 49 | + Construct an I2C object on the given bus. `bus` can only be 0. |
| 50 | + If the bus is not given, the default one will be selected (0). |
| 51 | + |
| 52 | +Methods |
| 53 | +------- |
| 54 | + |
| 55 | +.. method:: i2c.deinit() |
| 56 | + |
| 57 | + Turn off the I2C bus. |
| 58 | + |
| 59 | +.. only:: port_wipy |
| 60 | + |
| 61 | + .. method:: i2c.init(mode, \*, baudrate=100000, pins=(SDA, SCL)) |
| 62 | + |
| 63 | + Initialise the I2C bus with the given parameters: |
| 64 | + |
| 65 | + - ``mode`` must be ``I2C.MASTER`` |
| 66 | + - ``baudrate`` is the SCL clock rate |
| 67 | + - ``pins`` is an optional tuple with the pins to assign to the I2C bus. |
| 68 | + |
| 69 | + .. method:: i2c.readfrom(addr, nbytes) |
| 70 | + |
| 71 | + Read ``nbytes`` from the slave specified by ``addr``. |
| 72 | + Returns a ``bytes`` object with the data read. |
| 73 | + |
| 74 | + .. method:: i2c.readfrom_into(addr, buf) |
| 75 | + |
| 76 | + Read into ``buf`` from the slave specified by ``addr``. |
| 77 | + Returns the number of bytes read. |
| 78 | + |
| 79 | + .. method:: i2c.writeto(addr, buf, \*, stop=True) |
| 80 | + |
| 81 | + Write ``buf`` to the slave specified by ``addr``. Set ``stop`` to ``False`` |
| 82 | + if the transfer should be continued. |
| 83 | + Returns the number of bytes written. |
| 84 | + |
| 85 | + .. method:: i2c.readfrom_mem(addr, memaddr, nbytes, \*, addrsize=8) |
| 86 | + |
| 87 | + Read ``nbytes`` from the slave specified by ``addr`` starting from the memory |
| 88 | + address specified by ``memaddr``. |
| 89 | + Param ``addrsize`` specifies the address size in bits. |
| 90 | + Returns a ``bytes`` object with the data read. |
| 91 | + |
| 92 | + .. method:: i2c.readfrom_mem_into(addr, memaddr, buf, \*, addrsize=8) |
| 93 | + |
| 94 | + Read into ``buf`` from the slave specified by ``addr`` starting from the memory |
| 95 | + address specified by ``memaddr``. |
| 96 | + Param ``addrsize`` specifies the address size in bits. |
| 97 | + Returns the number of bytes read. |
| 98 | + |
| 99 | + .. method:: i2c.writeto_mem(addr, memaddr, buf, \*, addrsize=8) |
| 100 | + |
| 101 | + Write ``buf`` to the slave specified by ``addr`` starting from the |
| 102 | + memory address specified by ``memaddr``. Param ``addrsize`` specifies the |
| 103 | + address size in bits. |
| 104 | + Set ``stop`` to ``False`` if the transfer should be continued. |
| 105 | + Returns the number of bytes written. |
| 106 | + |
| 107 | +.. method:: i2c.scan() |
| 108 | + |
| 109 | + Scan all I2C addresses from 0x01 to 0x7f and return a list of those that respond. |
| 110 | + Only valid when in master mode. |
| 111 | + |
| 112 | +Constants |
| 113 | +--------- |
| 114 | + |
| 115 | +.. data:: I2C.MASTER |
| 116 | + |
| 117 | + for initialising the bus to master mode |
0 commit comments