SPI timeout in Linux and C

Viewed 3600

I'm trying to implement a timeout for some hardware transmissions, to add security to a big project. I already implemented timeout using select for UART transmission, but I don't know how to add a timeout in a SPI transmission.

This is my reading code:

int spi_read(int fd, char command, char* buffer, int size, int timeout)
{
    struct spi_ioc_transfer xfer[2];
    int status;

    memset(buffer, 0, sizeof(buffer));
    memset(xfer, 0, sizeof(xfer));

    xfer[0].tx_buf = (unsigned int)(&command);
    xfer[0].len = 1;

    xfer[1].rx_buf = (unsigned int)buffer;
    xfer[1].len = size;

    status = ioctl(fd, SPI_IOC_MESSAGE(2), xfer);
    if(status < 0)
        return NOERROR;
    else
        return EHWFAULT1;
}

It sends a byte sized command and receives a response of certain size (in half duplex mode). How can I implement a timeout in the response? Can it be implemented using select? Should I separe both transactions and use select or better use an alarm?

Then, I have the same question for a full duplex mode, which is implemented too using ioctl. Can you give me any hints?

1 Answers
Related