最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

I2C Slave respond to registry read on Raspberry Pi with Python using pigpio package - Stack Overflow

programmeradmin8浏览0评论

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

Currently, following the usual instructions for the pigpio package here: .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()

发布评论

评论列表(0)

  1. 暂无评论