Skip to content

Commit

Permalink
Add custom Syncword and Low DataRate optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
beegee-tokyo committed Jan 1, 2025
1 parent b8ee298 commit 847b224
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 27 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Arduino library for LoRa communication with Semtech SX126x chips. It is based on

# Release Notes

# V2.0.28 Add custom Syncword and Low DataRate optimization
- Add radio function to set a custom Syncword Radio.SetCustomSyncWord(uint16_t syncword);
- Add radio function to enforce Low DataRate optimization Radio.EnforceLowDRopt(bool enforce);

# V2.0.27 Access to NWsKey and AppsKey
- Add functions to get network session key and app session key after join

Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
----
## Changelog
[Code releases](CHANGELOG.md)
- 2025-01-01 Add custom Syncword and Low DataRate optimization
- Add radio function to set a custom Syncword Radio.SetCustomSyncWord(uint16_t syncword);
- Add radio function to enforce Low DataRate optimization Radio.EnforceLowDRopt(bool enforce);
- 2024-10-17 Access to NWsKey and AppsKey
- Add functions to get network session key and app session key after join
- 2024-07-02 Add missing header file
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "SX126x-Arduino",
"version": "2.0.27",
"version": "2.0.28",
"keywords": [
"lora",
"Semtech",
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=SX126x-Arduino
version=2.0.27
version=2.0.28
author=Bernd Giesecke <beegee@giesecke.tk>
maintainer=Bernd Giesecke <beegee@giesecke.tk>
sentence=Arduino library to use Semtech SX126x LoRa chips and modules to communicate
Expand Down
6 changes: 3 additions & 3 deletions src/mac/LoRaMacHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1153,9 +1153,9 @@ uint32_t lmh_getDevAddr(void)
return LoRaMacGetOTAADevId();
}

void lmh_getNwSkey(uint8_t * key)
void lmh_getNwSkey(uint8_t *key)
{
for (int idx=0; idx<16; idx++)
for (int idx = 0; idx < 16; idx++)
{
key[idx] = LoRaMacNwkSKey[idx];
}
Expand Down Expand Up @@ -1215,4 +1215,4 @@ uint8_t lmh_getConfRetries(void)
void lmh_reset_mac(void)
{
ResetMacCounters();
}
}
13 changes: 13 additions & 0 deletions src/mac/LoRaMacHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include "mac/region/Region.h"
#include "mac/region/RegionAS923.h"

extern bool force_low_dr_opt;

#define LORAWAN_CONFIRMED_MSG_ON 0 /**< LoRaWAN confirmed messages */
#define LORAWAN_CERTIF_PORT 224 /**< LoRaWAN certification port */
#define LORAWAN_APP_PORT 2 /**< LoRaWAN application port, do not use 224. It is reserved for certification */
Expand Down Expand Up @@ -314,7 +316,18 @@ uint8_t lmh_getConfRetries(void);
*/
void lmh_reset_mac(void);

/**
* @brief Get Network Session Key
*
* @param key Buffer where the key is stored
*/
void lmh_getNwSkey(uint8_t *key);

/**
* @brief Get Application Sesssion Key
*
* @param key Buffer where the key is stored
*/
void lmh_getAppSkey(uint8_t *key);

#endif
44 changes: 26 additions & 18 deletions src/radio/radio.h
Original file line number Diff line number Diff line change
Expand Up @@ -351,19 +351,19 @@ struct Radio_s
* \param enable if true, it enables a public network
*/
void (*SetPublicNetwork)(bool enable);
/*!
* \brief Sets a custom Sync-Word. Updates the sync byte.
*
* \remark Applies to LoRa modem only
*
* \param syncword 2 byte custom Sync-Word to be used
*/
/*!
* \brief Sets a custom Sync-Word. Updates the sync byte.
*
* \remark Applies to LoRa modem only
*
* \param syncword 2 byte custom Sync-Word to be used
*/
void (*SetCustomSyncWord)(uint16_t syncword);
/*!
* \brief Gets the time required for the board plus radio to get out of sleep.[ms]
*
* \retval time Radio plus board wakeup time in ms.
*/
* \brief Gets the time required for the board plus radio to get out of sleep.[ms]
*
* \retval time Radio plus board wakeup time in ms.
*/
uint32_t (*GetWakeupTime)(void);
/*!
* \brief Process radio irq in background task (nRF52 & ESP32)
Expand All @@ -390,13 +390,21 @@ struct Radio_s
*/
void (*RxBoosted)(uint32_t timeout);
/*!
* \brief Sets the Rx duty cycle management parameters
*
* \remark Available on SX126x radios only.
*
* \param rxTime Structure describing reception timeout value
* \param sleepTime Structure describing sleep timeout value
*/
* \brief Enforce use of Low Datarate Optimization
*
* \remark Available on SX126x radios only.
*
* \param enforce True = Enforce Low DR optimization
*/
void (*EnforceLowDRopt)(bool enforce);
/*!
* \brief Sets the Rx duty cycle management parameters
*
* \remark Available on SX126x radios only.
*
* \param rxTime Structure describing reception timeout value
* \param sleepTime Structure describing sleep timeout value
*/
void (*SetRxDutyCycle)(uint32_t rxTime, uint32_t sleepTime);
};

Expand Down
33 changes: 29 additions & 4 deletions src/radio/sx126x/radio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
TimerEvent_t TxTimeoutTimer;
TimerEvent_t RxTimeoutTimer;

/** Enforce low datarate optimization */
bool force_low_dr_opt = false;

/*!
* @brief Initializes the radio
*
Expand Down Expand Up @@ -365,6 +368,13 @@ void RadioRxBoosted(uint32_t timeout);
*/
void RadioSetRxDutyCycle(uint32_t rxTime, uint32_t sleepTime);

/*!
* @brief Enforce usage of Low Datarate optimization
*
* @param enforce True = Enforce usage of Low Datarate optimization
*/
void RadioEnforceLowDRopt(bool enforce);

/*!
* Radio driver structure initialization
*/
Expand Down Expand Up @@ -402,7 +412,9 @@ const struct Radio_s Radio =
RadioIrqProcessAfterDeepSleep,
// Available on SX126x only
RadioRxBoosted,
RadioSetRxDutyCycle};
RadioEnforceLowDRopt,
RadioSetRxDutyCycle,
};

/*
* Local types definition
Expand Down Expand Up @@ -779,7 +791,7 @@ void RadioSetRxConfig(RadioModems_t modem, uint32_t bandwidth,
SX126x.ModulationParams.Params.LoRa.CodingRate = (RadioLoRaCodingRates_t)coderate;

if (((bandwidth == 0) && ((datarate == 11) || (datarate == 12))) ||
((bandwidth == 1) && (datarate == 12)))
((bandwidth == 1) && (datarate == 12)) || force_low_dr_opt)
{
SX126x.ModulationParams.Params.LoRa.LowDatarateOptimize = 0x01;
}
Expand Down Expand Up @@ -896,7 +908,7 @@ void RadioSetTxConfig(RadioModems_t modem, int8_t power, uint32_t fdev,
SX126x.ModulationParams.Params.LoRa.CodingRate = (RadioLoRaCodingRates_t)coderate;

if (((bandwidth == 0) && ((datarate == 11) || (datarate == 12))) ||
((bandwidth == 1) && (datarate == 12)))
((bandwidth == 1) && (datarate == 12)) || force_low_dr_opt)
{
SX126x.ModulationParams.Params.LoRa.LowDatarateOptimize = 0x01;
}
Expand Down Expand Up @@ -1230,7 +1242,8 @@ void RadioSetPublicNetwork(bool enable)
}
}

void RadioSetCustomSyncWord(uint16_t syncword){
void RadioSetCustomSyncWord(uint16_t syncword)
{
hasCustomSyncWord = true;
RadioSetModem(MODEM_LORA);
SX126xWriteRegister(REG_LR_SYNCWORD, (syncword >> 8) & 0xFF);
Expand Down Expand Up @@ -1281,6 +1294,18 @@ void RadioOnRxTimeoutIrq(void)
RadioSleep();
}

void RadioEnforceLowDRopt(bool enforce)
{
if (enforce)
{
force_low_dr_opt = true;
}
else
{
force_low_dr_opt = false;
}
}

#if defined NRF52_SERIES || defined ESP32 || defined ARDUINO_RAKWIRELESS_RAK11300
/** Semaphore used by SX126x IRQ handler to wake up LoRaWAN task */
extern SemaphoreHandle_t _lora_sem;
Expand Down

0 comments on commit 847b224

Please sign in to comment.