InterruptIn Stuck

Hello,

I have problem with interrupt, I am using mbed API with simplest example but once the interrupt should be triggered it hangs MCU, couldn’t find the problm. same happens when I use HAL Gpio interrupt,

here is the code

/* mbed Microcontroller Library
 * Copyright (c) 2023 ARM Limited
 * SPDX-License-Identifier: Apache-2.0
 */

#include "mbed.h"
#include "TraceVSPI.h"
#include "RelayAPI.h"
#include "ADCDirectAPI.h"
//#include "ADCAlternatingAPI.h"

DigitalOut led(G_LED, 1);
Relay rel1(Relay1), rel2(Relay2);
InterruptIn button(PE_10);



//DirectADC adc1(DC_ADC1, 4);
//DirectADC adc2(DC_ADC2, 4);

uint32_t buffer[100];
//AnalogIn adc1(DC_ADC1);
//DigitalOut adc_en(PA_6);

#define debug_printf     TraceVSPI::printf



void flip()
{
    debug_printf("!");
}

int main()
{


    ThisThread::sleep_for(100ms);
    TraceVSPI::init();
    debug_printf("Trace Init Done -- \r\n");
    ThisThread::sleep_for(500ms);

    button.mode(PullUp);
    button.rise(&flip);

    while (1){
        debug_printf("time pass -- \r\n");
        ThisThread::sleep_for(500ms);

    }

}


Hello,

what exactly is this?

I am asking because all methods like - printf, getc/s, putc/s, wrire, read and so on are covered by Mutex in whole MbedOS and Mutex can not be called in ISR. You need to set a bool flag and proces it in main loop or take the code out from ISR context via EventQueue

Also some info about your target will be handy.

BR, Jan

1 Like

Please try to use mbed_error_printf(…) instead of printf(…) in interrupt context.

Such like as:

void flip()
{
/* Call mbed_error_printf(…) instead of printf(…) in interrupt context */
mbed_error_printf(“Called in ISR\r\n”);
}

void main()
{
button.rise(&flip);

/* Call printf(...) or mbed_error_printf(...) in thread context at least once before the mbed_error_printf(...) call in interrupt context */
mbed_error_printf("Called in main\r\n");

while (true);

}

That’s my class for printf and other stuff, turns out that was problem, I removed that function from interrupt callback and everything works just fine. maybe it was taking some time there and had trouble with interrupt handler.

Definitely the printf is function that take a lot, but in Mbed also trigger Mbed OS crash when is called from ISR because of Mutex.

BR, Jan

1 Like