Skip to content

Commit 4759a7a

Browse files
committed
feat(RadioInterface): Tx power gain calculation rework
- Rename REGULATORY_GAIN_LORA to TX_GAIN_LORA - Move gain-based Tx power clamping from RadioInterface::applyModemConfig() to RadioInterface::limitPower() - User-configured Tx power now matches the Tx power out of the device connector - Re-order [LoRa Chip]Interface.cpp limitPower() to take place before the final Tx power clamping so we clamp based on the pre-PA Tx power rather than user-configured Tx power Tested on XIAO BLE variant. Signed-off-by: Andrew Yong <[email protected]>
1 parent ef9d0d7 commit 4759a7a

8 files changed

+24
-19
lines changed

src/configuration.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
8080
// Override user saved region, for producing region-locked builds
8181
// #define REGULATORY_LORA_REGIONCODE meshtastic_Config_LoRaConfig_RegionCode_SG_923
8282

83-
// Total system gain in dBm to subtract from Tx power to remain within regulatory ERP limit for non-licensed operators
84-
// This value should be set in variant.h and is PA gain + antenna gain (if system ships with an antenna)
85-
#ifndef REGULATORY_GAIN_LORA
86-
#define REGULATORY_GAIN_LORA 0
83+
// Total system gain in dBm to subtract from Tx power to remain within regulatory and Tx PA limits
84+
// This value should be set in variant.h and is PA gain + antenna gain (if variant has a non-removable antenna)
85+
#ifndef TX_GAIN_LORA
86+
#define TX_GAIN_LORA 0
8787
#endif
8888

8989
// -----------------------------------------------------------------------------

src/mesh/LR11x0Interface.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ template <typename T> bool LR11x0Interface<T>::init()
7171

7272
RadioLibInterface::init();
7373

74+
limitPower();
75+
7476
if (power > LR1110_MAX_POWER) // Clamp power to maximum defined level
7577
power = LR1110_MAX_POWER;
7678

@@ -80,8 +82,6 @@ template <typename T> bool LR11x0Interface<T>::init()
8082
preambleLength = 12; // 12 is the default for operation above 2GHz
8183
}
8284

83-
limitPower();
84-
8585
#ifdef LR11X0_RF_SWITCH_SUBGHZ
8686
pinMode(LR11X0_RF_SWITCH_SUBGHZ, OUTPUT);
8787
digitalWrite(LR11X0_RF_SWITCH_SUBGHZ, getFreq() < 1e9 ? HIGH : LOW);

src/mesh/RF95Interface.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,11 @@ bool RF95Interface::init()
122122
power = dacDbValues.db;
123123
#endif
124124

125+
limitPower();
126+
125127
if (power > RF95_MAX_POWER) // This chip has lower power limits than some
126128
power = RF95_MAX_POWER;
127129

128-
limitPower();
129-
130130
iface = lora = new RadioLibRF95(&module);
131131

132132
#ifdef RF95_TCXO

src/mesh/RadioInterface.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -528,8 +528,8 @@ void RadioInterface::applyModemConfig()
528528

529529
power = loraConfig.tx_power;
530530

531-
if ((power == 0) || ((power + REGULATORY_GAIN_LORA > myRegion->powerLimit) && !devicestate.owner.is_licensed))
532-
power = myRegion->powerLimit - REGULATORY_GAIN_LORA;
531+
if ((power == 0) || ((power > myRegion->powerLimit) && !devicestate.owner.is_licensed))
532+
power = myRegion->powerLimit;
533533

534534
if (power == 0)
535535
power = 17; // Default to this power level if we don't have a valid regional power limit (powerLimit of myRegion defaults
@@ -616,7 +616,12 @@ void RadioInterface::limitPower()
616616
power = maxPower;
617617
}
618618

619-
LOG_INFO("Set radio: final power level=%d", power);
619+
if (TX_GAIN_LORA > 0) {
620+
LOG_INFO("Requested Tx power: %d dBm; Device LoRa Tx gain: %d dB", power, TX_GAIN_LORA);
621+
power -= TX_GAIN_LORA;
622+
}
623+
624+
LOG_INFO("Final Tx power: %d dBm", power);
620625
}
621626

622627
void RadioInterface::deliverToReceiver(meshtastic_MeshPacket *p)

src/mesh/STM32WLE5JCInterface.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ bool STM32WLE5JCInterface::init()
2525

2626
lora.setRfSwitchTable(rfswitch_pins, rfswitch_table);
2727

28+
limitPower();
29+
2830
if (power > STM32WLx_MAX_POWER) // This chip has lower power limits than some
2931
power = STM32WLx_MAX_POWER;
3032

31-
limitPower();
32-
3333
int res = lora.begin(getFreq(), bw, sf, cr, syncWord, power, preambleLength, tcxoVoltage);
3434

3535
LOG_INFO("STM32WLx init result %d", res);

src/mesh/SX126xInterface.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ template <typename T> bool SX126xInterface<T>::init()
6969

7070
RadioLibInterface::init();
7171

72+
limitPower();
73+
7274
if (power > SX126X_MAX_POWER) // Clamp power to maximum defined level
7375
power = SX126X_MAX_POWER;
7476

75-
limitPower();
76-
7777
int res = lora.begin(getFreq(), bw, sf, cr, syncWord, power, preambleLength, tcxoVoltage, useRegulatorLDO);
7878
// \todo Display actual typename of the adapter, not just `SX126x`
7979
LOG_INFO("SX126x init result %d", res);

src/mesh/SX128xInterface.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ template <typename T> bool SX128xInterface<T>::init()
6262

6363
RadioLibInterface::init();
6464

65+
limitPower();
66+
6567
if (power > SX128X_MAX_POWER) // This chip has lower power limits than some
6668
power = SX128X_MAX_POWER;
6769

68-
limitPower();
69-
7070
preambleLength = 12; // 12 is the default for this chip, 32 does not RX at all
7171

7272
int res = lora.begin(getFreq(), bw, sf, cr, syncWord, power, preambleLength);

variants/xiao_ble/variant.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,12 @@ static const uint8_t SCK = PIN_SPI_SCK;
145145
#ifdef EBYTE_E22_900M30S
146146
// 10dB PA gain and 30dB rated output; based on measurements from
147147
// https://github.com/S5NC/EBYTE_ESP32-S3/blob/main/E22-900M30S%20power%20output%20testing.txt
148-
#define REGULATORY_GAIN_LORA 7
148+
#define TX_GAIN_LORA 7
149149
#define SX126X_MAX_POWER 22
150150
#endif
151151
#ifdef EBYTE_E22_900M33S
152152
// 25dB PA gain and 33dB rated output; based on TX Power Curve from E22-900M33S_UserManual_EN_v1.0.pdf
153-
#define REGULATORY_GAIN_LORA 25
153+
#define TX_GAIN_LORA 25
154154
#define SX126X_MAX_POWER 8
155155
#endif
156156
#endif

0 commit comments

Comments
 (0)