Skip to content

Commit 2d7139c

Browse files
committed
[SD] Link tinyusb, refactor init
1 parent 4a6364e commit 2d7139c

File tree

5 files changed

+56
-30
lines changed

5 files changed

+56
-30
lines changed

src/Wippersnapper_V2.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,11 @@ void Wippersnapper_V2::provisionV2() {
104104
_littleFSV2 = new WipperSnapper_LittleFS();
105105
#endif
106106

107-
// Are we using sd-card/offline mode?
107+
// Determines if app is in SDLogger mode
108108
#ifdef USE_TINYUSB
109109
_fileSystemV2->GetSDCSPin();
110-
// TODO: Implement this for LittleFS TOO and do an elseif for USE_LITTLEFS
110+
#else
111+
_littleFSV2->GetSDCSPin();
111112
#endif
112113
if (WsV2._sdCardV2->InitSDCard())
113114
return;

src/provisioning/littlefs/WipperSnapper_LittleFS_V2.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,22 +155,30 @@ void WipperSnapper_LittleFS_V2::fsHalt(String msg) {
155155
}
156156
}
157157

158+
/**************************************************************************/
159+
/*!
160+
@brief Attempts to obtain the hardware's CS pin from the
161+
config.json file.
162+
*/
163+
/**************************************************************************/
158164
void WipperSnapper_LittleFS_V2::GetSDCSPin() {
159-
160-
BRENT YOU WERE HERE YOU WERE HERE !!!
161-
162-
File file_cfg = LittleFS.open("/config.json");
165+
File32 file_cfg;
166+
JsonDocument doc;
167+
DeserializationError error;
168+
// Attempt to open and deserialize the config.json file
169+
file_cfg = LittleFS.open("/config.json");
163170
if (!file_cfg) {
164171
WsV2.pin_sd_cs = 255;
165172
return;
166173
}
167-
DeserializationError error = deserializeJson(doc, file_cfg);
168-
// failed to deserialize the config file, bail out
174+
error = deserializeJson(doc, file_cfg);
169175
if (error) {
170176
file_cfg.close();
171177
WsV2.pin_sd_cs = 255;
172178
return;
173179
}
180+
181+
// Parse config.json and save the SD CS pin
174182
JsonObject exportedFromDevice = doc["exportedFromDevice"];
175183
WsV2.pin_sd_cs = exportedFromDevice["sd_cs_pin"] | 255;
176184
file_cfg.close();

src/provisioning/sdcard/ws_sdcard.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,21 @@ ws_sdcard::~ws_sdcard() {
3636
}
3737
}
3838

39+
/**************************************************************************/
40+
/*!
41+
@brief Attempts to initialize the SD card and filesystem.
42+
@returns True if the SD card was successfully initialized, False
43+
otherwise.
44+
*/
45+
/**************************************************************************/
3946
bool ws_sdcard::InitSDCard() {
40-
is_mode_offline = false;
41-
if (WsV2.pin_sd_cs == 255) { // No SD card CS pin defined
42-
return is_mode_offline;
43-
} else { // SD card CS pin defined
44-
if (_sd.begin(WsV2.pin_sd_cs))
45-
is_mode_offline = true;
46-
}
47-
return is_mode_offline;
47+
// Check if GetSDCSPin() threw error
48+
if (WsV2.pin_sd_cs == 255)
49+
return false;
50+
51+
if (!_sd.begin(WsV2.pin_sd_cs))
52+
return false;
53+
return true;
4854
}
4955

5056
/**************************************************************************/

src/provisioning/tinyusb/Wippersnapper_FS.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -279,15 +279,15 @@ bool Wippersnapper_FS::createBootFile() {
279279
bootFile.print("MAC Address: ");
280280
bootFile.println(sMAC);
281281

282-
// Print ESP-specific info to boot file
283-
#ifdef ARDUINO_ARCH_ESP32
282+
// Print ESP-specific info to boot file
283+
#ifdef ARDUINO_ARCH_ESP32
284284
// Get version of ESP-IDF
285285
bootFile.print("ESP-IDF Version: ");
286286
bootFile.println(ESP.getSdkVersion());
287287
// Get version of this core
288288
bootFile.print("ESP32 Core Version: ");
289289
bootFile.println(ESP.getCoreVersion());
290-
#endif
290+
#endif
291291

292292
bootFile.flush();
293293
bootFile.close();
@@ -617,26 +617,26 @@ DSTATUS fdisk_initialize(BYTE pdrv) {
617617
}
618618

619619
DRESULT fdisk_read(BYTE pdrv, /* Physical drive nmuber to identify the drive */
620-
BYTE *buff, /* Data buffer to store read data */
621-
DWORD sector, /* Start sector in LBA */
622-
UINT count /* Number of sectors to read */
620+
BYTE *buff, /* Data buffer to store read data */
621+
DWORD sector, /* Start sector in LBA */
622+
UINT count /* Number of sectors to read */
623623
) {
624624
(void)pdrv;
625625
return flash.readBlocks(sector, buff, count) ? RES_OK : RES_ERROR;
626626
}
627627

628628
DRESULT fdisk_write(BYTE pdrv, /* Physical drive nmuber to identify the drive */
629-
const BYTE *buff, /* Data to be written */
630-
DWORD sector, /* Start sector in LBA */
631-
UINT count /* Number of sectors to write */
629+
const BYTE *buff, /* Data to be written */
630+
DWORD sector, /* Start sector in LBA */
631+
UINT count /* Number of sectors to write */
632632
) {
633633
(void)pdrv;
634634
return flash.writeBlocks(sector, buff, count) ? RES_OK : RES_ERROR;
635635
}
636636

637637
DRESULT fdisk_ioctl(BYTE pdrv, /* Physical drive nmuber (0..) */
638-
BYTE cmd, /* Control code */
639-
void *buff /* Buffer to send/receive control data */
638+
BYTE cmd, /* Control code */
639+
void *buff /* Buffer to send/receive control data */
640640
) {
641641
(void)pdrv;
642642

src/provisioning/tinyusb/Wippersnapper_FS_V2.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,19 +162,30 @@ Wippersnapper_FS_V2::~Wippersnapper_FS_V2() {
162162
wipperFatFs_v2.end();
163163
}
164164

165+
/**************************************************************************/
166+
/*!
167+
@brief Attempts to obtain the hardware's CS pin from the
168+
config.json file.
169+
*/
170+
/**************************************************************************/
165171
void Wippersnapper_FS_V2::GetSDCSPin() {
166-
File32 file_cfg = wipperFatFs_v2.open("/config.json");
172+
File32 file_cfg;
173+
JsonDocument doc;
174+
DeserializationError error;
175+
// Attempt to open and deserialize the config.json file
176+
file_cfg = wipperFatFs_v2.open("/config.json");
167177
if (!file_cfg) {
168178
WsV2.pin_sd_cs = 255;
169179
return;
170180
}
171-
DeserializationError error = deserializeJson(doc, file_cfg);
172-
// failed to deserialize the config file, bail out
181+
deserializeJson(doc, file_cfg);
173182
if (error) {
174183
file_cfg.close();
175184
WsV2.pin_sd_cs = 255;
176185
return;
177186
}
187+
188+
// Parse config.json and save the SD CS pin
178189
JsonObject exportedFromDevice = doc["exportedFromDevice"];
179190
WsV2.pin_sd_cs = exportedFromDevice["sd_cs_pin"] | 255;
180191
file_cfg.close();

0 commit comments

Comments
 (0)