diff --git a/PCB/v2.x/snips/README.md b/PCB/v2.x/snips/README.md index 1f1b131..74d8fd1 100644 --- a/PCB/v2.x/snips/README.md +++ b/PCB/v2.x/snips/README.md @@ -21,7 +21,7 @@ A STEP model of the assembled board is in [`step/v2.0/snips_controller.step`](st - **Module:** an ESP32-S3-MINI-1U-N8 on the top, between the macro buttons and USB, turned 270 degrees. The 19.2 mm WROOM-1U does not fit the 17.5 mm there. The turn puts the pinless GND edge on the side and the signal pins toward the stick, the OLED and the carrier; pins are assigned by position, as `GPIO_table.md` explains. USB runs about 15 mm as a result, which is well within full-speed limits. Bluetooth and ESP-NOW work with no other radio. - **XBee:** optional. U_XBEE1 is a row of 12 hand-solder pads on the bottom for a 22 x 17 mm castellated daughter board, which carries the XBee 3 Micro, its decoupling, the RESET pull-up and RC, and the ATTN/BOOTMODE pull-up. The link is UART with flow control, not SPI. RTS and DTR also allow XBee serial firmware recovery through the ESP32. Leave the area inside the pads free of parts; vias there stay tented. See `GPIO_table.md`. - **Reflow:** the bottom side goes first and the top side last, because Espressif requires a single reflow for the module (datasheet section 12.3). The XBee reflows on its own carrier and never goes upside down, which is what stopped the two modules from sharing this board before. -- **Radio link:** Amidala's droids use XBee Zigbee 3.0, so a Zigbee controller needs the carrier. The firmware still has to move from the XBee's SPI interface to UART, and needs a V2 pin map. +- **Radio link:** Amidala's droids use XBee Zigbee 3.0, so a Zigbee controller needs the carrier. V2 firmware builds with `pio run -e esp32s3_v2` (upload with `-t upload`). It talks to the XBee over UART in API mode and runs without the carrier. - **Programming:** USB, through the module's native USB. To force download mode, hold SW_BOOT1 while pressing the power button. RXD0's pull-up holds the latch with no firmware, so a blank board flashes after a 1 s press, with no button held during the upload. - **No reset button:** the power button turns the board on, and only firmware turns it off. PWR_HOLD is on RXD0, whose pull-up holds the latch through any reset or USB upload, so a blank or hung board stays on until USB and the battery are removed. Keep a watchdog running to recover from a hang. C_LATCH_FB1 gives the latch hysteresis, so that pull-up cannot restart the board at power-off, and R_PWR_EN_PD1 is 22 kΩ so a rising gate cannot couple through it and lift EN. - **Buttons:** all 13 connect directly to module GPIOs. See `GPIO_table.md`. @@ -114,8 +114,8 @@ The droids use XBee Zigbee 3.0, so the controller joins that network through the Recommended bringup order: 1. Current-limited supply on VSYS, with no carrier fitted: press power for 1 s, then check 3V3 and idle current. -2. Flash over USB with no button held, with SW_BOOT1 held at power-on if the chip does not enumerate. Confirm `PWR_HOLD` reads high. -3. Time the shortest press that stays on, at 3.3 V and 4.2 V. The simulation gives under 0.4 s at worst, so a 1 s press is well clear. Run a USB upload with no button held, then confirm a watchdog reset reboots the board. Power off at least 20 times, on USB and on battery, and scope `PWR_EN` and `PWR_LATCH_G`: the board must stay off every time, since the simulation's margin has not been measured. +2. Flash `pio run -e esp32s3_v2 -t upload` over USB with no button held, with SW_BOOT1 held at power-on if the chip does not enumerate. Never flash the V1 `esp32s3` build. The serial monitor's first line should read `Snips Controller V2, firmware , reset: `, and with no carrier, `XBee carrier not found (CTS high)`. Confirm `PWR_HOLD` reads high. +3. Time the shortest press that stays on, at 3.3 V and 4.2 V. The simulation gives under 0.4 s at worst, so a 1 s press is well clear. Run a USB upload with no button held, then type `hang` into the serial monitor: the board should reboot within about 5 s and log `reset: task watchdog`. Power off at least 20 times, on USB and on battery, and scope `PWR_EN` and `PWR_LATCH_G`: the board must stay off every time, since the simulation's margin has not been measured. 4. OLED and ICM-42670-P on I2C. 5. SK6812 RGB LED. 6. Buttons, using the pin table. @@ -132,7 +132,7 @@ Recommended bringup order: - Boot: press the power button for about a second → latch enables 3V3 → RXD0's pull-up holds the latch through D_OR2 as soon as the chip has power → firmware drives RXD0 high. - Shutdown: firmware sees a hold on IO21 → graceful shutdown → RXD0 low → the gate decays through R_LATCH_G1 → C_LATCH_FB1 kicks it clear of threshold as PWR_EN falls → power cut after about 0.8-2.6 s, counted from release if the button is still held. - bq25185 STAT1 and STAT2 go to IO15 and IO14 (open drain, 10kΩ pull-ups to 3V3). -- Battery voltage: VSYS through R_VSYS1/R_VSYS2 to IO7, reading VSYS/3. +- Battery voltage: VSYS through R_VSYS1/R_VSYS2 to IO10, reading VSYS/3. - Below about 3.3 V on the battery the buck passes it through, and the DRV5055's minimum is 3.0 V, so the firmware cutoff should sit above that. ## Power Management diff --git a/include/pin_assignment.h b/include/pin_assignment.h index 09e626b..150ace6 100644 --- a/include/pin_assignment.h +++ b/include/pin_assignment.h @@ -19,7 +19,10 @@ namespace PinAssignment { #if defined(SNIPS_BOARD_V2) -// ---- UART (XBee carrier, pads 1-6) ---------------------------------------- +// Printed at boot, so the serial log shows which pin map was flashed. +constexpr const char *kBoardName = "V2"; + +// ---- UART (XBee carrier, pads 1-6)---------------------------------------- // The ESP32 transmits on the XBee's DIN and receives on its DOUT. CTS is // the XBee's output (low = ready); RTS is its input. With no carrier fitted // these float, so the transport pulls CTS, DOUT and ON/SLEEP up and treats @@ -83,6 +86,7 @@ constexpr int kRgbLedData = 48; #else // V1.x +constexpr const char *kBoardName = "V1.x"; // ---- SPI (XBee) -------------------------------------------------------- constexpr int kXbeeSpiSck = 10; diff --git a/include/serial_command.h b/include/serial_command.h new file mode 100644 index 0000000..699d8d7 --- /dev/null +++ b/include/serial_command.h @@ -0,0 +1,28 @@ +#pragma once + +#include + +// Bring-up commands typed into the USB serial monitor, one per line. Pure: +// loop() feeds it whatever bytes arrived, and acts on what it returns. +enum class SerialCommand { + kNone, + // "hang": stop feeding the loop watchdog, to check that the watchdog + // reboots the board. A hung V2 board can't be switched off (RXD0's + // pull-up holds the power latch), so the watchdog is the only way out. + kHang, +}; + +class SerialCommandReader { + public: + static constexpr size_t kMaxLineLength = 15; + + // Feeds one received byte. Returns the command when `c` ends a line + // ('\r' or '\n') that names one; kNone otherwise. Matching ignores case. + // A line longer than kMaxLineLength is dropped whole. + SerialCommand feed(char c); + + private: + char line_[kMaxLineLength + 1] = {}; + size_t length_ = 0; + bool overflowed_ = false; +}; diff --git a/include/xbee_api.h b/include/xbee_api.h index d567c96..53c4f59 100644 --- a/include/xbee_api.h +++ b/include/xbee_api.h @@ -18,7 +18,8 @@ class XbeeApi { // Sends a local AT command and waits (up to timeoutMs) for the matching // (by frame ID) AT Command Response. Returns false on a build failure, - // a non-OK status, or timeout. outValue/outValueLength may be null. + // a non-OK status, or timeout, and at once, without writing anything, + // if the link isn't ready(). outValue/outValueLength may be null. bool sendAtCommand(const char *atCmd, const uint8_t *value, uint8_t valueLength, uint8_t *outValue, uint8_t outValueCapacity, uint8_t *outValueLength, diff --git a/include/xbee_link.h b/include/xbee_link.h index 185567f..5a5ddb2 100644 --- a/include/xbee_link.h +++ b/include/xbee_link.h @@ -10,6 +10,10 @@ class XbeeLink { public: virtual ~XbeeLink() = default; + // False when there is no module to talk to (V2 with no carrier fitted), + // so AT commands fail at once instead of waiting out their timeout. + virtual bool ready() const { return true; } + // True if at least one frame may be waiting to be read. virtual bool frameAvailable() = 0; diff --git a/include/xbee_uart.h b/include/xbee_uart.h index bc0ff38..8469f26 100644 --- a/include/xbee_uart.h +++ b/include/xbee_uart.h @@ -28,6 +28,7 @@ class XbeeUart : public XbeeLink, public XbeeUartPort { public: void begin(); bool present() const { return present_; } + bool ready() const override { return present_ || setUp_; } bool frameAvailable() override; int32_t readFrame(uint8_t *buf, uint16_t maxLen) override; diff --git a/src/SnipsController.ino b/src/SnipsController.ino index 40ab28f..f85e29f 100644 --- a/src/SnipsController.ino +++ b/src/SnipsController.ino @@ -12,6 +12,7 @@ #include "calibration_store.h" #include "display_layout_store.h" #include "droid_persistence.h" +#include "firmware_version.h" #include "frame_buffer.h" #include "icm42670p_registers.h" #include "imu.h" @@ -28,6 +29,7 @@ #include "screen.h" #include "screen_brightness.h" #include "sequence_banner.h" +#include "serial_command.h" #include "snips_logo_bitmap.h" #include "status_led.h" #include "status_screen.h" @@ -65,6 +67,7 @@ PowerConfig powerConfig; PowerState previousPowerState; bool xbeeSleepRequested = false; LowBatteryMonitor lowBatteryMonitor; +SerialCommandReader serialCommandReader; bool wasInLowBatteryCountdown = false; char deviceSerialLowBuf[9] = {}; // must outlive setup() — see its use below bool lastReportedPressed[Buttons::kCount] = {}; @@ -248,6 +251,10 @@ void performGracefulShutdown() { delay(kShutdownRetryDelayMs); } + // Off before the spin below: the rail can stay up for seconds while the + // power button is still held, and a watchdog reset then would bring the + // board straight back up (RXD0's pull-up re-holds the latch on V2). + disableLoopWDT(); digitalWrite(PinAssignment::kPowerLatchHold, LOW); // The rail should collapse almost immediately; spin here rather than // falling back into loop() in an undefined half-shutdown state in case @@ -266,6 +273,31 @@ const char *chargeStateName(ChargeState state) { } } +const char *resetReasonName(esp_reset_reason_t reason) { + switch (reason) { + case ESP_RST_POWERON: return "power-on"; + case ESP_RST_SW: return "software"; + case ESP_RST_PANIC: return "panic"; + case ESP_RST_INT_WDT: return "interrupt watchdog"; + case ESP_RST_TASK_WDT: return "task watchdog"; + case ESP_RST_WDT: return "other watchdog"; + case ESP_RST_BROWNOUT: return "brownout"; + default: return "other"; // includes the USB reset that ends an upload + } +} + +// Bring-up commands from the serial monitor (see serial_command.h). +void handleSerialCommands() { + while (Serial.available() > 0) { + if (serialCommandReader.feed(static_cast(Serial.read())) == + SerialCommand::kHang) { + Serial.println("Hanging: the watchdog should reboot the board in 5 s."); + while (true) { + } + } + } +} + } // namespace void setup() { @@ -315,6 +347,12 @@ void setup() { delay(10); } + // Proof of which build is flashed, and why the chip last reset (a + // watchdog reset shows up here). + Serial.printf("Snips Controller %s, firmware %s, reset: %s\n", + PinAssignment::kBoardName, Firmware::kVersion, + resetReasonName(esp_reset_reason())); + // If the battery is already at/below the critical threshold the // instant the device is turned on (e.g. it sat unused long enough to // self-discharge), don't run a full boot — show a brief message and @@ -476,11 +514,21 @@ void setup() { delay(300); } rgbLed.show(statusColor(SystemState::kDisconnected)); + + // Reboots the board if loop() stops returning for 5 s (the task + // watchdog's timeout and panic setting come from the Arduino core's + // sdkconfig). Needed on V2 above all: with no reset button and RXD0's + // pull-up holding the latch, a hung board otherwise stays on until USB + // and the battery are removed. Started only now, since setup()'s XBee + // configuration can block for several seconds. + enableLoopWDT(); } void loop() { const unsigned long now = millis(); + handleSerialCommands(); + const bool powerButtonHeld = digitalRead(PinAssignment::kPowerButtonSense) == LOW; diff --git a/src/serial_command.cpp b/src/serial_command.cpp new file mode 100644 index 0000000..c2c0e54 --- /dev/null +++ b/src/serial_command.cpp @@ -0,0 +1,22 @@ +#include "serial_command.h" + +#include +#include + +SerialCommand SerialCommandReader::feed(char c) { + if (c != '\r' && c != '\n') { + if (length_ < kMaxLineLength) { + line_[length_++] = + static_cast(std::tolower(static_cast(c))); + } else { + overflowed_ = true; + } + return SerialCommand::kNone; + } + + line_[length_] = '\0'; + const bool hang = !overflowed_ && std::strcmp(line_, "hang") == 0; + length_ = 0; + overflowed_ = false; + return hang ? SerialCommand::kHang : SerialCommand::kNone; +} diff --git a/src/xbee_api.cpp b/src/xbee_api.cpp index e47f9f5..aaac579 100644 --- a/src/xbee_api.cpp +++ b/src/xbee_api.cpp @@ -7,6 +7,10 @@ bool XbeeApi::sendAtCommand(const char *atCmd, const uint8_t *value, uint8_t outValueCapacity, uint8_t *outValueLength, unsigned long timeoutMs) { + // No module: fail now. Waiting out the timeout here would stall loop() + // for 200 ms on every once-a-second RSSI query. + if (!link_->ready()) return false; + constexpr uint8_t kFrameId = 0x01; uint8_t requestFrame[64]; const uint16_t requestLength = XbeeFrame::buildAtCommandFrame( diff --git a/test/test_serial_command/test_serial_command.cpp b/test/test_serial_command/test_serial_command.cpp new file mode 100644 index 0000000..1a41b06 --- /dev/null +++ b/test/test_serial_command/test_serial_command.cpp @@ -0,0 +1,75 @@ +#include + +#include "serial_command.h" + +void setUp(void) {} +void tearDown(void) {} + +namespace { + +// Feeds `text` and returns the last non-kNone command, or kNone. +SerialCommand feedAll(SerialCommandReader *reader, const char *text) { + SerialCommand last = SerialCommand::kNone; + for (const char *p = text; *p != '\0'; ++p) { + const SerialCommand command = reader->feed(*p); + if (command != SerialCommand::kNone) last = command; + } + return last; +} + +} // namespace + +void test_hang_line_returns_hang() { + SerialCommandReader reader; + TEST_ASSERT_TRUE(feedAll(&reader, "hang\n") == SerialCommand::kHang); +} + +void test_command_only_fires_at_end_of_line() { + SerialCommandReader reader; + TEST_ASSERT_TRUE(feedAll(&reader, "hang") == SerialCommand::kNone); + TEST_ASSERT_TRUE(reader.feed('\r') == SerialCommand::kHang); +} + +void test_crlf_fires_once() { + SerialCommandReader reader; + TEST_ASSERT_TRUE(reader.feed('h') == SerialCommand::kNone); + feedAll(&reader, "ang"); + TEST_ASSERT_TRUE(reader.feed('\r') == SerialCommand::kHang); + TEST_ASSERT_TRUE(reader.feed('\n') == SerialCommand::kNone); +} + +void test_matching_ignores_case() { + SerialCommandReader reader; + TEST_ASSERT_TRUE(feedAll(&reader, "HaNg\n") == SerialCommand::kHang); +} + +void test_other_lines_are_ignored() { + SerialCommandReader reader; + TEST_ASSERT_TRUE(feedAll(&reader, "hangs\n") == SerialCommand::kNone); + TEST_ASSERT_TRUE(feedAll(&reader, "han\n") == SerialCommand::kNone); + TEST_ASSERT_TRUE(feedAll(&reader, "\n\n") == SerialCommand::kNone); + // The reader recovers for the next line. + TEST_ASSERT_TRUE(feedAll(&reader, "hang\n") == SerialCommand::kHang); +} + +void test_overlong_line_is_dropped_whole() { + SerialCommandReader reader; + // Its first kMaxLineLength characters are not "hang", and its tail must + // not be read as the start of a line. + TEST_ASSERT_TRUE(feedAll(&reader, "xxxxxxxxxxxxxxxhang\n") == + SerialCommand::kNone); + TEST_ASSERT_TRUE(feedAll(&reader, "hangxxxxxxxxxxxxxxxx\n") == + SerialCommand::kNone); + TEST_ASSERT_TRUE(feedAll(&reader, "hang\n") == SerialCommand::kHang); +} + +int main(int argc, char **argv) { + UNITY_BEGIN(); + RUN_TEST(test_hang_line_returns_hang); + RUN_TEST(test_command_only_fires_at_end_of_line); + RUN_TEST(test_crlf_fires_once); + RUN_TEST(test_matching_ignores_case); + RUN_TEST(test_other_lines_are_ignored); + RUN_TEST(test_overlong_line_is_dropped_whole); + return UNITY_END(); +} diff --git a/test/test_xbee_api/test_xbee_api.cpp b/test/test_xbee_api/test_xbee_api.cpp index 628897a..ff275c0 100644 --- a/test/test_xbee_api/test_xbee_api.cpp +++ b/test/test_xbee_api/test_xbee_api.cpp @@ -13,7 +13,9 @@ class FakeLink : public XbeeLink { std::vector> incoming; std::vector> written; int badFrames = 0; // returned as 0 before the queued frames + bool isReady = true; + bool ready() const override { return isReady; } bool frameAvailable() override { return badFrames > 0 || !incoming.empty(); } int32_t readFrame(uint8_t *buf, uint16_t maxLen) override { if (badFrames > 0) { @@ -104,6 +106,20 @@ void test_at_command_times_out_with_no_reply() { TEST_ASSERT_TRUE(gIdles >= 20); } +// Regression: on a V2 board with no carrier, each AT command used to wait +// out its whole timeout, so the once-a-second RSSI query froze loop() for +// 200 ms every second. +void test_at_command_fails_at_once_when_link_not_ready() { + FakeLink link; + link.isReady = false; + link.incoming.push_back(atResponse(0x01, "DB", 0, {0x2A})); + XbeeApi api(&link, fakeClock, fakeIdle); + uint8_t value = 0; + TEST_ASSERT_FALSE(api.sendAtCommand("DB", nullptr, 0, &value, 1, nullptr, 200)); + TEST_ASSERT_EQUAL(0, gIdles); + TEST_ASSERT_EQUAL(0, (int)link.written.size()); +} + void test_at_command_truncates_value_to_capacity() { FakeLink link; link.incoming.push_back(atResponse(0x01, "ID", 0, {1, 2, 3, 4})); @@ -173,6 +189,7 @@ int main(int argc, char **argv) { RUN_TEST(test_at_command_skips_other_frames_and_ids); RUN_TEST(test_at_command_error_status_fails); RUN_TEST(test_at_command_times_out_with_no_reply); + RUN_TEST(test_at_command_fails_at_once_when_link_not_ready); RUN_TEST(test_at_command_truncates_value_to_capacity); RUN_TEST(test_send_packet_wraps_payload_in_transmit_request); RUN_TEST(test_send_packet_drops_oversized_payload);