fix(rak3401): sleep onboard SX1262 before boot voltage check
Move the onboard radio probe into RAK3401Board::begin() so a low-voltage SYSTEMOFF at boot no longer leaves the RAK4631's onboard SX1262 in standby. Add board startup tests and run them in CI.
This commit is contained in:
1 parent
6dc7f2b047
commit
277cae93ac
9 files changed
+157
-6
No files matched your search
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include "../../mocks/Arduino.h"
|
||||
|
||||
#define NRF52_POWER_MANAGEMENT 1
|
||||
#define PWRMGT_LPCOMP_AIN 3
|
||||
#define PWRMGT_LPCOMP_REFSEL 4
|
||||
#define PWRMGT_VOLTAGE_BOOTLOCK 3300
|
||||
#define SX126X_POWER_EN 21
|
||||
#define INPUT 0
|
||||
#define OUTPUT 1
|
||||
#define LOW 0
|
||||
#define HIGH 1
|
||||
|
||||
void pinMode(uint32_t pin, uint32_t mode);
|
||||
void digitalWrite(uint32_t pin, uint32_t value);
|
||||
void analogReadResolution(uint32_t bits);
|
||||
uint32_t analogRead(uint32_t pin);
|
||||
@@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
struct StartupWire {
|
||||
void begin() {}
|
||||
};
|
||||
inline StartupWire Wire;
|
||||
@@ -0,0 +1,44 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#define SHUTDOWN_REASON_LOW_VOLTAGE 0x4C
|
||||
#define SHUTDOWN_REASON_BOOT_PROTECT 0x42
|
||||
|
||||
struct PowerMgtConfig {
|
||||
uint8_t lpcomp_ain_channel;
|
||||
uint8_t lpcomp_refsel;
|
||||
uint16_t voltage_bootlock;
|
||||
};
|
||||
|
||||
namespace startup_test {
|
||||
extern std::vector<std::string> events;
|
||||
extern bool permitBoot;
|
||||
struct SystemOff { uint8_t reason; };
|
||||
}
|
||||
|
||||
class NRF52Board {
|
||||
protected:
|
||||
bool checkBootVoltage(const PowerMgtConfig*) {
|
||||
startup_test::events.push_back("voltage");
|
||||
if (!startup_test::permitBoot) initiateShutdown(SHUTDOWN_REASON_BOOT_PROTECT);
|
||||
return true;
|
||||
}
|
||||
virtual void initiateShutdown(uint8_t reason) { enterSystemOff(reason); }
|
||||
void configureVoltageWake(uint8_t, uint8_t) {}
|
||||
void enterSystemOff(uint8_t reason) {
|
||||
startup_test::events.push_back("off");
|
||||
throw startup_test::SystemOff{reason};
|
||||
}
|
||||
public:
|
||||
explicit NRF52Board(const char* = nullptr) {}
|
||||
virtual uint16_t getBattMilliVolts() = 0;
|
||||
virtual const char* getManufacturerName() const = 0;
|
||||
};
|
||||
|
||||
class NRF52BoardDCDC : virtual public NRF52Board {
|
||||
public:
|
||||
void begin() {}
|
||||
};
|
||||
@@ -0,0 +1,70 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <array>
|
||||
// Exercise the real startup code with a power-management stub that stops at SYSTEMOFF.
|
||||
#include "../../variants/rak3401/RAK3401Board.cpp"
|
||||
|
||||
namespace startup_test {
|
||||
std::vector<std::string> events;
|
||||
bool permitBoot;
|
||||
std::array<uint32_t, 48> outputs;
|
||||
rak3401::OnboardRadioState radioState;
|
||||
}
|
||||
|
||||
void pinMode(uint32_t, uint32_t) {}
|
||||
void digitalWrite(uint32_t pin, uint32_t value) { startup_test::outputs.at(pin) = value; }
|
||||
void analogReadResolution(uint32_t) {}
|
||||
uint32_t analogRead(uint32_t) { return 2500; }
|
||||
|
||||
rak3401::OnboardRadioState rak3401::sleepOnboardRadio() {
|
||||
startup_test::events.push_back("onboard");
|
||||
return startup_test::radioState;
|
||||
}
|
||||
|
||||
class RakBoardStartup : public testing::Test {
|
||||
protected:
|
||||
void SetUp() override {
|
||||
startup_test::events.clear();
|
||||
startup_test::outputs.fill(HIGH);
|
||||
startup_test::permitBoot = true;
|
||||
startup_test::radioState = rak3401::OnboardRadioState::Asleep;
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(RakBoardStartup, OnboardRadioHandledBeforeVoltageCheck) {
|
||||
RAK3401Board board;
|
||||
board.begin();
|
||||
EXPECT_EQ(startup_test::events, (std::vector<std::string>{"onboard", "voltage"}));
|
||||
EXPECT_EQ(startup_test::outputs.at(SX126X_POWER_EN), HIGH);
|
||||
EXPECT_EQ(startup_test::outputs.at(PIN_3V3_EN), HIGH);
|
||||
}
|
||||
|
||||
TEST_F(RakBoardStartup, LowVoltageExitStillHandlesOnboardRadio) {
|
||||
startup_test::permitBoot = false;
|
||||
RAK3401Board board;
|
||||
try {
|
||||
board.begin();
|
||||
FAIL() << "Expected protective shutdown";
|
||||
} catch (const startup_test::SystemOff& off) {
|
||||
EXPECT_EQ(off.reason, SHUTDOWN_REASON_BOOT_PROTECT);
|
||||
}
|
||||
EXPECT_EQ(startup_test::events, (std::vector<std::string>{"onboard", "voltage", "off"}));
|
||||
EXPECT_EQ(startup_test::outputs.at(SX126X_POWER_EN), LOW);
|
||||
EXPECT_EQ(startup_test::outputs.at(PIN_3V3_EN), LOW);
|
||||
}
|
||||
|
||||
TEST_F(RakBoardStartup, OptionalRadioFailuresDoNotBypassBootProtection) {
|
||||
for (auto state : {rak3401::OnboardRadioState::Absent, rak3401::OnboardRadioState::BusyTimeout,
|
||||
rak3401::OnboardRadioState::SleepFailed}) {
|
||||
startup_test::events.clear();
|
||||
startup_test::permitBoot = false;
|
||||
startup_test::radioState = state;
|
||||
RAK3401Board board;
|
||||
EXPECT_THROW(board.begin(), startup_test::SystemOff);
|
||||
EXPECT_EQ(startup_test::events, (std::vector<std::string>{"onboard", "voltage", "off"}));
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
Reference in new issue
Block a user