33#include < USB.h>
44#include < USBMSC.h>
55#include < cstdint>
6- #include < iostream>
6+ #include < esp32-hal-log.h>
7+ #include < esp_partition.h>
78#include < string>
89
10+ #if ARDUINO_USB_CDC_ON_BOOT == 1
11+ #define HWSerial Serial0
12+ #define USBSerial Serial
13+ #else
14+ #define HWSerial Serial
15+ #include < USBCDC.h>
16+ USBCDC USBSerial;
17+ #endif
18+
919#if defined(ARDUINO_USB_MODE)
1020static_assert (ARDUINO_USB_MODE == 0 , " USB must be in OTG mode" );
1121#endif
@@ -14,16 +24,29 @@ const esp_partition_t *check_ffat_partition(const char *label); // defined in FF
1424
1525static const std::string rootPath = " /" ;
1626static constexpr std::size_t blockSize = 512 ; // bytes
27+ static const char *const TAG = " STORAGE" ;
1728
1829static const esp_partition_t *partition;
1930static USBMSC usbMsc;
2031static bool usbIsRunning = false ;
2132static bool fileSystemIsReady = false ;
2233
34+ static void usb_stopped_cb (void *const pvParameters)
35+ {
36+ Storage::switchToApplicationMode ();
37+ vTaskDelete (nullptr );
38+ }
39+
40+ static void usb_started_cb (void *const pvParameters)
41+ {
42+ Storage::switchToUsbMode ();
43+ vTaskDelete (nullptr );
44+ }
45+
2346static void callbackUsbStarted (void *, esp_event_base_t , int32_t event_id, void *)
2447{
2548 usbIsRunning = true ;
26- Storage::switchToUsbMode ( );
49+ xTaskCreate (usb_started_cb, " USB_Started_CB " , 4096 , nullptr , 5 , nullptr );
2750}
2851
2952static void callbackUsbStopped (void *, esp_event_base_t , int32_t event_id, void *)
@@ -33,7 +56,7 @@ static void callbackUsbStopped(void *, esp_event_base_t, int32_t event_id, void
3356 return ;
3457 }
3558 usbIsRunning = false ;
36- Storage::switchToApplicationMode ( );
59+ xTaskCreate (usb_stopped_cb, " USB_Stopped_CB " , 4096 , nullptr , 5 , nullptr );
3760}
3861
3962/* *
@@ -47,7 +70,7 @@ static void callbackUsbStopped(void *, esp_event_base_t, int32_t event_id, void
4770static std::int32_t usbMsc_onWrite (const std::uint32_t lba, const std::uint32_t offset, std::uint8_t *const buffer,
4871 const uint32_t bufsize)
4972{
50- std::cout << " MSC WRITE: lba: " << lba << " , offset: " << offset << " , bufsize: " << bufsize << std::endl ;
73+ ESP_LOGV (TAG, " MSC WRITE: lba: %u , offset: %u , bufsize: %u \n " , lba, offset, bufsize) ;
5174 const std::uint32_t byteOffset = lba * blockSize + offset;
5275 ESP_ERROR_CHECK (esp_partition_erase_range (partition, byteOffset, bufsize)); // erase must be called before write
5376 ESP_ERROR_CHECK (esp_partition_write (partition, byteOffset, buffer, bufsize));
@@ -68,16 +91,15 @@ static std::int32_t usbMsc_onWrite(const std::uint32_t lba, const std::uint32_t
6891static std::int32_t usbMsc_onRead (const std::uint32_t lba, const std::uint32_t offset, void *const buffer,
6992 const std::uint32_t bufsize)
7093{
71- std::cout << " MSC READ: lba: " << lba << " , offset: " << offset << " , bufsize: " << bufsize << std::endl ;
94+ ESP_LOGV (TAG, " MSC READ: lba: %u , offset: %u , bufsize: %u \n " , lba, offset, bufsize) ;
7295 const std::uint32_t byteOffset = lba * blockSize + offset;
7396 ESP_ERROR_CHECK (esp_partition_read (partition, byteOffset, buffer, bufsize));
7497 return bufsize;
7598}
7699
77100static bool usbMsc_onStartStop (const std::uint8_t power_condition, const bool start, const bool load_eject)
78101{
79- std::cout << " MSC START/STOP: power: " << power_condition << " , start: " << start << " , eject: " << load_eject
80- << std::endl;
102+ ESP_LOGV (TAG, " MSC START/STOP: power: %u, start: %u, eject: %u\n " , power_condition, start, load_eject);
81103 return true ;
82104}
83105
@@ -86,25 +108,27 @@ std::size_t Storage::size()
86108 return FFat.totalBytes ();
87109}
88110
89- void Storage::begin (const bool formatFsOnFail, const char *const partitionLabel)
111+ bool Storage::begin (const bool formatFsOnFail, const char *const partitionLabel)
90112{
113+ ESP_LOGI (TAG, " Starting storage..." );
91114 partition = check_ffat_partition (partitionLabel);
92115
93116 if (!partition)
94117 {
95- std::cerr << " Error with partition!" << std::endl ;
96- return ;
118+ ESP_LOGE (TAG, " Error with partition!" ) ;
119+ return false ;
97120 }
98121
99122 // initialize file system
100123 const auto basePath = rootPath + partitionLabel;
101124 constexpr auto maxOpenFiles = 10U ;
102125 if (!FFat.begin (formatFsOnFail, basePath.c_str (), maxOpenFiles, partitionLabel))
103126 {
104- std::cerr << " File-system initialization failed!" << std::endl ;
105- return ;
127+ ESP_LOGE (TAG, " File-system initialization failed!" ) ;
128+ return false ;
106129 }
107- std::cout << " Storage has a size of " << size () << " bytes." << std::endl;
130+ ESP_LOGI (TAG, " Storage has a size of %u bytes." , size ());
131+ ESP_LOGI (TAG, " Storage mounted at '%s'." , basePath.c_str ());
108132
109133 // setup USB Mass Storage Class
110134 usbMsc.vendorID (" TTS" ); // max 8 chars
@@ -116,13 +140,23 @@ void Storage::begin(const bool formatFsOnFail, const char *const partitionLabel)
116140 usbMsc.onWrite (usbMsc_onWrite);
117141 // usbMsc is ready for read/write
118142 usbMsc.mediaPresent (true );
119- usbMsc.begin (FFat.totalBytes () / blockSize, blockSize);
143+ if (!usbMsc.begin (FFat.totalBytes () / blockSize, blockSize))
144+ {
145+ ESP_LOGE (TAG, " USB MSC initialization failed!" );
146+ return false ;
147+ }
120148 fileSystemIsReady = true ;
121149
122150 // subscribe to USB events
123151 USB.onEvent (ARDUINO_USB_STARTED_EVENT, callbackUsbStarted);
124152 USB.onEvent (ARDUINO_USB_STOPPED_EVENT, callbackUsbStopped);
125- USB.begin ();
153+ if (!USB.begin ())
154+ {
155+ ESP_LOGE (TAG, " USB initialization failed!" );
156+ return false ;
157+ }
158+ ESP_LOGI (TAG, " Storage started." );
159+ return true ;
126160}
127161
128162void Storage::end ()
@@ -138,6 +172,7 @@ void Storage::switchToUsbMode()
138172 FFat.end (); // flush and unmount
139173 fileSystemIsReady = false ;
140174 usbMsc.mediaPresent (true );
175+ ESP_LOGD (TAG, " Switched to USB mode" );
141176}
142177
143178bool Storage::isFileSystemReady ()
@@ -151,6 +186,7 @@ void Storage::switchToApplicationMode()
151186 FFat.end (); // invalidate cache
152187 FFat.begin (); // update data
153188 fileSystemIsReady = true ;
189+ ESP_LOGD (TAG, " Switched to application mode" );
154190}
155191
156192fs::FS &Storage::getFileSystem ()
0 commit comments