1- #include " storage.hpp"
2-
31#include < Arduino.h>
4- #if defined(ARDUINO_USB_MODE)
5- static_assert (ARDUINO_USB_MODE == 0 , " must be used when USB is in OTG mode" );
6- #endif
2+
3+ #include " storage.hpp"
74#include < FFat.h>
85#include < USB.h>
96#include < USBMSC.h>
107#include < atomic>
118#include < condition_variable>
129#include < cstdint>
10+ #include < esp32-hal-log.h>
1311#include < esp_err.h>
1412#include < esp_partition.h>
1513#include < iostream>
1614#include < mutex>
1715
18- #if ARDUINO_USB_CDC_ON_BOOT == 1
19- #define HWSerial Serial0
20- #else
21- #define HWSerial Serial
16+ #if defined(ARDUINO_USB_MODE)
17+ static_assert (ARDUINO_USB_MODE == 0 , " USB must be in OTG mode" );
2218#endif
2319
24- static USBMSC MSC;
20+ const esp_partition_t *check_ffat_partition (const char *label); // defined in FFat.cpp
21+
22+ static USBMSC usbMsc;
2523
2624static constexpr std::uint16_t blockSize = 512 ; // Should be 512
2725
@@ -52,36 +50,52 @@ static class ReadyCondition
5250 std::atomic<bool > ready;
5351} fileSystemState;
5452
55- // Callback invoked when received WRITE10 command.
56- // Process data in buffer to disk's storage and
57- // return number of written bytes (must be multiple of block size)
58- static int32_t onWrite (uint32_t lba, uint32_t offset, uint8_t *buffer, uint32_t bufsize)
53+ /* *
54+ * Callback invoked when received WRITE10 command.
55+ *
56+ * Process data in buffer to disk's storage.
57+ *
58+ * @param lba logical block address
59+ * @returns the number of written bytes (must be multiple of block size)
60+ */
61+ static std::int32_t usbMsc_onWrite (const std::uint32_t lba, const std::uint32_t offset, std::uint8_t *const buffer,
62+ const uint32_t bufsize)
5963{
6064 ESP_LOGV (TAG, " MSC WRITE: lba: %u, offset: %u, bufsize: %u\n " , lba, offset, bufsize);
61- uint32_t byteOffset = lba * blockSize + offset;
62- // erase must be called before write
63- ESP_ERROR_CHECK (esp_partition_erase_range (partition, byteOffset, bufsize));
65+ const std::uint32_t byteOffset = lba * blockSize + offset;
66+ ESP_ERROR_CHECK (esp_partition_erase_range (partition, byteOffset, bufsize)); // erase must be called before write
6467 ESP_ERROR_CHECK (esp_partition_write (partition, byteOffset, buffer, bufsize));
6568 return bufsize;
6669}
6770
68- // Callback invoked when received READ10 command.
69- // Copy disk's data to buffer (up to bufsize) and
70- // return number of copied bytes (must be multiple of block size)
71- static int32_t onRead (uint32_t lba, uint32_t offset, void *buffer, uint32_t bufsize)
71+ /* *
72+ * Callback invoked when received READ10 command.
73+ *
74+ * Copy disk's data to buffer (up to bufsize).
75+ *
76+ * @param lba logical block address
77+ * @returns the number of copied bytes (must be multiple of block size)
78+ */
79+ static std::int32_t usbMsc_onRead (const std::uint32_t lba, const std::uint32_t offset, void *const buffer,
80+ const std::uint32_t bufsize)
7281{
7382 ESP_LOGV (TAG, " MSC READ: lba: %u, offset: %u, bufsize: %u\n " , lba, offset, bufsize);
74- uint32_t byteOffset = lba * blockSize + offset;
83+ const std:: uint32_t byteOffset = lba * blockSize + offset;
7584 ESP_ERROR_CHECK (esp_partition_read (partition, byteOffset, buffer, bufsize));
7685 return bufsize;
7786}
7887
79- static bool onStartStop ( uint8_t power_condition, bool start, bool load_eject)
88+ static bool usbMsc_onStartStop ( const std:: uint8_t power_condition, const bool start, const bool load_eject)
8089{
8190 ESP_LOGV (TAG, " MSC START/STOP: power: %u, start: %u, eject: %u\n " , power_condition, start, load_eject);
8291 return true ;
8392}
8493
94+ std::size_t Storage::size ()
95+ {
96+ return FFat.totalBytes ();
97+ }
98+
8599/* *
86100 * Lists files and directories at path.
87101 */
@@ -112,7 +126,7 @@ static void listFiles(const char *const dirname)
112126 */
113127static void switchToApplicationMode ()
114128{
115- MSC .mediaPresent (false );
129+ usbMsc .mediaPresent (false );
116130 FFat.end (); // invalidate cache
117131 ESP_ERROR_CHECK (FFat.begin ()); // update data
118132 fileSystemState.setReady (true );
@@ -125,7 +139,7 @@ static void switchToUSBMode()
125139{
126140 FFat.end (); // flush and unmount
127141 fileSystemState.setReady (false );
128- MSC .mediaPresent (true );
142+ usbMsc .mediaPresent (true );
129143}
130144
131145static void usb_stopped_cb (void *const pvParameters)
@@ -157,13 +171,11 @@ static void usbStartedCallback(void *, esp_event_base_t, int32_t, void *)
157171 xTaskCreate (usb_started_cb, " USB_Started_CB" , 4096 , nullptr , 5 , nullptr );
158172}
159173
160- const esp_partition_t *check_ffat_partition (const char *label); // defined in FFat.cpp
161-
162174void Storage::begin ()
163175{
164176
165177 if (!FFat.begin (true ))
166- { // `true` = Formatieren falls kein Dateisystem vorhanden
178+ {
167179 ESP_LOGE (TAG, " Failed to init files system, flash may not be formatted" );
168180 return ;
169181 }
@@ -179,18 +191,18 @@ void Storage::begin()
179191
180192 USB.onEvent (ARDUINO_USB_STARTED_EVENT, usbStartedCallback);
181193 USB.onEvent (ARDUINO_USB_STOPPED_EVENT, usbStoppedCallback);
182- MSC .vendorID (" ESP32" ); // max 8 chars
183- MSC .productID (" USB_MSC" ); // max 16 chars
184- MSC .productRevision (" 1.0" ); // max 4 chars
185- MSC .onStartStop (onStartStop );
194+ usbMsc .vendorID (" ESP32" ); // max 8 chars
195+ usbMsc .productID (" USB_MSC" ); // max 16 chars
196+ usbMsc .productRevision (" 1.0" ); // max 4 chars
197+ usbMsc .onStartStop (usbMsc_onStartStop );
186198 // Set callback
187- MSC .onRead (onRead );
188- MSC .onWrite (onWrite );
199+ usbMsc .onRead (usbMsc_onRead );
200+ usbMsc .onWrite (usbMsc_onWrite );
189201 // MSC is ready for read/write
190- MSC .mediaPresent (true );
202+ usbMsc .mediaPresent (true );
191203
192204 // Set disk size, block size should be 512 regardless of spi flash page size
193- if (!MSC .begin (FFat.totalBytes () / blockSize, blockSize))
205+ if (!usbMsc .begin (FFat.totalBytes () / blockSize, blockSize))
194206 {
195207 ESP_LOGE (TAG, " starting USB MSC failed" );
196208 }
@@ -200,6 +212,13 @@ void Storage::begin()
200212 }
201213}
202214
215+ void Storage::end ()
216+ {
217+ usbMsc.end ();
218+ usbIsRunning = false ;
219+ FFat.end ();
220+ }
221+
203222void Storage::waitForFileSystem ()
204223{
205224 fileSystemState.wait_unitl_ready ();
0 commit comments