-
Notifications
You must be signed in to change notification settings - Fork 345
i2c
This class includes full support for using ESP32 I2C peripheral
Both master and slave modes are supported.
Argument | Description |
---|---|
id |
The hardware I2C peripheral ID; 0 or 1 can be usedDefault: 0
|
mode |
I2C interface mode; master or slave Use the constants machine.I2C.MASTER or machine.I2C.SLAVE Default: master |
speed freq
|
I2C clock frequency in Hz Default: 100000 |
sda |
I2C sda pin; can be given as integer gpio number or Pin object |
scl |
I2C scl pin; can be given as integer gpio number or Pin object |
slave_address |
I2C slave address to be assigned to this i2c interface. Only used if SLAVE mode is selected 7-bit address, do not use reserved adresses 0x00-0x07 & 0x78-0x7F
|
Default: 32 (0x20) |
|
slave_bufflen |
Size of slave buffer used for master<->slave comunication in bytes; Range: 256 - 4096 Default: 256 Only used if SLAVE mode is selected |
Only sda
and scl
are required, all the others are optional and will be set to the default values if not given.
i2c = machine.I2C(0, sda=21, scl=22)
si2c = machine.I2C(1, mode=machine.I2C.SLAVE, sda=25, scl=26, slave_bufflen=512)
Deinitialize the I2C object, free all used resources.
Scan for i2c devices on I2C bus.
Returns the list of detected addresses.
Can only be used in master mode.
Read nbytes
bytes from i2c device with address addr
.
Bytearray of read bytes is returned.
Can only be used in master mode.
Read from i2c device with address addr
into buffer object buf
.
Size of buf
bytes are read.
Can only be used in master mode.
Write the content of the buffer object buf
to the i2c device with address adr
If optional stop
argument is set to False
, the stop signal is not issued.
Can only be used in master mode.
Argument | Description |
---|---|
addr |
i2c device address |
memaddr |
memory address to be wtitten before read |
n |
number of bytes to read |
adrlen |
optional; number of addres bytes to write, 1 - 4 If not given, number of bytes to send is determined from the memaddr value |
stop |
optional; Default: True If True , stop signal is issued after address writeIf False , repeated start signal is issued after address write |
Write the address to the i2c device with address addr
, then read n
bytes from it.
Bytearray of read bytes is returned.
Can only be used in master mode.
Argument | Description |
---|---|
addr |
i2c device address |
memaddr |
memory address to be wtitten before read |
buf |
Buffer object to read into |
adrlen |
optional; number of addres bytes to write, 1 - 4 If not given, number of bytes to send is determined from the memaddr value |
stop |
optional; Default: True If True , stop signal is issued after address writeIf False , repeated start signal is issued after address write |
Write the address to the i2c device with address addr
, then read from itinto buffer object buf
.
Size of buf
bytes are read.
Can only be used in master mode.
Argument | Description |
---|---|
addr |
i2c device address |
memaddr |
memory address to be wtitten before read |
buf |
Buffer object to write from |
adrlen |
optional; number of addres bytes to write, 1 - 4 If not given, number of bytes to send is determined from the memaddr value |
Write the address to the i2c device with address addr
, then write the content of the buffer object buf
to the device
Can only be used in master mode.
The I2C device can be configured to run in slave mode
The master <-> slave communication is performed via the slave bufer, size of which is configured on creating the I2C object.
The master can perform three kinds of operations with MicroPython I2C slave:
-
read from slave buffer issuing the following command sequence:
- write the 2-byte address & 2-byte length to MicroPython I2C slave. The address range and lenght must be set according to MicroPython I2C slave buffer size
- read length bytes from MicroPython I2C slave
-
write to the slave buffer issuing the following command sequence:
-
write 2-byte address following with data to MicroPython I2C slave.
The address range must be from0x1000
to0x1000
+ slave_buffer_size - 1
The0x1000
address offset is used to distingush the read and write operations.
-
write 2-byte address following with data to MicroPython I2C slave.
- send command with optional data to the slave
- The master can send any command in range
0x20
to0xFF
to the MicroPython I2C slave.
This command (and optional data) can be anylized in callback function and some action taken
- The master can send any command in range
The content of the slave buffer san be set or read from MicroPython at any time.
Separate callback functions can be defined for read, write and command events.
Set the content of the slave buffer at address addr
from buffer object buf
Can only be used in slave mode.
Get the length
bytes from the slave buffer at address addr
Bytearray of read bytes is returned.
Can only be used in slave mode.
Write the content of the buffer object buf
to the I2C slave transimt buffer
The master must ready to read that data.
Can be used, for example, from command callback function as a response to some master command.
Can only be used in slave mode.
Can only be used in slave mode.