5.1.7 I2C Communication Procedure
This section describes the typical command sequence for using the MCP2222 for I2C communication.
Writing to an I2C client device from a specified location:
- Configure I2C:
- Send the I2C_Config command
through Endpoint 0x03 (OUT) with the following parameters:
Parameter Value Description Command 0x40Configure I2C Clock speed 0x700x160x04100 kHz clock speed. 400 kHz clock speed.
1 MHz clock speed.
Clock stretch timeout 0x01-0xFAClock-stretch timeout in milliseconds (8-bit hexadecimal) - Read the response through
Endpoint 0x83 (IN). The responses can be one of the following:
Response code Status Action 0x40Response to I2C Config Continue reading 0x200x600x64I2C is configured. Invalid parameters.
I2C module busy.
Proceed with the write transaction. Retry with valid parameters.
Wait and retry later.
- Send the I2C_Config command
through Endpoint 0x03 (OUT) with the following parameters:
- Write transaction
- Send the I2C_Write command
through Endpoint 0x03 (OUT) with the following parameters:
Parameter Value Description Command 0x44Write to the I2C client device I2C Client address bAddress7-bit I2C client address Write data length wWrDtLenNumber of bytes to be written including the location address, encoded as a 16-bit hexadecimal value Location address vLocAddStarting location from where the data should be written Data bytes vDatPacData to be written (up to 60 bytes, including the location address) - Read the initial response
from Endpoint 0x83 (IN). Possible initial responses are:
Response code Status Action 0x44Response to I2C Write Continue reading 0x220x600x64The I2C write transaction is initiated. Invalid parameters.
The I2C module is busy.
Proceed to continue the write transaction. Retry with valid parameters.
Wait and retry later.
- If the number of data bytes
to write exceeds 60 bytes, send continuation packets through Endpoint 0x03
(OUT). Each continuation packet includes:
Parameter Value Description Command 0x44Continue writing to the I2C client device Continuation flag 0xA0Data bytes vDatPacData to be written (up to 62 bytes) - After each continuation
packet, and after the write transaction is completed, read the response from
Endpoint 0x83 (IN). The response can be one of the following:
Response code Status Action 0x44NACKResponse to I2C Write. No response available yet.
Continue reading. Continue polling.
0x220x680x800x840x880x8C0x20The I2C write transaction is continued. The buffer is full.
An address NACK occurred.
A data NACK occurred.
A clock-stretch timeout occurred.
I2C bus arbitration was lost.
The I2C write transaction is successfully completed.
Proceed to continue the write transaction. Retry until the packet is accepted.
Retry with a valid address.
Use a smaller data packet.
Run bus recovery.
Wait and retry (I2C bus in use).
-
Repeat sending continuation packet through Endpoint 0x03 (OUT) and polling Endpoint 0x83 (IN) until all required data bytes are written.
- Send the I2C_Write command
through Endpoint 0x03 (OUT) with the following parameters:
Reading data from an I2C client device from a specified location:
- Configure I2C:
- Refer to the Configure I2C section of Writing to a I2C client device from a specified location.
- Read data from specific locations of
an I2C client device:
- Send the I2C_WriteRead
command through Endpoint 0x03 (OUT) with the following parameters.
Parameter Value Description Command 0x4AWrite to and read from an I2C client device I2C Client address bAddress7-bit I2C client address Write data length wWrDtLenNumber of bytes in the location address, encoded as a 16-bit hexadecimal value Read data length wRdDtLenNumber of bytes to be read, encoded as a 16-bit hexadecimal value Location address vLocAddStarting location from where the data should be read - Read the initial response
from Endpoint 0x83 (IN). Possible initial responses are:
Response code Status Action 0x4AResponse to I2C WriteRead Continue reading 0x220x600x64I2C write-read transaction is initiated. Invalid parameters
The I2C module is busy.
Proceed to continue polling for the next response. Retry with valid parameters.
Wait and retry later.
- Keep polling Endpoint 0x83
(IN) until read data is returned. Possible poll results:
Response code Status Action 0x4ANACKResponse to I2C WriteRead. No response available yet.
Continue reading. Continue polling.
0x210x840x880x8CThe I2C write-read transaction continued. A data NACK occurred.
A clock-stretch timeout occurred.
I2C bus arbitration was lost.
Continue reading until the entire packet is read, then proceed to poll for the next response. Retry with the correct location address.
Run bus recovery.
Wait and retry (I2C bus in use).
wRdDtLenThe first 60 bytes of data are read Continue reading vDatPacRead all the available data bytes from the endpoint. Continue reading until all packets have been read.
- Repeat polling Endpoint 0x83
(IN), until all required data bytes are read:
Response code Status Action 0x4ANACKResponse to I2C WriteRead. No response available yet.
Continue reading. Continue polling.
0x210x880x8CThe I2C write-read transaction continued. A clock-stretch timeout occurred.
I2C bus arbitration was lost.
Continue reading until the entire packet is read, then proceed to poll for the next response. Run bus recovery.
Wait and retry (I2C bus in use).
vDatPacThe next 62 bytes of data are read. Read all available data bytes from the endpoint. Continue reading until all packets have been read.
- Send the I2C_WriteRead
command through Endpoint 0x03 (OUT) with the following parameters.
Recovering the I2C Bus on a Clock-Stretch Timeout
| Parameter | Value | Description |
|---|---|---|
| Command | 0x4E | Recover the I2C bus |
Read the response from Endpoint 0x83 (IN).
| Response code | Status | Action |
|---|---|---|
0x4E | Response to I2C bus recovery | Continue reading |
0x20
| The I2C bus is recovered. No clock-stretch timeout was detected. Bus recovery failed. | The I2C bus is in the idle state. The bus is available for transaction. Retry by sending the command again. |
Reading the logic states of the SCL and SDA lines:
To check the states of the SCL and SDA lines, follow these steps:
- Send the I2C_GetSCLSDAStatus (0x4C) through Endpoint 0x03 (OUT).
Parameter Value Description Command 0x4CRecover the I2C bus - Read the response from Endpoint 0x83(IN).
Response code Status Action 0x4CResponse to I2C bus recovery Continue reading 0x21SCL and SDA states are read Continue reading 0x000x000x000x00The SCL is in a low state, and the SDA is also in a low state The SCL is in a high state, and the SDA is in a low state
The SCL is in a low state, and the SDA is in a high state
The SCL is in a high state, and the SDA is also in a high state
Take the necessary action depending on the logical state read
