[Python] I2C Slave respond to registry read on Raspberry Pi with Python using pigpio package

Discussão em 'Python' iniciado por Stack, Março 24, 2025.

  1. Stack

    Stack Membro Participativo

    I need to respond to a specific registry read over I2C on an RPi.

    Currently, following the usual instructions for the pigpio package here: https://abyz.me.uk/rpi/pigpio/python.html#bsc_xfer i can see the reads to the device address. I cannot however in the documentation see how to respond to a specific registry read on the device

    The master device (outside of my control) will read the Slave (RPi), on address 0x20. It will then look to access the register 0x1c5h (for example) to read any data stored there. I cannot see how to:

    1. Detect which register is being read
    2. See how to respond appropriately (i.e. with the data that should be "stored" at that address)

    Code used so far (ignore the prints for debug!).

    from decimal import Decimal

    import bitstring
    import time
    import pigpio

    I2C_ADDR = 0x50 #device address


    def i2c(id, tick):
    global pi

    #s, b, d = pi.bsc_i2c(I2C_ADDR)
    #print(bitstring.BitArray(d).bin) #Print anything written

    s, b, d = pi.bsc_i2c(I2C_ADDR, '1240') #respond to read

    #convert decimal to bytes
    def decimal_to_bytes(num):
    x = Decimal(str(num))
    a = x ** Decimal(1) / Decimal(7)
    s = str(a)
    b = s.encode('ascii')
    return b;


    pi = pigpio.pi()

    if not pi.connected:
    exit()

    e = pi.event_callback(pigpio.EVENT_BSC, i2c)

    pi.bsc_i2c(I2C_ADDR) # Configure BSC as I2C slave

    time.sleep(600)

    e.cancel()

    pi.bsc_i2c(0) # Disable BSC peripheral

    pi.stop()

    Continue reading...

Compartilhe esta Página