2016-06-07 22:57:41 +01:00
|
|
|
.. currentmodule:: pyb
|
2014-11-04 18:25:20 +00:00
|
|
|
.. _pyb.I2C:
|
|
|
|
|
2014-10-31 22:21:37 +00:00
|
|
|
class I2C -- a two-wire serial protocol
|
|
|
|
=======================================
|
2014-10-31 01:37:19 +00:00
|
|
|
|
|
|
|
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
|
2015-06-10 22:29:56 +01:00
|
|
|
when created, or initialised later on.
|
|
|
|
|
2018-07-18 06:47:44 +01:00
|
|
|
Example::
|
2015-06-10 22:29:56 +01:00
|
|
|
|
2018-07-18 06:47:44 +01:00
|
|
|
from pyb import I2C
|
2015-09-11 13:57:48 +01:00
|
|
|
|
2018-07-18 06:47:44 +01:00
|
|
|
i2c = I2C(1) # create on bus 1
|
|
|
|
i2c = I2C(1, I2C.MASTER) # create and init as a master
|
|
|
|
i2c.init(I2C.MASTER, baudrate=20000) # init as a master
|
|
|
|
i2c.init(I2C.SLAVE, addr=0x42) # init as a slave with given address
|
|
|
|
i2c.deinit() # turn off the peripheral
|
2015-06-10 22:29:56 +01:00
|
|
|
|
2014-10-31 01:37:19 +00:00
|
|
|
Printing the i2c object gives you information about its configuration.
|
|
|
|
|
2018-07-18 06:47:44 +01:00
|
|
|
The basic methods are send and recv::
|
2014-10-31 01:37:19 +00:00
|
|
|
|
2018-07-18 06:47:44 +01:00
|
|
|
i2c.send('abc') # send 3 bytes
|
|
|
|
i2c.send(0x42) # send a single byte, given by the number
|
|
|
|
data = i2c.recv(3) # receive 3 bytes
|
2014-10-31 01:37:19 +00:00
|
|
|
|
2018-07-18 06:47:44 +01:00
|
|
|
To receive inplace, first create a bytearray::
|
2014-10-31 01:37:19 +00:00
|
|
|
|
2018-07-18 06:47:44 +01:00
|
|
|
data = bytearray(3) # create a buffer
|
|
|
|
i2c.recv(data) # receive 3 bytes, writing them into data
|
2014-10-31 01:37:19 +00:00
|
|
|
|
2018-07-18 06:47:44 +01:00
|
|
|
You can specify a timeout (in ms)::
|
2014-10-31 01:37:19 +00:00
|
|
|
|
2018-07-18 06:47:44 +01:00
|
|
|
i2c.send(b'123', timeout=2000) # timeout after 2 seconds
|
2014-10-31 01:37:19 +00:00
|
|
|
|
2018-07-18 06:47:44 +01:00
|
|
|
A master must specify the recipient's address::
|
2014-10-31 01:37:19 +00:00
|
|
|
|
2018-07-18 06:47:44 +01:00
|
|
|
i2c.init(I2C.MASTER)
|
|
|
|
i2c.send('123', 0x42) # send 3 bytes to slave with address 0x42
|
|
|
|
i2c.send(b'456', addr=0x42) # keyword for address
|
2014-10-31 01:37:19 +00:00
|
|
|
|
2018-07-18 06:47:44 +01:00
|
|
|
Master also has other methods::
|
2014-10-31 01:37:19 +00:00
|
|
|
|
2018-07-18 06:47:44 +01:00
|
|
|
i2c.is_ready(0x42) # check if slave 0x42 is ready
|
|
|
|
i2c.scan() # scan for slaves on the bus, returning
|
|
|
|
# a list of valid addresses
|
|
|
|
i2c.mem_read(3, 0x42, 2) # read 3 bytes from memory of slave 0x42,
|
|
|
|
# starting at address 2 in the slave
|
|
|
|
i2c.mem_write('abc', 0x42, 2, timeout=1000) # write 'abc' (3 bytes) to memory of slave 0x42
|
|
|
|
# starting at address 2 in the slave, timeout after 1 second
|
2015-09-11 13:57:48 +01:00
|
|
|
|
2014-10-31 01:37:19 +00:00
|
|
|
Constructors
|
|
|
|
------------
|
|
|
|
|
2018-07-18 06:47:44 +01:00
|
|
|
.. class:: pyb.I2C(bus, ...)
|
|
|
|
|
|
|
|
Construct an I2C object on the given bus. ``bus`` can be 1 or 2, 'X' or
|
|
|
|
'Y'. With no additional parameters, the I2C object is created but not
|
|
|
|
initialised (it has the settings from the last initialisation of
|
|
|
|
the bus, if any). If extra arguments are given, the bus is initialised.
|
|
|
|
See ``init`` for parameters of initialisation.
|
|
|
|
|
|
|
|
The physical pins of the I2C busses on Pyboards V1.0 and V1.1 are:
|
2014-10-31 01:37:19 +00:00
|
|
|
|
2018-07-18 06:47:44 +01:00
|
|
|
- ``I2C(1)`` is on the X position: ``(SCL, SDA) = (X9, X10) = (PB6, PB7)``
|
|
|
|
- ``I2C(2)`` is on the Y position: ``(SCL, SDA) = (Y9, Y10) = (PB10, PB11)``
|
2015-09-11 13:57:48 +01:00
|
|
|
|
2018-07-18 06:47:44 +01:00
|
|
|
On the Pyboard Lite:
|
2015-09-11 13:57:48 +01:00
|
|
|
|
2018-07-18 06:47:44 +01:00
|
|
|
- ``I2C(1)`` is on the X position: ``(SCL, SDA) = (X9, X10) = (PB6, PB7)``
|
|
|
|
- ``I2C(3)`` is on the Y position: ``(SCL, SDA) = (Y9, Y10) = (PA8, PB8)``
|
2015-09-11 13:57:48 +01:00
|
|
|
|
2018-07-18 06:47:44 +01:00
|
|
|
Calling the constructor with 'X' or 'Y' enables portability between Pyboard
|
|
|
|
types.
|
2015-06-10 22:29:56 +01:00
|
|
|
|
2014-10-31 01:37:19 +00:00
|
|
|
Methods
|
|
|
|
-------
|
|
|
|
|
2016-06-08 14:21:28 +01:00
|
|
|
.. method:: I2C.deinit()
|
2014-10-31 01:37:19 +00:00
|
|
|
|
|
|
|
Turn off the I2C bus.
|
|
|
|
|
2018-07-18 06:47:44 +01:00
|
|
|
.. method:: I2C.init(mode, \*, addr=0x12, baudrate=400000, gencall=False, dma=False)
|
2015-06-10 22:29:56 +01:00
|
|
|
|
2018-07-18 06:47:44 +01:00
|
|
|
Initialise the I2C bus with the given parameters:
|
2014-10-31 01:37:19 +00:00
|
|
|
|
2018-07-18 06:47:44 +01:00
|
|
|
- ``mode`` must be either ``I2C.MASTER`` or ``I2C.SLAVE``
|
|
|
|
- ``addr`` is the 7-bit address (only sensible for a slave)
|
|
|
|
- ``baudrate`` is the SCL clock rate (only sensible for a master)
|
|
|
|
- ``gencall`` is whether to support general call mode
|
|
|
|
- ``dma`` is whether to allow the use of DMA for the I2C transfers (note
|
|
|
|
that DMA transfers have more precise timing but currently do not handle bus
|
|
|
|
errors properly)
|
2015-06-10 22:29:56 +01:00
|
|
|
|
2018-07-18 06:47:44 +01:00
|
|
|
.. method:: I2C.is_ready(addr)
|
2015-06-10 22:29:56 +01:00
|
|
|
|
2018-07-18 06:47:44 +01:00
|
|
|
Check if an I2C device responds to the given address. Only valid when in master mode.
|
2015-09-11 13:57:48 +01:00
|
|
|
|
2018-07-18 06:47:44 +01:00
|
|
|
.. method:: I2C.mem_read(data, addr, memaddr, \*, timeout=5000, addr_size=8)
|
2015-09-11 13:57:48 +01:00
|
|
|
|
2018-07-18 06:47:44 +01:00
|
|
|
Read from the memory of an I2C device:
|
2015-09-11 13:57:48 +01:00
|
|
|
|
2018-07-18 06:47:44 +01:00
|
|
|
- ``data`` can be an integer (number of bytes to read) or a buffer to read into
|
|
|
|
- ``addr`` is the I2C device address
|
|
|
|
- ``memaddr`` is the memory location within the I2C device
|
|
|
|
- ``timeout`` is the timeout in milliseconds to wait for the read
|
|
|
|
- ``addr_size`` selects width of memaddr: 8 or 16 bits
|
2015-09-11 13:57:48 +01:00
|
|
|
|
2018-07-18 06:47:44 +01:00
|
|
|
Returns the read data.
|
|
|
|
This is only valid in master mode.
|
2015-09-11 13:57:48 +01:00
|
|
|
|
2018-07-18 06:47:44 +01:00
|
|
|
.. method:: I2C.mem_write(data, addr, memaddr, \*, timeout=5000, addr_size=8)
|
2015-09-11 13:57:48 +01:00
|
|
|
|
2018-07-18 06:47:44 +01:00
|
|
|
Write to the memory of an I2C device:
|
2015-09-11 13:57:48 +01:00
|
|
|
|
2018-07-18 06:47:44 +01:00
|
|
|
- ``data`` can be an integer or a buffer to write from
|
|
|
|
- ``addr`` is the I2C device address
|
|
|
|
- ``memaddr`` is the memory location within the I2C device
|
|
|
|
- ``timeout`` is the timeout in milliseconds to wait for the write
|
|
|
|
- ``addr_size`` selects width of memaddr: 8 or 16 bits
|
2015-09-11 13:57:48 +01:00
|
|
|
|
2018-07-18 06:47:44 +01:00
|
|
|
Returns ``None``.
|
|
|
|
This is only valid in master mode.
|
2015-09-11 13:57:48 +01:00
|
|
|
|
2018-07-18 06:47:44 +01:00
|
|
|
.. method:: I2C.recv(recv, addr=0x00, \*, timeout=5000)
|
2015-09-11 13:57:48 +01:00
|
|
|
|
2018-07-18 06:47:44 +01:00
|
|
|
Receive data on the bus:
|
2015-09-11 13:57:48 +01:00
|
|
|
|
2018-07-18 06:47:44 +01:00
|
|
|
- ``recv`` can be an integer, which is the number of bytes to receive,
|
|
|
|
or a mutable buffer, which will be filled with received bytes
|
|
|
|
- ``addr`` is the address to receive from (only required in master mode)
|
|
|
|
- ``timeout`` is the timeout in milliseconds to wait for the receive
|
2015-09-11 13:57:48 +01:00
|
|
|
|
2018-07-18 06:47:44 +01:00
|
|
|
Return value: if ``recv`` is an integer then a new buffer of the bytes received,
|
|
|
|
otherwise the same buffer that was passed in to ``recv``.
|
2015-09-11 13:57:48 +01:00
|
|
|
|
2018-07-18 06:47:44 +01:00
|
|
|
.. method:: I2C.send(send, addr=0x00, \*, timeout=5000)
|
2015-09-11 13:57:48 +01:00
|
|
|
|
2018-07-18 06:47:44 +01:00
|
|
|
Send data on the bus:
|
2015-09-11 13:57:48 +01:00
|
|
|
|
2018-07-18 06:47:44 +01:00
|
|
|
- ``send`` is the data to send (an integer to send, or a buffer object)
|
|
|
|
- ``addr`` is the address to send to (only required in master mode)
|
|
|
|
- ``timeout`` is the timeout in milliseconds to wait for the send
|
2015-09-11 13:57:48 +01:00
|
|
|
|
2018-07-18 06:47:44 +01:00
|
|
|
Return value: ``None``.
|
2015-09-11 13:57:48 +01:00
|
|
|
|
2016-06-08 14:21:28 +01:00
|
|
|
.. method:: I2C.scan()
|
2014-10-31 01:37:19 +00:00
|
|
|
|
|
|
|
Scan all I2C addresses from 0x01 to 0x7f and return a list of those that respond.
|
|
|
|
Only valid when in master mode.
|
|
|
|
|
|
|
|
Constants
|
|
|
|
---------
|
|
|
|
|
|
|
|
.. data:: I2C.MASTER
|
|
|
|
|
|
|
|
for initialising the bus to master mode
|
|
|
|
|
2018-07-18 06:47:44 +01:00
|
|
|
.. data:: I2C.SLAVE
|
2014-10-31 01:37:19 +00:00
|
|
|
|
2018-07-18 06:47:44 +01:00
|
|
|
for initialising the bus to slave mode
|