With Raspberry Pi 4B as a I2C slave how do I split read/write in event handler?

73 views Asked by At

I have followed the example at: http://abyz.me.uk/rpi/pigpio/python.html#bsc_i2c to implement an I2C Slave on A Raspberry Pi 4 in Python. That example implements two I2C registers that can be read by an I2C Master to provide date or time. I would like implement write access to some registers, but I can find no way of telling whether the data being read from the FIFO is for a read or write command. From the master: i2cget -y 1 <address> <register> returns with the register contents. However, my slave code cannot distinguish: i2cget -y 1 <address> <register> <value> I cannot use the number of bytes in the I2C receive buffer to make a selection as running i2cdump on the master can fill the buffer with multiple register selections.

Is the status value returned by: pi.bsc_i2c(I2C_ADDR) supposed to contain a flag that can be used to determine read/write? It always seems to have value: 0x12

My code for handling a read is similar to this:

def i2c_callback(self, _event_id, _tick):
    """Process an I2C command received from I2C Master"""
    status, length, data = self.gpio.bsc_i2c(I2C_DEVICE)
    if length:
        print(f"I2C slave callback {status:02x} {length:02x} {data}")
    response: bytearray = bytearray(8)
    if length:  # read or write
        for reg_num in data:
            if reg_num in READABLE_REG:
                status, length, data = self.gpio.bsc_i2c(
                    I2C_DEVICE,
                    self.registers[reg_num:reg_num+1]
                    )
0

There are 0 answers