Teensy 4.1 MIDIUsb library

I’m programming a MIDI controller using Teensy 4.1 and the MIDIUsb library, which only supports Atmega32U4.

I can compile the application with no problem on the Arduino IDE but using Platformio in MSCode, the following error is thrown:

#error MIDIUSB can only be used with an USB MCU

I’m not sure what to do to correct this. As it is now, I use a pre-compiler directive to ignore all calls to the library so that I can use the MSCode IDE for development. But in order to test the app, I have to compile and upload using the Arduino IDE. It works, but it is an extra step I could do without.

Any suggestions?

Huh, so this can’t impossibly work?

Can you show your whole platformio.ini and needed example code?

Here is the platformio.ini:

[env:teensy41]
platform = teensy
board = teensy41
framework = arduino
lib_extra_dirs = ~/Arduino/libraries

build_flags = -DUSB_MIDI

There really is no offending code, since it won’t compile, however, here is the include for the MIDIUsb library:

//#ifndef Debug_Only
#include <MIDIUSB.h>
//#endif

As a side note, the precompiler processes the include statement regardless of the existence of the Debug_Only definition. I have to comment out the #include statement.

How did you successfully compile the MIDIUSB library for the Teensy 4.1 target in the Arduino IDE? The library has only cases for AVR, SAM and SAMD, the Teensy’s NXP iMX.RT CPU isn’t supported. It should throw you a “unsupported architecture” compile-time error. Also the lib itself would need the USBCON macro to be enabled.

The Teensy already has its own USB MIDI code in the core which you should use. You’ve already enabled it with -DUSB_MIDI as per docs, now you just need to use the provided global usbMIDI object with it from usb_api.h

Aaah okay I get what’s going on here.

There is a cores/teensy4/MIDIUSB.h at master · PaulStoffregen/cores · GitHub compatibility layer based on the actual usb_midi.h but since you write in your platformio.ini:

It might pick up the wrong library (for AVR/SAM/SAMD) from your Arduino folder.

Just remove that line and rely on explicit, proper library declarations as documented in Redirecting..., and it should work. Since the USBMIDI library is built-in in the Teensy core, no lib_deps declaration is needed, but for the AVR version it should be there.

Thanks for responding to this.

My problem is that I’m not really a C/C++ programmer. I have picked up enough to do what I want to do with my project, but I haven’t got deep knowledge.

I don’t really understand your answer. If I remove the lib_extra_dirs, it doesn’t find the sdio library. I’ve looked at the documentation you linked to, and I could probably spend hours trying to understand it and still not get it right.

It looks like Platformio is not for casual users. I enjoy using the MSCode IDE for code completion, etc. But the time I would save using it is being used by trying to understand the dependency model of platformio. I’m not sure it’s worth it in the long run.

Then either copy the SDIO library folder that it’s using into the lib/ folder (aka lib/SDIO/*files*) of the project or search up the library in PlatformIO Registry and use lib_deps accordingly. If there are things which are unclear in the docs let me know, especially the LDF start page is important.

OK, I’ve managed to resolve all dependency issues by copying all the includes into the project lib directory except for MIDIUSB.h which, as you say, is apparently part of the platformio core.

Where is it documented which libraries are part of the core?

Also, being new to this platformI find the LDF start page confusing. Perhaps more use cases and examples would be helpful.

And as a suggestion, possibly the import Arduino project wizard could be a little more helpful. For instance, if there is only one source file in the Arduino project, it could rename it main.cpp. It could also optionally copy the libraries referenced by the Arduino project into the project lib folder.

Anyway, thanks for you help. I’m sure I’ll have more questions later.

1 Like