I2CSlave — Two wire serial protocol slave

class i2cslave.I2CSlave(scl, sda, addresses, smbus=False)

I2C is a two-wire protocol for communicating between devices. This implements the slave side.

Parameters:
  • scl (Pin) – The clock pin
  • sda (Pin) – The data pin
  • addresses (tuple) – The I2C addresses to respond to (how many is hw dependent).
  • smbus (bool) – Use SMBUS timings if the hardware supports it
deinit()

Releases control of the underlying hardware so other classes can use it.

__enter__()

No-op used in Context Managers.

__exit__()

Automatically deinitializes the hardware on context exit. See Lifetime and ContextManagers for more info.

request(timeout=-1)

Wait for an I2C request from a master.

Parameters:timeout (float) – Timeout in seconds. Zero means wait forever, a negative value means check once
Returns:I2C Slave Request or None if timeout=-1 and there’s no request
Return type:I2CSlaveRequest

I2CSlaveRequest — I2C Slave Request

class i2cslave.I2CSlaveRequest(slave, address, is_read, is_restart)

I2C transfer request from a master. This cannot be instantiated directly, but is returned by I2CSlave.request().

Parameters:
  • slave (I2CSlave) – The I2C Slave receiving this request
  • address (int) – I2C address
  • is_read (bool) – I2C Master read request
  • is_restart (bool) – Repeated Start Condition
__enter__()

No-op used in Context Managers.

__exit__()

Close the request.

address

The I2C address of the request.

is_read

The I2C master is reading from the device.

is_restart

Is Repeated Start Condition.

read(n=-1, ack=True)

Read data. If ack=False, the caller is responsible for calling I2CSlaveRequest.ack().

Parameters:
  • n (int) – Number of bytes to read (negative means all)
  • ack (bool) – Whether or not to send an ACK after the n’th byte
Returns:

Bytes read

Return type:

bytearray

write(buffer)

Write the data contained in buffer.

Parameters:buffer (bytearray) – Write out the data in this buffer
Returns:Number of bytes written
ack(ack=True)

Acknowledge or Not Acknowledge last byte received. Use together with I2CSlaveRequest.read() ack=False.

Parameters:ack (bool) – Whether to send an ACK or NACK