2222 * it using follow sketch "https://github.com/adafruit/Adafruit_SPIFlash/tree/master/examples/SdFat_format"
2323 * - Copy all files in 'data/' folder of this example to the root directory of the MassStorage disk drive
2424 * 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
2728 *
2829 * NOTE: Following library is required
2930 * - Adafruit_SPIFlash https://github.com/adafruit/Adafruit_SPIFlash
@@ -65,7 +66,7 @@ FatFileSystem fatfs;
6566Adafruit_USBD_MSC usb_msc;
6667
6768bool 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
6970
7071const char * host = " esp32fs" ;
7172WebServer server (80 );
@@ -90,21 +91,25 @@ void setupMassStorage(void)
9091 usb_msc.setCapacity (flash.size ()/512 , 512 );
9192
9293 // MSC is ready for read/write
93- usb_msc.setUnitReady (true );
94+ fs_changed = false ;
95+ usb_msc.setReadyCallback (0 , msc_ready_callback);
9496
9597 usb_msc.begin ();
9698
9799 // Init file system on the flash
98100 fs_formatted = fatfs.begin (&flash);
99101
100- fs_changed = true ; // to print contents initially
101-
102102 if ( !fs_formatted )
103103 {
104104 DBG_SERIAL.println (" Failed to init files system, flash may not be formatted" );
105105 }
106106}
107107
108+ void refreshMassStorage (void )
109+ {
110+ fs_changed = true ;
111+ }
112+
108113void setupServer (void )
109114{
110115 // WIFI INIT
@@ -292,6 +297,7 @@ void handleFileUpload() {
292297 } else if (upload.status == UPLOAD_FILE_END) {
293298 if (fsUploadFile) {
294299 fsUploadFile.close ();
300+ refreshMassStorage ();
295301 }
296302 DBG_SERIAL.print (" handleFileUpload Size: " ); DBG_SERIAL.println (upload.totalSize );
297303 }
@@ -310,6 +316,7 @@ void handleFileDelete() {
310316 return server.send (404 , " text/plain" , " FileNotFound" );
311317 }
312318 fatfs.remove (path.c_str ());
319+ refreshMassStorage ();
313320 server.send (200 , " text/plain" , " " );
314321 path = String ();
315322}
@@ -378,52 +385,6 @@ void loop()
378385{
379386 server.handleClient ();
380387 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- }
427388}
428389
429390// Callback invoked when received READ10 command.
@@ -460,9 +421,17 @@ void msc_flush_cb (void)
460421 // clear file system's cache to force refresh
461422 fatfs.cacheClear ();
462423
463- fs_changed = true ;
464-
465424#ifdef LED_BUILTIN
466425 digitalWrite (LED_BUILTIN, LOW);
467426#endif
468427}
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