SPI with timeout

I have a project with SPI communication to a LoRa module. I want to get information about availabilty of the LoRa module, but the SPI interface has no timeout functionality.
The problem is the transfer function:

byte SPIClass::transfer(byte _data) {
  SPDR = _data;
  while (!(SPSR & _BV(SPIF)))
    ;
  return SPDR;
}

I tried to replace the SPI and LoRa library with own local ones, where the transfer function is replaced and the SPI library is renamed (this is only one possibility to get a request even when the SPI interface is corrupted):

byte SPIClass::transfer(byte _data) {
  SPDR = _data;
  for (unsigned long counter = 0; counter < 100000L; counter++)
    {
    if (SPSR & _BV(SPIF))
       break;
    }
  return SPDR;
}

but the compiler always takes the original SPI library.
My questions:

  • how can I force the compiler to use the local SPI library.
  • Why has the internal SPI library not a timeout functionality. It would be helpful for projects to monitor the availability of peripheral devices.

Thanks for helpfully answers.

There is no generic timeout function, because the SPI interface itself has no concept of what a valid or not valid response is.

The valid data an SPI device can provide when selected and a clock is applied, could be 0x00 or 0xFF, so how can you tell if there is no device there ?

Detecting a SPI 'timeout' needs to be specific to the actual device.

To tell if a LoRa device is actually present is trivial, just choose a register that can be written to and read\write some values to it and see if it changes appropriately.

When I write to a register, I do not have a response. Thats OK. But when you want to read the register, the application hangs in the transfer "while" in a permanent loop.

Cannot assist with that, I have never attempted to use SPI devices outside of the standard (and working) SPI library functions and neither can I think of a good reason to do so.

Did you try #include “SPI.h” instead of <SPI.h> ?