Skip to content

Commit

Permalink
[SX128x] Added support for changing LoRa sync word (#311)
Browse files Browse the repository at this point in the history
  • Loading branch information
jgromes committed Jun 13, 2021
1 parent 0abfb5f commit 8be419f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/modules/SX128x/SX128x.cpp
Expand Up @@ -5,7 +5,7 @@ SX128x::SX128x(Module* mod) : PhysicalLayer(SX128X_FREQUENCY_STEP_SIZE, SX128X_M
_mod = mod;
}

int16_t SX128x::begin(float freq, float bw, uint8_t sf, uint8_t cr, int8_t power, uint16_t preambleLength) {
int16_t SX128x::begin(float freq, float bw, uint8_t sf, uint8_t cr, uint8_t syncWord, int8_t power, uint16_t preambleLength) {
// set module properties
_mod->init(RADIOLIB_USE_SPI);
Module::pinMode(_mod->getIrq(), INPUT);
Expand Down Expand Up @@ -48,6 +48,9 @@ int16_t SX128x::begin(float freq, float bw, uint8_t sf, uint8_t cr, int8_t power
state = setCodingRate(cr);
RADIOLIB_ASSERT(state);

state = setSyncWord(syncWord);
RADIOLIB_ASSERT(state);

state = setPreambleLength(preambleLength);
RADIOLIB_ASSERT(state);

Expand Down Expand Up @@ -906,6 +909,17 @@ int16_t SX128x::setSyncWord(uint8_t* syncWord, uint8_t len) {
return(setPacketParamsGFSK(_preambleLengthGFSK, _syncWordLen, _syncWordMatch, _crcGFSK, _whitening));
}

int16_t SX128x::setSyncWord(uint8_t syncWord, uint8_t controlBits) {
// check active modem
if(getPacketType() != SX128X_PACKET_TYPE_LORA) {
return(ERR_WRONG_MODEM);
}

// update register
uint8_t data[2] = {(uint8_t)((syncWord & 0xF0) | ((controlBits & 0xF0) >> 4)), (uint8_t)(((syncWord & 0x0F) << 4) | (controlBits & 0x0F))};
return(writeRegister(SX128X_REG_LORA_SYNC_WORD_MSB, data, 2));
}

int16_t SX128x::setCRC(uint8_t len, uint32_t initial, uint16_t polynomial) {
// check active modem
uint8_t modem = getPacketType();
Expand Down
15 changes: 14 additions & 1 deletion src/modules/SX128x/SX128x.h
Expand Up @@ -375,13 +375,15 @@ class SX128x: public PhysicalLayer {
\param cr LoRa coding rate denominator. Defaults to 7 (coding rate 4/7).
\param syncWord 2-byte LoRa sync word. Defaults to SX128X_SYNC_WORD_PRIVATE (0x12).
\param power Output power in dBm. Defaults to 10 dBm.
\param preambleLength LoRa preamble length in symbols. Defaults to 12 symbols.
\returns \ref status_codes
*/
int16_t begin(float freq = 2400.0, float bw = 812.5, uint8_t sf = 9, uint8_t cr = 7, int8_t power = 10, uint16_t preambleLength = 12);
int16_t begin(float freq = 2400.0, float bw = 812.5, uint8_t sf = 9, uint8_t cr = 7, uint8_t syncWord = SX128X_SYNC_WORD_PRIVATE, int8_t power = 10, uint16_t preambleLength = 12);

/*!
\brief Initialization method for GFSK modem.
Expand Down Expand Up @@ -666,6 +668,17 @@ class SX128x: public PhysicalLayer {
*/
int16_t setSyncWord(uint8_t* syncWord, uint8_t len);

/*!
\brief Sets LoRa sync word.
\param syncWord LoRa sync word to be set.
\param controlBits Undocumented control bits, required for compatibility purposes.
\returns \ref status_codes
*/
int16_t setSyncWord(uint8_t syncWord, uint8_t controlBits = 0x44);

/*!
\brief Sets CRC configuration.
Expand Down

0 comments on commit 8be419f

Please sign in to comment.