Skip to content

Commit dc2d757

Browse files
authored
6 Mass Storage Support (#35)
* [Feature] USB Mass Storage Device - This allows a USB Mass Storage device to expose the SD card. Useful for taking off track logs. NOTE: I could not get the CDC mode to work once mass storage began, meaning, when you plug into the computer, it won't expose Serial, or ability to flash it. To get around this limitation, I added a new platform (available in PlatformIO down the bottom bar) to enable a build to disable mass storage in non-release builds. * [bug] work with windows Some race condition I don't understand. If this isn't done before Serial is started/used, we won't see it unless behind a USB hub. Weird. * [Feature] Also allow firmware MSC Updates Adds a new drive (separate from SD card) that operators can update new firmware by a simple drag and drop of a "firmware.bin" file into.
1 parent 4a08e73 commit dc2d757

File tree

4 files changed

+80
-6
lines changed

4 files changed

+80
-6
lines changed

platformio.ini

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
[platformio]
2+
src_dir = src/vario
3+
14
# Mostly taken from
25
# https://github.com/sivar2311/ESP32-S3-PlatformIO-Flash-and-PSRAM-configurations?tab=readme-ov-file#esp32-s3-fn8
3-
[env:leaf]
6+
[env]
47
# platform = espressif32 # Old, default platform
58
# https://github.com/pioarduino/platform-espressif32
69
platform = https://github.com/pioarduino/platform-espressif32/releases/download/stable/platform-espressif32.zip
@@ -28,10 +31,19 @@ lib_deps =
2831
; WiFi Manager for configuring WiFi credentials over captive portal
2932
WiFiManager
3033

31-
# build_type = debug # Might be useful when looking at stack traces
32-
# Used to take exception traces and decode them to meaningful errors
33-
monitor_filters = esp32_exception_decoder
34+
[env:release]
35+
extends = env
36+
build_flags =
37+
${env.build_flags}
38+
# Enables USB Mass Storage for accessing SD card
39+
# NOTE: Disables Serial for debugging / uploading.
40+
-D ENABLE_MASS_STORAGE
3441

35-
[platformio]
36-
src_dir = src/vario
3742

43+
[env:dev]
44+
extends = env
45+
build_flags =
46+
${env.build_flags}
47+
build_type = debug # Might be useful when looking at stack traces
48+
# Used to take exception traces and decode them to meaningful errors
49+
monitor_filters = esp32_exception_decoder

src/vario/SDcard.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@
1515
bool remountSDCard = false; // flag set if the SD_DETECT pin changes state, so we know to attempt a re-mounting if the card is inserted or removed
1616
bool SDcardIsPresent = false;
1717

18+
#include "USB.h"
19+
#include "USBMSC.h"
20+
#include "FirmwareMSC.h"
1821

22+
FirmwareMSC MSC_Update;
23+
USBMSC MSC;
1924

2025
void listDir(fs::FS &fs, const char * dirname, uint8_t levels) {
2126
Serial.printf("Listing directory: %s\n", dirname);
@@ -235,6 +240,54 @@ void SDcard_update() {
235240
SDcard_detectStateLast = SDcard_detectState;
236241
}
237242

243+
static int32_t onRead(uint32_t lba, uint32_t offset, void *buffer, uint32_t bufsize) {
244+
// Check bufSize is a multiple of block size
245+
if (bufsize % 512) {
246+
return -1;
247+
}
248+
249+
auto bufferOffset = 0;
250+
for(int sector = lba; sector < lba + bufsize / 512; sector++) {
251+
if (!SD_MMC.readRAW((uint8_t*)buffer + bufferOffset, sector)) {
252+
return -1;
253+
}
254+
bufferOffset += 512;
255+
}
256+
257+
return bufsize;
258+
}
259+
260+
static int32_t onWrite(uint32_t lba, uint32_t offset, uint8_t *buffer, uint32_t bufsize) {
261+
// Check bufSize is a multiple of block size
262+
if (bufsize % 512) {
263+
return -1;
264+
}
265+
266+
auto bufferOffset = 0;
267+
for(int sector = lba; sector < lba + bufsize / 512; sector++) {
268+
if (!SD_MMC.writeRAW(buffer + bufferOffset, sector)) {
269+
return -1;
270+
}
271+
bufferOffset += 512;
272+
}
273+
274+
return bufsize;
275+
}
276+
277+
void SDCard_SetupMassStorage() {
278+
Serial.setDebugOutput(true);
279+
MSC.vendorID("Leaf");
280+
MSC.productID("Leaf_Vario");
281+
MSC.productRevision("1.0");
282+
MSC.onRead(onRead);
283+
MSC.onWrite(onWrite);
284+
MSC.isWritable(true);
285+
MSC.mediaPresent(true);
286+
MSC.begin(SD_MMC.numSectors(), 512);
287+
MSC_Update.begin();
288+
USB.begin();
289+
}
290+
238291
bool SDcard_mount() {
239292
// we may be re-mounting after changing power states (from ON to CHARGE and back to ON).
240293
// If the SD card was removed during that process, we need to force-end the SD_MCC object so we can try to restart it, so we can properly tell if re-mounting fails (otherwise the SD_MCC object would falsely persist)
@@ -246,6 +299,9 @@ bool SDcard_mount() {
246299
} else {
247300
if (DEBUG_SDCARD) Serial.println("SDcard Mount Success");
248301
SDcardIsPresent = true;
302+
#ifdef ENABLE_MASS_STORAGE
303+
SDCard_SetupMassStorage();
304+
#endif
249305
}
250306

251307
return SDcardIsPresent;

src/vario/SDcard.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,6 @@ bool SDcard_createDataFile(String filename);
4545
void SDcard_writeData(String data);
4646
void SDcard_closeDataFile(void);
4747

48+
void SDCard_SetupMassStorage(void);
49+
4850
#endif

src/vario/vario.ino

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@
7272
/////////////////////////////////////////////////
7373
void setup() {
7474

75+
#ifdef ENABLE_MASS_STORAGE
76+
SDCard_SetupMassStorage();
77+
#endif
78+
7579
// Start USB Serial Debugging Port
7680
Serial.begin(115200);
7781
delay(200);

0 commit comments

Comments
 (0)