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:

  1. Configure I2C:
    • Send the I2C_Config command through Endpoint 0x03 (OUT) with the following parameters:
      ParameterValueDescription
      Command0x40Configure I2C
      Clock speed0x70

      0x16

      0x04

      100 kHz clock speed.

      400 kHz clock speed.

      1 MHz clock speed.

      Clock stretch timeout0x01 - 0xFAClock-stretch timeout in milliseconds (8-bit hexadecimal)
    • Read the response through Endpoint 0x83 (IN). The responses can be one of the following:
      Response codeStatusAction
      0x40Response to I2C ConfigContinue reading
      0x20

      0x60

      0x64

      I2C is configured.

      Invalid parameters.

      I2C module busy.

      Proceed with the write transaction.

      Retry with valid parameters.

      Wait and retry later.

  2. Write transaction
    • Send the I2C_Write command through Endpoint 0x03 (OUT) with the following parameters:
      ParameterValueDescription
      Command0x44Write to the I2C client device
      I2C Client addressbAddress7-bit I2C client address
      Write data lengthwWrDtLenNumber of bytes to be written including the location address, encoded as a 16-bit hexadecimal value
      Location addressvLocAddStarting location from where the data should be written
      Data bytesvDatPacData to be written (up to 60 bytes, including the location address)
    • Read the initial response from Endpoint 0x83 (IN). Possible initial responses are:
      Response codeStatusAction
      0x44Response to I2C WriteContinue reading
      0x22

      0x60

      0x64

      The 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:
      ParameterValueDescription
      Command0x44Continue writing to the I2C client device
      Continuation flag0xA0
      Data bytesvDatPacData 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 codeStatusAction
      0x44

      NACK

      Response to I2C Write.

      No response available yet.

      Continue reading.

      Continue polling.

      0x22

      0x68

      0x80

      0x84

      0x88

      0x8C

      0x20

      The 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.

Reading data from an I2C client device from a specified location:

  1. Configure I2C:
    • Refer to the Configure I2C section of Writing to a I2C client device from a specified location.
  2. Read data from specific locations of an I2C client device:
    • Send the I2C_WriteRead command through Endpoint 0x03 (OUT) with the following parameters.
      ParameterValueDescription
      Command0x4AWrite to and read from an I2C client device
      I2C Client addressbAddress7-bit I2C client address
      Write data lengthwWrDtLenNumber of bytes in the location address, encoded as a 16-bit hexadecimal value
      Read data lengthwRdDtLenNumber of bytes to be read, encoded as a 16-bit hexadecimal value
      Location addressvLocAddStarting location from where the data should be read
    • Read the initial response from Endpoint 0x83 (IN). Possible initial responses are:
      Response codeStatusAction
      0x4AResponse to I2C WriteReadContinue reading
      0x22

      0x60

      0x64

      I2C 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 codeStatusAction
      0x4A

      NACK

      Response to I2C WriteRead.

      No response available yet.

      Continue reading.

      Continue polling.

      0x21

      0x84

      0x88

      0x8C

      The 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 readContinue 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 codeStatusAction
      0x4A

      NACK

      Response to I2C WriteRead.

      No response available yet.

      Continue reading.

      Continue polling.

      0x21

      0x88

      0x8C

      The 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.

Recovering the I2C Bus on a Clock-Stretch Timeout

Send the I2C_BusRecovery command through Endpoint 0x03 (OUT) if a clock-stretch timeout occurs during a write, read, or writ-read transaction.
ParameterValueDescription
Command0x4ERecover the I2C bus

Read the response from Endpoint 0x83 (IN).

Response codeStatusAction
0x4EResponse to I2C bus recoveryContinue reading
0x20

0x60

0x80

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:

  1. Send the I2C_GetSCLSDAStatus (0x4C) through Endpoint 0x03 (OUT).
    ParameterValueDescription
    Command0x4CRecover the I2C bus
  2. Read the response from Endpoint 0x83(IN).
    Response codeStatusAction
    0x4CResponse to I2C bus recoveryContinue reading
    0x21SCL and SDA states are readContinue reading
    0x00

    0x00

    0x00

    0x00

    The 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