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:
agessaman committed 2026-09-23 15:43:01 -07:00
1 parent 772edc1fcd
commit fa18757f2c
14 files changed
+553 -31

No files matched your search

+68 -2
View File
@@ -118,10 +118,15 @@ MeshCore-specific functionality uses the standard KISS SetHardware command. The
| Reboot | `0x18` | - |
| SetSignalReport | `0x19` | Enable (1): 0x00=disable, nonzero=enable |
| GetSignalReport | `0x1A` | - |
| GetCapabilities | `0x1B` | - |
| SetAgcResetInterval | `0x1C` | Seconds (2): 0=disable, 0-1020 |
| GetAgcResetInterval | `0x1D` | - |
| SetFemState | `0x1E` | Apply mask (1) + Value mask (1) |
| GetFemState | `0x1F` | - |
### Response Sub-commands (TNC to Host)
Response codes use the high-bit convention: `response = command | 0x80`. Generic and unsolicited responses use the `0xF0`+ range.
Response codes use the high-bit convention: `response = command | 0x80`. Generic and unsolicited responses use the `0xF0`+ range. Set commands that report state (SetSignalReport, SetAgcResetInterval, SetFemState) reply with the matching Get response, so `0x9C` and `0x9E` are never sent.
| Sub-command | Value | Data |
|--------------|--------|-----------------------------------------|
@@ -147,6 +152,9 @@ Response codes use the high-bit convention: `response = command | 0x80`. Generic
| DeviceName | `0x96` | Name (variable, UTF-8) |
| Pong | `0x97` | - |
| SignalReport | `0x9A` | Status (1): 0x00=disabled, 0x01=enabled |
| Capabilities | `0x9B` | Feature bits (4) |
| AgcResetInterval | `0x9D` | Effective seconds (2) |
| FemState | `0x9F` | Capability mask (1) + Value mask (1) |
| OK | `0xF0` | - |
| Error | `0xF1` | Error code (1) |
| TxDone | `0xF8` | Result (1): 0x00=failed, 0x01=success |
@@ -163,6 +171,7 @@ Response codes use the high-bit convention: `response = command | 0x80`. Generic
| UnknownCmd | `0x05` | Unknown sub-command |
| EncryptFailed | `0x06` | Encryption failed |
| TxBusy | `0x07` | Radio TX busy, or host output queue full |
| Unsupported | `0x08` | Command known, but not supported by this board |
### Unsolicited Events
@@ -215,7 +224,64 @@ All values little-endian.
|-------------|---------|-----------------------|
| Noise floor | 2 bytes | int16_t, dBm (signed) |
The modem recalibrates the noise floor every 2 seconds with an AGC reset every 30 seconds.
The modem recalibrates the noise floor every 2 seconds. AGC resets follow the configured AGC reset interval (default 30 seconds).
### Capabilities (Capabilities response)
All values little-endian. Hosts should probe capabilities rather than infer them from the firmware version: boards running the same version expose different hardware controls. Firmware older than version 2 answers GetCapabilities with `UnknownCmd`; treat that as no capabilities.
| Field | Size | Description |
|--------------|---------|--------------------|
| Feature bits | 4 bytes | uint32_t, see below |
| Bit | Value | Description |
|-----|--------|-------------------------------------------|
| 0 | `0x01` | AGC reset interval control (always set) |
| 1 | `0x02` | External FEM RX (LNA) gain control |
| 2 | `0x04` | External FEM TX (PA) gain control |
Remaining bits are reserved and sent as 0.
### AGC Reset Interval (SetAgcResetInterval / AgcResetInterval response)
All values little-endian.
| Field | Size | Description |
|---------|---------|-------------------------------------------|
| Seconds | 2 bytes | uint16_t, 0 = disabled, maximum 1020 |
The modem periodically resets the radio's AGC while idle (no transmission in progress and host output not backed up). The interval has 4-second resolution, matching the MeshCore `agc.reset.interval` setting: requested values are rounded down to a multiple of 4, so 1-3 disable the reset. Values above 1020 return `InvalidParam`. SetAgcResetInterval replies with the AgcResetInterval response carrying the effective value, and restarts the countdown from the time of the request.
The default is 30 seconds. The setting is not persisted; hosts should re-send it after reconnecting or rebooting the modem.
### FEM State (SetFemState / FemState response)
Controls the board's external LoRa front-end module gain. This is separate from the radio chip's own RX boosted gain. TX/RX switching of the FEM is handled by the board and is not affected by these settings.
| Bit | Value | Description |
|-----|--------|---------------------------------|
| 0 | `0x01` | RX gain (LNA enabled) |
| 1 | `0x02` | TX gain (PA high gain enabled) |
SetFemState data:
| Field | Size | Description |
|------------|--------|------------------------------------------------|
| Apply mask | 1 byte | Bits to change; bits not set are left as-is |
| Value mask | 1 byte | New value for each bit in the apply mask |
FemState response data:
| Field | Size | Description |
|-----------------|--------|---------------------------------------------------|
| Capability mask | 1 byte | Bits this board can control |
| Value mask | 1 byte | Current state; always 0 for uncontrollable bits |
Example: enable RX gain without changing TX gain: apply `0x01`, value `0x01`.
If any bit in the apply mask is not in the board's capability mask (including reserved bits), the modem replies `Unsupported` and changes nothing. On success, SetFemState replies with the FemState response carrying the new state. A request received while a packet is on air is applied as soon as that transmission ends, and the FemState response is sent then; a second request during the same transmission returns `TxBusy`. Capabilities come from the board, so a board with a fixed, board-managed FEM (for example RAK3401) reports none.
The setting is not persisted; the board's power-on default applies after reboot.
### Stats (Stats response)