Skip to main content

SPI

Introduction

SPI is a high-speed, full-duplex, synchronous serial communication interface for connecting microcontrollers, sensors, storage devices, etc.

Face-RK3399 SPI leads to a SPI2(reusable GPIO) for external use.

Face-RK3399 development board provides the SPI4 (single chip optional) interface, and the specific position is as follows:

_images/spi.png

How SPI works

SPI works in a master-slave mode, which typically has one master device and one or more slave devices, requiring at least four wires, respectively:

CS #Slice selection signal
SCLK #Clock signal
MOSI #Master device data output and slave device data input
MISO #Master device data input and slave device data output

The Linux kernel uses a combination of CPOL and CPHA to represent the four working modes of the current SPI:

CPOL=0,CPHA=0 SPI_MODE_0
CPOL=0,CPHA=1 SPI_MODE_1
CPOL=1,CPHA=0 SPI_MODE_2
CPOL=1,CPHA=1 SPI_MODE_3
  • CPOL : Represents the state of the initial level of the clock signal, 0 is the low level and 1 is the high level.
  • CPHA : Is sampling along which clock, 0 is sampling along the first clock and 1 is sampling along the second clock.

The waveforms of SPI’s four working modes are as follows:

_images/SPI_work_en.jpg

Drive coding

The following W25Q128FV Flash module as an example of a simple introduction to the preparation of SPI driver.

Makefile/Kconfig

Add the corresponding driver file configuration in kernel/drivers/spi/Kconfig:

config SPI_FIREFLY
tristate "Firefly SPI demo support "
default y
help
Select this option if your Firefly board needs to run SPI demo.

Add the corresponding driver file name in kernel/drivers/spi/Makefile:

obj-$(CONFIG_SPI_FIREFLY) += spi-firefly-demo.o

Select the added driver file in config, such as:

│ Symbol: SPI_FIREFLY [=y]
│ Type : tristate
│ Prompt: Firefly SPI demo support
│ Location:
│ -> Device Drivers
│ -> SPI support (SPI [=y])
│ Defined at drivers/spi/Kconfig:704
│ Depends on: SPI [=y] && SPI_MASTER [=y]

DTS configuration

Add SPI driver node description in kernel/arch/arm64/boot/dts/rockchip/rk3399-firefly-demo.dtsi:

&spi4 {
status = "disabled";
max-freq = <48000000>;
spidev@00 &#123;
status = "disabled";
compatible = "linux,spidev";
reg = <0x00>;
spi-max-frequency = <48000000>;
&#125;;
&#125;;
  • status: Set okay if you want to enable SPI, or disable if not.
  • compatible: The attribute here must be compatible with the member of the structure in the driver: of_device_id.
  • reg: This is consistent with spi-demo@00, set to 0x00 in this example.
  • spi-max-frequency: Set the highest frequency used by spi. RK3399 supports up to 48000000.

Define SPI drivers

Create a new driver file in kernel/drivers/spi/, such as: spi-firefly-demo.c.

Before defining the SPI driver, should define the variable of_device_id. *of_device_id is used to call the device information defined in the DTS in the driver. The definition is as follows:

static struct of_device_id firefly_match_table[] = &#123;&#123; .compatible = "linux,spidev",&#125;,&#123;&#125;,&#125;;

The values of compatible here are consistent with those in the DTS.

spi_driver is defined as follows:

static struct spi_driver firefly_spi_driver = &#123;
.driver = &#123;
.name = "firefly-spi",
.owner = THIS_MODULE,
.of_match_table = firefly_match_table,&#125;,
.probe = firefly_spi_probe,
&#125;;

Registration of SPI equipment

spi_register_driver(&firefly_spi_driver) was registered into kernel by Static int __init spidev_init(void).

If the kernel is successfully matched on startup, the SPI core will configure SPI’s parameters (mode, speed, etc.) and call firefly_spi_probe.

Read-write SPI date

Firefly_spi_probe uses two interface operations to read the ID of W25Q128FV:

  • The firefly_spi_read_w25x_id_0 interface uses spi_transfer and spi_message to send data.
  • The firefly_spi_read_w25x_id_1 interface uses the SPI interface spi_write_then_read to read and write data.

After success, it will print:

root@rk3399_firefly_face:/ # dmesg | grep firefly-spi
[ 1.006235] firefly-spi spi0.0: Firefly SPI demo program
[ 1.006246] firefly-spi spi0.0: firefly_spi_probe: setup mode 0, 8 bits/w, 48000000 Hz max
[ 1.006298] firefly-spi spi0.0: firefly_spi_read_w25x_id_0: ID = ef 40 18 00 00
[ 1.006361] firefly-spi spi0.0: firefly_spi_read_w25x_id_1: ID = ef 40 18 00 00

Open SPI demo

The Spi of Face-RK3399 is not opened by default. User can open it in rk3399-firefly-face.dtsi:

&spi4 &#123;
status = "disabled";
max-freq = <48000000>;
spidev@00 &#123;
- status = "disabled";
+ status = "okay"
compatible = "linux,spidev";
reg = <0x00>;
spi-max-frequency = <48000000>;
&#125;;

Common SPI interface

Here are the common SPI API definitions:

void spi_message_init(struct spi_message *m);
void spi_message_add_tail(struct spi_transfer *t, struct spi_message *m);
int spi_sync(struct spi_device *spi, struct spi_message *message) ;
int spi_write(struct spi_device *spi, const void *buf, size_t len);
int spi_read(struct spi_device *spi, void *buf, size_t len);
ssize_t spi_w8r8(struct spi_device *spi, u8 cmd);
ssize_t spi_w8r16(struct spi_device *spi, u8 cmd);
ssize_t spi_w8r16be(struct spi_device *spi, u8 cmd);
int spi_write_then_read(struct spi_device *spi, const void *txbuf, unsigned n_tx, void *rxbuf, unsigned n_rx);

Please refer origin code to get usage: /kernel/driver/spi/spidev.c.

FAQs

Q1: SPI data transfer exception?

A1 : Make sure the IOMUX configuration of SPI 4 pins is correct. Confirm that when TX sends data, TX pins have normal waveform, CLK frequency is correct, CS signal is pulled down, and mode matches the device.