1 year, 3 months ago.

EventQueue not working for a user defined callback

I am trying following program (marked within <<code>> My program <</code>>). The first two calls (see comment below) work. However the next call crashes with Hard-fault. This crashing statement uses a user defined function. I am using NUCLEO-F103RB. I tried both online and offline. There is no difference.

#include "mbed.h"
#include <stdio.h>



DigitalOut led1(PB_15, 1);
DigitalOut led2(PC_1, 1);
DigitalOut led3(PC_6, 1);
DigitalOut led4(PC_3, 1);
DigitalOut led5(PC_0, 1);
DigitalOut led6(PC_7, 1);


void timerFunction() {
    led2 = !led2;
       
}

int main() {
    led1 = 1;
    
    EventQueue timer;
    
//Following 2 lines calls are working
    timer.call(printf, "called immediately\n");
    timer.call_every(2000, printf, "called periodically\n");

//Following line crashes
    timer.call_every(200, timerFunction);
    timer.dispatch();
    
    while(true) {
        Thread::wait(500);
        led1 = !led1;
    }
    
}


Hi, I tried your code on my two Nucleos (F767 and L152) without a crash. You can try examples from https://os.mbed.com/docs/v5.9/reference/eventqueue.html

posted by Jan Kamidra 29 Jul 2018
Comment on this question

1 Answer

1 year, 3 months ago.

If you want to dispatch queue you need to create a Thread for that.

int main(){
    EventQueue timer;
    Thread eventThread;
    eventThread.start(callback(&timer, &EventQueue::dispatch_forever));

    timer.call_every(2000, printf, "called periodically\n");

    while(1){
        ...
    }
}


You need to log in to post a question