Serial peripheral interface (SPI)

The NXP i.MX6UL CPU has four SPI buses.

On the ConnectCore 6UL system-on-module:

On the ConnectCore 6UL SBC Express:

On the ConnectCore 6UL SBC Pro:

Kernel configuration

You can manage the SPI driver support through the kernel configuration option:

This option is enabled as built-in on default default ConnectCore 6UL SBC Pro kernel configuration file.

Platform driver mapping

The SPI bus driver for the ConnectCore 6UL system-on-module is located at drivers/spi/spi-imx.c.

Device tree bindings and customization

The i.MX6UL SPI interface device tree binding is documented at Documentation/devicetree/bindings/spi/fsl-imx-cspi.txt.

The common i.MX6UL CPU device tree defines all the SPI ports. The platform device tree must:

Example: SPI1 port (as master) on the ConnectCore 6UL SBC Pro

ConnectCore 6UL SBC Pro device tree
/* ECSPI1 (as master) */
&ecspi1 {
    fsl,spi-num-chipselects = <1>;
    cs-gpios = <&gpio3 26 GPIO_ACTIVE_LOW>;
    pinctrl-names = "default";
    pinctrl-0 = <&pinctrl_ecspi1_master>;
    status = "okay";
 
    /*
     * Add your slave devices here. Next is an example of spidev.
     * Expect a harmless kernel warning if you enable spidev as slave.
     */
    spidev@0 {
         reg = <0>;
         compatible = "spidev";
         spi-max-frequency = <1000000>;
     };
 };
 
&iomuxc {
    imx6ul-ccimx6ul {
        pinctrl_ecspi1_master: ecspi1grp1 {
            fsl,pins = <
                MX6UL_PAD_LCD_DATA20__ECSPI1_SCLK    0x10b0
                MX6UL_PAD_LCD_DATA22__ECSPI1_MOSI    0x10b0
                MX6UL_PAD_LCD_DATA23__ECSPI1_MISO    0x10b0
                MX6UL_PAD_LCD_DATA21__GPIO3_IO26     0x10b0 /* Chip Select */
            >;
        };
    };
};

SPI user space usage

The SPI bus cannot be accessed directly from user space. Instead, it is accessed via the SPI client drivers. However, a special sample client driver allows raw access to the SPI bus.

SPI device interface

The Linux kernel offers a sample client driver called spidev that gives you read and write data access to the SPI bus through the /dev interface:

You can find this driver under the kernel configuration option User mode SPI device driver support (CONFIG_SPI_SPIDEV). On Digi Embedded Yocto this driver is enabled as a loadable module. The default device tree includes the spidev node in the device tree as an SPI device hanging from the SPI bus:

/* ECSPI1 (as master) */
&ecspi1 {
    status = "okay";
  
    /*
     * Add your slave devices here. Next is an example of spidev.
     * Expect a harmless kernel warning if you enable spidev as slave.
     */
    spidev@0 {
        reg = <0>;
        compatible = "spidev";
        spi-max-frequency = <1000000>;
    };
};

To use it, load the spidev module from user space:

# modprobe spidev
spidev spi0.0: buggy DT: spidev listed directly in DT

Note Spidev is not a real hardware SPI slave device but a detail of how Linux controls a device. Expect a harmless kernel warning for having spidev in the device tree. For reference, see https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=956b200a846e324322f6211034c734c65a38e550

Linux will create a device node in the form /dev/spidevX.Y device node where X corresponds to the SPI port index, starting at zero (as per the ports enabled in the device tree), and Y corresponds to the SPI bus chip select, starting at zero.

SPI device test application

Build the package dey-examples-spi in your Yocto project to install the test application spidev_test.

This spidev_test application is a simple utility used to test SPI functionality via the spidev device.

Syntax

To display the application's syntax run:

spidev_test --help

Example 1: SPI as master (loopback  test)

Short-circuit the MISO and MOSI lines of your SPI bus to create a loopback that allows the bus to receive the same data it is sending.

Run the application using your spidev node /dev/spidevX.Y, where X is the SPI bus index and Y is the SPI bus chip select:

~# spidev_test -D /dev/spidev0.0 -v
spi mode: 0x0
bits per word: 8
max speed: 500000 Hz (500 KHz)
TX | FF FF FF FF FF FF 40 00 00 00 00 95 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF F0 0D | ......@....�..................�.
RX | FF FF FF FF FF FF 40 00 00 00 00 95 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF F0 0D | ......@....�..................�.

You should receive the same data displayed in the preceding excerpt.