PlatfotmIO cannot find TinyUSB

Hello!
I’m stuck with this problem for a third day in a row and I’m literally banging my head on the table. Here’s the thing:
I bought Seeeduino Xiao with the intent of using it as USB device. I’ve added it to PlatformIO and simple sketches are compiling without any issue. When I try to add TinyUSB library it gives all sort of errors. Mainly that Adafruit_USBD_CDC.h is not found. Also in the VScode the c_cpp_properties.json gives an error that "Cannot find: “home/…/framework-arduino-samd-seeed/cores/arduino/TinyUSB” and bunch more folders ending with TinyUSB cannot be found. I’ve check this path and indeed this folder is missing.

Here’s roughly what I’ve tried:

  • adding .zip library - gives missing header files,
  • copying library to /cores/arduino/TinyUSB - this was the closest I’ve ever been as IntelliJ was finding everything and didn’t gave any errors, but it couldn’t compile,
  • reinstalling PlatformIO - no change,
  • trying older versions of PlatformIO,
  • tried on another system - I’m using OpenSUSE everyday, tried on Debian, didn’t try on Windows,
  • adding lib_deps with older versions,
  • and bunch more that I don’t remember.

I’ve read online that TinyUSB is supported out of the box for Xiao, so I don’t know what is going on. _DUSE_TINYUSB is set, lib_archive=no doesn’t make a difference. What’s interesting it works with ArduinoIDE (don’t say I should just use it - I refuse to do so) just by adding .zip library. I also tried to importing ArduinoIDE project to PlatformIO and checking “use arduino libraries” or smth like that. I don’t know if I should add some logs so I’ll leave it like that.

If you need any info just ask. Any help appreciated. Thanks

Please post your exact platformio.ini and the src/main.cpp code that tries to do something with TinyUSB.

Here’s the platformio.ini

[env:seeed_xiao]
lib_archive = no
platform = atmelsam
board = seeed_xiao
framework = arduino
lib_deps=
    adafruit/Adafruit TinyUSB Library @ ^2.2.7
build_flags=
    -DUSE_TINYUSB

And here’s xiao.cpp. It’s literally empty

#include <Arduino.h>
#include <Adafruit_TinyUSB.h>

void setup(){

}

void loop(){
    
}

With this config it gives error in Arduino.h

.platformio/packages/framework-arduino-samd-seeed/cores/arduino/Arduino.h:173:10: fatal error: Adafruit_USBD_CDC.h: No such file or directory

In this line:

#ifdef USE_TINYUSB
#include "Adafruit_USBD_CDC.h"
#else

And in vscode in problems tab there is:

Cannot find "/home/suse/.platformio/packages/framework-arduino-samd-seeed/cores/arduino/TinyUSB".
Cannot find "/home/suse/.platformio/packages/framework-arduino-samd-seeed/cores/arduino/TinyUSB/Adafruit_TinyUSB_ArduinoCore".
Cannot find "/home/suse/.platformio/packages/framework-arduino-samd-seeed/cores/arduino/TinyUSB/Adafruit_TinyUSB_ArduinoCore/tinyusb/src".
Cannot find "/home/suse/.platformio/packages/framework-arduino-samd-seeed/cores/arduino/TinyUSB".
Cannot find "/home/suse/.platformio/packages/framework-arduino-samd-seeed/cores/arduino/TinyUSB/Adafruit_TinyUSB_ArduinoCore".
Cannot find "/home/suse/.platformio/packages/framework-arduino-samd-seeed/cores/arduino/TinyUSB/Adafruit_TinyUSB_ArduinoCore/tinyusb/src".

I think that for some reason or another the TinyUSB library is not entirely included into path.

Hm, there is some sort of mismatch going on, the framework-arduino-samd-seed package, as derived from the official repo, has no cores/arduino/TinyUSB folder. So it should not be found there.

However, in the libraries/ folder, it does have Adafruit_TinyUSB_Arduino/Adafruit_TinyUSB.h built in. (So no lib_deps needed). Let me take a look…

Yup, in libraries/ there is Adafruit_TinyUSB_Arduino although Adafruit_TinyUSB.h is in Adafruit_TinyUSB_Arduino/src/Adafruit_TinyUSB.h. I tried now without lib_deps and it says the same fatal error: Adafruit_USBD_CDC.h: No such file or directory.

This is really broken. I can get around the include path issue with

[env:seeed_xiao]
platform = atmelsam
board = seeed_xiao
framework = arduino
build_flags = 
  -DUSE_TINYUSB
  -I"C:\Users\Max\.platformio\packages\framework-arduino-samd-adafruit\libraries\Adafruit_TinyUSB_Arduino\src\arduino"
lib_ignore = USBHost
lib_ldf_mode = deep+

but now it wants an implementation of Adafruit_TinyUSB_Core_init() which has been long removed from the core. This was present in older SAMD cores, e.g. here. And hacking it back in by copy pasting also doesn’t feel like a valid solution.

Let me countercheck that how it behaves in the Arduino IDE with tools->USB stack selected as TinyUSB.

The code for TinyUSB was definitely corrected in 1.8.5, but PlatformIO uses 1.8.4. Here’s how I made it work

[env:seeed_xiao]
platform = atmelsam
board = seeed_xiao
framework = arduino
; Need 1.8.5 version
; Download https://github.com/Seeed-Studio/ArduinoCore-samd/releases/download/v1.8.5/ArduinoCore-samd-1.8.5.tar.bz2 and extract it somewhere
platform_packages = 
  framework-arduino-samd-seeed@symlink://C:\Users\Max\Downloads\ArduinoCore-samd-1.8.5\
; activate tinyusb, Fix include paths. Adapt path as needed 
build_flags = 
  -DUSE_TINYUSB
  -I"C:/Users/Max/Downloads/ArduinoCore-samd-1.8.5/libraries/Adafruit_TinyUSB_Arduino/src/arduino"
; ignore wrong libraries
lib_ignore = USBHost
lib_ldf_mode = deep+
; fix issues with interrupt handler linkage
lib_archive = no

Then

#include <Arduino.h>
#include <Adafruit_TinyUSB.h>

void setup() {
    Serial.begin(115200);
}

void loop() {

}

compiles normally and uses TinyUSB.

This is not optimal and needs a package update and builder scripts update. You should report this to https://github.com/platformio/platform-atmelsam/issues if not already done so by someone else.

Edit: Seems to be tracked in https://github.com/platformio/platform-atmelsam/issues/220 now.

2 Likes

I cannot believe this. It works! You’re my savior, thank you!

You should report this to Issues · platformio/platform-atmelsam · GitHub if not already done so by someone else

Definitely will, thank you again!

1 Like

I noticed that the issue hasn’t been updated but the behaviour has moved forward (for the better). Updating this to observe the following:

  1. PlatformIO is now using 1.8.5 , so no longer need to download 1.8.5

  2. Still need to lib_ignore=USBHost, and still need to add -I path for Adafruit_USBD_CDC.h

  3. Better way to add the path (rather than hardcoding your C:/ etc) is:
    -I "${PROJECT_CORE_DIR}/packages/framework-arduino-samd-seeed/libraries/Adafruit_TinyUSB_Arduino/src/arduino"

in other words, as of this writing, all that’s required is now just:

[env:your_env_here]
platform = atmelsam
board = seeed_xiao
framework = arduino
lib_ignore = USBHost
build_flags =
-DUSE_TINYUSB
-I "${PROJECT_CORE_DIR}/packages/framework-arduino-samd-seeed/libraries/Adafruit_TinyUSB_Arduino/src/arduino"