Add repeater protocol and device firmware features

This commit is contained in:
Lars Nolden
2026-09-21 12:30:50 +02:00
parent 73d3ddf00c
commit a11a4b3b89
41 changed files with 7791 additions and 228 deletions
+36 -14
View File
@@ -55,8 +55,8 @@ static spi_device_handle_t s_spi;
static sx126x_irq_cb_t s_irq_cb;
static uint8_t s_rx_payload_len = 255;
static int8_t s_tx_dbm = -9;
static bool s_tx_fdev_t1; // deviation currently programmed for T-mode TX
static SemaphoreHandle_t s_lock; // serializes SPI access across tasks
static bool s_pa_low = true; // true = +10 dBm-optimal PA row (lowest output)
// Preamble detector length. Shorter = more sensitive but more false syncs,
// and every false sync costs one fixed-255-byte RX window (~12 ms).
// SX126x codes: 0x00 off, 0x04 = 8 bits, 0x05 = 16, 0x06 = 24, 0x07 = 32.
@@ -308,15 +308,11 @@ 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. 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);
// SX1262 datasheet table 13-21: +14 dBm is the lowest valid optimal
// PA row for SX1262. The superficially lower (0x01, 0x00) row is the
// +10 dBm SX1261 row and selecting it with deviceSel=SX1262 costs about
// 32 dB on this board. The external KCT8103L supplies the remaining gain.
ok &= cmd(OP_SET_PA_CONFIG, (uint8_t[]){0x02, 0x02, 0x00, 0x01}, 4);
// OCP
uint8_t ocp = BOARD_SX1262_OCP;
@@ -337,6 +333,7 @@ bool sx126x_config_wmbus_c1(uint32_t freq_hz, uint8_t rx_payload_len)
0x00, 0xB8, 0x52, // fdev
};
ok &= cmd(OP_SET_MODULATION, mod, 8);
s_tx_fdev_t1 = false;
// Sync word = wM-Bus T/C access code 0x543D. The two C-mode "mode bytes"
// (54CD = frame A, 543D = frame B) are received/sent as payload so that
@@ -384,7 +381,7 @@ bool sx126x_restore_rx_payload_len(void)
bool sx126x_set_tx_power(int8_t dbm)
{
if (dbm < -9) dbm = -9;
if (dbm > 14) dbm = 14; // deliberately capped: external FEM adds gain
if (dbm > 2) dbm = 2; // KCT8103L follows; known-good V4.3 firmware uses +2
s_tx_dbm = dbm;
uint8_t tp[2] = {(uint8_t)dbm, 0x02}; // ramp 40 us
return cmd(OP_SET_TX_PARAMS, tp, 2);
@@ -423,10 +420,32 @@ bool sx126x_set_rx(void)
return cmd(OP_SET_RX, (uint8_t[]){0xFF, 0xFF, 0xFF}, 3);
}
bool sx126x_start_tx(const uint8_t *buf, uint8_t len)
// C and T transmit differ only in FSK deviation: 45 kHz (C) vs 50 kHz (T).
// RX is unaffected (deviation is a TX-only GFSK parameter; the 234.3 kHz
// RX bandwidth covers both), so the swap is done lazily per TX.
static bool set_tx_deviation(bool t1)
{
if (t1 == s_tx_fdev_t1) return true;
// fdev(C) = 45e3 * 2^25 / 32e6 = 47186 = 0x00B852
// fdev(T) = 50e3 * 2^25 / 32e6 = 52429 = 0x00CCCD
uint8_t mod[8] = {
0x00, 0x28, 0x00, // bitrate 100 kbps
0x00, // no pulse shaping
0x0A, // RX BW 234.3 kHz
0x00,
t1 ? 0xCC : 0xB8,
t1 ? 0xCD : 0x52,
};
if (!cmd(OP_SET_MODULATION, mod, 8)) return false;
s_tx_fdev_t1 = t1;
return true;
}
bool sx126x_start_tx_mode(const uint8_t *buf, uint8_t len, bool t1_deviation)
{
if (!sx126x_standby()) return false;
sx126x_clear_irq(SX126X_IRQ_ALL);
if (!set_tx_deviation(t1_deviation)) return false;
if (!set_packet_params(len)) return false;
// WriteBuffer needs op + offset + payload in one transaction
@@ -445,6 +464,11 @@ bool sx126x_start_tx(const uint8_t *buf, uint8_t len)
return true;
}
bool sx126x_start_tx(const uint8_t *buf, uint8_t len)
{
return sx126x_start_tx_mode(buf, len, false);
}
uint16_t sx126x_get_irq(void)
{
uint8_t st[2] = {0, 0};
@@ -519,7 +543,5 @@ uint8_t sx126x_get_status(void)
return rx[1];
}
void sx126x_set_pa_low(bool low) { s_pa_low = low; }
void sx126x_set_preamble_detect(uint8_t code) { s_preamble_det = code; }
uint8_t sx126x_get_preamble_detect(void) { return s_preamble_det; }