Hello everyone,
I've been working on a project using SPI (cf this discussion). I've been struggling with some delay that appears before each transfer.
It's not a constant delay and it varies roughly between 40 µs and few hundreds microseconds, as you can see in the screenshot.

The time references are taken between two consecutive code lines in the render.cpp file. In pseudo code it would be something like that, see the comments:
setup() {
...
spiDevice.setup({
.device = "/dev/spidev1.0",
.speed = 16000000,
.delay = 0,
.numBits = 8,
.mode = Spi::MODE1
});
...
SpiTask = Bela_createAuxiliaryTask(readSpi, 50, "bela-Spi", (void*) context);
}
readSpi(...) {
if (data_ready) {
custom_select_device(); // end of instruction is the first time mark
spiDevice.transfer(Tx, Rx, transmissionLength); // this begins the transfer, supposedly second time mark
}
}
The custom_select_device is a function that writes to some IO to select a device through some custom HW block.
I'm using the Spi library of the Bela project #include <libraries/Spi/Spi.h> .
At first I thought the delay was introduced from preemption by the RT audio thread, so I tried to move the SPI transfer inside the render function but I had the same results.
Do you have any idea where this delay is coming from, and how can I workaround it ?
I might have to try using the PRU to bitbang the SPI protocol, but I'd rather be sure of the cause of this behavior beforehand.
Thank you for reading, any help would be appreciated