Skip to content

SPI

Kernel configuration

Verify that the user mode SPI driver (CONFIG_SPI_SPIDEV) is enabled in your kernel configuration:

  • In menuconfig: Device Drivers -> SPI support -> <*> User mode SPI device driver support

Device tree configuration

Enable the required ECSPI controller in the device tree that matches your carrier board and storage option. The exact pinmux and chip-select GPIOs depend on the carrier-board design, so use the i.MX7 pad names from the SoC reference manual and make sure they do not conflict with another enabled peripheral.

The example below shows the typical structure for adding one spidev device:

&ecspi1 {
    fsl,spi-num-chipselects = <1>;
    cs-gpios = <&gpioX Y GPIO_ACTIVE_LOW>;
    pinctrl-names = "default";
    pinctrl-0 = <&pinctrl_ecspi1>;
    status = "okay";

    spidev@0 {
        compatible = "var,spidev";
        spi-max-frequency = <12000000>;
        reg = <0>;
    };
};

&iomuxc {
    pinctrl_ecspi1: ecspi1grp {
        fsl,pins = <
            /* Add the ECSPI1 MISO, MOSI, SCLK, and CS pads here. */
        >;
    };
};

Build and install the updated device tree by following the Build the Linux kernel from source code guide.

Testing

The kernel source tree includes an SPI test utility at tools/spi/spidev_test.c.

Compile it with the Debian cross-compiler:

arm-linux-gnueabihf-gcc \
  ./tools/spi/spidev_test.c -o ./spidev_test

Copy spidev_test to the target and run a basic transfer:

./spidev_test -D /dev/spidevM.N

where M is the SPI bus index and N is the chip-select index.

For a loopback test, short MISO and MOSI on the selected SPI bus and run:

echo "SPI test packet" > /tmp/SPI.in
echo "" > /tmp/SPI.out
./spidev_test -v -D /dev/spidevM.N -i /tmp/SPI.in -o /tmp/SPI.out