aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--test_fw/README.md29
-rw-r--r--test_fw/src/main.cpp62
2 files changed, 77 insertions, 14 deletions
diff --git a/test_fw/README.md b/test_fw/README.md
new file mode 100644
index 0000000..4ad7d16
--- /dev/null
+++ b/test_fw/README.md
@@ -0,0 +1,29 @@
+# ocularium test firmware
+
+This is Arduino-based test firmware written to derisk systems quickly before moving to a Rust
+implementation.
+
+## SD bug (?)
+
+This firmware _doesn't_ work with the stock `SD.h` that ships with the RP2040 Arduino core. It needs
+the following change at the end of `Sd2Card.cpp:cardCommand`:
+
+```c++
+ // XXX: Assume CMD0 succeeds.
+ if (cmd == CMD0)
+ {
+ spiRec();
+ status_ = 0x1;
+ }
+```
+
+With this, it works perfectly, but without, it gets stuck unable to ever initialize the card.
+
+Logic analyzer says the card (I've tried multiple brands) sends an `0x7f` (general error) in the
+byte after the CMD0 sequence completes, and _then_ sends the expected `0x01` R1 in the following
+byte. The SD library is looking for any byte starting with a 0 bit (indicating message presence), so
+treats this as a genuine error and always fails. If we change the implementation to ignore this
+byte, the rest of the initialization succeeds without error.
+
+Currently, I'm just applying a transient patch to my local PlatformIO download of the RP2040 core to
+resolve this temporarily.
diff --git a/test_fw/src/main.cpp b/test_fw/src/main.cpp
index 8a6b3c4..6ffa9de 100644
--- a/test_fw/src/main.cpp
+++ b/test_fw/src/main.cpp
@@ -38,7 +38,8 @@ static VEML lux(Wire1);
static Adafruit_LSM6DSL lsm;
#define SCAN_I2C 0
-#define ENABLE_SD 0
+#define ENABLE_SD 1
+#define DEBUG_SD 0
#define SDCARD_SPI SD_SPI
#include <SD.h>
@@ -107,9 +108,12 @@ static QueueHandle_t bme_buffer_handle;
#endif
void setup() {
-#if !USE_RTT
- Serial.begin(115200);
+#if USE_RTT
+ rtt.trimDownBufferFull();
+ rtt.trimUpBufferFull();
#endif
+
+ LOGGER.begin(115200);
LOGGER.println("boot");
bringup::init_buses();
@@ -135,7 +139,6 @@ void setup() {
#endif
bringup::boot_animation();
- delay(100);
}
@@ -246,10 +249,19 @@ void loop() {
sd_connected = false;
}
- if (millis() - sd_last_connect > sd_period && card_detected)
+ if (millis() - sd_last_connect > sd_period && card_detected && !sd_connected)
{
sd_last_connect = millis();
+#if 0
+ Sd2Card card;
+ auto result = card.init(3, SD_CS);
+
+ if (!result)
+ {
+ LOGGER.printf("failed to connect to sd card, code: %d, data: %d", card.errorCode(), card.errorData());
+ }
+#else
if (!sd.begin(SD_CS))
{
LOGGER.println("failed connection to sd card");
@@ -258,6 +270,7 @@ void loop() {
{
sd_connected = true;
}
+#endif
}
#endif
@@ -331,22 +344,39 @@ void loop() {
};
- for (const auto &[name, value] : readings)
+#if !DEBUG_SD
+ if (LOGGER)
{
- // This awkwardness is to support the fact that the RTT implementation
- // doesn't support formatting floats and instead silently fails -- manually
- // format first, then printf the string.
- char buf[32];
- auto n = snprintf(buf, sizeof(buf), "%s:%f,", name, value);
+ for (const auto &[name, value] : readings)
+ {
+ // This awkwardness is to support the fact that the RTT implementation
+ // doesn't support formatting floats and instead silently fails -- manually
+ // format first, then printf the string.
+ char buf[32];
+ snprintf(buf, sizeof(buf), "%s:%f,", name, value);
- LOGGER.printf("%s", buf);
- }
+ LOGGER.printf("%s", buf);
+ }
- LOGGER.println();
+ LOGGER.println();
+ }
+#endif
#if ENABLE_SD
if (sd_connected) {
auto data = sd.open("data.csv", O_CREAT | O_WRITE | O_APPEND);
+ if (data.size() == 0)
+ {
+ data.write("uptime_ms,");
+
+ for (const auto &[name, _] : readings)
+ {
+ data.write(name);
+ data.write(",");
+ }
+
+ data.write("\n");
+ }
data.write(String(millis()).c_str());
@@ -358,6 +388,10 @@ void loop() {
data.write("\n");
data.close();
+
+#if DEBUG_SD
+ LOGGER.println("wrote entry to sd");
+#endif
}
#endif
}