Hardware Reference
In-Depth Information
These bit values are simply or-ed together to specify the options that are required.
The use of
SPI_CPOL
implies CPOL = 1. Its absence implies CPOL = 0. Similarly, the use
of
SPI_CPHA
implies CPHA = 1 (otherwise, CPHA = 0). The options
SPI_MODE_x
use the
SPI_CPOL
and
SPI_CPHA
macros to define them. You don't need to use them both in your
code. The mode definitions are shown here:
#define SPI_MODE_0 (0|0)
#define SPI_MODE_1 (0|SPI_CPHA)
#define SPI_MODE_2 (SPI_CPOL|0)
#define SPI_MODE_3 (SPI_CPOL|SPI_CPHA)
The unsupported options are not shown, though one or more of these could be
supported in the future.
■
the mode values
SPI_LOOP
,
SPI_LSB_FIRST
,
SPI_3WIRE
, and
SPI_READY
are not
currently supported in the wheezy release of raspbian Linux.
Note
The following is an example that defines
SPI_MODE_0
:
uint8_t mode = SPI_MODE_0;
int rc;
rc = ioctl(fd,SPI_IOC_WR_MODE,&mode);
if ( rc < 0 ) {
perror("Can't set SPI write mode.");
If you'd like to find out how the SPI driver is currently configured, you can read the
SPI mode with
ioctl(2)
as follows:
uint8_t mode;
int rc;
rc = ioctl(fd,SPI_IOC_RD_MODE,&mode);
if ( rc < 0 ) {
perror("Can't get SPI read mode.");