USB midi shutdown / init

mattomatto

Well-known member
Hi all,

I'm using Teensy 3.1 and I have two programs in my teensy flash and I can jump between them. Everything appears to be working nicely except they both require USB MIDI.

When running the first program at the start of flash, the midi works fine, but when jumping to the program at another position in flash, the MIDI no longer works. The port is visible, but will not send or receive.

I believe I need to shutdown the usb entirely and reinitialise it to get it working. I've found usb_shutdown() and usb_init() in usb.c, but cannot call these functions from within setup() or loop() (Cannot find function declaration)

Any help would be greatly appreciated :)


Matt
 
Hi again all,

I've made my question more concise, so hopefully Paul or someone else can shed some light on this.

Using Teensy 3.1 running the following code, the USB device doesn't disconnect.. Neither usbMIDI.end() nor Serial.end() seem to do anything?


Code:
void setup() {
  // put your setup code here, to run once:
  delay(2000);
  Serial.println("Midi device connected...");
  delay(4000);
  usbMIDI.end();
  Serial.end();
  Serial.println("Midi device should be disconnected...");
  delay(4000);
  

}

void loop() {
  // put your main code here, to run repeatedly:

}

Thanks for your help!
 
Last edited:
To shut down the USB, at the very least you'll need to turn off its interrupt and then disable the pullup resistor. Try writing a 0 to USB0_CONTROL. Then wait a good delay time before you call usb_init(). Some hubs might take half a second to properly recognize the disconnect.

Since usb_init() is in a C file, to call it from C++ code you'd add this:

Code:
extern "C" void usb_init(void);

Then you can call it like any other function.

This particular type of usage, turning the USB off and back on again, is almost certainly possible. But as you can see from the code, it's never been supported and tested. You might run into other issues.
 
To shut down the USB, at the very least you'll need to turn off its interrupt and then disable the pullup resistor. Try writing a 0 to USB0_CONTROL. Then wait a good delay time before you call usb_init(). Some hubs might take half a second to properly recognize the disconnect.

Since usb_init() is in a C file, to call it from C++ code you'd add this:

Code:
extern "C" void usb_init(void);

Then you can call it like any other function.

This particular type of usage, turning the USB off and back on again, is almost certainly possible. But as you can see from the code, it's never been supported and tested. You might run into other issues.


Thanks Paul!! This works for me:

Code:
    USB0_INTEN = 0b00000000;  //Disable USB interrupts
    USB0_CTL = 0b00000000;    //Disconnect USB devices
    delay(750);
 
How can I do the same thing for the teensy4 parts? I'd like to use software to effectively disconnect the USB cable. Then either power cycle or press PROG button to restore USB.
 
Back
Top