Skip to content

Commit 3f59f9d

Browse files
committed
make it more dry
1 parent 2be9bb0 commit 3f59f9d

5 files changed

Lines changed: 41 additions & 104 deletions

File tree

src/helpers/radiolib/CustomLLCC68Wrapper.h

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "CustomLLCC68.h"
44
#include "RadioLibWrappers.h"
5+
#include "SX126xReset.h"
56

67
class CustomLLCC68Wrapper : public RadioLibWrapper {
78
public:
@@ -20,29 +21,5 @@ class CustomLLCC68Wrapper : public RadioLibWrapper {
2021
return packetScoreInt(snr, sf, packet_len);
2122
}
2223

23-
void doResetAGC() override {
24-
auto* radio = (CustomLLCC68 *)_radio;
25-
radio->sleep(true);
26-
radio->standby(RADIOLIB_SX126X_STANDBY_RC, true);
27-
uint8_t calData = RADIOLIB_SX126X_CALIBRATE_ALL;
28-
radio->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_CALIBRATE, &calData, 1, true, false);
29-
radio->mod->hal->delay(5);
30-
uint32_t start = millis();
31-
while (radio->mod->hal->digitalRead(radio->mod->getGpio())) {
32-
if (millis() - start > 50) break;
33-
radio->mod->hal->yield();
34-
}
35-
#ifdef SX126X_DIO2_AS_RF_SWITCH
36-
radio->setDio2AsRfSwitch(SX126X_DIO2_AS_RF_SWITCH);
37-
#endif
38-
#ifdef SX126X_RX_BOOSTED_GAIN
39-
radio->setRxBoostedGainMode(SX126X_RX_BOOSTED_GAIN);
40-
#endif
41-
#ifdef SX126X_REGISTER_PATCH
42-
uint8_t r_data = 0;
43-
radio->readRegister(0x8B5, &r_data, 1);
44-
r_data |= 0x01;
45-
radio->writeRegister(0x8B5, &r_data, 1);
46-
#endif
47-
}
24+
void doResetAGC() override { sx126xResetAGC((SX126x *)_radio); }
4825
};

src/helpers/radiolib/CustomSTM32WLxWrapper.h

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "CustomSTM32WLx.h"
44
#include "RadioLibWrappers.h"
5+
#include "SX126xReset.h"
56
#include <math.h>
67

78
class CustomSTM32WLxWrapper : public RadioLibWrapper {
@@ -21,29 +22,5 @@ class CustomSTM32WLxWrapper : public RadioLibWrapper {
2122
return packetScoreInt(snr, sf, packet_len);
2223
}
2324

24-
void doResetAGC() override {
25-
auto* radio = (CustomSTM32WLx *)_radio;
26-
radio->sleep(true);
27-
radio->standby(RADIOLIB_SX126X_STANDBY_RC, true);
28-
uint8_t calData = RADIOLIB_SX126X_CALIBRATE_ALL;
29-
radio->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_CALIBRATE, &calData, 1, true, false);
30-
radio->mod->hal->delay(5);
31-
uint32_t start = millis();
32-
while (radio->mod->hal->digitalRead(radio->mod->getGpio())) {
33-
if (millis() - start > 50) break;
34-
radio->mod->hal->yield();
35-
}
36-
#ifdef SX126X_DIO2_AS_RF_SWITCH
37-
radio->setDio2AsRfSwitch(SX126X_DIO2_AS_RF_SWITCH);
38-
#endif
39-
#ifdef SX126X_RX_BOOSTED_GAIN
40-
radio->setRxBoostedGainMode(SX126X_RX_BOOSTED_GAIN);
41-
#endif
42-
#ifdef SX126X_REGISTER_PATCH
43-
uint8_t r_data = 0;
44-
radio->readRegister(0x8B5, &r_data, 1);
45-
r_data |= 0x01;
46-
radio->writeRegister(0x8B5, &r_data, 1);
47-
#endif
48-
}
25+
void doResetAGC() override { sx126xResetAGC((SX126x *)_radio); }
4926
};

src/helpers/radiolib/CustomSX1262Wrapper.h

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "CustomSX1262.h"
44
#include "RadioLibWrappers.h"
5+
#include "SX126xReset.h"
56

67
class CustomSX1262Wrapper : public RadioLibWrapper {
78
public:
@@ -23,33 +24,5 @@ class CustomSX1262Wrapper : public RadioLibWrapper {
2324
((CustomSX1262 *)_radio)->sleep(false);
2425
}
2526

26-
void doResetAGC() override {
27-
auto* radio = (CustomSX1262 *)_radio;
28-
// Warm sleep powers down analog frontend (resets AGC gain state)
29-
radio->sleep(true);
30-
// Wake to STDBY_RC for calibration
31-
radio->standby(RADIOLIB_SX126X_STANDBY_RC, true);
32-
// Recalibrate all blocks (ADC, PLL, image, oscillators)
33-
uint8_t calData = RADIOLIB_SX126X_CALIBRATE_ALL;
34-
radio->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_CALIBRATE, &calData, 1, true, false);
35-
radio->mod->hal->delay(5);
36-
uint32_t start = millis();
37-
while (radio->mod->hal->digitalRead(radio->mod->getGpio())) {
38-
if (millis() - start > 50) break;
39-
radio->mod->hal->yield();
40-
}
41-
// Re-apply RX settings that calibration may reset
42-
#ifdef SX126X_DIO2_AS_RF_SWITCH
43-
radio->setDio2AsRfSwitch(SX126X_DIO2_AS_RF_SWITCH);
44-
#endif
45-
#ifdef SX126X_RX_BOOSTED_GAIN
46-
radio->setRxBoostedGainMode(SX126X_RX_BOOSTED_GAIN);
47-
#endif
48-
#ifdef SX126X_REGISTER_PATCH
49-
uint8_t r_data = 0;
50-
radio->readRegister(0x8B5, &r_data, 1);
51-
r_data |= 0x01;
52-
radio->writeRegister(0x8B5, &r_data, 1);
53-
#endif
54-
}
27+
void doResetAGC() override { sx126xResetAGC((SX126x *)_radio); }
5528
};

src/helpers/radiolib/CustomSX1268Wrapper.h

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "CustomSX1268.h"
44
#include "RadioLibWrappers.h"
5+
#include "SX126xReset.h"
56

67
class CustomSX1268Wrapper : public RadioLibWrapper {
78
public:
@@ -20,29 +21,5 @@ class CustomSX1268Wrapper : public RadioLibWrapper {
2021
return packetScoreInt(snr, sf, packet_len);
2122
}
2223

23-
void doResetAGC() override {
24-
auto* radio = (CustomSX1268 *)_radio;
25-
radio->sleep(true);
26-
radio->standby(RADIOLIB_SX126X_STANDBY_RC, true);
27-
uint8_t calData = RADIOLIB_SX126X_CALIBRATE_ALL;
28-
radio->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_CALIBRATE, &calData, 1, true, false);
29-
radio->mod->hal->delay(5);
30-
uint32_t start = millis();
31-
while (radio->mod->hal->digitalRead(radio->mod->getGpio())) {
32-
if (millis() - start > 50) break;
33-
radio->mod->hal->yield();
34-
}
35-
#ifdef SX126X_DIO2_AS_RF_SWITCH
36-
radio->setDio2AsRfSwitch(SX126X_DIO2_AS_RF_SWITCH);
37-
#endif
38-
#ifdef SX126X_RX_BOOSTED_GAIN
39-
radio->setRxBoostedGainMode(SX126X_RX_BOOSTED_GAIN);
40-
#endif
41-
#ifdef SX126X_REGISTER_PATCH
42-
uint8_t r_data = 0;
43-
radio->readRegister(0x8B5, &r_data, 1);
44-
r_data |= 0x01;
45-
radio->writeRegister(0x8B5, &r_data, 1);
46-
#endif
47-
}
24+
void doResetAGC() override { sx126xResetAGC((SX126x *)_radio); }
4825
};

src/helpers/radiolib/SX126xReset.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#pragma once
2+
3+
#include <RadioLib.h>
4+
5+
// Full receiver reset for all SX126x-family chips (SX1262, SX1268, LLCC68, STM32WLx).
6+
// Warm sleep powers down analog, Calibrate(0x7F) refreshes ADC/PLL/image calibration,
7+
// then re-applies RX settings that calibration may reset.
8+
inline void sx126xResetAGC(SX126x* radio) {
9+
radio->sleep(true);
10+
radio->standby(RADIOLIB_SX126X_STANDBY_RC, true);
11+
12+
uint8_t calData = RADIOLIB_SX126X_CALIBRATE_ALL;
13+
radio->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_CALIBRATE, &calData, 1, true, false);
14+
radio->mod->hal->delay(5);
15+
uint32_t start = millis();
16+
while (radio->mod->hal->digitalRead(radio->mod->getGpio())) {
17+
if (millis() - start > 50) break;
18+
radio->mod->hal->yield();
19+
}
20+
21+
#ifdef SX126X_DIO2_AS_RF_SWITCH
22+
radio->setDio2AsRfSwitch(SX126X_DIO2_AS_RF_SWITCH);
23+
#endif
24+
#ifdef SX126X_RX_BOOSTED_GAIN
25+
radio->setRxBoostedGainMode(SX126X_RX_BOOSTED_GAIN);
26+
#endif
27+
#ifdef SX126X_REGISTER_PATCH
28+
uint8_t r_data = 0;
29+
radio->readRegister(0x8B5, &r_data, 1);
30+
r_data |= 0x01;
31+
radio->writeRegister(0x8B5, &r_data, 1);
32+
#endif
33+
}

0 commit comments

Comments
 (0)