Use nRF52 profile clock only for dual-radio scanning
This commit is contained in:
1 parent
1e1ee2618a
commit
69c2e4855f
4 files changed
+128
-27
No files matched your search
@@ -0,0 +1,74 @@
|
||||
"""Exercise the production profile clock on and off both nRF52 and other boards."""
|
||||
from pathlib import Path
|
||||
import os
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
SOURCE = (ROOT / "src/helpers/radiolib/RadioLibWrappers.cpp").read_text()
|
||||
CLOCK = "static void syncProfileClock(bool dual_profile) {" + SOURCE.split(
|
||||
"static void syncProfileClock(bool dual_profile) {", 1
|
||||
)[1].split("// this function is called", 1)[0]
|
||||
|
||||
HARNESS = r'''
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
struct FakeRtc {
|
||||
uint32_t TASKS_STOP = 0, PRESCALER = 9, TASKS_CLEAR = 0, TASKS_START = 0;
|
||||
uint32_t COUNTER = 0;
|
||||
} rtc;
|
||||
#define NRF_RTC2 (&rtc)
|
||||
uint32_t micros() { return 1234; }
|
||||
@CLOCK@
|
||||
int main() {
|
||||
assert(profileTimestamp(false) == 1234);
|
||||
assert(rtc.TASKS_START == 0 && rtc.TASKS_STOP == 0);
|
||||
assert(profileElapsedUs(12, 42, false) == 30);
|
||||
#if defined(NRF52_PLATFORM)
|
||||
rtc.COUNTER = 100;
|
||||
assert(profileTimestamp(true) == 100);
|
||||
assert(rtc.TASKS_STOP == 1 && rtc.PRESCALER == 0);
|
||||
assert(rtc.TASKS_CLEAR == 1 && rtc.TASKS_START == 1);
|
||||
rtc.TASKS_CLEAR = rtc.TASKS_START = rtc.TASKS_STOP = 0;
|
||||
assert(profileTimestamp(true) == 100);
|
||||
assert(rtc.TASKS_CLEAR == 0 && rtc.TASKS_START == 0 && rtc.TASKS_STOP == 0);
|
||||
assert(profileElapsedUs(0, 32768, true) == 1000000);
|
||||
assert(profileElapsedUs(0x00fffff0, 0x10, true) == 977);
|
||||
syncProfileClock(false);
|
||||
assert(rtc.TASKS_STOP == 1 && profileTimestamp(false) == 1234);
|
||||
rtc.TASKS_CLEAR = rtc.TASKS_START = rtc.TASKS_STOP = 0;
|
||||
syncProfileClock(false);
|
||||
assert(rtc.TASKS_CLEAR == 0 && rtc.TASKS_START == 0 && rtc.TASKS_STOP == 0);
|
||||
rtc.COUNTER = 200;
|
||||
assert(profileTimestamp(true) == 200);
|
||||
assert(rtc.TASKS_CLEAR == 1 && rtc.TASKS_START == 1);
|
||||
#else
|
||||
assert(profileTimestamp(true) == 1234);
|
||||
assert(profileElapsedUs(12, 42, true) == 30);
|
||||
assert(rtc.TASKS_START == 0 && rtc.TASKS_STOP == 0);
|
||||
#endif
|
||||
}
|
||||
'''
|
||||
|
||||
|
||||
class ProfileClockTest(unittest.TestCase):
|
||||
def test_clock_selection_and_lifecycle(self):
|
||||
with tempfile.TemporaryDirectory() as folder:
|
||||
cpp = Path(folder) / "test.cpp"
|
||||
exe = Path(folder) / "test.exe"
|
||||
cpp.write_text(HARNESS.replace("@CLOCK@", CLOCK))
|
||||
for defines in ([], ["-DNRF52_PLATFORM=1"]):
|
||||
result = subprocess.run(
|
||||
[os.environ.get("CXX", "g++"), "-std=c++17", "-Wall", "-Wextra",
|
||||
"-Werror", *defines, str(cpp), "-o", str(exe)],
|
||||
capture_output=True, text=True,
|
||||
)
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
result = subprocess.run([str(exe)], capture_output=True, text=True)
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -24,8 +24,10 @@ namespace mesh { enum class RadioParamApplyResult { APPLIED, BUSY, FAILED }; }
|
||||
static uint64_t elapsed_us;
|
||||
static uint8_t state;
|
||||
uint32_t micros() { return (uint32_t)elapsed_us; }
|
||||
uint32_t profileTimestamp() { return micros(); }
|
||||
uint32_t profileElapsedUs(uint32_t since, uint32_t now) { return now - since; }
|
||||
static bool profile_clock_active=false;
|
||||
void syncProfileClock(bool dual_profile) { profile_clock_active=dual_profile; }
|
||||
uint32_t profileTimestamp(bool) { return micros(); }
|
||||
uint32_t profileElapsedUs(uint32_t since, uint32_t now, bool) { return now - since; }
|
||||
uint32_t millis() { return (uint32_t)(elapsed_us / 1000); }
|
||||
struct Chip { bool standbyXOSC=false; int standby() { return 0; } };
|
||||
using CustomSX1262 = Chip;
|
||||
@@ -59,7 +61,7 @@ struct RadioLibWrapper {
|
||||
state=STATE_RX; elapsed_us=100000;
|
||||
auto& p=_profiles.primary;
|
||||
p.freq=_cur_freq; p.bw=_cur_bw; p.sf=_cur_sf; p.cr=_cur_cr;
|
||||
_profile_visit_stamp=profileTimestamp();
|
||||
_profile_visit_stamp=profileTimestamp(_profiles.enabled());
|
||||
}
|
||||
bool isChipBusy() { return busy; }
|
||||
bool isInRecvMode() const { return (state & ~STATE_INT_READY) == STATE_RX; }
|
||||
@@ -71,7 +73,7 @@ struct RadioLibWrapper {
|
||||
void startRecv() {
|
||||
if (failRxStarts) { --failRxStarts; state=STATE_IDLE; return; }
|
||||
if (_profile_rxps_suspended) assert(chip.standbyXOSC && !_rx_ps_enabled);
|
||||
state=STATE_RX; _profile_visit_stamp=profileTimestamp(); _rx_ps_armed=_rx_ps_enabled;
|
||||
state=STATE_RX; _profile_visit_stamp=profileTimestamp(_profiles.enabled()); _rx_ps_armed=_rx_ps_enabled;
|
||||
}
|
||||
void stopReceiveDutyCycle() { _rx_ps_armed=false; }
|
||||
bool applyParams(float f,float,uint8_t,uint8_t) { ++applies; elapsed_us+=switch_apply_us; return !fail || f==909.5f; }
|
||||
@@ -100,6 +102,15 @@ struct RadioLibWrapper {
|
||||
@METHODS@
|
||||
int main() {
|
||||
using Result=mesh::RadioParamApplyResult;
|
||||
{
|
||||
RadioLibWrapper w;
|
||||
w.enable();
|
||||
assert(profile_clock_active);
|
||||
w._cw_active=true;
|
||||
w._profiles.setSecondary({},false);
|
||||
w.serviceProfileScan();
|
||||
assert(!profile_clock_active); // even a carrier-wave early return stops RTC2
|
||||
}
|
||||
{
|
||||
RadioLibWrapper w; w.enable();
|
||||
assert(w._profiles.switchTestReady());
|
||||
@@ -275,7 +286,7 @@ int main() {
|
||||
w.enable();
|
||||
w._floor_estimator.add(-100, millis());
|
||||
w._active_profile=0;
|
||||
w._profile_visit_stamp=profileTimestamp();
|
||||
w._profile_visit_stamp=profileTimestamp(w._profiles.enabled());
|
||||
const auto primary_visit=w._profiles.listenUs(0);
|
||||
elapsed_us += primary_visit - 1; w.serviceProfileScan(); assert(w._active_profile==0);
|
||||
elapsed_us++; w.serviceProfileScan(); assert(w._active_profile==1);
|
||||
|
||||
Reference in new issue
Block a user