tx: lowest documented PA table, test_burst, tx payload logging; 200ms delay improves SDR repeat decode 3%->30%

This commit is contained in:
Lars Nolden
2026-09-01 08:18:17 +02:00
parent 50e8ee2fb8
commit 0df784525a
9 changed files with 211 additions and 63 deletions
+13
View File
@@ -44,6 +44,15 @@ static void handle_set(char *args)
} else if (strcmp(key, "repeat_enabled") == 0) {
g_cfg.repeat_enabled = (val[0] == '1');
ack("set repeat_enabled", true, NULL);
} else if (strcmp(key, "pa_low") == 0) {
sx126x_set_pa_low(val[0] == '1');
repeater_request_radio_reset();
ack("set pa_low", true, "radio reinit queued");
} else if (strcmp(key, "test_lfield") == 0) {
long v = strtol(val, NULL, 10);
if (v < 19 || v > 200) { ack("set", false, "range 19..200"); return; }
repeater_set_test_lfield((uint8_t)v);
ack("set test_lfield", true, NULL);
} else if (strcmp(key, "fem_rx_csd") == 0) {
fem_set_rx_csd(val[0] == '1');
ack("set fem_rx_csd", true, "applies at next RX entry");
@@ -96,6 +105,10 @@ static void console_task(void *arg)
repeater_request_probe();
ack("probe", true, "queued");
}
else if (strcmp(line, "test_burst") == 0) {
repeater_request_test_burst();
ack("test_burst", true, "queued");
}
else if (strcmp(line, "test_tx") == 0) {
repeater_request_test_tx();
ack("test_tx", true, "queued");
+71 -15
View File
@@ -33,7 +33,9 @@ static volatile int64_t s_irq_ts;
static volatile bool s_want_radio_reset;
static volatile bool s_want_test_tx;
static volatile bool s_want_probe;
static volatile bool s_want_test_burst;
static uint8_t s_test_access_nr;
static uint8_t s_test_lfield = 19;
static uint32_t s_seq;
static struct { uint32_t hash; int64_t ts; } s_dup[DUP_RING];
@@ -224,24 +226,33 @@ static const char *do_tx(const uint8_t *payload, int payload_len,
}
static void log_tx(const char *result, uint32_t hash, int len,
int64_t rx_ts, int64_t tx_start, int64_t tx_done)
int64_t rx_ts, int64_t tx_start, int64_t tx_done,
const uint8_t *payload, int payload_len)
{
LOGJ("\"event\":\"wmbus_tx\",\"timestamp_us\":%lld,"
"\"source_rx_timestamp_us\":%lld,\"delay_us\":%lld,"
"\"length\":%d,\"hash\":\"%08" PRIx32 "\","
"\"configured_tx_dbm\":%d,\"airtime_us\":%lld,\"result\":\"%s\"",
(long long)tx_start, (long long)rx_ts,
(long long)(tx_start - rx_ts), len, hash,
(int)g_cfg.tx_power_dbm,
(long long)(tx_done > tx_start ? tx_done - tx_start : 0), result);
printf("{\"seq\":%" PRIu32 ",\"event\":\"wmbus_tx\",\"timestamp_us\":%lld,"
"\"source_rx_timestamp_us\":%lld,\"delay_us\":%lld,"
"\"length\":%d,\"hash\":\"%08" PRIx32 "\","
"\"configured_tx_dbm\":%d,\"airtime_us\":%lld,\"result\":\"%s\"",
s_seq++, (long long)tx_start, (long long)rx_ts,
(long long)(tx_start - rx_ts), len, hash,
(int)g_cfg.tx_power_dbm,
(long long)(tx_done > tx_start ? tx_done - tx_start : 0), result);
if (payload && payload_len > 0) {
printf(",\"tx_payload\":\"");
print_hex(payload, payload_len);
printf("\"");
}
printf("}\n");
fflush(stdout);
}
static void run_test_tx(void)
{
uint8_t frame[64];
int n = wmbus_build_test_frame(frame, sizeof(frame) - 2, s_test_access_nr++);
uint8_t frame[256];
int n = wmbus_build_test_frame(frame, sizeof(frame), s_test_access_nr++,
s_test_lfield);
if (n < 0) return;
uint8_t payload[66];
static uint8_t payload[258];
payload[0] = WMBUS_MODE_BYTE_A_HI;
payload[1] = WMBUS_MODE_BYTE_A_LO;
memcpy(payload + 2, frame, n);
@@ -249,8 +260,9 @@ static void run_test_tx(void)
int64_t t0, t1;
const char *res = do_tx(payload, n + 2, &t0, &t1);
log_tx(res, hash, n, t0, t0, t1);
printf("{\"seq\":%" PRIu32 ",\"event\":\"test_tx_frame\",\"raw\":\"", s_seq++);
log_tx(res, hash, n, t0, t0, t1, NULL, 0);
printf("{\"seq\":%" PRIu32 ",\"event\":\"test_tx_frame\",\"lfield\":%u,\"raw\":\"",
s_seq++, s_test_lfield);
print_hex(frame, n);
printf("\"}\n");
fflush(stdout);
@@ -258,6 +270,44 @@ static void run_test_tx(void)
sx126x_restore_rx_payload_len();
if (!sx126x_set_rx()) recover_ladder("test_tx_rx_restore");
}
// Transmit the SAME frame twice, separated by repeat_delay_ms. This
// reproduces the original+repeat timing pattern without needing a meter,
// isolating "can the observer decode a second identical frame N ms later"
// from anything in the RX path.
static void run_test_burst(void)
{
uint8_t frame[256];
int n = wmbus_build_test_frame(frame, sizeof(frame), s_test_access_nr++,
s_test_lfield);
if (n < 0) return;
static uint8_t payload[258];
payload[0] = WMBUS_MODE_BYTE_A_HI;
payload[1] = WMBUS_MODE_BYTE_A_LO;
memcpy(payload + 2, frame, n);
uint32_t hash = wmbus_hash(frame, n);
printf("{\"seq\":%" PRIu32 ",\"event\":\"test_burst_frame\",\"lfield\":%u,"
"\"delay_ms\":%" PRIu32 ",\"raw\":\"",
s_seq++, s_test_lfield, g_cfg.repeat_delay_ms);
print_hex(frame, n);
printf("\"}\n");
fflush(stdout);
int64_t t0, t1;
const char *r1 = do_tx(payload, n + 2, &t0, &t1);
int64_t first_end = t1 > t0 ? t1 : esp_timer_get_time();
log_tx(r1, hash, n, t0, t0, t1, NULL, 0);
precise_delay_until(first_end + (int64_t)g_cfg.repeat_delay_ms * 1000);
int64_t t2, t3;
const char *r2 = do_tx(payload, n + 2, &t2, &t3);
log_tx(r2, hash, n, first_end, t2, t3, NULL, 0);
sx126x_restore_rx_payload_len();
if (!sx126x_set_rx()) recover_ladder("test_burst_rx_restore");
}
// ---------------------------------------------------------------- probe
static void hexdump_json(const char *key, const uint8_t *d, int n)
@@ -394,7 +444,7 @@ static void handle_rx(int64_t rxdone_ts)
int64_t t0, t1;
const char *res = do_tx(txbuf, total + 2, &t0, &t1);
remember_dup(hash, esp_timer_get_time());
log_tx(res, hash, total, frame_end_ts, t0, t1);
log_tx(res, hash, total, frame_end_ts, t0, t1, txbuf, total + 2);
// Always return to RX
sx126x_restore_rx_payload_len();
@@ -438,6 +488,10 @@ static void repeater_task(void *arg)
s_want_test_tx = false;
run_test_tx();
}
if (s_want_test_burst) {
s_want_test_burst = false;
run_test_burst();
}
if (s_want_probe) {
s_want_probe = false;
run_probe();
@@ -477,6 +531,8 @@ void repeater_start(void)
void repeater_request_radio_reset(void) { s_want_radio_reset = true; }
void repeater_request_test_tx(void) { s_want_test_tx = true; }
void repeater_request_probe(void) { s_want_probe = true; }
void repeater_set_test_lfield(uint8_t l) { s_test_lfield = l; }
void repeater_request_test_burst(void) { s_want_test_burst = true; }
// ------------------------------------------------------------------ status
+2
View File
@@ -37,6 +37,8 @@ void repeater_start(void);
void repeater_request_radio_reset(void);
void repeater_request_test_tx(void);
void repeater_request_probe(void);
void repeater_set_test_lfield(uint8_t l);
void repeater_request_test_burst(void);
void repeater_print_status(void);
void repeater_print_stats(void);
void repeater_print_radio_status(void);
+11 -2
View File
@@ -56,6 +56,7 @@ static sx126x_irq_cb_t s_irq_cb;
static uint8_t s_rx_payload_len = 255;
static int8_t s_tx_dbm = -9;
static SemaphoreHandle_t s_lock; // serializes SPI access across tasks
static bool s_pa_low = true; // true = +10 dBm-optimal PA row (lowest output)
// ---------------------------------------------------------------- low level
@@ -303,8 +304,14 @@ bool sx126x_config_wmbus_c1(uint32_t freq_hz, uint8_t rx_payload_len)
uint8_t f[4] = {frf >> 24, frf >> 16, frf >> 8, frf};
ok &= cmd(OP_SET_RF_FREQUENCY, f, 4);
// PA config for SX1262, +14 dBm optimal table (we run far below that)
ok &= cmd(OP_SET_PA_CONFIG, (uint8_t[]){0x02, 0x02, 0x00, 0x01}, 4);
// PA config. SX1262 datasheet table 13-21 documents optimal settings
// per target power; we deliberately use the +10 dBm-optimal row
// (paDutyCycle=0x01, hpMax=0x00) because it yields the lowest output
// this chip supports, and the external KCT8103L PA adds gain after it.
// s_pa_low=false selects the +14 dBm row for comparison experiments.
ok &= s_pa_low
? cmd(OP_SET_PA_CONFIG, (uint8_t[]){0x01, 0x00, 0x00, 0x01}, 4)
: cmd(OP_SET_PA_CONFIG, (uint8_t[]){0x02, 0x02, 0x00, 0x01}, 4);
ok &= sx126x_set_tx_power(s_tx_dbm);
// OCP
@@ -488,3 +495,5 @@ uint8_t sx126x_get_status(void)
if (!xfer(tx, rx, 2)) return 0;
return rx[1];
}
void sx126x_set_pa_low(bool low) { s_pa_low = low; }
+1
View File
@@ -58,6 +58,7 @@ bool sx126x_raw(uint8_t op, const uint8_t *args, size_t n_tx,
void fem_set_tx(bool tx);
void fem_power(bool on);
void fem_set_rx_csd(bool en); // LNA experiment: FEM CSD level in RX
void sx126x_set_pa_low(bool low); // PA table: true = lowest documented output
// Registered by caller before init; invoked from DIO1 GPIO ISR.
typedef void (*sx126x_irq_cb_t)(int64_t timestamp_us);
+25 -13
View File
@@ -97,13 +97,16 @@ uint32_t wmbus_hash(const uint8_t *data, size_t len)
return h;
}
int wmbus_build_test_frame(uint8_t *out, size_t maxlen, uint8_t access_nr)
int wmbus_build_test_frame(uint8_t *out, size_t maxlen, uint8_t access_nr,
uint8_t lfield)
{
// Format A frame: L C M(2) A(6) | CRC | CI ...data... | CRC
// Format A frame: L C M(2) A(6) | CRC | CI ...data... | CRC per block
// Manufacturer "SLN" = ((19)<<10)|(12<<5)|(14) = 0x4D8E (Solenos test)
// ID 0x00000001 (BCD 00000001), version 0x01, device type 0x07 (water)
uint8_t body[26];
int L = 19; // C M A CI + 10 data bytes = 1+2+6+1+9... keep L=19: C(1)+M(2)+A(6)+CI(1)+data(9)
uint8_t body[256];
if (lfield < 19) lfield = 19;
if (lfield > 200) lfield = 200;
int L = lfield;
body[0] = (uint8_t)L;
body[1] = 0x44; // SND_NR
body[2] = 0x8E; body[3] = 0x4D; // manufacturer, little endian
@@ -111,13 +114,16 @@ int wmbus_build_test_frame(uint8_t *out, size_t maxlen, uint8_t access_nr)
body[8] = 0x01; // version
body[9] = 0x07; // device type
body[10] = 0x78; // CI: response, no header
// 9 data bytes: marker + access counter
// data: marker + access counter + PRBS-ish padding
body[11] = 0x53; body[12] = 0x4F; body[13] = 0x4C; body[14] = 0x54; // "SOLT"
body[15] = 0x58; // "X"
body[16] = access_nr;
body[17] = 0xA5; body[18] = 0x5A; body[19] = access_nr ^ 0xFF;
uint8_t x = 0xA5 ^ access_nr;
for (int i = 17; i <= L; i++) {
x = (uint8_t)(x * 197 + 31);
body[i] = x;
}
// assemble with block CRCs
int total = wmbus_frame_total_len(WMBUS_FRAME_A, (uint8_t)L);
if (total < 0 || (size_t)total > maxlen) return -1;
@@ -125,11 +131,17 @@ int wmbus_build_test_frame(uint8_t *out, size_t maxlen, uint8_t access_nr)
for (int i = 0; i < 10; i++) out[i] = body[i];
uint16_t c1 = wmbus_crc16(out, 10);
out[10] = c1 >> 8; out[11] = c1 & 0xFF;
// block 2: bytes 10..(L) of body (CI + data = L-9 bytes) + crc
int n2 = L - 9;
for (int i = 0; i < n2; i++) out[12 + i] = body[10 + i];
uint16_t c2 = wmbus_crc16(out + 12, n2);
out[12 + n2] = c2 >> 8; out[13 + n2] = c2 & 0xFF;
// blocks 2..n: 16 data bytes + crc each (last block short)
int src = 10; // next body byte (CI onward), body holds L+1 bytes
int dst = 12;
while (src <= L) {
int n = L + 1 - src;
if (n > 16) n = 16;
for (int i = 0; i < n; i++) out[dst + i] = body[src + i];
uint16_t c = wmbus_crc16(out + dst, n);
out[dst + n] = c >> 8; out[dst + n + 1] = c & 0xFF;
src += n;
dst += n + 2;
}
return total;
}
+2 -1
View File
@@ -31,7 +31,8 @@ uint16_t wmbus_crc16(const uint8_t *data, size_t len);
// Build a valid C-mode format A test frame with our test manufacturer/ID.
// Returns total frame length written to out (>= 12), or -1 if payload_len
// does not fit. out[0] = L-field.
int wmbus_build_test_frame(uint8_t *out, size_t maxlen, uint8_t access_nr);
int wmbus_build_test_frame(uint8_t *out, size_t maxlen, uint8_t access_nr,
uint8_t lfield);
// 32-bit FNV-1a hash used to correlate RX/TX events in the logs.
uint32_t wmbus_hash(const uint8_t *data, size_t len);