adafruit / Adafruit_Blinka

Add CircuitPython hardware API and libraries to MicroPython & CPython devices
https://learn.adafruit.com/circuitpython-on-raspberrypi-linux
MIT License
453 stars 340 forks source link

ftdi_mpsse/mpsse/spi.py write_readinto() doesn't return buffer contents to caller #693

Closed EazyBreeze closed 1 year ago

EazyBreeze commented 1 year ago

Integrating to a MCP4151 digipot via SPI with the Adafruit FT232H Breakout. When calling to read the set wiper resistance via busio.write_readinto(), a value is never returned. The in buffer is read correctly but no return statement is defined to return it to the caller. src/adafruit_blinka/microcontroller/ftdi_mpsse/mpsse/spi.py line 100.

In my case, the relying caller that never received the buffer contents was src/busio.py SPI.write_readinto(). Which always returns None.

I confirmed in a few experiments the resistance is correctly read and represented in the buffer_in variable.

caternuson commented 1 year ago

This is expected behavior. There is no return, so it will always return None. The read results are copied into the buffer passed via the buffer_in parameter.

EazyBreeze commented 1 year ago

Thanks for the quick response. I see that buffer in is locally scoped to the write_readinto() method of the SPI class. How does it get accessed outside the SPI class then? My questioning originated from the busio.py's write_readinto(), which has a return statement defined, but does not receive anything in response from the underlying SPI class. Leading me to wonder what is actually supposed to get returned then?

Brief example

import board
import busio
import digitialio

# ... Previous setup ....

spi = busio.SPI(clock=board.SCK, MOSI=board.MOSI, MISO=board.MISO)  # FT232H board.
spi.write(set_res_bytes)  # Set a pot resistance. Works.
read_resist = spi.write_readinto(read_res_bytes, [0x00, 0x00])  # Defined to return SPI class response, but always None.
print(read_resist)  # None
EazyBreeze commented 1 year ago

I'll close this out. A pass by reference worked after a small edit.

import board
import busio
import digitialio

# ... Previous setup ....

spi = busio.SPI(clock=board.SCK, MOSI=board.MOSI, MISO=board.MISO)  # FT232H board.
spi.write(set_res_bytes)  # Set a pot resistance. Works.
read_resist = [0x00, 0x00]
spi.write_readinto(read_res_bytes, read_resist)  # read_resist, passed by reference.
print(read_resist) # Worked.