Files
meshcore-tth/variants/rak3401/OnboardRadioSleep.h
T
agessaman 6dc7f2b047 feat(rak3401): auto-sleep optional onboard SX1262
Use the existing RAK3401 firmware roles for RAK4631 cores paired with a
RAK13302. Probe the optional onboard radio and put it into cold sleep
before handing SPI to the primary radio, removing seven duplicate build
environments.

Preserve BUSY startup readiness, release probe pulls before sleep, and
disconnect unused GPIO input buffers during cleanup. Add focused tests
for sequencing, missing signatures, timeouts, and failed sleep readback.

Validated with four native tests, seven firmware-role builds, and repeater
boot checks on RAK4631 + RAK13302. Supply current was not measured.
2026-10-16 19:02:23 -07:00

35 lines
1019 B
C++

#pragma once
#include <string.h>
namespace rak3401 {
enum class OnboardRadioState { Absent, Asleep, BusyTimeout, SleepFailed };
// IO owns the temporary SPI pin assignment; every outcome must release it.
template <typename IO>
OnboardRadioState sleepOnboardRadio(IO& io, const char* expectedVersion) {
io.begin();
io.reset();
const auto state = [&]() {
// A missing chip also times out: BUSY is pulled high until a radio drives it low.
if (!io.waitBusy(false, 20)) return OnboardRadioState::BusyTimeout;
char version[6];
io.readVersion(version);
if (memcmp(version, expectedVersion, sizeof(version)) != 0) {
return OnboardRadioState::Absent;
}
io.releaseProbePulls();
if (!io.waitBusy(false, 20)) return OnboardRadioState::BusyTimeout;
io.sleep();
// Reading over SPI here would wake the radio again. BUSY is a passive check.
return io.waitBusy(true, 20) ? OnboardRadioState::Asleep : OnboardRadioState::SleepFailed;
}();
io.end();
return state;
}
}