kiss_modem: add AGC reset interval and FEM gain control (protocol v2)
Add SetHardware sub-commands so a host can probe and configure per-board RF front-end settings: - GetCapabilities (0x1B): uint32 feature bits (AGC, FEM RX, FEM TX) - Set/GetAgcResetInterval (0x1C/0x1D): 0-1020 s, 4 s resolution, 0 disables; default stays 30 s - Set/GetFemState (0x1E/0x1F): apply/value masks for FEM RX (LNA) and TX (PA) gain; all-or-nothing, Unsupported (0x08) if any requested bit is not controllable on this board The AGC scheduler moves from main.cpp into KissModem so it is configurable and unit-tested. FEM changes that arrive mid-TX are deferred until the packet ends, since board setters drive FEM pins directly. MainBoard gains virtual FEM hooks (canControl/set/is for LNA and PA gain). Boards that already implemented these methods privately now expose them as overrides: Heltec V4, V4 R8, T096, Tracker V2, Station G3 and Wio Tracker L1 1W. KISS_FIRMWARE_VERSION bumped to 2.
This commit is contained in:
1 parent
772edc1fcd
commit
fa18757f2c
14 files changed
+553
-31
No files matched your search
@@ -22,6 +22,11 @@ KissModem::KissModem(Stream& serial, mesh::LocalIdentity& identity, mesh::RNG& r
|
||||
_getStatsCallback = nullptr;
|
||||
_config = {0, 0, 0, 0, 0};
|
||||
_signal_report_enabled = true;
|
||||
_agc_reset_interval_sec = KISS_AGC_RESET_DEFAULT_SEC;
|
||||
_next_agc_reset_ms = 0;
|
||||
_fem_deferred = false;
|
||||
_fem_deferred_apply = 0;
|
||||
_fem_deferred_value = 0;
|
||||
resetOutputQueue();
|
||||
}
|
||||
|
||||
@@ -31,6 +36,8 @@ void KissModem::begin() {
|
||||
_rx_active = false;
|
||||
_has_pending_tx = false;
|
||||
_tx_state = TX_IDLE;
|
||||
_fem_deferred = false;
|
||||
_next_agc_reset_ms = millis() + (uint32_t)_agc_reset_interval_sec * 1000;
|
||||
resetOutputQueue();
|
||||
}
|
||||
|
||||
@@ -240,10 +247,24 @@ void KissModem::loop() {
|
||||
}
|
||||
|
||||
processTx();
|
||||
processDeferredFem();
|
||||
maybeResetAgc();
|
||||
tryFlushFrames();
|
||||
queuePendingBusyError();
|
||||
}
|
||||
|
||||
void KissModem::maybeResetAgc() {
|
||||
if (_agc_reset_interval_sec == 0) return;
|
||||
// same guard as the RX path in main.cpp, so receive is restarted right after the reset
|
||||
if (_tx_state != TX_IDLE || isHostOutputBackedUp()) return;
|
||||
|
||||
uint32_t now = millis();
|
||||
if ((int32_t)(now - _next_agc_reset_ms) >= 0) {
|
||||
_radio.resetAGC();
|
||||
_next_agc_reset_ms = now + (uint32_t)_agc_reset_interval_sec * 1000;
|
||||
}
|
||||
}
|
||||
|
||||
void KissModem::processFrame() {
|
||||
if (_rx_len < 1) return;
|
||||
|
||||
@@ -381,6 +402,21 @@ void KissModem::handleHardwareCommand(uint8_t sub_cmd, const uint8_t* data, uint
|
||||
case HW_CMD_GET_SIGNAL_REPORT:
|
||||
handleGetSignalReport();
|
||||
break;
|
||||
case HW_CMD_GET_CAPABILITIES:
|
||||
handleGetCapabilities();
|
||||
break;
|
||||
case HW_CMD_SET_AGC_RESET_INTERVAL:
|
||||
handleSetAgcResetInterval(data, len);
|
||||
break;
|
||||
case HW_CMD_GET_AGC_RESET_INTERVAL:
|
||||
handleGetAgcResetInterval();
|
||||
break;
|
||||
case HW_CMD_SET_FEM_STATE:
|
||||
handleSetFemState(data, len);
|
||||
break;
|
||||
case HW_CMD_GET_FEM_STATE:
|
||||
handleGetFemState();
|
||||
break;
|
||||
default:
|
||||
writeHardwareError(HW_ERR_UNKNOWN_CMD);
|
||||
break;
|
||||
@@ -732,3 +768,101 @@ void KissModem::handleGetSignalReport() {
|
||||
uint8_t val = _signal_report_enabled ? 0x01 : 0x00;
|
||||
writeHardwareFrame(HW_RESP(HW_CMD_GET_SIGNAL_REPORT), &val, 1);
|
||||
}
|
||||
|
||||
void KissModem::handleGetCapabilities() {
|
||||
uint32_t caps = HW_CAP_AGC_RESET;
|
||||
if (_board.canControlLoRaFemLna()) caps |= HW_CAP_FEM_RX_GAIN;
|
||||
if (_board.canControlLoRaFemPaGain()) caps |= HW_CAP_FEM_TX_GAIN;
|
||||
uint8_t buf[4] = { (uint8_t)caps, (uint8_t)(caps >> 8), (uint8_t)(caps >> 16), (uint8_t)(caps >> 24) };
|
||||
writeHardwareFrame(HW_RESP(HW_CMD_GET_CAPABILITIES), buf, 4);
|
||||
}
|
||||
|
||||
void KissModem::handleSetAgcResetInterval(const uint8_t* data, uint16_t len) {
|
||||
if (len < 2) {
|
||||
writeHardwareError(HW_ERR_INVALID_LENGTH);
|
||||
return;
|
||||
}
|
||||
uint16_t secs = (uint16_t)(data[0] | (data[1] << 8));
|
||||
if (secs > KISS_AGC_RESET_MAX_SEC) {
|
||||
writeHardwareError(HW_ERR_INVALID_PARAM);
|
||||
return;
|
||||
}
|
||||
_agc_reset_interval_sec = secs - (secs % KISS_AGC_RESET_STEP_SEC);
|
||||
_next_agc_reset_ms = millis() + (uint32_t)_agc_reset_interval_sec * 1000;
|
||||
handleGetAgcResetInterval();
|
||||
}
|
||||
|
||||
void KissModem::handleGetAgcResetInterval() {
|
||||
uint8_t buf[2] = { (uint8_t)_agc_reset_interval_sec, (uint8_t)(_agc_reset_interval_sec >> 8) };
|
||||
writeHardwareFrame(HW_RESP(HW_CMD_GET_AGC_RESET_INTERVAL), buf, 2);
|
||||
}
|
||||
|
||||
uint8_t KissModem::femCapabilityMask() const {
|
||||
uint8_t mask = 0;
|
||||
if (_board.canControlLoRaFemLna()) mask |= HW_FEM_RX_GAIN;
|
||||
if (_board.canControlLoRaFemPaGain()) mask |= HW_FEM_TX_GAIN;
|
||||
return mask;
|
||||
}
|
||||
|
||||
uint8_t KissModem::femValueMask() const {
|
||||
uint8_t mask = 0;
|
||||
if (_board.canControlLoRaFemLna() && _board.isLoRaFemLnaEnabled()) mask |= HW_FEM_RX_GAIN;
|
||||
if (_board.canControlLoRaFemPaGain() && _board.isLoRaFemPaGainEnabled()) mask |= HW_FEM_TX_GAIN;
|
||||
return mask;
|
||||
}
|
||||
|
||||
void KissModem::writeFemState() {
|
||||
uint8_t buf[2] = { femCapabilityMask(), femValueMask() };
|
||||
writeHardwareFrame(HW_RESP(HW_CMD_GET_FEM_STATE), buf, 2);
|
||||
}
|
||||
|
||||
void KissModem::applyFemState(uint8_t apply_mask, uint8_t value_mask) {
|
||||
bool ok = true;
|
||||
if (apply_mask & HW_FEM_RX_GAIN) {
|
||||
ok &= _board.setLoRaFemLnaEnabled((value_mask & HW_FEM_RX_GAIN) != 0);
|
||||
}
|
||||
if (apply_mask & HW_FEM_TX_GAIN) {
|
||||
ok &= _board.setLoRaFemPaGainEnabled((value_mask & HW_FEM_TX_GAIN) != 0);
|
||||
}
|
||||
if (ok) {
|
||||
writeFemState();
|
||||
} else {
|
||||
writeHardwareError(HW_ERR_UNSUPPORTED);
|
||||
}
|
||||
}
|
||||
|
||||
void KissModem::processDeferredFem() {
|
||||
if (!_fem_deferred || _tx_state == TX_SENDING) return;
|
||||
_fem_deferred = false;
|
||||
applyFemState(_fem_deferred_apply, _fem_deferred_value);
|
||||
}
|
||||
|
||||
void KissModem::handleSetFemState(const uint8_t* data, uint16_t len) {
|
||||
if (len < 2) {
|
||||
writeHardwareError(HW_ERR_INVALID_LENGTH);
|
||||
return;
|
||||
}
|
||||
uint8_t apply_mask = data[0];
|
||||
uint8_t value_mask = data[1];
|
||||
// validate every requested bit before touching hardware so a request is all-or-nothing
|
||||
if (apply_mask & ~femCapabilityMask()) {
|
||||
writeHardwareError(HW_ERR_UNSUPPORTED);
|
||||
return;
|
||||
}
|
||||
if (_tx_state == TX_SENDING) {
|
||||
// board setters drive FEM pins directly; changing them mid-packet would corrupt the TX
|
||||
if (_fem_deferred) {
|
||||
writeHardwareError(HW_ERR_TX_BUSY);
|
||||
return;
|
||||
}
|
||||
_fem_deferred = true;
|
||||
_fem_deferred_apply = apply_mask;
|
||||
_fem_deferred_value = value_mask;
|
||||
return;
|
||||
}
|
||||
applyFemState(apply_mask, value_mask);
|
||||
}
|
||||
|
||||
void KissModem::handleGetFemState() {
|
||||
writeFemState();
|
||||
}
|
||||
@@ -62,6 +62,11 @@
|
||||
#define HW_CMD_REBOOT 0x18
|
||||
#define HW_CMD_SET_SIGNAL_REPORT 0x19
|
||||
#define HW_CMD_GET_SIGNAL_REPORT 0x1A
|
||||
#define HW_CMD_GET_CAPABILITIES 0x1B
|
||||
#define HW_CMD_SET_AGC_RESET_INTERVAL 0x1C
|
||||
#define HW_CMD_GET_AGC_RESET_INTERVAL 0x1D
|
||||
#define HW_CMD_SET_FEM_STATE 0x1E
|
||||
#define HW_CMD_GET_FEM_STATE 0x1F
|
||||
|
||||
/* Response code = command code | 0x80. Generic / unsolicited use 0xF0+. */
|
||||
#define HW_RESP(cmd) ((cmd) | 0x80)
|
||||
@@ -81,8 +86,22 @@
|
||||
#define HW_ERR_UNKNOWN_CMD 0x05
|
||||
#define HW_ERR_ENCRYPT_FAILED 0x06
|
||||
#define HW_ERR_TX_BUSY 0x07
|
||||
#define HW_ERR_UNSUPPORTED 0x08
|
||||
|
||||
#define KISS_FIRMWARE_VERSION 1
|
||||
/* GetCapabilities feature bits */
|
||||
#define HW_CAP_AGC_RESET (1UL << 0)
|
||||
#define HW_CAP_FEM_RX_GAIN (1UL << 1)
|
||||
#define HW_CAP_FEM_TX_GAIN (1UL << 2)
|
||||
|
||||
/* SetFemState / GetFemState mask bits */
|
||||
#define HW_FEM_RX_GAIN (1 << 0)
|
||||
#define HW_FEM_TX_GAIN (1 << 1)
|
||||
|
||||
#define KISS_AGC_RESET_DEFAULT_SEC 30
|
||||
#define KISS_AGC_RESET_MAX_SEC 1020
|
||||
#define KISS_AGC_RESET_STEP_SEC 4
|
||||
|
||||
#define KISS_FIRMWARE_VERSION 2
|
||||
|
||||
typedef void (*SetRadioCallback)(float freq, float bw, uint8_t sf, uint8_t cr);
|
||||
typedef void (*SetTxPowerCallback)(uint8_t power);
|
||||
@@ -139,6 +158,11 @@ class KissModem {
|
||||
|
||||
RadioConfig _config;
|
||||
bool _signal_report_enabled;
|
||||
uint16_t _agc_reset_interval_sec;
|
||||
uint32_t _next_agc_reset_ms;
|
||||
bool _fem_deferred;
|
||||
uint8_t _fem_deferred_apply;
|
||||
uint8_t _fem_deferred_value;
|
||||
uint8_t _tx_frame_buf[KISS_TX_FRAME_QUEUE_DEPTH][KISS_MAX_ENCODED_FRAME_SIZE];
|
||||
uint16_t _tx_frame_len[KISS_TX_FRAME_QUEUE_DEPTH];
|
||||
uint16_t _tx_frame_written[KISS_TX_FRAME_QUEUE_DEPTH];
|
||||
@@ -165,6 +189,12 @@ class KissModem {
|
||||
void processFrame();
|
||||
void handleHardwareCommand(uint8_t sub_cmd, const uint8_t* data, uint16_t len);
|
||||
void processTx();
|
||||
void maybeResetAgc();
|
||||
uint8_t femCapabilityMask() const;
|
||||
uint8_t femValueMask() const;
|
||||
void writeFemState();
|
||||
void applyFemState(uint8_t apply_mask, uint8_t value_mask);
|
||||
void processDeferredFem();
|
||||
|
||||
void handleGetIdentity();
|
||||
void handleGetRandom(const uint8_t* data, uint16_t len);
|
||||
@@ -192,6 +222,11 @@ class KissModem {
|
||||
void handleGetDeviceName();
|
||||
void handleSetSignalReport(const uint8_t* data, uint16_t len);
|
||||
void handleGetSignalReport();
|
||||
void handleGetCapabilities();
|
||||
void handleSetAgcResetInterval(const uint8_t* data, uint16_t len);
|
||||
void handleGetAgcResetInterval();
|
||||
void handleSetFemState(const uint8_t* data, uint16_t len);
|
||||
void handleGetFemState();
|
||||
|
||||
public:
|
||||
KissModem(Stream& serial, mesh::LocalIdentity& identity, mesh::RNG& rng,
|
||||
|
||||
@@ -19,7 +19,6 @@
|
||||
#endif
|
||||
|
||||
#define NOISE_FLOOR_CALIB_INTERVAL_MS 2000
|
||||
#define AGC_RESET_INTERVAL_MS 30000
|
||||
#define USB_TX_TIMEOUT_MS 50
|
||||
#define USB_TX_BUFFER_SIZE 1024
|
||||
|
||||
@@ -27,7 +26,6 @@ StdRNG rng;
|
||||
mesh::LocalIdentity identity;
|
||||
KissModem* modem;
|
||||
static uint32_t next_noise_floor_calib_ms = 0;
|
||||
static uint32_t next_agc_reset_ms = 0;
|
||||
|
||||
void halt() {
|
||||
while (1) ;
|
||||
@@ -133,13 +131,6 @@ void loop() {
|
||||
modem->loop();
|
||||
|
||||
if (!modem->isActuallyTransmitting() && !modem->isHostOutputBackedUp()) {
|
||||
if (!modem->isTxBusy()) {
|
||||
if ((uint32_t)(millis() - next_agc_reset_ms) >= AGC_RESET_INTERVAL_MS) {
|
||||
radio_driver.resetAGC();
|
||||
next_agc_reset_ms = millis();
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t rx_buf[256];
|
||||
int rx_len = radio_driver.recvRaw(rx_buf, sizeof(rx_buf));
|
||||
if (rx_len > 0) {
|
||||
|
||||
Reference in new issue
Block a user