Skip to content

Commit

Permalink
Merge pull request #865 from osmanovv/sx1268-support
Browse files Browse the repository at this point in the history
SX1268 module support and base class for SX126x module family
  • Loading branch information
geeksville authored Sep 18, 2021
2 parents b9443d8 + 16d2c56 commit 2f7e200
Show file tree
Hide file tree
Showing 22 changed files with 476 additions and 395 deletions.
20 changes: 10 additions & 10 deletions src/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define LORA_DIO3 // Not connected on PCB, but internally on the TTGO SX1262, if DIO3 is high the TXCO is enabled

#ifdef USE_SX1262
#define SX1262_CS RF95_NSS // FIXME - we really should define LORA_CS instead
#define SX1262_DIO1 LORA_DIO1
#define SX1262_BUSY LORA_DIO2
#define SX1262_RESET LORA_RESET
#define SX1262_E22 // Not really an E22 but TTGO seems to be trying to clone that
#define SX126X_CS RF95_NSS // FIXME - we really should define LORA_CS instead
#define SX126X_DIO1 LORA_DIO1
#define SX126X_BUSY LORA_DIO2
#define SX126X_RESET LORA_RESET
#define SX126X_E22 // Not really an E22 but TTGO seems to be trying to clone that
// Internally the TTGO module hooks the SX1262-DIO2 in to control the TX/RX switch (which is the default for the sx1262interface
// code)
#endif
Expand Down Expand Up @@ -462,11 +462,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define LORA_DIO3 // Not connected on PCB, but internally on the TTGO SX1262, if DIO3 is high the TXCO is enabled

#ifdef USE_SX1262
#define SX1262_CS 20 // CS0 on pinelora schematic, hooked to gpio D0 on ch341f
#define SX1262_DIO1 LORA_DIO1
#define SX1262_BUSY LORA_DIO2
#define SX1262_RESET LORA_RESET
// HOPE RFM90 does not have a TCXO therefore not SX1262_E22
#define SX126X_CS 20 // CS0 on pinelora schematic, hooked to gpio D0 on ch341f
#define SX126X_DIO1 LORA_DIO1
#define SX126X_BUSY LORA_DIO2
#define SX126X_RESET LORA_RESET
// HOPE RFM90 does not have a TCXO therefore not SX126X_E22
#endif

#endif
Expand Down
26 changes: 20 additions & 6 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

#include "RF95Interface.h"
#include "SX1262Interface.h"
#include "SX1268Interface.h"

#ifdef NRF52_SERIES
#include "variant.h"
Expand Down Expand Up @@ -491,10 +492,10 @@ void setup()
}
}

#ifdef SX1262_ANT_SW
// make analog PA vs not PA switch on SX1262 eval board work properly
pinMode(SX1262_ANT_SW, OUTPUT);
digitalWrite(SX1262_ANT_SW, 1);
#ifdef SX126X_ANT_SW
// make analog PA vs not PA switch on SX126x eval board work properly
pinMode(SX126X_ANT_SW, OUTPUT);
digitalWrite(SX126X_ANT_SW, 1);
#endif

// radio init MUST BE AFTER service.init, so we have our radio config settings (from nodedb init)
Expand All @@ -512,9 +513,9 @@ void setup()
}
#endif

#if defined(SX1262_CS)
#if defined(USE_SX1262)
if (!rIf) {
rIf = new SX1262Interface(SX1262_CS, SX1262_DIO1, SX1262_RESET, SX1262_BUSY, SPI);
rIf = new SX1262Interface(SX126X_CS, SX126X_DIO1, SX126X_RESET, SX126X_BUSY, SPI);
if (!rIf->init()) {
DEBUG_MSG("Warning: Failed to find SX1262 radio\n");
delete rIf;
Expand All @@ -525,6 +526,19 @@ void setup()
}
#endif

#if defined(USE_SX1268)
if (!rIf) {
rIf = new SX1268Interface(SX126X_CS, SX126X_DIO1, SX126X_RESET, SX126X_BUSY, SPI);
if (!rIf->init()) {
DEBUG_MSG("Warning: Failed to find SX1268 radio\n");
delete rIf;
rIf = NULL;
} else {
DEBUG_MSG("SX1268 Radio init succeeded, using SX1268 radio\n");
}
}
#endif

#ifdef USE_SIM_RADIO
if (!rIf) {
rIf = new SimRadio;
Expand Down
6 changes: 6 additions & 0 deletions src/mesh/InterfacesTemplates.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "SX126xInterface.h"
#include "SX126xInterface.cpp"

// We need this declaration for proper linking in derived classes
template class SX126xInterface<SX1262>;
template class SX126xInterface<SX1268>;
4 changes: 2 additions & 2 deletions src/mesh/RF95Interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ bool RF95Interface::init()
#endif
setTransmitEnable(false);

int res = lora->begin(freq, bw, sf, cr, syncWord, power, currentLimit, preambleLength);
int res = lora->begin(getFreq(), bw, sf, cr, syncWord, power, currentLimit, preambleLength);
DEBUG_MSG("RF95 init result %d\n", res);

// current limit was removed from module' ctor
Expand Down Expand Up @@ -119,7 +119,7 @@ bool RF95Interface::reconfigure()
err = lora->setPreambleLength(preambleLength);
assert(err == ERR_NONE);

err = lora->setFrequency(freq);
err = lora->setFrequency(getFreq());
if (err != ERR_NONE)
RECORD_CRITICALERROR(CriticalErrorCode_InvalidRadioSetting);

Expand Down
10 changes: 5 additions & 5 deletions src/mesh/RadioInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,18 +318,18 @@ void RadioInterface::applyModemConfig()
// If user has manually specified a channel num, then use that, otherwise generate one by hashing the name
const char *channelName = channels.getName(channels.getPrimaryIndex());
int channel_num = channelSettings.channel_num ? channelSettings.channel_num - 1 : hash(channelName) % myRegion->numChannels;
freq = myRegion->freq + radioConfig.preferences.frequency_offset + myRegion->spacing * channel_num;
float freq = myRegion->freq + radioConfig.preferences.frequency_offset + myRegion->spacing * channel_num;

saveChannelNum(channel_num);
saveFreq(freq);

DEBUG_MSG("Set radio: name=%s, config=%u, ch=%d, power=%d\n", channelName, channelSettings.modem_config, channel_num, power);
DEBUG_MSG("Radio myRegion->freq: %f\n", myRegion->freq);
DEBUG_MSG("Radio myRegion->spacing: %f\n", myRegion->spacing);
DEBUG_MSG("Radio myRegion->numChannels: %d\n", myRegion->numChannels);
DEBUG_MSG("Radio channel_num: %d\n", channel_num);
DEBUG_MSG("Radio frequency: %f\n", freq);
DEBUG_MSG("Radio frequency: %f\n", getFreq()); // the frequency could be overridden in RadioInterface::getFreq() for some modules
DEBUG_MSG("Short packet time: %u msec\n", shortPacketMsec);

saveChannelNum(channel_num);
saveFreq(freq);
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/mesh/RadioInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ class RadioInterface
void deliverToReceiver(MeshPacket *p);

public:
float freq = 915.0;

/** pool is the pool we will alloc our rx packets from
*/
RadioInterface();
Expand Down Expand Up @@ -149,7 +147,7 @@ class RadioInterface
/**
* Get the frequency we saved.
*/
float getFreq();
virtual float getFreq();

/// Some boards (1st gen Pinetab Lora module) have broken IRQ wires, so we need to poll via i2c registers
virtual bool isIRQPending() { return false; }
Expand Down
Loading

0 comments on commit 2f7e200

Please sign in to comment.