Skip to content

TESTING

pschatzmann edited this page Nov 24, 2026 · 6 revisions

Host mode and device mode are both tested automatically, in two ways: desktop unit tests (CMake/ctest, also run by .github/workflows/tests.yml) and firmware compile tests (test_compile.sh). Test names say which side they cover: host_* / USBHostMSC_* and device / USBDeviceMSC_*.

Desktop unit tests (Linux/macOS, CMake)

Host mode (host_*)

The hardware-independent part of the host side -- the FAT / ext4 block drivers USBHostMscFatIO / USBHostMscExt4IO with TinyFATFS / TinyEXT4 on top (partition lookup is done by those libraries) -- is built against a stub of USBHostMSC that reads and writes a disk-image file (tests/stub/USBHostMSC_host_test.h; the real header is swapped out by -DUSBHOSTMSC_HOST_TEST). The images are created with the real mkfs tools, and fsck is run on them afterwards.

cmake -B build
cmake --build build -j
ctest --test-dir build --output-on-failure

Needs cmake, a C++17 compiler and these tools on PATH: mkfs.vfat and fsck.vfat (dosfstools), mkfs.exfat and fsck.exfat (exfatprogs), mkfs.ext2/4 and e2fsck (e2fsprogs, >= 1.43), mcopy/mmd (mtools), sfdisk (util-linux) and sgdisk (gdisk). The tests build with AddressSanitizer and UBSan by default (-DUSBHOSTMSC_SANITIZE=OFF to disable).

TinyFATFS and TinyEXT4 are taken from a sibling checkout (../TinyFATFS, ../TinyEXT4) if present, otherwise downloaded; point CMake at others with -DTINYFATFS_DIR=... / -DTINYEXT4_DIR=....

Suite Covers
host_fat FAT32 raw/MBR, FAT16 MBR, exFAT raw/MBR: reading, listing, writing, multi-cluster files, fsck clean, remount; rejects ext4 disks; no drive plugged in
host_ext4 ext4 on raw, MBR and GPT disks; real file sizes in listings; read-only request; ext2 forced read-only; e2fsck clean; rejects non-ext disks
host_lifetime FAT and ext4: drive unplugged and replugged, drive replaced by another one, I/O errors while files are open (use-after-free and flush-on-teardown, under ASan); large transfers are split; block sizes other than 512 rejected

Device mode (device)

USBDeviceMSC.cpp is compiled against a stub of Adafruit_USBD_MSC and TinyUSBDevice (tests/stub/Adafruit_TinyUSB.h), so the block layer runs on the desktop.

Suite Covers
device A TinyFATFS RAM disk exported with USBDeviceMSC, checked as the PC would see it with the real fsck.vfat and mtools; read/write plumbing incl. unaligned access (as the ESP32 core issues); exporting a TinyEXT4 RamIO; rejects drivers without sectors

What the desktop tests cannot cover

TinyUSB enumeration and real transfers (host and device; see Hardware tests for the manual runs), USBHostMSC's blocking/timeout logic, USBMscSdCard on real cards, DMA alignment, the ESP32/RP2040 fs::FS adapters, the STM32 USBMscFile, and board bring-up. Those need hardware (or at least the Arduino cores).

Firmware compile tests

test_compile.sh builds every example with arduino-cli; see the header of that script. Host examples (USBHostMSC_*) build for RP2040 (host stack), ESP32-S3 (USB-OTG mode, core TinyUSB), STM32 and classic ESP32 (needs the Adafruit TinyUSB Library). Device examples (USBDeviceMSC_*) build for RP2040 (usbstack=tinyusb, the device stack) and ESP32-S3. The two modes need different RP2040 USB stacks, hence the separate rp2040 and rp2040dev rows.

Hardware tests

Manual tests on real boards.

Date Board Mode Test Result
2026-11-24 Seengreat RP2350-MINI-A (RP2350A, 4 MB flash, 8 MB PSRAM, PSRAM CS = GPIO19) Device (usbstack=tinyusb) 2 MB RamIO disk in PSRAM, formatted with TinyFATFS, exported with USBDeviceMSC OK -- see below
2026-11-24 ESP32-S3 N16R8 (QFN56 rev v0.2, 16 MB flash, 8 MB octal PSRAM) Device (USB-OTG / TinyUSB) same test OK -- see below
2026-11-24 ESP32-S3 N16R8 (same board) Host (USB-OTG / core TinyUSB) USBHostMSC_Info, USBHostMSC_AutoFS_ListFiles, writing a new file on a 256 MB FAT16 flash drive OK -- see below
2026-11-24 ESP32-S3 N16R8 (same board) Host USBHostMSC_FatFS_ListFiles, USBHostMSC_Hub_ListFiles (drive without hub), USBHostMSC_Ext4ReadFile on a FAT drive, read throughput OK -- see below
2026-11-24 Raspberry Pi Pico (RP2040) Host (USB Stack "Adafruit TinyUSB Host (native)") inquiry/capacity, FAT mount and listing, reading the largest file, writing a new file on the 256 MB FAT16 drive OK -- see below

RP2350 with PSRAM -- board settings: Generic RP2350, USB Stack "Adafruit TinyUSB", PSRAM Size 8 MB, PSRAM CS GPIO 19, Flash Size 4 MB (rp2040:rp2040:generic_rp2350:usbstack=tinyusb,psram=8mb,psramcs=GPIO19,flash=4194304_0).

  • rp2040.getPSRAMSize() reports 8 MB; pmalloc() returns PSRAM addresses (0x11xxxxxx).
  • 256 KB PSRAM write/read pattern test: 0 errors (write ~30 ms, read ~14 ms).
  • RamIO(4096) (2 MB): all sectors allocated in PSRAM (used PSRAM heap 2 MB, internal heap unchanged).
  • The PC enumerates CDC + Mass Storage; the drive shows up as "Arduino PSRAM Disk", 2 MB vfat.
  • HELLO.TXT written by the sketch is readable on the PC.
  • 1.5 MB random file copied to the drive, unmounted, remounted and compared: identical.

ESP32-S3 with PSRAM -- board settings: ESP32S3 Dev Module, USB Mode "USB-OTG (TinyUSB)", USB CDC On Boot disabled (serial output on the UART port), PSRAM "OPI PSRAM", Flash Size 16 MB; the PC is connected to the native USB port (esp32:esp32:esp32s3:USBMode=default,CDCOnBoot=default,PSRAM=opi,FlashSize=16M).

  • ESP.getPsramSize() reports 8 MB; ps_malloc() returns PSRAM addresses (0x3cxxxxxx).
  • 256 KB PSRAM write/read pattern test: 0 errors (write ~7 ms, read ~5 ms).
  • RamIO(4096) (2 MB): sectors allocated in PSRAM (RamIO uses ps_malloc() on ESP32).
  • The PC enumerates Mass Storage; the drive shows up as "Arduino PSRAM Disk", 2 MB vfat.
  • HELLO.TXT written by the sketch is readable on the PC.
  • 1.5 MB random file copied to the drive (~1.8 s), unmounted, remounted and compared: identical.

ESP32-S3 as USB host -- board settings: ESP32S3 Dev Module, USB Mode "USB-OTG (TinyUSB)", USB CDC On Boot disabled; upload and Serial over the UART port, the flash drive in the native USB port through an OTG adapter (esp32:esp32:esp32s3:USBMode=default,CDCOnBoot=default,PSRAM=opi,FlashSize=16M). Drive: "Generic STORAGE DEVICE" rev 1.02, 512000 x 512-byte blocks (256 MB), FAT16 without partition table, holding 4 files (21-65 MB each).

  • The drive needs 5 V on the native port's VBUS: with the board powered only from the UART port it got no power (LED off) and never enumerated -- see SINGLE_PORT.

  • USBHostMSC_Info: inquiry (vendor/product/revision), capacity and the LBA 0 hex dump (FAT16 boot sector) are correct.

  • USBHostMSC_AutoFS_ListFiles: mounted as FAT, all 4 files listed with the correct sizes. Run with a guard that refuses and counts every disk_write(): 0 write attempts, i.e. mounting and listing do not write.

  • Write test: a new 64 KB file created (FA_CREATE_NEW) in 512-byte write() calls, closed, volume unmounted and remounted, read back and compared: identical; the existing files are unchanged. Write ~11 KB/s, read ~56 KB/s with 512-byte calls (the drive is slow on single-sector writes; bigger calls should be faster, not measured).

  • USBHostMSC_FatFS_ListFiles: all files listed with the correct sizes.

  • USBHostMSC_Hub_ListFiles with the drive plugged in directly (no hub): mounted in slot 1 of 4 and listed.

  • USBHostMSC_Ext4ReadFile on the FAT drive: cleanly reports "not an ext4 file system".

  • Read throughput, 27.7 MB file read with TinyFATFS (same data in every run):

    USB_HOST_MSC_MAX_BLOCKS_PER_XFER 512-byte reads 4 KB reads 32 KB reads
    1 (default) 170 KB/s 170 KB/s 170 KB/s (whole file)
    8 170 KB/s 815 KB/s 816 KB/s
    64 170 KB/s 815 KB/s 816 KB/s

    With 8 blocks the drive / USB full speed is already the limit.

  • A reset while the file is open leaves it with size 0 (FAT updates the size only on close()/sync()); call flush() on long writes.

RP2040 (Raspberry Pi Pico) as USB host -- board settings: Raspberry Pi Pico, USB Stack "Adafruit TinyUSB Host (native)" (rp2040:rp2040:rpipico:usbstack=tinyusb_host). The Pico's only USB port is the host port: the drive is plugged into the micro-USB port through an OTG adapter, and 5 V for the Pico and the drive is fed into VBUS (pin 40) and GND. Same drive as in the ESP32-S3 tests.

The test sketch runs on every mount and checks: SCSI inquiry, capacity and LBA 0; mounting with TinyFATFS and listing the root directory; reading up to 4 MB of the largest file; creating a new 64 KB file (FA_CREATE_NEW), unmounting, remounting and comparing it. Result: PASS. The existing files were only read.

  • Drive detected 2 s after boot: "Generic STORAGE DEVICE" rev 1.02, 512000 x 512-byte blocks, 1 LUN; LBA 0 is the FAT boot sector (55aa).
  • All files listed with the correct sizes.
  • Read: 4 MB of a 65 MB file in 4096-byte read() calls: 927 KB/s.
  • Write: new 64 KB file in 4096-byte write() calls: 63 KB/s; verified on the board after remount (0 mismatches) and again on a PC (identical).

Since the board is not on the PC's USB in host mode, the result was reported by the on-board LED and by a log file written to the drive (Serial1 on GP0 also works with a USB-UART adapter).

Notes:

  • With the wrong PSRAM CS pin getPSRAMSize() is 0 and pmalloc() returns nullptr; RamIO then silently falls back to malloc().
  • When setup() takes long (e.g. formatting a large disk) before msc.begin(), the host has already enumerated the device without the MSC interface. USBDeviceMSC::begin() therefore re-attaches the device (TinyUSBDevice.detach()/attach()) if it is already mounted.

Clone this wiki locally