Fix trackball click: I2S MCLK was claiming GPIO0; emulator pin-ownership parity

Root cause (on device): sound_engine zero-initialized i2s_pin_config_t,
leaving mck_io_num == 0. i2s_set_pin() then claimed GPIO0 — an
MCLK-capable pin on the S3 and the trackball centre / BOOT line — as
its MCLK output. Verified on device with a diagnostic build: pin0 read
LOW at idle, exactly one latched FALLING edge, clicks never registered.

- sound_engine: set pins.mck_io_num = I2S_PIN_NO_CHANGE explicitly.
- NetDeck: register the trackball click on release with contact-bounce
  collapse (the proven CrtTerminal pattern); pollTouch re-arms when
  isPressed() stays true but getPoint() reports no points (stuck GT911
  IRQ on some units).
- Emulator parity (so this class of bug dies before flashing):
  GPIO-matrix pin-ownership model in the shim — i2s_set_pin() claims
  its pins, claimed pins are dead inputs (digitalRead LOW, no edges,
  loud WARNING on claim/ISR collisions); host pin writes go through
  emu::setPin/togglePin with an edge-accurate ISR latch (a full
  press+release pulse between dispatches no longer gets swallowed);
  E2E inits SDL_INIT_AUDIO so the I2S path actually runs there.
  Regression coverage: e2e/trackball_click (fails on the old engine),
  unit/shim_claimed_pin_is_dead_input,
  unit/shim_edge_survives_same_dispatch_pulse.
- Makefile: firmware step follows the sketch move (root platformio.ini,
  src_dir = src/netdeck).
- HW.md: new notes file for emulator-vs-hardware quirks and the
  on-device debugging recipe.

On-device verified: clicks register in both views (beacon drops, node
routing). Fast layer 23 unit + 7 integration green. Note: e2e
boot_and_controls + network_journey failures are pre-existing at HEAD
(reproduced on a clean stash before this change).
This commit is contained in:
Dejvino
2026-11-15 10:24:30 +01:00
parent 4645c7a1c6
commit 1dcc87d8d4
12 changed files with 368 additions and 30 deletions
+19 -17
View File
@@ -41,6 +41,8 @@ bool emuAutoplay = false;
// ---------------------------------------------------------------------------
namespace emu {
uint8_t pinLevels[64];
const char *pinDrivenBy[64];
std::deque<PinEdge> pinEdges;
bool touchDown = false;
int touchX = 0, touchY = 0;
std::deque<uint8_t> keyboard;
@@ -165,13 +167,13 @@ int main(int argc, char **argv)
if (e.key.repeat) break;
SDL_Keycode k = e.key.key;
switch (k) {
case SDLK_RIGHT: held[0] = true; emu::pinLevels[dirPin[0]] = 0; break;
case SDLK_UP: held[1] = true; emu::pinLevels[dirPin[1]] = 0; break;
case SDLK_LEFT: held[2] = true; emu::pinLevels[dirPin[2]] = 0; break;
case SDLK_DOWN: held[3] = true; emu::pinLevels[dirPin[3]] = 0; break;
case SDLK_RIGHT: held[0] = true; emu::setPin(dirPin[0], 0); break;
case SDLK_UP: held[1] = true; emu::setPin(dirPin[1], 0); break;
case SDLK_LEFT: held[2] = true; emu::setPin(dirPin[2], 0); break;
case SDLK_DOWN: held[3] = true; emu::setPin(dirPin[3], 0); break;
case SDLK_RETURN:
case SDLK_KP_ENTER:
emu::pinLevels[BOARD_BOOT_PIN] = 0; // trackball press
emu::setPin(BOARD_BOOT_PIN, 0); // trackball press
break;
case SDLK_SPACE:
emu::keyboard.push_back(' ');
@@ -202,13 +204,13 @@ int main(int argc, char **argv)
case SDL_EVENT_KEY_UP: {
SDL_Keycode k = e.key.key;
switch (k) {
case SDLK_RIGHT: held[0] = false; emu::pinLevels[dirPin[0]] = 1; break;
case SDLK_UP: held[1] = false; emu::pinLevels[dirPin[1]] = 1; break;
case SDLK_LEFT: held[2] = false; emu::pinLevels[dirPin[2]] = 1; break;
case SDLK_DOWN: held[3] = false; emu::pinLevels[dirPin[3]] = 1; break;
case SDLK_RIGHT: held[0] = false; emu::setPin(dirPin[0], 1); break;
case SDLK_UP: held[1] = false; emu::setPin(dirPin[1], 1); break;
case SDLK_LEFT: held[2] = false; emu::setPin(dirPin[2], 1); break;
case SDLK_DOWN: held[3] = false; emu::setPin(dirPin[3], 1); break;
case SDLK_RETURN:
case SDLK_KP_ENTER:
emu::pinLevels[BOARD_BOOT_PIN] = 1;
emu::setPin(BOARD_BOOT_PIN, 1);
break;
default: break;
}
@@ -267,8 +269,8 @@ int main(int argc, char **argv)
emu::touchX = wx;
emu::touchY = wy;
} else if (e.button.button == SDL_BUTTON_RIGHT) {
emu::pinLevels[BOARD_BOOT_PIN] =
(e.type == SDL_EVENT_MOUSE_BUTTON_DOWN) ? 0 : 1;
emu::setPin(BOARD_BOOT_PIN,
(e.type == SDL_EVENT_MOUSE_BUTTON_DOWN) ? 0 : 1);
}
break;
}
@@ -280,13 +282,13 @@ int main(int argc, char **argv)
// mouse-wheel pulse queues (one toggle per iteration = one ball edge).
for (int d = 0; d < 4; d++) {
if (wheelPulse[d] > 0) {
emu::pinLevels[dirPin[d]] ^= 1;
emu::togglePin(dirPin[d]);
wheelPulse[d]--;
}
}
if (++frame % 3 == 0) {
for (int d = 0; d < 4; d++) {
if (held[d]) emu::pinLevels[dirPin[d]] ^= 1;
if (held[d]) emu::togglePin(dirPin[d]);
}
}
@@ -302,7 +304,7 @@ int main(int argc, char **argv)
const TypeEvt &te = typeEvts[curType];
if (frame >= (uint32_t)te.at) {
if (typePressPin >= 0) {
emu::pinLevels[typePressPin] = 1; // release the Enter press
emu::setPin(typePressPin, 1); // release the Enter press
typePressPin = -1;
}
const char *typeEnd = te.s + strlen(te.s);
@@ -314,7 +316,7 @@ int main(int argc, char **argv)
lastTypeMs = millis();
if (*typeNext == '\n' || *typeNext == '\r') {
typeNext++; // consume the Enter char
emu::pinLevels[BOARD_BOOT_PIN] = 0;
emu::setPin(BOARD_BOOT_PIN, 0);
typePressPin = BOARD_BOOT_PIN;
} else {
emu::keyboard.push_back((uint8_t)*typeNext++);
@@ -333,7 +335,7 @@ int main(int argc, char **argv)
}
static int dir = 0;
if (frame % 45 == 0) dir = (dir + 1) & 3;
if (frame % 4 == 0) emu::pinLevels[dirPin[dir]] ^= 1;
if (frame % 4 == 0) emu::togglePin(dirPin[dir]);
if (frame % 75 == 0) emu::keyboard.push_back('s');
}
#endif