Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 6 of 6

Thread: 4.0 Running as both USB host and USB device at once

  1. #1
    Junior Member
    Join Date
    Oct 2020
    Posts
    3

    4.0 Running as both USB host and USB device at once

    I'm trying to host a MIDI device via USB2 (The USB2_DN and USB2_DP pins) on a teensy 4.0, modify the signal, and send the modified signal over USB1 (The main USB port).

    I've wired a USB plug on the backside on the board to 5v, ground, and the D+ and D- solder spots. and I'm using the USBHost_t36 library to recieve data.

    With the Example called "InputFunctions" (C:\Program Files (x86)\Arduino\hardware\teensy\avr\libraries\USBHos t_t36\examples\Serial\MIDI\InputFunctions) I am able to read the input from USB2, and print the info to console. But I have not been able to write the input as MIDI over USB1, so that I can detect it in a DAW.

    I've tried multiple other libraries to modify the example code to output both to serial, but also send the signal over USB. I have however not been able to come up with a solution that I can compile.
    The example below uses the library "MIDIUSB" where I get the following errors:

    1:
    "In file included from C:\Users\USER\Desktop\MIDItest\MIDItest.ino:3:0:
    C:\Users\USER\Documents\Arduino\libraries\MIDIUSB\ src/MIDIUSB_Defs.h:10:3: error: conflicting declaration 'typedef struct midiEventPacket_t midiEventPacket_t'
    } midiEventPacket_t;
    ^"
    2:
    "In file included from C:\Users\USER\Desktop\MIDItest\MIDItest.ino:2:0:
    C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy4/MIDIUSB.h:23:3: note: previous declaration as 'typedef struct midiEventPacket_t midiEventPacket_t'
    } midiEventPacket_t;
    ^"

    I've tried editing the two documents so that the statements don't conflict, but that just made it worse.

    I've also tried the "USBMIDI" library, but that also couldn't compile.

    It seems like the teensy is trying to read and write on the same port, so I need to Host from USB2, and be a device on USB1.

    Image of the wiring for the USB2 connection: https://imgur.com/a/7Yk6Rr3

    Included below is the modified example code which produces the errors above


    Code:
    #include <frequencyToNote.h>
    #include <MIDIUSB.h>
    #include <MIDIUSB_Defs.h>
    #include <pitchToFrequency.h>
    #include <pitchToNote.h>
    
    
    
    
    /* Receive Incoming USB Host MIDI using functions.  As usbMIDI
       reads incoming messages, handler functions are run.
       See the InputRead example for the non-function alterative.
    
       This very long example demonstrates all possible handler
       functions.  Most applications need only some of these.
       This example is meant to allow easy copy-and-paste of the
       desired functions.
    
       Use the Arduino Serial Monitor to view the messages
       as Teensy receives them by USB MIDI
    
       You must select MIDI from the "Tools > USB Type" menu
    
       This example code is in the public domain.
    */
    
    #include <USBHost_t36.h>
    
    
    USBHost myusb;
    USBHub hub1(myusb);
    USBHub hub2(myusb);
    MIDIDevice midi1(myusb);
    
    
    void setup() {
      Serial.begin(115200);
    
      // Wait 1.5 seconds before turning on USB Host.  If connected USB devices
      // use too much power, Teensy at least completes USB enumeration, which
      // makes isolating the power issue easier.
      delay(1500);
      Serial.println("USB Host InputFunctions example");
      delay(10);
      myusb.begin();
    
      midi1.setHandleNoteOn(myNoteOn);
      midi1.setHandleNoteOff(myNoteOff);
      midi1.setHandleAfterTouchPoly(myAfterTouchPoly);
      midi1.setHandleControlChange(myControlChange);
      midi1.setHandleProgramChange(myProgramChange);
      midi1.setHandleAfterTouchChannel(myAfterTouchChannel);
      midi1.setHandlePitchChange(myPitchChange);
      // Only one of these System Exclusive handlers will actually be
      // used.  See the comments below for the difference between them.
      midi1.setHandleSystemExclusive(mySystemExclusiveChunk);
      midi1.setHandleSystemExclusive(mySystemExclusive); 
      midi1.setHandleTimeCodeQuarterFrame(myTimeCodeQuarterFrame);
      midi1.setHandleSongPosition(mySongPosition);
      midi1.setHandleSongSelect(mySongSelect);
      midi1.setHandleTuneRequest(myTuneRequest);
      midi1.setHandleClock(myClock);
      midi1.setHandleStart(myStart);
      midi1.setHandleContinue(myContinue);
      midi1.setHandleStop(myStop);
      midi1.setHandleActiveSensing(myActiveSensing);
      midi1.setHandleSystemReset(mySystemReset);
      // This generic System Real Time handler is only used if the
      // more specific ones are not set.
      midi1.setHandleRealTimeSystem(myRealTimeSystem);
    }
    
    void loop() {
      // The handler functions are called when midi1 reads data.  They
      // will not be called automatically.  You must call midi1.read()
      // regularly from loop() for midi1 to actually read incoming
      // data and run the handler functions as messages arrive.
      myusb.Task();
      midi1.read();
      
    }
    
    
    void myNoteOn(byte channel, byte note, byte velocity) {
      // When a USB device with multiple virtual cables is used,
      // midi1.getCable() can be used to read which of the virtual
      // MIDI cables received this message.
      Serial.print("Note On, ch=");
      Serial.print(channel, DEC);
      Serial.print(", note=");
      Serial.print(note, DEC);
      Serial.print(", velocity=");
      Serial.println(velocity, DEC);
    
      //addition from the MIDIUSB library
      noteOn(channel, note, velocity);
      MidiUSB.flush();
    }
    
    void myNoteOff(byte channel, byte note, byte velocity) {
      Serial.print("Note Off, ch=");
      Serial.print(channel, DEC);
      Serial.print(", note=");
      Serial.print(note, DEC);
      Serial.print(", velocity=");
      Serial.println(velocity, DEC);
    
      //addition from the MIDIUSB library
      noteOff(channel, note, velocity);
      MidiUSB.flush();
    }
    
    void myAfterTouchPoly(byte channel, byte note, byte velocity) {
      Serial.print("AfterTouch Change, ch=");
      Serial.print(channel, DEC);
      Serial.print(", note=");
      Serial.print(note, DEC);
      Serial.print(", velocity=");
      Serial.println(velocity, DEC);
    }
    
    void myControlChange(byte channel, byte control, byte value) {
      Serial.print("Control Change, ch=");
      Serial.print(channel, DEC);
      Serial.print(", control=");
      Serial.print(control, DEC);
      Serial.print(", value=");
      Serial.println(value, DEC);
    }
    
    void myProgramChange(byte channel, byte program) {
      Serial.print("Program Change, ch=");
      Serial.print(channel, DEC);
      Serial.print(", program=");
      Serial.println(program, DEC);
    }
    
    void myAfterTouchChannel(byte channel, byte pressure) {
      Serial.print("After Touch, ch=");
      Serial.print(channel, DEC);
      Serial.print(", pressure=");
      Serial.println(pressure, DEC);
    }
    
    void myPitchChange(byte channel, int pitch) {
      Serial.print("Pitch Change, ch=");
      Serial.print(channel, DEC);
      Serial.print(", pitch=");
      Serial.println(pitch, DEC);
    }
    
    
    // This 3-input System Exclusive function is more complex, but allows you to
    // process very large messages which do not fully fit within the midi1's
    // internal buffer.  Large messages are given to you in chunks, with the
    // 3rd parameter to tell you which is the last chunk.  This function is
    // a Teensy extension, not available in the Arduino MIDI library.
    //
    void mySystemExclusiveChunk(const byte *data, uint16_t length, bool last) {
      Serial.print("SysEx Message: ");
      printBytes(data, length);
      if (last) {
        Serial.println(" (end)");
      } else {
        Serial.println(" (to be continued)");
      }
    }
    
    // This simpler 2-input System Exclusive function can only receive messages
    // up to the size of the internal buffer.  Larger messages are truncated, with
    // no way to receive the data which did not fit in the buffer.  If both types
    // of SysEx functions are set, the 3-input version will be called by midi1.
    //
    void mySystemExclusive(byte *data, unsigned int length) {
      Serial.print("SysEx Message: ");
      printBytes(data, length);
      Serial.println();
    }
    
    void myTimeCodeQuarterFrame(byte data) {
      static char SMPTE[8]={'0','0','0','0','0','0','0','0'};
      static byte fps=0;
      byte index = data >> 4;
      byte number = data & 15;
      if (index == 7) {
        fps = (number >> 1) & 3;
        number = number & 1;
      }
      if (index < 8 || number < 10) {
        SMPTE[index] = number + '0';
        Serial.print("TimeCode: ");  // perhaps only print when index == 7
        Serial.print(SMPTE[7]);
        Serial.print(SMPTE[6]);
        Serial.print(':');
        Serial.print(SMPTE[5]);
        Serial.print(SMPTE[4]);
        Serial.print(':');
        Serial.print(SMPTE[3]);
        Serial.print(SMPTE[2]);
        Serial.print('.');
        Serial.print(SMPTE[1]);  // perhaps add 2 to compensate for MIDI latency?
        Serial.print(SMPTE[0]);
        switch (fps) {
          case 0: Serial.println(" 24 fps"); break;
          case 1: Serial.println(" 25 fps"); break;
          case 2: Serial.println(" 29.97 fps"); break;
          case 3: Serial.println(" 30 fps"); break;
        }
      } else {
        Serial.print("TimeCode: invalid data = ");
        Serial.println(data, HEX);
      }
    }
    
    void mySongPosition(uint16_t beats) {
      Serial.print("Song Position, beat=");
      Serial.println(beats);
    }
    
    void mySongSelect(byte songNumber) {
      Serial.print("Song Select, song=");
      Serial.println(songNumber, DEC);
    }
    
    void myTuneRequest() {
      Serial.println("Tune Request");
    }
    
    void myClock() {
      Serial.println("Clock");
    }
    
    void myStart() {
      Serial.println("Start");
    }
    
    void myContinue() {
      Serial.println("Continue");
    }
    
    void myStop() {
      Serial.println("Stop");
    }
    
    void myActiveSensing() {
      Serial.println("Actvice Sensing");
    }
    
    void mySystemReset() {
      Serial.println("System Reset");
    }
    
    void myRealTimeSystem(uint8_t realtimebyte) {
      Serial.print("Real Time Message, code=");
      Serial.println(realtimebyte, HEX);
    }
    
    
    
    void printBytes(const byte *data, unsigned int size) {
      while (size > 0) {
        byte b = *data++;
        if (b < 16) Serial.print('0');
        Serial.print(b, HEX);
        if (size > 1) Serial.print(' ');
        size = size - 1;
      }
    }
    
    //additions from the MIDIUSB library
    void noteOn(byte channel, byte pitch, byte velocity) {
      midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
      MidiUSB.sendMIDI(noteOn);
    }
    void noteOff(byte channel, byte pitch, byte velocity) {
      midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
      MidiUSB.sendMIDI(noteOff);
    }

  2. #2
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    8,217
    You might look at the usbhost_t36 library which does USB Hosting on the second USB pins. There is a MIDI host as part of it...

  3. #3
    Junior Member
    Join Date
    Oct 2020
    Posts
    3
    Quote Originally Posted by KurtE View Post
    You might look at the usbhost_t36 library which does USB Hosting on the second USB pins. There is a MIDI host as part of it...
    As I also wrote in the post, this is the library I am currently using to host a MIDI device on the teensy. My problem is outputting MIDI through the main USB at the same time.

  4. #4
    I used to have problems trying to use the MIDIUSB library and ended up not using it anymore. Now all I do is select MIDI as the usb type in the programming options pulldown before flashing the teensy and I use

    Code:
    usbMIDI.send(type, data1, data2, channel, 0);
    to send data to the non-host usb and

    Code:
    usbMIDI.read()
    as my read. Works well. I'm also using DIN serial connections, and the only libraries I'm running in the sketch are

    Code:
    #include <Arduino.h>
    #include <MIDI.h>
    #include <USBHost_t36.h>
    If I forget to select MIDI as the USB type before programming, the compiler chokes that usbMIDI isn't recognized.

  5. #5
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    23,485
    On Teensy, MIDIUSB.h is just a thin compatibility wrapper which turns Arduino's rather awkward MIDI API into Teensy's usbMIDI (which uses the same API and the MIDI library).

    But we don't have any MIDIUSB_Defs.h file, only MIDIUSB.h. Unless you have a MIDIUSB_Defs.h as part of your program, including that is probably causing Arduino to try to compile a bunch of non-Teensy stuff into your program. I tried compiling your code, but clearly you have some other files like frequencyToNote.h which you didn't post here, so I can't successfully compile your program to see what the Arduino IDE is actually doing.

    So the best I can do is explain about MIDIUSB.h and suggest you either delete the include for MIDIUSB_Defs.h, or find the file and copy whatever you need from it.

    Before you get your program running properly, maybe also consider saving a copy as a test case so I can use to improve our MIDIUSB.h compatibility for others in the future. I can only do that if you give me a complete set of files I can actually compile in Arduino. It would also help immensely if you could trim away as much non-essential code as possible and clearly document which incoming MIDI message doesn't get properly relayed. I can almost always fix these sorts of compatibility issues when I'm able to reproduce the problem, but code and the exact hardware to use and specific steps to cause it to actually reproduce the problem are usually elusive. I know you probably just want to get your project working and move on, but please do consider saving a copy of this code and trimming it down to a test case, so this problem can be investigated and fixed.

  6. #6
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    23,485
    Oh, I see Arduino's MIDIUSB library has more .h files now....

    https://github.com/arduino-libraries...ree/master/src

    Still trying to decide how I feel about adding these to Teensy's core library.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •