|
1 | 1 | #include "storage.hpp" |
| 2 | +#include <FFat.h> |
| 3 | +#include <USB.h> |
| 4 | +#include <USBMSC.h> |
| 5 | +#include <cstdint> |
2 | 6 | #include <iostream> |
| 7 | +#include <string> |
| 8 | + |
| 9 | +#if defined(ARDUINO_USB_MODE) |
| 10 | +static_assert(ARDUINO_USB_MODE == 0, "USB must be in OTG mode"); |
| 11 | +#endif |
3 | 12 |
|
4 | 13 | const esp_partition_t *check_ffat_partition(const char *label); // defined in FFat.cpp |
5 | 14 |
|
6 | | -Storage::Storage(const bool formatFsOnFail, const char *const partitionLabel, ESPUSB &usb, fs::F_Fat &fs, const std::size_t blockSize): |
7 | | -partition(check_ffat_partition(partitionLabel)), |
8 | | -blockSize(blockSize), |
9 | | -fs(fs), |
10 | | -usb(usb) |
| 15 | +static const std::string rootPath = "/"; |
| 16 | +static constexpr std::size_t blockSize = 512; // bytes |
| 17 | + |
| 18 | +static const esp_partition_t *partition; |
| 19 | +static USBMSC usbMsc; |
| 20 | +static bool usbIsRunning = false; |
| 21 | +static bool fileSystemIsReady = false; |
| 22 | + |
| 23 | +static void callbackUsbStarted(void *, esp_event_base_t, int32_t event_id, void *) |
| 24 | +{ |
| 25 | + usbIsRunning = true; |
| 26 | + Storage::switchToUsbMode(); |
| 27 | +} |
| 28 | + |
| 29 | +static void callbackUsbStopped(void *, esp_event_base_t, int32_t event_id, void *) |
| 30 | +{ |
| 31 | + if (!usbIsRunning) /* https://github.com/espressif/arduino-esp32/issues/7228 */ |
| 32 | + { |
| 33 | + return; |
| 34 | + } |
| 35 | + usbIsRunning = false; |
| 36 | + Storage::switchToApplicationMode(); |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Callback invoked when received WRITE10 command. |
| 41 | + * |
| 42 | + * Process data in buffer to disk's storage. |
| 43 | + * |
| 44 | + * @param lba logical block address |
| 45 | + * @returns the number of written bytes (must be multiple of block size) |
| 46 | + */ |
| 47 | +static std::int32_t usbMsc_onWrite(const std::uint32_t lba, const std::uint32_t offset, std::uint8_t *const buffer, |
| 48 | + const uint32_t bufsize) |
| 49 | +{ |
| 50 | + std::cout << "MSC WRITE: lba: " << lba << ", offset: " << offset << ", bufsize: " << bufsize << std::endl; |
| 51 | + const std::uint32_t byteOffset = lba * blockSize + offset; |
| 52 | + ESP_ERROR_CHECK(esp_partition_erase_range(partition, byteOffset, bufsize)); // erase must be called before write |
| 53 | + ESP_ERROR_CHECK(esp_partition_write(partition, byteOffset, buffer, bufsize)); |
| 54 | + return bufsize; |
| 55 | +} |
| 56 | + |
| 57 | +// and |
| 58 | +// return |
| 59 | + |
| 60 | +/** |
| 61 | + * Callback invoked when received READ10 command. |
| 62 | + * |
| 63 | + * Copy disk's data to buffer (up to bufsize). |
| 64 | + * |
| 65 | + * @param lba logical block address |
| 66 | + * @returns the number of copied bytes (must be multiple of block size) |
| 67 | + */ |
| 68 | +static std::int32_t usbMsc_onRead(const std::uint32_t lba, const std::uint32_t offset, void *const buffer, |
| 69 | + const std::uint32_t bufsize) |
| 70 | +{ |
| 71 | + std::cout << "MSC READ: lba: " << lba << ", offset: " << offset << ", bufsize: " << bufsize << std::endl; |
| 72 | + const std::uint32_t byteOffset = lba * blockSize + offset; |
| 73 | + ESP_ERROR_CHECK(esp_partition_read(partition, byteOffset, buffer, bufsize)); |
| 74 | + return bufsize; |
| 75 | +} |
| 76 | + |
| 77 | +static bool usbMsc_onStartStop(const std::uint8_t power_condition, const bool start, const bool load_eject) |
| 78 | +{ |
| 79 | + std::cout << "MSC START/STOP: power: " << power_condition << ", start: " << start << ", eject: " << load_eject |
| 80 | + << std::endl; |
| 81 | + return true; |
| 82 | +} |
| 83 | + |
| 84 | +std::size_t Storage::size() |
| 85 | +{ |
| 86 | + return FFat.totalBytes(); |
| 87 | +} |
| 88 | + |
| 89 | +void Storage::begin(const bool formatFsOnFail, const char *const partitionLabel) |
| 90 | +{ |
| 91 | + partition = check_ffat_partition(partitionLabel); |
| 92 | + |
| 93 | + if (!partition) |
| 94 | + { |
| 95 | + std::cerr << "Error with partition!" << std::endl; |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + // initialize file system |
| 100 | + const auto basePath = rootPath + partitionLabel; |
| 101 | + constexpr auto maxOpenFiles = 10U; |
| 102 | + if (!FFat.begin(formatFsOnFail, basePath.c_str(), maxOpenFiles, partitionLabel)) |
| 103 | + { |
| 104 | + std::cerr << "File-system initialization failed!" << std::endl; |
| 105 | + return; |
| 106 | + } |
| 107 | + std::cout << "Storage has a size of " << size() << " bytes." << std::endl; |
| 108 | + |
| 109 | + // setup USB Mass Storage Class |
| 110 | + usbMsc.vendorID("TTS"); // max 8 chars |
| 111 | + usbMsc.productID("TTD"); // max 16 chars |
| 112 | + usbMsc.productRevision("1.0"); // max 4 chars |
| 113 | + usbMsc.onStartStop(usbMsc_onStartStop); |
| 114 | + // Set callback |
| 115 | + usbMsc.onRead(usbMsc_onRead); |
| 116 | + usbMsc.onWrite(usbMsc_onWrite); |
| 117 | + // usbMsc is ready for read/write |
| 118 | + usbMsc.mediaPresent(true); |
| 119 | + usbMsc.begin(FFat.totalBytes() / blockSize, blockSize); |
| 120 | + fileSystemIsReady = true; |
| 121 | + |
| 122 | + // subscribe to USB events |
| 123 | + USB.onEvent(ARDUINO_USB_STARTED_EVENT, callbackUsbStarted); |
| 124 | + USB.onEvent(ARDUINO_USB_STOPPED_EVENT, callbackUsbStopped); |
| 125 | + USB.begin(); |
| 126 | +} |
| 127 | + |
| 128 | +void Storage::end() |
11 | 129 | { |
12 | | - if(!partition){ |
13 | | - std::cerr << "Error with partition!" << std::endl; |
14 | | - return; |
15 | | - } |
| 130 | + usbMsc.end(); |
| 131 | + usbIsRunning = false; |
| 132 | + FFat.end(); |
| 133 | + fileSystemIsReady = false; |
| 134 | +} |
16 | 135 |
|
17 | | - // initialize file system |
18 | | - const auto basePath = rootPath + partitionLabel; |
19 | | - constexpr auto maxOpenFiles = 10; |
20 | | - if(!fs.begin(formatFsOnFail, basePath.c_str(), maxOpenFiles, partitionLabel)){ |
21 | | - std::cerr << "File-system initialization failed!" << std::endl; |
22 | | - return; |
23 | | - } |
24 | | - std::cout << "Storage has a size of " << size() << " bytes." << std::endl; |
| 136 | +void Storage::switchToUsbMode() |
| 137 | +{ |
| 138 | + FFat.end(); // flush and unmount |
| 139 | + fileSystemIsReady = false; |
| 140 | + usbMsc.mediaPresent(true); |
| 141 | +} |
25 | 142 |
|
26 | | - // subscribe to USB events |
27 | | - callbackSubscribers.push_back(this); |
28 | | - usb.onEvent(ARDUINO_USB_STARTED_EVENT, callbackUsbStarted_s); |
29 | | - usb.onEvent(ARDUINO_USB_STOPPED_EVENT, callbackUsbStopped_s); |
| 143 | +bool Storage::isFileSystemReady() |
| 144 | +{ |
| 145 | + return fileSystemIsReady; |
| 146 | +} |
| 147 | + |
| 148 | +void Storage::switchToApplicationMode() |
| 149 | +{ |
| 150 | + usbMsc.mediaPresent(false); |
| 151 | + FFat.end(); // invalidate cache |
| 152 | + FFat.begin(); // update data |
| 153 | + fileSystemIsReady = true; |
30 | 154 | } |
31 | 155 |
|
32 | | -std::size_t Storage::size() const |
| 156 | +fs::FS &Storage::getFileSystem() |
33 | 157 | { |
34 | | - return fs.totalBytes(); |
| 158 | + return FFat; |
35 | 159 | } |
0 commit comments