Hardware Reference
In-Depth Information
SPI API
The bare-metal API for SPI involves calls to
ioctl(2)
to configure the interface and
further calls to
ioctl(2)
for simultaneous read and write. The usual
read(2)
and
write(2)
system calls can be used, when a one-sided transfer is being performed.
Header Files
The header files needed for SPI programming are as follows:
#include <fcntl.h>
#include <unistd.h>
#include <stdint.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>
The
spidev.h include
file defines several macros and the
struct spi_ioc_transfer
.
Table
13-4
lists the main macros that are declared. The macros
SPI_CPOL
and
SPI_CPHA
are
used in the definitions of the values
SPI_MODE_x
. If you prefer, it is possible to use
SPI_CPOL
and
SPI_CPHA
in place of the mode macros.
Table 13-4.
SPI Macro Definitions
Macro
Supported
Description
SPI_CPOL
Yes
Clock polarity inverted (CPOL = 1)
SPI_CPHA
Yes
Clock phase is 1 (CPHA = 1)
SPI_MODE_0
Yes
SPI Mode 0,0 (CPOL = 0, CPHA = 0)
SPI_MODE_1
Yes
SPI Mode 0,1 (CPOL = 0, CPHA = 1)
SPI_MODE_2
Yes
SPI Mode 1,0 (CPOL = 1, CPHA = 0)
SPI_MODE_3
Yes
SPI Mode 1,1 (CPOL = 1, CPHA = 1)
SPI_CS_HIGH
Yes
Chip select is active high
SPI_LSB_FIRST
No
LSB is transmitted first
SPI_3WIRE
No
Use 3-Wire data I/O mode
SPI_LOOP
No
Loop the MOSI/MISO data line
SPI_NO_CS
Yes
Do not apply Chip Select
SPI_READY
No
Enable extra Ready signal