0

I have anecdotal experience that using Serial.print inside a slave's SPI interrupt routine (ISR) causes issues with transmission. It also seems that interrupts may be called more than once before the main loop can react, so it is difficult to print info from all interrupts. Is this because Serial is inherently unsafe, or too slow? Can Serial be used safely inside the ISR routine?

4

Serial should be avoided in ISRs because interrupts are disabled inside ISRs, and Serial transmission uses interrupts to operate. Better to have the ISR set a flag that is read in the next iteration of loop() to print the desired info.

  • 1
    Hardware Serial write(byte) function in Arduino AVR core will work in interrupt. It checks if interrupts are disabled and if yes and the buffer is full, it executes directly the interrupt function, which pushes bytes from buffer to output. – Juraj Jan 16 at 17:46
  • @Juraj is write or print re-entrant? – qwr Jan 16 at 17:47
  • it doesn't need to be re-entrant – Juraj Jan 16 at 17:49
  • @Juraj That is interesting to know. I was unaware of that behavior – jose can u c Jan 16 at 17:49
  • 1
    @Gerben, it was before this? github.com/arduino/ArduinoCore-avr/commit/… – Juraj Jan 17 at 15:22
1

Hardware Serial write(byte) function in Arduino AVR core will work in interrupt. It checks if interrupts are disabled and if yes and the buffer is full, it executes directly the interrupt function, which pushes bytes from buffer to output.

(All write and print functions of hardware Serial use the write(byte) function to send data.)

It depends on other factors if a long time spent in interrupt handler will disturb things.

0

In general, interrupts should run as fast as possible. This is especially true when using SPI, since in the slave's ISR, the slave needs to put a byte in the SPI Data Register (SPDR) before the next transfer occurs. Thus it is good practice to avoid Serial transmission entirely in an interrupt, since in simple interrupts it can be by far the slowest portion of the interrupt.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.