Create a small delay in the Linux interrupt handler

I am working on an interrupt handler with a hardware design team and we are trying to figure out where the error is. I am reading a chip on the 5 kHz SPI bus. The chip loads 4 bytes and starts data output.

My interrupt handler wakes up and reads 4 bytes from the SPI bus and saves the data in the buffer. Oddly enough, every 17th reading gives 4 bytes of all 0, which is incorrect. One of the options that we are studying is that the chip is not always ready when it sends a data ready signal.

So, I know that I can’t sleep in the interrupt handler, but I would like to try to introduce a delay of 10 or 20 microseconds. Right now I have a for loop that is 100,000 and then handles the interrupt. I did not see any changes, so I thought I could see if anyone had the best technique for lively waiting. Or at least the best way to figure out how many cycles I have to go through, since I'm not sure how long it will take, or if the compiler just optimizes it all.

+1
c linux interrupt embedded spi
source share
2 answers

I don’t know if you have access to any pseudo-random number generation libraries on the built-in device, but with a large number of multiplications, followed by the mod, it will definitely take several cycles. Instead of just adding 1 (which is very fast at the hardware level, and the compiler can optimize it for switching, since you do this static number of times), use a random numerical seed (does the system have access to the time clock?), If available, and execute multiple multiplication, module or factorial operations, negative division of a number also lasts forever. Remember that separation takes the largest place at the hardware level. Take advantage of this.

+1
source share

I assume that your compiler will cross out a simple loop.

You must use volatile.

volatile unsigned long i; for (i=0;i< 1000000; i++) continue; 

I also assume that this will not fix the problem or help you.

I can’t believe that the SPI peripheral has such an error.

But it’s possible that you are reading to slow down data from SPI-Fifo.
Therefore, some of the data received will be deleted.

You should check the SPI module error flags and check the RX-empty RX-fullflags SPI.

0
source share

All Articles