2016-06-07 17:46:27 -04:00
|
|
|
.. currentmodule:: machine
|
2017-04-18 01:27:37 -04:00
|
|
|
.. _machine.I2C:
|
2015-10-14 06:32:01 -04:00
|
|
|
|
|
|
|
class I2C -- a two-wire serial protocol
|
|
|
|
=======================================
|
|
|
|
|
|
|
|
I2C is a two-wire protocol for communicating between devices. At the physical
|
|
|
|
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
|
|
|
|
when created, or initialised later on.
|
|
|
|
|
2017-04-18 01:04:51 -04:00
|
|
|
Printing the I2C object gives you information about its configuration.
|
2015-10-14 06:32:01 -04:00
|
|
|
|
2020-09-29 02:50:23 -04:00
|
|
|
Both hardware and software I2C implementations exist via the
|
|
|
|
:ref:`machine.I2C <machine.I2C>` and `machine.SoftI2C` classes. Hardware I2C uses
|
|
|
|
underlying hardware support of the system to perform the reads/writes and is
|
|
|
|
usually efficient and fast but may have restrictions on which pins can be used.
|
|
|
|
Software I2C is implemented by bit-banging and can be used on any pin but is not
|
|
|
|
as efficient. These classes have the same methods available and differ primarily
|
|
|
|
in the way they are constructed.
|
|
|
|
|
2017-04-18 01:04:51 -04:00
|
|
|
Example usage::
|
2015-10-14 06:32:01 -04:00
|
|
|
|
2017-04-18 01:04:51 -04:00
|
|
|
from machine import I2C
|
2015-10-14 06:32:01 -04:00
|
|
|
|
2017-04-18 01:04:51 -04:00
|
|
|
i2c = I2C(freq=400000) # create I2C peripheral at frequency of 400kHz
|
|
|
|
# depending on the port, extra parameters may be required
|
|
|
|
# to select the peripheral and/or pins to use
|
2015-10-14 06:32:01 -04:00
|
|
|
|
2021-06-12 00:51:05 -04:00
|
|
|
i2c.scan() # scan for peripherals, returning a list of 7-bit addresses
|
2015-10-14 06:32:01 -04:00
|
|
|
|
2021-06-12 00:51:05 -04:00
|
|
|
i2c.writeto(42, b'123') # write 3 bytes to peripheral with 7-bit address 42
|
|
|
|
i2c.readfrom(42, 4) # read 4 bytes from peripheral with 7-bit address 42
|
2015-10-14 06:32:01 -04:00
|
|
|
|
2021-06-12 00:51:05 -04:00
|
|
|
i2c.readfrom_mem(42, 8, 3) # read 3 bytes from memory of peripheral 42,
|
|
|
|
# starting at memory-address 8 in the peripheral
|
|
|
|
i2c.writeto_mem(42, 2, b'\x10') # write 1 byte to memory of peripheral 42
|
|
|
|
# starting at address 2 in the peripheral
|
2015-10-14 06:32:01 -04:00
|
|
|
|
|
|
|
Constructors
|
|
|
|
------------
|
|
|
|
|
2020-09-29 02:50:23 -04:00
|
|
|
.. class:: I2C(id, *, scl, sda, freq=400000)
|
2015-10-14 06:32:01 -04:00
|
|
|
|
2017-04-18 01:04:51 -04:00
|
|
|
Construct and return a new I2C object using the following parameters:
|
2016-12-29 23:25:48 -05:00
|
|
|
|
2020-09-29 02:50:23 -04:00
|
|
|
- *id* identifies a particular I2C peripheral. Allowed values for
|
|
|
|
depend on the particular port/board
|
|
|
|
- *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.
|
|
|
|
|
|
|
|
Note that some ports/boards will have default values of *scl* and *sda*
|
|
|
|
that can be changed in this constructor. Others will have fixed values
|
|
|
|
of *scl* and *sda* that cannot be changed.
|
|
|
|
|
|
|
|
.. _machine.SoftI2C:
|
|
|
|
.. class:: SoftI2C(scl, sda, *, freq=400000, timeout=255)
|
|
|
|
|
|
|
|
Construct a new software I2C object. The parameters are:
|
|
|
|
|
2017-06-25 06:30:29 -04:00
|
|
|
- *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
|
2017-04-18 01:04:51 -04:00
|
|
|
for SCL.
|
2020-09-29 02:50:23 -04:00
|
|
|
- *timeout* is the maximum time in microseconds to wait for clock
|
|
|
|
stretching (SCL held low by another device on the bus), after
|
|
|
|
which an ``OSError(ETIMEDOUT)`` exception is raised.
|
2016-05-02 07:31:17 -04:00
|
|
|
|
|
|
|
General Methods
|
|
|
|
---------------
|
2015-10-14 06:32:01 -04:00
|
|
|
|
2020-07-11 02:53:26 -04:00
|
|
|
.. method:: I2C.init(scl, sda, *, freq=400000)
|
2015-10-14 06:32:01 -04:00
|
|
|
|
2017-04-18 01:04:51 -04:00
|
|
|
Initialise the I2C bus with the given arguments:
|
2015-10-14 06:32:01 -04:00
|
|
|
|
2017-06-25 06:30:29 -04:00
|
|
|
- *scl* is a pin object for the SCL line
|
|
|
|
- *sda* is a pin object for the SDA line
|
|
|
|
- *freq* is the SCL clock rate
|
2015-10-14 06:32:01 -04:00
|
|
|
|
2016-06-07 18:33:49 -04:00
|
|
|
.. method:: I2C.deinit()
|
2015-10-14 06:32:01 -04:00
|
|
|
|
2016-05-02 07:31:17 -04:00
|
|
|
Turn off the I2C bus.
|
2015-10-14 06:32:01 -04:00
|
|
|
|
2016-05-02 07:31:17 -04:00
|
|
|
Availability: WiPy.
|
2015-10-14 06:32:01 -04:00
|
|
|
|
2016-06-07 18:33:49 -04:00
|
|
|
.. method:: I2C.scan()
|
2015-10-14 06:32:01 -04:00
|
|
|
|
2016-05-02 07:31:17 -04:00
|
|
|
Scan all I2C addresses between 0x08 and 0x77 inclusive and return a list of
|
|
|
|
those that respond. A device responds if it pulls the SDA line low after
|
2017-03-18 19:09:58 -04:00
|
|
|
its address (including a write bit) is sent on the bus.
|
2015-10-14 06:32:01 -04:00
|
|
|
|
2016-05-02 07:31:17 -04:00
|
|
|
Primitive I2C operations
|
|
|
|
------------------------
|
2015-10-14 06:32:01 -04:00
|
|
|
|
2021-06-12 00:51:05 -04:00
|
|
|
The following methods implement the primitive I2C controller bus operations and can
|
2016-05-02 07:31:17 -04:00
|
|
|
be combined to make any I2C transaction. They are provided if you need more
|
|
|
|
control over the bus, otherwise the standard methods (see below) can be used.
|
2015-10-14 06:32:01 -04:00
|
|
|
|
2020-09-29 02:50:23 -04:00
|
|
|
These methods are only available on the `machine.SoftI2C` class.
|
2018-08-02 12:34:34 -04:00
|
|
|
|
2016-06-07 18:33:49 -04:00
|
|
|
.. method:: I2C.start()
|
2015-10-14 06:32:01 -04:00
|
|
|
|
2016-11-17 01:34:48 -05:00
|
|
|
Generate a START condition on the bus (SDA transitions to low while SCL is high).
|
2015-10-14 06:32:01 -04:00
|
|
|
|
2016-06-07 18:33:49 -04:00
|
|
|
.. method:: I2C.stop()
|
2016-05-02 07:31:17 -04:00
|
|
|
|
2016-11-17 01:34:48 -05:00
|
|
|
Generate a STOP condition on the bus (SDA transitions to high while SCL is high).
|
2016-05-02 07:31:17 -04:00
|
|
|
|
2020-01-04 05:00:27 -05:00
|
|
|
.. method:: I2C.readinto(buf, nack=True, /)
|
2016-05-02 07:31:17 -04:00
|
|
|
|
2017-06-25 06:30:29 -04:00
|
|
|
Reads bytes from the bus and stores them into *buf*. The number of bytes
|
|
|
|
read is the length of *buf*. An ACK will be sent on the bus after
|
|
|
|
receiving all but the last byte. After the last byte is received, if *nack*
|
2016-11-17 01:34:48 -05:00
|
|
|
is true then a NACK will be sent, otherwise an ACK will be sent (and in this
|
2021-06-12 00:51:05 -04:00
|
|
|
case the peripheral assumes more bytes are going to be read in a later call).
|
2016-05-02 07:31:17 -04:00
|
|
|
|
2016-06-07 18:33:49 -04:00
|
|
|
.. method:: I2C.write(buf)
|
2016-05-02 07:31:17 -04:00
|
|
|
|
2017-06-25 06:30:29 -04:00
|
|
|
Write the bytes from *buf* to the bus. Checks that an ACK is received
|
2016-11-17 01:34:48 -05:00
|
|
|
after each byte and stops transmitting the remaining bytes if a NACK is
|
|
|
|
received. The function returns the number of ACKs that were received.
|
2016-05-02 07:31:17 -04:00
|
|
|
|
|
|
|
Standard bus operations
|
|
|
|
-----------------------
|
|
|
|
|
2021-06-12 00:51:05 -04:00
|
|
|
The following methods implement the standard I2C controller read and write
|
|
|
|
operations that target a given peripheral device.
|
2015-10-14 06:32:01 -04:00
|
|
|
|
2020-01-04 05:00:27 -05:00
|
|
|
.. method:: I2C.readfrom(addr, nbytes, stop=True, /)
|
2016-05-02 07:31:17 -04:00
|
|
|
|
2021-06-12 00:51:05 -04:00
|
|
|
Read *nbytes* from the peripheral specified by *addr*.
|
2017-06-25 06:30:29 -04:00
|
|
|
If *stop* is true then a STOP condition is generated at the end of the transfer.
|
2016-05-02 07:31:17 -04:00
|
|
|
Returns a `bytes` object with the data read.
|
|
|
|
|
2020-01-04 05:00:27 -05:00
|
|
|
.. method:: I2C.readfrom_into(addr, buf, stop=True, /)
|
2016-05-02 07:31:17 -04:00
|
|
|
|
2021-06-12 00:51:05 -04:00
|
|
|
Read into *buf* from the peripheral specified by *addr*.
|
2017-06-25 06:30:29 -04:00
|
|
|
The number of bytes read will be the length of *buf*.
|
|
|
|
If *stop* is true then a STOP condition is generated at the end of the transfer.
|
2016-05-02 07:31:17 -04:00
|
|
|
|
2017-06-25 06:30:29 -04:00
|
|
|
The method returns ``None``.
|
2016-05-02 07:31:17 -04:00
|
|
|
|
2020-01-04 05:00:27 -05:00
|
|
|
.. method:: I2C.writeto(addr, buf, stop=True, /)
|
2016-05-02 07:31:17 -04:00
|
|
|
|
2021-06-12 00:51:05 -04:00
|
|
|
Write the bytes from *buf* to the peripheral specified by *addr*. If a
|
2017-06-25 06:30:29 -04:00
|
|
|
NACK is received following the write of a byte from *buf* then the
|
|
|
|
remaining bytes are not sent. If *stop* is true then a STOP condition is
|
2016-11-17 01:34:48 -05:00
|
|
|
generated at the end of the transfer, even if a NACK is received.
|
|
|
|
The function returns the number of ACKs that were received.
|
2016-05-02 07:31:17 -04:00
|
|
|
|
2020-01-04 05:00:27 -05:00
|
|
|
.. method:: I2C.writevto(addr, vector, stop=True, /)
|
2018-08-04 02:18:52 -04:00
|
|
|
|
2021-06-12 00:51:05 -04:00
|
|
|
Write the bytes contained in *vector* to the peripheral specified by *addr*.
|
2018-08-04 02:18:52 -04:00
|
|
|
*vector* should be a tuple or list of objects with the buffer protocol.
|
|
|
|
The *addr* is sent once and then the bytes from each object in *vector*
|
|
|
|
are written out sequentially. The objects in *vector* may be zero bytes
|
|
|
|
in length in which case they don't contribute to the output.
|
|
|
|
|
|
|
|
If a NACK is received following the write of a byte from one of the
|
|
|
|
objects in *vector* then the remaining bytes, and any remaining objects,
|
|
|
|
are not sent. If *stop* is true then a STOP condition is generated at
|
|
|
|
the end of the transfer, even if a NACK is received. The function
|
|
|
|
returns the number of ACKs that were received.
|
|
|
|
|
2016-05-02 07:31:17 -04:00
|
|
|
Memory operations
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
Some I2C devices act as a memory device (or set of registers) that can be read
|
|
|
|
from and written to. In this case there are two addresses associated with an
|
2021-06-12 00:51:05 -04:00
|
|
|
I2C transaction: the peripheral address and the memory address. The following
|
2016-05-02 07:31:17 -04:00
|
|
|
methods are convenience functions to communicate with such devices.
|
|
|
|
|
2020-07-11 02:53:26 -04:00
|
|
|
.. method:: I2C.readfrom_mem(addr, memaddr, nbytes, *, addrsize=8)
|
2016-05-02 07:31:17 -04:00
|
|
|
|
2021-06-12 00:51:05 -04:00
|
|
|
Read *nbytes* from the peripheral specified by *addr* starting from the memory
|
2017-06-25 06:30:29 -04:00
|
|
|
address specified by *memaddr*.
|
|
|
|
The argument *addrsize* specifies the address size in bits.
|
2016-05-02 07:31:17 -04:00
|
|
|
Returns a `bytes` object with the data read.
|
|
|
|
|
2020-07-11 02:53:26 -04:00
|
|
|
.. method:: I2C.readfrom_mem_into(addr, memaddr, buf, *, addrsize=8)
|
2016-05-02 07:31:17 -04:00
|
|
|
|
2021-06-12 00:51:05 -04:00
|
|
|
Read into *buf* from the peripheral specified by *addr* starting from the
|
2017-06-25 06:30:29 -04:00
|
|
|
memory address specified by *memaddr*. The number of bytes read is the
|
|
|
|
length of *buf*.
|
|
|
|
The argument *addrsize* specifies the address size in bits (on ESP8266
|
2016-05-02 07:31:17 -04:00
|
|
|
this argument is not recognised and the address size is always 8 bits).
|
|
|
|
|
2017-06-25 06:30:29 -04:00
|
|
|
The method returns ``None``.
|
2016-05-02 07:31:17 -04:00
|
|
|
|
2020-07-11 02:53:26 -04:00
|
|
|
.. method:: I2C.writeto_mem(addr, memaddr, buf, *, addrsize=8)
|
2016-05-02 07:31:17 -04:00
|
|
|
|
2021-06-12 00:51:05 -04:00
|
|
|
Write *buf* to the peripheral specified by *addr* starting from the
|
2017-06-25 06:30:29 -04:00
|
|
|
memory address specified by *memaddr*.
|
|
|
|
The argument *addrsize* specifies the address size in bits (on ESP8266
|
2016-05-02 07:31:17 -04:00
|
|
|
this argument is not recognised and the address size is always 8 bits).
|
|
|
|
|
2017-06-25 06:30:29 -04:00
|
|
|
The method returns ``None``.
|