0

I learned, that SPI-code uses interrupts. So is it true then, that I can't use SPI related code inside an ISR?

Background: I want to capture one or more revolution speeds with an MCP23S17. I rewired the interrupt lines to INT1 and INT2 accordingly to get the interrupts at the Arduino.

I thought it was good to fetch the port registers from the MCP23S17 inside the ISR, but this apparently doesn't work.

Is the only thing I can do in my ISR set a flag for another test in the main loop? And let a routine called from lood() then poll the registers from the port expander? This opposes the idea to fetch the relevant data which issued the interrupt ASAP.

asked Aug 20, 2015 at 9:29
10
  • 1
    This is why you don't use port expanders for timing-sensitive applications. Commented Aug 20, 2015 at 9:39
  • And instead of port expanders i shall use what? Commented Aug 20, 2015 at 9:42
  • Real IO pins on the microcontroller. Commented Aug 20, 2015 at 9:45
  • 1
    Please post your code. Majenko is perfectly correct. SPI does not use interrupts, at least during SPI.transfer(). I learned, that SPI-code uses interrupts. - where did you learn that? Commented Aug 20, 2015 at 10:23
  • 2
    apparently doesn't work. - I hate that expression. What happened? What did you expect to happen? In what way did they differ? Commented Aug 20, 2015 at 10:25

1 Answer 1

4

SPI doesn't use interrupts. It references the interrupt flag to know if a transfer has been completed, but it doesn't actually have interrupts enabled:

SPDR = data;
asm volatile("nop");
while (!(SPSR & _BV(SPIF))) ; // wait
return SPDR;

It should be perfectly possible to use SPI within the interrupt - indeed there is portions of the SPI API that deal specifically with this - see https://www.arduino.cc/en/Reference/SPIusingInterrupt for instance.

answered Aug 20, 2015 at 9:45
1
  • Ok, I missed that. Then there must be another thing I apparently have botched... Commented Aug 20, 2015 at 9:53

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.