InterruptIn with I2C interface on F042K6

Ahoj,

please use these symbols ``` before and after your code in your post. Then it looks nicely and it is better readable.

Since you probably use the Mbed2, it should not so big impact but anyway.
During an ISR is not recommended of using a long wait, a printf and another slow, blocking or complicated functions. You want to have ISR shortest as is possible.

You can set a flag in the ISR and the flag can be processed and cleaned in the main loop. For example like bellow.

#include "mbed.h"

DigitalOut led(LED1);
InterruptIn button(USER_BUTTON);

volatile bool button_state = false;

void cb_button(){
    if(!button_state){
        button.disable_irq()   // protection against further interruption
        button_state = true;
    } 
}

int main(){
    printf("Starting\n");
    button.fall(callback(&cb_button));
    while (true) {
        if(button_state){
            printf("Button was pressed!\n");
            // do something magic - I2C transmission and so on
            button_state = false;
            button.enable_irq();
        }
        led = !led;
        wait(0.1);
    }
}

BR, Jan

1 Like