Search code examples
linuxraspberry-pi3embedded-linuxspiioctl

SPI_IOC_MESSAGE(N) on Raspberry Pi 3


I am working on a project on a Raspberry Pi 3, and I saw this code line :

ioctl(sSpiObj.spiFd, SPI_IOC_MESSAGE(1), &sSpiTransfer);        

I don't know what SPI_IOC_MESSAGE(1) does, and I tried to find on the internet its explanation, but I couldn't find it. Is there anyone who could explain what it does?


Solution

  • See the ioctl declaration from here: link You see you have to pass the request type in the second argument to tell the driver what you want. Your second argument is SPI_IOC_MESSAGE which is a macro defined in Linux code here link. It creates a request type number that incorporates the number of messages you want to send and then this is passed to the driver and then driver decodes the request type and the number of messages from this and process the third argument as messages to be send and finally sends the messages.

    From the linux kernel Documents:

     * SPI_IOC_MESSAGE gives userspace the equivalent of kernel spi_sync().
     * Pass it an array of related transfers, they'll execute together.
     * Each transfer may be half duplex (either direction) or full duplex.
     *
     *  struct spi_ioc_transfer mesg[4];
     *  ...
     *  status = ioctl(fd, SPI_IOC_MESSAGE(4), mesg);
     *
     * So for example one transfer might send a nine bit command (right aligned
     * in a 16-bit word), the next could read a block of 8-bit data before
     * terminating that command by temporarily deselecting the chip; the next
     * could send a different nine bit command (re-selecting the chip), and the
     * last transfer might write some register values.