Problems compiling Teensy for different USB endpoint type (with solution)

The Teensy can be configured as a number of different types of USB device. I am particularly interested in running it as USB Audio, to use with the Audio Library. I had a lot of trouble getting this working, and I wanted to share this solution with other users, and ask for the docs updated.

As described in the Teensy platform docs, you need to define the appropriate option for the USB endpoint type you wish the teensy to appear as. This is equivalent to choosing one of the menu options in the Arduino IDE (as is mentioned in many of the tutorials). The option -DUSB_AUDIO is needed for audio applications, and is not currently included in this list.

However, all of these options clash with the preconfigured option -DUSB_SERIAL, which is included in the Makefile. As can be seen in usb_desc.h, the different options are defined with #elif, therefore only one can be defined at a time, and the serial option needs to be undefined.

So in the case of USB audio, the solution is to add the following line to platformio.ini

build_flags = -DUSB_AUDIO -UUSB_SERIAL

Thanks a lot for the report!

You don’t need to do -UUSB_SERIAL. Could you try build_flags = -DUSB_AUDIO?

I initially tried only build_flags = -DUSB_AUDIO, and I spent a frustratingly long time wondering why it didn’t work. If you look at the usb_desc.h file, you will see that if USB_SERIAL is defined, the preprocessor will never check if USB_AUDIO is defined, because of the cascading series of #elifs.

Furthermore, unbuild_flags = -DUSB_SERIAL didn’t help either, because it is declared within the Makefile, rather than by the platformio system.

This our bug and I’ve just fixed it in v1.2.1:
https://github.com/platformio/platform-teensy/commit/c907e91e9bbee90e2fae39310f436c62f54dd28e

Please run pio update and use only build_flags = -DUSB_AUDIO.

P.S: I’ll update docs. Thanks.

Thanks, I confirmed that your fix works.

1 Like

Hi,
Reopening this old thread because I’m running in the same issue trying to use -DUSB_AUDIO:
My project does not compile because the build flag USB_AUDIO does not define AUDIO_INTERFACE in usb_audio.h of the Teensy audio library, for some reason. Here is the ini that fails, resulting in a failure to recognize the type AudioOutputUSB:

[env:teensy40]
framework = arduino
platform = teensy
upload_protocol = teensy-cli
board = teensy40
build_flags = -DUSB_AUDIO
lib_deps = paulstoffregen/Audio @ ^1.3

Note that my code works when compiled/uploaded from the Arduino IDE.
Thanks for your help!

Actually completely removing the lib_deps does the trick, the library from the repo is probably not the same as the one included by default with the teensy framework.