8 years, 8 months ago.

The simple program hangs when using USBMIDI(LPC1768)

Hi, firends! Help to solve a problem, please. It is very simple program hangs when pressing the Click button (at an entrance to interruption). Thus the message of NoteOn (2) passes, but the message of NoteOff (2) doesn't pass and the program hangs.

#include "mbed.h"
#include "USBMIDI.h"

USBMIDI midi;

InterruptIn Click( P0_25 );

void Click_handler()
{
    midi.write(MIDIMessage::NoteOn(2));
    wait(0.25);
    midi.write(MIDIMessage::NoteOff(2));        
}

int main() 
{         
    Click.rise(&Click_handler);
    while (1)
    {   
        midi.write(MIDIMessage::NoteOn(1));
        wait(0.25);
        midi.write(MIDIMessage::NoteOff(1));
        wait(0.5);
      
    }
}

I thank for the help!

UPD

I made polling of the button in an infinite cycle and the program worked. But why it doesn't work with interruption?

#include "mbed.h"
#include "USBMIDI.h"

DigitalIn       Click( P0_25 );

USBMIDI midi;

int main() 
{         
    while (1)
    {   
        midi.write(MIDIMessage::NoteOn(1));
        wait(0.25);
        midi.write(MIDIMessage::NoteOff(1));  
        if( Click )
        {
            midi.write(MIDIMessage::NoteOn(2));
            wait(0.25);
            midi.write(MIDIMessage::NoteOff(2));     
        }
        
    }
}

1 Answer

8 years, 8 months ago.

Don t use wait() function into an interrupt !!!

https://developer.mbed.org/handbook/InterruptIn

Thank you, Raph! I commented line with wait()

void Click_handler()
{
    midi.write(MIDIMessage::NoteOn(2));
    //wait(0.25);
    //midi.write(MIDIMessage::NoteOff(2));        
}

but the problem remains.

posted by ro mu 02 Sep 2015

Usbmidi use a lot of while functions... So, may be a better way of doing your job with an interrupt in could be something as :

Declare a volatile flag int and set it to 0. Set this value to 1 inside the interrupt function.

Before you have to create a thread with the note on and note off as you want (even with wait functions)

Inside the trhead use an loop and test the value of the volatile flag. If the value is 1 then execute the midi process and at the end set the flag back to 0.

Use rtos thread functions to realize it.

https://developer.mbed.org/handbook/RTOS

posted by Raph Francois 02 Sep 2015