The raspberry pi SPI0 by default has 2 CS pins configured. The SPI driver in the kernel uses GPIOS toggled by software, rather than hardware controlled chip selects. This means that any GPIO can be used for a chip select, and any number of them can be supported concurrently. All of these setup for the SPI driver is defined in the device tree, and we can use device tree overlays stored in /boot to dynamically configure the device tree.
The attached example creates a SPI device with 5 CS pins, on GPIO 8, 7, 1, 5, and 6.
To compile it to a binary:
dtc -@ -I dts -O dtb -o spi-cs-extend.dtbo spi-cs-extend.dts
Then place spi-cs-extend.dtbo
into /boot/overlays
and add the following line to your /boot/config.txt
:
dtoverlay=spi-cs-extend
.
After your next reboot, you should find /dev/spi0.2
, /dev/spi0.3
, and /dev/spi0.4
have been created.
Thanks for sharing, and a note to anyone looking to use this... To change the pin numbers is quite obvious, but to have 3 chip selects I had to modify line 11 and 21 by changing the BCM pin numbers, then lines 26, 34, and 42 to change the register to 0, 1, and 2. Also, 24, 32, and 40 needed their indexes changing. My finished overlay is as follows and works a treat.