I recently designed a PCB that features several SPI devices. One of the devices is the Microchip MCP23S17 GPIO expander (http://ww1.microchip.com/downloads/en/DeviceDoc/20001952C.pdf). I have the chip select line connected to GPIO 24 (pin 18) on the Raspberry Pi 3.
Now I was previously planning on doing the SPI communications in C, however, I thought I'd get Python a try since everyone is always raving about it.
I went ahead and downloaded the Python spidev library (http://tightdev.net/SpiDev_Doc.pdf). One thing I noticed is that it forces you to choose one of the two chip select lines listed on the Raspberry Pi. I didn't plan on using these preselected IO points because I have more than two spi devices.
Instead, I'm manually toggling GPIO 24 before I issue a SPI data transfer. I've also went ahead and configured the Python SPI device to "chip select 0" which isn't connected to anything in my circuit. Thus I let "think" it has control over a chip select line when it actually doesn't.
However, now I can't get any of the SPI communications to work.
I've troubleshooted the following: - Verified the GPIO expander IC has power - Verified good solder connection to the pad - Verified chip select is actually being toggled
However, I haven't put a scope up to it yet since I don't currently have access to one.
For anyone who has worked with the Python SPI library, do you see any issues with what I'm trying to do?
Code:
#! /usr/bin/python
import spidev
import time
import RPi.GPIO as GPIO
#######################
#### FUNCTION DEFS ####
#######################
def configMCP23S17():
# config port direction
to_send = [0x40, 0x00, 0xFF]
GPIO.output(24, GPIO.LOW)
resp = spi.xfer2(to_send)
GPIO.output(24, GPIO.HIGH)
#config polarity
to_send = [0x40, 0x02, 0x00]
GPIO.output(24, GPIO.LOW)
resp = spi.xfer2(to_send)
GPIO.output(24, GPIO.HIGH)
#config pull up resistors
to_send = [0x40, 0x0C, 0xFF]
GPIO.output(24, GPIO.LOW)
resp = spi.xfer2(to_send)
GPIO.output(24, GPIO.HIGH)
# end def
def readMCP23S17():
#read port A
GPIO.output(24, GPIO.LOW)
to_send = [0x40, 0x12, 0x00]
resp = spi.xfer2(to_send)
GPIO.output(24, GPIO.HIGH);
return resp
#end def
#######################
#### PROGRAM START ####
#######################
GPIO.setmode(GPIO.BCM)
GPIO.setup(05, GPIO.OUT)
GPIO.setup(06, GPIO.OUT)
GPIO.setup(12, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
GPIO.output(05, GPIO.HIGH)
GPIO.output(06, GPIO.HIGH)
GPIO.output(12, GPIO.HIGH)
GPIO.output(24, GPIO.HIGH)
spi = spidev.SpiDev()
spi.open(0, 0)
configMCP23S17()
try:
while True:
output = readMCP23S17()
print(output)
time.sleep(1.0)
except KeyboardInterrupt:
spi.close()