Serial Peripheral Interface (SPI)
The NXP i.MX6 CPU has five SPI buses. On the ConnectCore 6 system-on-module, only SPI1 bus is available for peripherals to use. On the ConnectCore 6 SBC, SPI1 is available through an expansion connector. The Linux driver supports the SPI bus in master mode only, and using PIO mode.
Kernel configuration
You can manage the SPI driver support through the kernel configuration options:
- Freescale i.MX SPI controllers interface (CONFIG_SPI_IMX)
- Utilities for Bitbanging SPI masters (CONFIG_SPI_BITBANG)
These options are enabled as built-in on the default ConnectCore 6 SBC kernel configuration file.
Platform driver mapping
The SPI bus driver for the ConnectCore 6 system-on-module is located at drivers/spi/spi-imx.c.
Device tree bindings and customization
The i.MX6 SPI interface device tree binding is documented at Documentation/devicetree/bindings/spi/fsl-imx-cspi.txt.
The SPI interfaces are defined in the i.MX6 CPU, ConnectCore 6 system-on-module, and ConnectCore 6 SBC device tree files.
Example: SPI1
Definition of the bus
Common i.MX6 device tree
ecspi1: ecspi@02008000 { #address-cells = <1>; #size-cells = <0>; compatible = "fsl,imx6q-ecspi", "fsl,imx51-ecspi"; reg = <0x02008000 0x4000>; interrupts = <0 31 IRQ_TYPE_LEVEL_HIGH>; clocks = <&clks IMX6QDL_CLK_ECSPI1>, <&clks IMX6QDL_CLK_ECSPI1>; clock-names = "ipg", "per"; status = "disabled"; };
IOMUX configuration
ConnectCore 6 system-on-module device tree
ecspi1 { pinctrl_ecspi1: ecspi1 { fsl,pins = < MX6QDL_PAD_EIM_D17__ECSPI1_MISO 0x100b1 MX6QDL_PAD_EIM_D18__ECSPI1_MOSI 0x100b1 MX6QDL_PAD_EIM_D16__ECSPI1_SCLK 0x100b1 MX6QDL_PAD_EIM_EB2__GPIO2_IO30 0x100b1 MX6QDL_PAD_KEY_COL2__GPIO4_IO10 0x100b1 >; }; };
Bus enabling
ConnectCore 6 SBC device tree
&ecspi1 { fsl,spi-num-chipselects = <2>; cs-gpios = <&gpio2 30 0>,<&gpio4 10 0>; pinctrl-names = "default"; pinctrl-0 = <&pinctrl_ecspi1>; status = "okay"; };
SPI user space usage
The SPI bus cannot be accessed directly from user space. Instead, it is accessed via the SPI client drivers.
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:
Enable the kernel configuration option User mode SPI device driver support (CONFIG_SPI_SPIDEV).
Add spidev node in the device tree as an SPI device hanging from your SPI bus, for example:
&ecspi1 { fsl,spi-num-chipselects = <2>; cs-gpios = <&gpio2 30 0>,<&gpio4 10 0>; pinctrl-names = "default"; pinctrl-0 = <&pinctrl_ecspi1>; status = "okay"; spidev@0 { compatible = "spidev"; reg = <0>; spi-max-frequency = <4000000>; status = "okay"; }; };
Linux will create a device node in the form /dev/spidevX.Y device node where X corresponds to the SPI bus index, starting at zero, and Y corresponds to the SPI bus chip select, starting at zero. For example, the ConnectCore 6 SBC peripheral SPI1 bus would be accessed through device node /dev/spidev0.0.
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
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, for example:
# spidev_test -D /dev/spidev0.0 spi mode: 0 bits per word: 8 max speed: 500000 Hz (500 KHz) 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 DE AD BE EF BA AD F0 0D
You should receive the same data displayed in the preceding excerpt.