Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ jobs:

- name: Coverage report
run: |
output=$(gcovr --root . --filter 'src/' --exclude 'src/SnipsController\.ino' --exclude 'src/oled\.cpp' --exclude 'src/rgb_led\.cpp' --exclude 'src/calibration_store\.cpp' --exclude 'src/droid_persistence\.cpp' --exclude 'src/xbee_spi\.cpp' --exclude 'src/xbee_control\.cpp' --exclude 'src/display_layout_store\.cpp' --exclude 'src/accelerometer\.cpp' --exclude 'src/power_config_store\.cpp' --print-summary --fail-under-line 90)
output=$(gcovr --root . --filter 'src/' --exclude 'src/SnipsController\.ino' --exclude 'src/oled\.cpp' --exclude 'src/rgb_led\.cpp' --exclude 'src/calibration_store\.cpp' --exclude 'src/droid_persistence\.cpp' --exclude 'src/xbee_spi\.cpp' --exclude 'src/xbee_uart\.cpp' --exclude 'src/xbee_control\.cpp' --exclude 'src/display_layout_store\.cpp' --exclude 'src/accelerometer\.cpp' --exclude 'src/power_config_store\.cpp' --print-summary --fail-under-line 90)
status=$?
{
echo '### Coverage (src/, excluding SnipsController.ino and hardware adapters)'
Expand Down Expand Up @@ -155,5 +155,8 @@ jobs:
- name: Install PlatformIO
run: pip install --upgrade platformio

- name: Build esp32s3 firmware
- name: Build esp32s3 firmware (V1)
run: pio run -e esp32s3

- name: Build esp32s3_v2 firmware (V2)
run: pio run -e esp32s3_v2
2 changes: 1 addition & 1 deletion PCB/v2.x/snips/GPIO_table.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Pins are chosen by position, not carried over from V1, so V2 needs its own firmw
- **TXD0 (IO43)** prints the boot log, so it stays free for debugging. RGB data has moved off it, so the LED no longer sees that traffic.
- **Boot glitches:** the low-level power-up glitches on IO1-IO17 (Table 2-2, where IO15 and IO16 appear as XTAL_32K) are 60 µs. Those pins carry only inputs, except IO1 (SLEEP_RQ, where low means awake). XBee RESET is on IO42, which has no listed glitch.
- **IO26 stays unconnected.** It is the PSRAM chip-select, and Arduino runs a PSRAM probe at every boot, so it is driven before `setup()`. The probe fails on the -N8, which has no PSRAM, and boot continues.
- **Do not flash V1 firmware on V2.** It would treat V2 pins as SPI and read the wrong buttons. V2 needs its own pin map.
- **Do not flash V1 firmware on V2.** It would treat V2 pins as SPI and read the wrong buttons. Build V2 firmware with `pio run -e esp32s3_v2`, which uses V2's pin map in `include/pin_assignment.h`.
- **EN:** 10 kΩ and 1 µF, as Espressif advises (Peripheral Schematics), so EN crosses 0.75 × VDD about 14 ms after the rail is up. V1 has no capacitor.
- **Pad 61:** the thermal pad is 9 segments, soldered to GND.

Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,12 @@ This is yet to be determined since we're so early in the prototype stage, but th
Requires [PlatformIO](https://platformio.org/) (CLI or IDE extension).

```bash
# Build the firmware
# Build the firmware for V1.x boards (the default)
pio run

# Build the firmware for V2 boards
pio run -e esp32s3_v2

# Run unit tests (no hardware required)
pio test -e native
```
Expand Down
88 changes: 82 additions & 6 deletions include/pin_assignment.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,89 @@
#pragma once

// Raw GPIO pin assignments for the Snips Controller (ESP32-S3-WROOM-1).
// Values here are Arduino GPIO numbers (e.g. 16 means GPIO16), not the
// module's physical pin numbers used in the PCB docs.
// Raw GPIO pin assignments for the Snips Controller. Values here are
// Arduino GPIO numbers (e.g. 16 means GPIO16), not the module's physical
// pin numbers used in the PCB docs.
//
// This file is pure reference data — it should stay a 1:1 mirror of
// PCB/v1.x/GPIO_table.md. Cross-reference that file for the physical-pin-to-GPIO
// mapping, reserved pins, and the rationale behind each assignment.
// Two boards, picked at build time:
// - V1.x (ESP32-S3-WROOM-1, `pio run -e esp32s3`): a 1:1 mirror of
// PCB/v1.x/GPIO_table.md. The XBee is on SPI.
// - V2 (ESP32-S3-MINI-1U, `pio run -e esp32s3_v2`, which defines
// SNIPS_BOARD_V2): a 1:1 mirror of PCB/v2.x/snips/GPIO_table.md. The
// XBee is on an optional carrier board, over UART.
// The two maps are not interchangeable: V1 firmware on a V2 board would
// read the wrong buttons. Cross-reference the GPIO tables for the
// physical-pin-to-GPIO mapping, reserved pins, and the rationale behind
// each assignment.

namespace PinAssignment {

#if defined(SNIPS_BOARD_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
// CTS high as "no XBee" (see xbee_uart.h).
constexpr int kXbeeUartTx = 35; // carrier pad 2, XBee DIN
constexpr int kXbeeUartRx = 36; // carrier pad 3, XBee DOUT
constexpr int kXbeeCts = 34; // carrier pad 1
constexpr int kXbeeRts = 39; // carrier pad 4
constexpr int kXbeeOnSleep = 41; // carrier pad 5
// Carrier pad 6. Drive open drain only: low, or released (input). The
// pull-up is on the carrier.
constexpr int kXbeeReset = 42;
// Carrier pad 7, XBee DTR/SLEEP_RQ. Low = stay awake; set it as one of the
// first instructions in setup(), same as on V1.
constexpr int kXbeeSleepRq = 1;

// ---- I2C (OLED, ICM-42670-P IMU) ------------------------------------------
constexpr int kI2cSda = 12;
constexpr int kI2cScl = 13;

// ---- IMU interrupt (ICM-42670-P INT1) --------------------------------------
// No longer on a strap pin, unlike V1's GPIO46.
constexpr int kAccelInterrupt = 37;

// ---- ADC1 (analog trigger, thumbstick, battery sense) -----------------------
constexpr int kAnalogTrigger = 9;
constexpr int kThumbstickX = 7;
constexpr int kThumbstickY = 6;
constexpr int kVsysSense = 10; // divide-by-3, same as V1

// ---- Digital buttons -------------------------------------------------------
// Same names and meanings as V1; only the GPIOs moved. V2 has one bumper.
constexpr int kVolUp = 38;
constexpr int kVolDown = 40;
constexpr int kTriggerUp = 33;
constexpr int kTriggerDown = 18;
constexpr int kDigitalTrigger = 47; // bumper
constexpr int kThumbstickClick = 17;
constexpr int kMacro1 = 16;
constexpr int kMacro2 = 5;
constexpr int kMacro3 = 4;
constexpr int kMacro4 = 11;
constexpr int kMacro5 = 8;
constexpr int kMacro6 = 2;

// ---- Power -------------------------------------------------------------------
// RTC-domain pin, like V1's GPIO2.
constexpr int kPowerButtonSense = 21;
// RXD0. Its pull-up holds the latch through any reset or USB upload.
// Firmware drives it high early in boot and low to power off, and must
// never start UART0 receive on it (Serial is native USB, so it doesn't).
constexpr int kPowerLatchHold = 44;

// ---- Charging (bq25185) ----------------------------------------------------
constexpr int kChargeStat1 = 15;
constexpr int kChargeStat2 = 14;

// ---- RGB status LED (SK6812, RMT-driven) -------------------------------------
// Moved off TXD0, which prints the boot log.
constexpr int kRgbLedData = 48;

#else // V1.x


// ---- SPI (XBee) --------------------------------------------------------
constexpr int kXbeeSpiSck = 10;
constexpr int kXbeeSpiMosi = 11;
Expand Down Expand Up @@ -71,4 +145,6 @@ constexpr int kChargeStat2 = 41;
// ---- RGB status LED (SK6812/WS2812, RMT-driven) ---------------------------
constexpr int kRgbLedData = 43;

#endif // SNIPS_BOARD_V2

} // namespace PinAssignment
42 changes: 42 additions & 0 deletions include/xbee_api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#pragma once

#include <cstdint>

#include "xbee_link.h"

// The XBee API operations this firmware uses, over any XbeeLink: local AT
// commands, sending our application payload to the coordinator, and
// polling for payloads from it. Pure: the clock and the idle hook are
// passed in, so native tests can drive it with a fake link.
class XbeeApi {
public:
using ClockFn = unsigned long (*)();
using IdleFn = void (*)();

XbeeApi(XbeeLink *link, ClockFn clock, IdleFn idle)
: link_(link), clock_(clock), idle_(idle) {}

// 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.
bool sendAtCommand(const char *atCmd, const uint8_t *value,
uint8_t valueLength, uint8_t *outValue,
uint8_t outValueCapacity, uint8_t *outValueLength,
unsigned long timeoutMs = 200);

// Sends our own application payload to the coordinator as a Transmit
// Request (0x10). Fire-and-forget: the XBee's own ACK/retry handles
// reliability at the radio level.
void sendPacket(const uint8_t *payload, uint16_t payloadLength);

// Reads at most one queued frame, returning the payload of a Receive
// Packet (0x90). Frames of any other type are discarded. Returns false
// if nothing usable was read this call.
bool pollForPacket(uint8_t *outPayload, uint16_t outPayloadCapacity,
uint16_t *outPayloadLength);

private:
XbeeLink *link_;
ClockFn clock_;
IdleFn idle_;
};
31 changes: 22 additions & 9 deletions include/xbee_control.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,26 @@
#include <cstddef>

#include "droid_switcher.h"
#include "xbee_api.h"
#include "xbee_role.h"
#if defined(SNIPS_BOARD_V2)
#include "xbee_uart.h"
#else
#include "xbee_spi.h"
#endif

// Real XbeeTransport (see droid_switcher.h), over the SPI transport in
// xbee_spi.h. Exact AT command sequencing (NR for leave, ID for the new
// Real XbeeTransport (see droid_switcher.h), over the board's XBee link:
// SPI on V1 (xbee_spi.h), UART to the optional carrier on V2
// (xbee_uart.h). Exact AT command sequencing (NR for leave, ID for the new
// PAN, AC to apply and trigger rejoin) is this class's best-effort
// reading of Digi's XBee3 manual, not yet validated against real
// hardware — confirm during this PR's bring-up and adjust here if the
// sequence needs correcting.
class XbeeControl : public XbeeTransport, public XbeeRoleTransport {
public:
void begin() { spi_.begin(); }
XbeeControl();

void begin() { link_.begin(); }

bool leaveNetwork() override;
bool setPanId(const char *panId) override;
Expand Down Expand Up @@ -50,18 +58,23 @@ class XbeeControl : public XbeeTransport, public XbeeRoleTransport {
// Returns false on query failure, leaving outDbm untouched.
bool queryLocalRssiDbm(int *outDbm);

// Passthroughs to the owned XbeeSpi — see xbee_spi.h. XbeeControl is
// kept as the single facade over the one physical XBee connection
// rather than SnipsController.ino owning a second XbeeSpi instance.
// Passthroughs to the API over the owned link — see xbee_api.h.
// XbeeControl is kept as the single facade over the one physical XBee
// connection rather than SnipsController.ino owning a second one.
void sendPacket(const uint8_t *payload, uint16_t payloadLength) {
spi_.sendPacket(payload, payloadLength);
api_.sendPacket(payload, payloadLength);
}
bool pollForPacket(uint8_t *outPayload, uint16_t outPayloadCapacity,
uint16_t *outPayloadLength) {
return spi_.pollForPacket(outPayload, outPayloadCapacity,
return api_.pollForPacket(outPayload, outPayloadCapacity,
outPayloadLength);
}

private:
XbeeSpi spi_;
#if defined(SNIPS_BOARD_V2)
XbeeUart link_;
#else
XbeeSpi link_;
#endif
XbeeApi api_;
};
37 changes: 37 additions & 0 deletions include/xbee_frame_reader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once

#include <cstdint>

// Assembles XBee API frames (AP=1, unescaped) from a byte stream, one byte
// at a time, for the UART link (xbee_uart.h), where bytes arrive whenever
// they arrive rather than a frame per SPI read. Pure. Bytes before a 0x7E
// delimiter are skipped; a 0x7E inside a frame is just data, since the
// length says where the frame ends.
class XbeeFrameReader {
public:
static constexpr uint16_t kMaxFrameLength = 128;

enum class Result {
kNeedMore, // no complete frame yet
kFrame, // frame() / length() hold a frame with a valid checksum
kBadFrame, // a frame ended with a bad checksum, or its length was
// impossible; dropped, and the reader is looking for the
// next delimiter
};

Result feed(uint8_t byte);

// Only meaningful right after feed() returns kFrame.
const uint8_t *frame() const { return data_; }
uint16_t length() const { return length_; }

// Drops any partial frame, e.g. after changing the baud rate.
void reset() { state_ = State::kDelimiter; }

private:
enum class State { kDelimiter, kLengthHigh, kLengthLow, kData, kChecksum };
State state_ = State::kDelimiter;
uint8_t data_[kMaxFrameLength] = {};
uint16_t length_ = 0;
uint16_t received_ = 0;
};
24 changes: 24 additions & 0 deletions include/xbee_link.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <cstdint>

// One complete XBee API frame at a time, over whichever wire the board
// uses: SPI on V1 (xbee_spi.h), UART on V2 (xbee_uart.h). Frame data here
// excludes the 0x7E delimiter, length and checksum; the link adds and
// checks those. XbeeApi (xbee_api.h) builds everything else on top.
class XbeeLink {
public:
virtual ~XbeeLink() = default;

// True if at least one frame may be waiting to be read.
virtual bool frameAvailable() = 0;

// Reads one frame's data into buf. Returns its length; -1 if no frame is
// ready after all (the caller should stop draining); or 0 if a frame was
// found but unusable (bad length or checksum), with the stream still in
// sync, so the caller should keep going.
virtual int32_t readFrame(uint8_t *buf, uint16_t maxLen) = 0;

// Writes one complete frame (delimiter, length, data, checksum).
virtual void writeFrame(const uint8_t *frameData, uint16_t length) = 0;
};
54 changes: 15 additions & 39 deletions include/xbee_spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,26 @@

#include <cstdint>

#include "xbee_link.h"

// Thin hardware adapter for the XBee3's SPI interface (AP=1/unescaped API
// mode). Frame envelope/checksum logic lives in xbee_frame.h (pure,
// tested); this file only does real SPI transfers, CS toggling, and ATTN
// polling — mirrors thePunderWoman/Amidala's proven xbee_spi.cpp read
// path (same module family, same SPI settings), extended with a write
// path and a blocking AT-command round trip. Excluded from native
// build/coverage (see platformio.ini's [env:native] build_src_filter).
class XbeeSpi {
// mode), used on V1 boards. Frame envelope/checksum logic lives in
// xbee_frame.h and the API operations in xbee_api.h (both pure, tested);
// this file only does real SPI transfers, CS toggling, and ATTN polling —
// mirrors thePunderWoman/Amidala's proven xbee_spi.cpp read path (same
// module family, same SPI settings), extended with a write path. Excluded
// from native build/coverage (see platformio.ini's [env:native]
// build_src_filter).
class XbeeSpi : public XbeeLink {
public:
void begin();

// True if the module has at least one frame queued (ATTN asserted).
bool frameAvailable();

// Reads one queued frame's data (excludes the 0x7E/length header and
// trailing checksum) into buf. Returns the length on success, -1 if no
// start delimiter was found at all (ATTN may be stuck low with nothing
// actually queued — caller should stop draining), or 0 if a delimiter
// was found but the frame was unusable (bad length or checksum — the
// SPI stream is still in sync, so the caller should keep going).
int32_t readFrame(uint8_t *buf, uint16_t maxLen);

// Writes one complete frame (0x7E + length + frameData + checksum).
void writeFrame(const uint8_t *frameData, uint16_t length);

// Sends a local AT command and blocks (up to timeoutMs) reading frames
// until the matching (by frameId) AT Command Response arrives. Returns
// false on send failure, an unmatched/malformed response, or timeout.
bool sendAtCommand(const char *atCmd, const uint8_t *value,
uint8_t valueLength, uint8_t *outValue,
uint8_t outValueCapacity, uint8_t *outValueLength,
unsigned long timeoutMs = 200);
bool frameAvailable() override;

// Sends our own application payload to the coordinator as a Transmit
// Request (0x10) — non-blocking, fire-and-forget (no response is
// expected or waited for; XBee's own ACK/retry handles reliability at
// the radio level).
void sendPacket(const uint8_t *payload, uint16_t payloadLength);
// See XbeeLink. -1 means no start delimiter was found at all (ATTN may be
// stuck low with nothing actually queued).
int32_t readFrame(uint8_t *buf, uint16_t maxLen) override;

// Non-blocking: drains any queued frames, returning the payload of the
// first Receive Packet (0x90) found (frames of any other type are
// silently discarded — this firmware only expects AT Command Responses,
// handled synchronously by sendAtCommand(), or Receive Packets here).
// Returns false if nothing was available this call.
bool pollForPacket(uint8_t *outPayload, uint16_t outPayloadCapacity,
uint16_t *outPayloadLength);
void writeFrame(const uint8_t *frameData, uint16_t length) override;
};
Loading
Loading