Files
wadamesh/variants/sensecap_indicator/target.cpp
T
Confituurke 8acb68415d feat(sensecap-indicator): SX1262 behind the TCA9535 via a RadioLib HAL
The SX1262's NSS, RESET, BUSY and DIO1 sit on the TCA9535 (P0.0..P0.3)
while SCK/MISO/MOSI are real GPIOs. IndicatorRadioHal derives from
ArduinoHal, keeps hardware FSPI at 4 MHz and maps the virtual pins
0x40..0x43 onto expander transfers; every other pin goes straight to
the Arduino pin and interrupt functions, as ArduinoHal does. The
meshcomod core is used as is: CustomSX1262 and its wrapper run
unchanged on top of the HAL.

DIO1 has no GPIO edge. The expander's open-drain /INT on GPIO42 has an
IRAM ISR that only notifies a dispatch task, because the read needs
I2C and the bus mutex. That task reads both input bytes under the bus
lock: the TCA9535 releases /INT only when the port that changed is
read, and several port-1 inputs float, so a port-0-only read could
leave GPIO42 low for good. Every port-0 byte feeds a small edge
detector (Dio1Edge) through the expander's port-0 observer, which
calls the callback RadioLib attached to DIO1 once per rise. That
callback is the core's setFlag, so TX-done and RX-done reach the core
exactly as they would from a DIO1 interrupt.

A BUSY poll is a port-0 read as well, and it clears /INT. Sampling DIO1
in those reads means a rise that lands while RadioLib waits on BUSY
fires the callback from that read instead of being lost; the dispatch
task's next read sees the level already high and does not fire again.
The task also re-reads every 50 ms, so a rise whose /INT was missed
still fires the callback, at most 50 ms late. MESH_RADIO_DIO1_POLLED is
not set: polling the IRQ register would add an SPI transfer, and with
it expander writes for NSS, on every pass of the receive path.

Floating port-1 inputs can keep /INT toggling, each toggle an I2C read
at high priority. The task counts ISR wakes and timeouts and logs them
for each of the first six 10 s windows, then only above 500 wakes/s
and at most once a minute, so the idle wake rate is visible at
bring-up without flooding the console later.

TCXO or crystal comes from the radio strap read at boot: the env's
SX126X_DIO3_TCXO_VOLTAGE calls indicatorTcxoVoltage(), which returns
2.4 V for the TCXO radio and 0 for the crystal one. radio_init() starts
the radio with the strap's setting and, if that fails, with the other
one, because std_init only falls back from TCXO to 0 V and a TCXO board
whose strap misread as crystal would never start otherwise. main.cpp's
one retry runs both again. The log says which voltage was requested and
which one the radio came up with.

The board reports GPIO42 (indicator::kExpanderIntGpio, shared with the
HAL) as its IRQ line and never light-sleeps: the inherited ESP32Board
versions would treat the virtual DIO1 pin as a GPIO, and /INT is active
low while the light-sleep wake source is high-level.

test_indicator_dio1 covers the virtual-pin routing and the
once-per-rise rule across dispatch and BUSY reads; run-host-tests.sh
gets its include path.

Signed-off-by: Confituurke <9595028+Confituurke@users.noreply.github.com>
2026-10-17 22:39:29 +02:00

62 lines
2.1 KiB
C++

// SPDX-License-Identifier: GPL-3.0-or-later
#include <Arduino.h>
#include "target.h"
#include "IndicatorIo.h"
#include "IndicatorRadioHal.h"
IndicatorBoard board;
// The radio's NSS/RESET/BUSY/DIO1 sit on the TCA9535; the P_LORA_* values are
// virtual pins that IndicatorRadioHal resolves to expander bits. SCK/MISO/MOSI
// are real GPIOs on FSPI.
static SPIClass radioSpi(FSPI);
static IndicatorRadioHal radio_hal(radioSpi);
RADIO_CLASS radio = new Module(&radio_hal, P_LORA_NSS, P_LORA_DIO_1, P_LORA_RESET, P_LORA_BUSY);
WRAPPER_CLASS radio_driver(radio, board);
ESP32RTCClock fallback_clock;
ClockFloorRTC rtc_clock(fallback_clock);
EnvironmentSensorManager sensors;
IndicatorDisplay display;
MomentaryButton user_btn(PIN_USER_BTN, 1000, true);
// The strap picks the TCXO setting. std_init only falls back from TCXO to 0 V,
// so a TCXO board whose strap misreads as crystal would never start: a failed
// start tries the other setting before giving up. main.cpp's retry after a
// transient failure runs both again.
static bool s_tcxoAlternate = false;
float indicatorTcxoVoltage() {
const bool tcxo = IndicatorIo::radioHasTcxo() != s_tcxoAlternate;
return tcxo ? 2.4f : 0.0f;
}
static bool radioStart(bool alternate) {
s_tcxoAlternate = alternate;
Serial.printf("[indicator] radio tcxo requested=%.1f V (strap: %s%s)\n", indicatorTcxoVoltage(),
IndicatorIo::radioHasTcxo() ? "tcxo" : "crystal",
alternate ? ", trying the other setting" : "");
// std_init starts FSPI on GPIO41/47/48; main.cpp runs display.begin() first,
// and the panel init has handed GPIO41/48 back by then.
const bool ok = radio.std_init(&radioSpi);
// std_init and RadioLib can each drop to 0 V on their own; tcxoVoltage holds
// the value the last begin() ran with.
Serial.printf("[indicator] radio %s, tcxo used=%.1f V\n", ok ? "up" : "init failed",
radio.tcxoVoltage);
return ok;
}
bool radio_init() {
fallback_clock.begin();
rtc_clock.begin(Wire);
return radioStart(false) || radioStart(true);
}
mesh::LocalIdentity radio_new_identity() {
RadioNoiseListener rng(radio);
return mesh::LocalIdentity(&rng);
}