Skip to content

Commit ab0a115

Browse files
committed
Add logging
1 parent 7b1882d commit ab0a115

File tree

4 files changed

+74
-22
lines changed

4 files changed

+74
-22
lines changed

lib/storage/storage.cpp

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,19 @@
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)
1020
static_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

1525
static const std::string rootPath = "/";
1626
static constexpr std::size_t blockSize = 512; // bytes
27+
static const char *const TAG = "STORAGE";
1728

1829
static const esp_partition_t *partition;
1930
static USBMSC usbMsc;
2031
static bool usbIsRunning = false;
2132
static 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+
2346
static 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

2952
static 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
4770
static 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
6891
static 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

77100
static 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

128162
void 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

143178
bool 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

156192
fs::FS &Storage::getFileSystem()

lib/storage/storage.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44
#include <USBMSC.h>
55
#include <cstddef>
66
#include <esp_event_base.h>
7-
#include <esp_partition.h>
87
#include <string>
98

109
class Storage
1110
{
1211
public:
13-
static void begin(bool formatFsOnFail = false, const char *partitionLabel = FFAT_PARTITION_LABEL);
12+
static bool begin(bool formatFsOnFail = false, const char *partitionLabel = FFAT_PARTITION_LABEL);
1413
static void end();
1514
static bool isFileSystemReady();
1615
static std::size_t size();

platformio.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ board_build.filesystem = fatfs
66
board_build.partitions = default_ffat_8MB.csv
77
monitor_filters = esp32_exception_decoder
88
monitor_speed = 115200
9+
build_type = debug
910

1011
build_unflags =
1112
-DARDUINO_USB_MODE=1
@@ -18,3 +19,5 @@ build_flags =
1819
-DARDUINO_USB_CDC_ON_BOOT=1
1920
-DARDUINO_USB_MSC_ON_BOOT=0
2021
-DARDUINO_USB_DFU_ON_BOOT=0
22+
-DCONFIG_ARDUHAL_LOG_COLORS=1
23+
-DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_VERBOSE

src/main.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#include <Arduino.h>
2+
#include <esp32-hal-log.h>
3+
#include <iostream>
24
#include <storage.hpp>
35

46
#if ARDUINO_USB_CDC_ON_BOOT == 1
@@ -10,6 +12,8 @@
1012
USBCDC USBSerial;
1113
#endif
1214

15+
static const char *const TAG = "MAIN";
16+
1317
struct TestStorage : Storage
1418
{
1519

@@ -18,12 +22,13 @@ struct TestStorage : Storage
1822
*/
1923
static void listFiles(const char *const dirname)
2024
{
21-
auto FFat = getFileSystem();
25+
// auto FFat = getFileSystem();
2226
HWSerial.printf("Directory: '%s'\n", dirname);
2327
File root = FFat.open(dirname);
2428
if (!root || !root.isDirectory())
2529
{
26-
HWSerial.printf("Error: '%s' is not a directory!\n", dirname);
30+
ESP_LOGE(TAG, "'%s' is not a directory!\n", dirname);
31+
root.close();
2732
return;
2833
}
2934

@@ -33,15 +38,24 @@ struct TestStorage : Storage
3338
HWSerial.printf(" %s (%s, %d Bytes)\n", file.name(), file.isDirectory() ? "d" : "f", file.size());
3439
file = root.openNextFile();
3540
}
41+
file.close();
42+
root.close();
3643
}
3744
};
3845

3946
void setup()
4047
{
41-
ESP_LOGE("tag", "hello"), HWSerial.begin(115200);
48+
HWSerial.begin(115200);
4249
HWSerial.setDebugOutput(true);
43-
Storage::begin();
44-
50+
delay(300); // in order to give the serial monitor time to start
51+
HWSerial.println("START HWSerial");
52+
ESP_LOGE(TAG, "Example error");
53+
ESP_LOGW(TAG, "Example warning");
54+
ESP_LOGI(TAG, "Example info");
55+
ESP_LOGD(TAG, "Example debug");
56+
ESP_LOGV(TAG, "Example verbose");
57+
std::cout << "Hello" << std::endl;
58+
Storage::begin(true);
4559
USBSerial.begin();
4660
}
4761

0 commit comments

Comments
 (0)