FAQ: How can I make a software-based (bit-bang) SPI bus/I need 2 SPI devices/Can I use any 4 GPIOs as an SPI bus?
-
Software-based (bit-bang) SPI on the GPIOs is doable!
The hardware SPI can only drive 2 devices, as there is only CS0 (already used by internal flash) and CS1.
If you need more than 1 additional SPI device, you'll need to resort to software (GPIO based) SPI. (Also known as bit-bang SPI).
Setting up a Software I2C Bus
Install the required kernel modules
opkg update opkg install kmod-spi-gpio-custom
Then load the
spi-gpio-custom
kernel module:# configure software GPIO SPI # bus<Y>=<devid>,<SCK>,<MOSI>,<MISO>,<mode>,<speedInHz>,<CS> insmod spi-gpio-custom bus0=1,11,5,4,0,100000,18
Where
- Y just numbers the software SPI "buses" controlled by
spi-gpio-custom
. If you need multiple software SPI, you need to specify all of them on the same insmod command line, and specify parameters likebus0=... bus1=... bus2=...
- devid is the number X you'll have in the /dev/spidevX.0 name
- SCK, MOSI, MISO, CS are GPIO numbers for the respective signals
- mode is the SPI mode, usually 0
- speedinHz is the speed. Note that software SPI speed is limited, you'll not get the HW SPI top speed with it
The example above will get you a
/dev/spidev1.0
on pins 11,5,4 and 18, in mode 0 and with 100kHz speed.Drawbacks
Lower Bus Speed. Since this is a software-based bus, it will not be as fast as a hardware SPI bus.
Credit for this FAQ goes to @luz from this thread
- Y just numbers the software SPI "buses" controlled by