Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Teensy USB Host not receiving messages

Salchicha

New Member
I've attached the USB host cable to the USB Host pins on the Teensy 4.1, my midi controller comes on when plugged into it and I've created this simple sketch to print out the CC number of any messages received, however it's not printing anything. This code has been cobbled together from bits I've found online, not sure if its correct or not but it compiles ok....

C++:
#include <USBHost_t36.h>
#include <MIDI.h>

USBHost myusb;

  void myNoteOn(byte channel, byte note, byte velocity) {}
  void myNoteOff(byte channel, byte note, byte velocity) {}
  void myAfterTouchPoly(byte channel, byte note, byte velocity) {}
  void myControlChange(byte channel, byte control, byte value) {
    Serial.println("CC Received");
    byte type = usbMIDI.getType();
    if (type == midi::ControlChange) {
      byte channel = usbMIDI.getChannel();
      byte ccNumber = usbMIDI.getData1();
      Serial.print("Received CC number: ");
      Serial.println(ccNumber);
    }
  }
  void myProgramChange(byte channel, byte program) {}
  void myAfterTouch(byte channel, byte pressure) {}
  void myPitchChange(byte channel, int pitch) {}
  void mySystemExclusiveChunk(const byte *data, uint16_t length, bool last) {}
  void mySystemExclusive(byte *data, unsigned int length) {}
  void myTimeCodeQuarterFrame(byte data) {}
  void mySongPosition(uint16_t beats) {}
  void mySongSelect(byte songNumber) {}
  void myTuneRequest() {}
  void myClock() {}
  void myStart() {}
  void myContinue() {}
  void myStop() {}
  void myActiveSensing() {}
  void mySystemReset() {}
  void myRealTimeSystem(byte realtimebyte) {}


void setup() {
 
  Serial.begin(9600);

  usbMIDI.setHandleNoteOff(myNoteOff);
  usbMIDI.setHandleNoteOn(myNoteOn);
  usbMIDI.setHandleAfterTouchPoly(myAfterTouchPoly);
  usbMIDI.setHandleControlChange(myControlChange);
  usbMIDI.setHandleProgramChange(myProgramChange);
  usbMIDI.setHandleAfterTouch(myAfterTouch);
  usbMIDI.setHandlePitchChange(myPitchChange);
  usbMIDI.setHandleSystemExclusive(mySystemExclusiveChunk);
  usbMIDI.setHandleTimeCodeQuarterFrame(myTimeCodeQuarterFrame);
  usbMIDI.setHandleSongPosition(mySongPosition);
  usbMIDI.setHandleSongSelect(mySongSelect);
  usbMIDI.setHandleTuneRequest(myTuneRequest);
  usbMIDI.setHandleClock(myClock);
  usbMIDI.setHandleStart(myStart);
  usbMIDI.setHandleContinue(myContinue);
  usbMIDI.setHandleStop(myStop);
  usbMIDI.setHandleActiveSensing(myActiveSensing);
  usbMIDI.setHandleSystemReset(mySystemReset);
  usbMIDI.setHandleRealTimeSystem(myRealTimeSystem);

  myusb.begin();
}

void loop() {
  myusb.Task();
  usbMIDI.read();
  //Serial.println("Reading MIDI...");

  delay(10);

}
 
Last edited:
You are missing a
Serial.begin(xxxxx);
Command where xxxxx is the baud rate for the connection. It must be set to the same baud rate in the Serial monitor page.
 
I've added it, it hasn't worked though. I enabled debugging output in USBHost_t36 and get the following:

Code:
USB2 PLL running
 reset waited 6
USBHS_ASYNCLISTADDR = 0
USBHS_PERIODICLISTBASE = 20002000
periodictable = 20002000
Setup complete.
port change: 10001803
    connect
  begin reset
port change: 10001005
  port enabled
  end recovery
new_Device: 12 Mbit/sec
new_Pipe
enumeration:
enumeration:
  error allocating data transfer
 
Which baud rate did you use? And which baud rate did you set on your Serial Monifot? Are you using the Arduino IDE or another IDE. Or, is this running as a pure USB Host ? In that case, where (what device) are you printing to?
 
I've set it to 9600 - Serial.begin(9600);

There doesn't appear to be any options to set the rate of the serial monitor, I'm using Arduino IDE. It is printing stuff to the serial monitor, it prints the "Reading MIDI...." message in the loop().
 

Latest threads

New Articles From Microcontroller Tips

Back
Top