SPI

This document mainly introduces how to use SPI under Ubuntu.

Enable SPI

The default SPI is disabled. If you need to use SPI, you can enable it through the overlays function.

khadas@Khadas:~$ sudo vim /boot/env.txt

Modify the overlays part, remove pwm_f and uart3, and open SPI.

1
overlays=uart3 pwm_f i2c3 os08a10 --> overlays=i2c3 os08a10 spi1

For Overlays documentation, please refer to –> How to use Device Tree Overlay

Restart, SPI will be enabled.

Connect

1
2
3
4
5
SPI
Sensor MOSI <--> SPIB_MISO
Sensor MISO <--> SPIB_MOSI
Sensor CS <--> SPIB_SS
Sensor CLK <--> SPIB_SCLK

Simple test

SPI configures a loopback test device by default.

khadas@Khadas:~$ ls /dev/spidev1.1
/dev/spidev1.1
  1. Get test code
khadas@Khadas:~$ wget https://raw.githubusercontent.com/yan-wyb/source/master/c%26c%2B%2B/embedded/app/spi/spi_test/spidev_test.c
  1. Compile test code
khadas@Khadas:~$ gcc -o spidev_test spidev_test.c
  1. Short-circuit PIN35 and PIN37

Short-circuit MISO and MOSI to form a loop, spontaneously send and receive

  1. Test
khadas@Khadas:~$ cat ./spi_in.txt
Amazing Khadas!
khadas@Khadas:~$
khadas@Khadas:~$ ./spidev_test -i spi_in.txt -o ./spi_out.txt
spi mode: 0x0
bits per word: 8
max speed: 500000 Hz (500 KHz)
khadas@Khadas:~$ cat ./spi_out.txt
Amazing Khadas!