Skip to content

Commit 1fd4328

Browse files
committed
[SD] Untested on HW | Fix for ESP32 SPI clock issue
1 parent 253a2c0 commit 1fd4328

File tree

8 files changed

+65
-48
lines changed

8 files changed

+65
-48
lines changed

.vscode/settings.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
22
"files.associations": {
33
"limits": "c",
44
"type_traits": "c",
5-
"vector": "cpp"
5+
"vector": "cpp",
6+
"array": "cpp",
7+
"deque": "cpp",
8+
"string": "cpp",
9+
"unordered_map": "cpp",
10+
"string_view": "cpp",
11+
"initializer_list": "cpp",
12+
"span": "cpp"
613
},
714
"C_Cpp.dimInactiveRegions": false,
815
"dotnet.defaultSolution": "disable",

platformio.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,6 @@ extends = common:esp32
296296
board = adafruit_qtpy_esp32
297297
board_build.partitions = default_8MB.csv
298298
board_build.filesystem = littlefs
299-
build_type = debug
300299
build_flags = -DARDUINO_ADAFRUIT_QTPY_ESP32
301300

302301
; Adafruit QT Py ESP32-C3
@@ -306,6 +305,7 @@ board = adafruit_qtpy_esp32c3
306305
build_flags = -DARDUINO_ADAFRUIT_QTPY_ESP32C3
307306
board_build.filesystem = littlefs
308307
board_build.partitions = min_spiffs.csv
308+
upload_port = /dev/cu.usbserial-120
309309

310310
; Adafruit QT Py ESP32-S2
311311
[env:adafruit_qtpy_esp32s2]

src/Wippersnapper_V2.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,6 @@ Wippersnapper_V2::Wippersnapper_V2() {
6868
// Initialize model classes
6969
WsV2.sensorModel = new SensorModel();
7070

71-
// SD Card
72-
WsV2._sdCardV2 = new ws_sdcard();
73-
7471
// Initialize controller classes
7572
WsV2.digital_io_controller = new DigitalIOController();
7673
WsV2.analogio_controller = new AnalogIOController();
@@ -106,12 +103,18 @@ void Wippersnapper_V2::provisionV2() {
106103

107104
// Determine if app is in SDLogger mode
108105
#ifdef USE_TINYUSB
109-
_fileSystemV2->GetSDCSPin();
110-
#else
106+
_fileSystemV2->GetSDCSPin();
107+
#elif defined(USE_LITTLEFS)
111108
_littleFSV2->GetSDCSPin();
112109
#endif
113-
if (WsV2._sdCardV2->InitSDCard())
110+
WsV2._sdCardV2 = new ws_sdcard();
111+
if (WsV2._sdCardV2->isSDCardInitialized()) {
112+
haltErrorV2("GOOD: passed sd cspin check", WS_LED_STATUS_KAT);
114113
return;
114+
} else {
115+
// We are hitting against this at 4:16pm 12/16/2021
116+
haltErrorV2("ERROR: failed sd cspin check", WS_LED_STATUS_MQTT_CONNECTING);
117+
}
115118

116119
#ifdef USE_DISPLAY
117120
// Initialize the display

src/Wippersnapper_demo.ino

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@ Wippersnapper_Manager manager;
2626

2727
void setup() {
2828
// NOTE: Provisioning must occur prior to serial init.h
29+
Serial.begin(115200);
30+
while (!Serial) delay(10);
2931
manager.checkAPIVersion(API_PIN);
3032
manager.provision();
3133

32-
Serial.begin(115200);
33-
while (!Serial) delay(10);
34-
3534
Serial.println("Adafruit Wippersnapper API Manager Demo");
3635
Serial.print("Running Wippersnapper API Version: ");
3736
Serial.println(manager.getAPIVersion());

src/provisioning/littlefs/WipperSnapper_LittleFS_V2.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
defined(ARDUINO_ADAFRUIT_ITSYBITSY_ESP32) || \
1818
defined(ARDUINO_ADAFRUIT_FEATHER_ESP32_V2) || \
1919
defined(ARDUINO_ADAFRUIT_QTPY_ESP32_PICO) || \
20-
defined(ARDUINO_ADAFRUIT_QTPY_ESP32C3) || \
21-
defined(ARDUINO_ESP32_DEV) || \
20+
defined(ARDUINO_ADAFRUIT_QTPY_ESP32C3) || defined(ARDUINO_ESP32_DEV) || \
2221
defined(ESP32_DEV)
2322
#include "WipperSnapper_LittleFS_V2.h"
2423

@@ -31,8 +30,8 @@
3130
WipperSnapper_LittleFS_V2::WipperSnapper_LittleFS_V2() {
3231
// Attempt to initialize filesystem
3332
if (!LittleFS.begin()) {
34-
setStatusLEDColor(RED);
35-
fsHalt("ERROR: Failure initializing LittleFS!");
33+
fsHalt("ERROR: Failure initializing LittleFS!",
34+
WS_LED_STATUS_WAITING_FOR_REG_MSG);
3635
}
3736
}
3837

@@ -145,8 +144,9 @@ void WipperSnapper_LittleFS_V2::parseSecrets() {
145144
Error message to print to serial console.
146145
*/
147146
/**************************************************************************/
148-
void WipperSnapper_LittleFS_V2::fsHalt(String msg) {
149-
statusLEDSolid(WS_LED_STATUS_FS_WRITE);
147+
void WipperSnapper_LittleFS_V2::fsHalt(String msg,
148+
ws_led_status_t status_state) {
149+
statusLEDSolid(status_state);
150150
while (1) {
151151
WS_DEBUG_PRINTLN("Fatal Error: Halted execution!");
152152
WS_DEBUG_PRINTLN(msg.c_str());
@@ -167,6 +167,7 @@ void WipperSnapper_LittleFS_V2::GetSDCSPin() {
167167
File file_cfg = LittleFS.open("/config.json");
168168
if (!file_cfg)
169169
WsV2.pin_sd_cs = 255;
170+
170171
error = deserializeJson(WsV2._config_doc, file_cfg);
171172
if (error) {
172173
file_cfg.close();

src/provisioning/littlefs/WipperSnapper_LittleFS_V2.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#define WIPPERSNAPPER_LITTLEFS_V2_H
1717

1818
#include "Wippersnapper_V2.h"
19-
#include <FS.h>
2019
#include <LittleFS.h>
2120

2221
// forward decl.
@@ -32,7 +31,8 @@ class WipperSnapper_LittleFS_V2 {
3231
WipperSnapper_LittleFS_V2();
3332
~WipperSnapper_LittleFS_V2();
3433
void parseSecrets();
35-
void fsHalt(String msg);
34+
void fsHalt(String msg,
35+
ws_led_status_t status_state = WS_LED_STATUS_ERROR_RUNTIME);
3636
void GetSDCSPin();
3737
};
3838
extern Wippersnapper_V2 WsV2;

src/provisioning/sdcard/ws_sdcard.cpp

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,25 @@
1919
@brief Constructs an instance of the Wippersnapper SD card class.
2020
*/
2121
/**************************************************************************/
22-
ws_sdcard::ws_sdcard() {
22+
ws_sdcard::ws_sdcard()
23+
: _sd_spi_cfg(WsV2.pin_sd_cs, DEDICATED_SPI, SPI_SD_CLOCK) {
2324
is_mode_offline = false;
2425
_use_test_data = false;
2526
_sz_cur_log_file = 0;
27+
28+
if (WsV2.pin_sd_cs == PIN_SD_CS_ERROR)
29+
is_mode_offline = false;
30+
31+
if (!_sd.begin(_sd_spi_cfg)) {
32+
WS_DEBUG_PRINTLN(
33+
"SD initialization failed.\nDo not reformat the card!\nIs the card "
34+
"correctly inserted?\nIs there a wiring/soldering problem\n");
35+
is_mode_offline = false;
36+
} else {
37+
// Card initialized - calculate file limits
38+
is_mode_offline = true;
39+
calculateFileLimits();
40+
}
2641
}
2742

2843
/**************************************************************************/
@@ -37,45 +52,26 @@ ws_sdcard::~ws_sdcard() {
3752
}
3853
}
3954

40-
/**************************************************************************/
41-
/*!
42-
@brief Attempts to initialize the SD card and filesystem.
43-
@returns True if the SD card was successfully initialized, False
44-
otherwise.
45-
*/
46-
/**************************************************************************/
47-
bool ws_sdcard::InitSDCard() {
48-
is_mode_offline = false;
49-
csd_t csd;
50-
if (WsV2.pin_sd_cs == PIN_SD_CS_ERROR)
51-
return is_mode_offline;
52-
53-
if (!_sd.begin(WsV2.pin_sd_cs)) {
54-
WS_DEBUG_PRINTLN(
55-
"SD initialization failed.\nDo not reformat the card!\nIs the card "
56-
"correctly inserted?\nIs there a wiring/soldering problem\n");
57-
return is_mode_offline;
58-
}
59-
55+
void ws_sdcard::calculateFileLimits() {
6056
// Calculate the maximum number of log files that can be stored on the SD card
57+
csd_t csd;
6158
if (!_sd.card()->readCSD(&csd)) {
6259
WS_DEBUG_PRINTLN("ERROR: Could not read sdcard information");
63-
return is_mode_offline;
60+
return;
6461
}
62+
6563
// get the complete sdcard capacity in bytes
6664
_sd_capacity = (uint64_t)512 * csd.capacity();
6765
// account for 3-5% fatfs overhead utilization
6866
size_t sd_capacity_usable = _sd_capacity * (1 - 0.05);
69-
// proportionally set sz of each log file to 10% of the SD card's usable capacity
67+
// proportionally set sz of each log file to 10% of the SD card's usable
68+
// capacity
7069
_max_sz_log_file = sd_capacity_usable / 10;
7170
// Regardless of sd card size, cap log files to 512MB
7271
if (_max_sz_log_file > MAX_SZ_LOG_FILE) {
7372
_max_sz_log_file = MAX_SZ_LOG_FILE;
7473
}
7574
_sd_max_num_log_files = sd_capacity_usable / _max_sz_log_file;
76-
77-
is_mode_offline = true;
78-
return is_mode_offline;
7975
}
8076

8177
/**************************************************************************/

src/provisioning/sdcard/ws_sdcard.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,18 @@
2020
#include "Wippersnapper_V2.h"
2121
#include "sdios.h"
2222

23-
#define SD_FAT_TYPE 3 ///< SdFat type (3 = SdFs)
23+
#if defined(ARDUINO_FEATHER_ESP32) || \
24+
defined(ARDUINO_ADAFRUIT_QTPY_ESP32_PICO) || \
25+
defined(ARDUINO_ADAFRUIT_FEATHER_ESP32_V2)
26+
#define SPI_SD_CLOCK \
27+
SD_SCK_MHZ(25) ///< For ESP32/Pico silicon rev 3.0, we clock at 25MHz
28+
#else
29+
#define SPI_SD_CLOCK SD_SCK_MHZ(50) ///< Default SPI clock speed
30+
#endif
31+
32+
#define SD_FAT_TYPE 3 ///< SD type (3 = FAT16/FAT32 and exFAT)
2433
#define PIN_SD_CS_ERROR 255 ///< Error code for invalid SD card CS pin
25-
#define UNKNOWN_VALUE "unknown" ///< Unknown JSON field value
34+
#define UNKNOWN_VALUE "unknown" ///< Default unknown JSON field value
2635
#define MAX_SZ_LOG_FILE (512 * 1024 * 1024) ///< Maximum log file size, in Bytes
2736

2837
// forward decl.
@@ -38,7 +47,8 @@ class ws_sdcard {
3847
public:
3948
ws_sdcard();
4049
~ws_sdcard();
41-
bool InitSDCard();
50+
void calculateFileLimits();
51+
bool isSDCardInitialized() { return is_mode_offline; }
4252
bool parseConfigFile();
4353
bool CreateNewLogFile();
4454
bool isModeOffline() { return is_mode_offline; }
@@ -82,6 +92,7 @@ class ws_sdcard {
8292
bool AddSignalMessageToSharedBuffer(
8393
wippersnapper_signal_BrokerToDevice &msg_signal);
8494

95+
SdSpiConfig _sd_spi_cfg; ///< SPI configuration for the SD card
8596
SdFat _sd; ///< SD object from Adafruit SDFat library
8697
size_t _sd_capacity; ///< Capacity of the SD card, in Bytes
8798
size_t _sz_cur_log_file; ///< Size of the current log file, in Bytes

0 commit comments

Comments
 (0)