8 years ago.

Add interrupt into queue

Hello,

I have many InterruptIns and as callback I send message over serial communication. The problem is when multiple interrupts are triggered, the printf function send for example two of five bytes and get interrupted, which then currupts sent datas. I have found disable_irq function, but does disable interrupts or only "block", so after enabling it, it triggers interrupt, which was block during previous interrupt?

Thanks in advance

2 Answers

8 years ago.

You can perfectly well use printf during an interrupt block, the question is if you should use it. Printf is a relative slow function (unless you use a library like BufferedSerial), and in general you don't want your interrupts blocking for longer than absolutely needed. But this is relative: If you have interrupts firing every fraction of a millisecond, don't use printfs in them. Do you have interrupts happening every few seconds it isn't a big deal generally.

To answer your question, it should only delay them until you enable interrupts again. However by default one interrupt cannot interrupt another interrupt.

Accepted Answer
8 years ago.

You can not use printf inside interruptin function.

Use an volatile variable as flag and then throw you printf indise the main loop or an external thread.

#include "mbed.h"
 
InterruptIn button(p5);
DigitalOut flash(LED4);
volatile bool doIt=false;

 
void flip() {
    doIt=true
}
 
int main() {
    button.rise(&flip);  // attach the address of the flip function to the rising edge
    while(1) {           // wait around, interrupts will interrupt this!
        flash = !flash;
        wait(0.25);

        if (doIt)
       {
           doIt=false;
           printf("Here we are\n");
       }
    }
}