@@ -59,12 +59,10 @@ Adafruit_USBD_MSC usb_msc_v2; /*!< USB mass storage object */
5959FATFS elmchamFatfs_v2; // /< Elm Cham's fatfs object
6060uint8_t workbuf_v2[4096 ]; // /< Working buffer for f_fdisk function.
6161
62- bool mk_fs (void ) {
62+ FRESULT mk_fs (void ) {
6363 // Make filesystem
6464 FRESULT r = f_mkfs (" " , FM_FAT | FM_SFD, 0 , workbuf_v2, sizeof (workbuf_v2));
65- if (r != FR_OK)
66- return false ;
67- return true ;
65+ return r;
6866}
6967
7068bool mk_mount_disk_label (void ) {
@@ -92,12 +90,43 @@ bool mk_set_disk_label(void) {
9290 return true ;
9391}
9492
93+ String getFRESULTMessage (FRESULT result) {
94+ switch (result) {
95+ case FR_OK: return " Succeeded" ;
96+ case FR_DISK_ERR: return " A hard error occurred in the low level disk I/O layer" ;
97+ case FR_INT_ERR: return " Assertion failed" ;
98+ case FR_NOT_READY: return " The physical drive cannot work" ;
99+ case FR_NO_FILE: return " Could not find the file" ;
100+ case FR_NO_PATH: return " Could not find the path" ;
101+ case FR_INVALID_NAME: return " The path name format is invalid" ;
102+ case FR_DENIED: return " Access denied due to prohibited access or directory full" ;
103+ case FR_EXIST: return " Access denied due to prohibited access" ;
104+ case FR_INVALID_OBJECT: return " The file/directory object is invalid" ;
105+ case FR_WRITE_PROTECTED: return " The physical drive is write protected" ;
106+ case FR_INVALID_DRIVE: return " The logical drive number is invalid" ;
107+ case FR_NOT_ENABLED: return " The volume has no work area" ;
108+ case FR_NO_FILESYSTEM: return " There is no valid FAT volume" ;
109+ case FR_MKFS_ABORTED: return " The f_mkfs() aborted due to any problem" ;
110+ case FR_TIMEOUT: return " Could not get a grant to access the volume within defined period" ;
111+ case FR_LOCKED: return " The operation is rejected according to the file sharing policy" ;
112+ case FR_NOT_ENOUGH_CORE: return " LFN working buffer could not be allocated" ;
113+ case FR_TOO_MANY_OPEN_FILES: return " Number of open files > FF_FS_LOCK" ;
114+ case FR_INVALID_PARAMETER: return " Given parameter is invalid" ;
115+ default : return " Unknown error" ;
116+ }
117+ }
118+
95119/* *************************************************************************/
96120/* !
97121 @brief Initializes USB-MSC and the QSPI flash filesystem.
98122*/
99123/* *************************************************************************/
100124Wippersnapper_FS_V2::Wippersnapper_FS_V2 () {
125+ // Detach USB device during init.
126+ TinyUSBDevice.detach ();
127+ // Wait for detach
128+ delay (500 );
129+
101130 // Attempt to initialize the flash chip
102131 if (!flash_v2.begin ()) {
103132 setStatusLEDColor (RED);
@@ -106,16 +135,16 @@ Wippersnapper_FS_V2::Wippersnapper_FS_V2() {
106135
107136 // Check if the filesystem is formatted
108137 _isFormatted = wipperFatFs_v2.begin (&flash_v2);
138+ _isFormatted = false ;
109139
110140 // If we are not formatted, attempt to format the filesystem as fat12
111141 if (!_isFormatted) {
112- if (! mk_fs ()) {
113- setStatusLEDColor (RED);
114- fsHalt (" Failed to format the filesystem!" );
115- }
142+ FRESULT rc = mk_fs ();
143+ fsHalt (getFRESULTMessage (rc));
144+
116145 if (! mk_mount_disk_label () ) {
117146 setStatusLEDColor (RED);
118- fsHalt (" Failed to mount the filesystem!" );
147+ fsHalt (" L125: Failed to mount the filesystem!" );
119148 }
120149 if (! mk_set_disk_label () ) {
121150 setStatusLEDColor (RED);
@@ -131,6 +160,12 @@ Wippersnapper_FS_V2::Wippersnapper_FS_V2() {
131160 _freshFS = true ;
132161 }
133162
163+ if (!wipperFatFs_v2.begin (&flash_v2)) {
164+ setStatusLEDColor (RED);
165+ // pass res to the fsHalt
166+ fsHalt (" L143: Failed to mount the filesystem" );
167+ }
168+
134169 // Write contents to the filesystem
135170 if (! writeFSContents ()) {
136171 setStatusLEDColor (RED);
0 commit comments