Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save tyan-boot/0a6971fc12751966b6556a7643eb2d28 to your computer and use it in GitHub Desktop.

Select an option

Save tyan-boot/0a6971fc12751966b6556a7643eb2d28 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>;
};
};
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment