|
@ -9,86 +9,55 @@ level it consists of 2 wires: SCL and SDA, the clock and data lines respectively |
|
|
I2C objects are created attached to a specific bus. They can be initialised |
|
|
I2C objects are created attached to a specific bus. They can be initialised |
|
|
when created, or initialised later on. |
|
|
when created, or initialised later on. |
|
|
|
|
|
|
|
|
.. only:: port_wipy |
|
|
Printing the I2C object gives you information about its configuration. |
|
|
|
|
|
|
|
|
Example:: |
|
|
Example usage:: |
|
|
|
|
|
|
|
|
from machine import I2C |
|
|
from machine import I2C |
|
|
|
|
|
|
|
|
i2c = I2C(0) # create on bus 0 |
|
|
i2c = I2C(freq=400000) # create I2C peripheral at frequency of 400kHz |
|
|
i2c = I2C(0, I2C.MASTER) # create and init as a master |
|
|
# depending on the port, extra parameters may be required |
|
|
i2c.init(I2C.MASTER, baudrate=20000) # init as a master |
|
|
# to select the peripheral and/or pins to use |
|
|
i2c.deinit() # turn off the peripheral |
|
|
|
|
|
|
|
|
|
|
|
Printing the i2c object gives you information about its configuration. |
|
|
i2c.scan() # scan for slaves, returning a list of 7-bit addresses |
|
|
|
|
|
|
|
|
.. only:: port_wipy |
|
|
i2c.writeto(42, b'123') # write 3 bytes to slave with 7-bit address 42 |
|
|
|
|
|
i2c.readfrom(42, 4) # read 4 bytes from slave with 7-bit address 42 |
|
|
|
|
|
|
|
|
A master must specify the recipient's address:: |
|
|
i2c.readfrom_mem(42, 8, 3) # read 3 bytes from memory of slave 42, |
|
|
|
|
|
# starting at memory-address 8 in the slave |
|
|
i2c.init(I2C.MASTER) |
|
|
i2c.writeto_mem(42, 2, b'\x10') # write 1 byte to memory of slave 42 |
|
|
i2c.writeto(0x42, '123') # send 3 bytes to slave with address 0x42 |
|
|
# starting at address 2 in the slave |
|
|
i2c.writeto(addr=0x42, b'456') # keyword for address |
|
|
|
|
|
|
|
|
|
|
|
Master also has other methods:: |
|
|
|
|
|
|
|
|
|
|
|
i2c.scan() # scan for slaves on the bus, returning |
|
|
|
|
|
# a list of valid addresses |
|
|
|
|
|
i2c.readfrom_mem(0x42, 2, 3) # read 3 bytes from memory of slave 0x42, |
|
|
|
|
|
# starting at address 2 in the slave |
|
|
|
|
|
i2c.writeto_mem(0x42, 2, 'abc') # write 'abc' (3 bytes) to memory of slave 0x42 |
|
|
|
|
|
# starting at address 2 in the slave, timeout after 1 second |
|
|
|
|
|
|
|
|
|
|
|
Constructors |
|
|
Constructors |
|
|
------------ |
|
|
------------ |
|
|
|
|
|
|
|
|
.. only:: port_wipy |
|
|
.. class:: I2C(id=-1, \*, scl, sda, freq=400000) |
|
|
|
|
|
|
|
|
.. class:: I2C(bus, ...) |
|
|
|
|
|
|
|
|
|
|
|
Construct an I2C object on the given bus. `bus` can only be 0. |
|
|
|
|
|
If the bus is not given, the default one will be selected (0). |
|
|
|
|
|
|
|
|
|
|
|
.. only:: not port_wipy |
|
|
Construct and return a new I2C object using the following parameters: |
|
|
|
|
|
|
|
|
.. class:: I2C(id=-1, \*, scl, sda, freq=400000) |
|
|
- `id` identifies the particular I2C peripheral. The default |
|
|
|
|
|
value of -1 selects a software implementation of I2C which can |
|
|
Construct and return a new I2C object using the following parameters: |
|
|
work (in most cases) with arbitrary pins for SCL and SDA. |
|
|
|
|
|
If `id` is -1 then `scl` and `sda` must be specified. Other |
|
|
- `id` identifies the particular I2C peripheral. The default |
|
|
allowed values for `id` depend on the particular port/board, |
|
|
value of -1 selects a software implementation of I2C which can |
|
|
and specifying `scl` and `sda` may or may not be required or |
|
|
work (in most cases) with arbitrary pins for SCL and SDA. |
|
|
allowed in this case. |
|
|
If `id` is -1 then `scl` and `sda` must be specified. Other |
|
|
- `scl` should be a pin object specifying the pin to use for SCL. |
|
|
allowed values for `id` depend on the particular port/board, |
|
|
- `sda` should be a pin object specifying the pin to use for SDA. |
|
|
and specifying `scl` and `sda` may or may not be required or |
|
|
- `freq` should be an integer which sets the maximum frequency |
|
|
allowed in this case. |
|
|
for SCL. |
|
|
- `scl` should be a pin object specifying the pin to use for SCL. |
|
|
|
|
|
- `sda` should be a pin object specifying the pin to use for SDA. |
|
|
|
|
|
- `freq` should be an integer which sets the maximum frequency |
|
|
|
|
|
for SCL. |
|
|
|
|
|
|
|
|
|
|
|
General Methods |
|
|
General Methods |
|
|
--------------- |
|
|
--------------- |
|
|
|
|
|
|
|
|
.. only:: port_wipy |
|
|
.. method:: I2C.init(scl, sda, \*, freq=400000) |
|
|
|
|
|
|
|
|
.. method:: I2C.init(mode, \*, baudrate=100000, pins=(SDA, SCL)) |
|
|
|
|
|
|
|
|
|
|
|
Initialise the I2C bus with the given parameters: |
|
|
|
|
|
|
|
|
|
|
|
- ``mode`` must be ``I2C.MASTER`` |
|
|
Initialise the I2C bus with the given arguments: |
|
|
- ``baudrate`` is the SCL clock rate |
|
|
|
|
|
- ``pins`` is an optional tuple with the pins to assign to the I2C bus. |
|
|
|
|
|
|
|
|
|
|
|
.. only:: port_esp8266 |
|
|
- `scl` is a pin object for the SCL line |
|
|
|
|
|
- `sda` is a pin object for the SDA line |
|
|
.. method:: I2C.init(scl, sda, \*, freq=400000) |
|
|
- `freq` is the SCL clock rate |
|
|
|
|
|
|
|
|
Initialise the I2C bus with the given arguments: |
|
|
|
|
|
|
|
|
|
|
|
- `scl` is a pin object for the SCL line |
|
|
|
|
|
- `sda` is a pin object for the SDA line |
|
|
|
|
|
- `freq` is the SCL clock rate |
|
|
|
|
|
|
|
|
|
|
|
.. method:: I2C.deinit() |
|
|
.. method:: I2C.deinit() |
|
|
|
|
|
|
|
@ -102,8 +71,6 @@ General Methods |
|
|
those that respond. A device responds if it pulls the SDA line low after |
|
|
those that respond. A device responds if it pulls the SDA line low after |
|
|
its address (including a write bit) is sent on the bus. |
|
|
its address (including a write bit) is sent on the bus. |
|
|
|
|
|
|
|
|
Note: on WiPy the I2C object must be in master mode for this method to be valid. |
|
|
|
|
|
|
|
|
|
|
|
Primitive I2C operations |
|
|
Primitive I2C operations |
|
|
------------------------ |
|
|
------------------------ |
|
|
|
|
|
|
|
@ -204,12 +171,3 @@ methods are convenience functions to communicate with such devices. |
|
|
|
|
|
|
|
|
On WiPy the return value is the number of bytes written. Otherwise the |
|
|
On WiPy the return value is the number of bytes written. Otherwise the |
|
|
return value is `None`. |
|
|
return value is `None`. |
|
|
|
|
|
|
|
|
Constants |
|
|
|
|
|
--------- |
|
|
|
|
|
|
|
|
|
|
|
.. data:: I2C.MASTER |
|
|
|
|
|
|
|
|
|
|
|
for initialising the bus to master mode |
|
|
|
|
|
|
|
|
|
|
|
Availability: WiPy. |
|
|
|
|
|