Skip to content

Instantly share code, notes, and snippets.

@mcbridejc
Last active July 1, 2024 11:25
Show Gist options
  • Save mcbridejc/d060602e892f6879e7bc8b93aa3f85be to your computer and use it in GitHub Desktop.
Save mcbridejc/d060602e892f6879e7bc8b93aa3f85be to your computer and use it in GitHub Desktop.
How to add or change SPI chip select pins on raspberry PI with device tree overlay

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.

/dts-v1/;
/plugin/;
/ {
compatible = "brcm,bcm2835";
fragment@0 {
target = <&spi0_cs_pins>;
frag0: __overlay__ {
brcm,pins = <8 7 1 5 6>;
};
};
fragment@1 {
target = <&spi0>;
frag1: __overlay__ {
#address-cells = <1>;
#size-cells = <0>;
cs-gpios = <&gpio 8 1>, <&gpio 7 1>, <&gpio 1 1>, <&gpio 5 1>, <&gpio 6 1>;
status = "okay";
spidev0_2: spidev@2 {
compatible = "spidev";
reg = <2>;
#address-cells = <1>;
#size-cells = <0>;
spi-max-frequency = <125000000>;
};
spidev0_3: spidev@3 {
compatible = "spidev";
reg = <3>;
#address-cells = <1>;
#size-cells = <0>;
spi-max-frequency = <125000000>;
};
spidev0_4: spidev@4 {
compatible = "spidev";
reg = <4>;
#address-cells = <1>;
#size-cells = <0>;
spi-max-frequency = <125000000>;
};
};
};
};
@zultekeng
Copy link

Great work guys, I have 20 spi devices to control.... Wanting to use the RPi Zero2 for my project, is this also compatible with the BCM2710A1, if so is it just a matter of modifying the "compatible = " line to add the required processor? Thanks for your help

@beartronix
Copy link

Great work, I love it! I had to remove GPIO1 from the list though, as I'm using the PoE Hat Plus Fan, which uses i2c0 (GPIO1 and GPIO0) for PWM control over i2c.

For reference: rpi-poe-plus-overlay.dts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment