22
22
* it using follow sketch "https://github.com/adafruit/Adafruit_SPIFlash/tree/master/examples/SdFat_format"
23
23
* - Copy all files in 'data/' folder of this example to the root directory of the MassStorage disk drive
24
24
* 5. When prompted, open http://esp32fs.local/edit to access the file browser
25
- * 6. Try to modify USB drive then refresh your browser to see if the change is updated
26
- * 7.
25
+ * 6. Modify/Update USB drive then refresh your browser to see if the change is updated
26
+ * 7. Upload/Edit a file using web browser then see if the USB Drive is updated. Note: the usb drive could
27
+ * briefly disappear and reappear to force PC to refresh its cache
27
28
*
28
29
* NOTE: Following library is required
29
30
* - Adafruit_SPIFlash https://github.com/adafruit/Adafruit_SPIFlash
@@ -65,7 +66,7 @@ FatFileSystem fatfs;
65
66
Adafruit_USBD_MSC usb_msc;
66
67
67
68
bool fs_formatted; // Check if flash is formatted
68
- bool fs_changed; // Set to true when PC write to flash
69
+ bool fs_changed; // Set to true when browser write to flash
69
70
70
71
const char * host = " esp32fs" ;
71
72
WebServer server (80 );
@@ -90,21 +91,25 @@ void setupMassStorage(void)
90
91
usb_msc.setCapacity (flash.size ()/512 , 512 );
91
92
92
93
// MSC is ready for read/write
93
- usb_msc.setUnitReady (true );
94
+ fs_changed = false ;
95
+ usb_msc.setReadyCallback (0 , msc_ready_callback);
94
96
95
97
usb_msc.begin ();
96
98
97
99
// Init file system on the flash
98
100
fs_formatted = fatfs.begin (&flash);
99
101
100
- fs_changed = true ; // to print contents initially
101
-
102
102
if ( !fs_formatted )
103
103
{
104
104
DBG_SERIAL.println (" Failed to init files system, flash may not be formatted" );
105
105
}
106
106
}
107
107
108
+ void refreshMassStorage (void )
109
+ {
110
+ fs_changed = true ;
111
+ }
112
+
108
113
void setupServer (void )
109
114
{
110
115
// WIFI INIT
@@ -292,6 +297,7 @@ void handleFileUpload() {
292
297
} else if (upload.status == UPLOAD_FILE_END) {
293
298
if (fsUploadFile) {
294
299
fsUploadFile.close ();
300
+ refreshMassStorage ();
295
301
}
296
302
DBG_SERIAL.print (" handleFileUpload Size: " ); DBG_SERIAL.println (upload.totalSize );
297
303
}
@@ -310,6 +316,7 @@ void handleFileDelete() {
310
316
return server.send (404 , " text/plain" , " FileNotFound" );
311
317
}
312
318
fatfs.remove (path.c_str ());
319
+ refreshMassStorage ();
313
320
server.send (200 , " text/plain" , " " );
314
321
path = String ();
315
322
}
@@ -378,52 +385,6 @@ void loop()
378
385
{
379
386
server.handleClient ();
380
387
delay (2 );// allow the cpu to switch to other tasks
381
-
382
- if ( fs_changed )
383
- {
384
- fs_changed = false ;
385
-
386
- // check if host formatted disk
387
- if (!fs_formatted)
388
- {
389
- fs_formatted = fatfs.begin (&flash);
390
- }
391
-
392
- // skip if still not formatted
393
- if (!fs_formatted) return ;
394
-
395
- // DBG_SERIAL.println("Opening root");
396
-
397
- // if ( !root.open("/") )
398
- // {
399
- // DBG_SERIAL.println("open root failed");
400
- // return;
401
- // }
402
- //
403
- // DBG_SERIAL.println("Flash contents:");
404
- //
405
- // // Open next file in root.
406
- // // Warning, openNext starts at the current directory position
407
- // // so a rewind of the directory may be required.
408
- // while ( file.openNext(&root, O_READ) )
409
- // {
410
- // file.printFileSize(&DBG_SERIAL);
411
- // DBG_SERIAL.write(' ');
412
- // file.printName(&DBG_SERIAL);
413
- // if ( file.isDir() )
414
- // {
415
- // // Indicate a directory.
416
- // DBG_SERIAL.write('/');
417
- // }
418
- // DBG_SERIAL.println();
419
- // file.close();
420
- // }
421
- //
422
- // root.close();
423
-
424
- // DBG_SERIAL.println();
425
- // delay(1000);
426
- }
427
388
}
428
389
429
390
// Callback invoked when received READ10 command.
@@ -460,9 +421,17 @@ void msc_flush_cb (void)
460
421
// clear file system's cache to force refresh
461
422
fatfs.cacheClear ();
462
423
463
- fs_changed = true ;
464
-
465
424
#ifdef LED_BUILTIN
466
425
digitalWrite (LED_BUILTIN, LOW);
467
426
#endif
468
427
}
428
+
429
+ // Invoked when received Test Unit Ready command.
430
+ // return true allowing host to read/write this LUN e.g SD card inserted
431
+ bool msc_ready_callback (void )
432
+ {
433
+ // if fs has changed, mark unit as not ready temporarily to force PC to flush cache
434
+ bool ret = !fs_changed;
435
+ fs_changed = false ;
436
+ return ret;
437
+ }
0 commit comments