spi_cmdread()

The spi_cmdread() function sends a command to, and then reads data from, a specific device on SPI bus. The prototype for this function is:

int spi_cmdread( int fd,
                 uint32_t device,
                 void *cbuf,
                 int16_t clen,
                 void *rbuf,
                 int rlen );

The arguments are:

fd
The file descriptor returned by spi_open().
device
The device ID with at most one of the following flags optionally ORed in:
  • SPI_DEV_LOCK
  • SPI_DEV_UNLOCK
cbuf
A pointer to the command buffer.
clen
The command length, in bytes.
rbuf
A pointer to the receive buffer.
rlen
The read length, in bytes.

The function returns the number of bytes of data that it successfully read. If an error occurred, the function returns -1 and sets errno:

EIO
The write to the device failed, or a hardware error occurred.
EINVAL
The device ID is invalid, or you're trying to unlock a device that isn't locked.
ENOMEM
Insufficient memory.
EPERM
The device is locked by another connection.

An SPI driver typically considers it to be an error if the number of bytes returned by this function isn't the same as the number of bytes it asked the function to read.

Note: You can achieve the same results by calling spi_xchange().