Skip to content

Commit 1c3f434

Browse files
committed
Merge branch 'feature/6-persistent-storage-for-configuration-and-task-information-2' into feature/6-persistent-storage-for-configuration-and-task-information-7
2 parents e1b6c20 + 351da67 commit 1c3f434

File tree

3 files changed

+64
-47
lines changed

3 files changed

+64
-47
lines changed

lib/storage/storage.cpp

Lines changed: 55 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
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

2624
static 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
*/
113127
static 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

131145
static 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-
162174
void 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+
203222
void Storage::waitForFileSystem()
204223
{
205224
fileSystemState.wait_unitl_ready();

lib/storage/storage.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#pragma once
2+
#include <cstddef>
23

34
struct Storage
45
{
56
static void begin();
67
static void waitForFileSystem();
8+
static void end();
9+
static std::size_t size();
710
};

src/main.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
#include <Arduino.h>
2-
#if defined(ARDUINO_USB_MODE)
3-
static_assert(ARDUINO_USB_MODE == 0, "must be used when USB is in OTG mode");
4-
#endif
5-
#include <USB.h>
6-
#include <USBMSC.h>
72
#include <esp32-hal-log.h>
83
#include <esp_err.h>
94
#include <iostream>
@@ -20,14 +15,14 @@ static const char *const TAG = "MAIN";
2015
void setup()
2116
{
2217
HWSerial.begin(115200);
23-
delay(200); // wait for the serial monitor to be ready
2418
HWSerial.setDebugOutput(true);
25-
ESP_LOGV(TAG, "verbose");
26-
ESP_LOGD(TAG, "debug");
27-
ESP_LOGI(TAG, "info");
28-
ESP_LOGW(TAG, "warning");
29-
ESP_LOGE(TAG, "error");
19+
delay(300); // in order to give the serial monitor time to start
3020
std::cout << "Started program" << std::endl;
21+
ESP_LOGE(TAG, "Example error");
22+
ESP_LOGW(TAG, "Example warning");
23+
ESP_LOGI(TAG, "Example info");
24+
ESP_LOGD(TAG, "Example debug");
25+
ESP_LOGV(TAG, "Example verbose");
3126
Storage::begin();
3227
}
3328

0 commit comments

Comments
 (0)