I'm trying to read out an MCP3008 chip using hardware SPI on a pi4, it wasn't working and in an effort to debug the problem I ended up connecting the spi input pin (MISO) directly to a 3.3V pin. I can read the pin as GPIO 9 and see that it is indeed correctly being set to HIGH.
I expect in this setup I would only be able to read bits on HIGH, which should result in maxed out values from the spi transaction, regardless of the setup of any other pins. But it actually still only gives me 0. I'm guessing I messed something up with the raspberry's spi or GPIO setup which is preventing spidev from actually reading any data from the MISO pin.
Here is the python code I'm using:
import time import sys import spidev def buildReadCommand(channel): startBit = 0x01 singleEnded = 0x08 return [startBit, (singleEnded|(channel & 0xff))<<4, 0] def processAdcValue(result): byte2 = (result[1] & 0x03) return (byte2 << 8) | result[2] def readAdc(channel): r = spi.xfer2(buildReadCommand(channel)) return processAdcValue(r) if __name__ == '__main__': try: spi = spidev.SpiDev() spi.open(0, 0) # testing code, no changes: # spi.max_speed_hz = 500000 # spi.no_cs = True i = 0 while True: channel = i % 8 i += 1 val = readAdc(channel) print(str(channel) + ": " + str(val)) time.sleep(0.1) except KeyboardInterrupt: spi.close() sys.exit(0) Any direction I can look for debugging? I'm completely at a loss. Note that software SPI seems to work fine, but this is not an option for my application.