I'm using a Pi 3B to interface with an SPI device. My program is written in Python 2, the Pi is the SPI master and the external device is the slave. I am using both the spidev module and the RPi.GPIO modules, with good success. I can communicate with my SPI slave using either spidev or a bit-banged SPI driver I wrote using RPi.GPIO. Both work fine on their own.
My troubles arise when I communicate using my bit-banged RPI.GPIO-based driver, and then try to use spidev: I can see on my oscilloscope that spidev can't drive the MOSI or SCLK lines after I've used my bit-banged RPi.GPIO driver.
The cause of this is clear to me, using the gpio readall
command I can see that RPi.GPIO.cleanup()
leaves the MOSI and SCLK pins as INPUTS. Prior to running any of my code (i.e. right after booting the Pi) I can see that MOSI and CLK are set to ALT0 -- just what I'd expect.
My question is, how can I get RPi.GPIO to, on exit, reconfigure the GPIO pins as they were before I used RPi.GPIO? Or, if that's not possible, is there a way to assign a GPIO pin to an alternate function (like ALT0 in this case) either from Python or the command line?
I'm pretty sure it's possible from C, but I'd like to avoid that route if possible.
Now for the obligatory code snippets:
This works (using spidev):
import spidev
spi = spidev.SpiDev()
spi.open(0, 0)
spi.xfer([0xaa]) # results appear on the scope as expected
spi.close()
This also works (using a bit-banged driver):
import RPi.GPIO as gpio
gpio.setmode(gpio.BCM)
gpio.setup(10, gpio.OUT)
gpio.setup(11, gpio.OUT)
gpio.setup(8, gpio.OUT)
# bit-bang pins 10, 11 and 8 here ...
# scope signals look good, just as expected
gpio.cleanup() # this leaves MOSI & CLK as INputs
# *not* calling gpio.cleanup() leaves MOSI and CLK
# as OUTputs, which doesn't work either
But after the above RPi.GPIO-based code executes, the spidev code doesn't work any longer.
I don't have anything hooked up to the GPIO pins other than the scope itself -- there are no loads or supplies other than what the Pi provides.
I sure would appreciate any ideas!