Using TinyUSB to access onboard storage and log to serial at the same time

Long story short, I am want to be able to both access my contraption’s onboard SD card via the USB connection to the computer as well as use that same USB connection for logging.

I have had a look at the msc_sd.ino example in the Adafruit_TinyUSB library, and this compiles and works perfectly with the Arduino IDE. Trying to take this code into PlatformIO, however, has induced several days of headaches (4 so far), and I am no longer sure what is a PIO bug and what I am doing right or wrong. I understand that I need to use TinyUSB for this task, but that’s about it.

So, I’m just asking for guidance. I’ve been so bogged down with bugs and issues and circular library dependencies that I am no longer sure what I need to be doing or installing or uninstalling to reach my goal. Can anyone point me in the right direction?

So the msc_sd.ino compiles fine in the Arduino IDE, but you can’t get that exact piece of code to compile and work in PlatformIO?

What exactly is your platformio.ini?

[env:adafruit_feather_m4_can]
platform = atmelsam
board = adafruit_feather_m4_can
framework = arduino
lib_archive = false
lib_ldf_mode = deep+
lib_deps =
  SPI
  adafruit/Adafruit TinyUSB Library@3.3.1
  adafruit/Adafruit Zero DMA Library@^1.1.3
lib_ignore =
  USBHost ; needed per [https://community.platformio.org/t/moteinom0-adafruit-tinyusb-build-errors/36891](https://community.platformio.org/t/moteinom0-adafruit-tinyusb-build-errors/36891) to get Adafruit TinyUSB working
build_flags =
  -D USE_TINYUSB

a lot of those additions are to solve issues detailed in:

… and a few others, but i can only put 2 links in a single post

All these people only have these problems because they just had to get the very latest version of Adafruit TinyUSB. The Adafruit SAMD Arduino core has a builtin, matched version of Adafruit TInyUSB already, as is evident by looking into https://github.com/adafruit/ArduinoCore-samd/tree/1.7.16/libraries. Thus, the correct way is to absolutely not include TinyUSB in the lib_deps expression. PlatformIO will comfortably find it in the builtin libraries of the Arduino core.

The msc_sd.ino files compiles with the most simplistic platformio.ini just fine. Take a look at

Thank you for the reply. I took some time to tear out everything I had attempted and was still having the same issue. I did a little more digging and found your response to another thread (Choose USB stack as tiny Usb - #4 by maxgerhardt) and that helped quite a bit. I had set lib_archive to false when it should have been set to no. Thank you again!

Hello icannotfly,

You said you were able to compile the msc_sd example in arduino. This one, right?
File->Examples->Adafruit TinyUSB Library->Mass Storage->msc_sd
Can you tell me what board are you using?

I ask you this because I am trying to do the same using ESP32-S2 Dev Module (my board is EPS32-S2-MINI-1) and I get a lot of errors but I do not understand why:

C:\Users\Fabio Oliveira\AppData\Local\Temp\.arduinoIDE-unsaved2025316-5424-1ps5nnd.kjmc\msc_sd\msc_sd.ino:23:1: error: 'Sd2Card' does not name a type
   23 | Sd2Card card;
      | ^~~~~~~
C:\Users\Fabio Oliveira\AppData\Local\Temp\.arduinoIDE-unsaved2025316-5424-1ps5nnd.kjmc\msc_sd\msc_sd.ino:24:1: error: 'SdVolume' does not name a type
   24 | SdVolume volume;
      | ^~~~~~~~
C:\Users\Fabio Oliveira\AppData\Local\Temp\.arduinoIDE-unsaved2025316-5424-1ps5nnd.kjmc\msc_sd\msc_sd.ino: In function 'void setup()':
C:\Users\Fabio Oliveira\AppData\Local\Temp\.arduinoIDE-unsaved2025316-5424-1ps5nnd.kjmc\msc_sd\msc_sd.ino:53:9: error: 'card' was not declared in this scope
   53 |   if ( !card.init(SPI_HALF_SPEED, chipSelect) ) {
      |         ^~~~
C:\Users\Fabio Oliveira\AppData\Local\Temp\.arduinoIDE-unsaved2025316-5424-1ps5nnd.kjmc\msc_sd\msc_sd.ino:53:19: error: 'SPI_HALF_SPEED' was not declared in this scope
   53 |   if ( !card.init(SPI_HALF_SPEED, chipSelect) ) {
      |                   ^~~~~~~~~~~~~~
C:\Users\Fabio Oliveira\AppData\Local\Temp\.arduinoIDE-unsaved2025316-5424-1ps5nnd.kjmc\msc_sd\msc_sd.ino:62:8: error: 'volume' was not declared in this scope
   62 |   if (!volume.init(card)) {
      |        ^~~~~~
C:\Users\Fabio Oliveira\AppData\Local\Temp\.arduinoIDE-unsaved2025316-5424-1ps5nnd.kjmc\msc_sd\msc_sd.ino:62:20: error: 'card' was not declared in this scope
   62 |   if (!volume.init(card)) {
      |                    ^~~~
C:\Users\Fabio Oliveira\AppData\Local\Temp\.arduinoIDE-unsaved2025316-5424-1ps5nnd.kjmc\msc_sd\msc_sd.ino:67:26: error: 'volume' was not declared in this scope
   67 |   uint32_t block_count = volume.blocksPerCluster()*volume.clusterCount();
      |                          ^~~~~~
C:\Users\Fabio Oliveira\AppData\Local\Temp\.arduinoIDE-unsaved2025316-5424-1ps5nnd.kjmc\msc_sd\msc_sd.ino: In function 'int32_t msc_read_cb(uint32_t, void*, uint32_t)':
C:\Users\Fabio Oliveira\AppData\Local\Temp\.arduinoIDE-unsaved2025316-5424-1ps5nnd.kjmc\msc_sd\msc_sd.ino:88:10: error: 'card' was not declared in this scope
   88 |   return card.readBlock(lba, (uint8_t*) buffer) ? 512 : -1;
      |          ^~~~
C:\Users\Fabio Oliveira\AppData\Local\Temp\.arduinoIDE-unsaved2025316-5424-1ps5nnd.kjmc\msc_sd\msc_sd.ino: In function 'int32_t msc_write_cb(uint32_t, uint8_t*, uint32_t)':
C:\Users\Fabio Oliveira\AppData\Local\Temp\.arduinoIDE-unsaved2025316-5424-1ps5nnd.kjmc\msc_sd\msc_sd.ino:96:10: error: 'card' was not declared in this scope
   96 |   return card.writeBlock(lba, buffer) ? 512 : -1;
      |          ^~~~
Multiple libraries were found for "SD.h"
  Used: C:\Users\Fabio Oliveira\AppData\Local\Arduino15\packages\esp32\hardware\esp32\3.2.0\libraries\SD
  Not used: C:\Users\Fabio Oliveira\AppData\Local\Arduino15\libraries\SD
exit status 1

Compilation error: 'Sd2Card' does not name a type

Thank you!

I am using the Adafruit M4 CAN Express board.

Those errors look like you are missing a library somewhere. Double-check that you have all your #include’d libraries actually installed locally.

1 Like

You cannot use this this sketch for an ESP32S2. It is also explicitly ignored in the compilation checks. The sketch is designed to work with the arduino-libraries/SD version of the “SD” library, not the Espressif ESP32 version of the “SD” library.

However, there is also msc_sdfat.ino sketch in that same examples folder, which requires you to install the SdFat - Adafruit Fork library, but this one should compile for an ESP32S2.

Hello maxgerhardt,

Thank you for the feedback. I was not aware of that .skip.text file. I always thought if a specific board was not supported, some sort of message would appear. I am really thank you for pointed that out.

The example you mentioned, msc_sdfat.ino, I installed the library, compiled it, and it returned some errors:

C:/Users/Fabio Oliveira/AppData/Local/Arduino15/packages/esp32/tools/esp-x32/2411/bin/../lib/gcc/xtensa-esp-elf/14.2.0/../../../../xtensa-esp-elf/bin/ld.exe: C:\Users\Fabio Oliveira\AppData\Local\arduino\sketches\ECB43BBC7B145E489A3CA86A8CA5C611\libraries\Adafruit_TinyUSB_Library\portable\analog\max3421\hcd_max3421.c.o:(.literal.hcd_edpt_xfer+0x4): undefined reference to `__atomic_test_and_set'
C:/Users/Fabio Oliveira/AppData/Local/Arduino15/packages/esp32/tools/esp-x32/2411/bin/../lib/gcc/xtensa-esp-elf/14.2.0/../../../../xtensa-esp-elf/bin/ld.exe: C:\Users\Fabio Oliveira\AppData\Local\arduino\sketches\ECB43BBC7B145E489A3CA86A8CA5C611\libraries\Adafruit_TinyUSB_Library\portable\analog\max3421\hcd_max3421.c.o: in function `usb_xfer_test_and_set':
c:\Users\Fabio Oliveira\Documents\Arduino\libraries\Adafruit_TinyUSB_Library\src\portable\analog\max3421/hcd_max3421.c:272:(.text.hcd_edpt_xfer+0x72): undefined reference to `__atomic_test_and_set'
C:/Users/Fabio Oliveira/AppData/Local/Arduino15/packages/esp32/tools/esp-x32/2411/bin/../lib/gcc/xtensa-esp-elf/14.2.0/../../../../xtensa-esp-elf/bin/ld.exe: c:\Users\Fabio Oliveira\Documents\Arduino\libraries\Adafruit_TinyUSB_Library\src\portable\analog\max3421/hcd_max3421.c:272:(.text.hcd_setup_send+0x4c): undefined reference to `__atomic_test_and_set'
collect2.exe: error: ld returned 1 exit status

exit status 1

Compilation error: exit status 1

I guess I will give up of this. To much sand for my truck. :sweat_smile:

This is a known issue. Just downgrade your ESP32 Boards version in the Arduino IDE to one below 3.2.0, which would be 3.1.3.

I am so happy! :sparkler: :grinning:

Thank you maxgerhardt. :pray:

I was able to compile and to program the example:

File->Examples->Adafruit TinyUSB Library->Mass Storage->msc_sdfat.ino

Using:
ESP32 boards version 3.1.3
Adafruit TinyUSB Library version 3.4.4
SdFat Adafruit Fork library version 2.2.54
Board: ESP32S2 Dev Module (mine is EPS32-S2-MINI-1 module)
Arduino IDE Version 2.3.4

Was this thread hit by a meteor, fusing two different threads, or is someone running Xtensa code on a SAMD processor? (uuuh, no) Is there a segue missing on how an M4 CAN (Microchip, right?) was pollenated with the ESP-S3 code successfully?

And just so I’m up on the parlance, it’s weird that the Adafruit TinyUSB is incompatible with the Adafruit TinyUSB, but there’s already a TinyUSB in ESP-IDF, so I don’t understand how introducing yet two more copies in (or even one more copy) advances things. Then the pathnames look like ancient Arduino1.5 tool pathnames and not PlatformIO pathnames at all. I hae read this thread over and over and can’t make it make sense, but the post ends in happiness, so maybe I should just let this sleeping dog lie. I’ll give Max a deserved gold star for delivering a successful answer when I can 't even understand the question.