What error are you getting? Mutex error?
I have never used USBMIDI and cannot provide MIDI examples. But, if you haven’t tried EventQueue
, you may want to look into it.
https://os.mbed.com/docs/mbed-os/v5.15/tutorials/the-eventqueue-api.html
If your error occurs because your program executes codes in an interrupt context, you may be able to solve that by deferring the execution to a different context using EventQueue
.
Below is an example. This postpones an execution of myCallback
function (which printf
and writes to SPI port) from ISR context to a user context.
#include "mbed.h"
#define MOSI p5
#define MISO p6
#define SCLK p7
#define CS p8
EventQueue *queue = mbed_event_queue(); // event queue
RawSerial pc(USBTX, USBRX);
SPI spi(MOSI, MISO, SCLK);
DigitalOut cs(CS);
void onDataReceived();
void myCallback() {
char c = pc.getc();
printf("received: %02x\r\n", c);
cs = 0;
spi.write(c);
cs = 1;
pc.attach(&onDataReceived, Serial::RxIrq); // reattach interrupt
}
void onDataReceived() {
pc.attach(NULL, Serial::RxIrq); // detach interrupt
queue->call(myCallback); // process in a different context
}
int main() {
pc.attach(&onDataReceived, Serial::RxIrq);
queue->dispatch_forever();
}
See queue->call(myCallback)
in onDataReceived
. If I have just myCallback()
there instead, it crashes because it is not allowed to do printf and spi write in ISR context.