Skip to content

Commit 7aeb935

Browse files
committed
UNTESTED ADR back-off according to spec. Details about EU DR6
1 parent 606dbde commit 7aeb935

File tree

4 files changed

+57
-19
lines changed

4 files changed

+57
-19
lines changed

SlimLoRa.cpp

+43-9
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ void SlimLoRa::printMAC(){
322322
Serial.print(F("Rx1 DR offset\t: "));Serial.println(GetRx1DataRateOffset());
323323
Serial.print(F("Rx2 DR RAM\t: "));Serial.println(rx2_data_rate_);
324324
Serial.print(F("Rx2 DR EEPROM\t: "));Serial.println(GetRx2DataRate());
325-
Serial.print(F("ADR_ACK_cnt\t: "));Serial.println(adr_ack_counter_);
325+
Serial.print(F("ADR_ACK_cnt\t: "));Serial.println(adr_ack_limit_counter_);
326326
}
327327
#endif // DEBUG_SLIM
328328

@@ -758,7 +758,10 @@ void SlimLoRa::RfmSendPacket(uint8_t *packet, uint8_t packet_length, uint8_t cha
758758
// semi TODO fCnt don't increase if we havee UNCONFIRMED_DATA_UP and NbTrans to do.
759759
if ( NbTrans_counter == 0 ) {
760760
tx_frame_counter_++;
761-
adr_ack_counter_++;
761+
adr_ack_limit_counter_++;
762+
if ( adr_ack_limit_counter_ >= LORAWAN_ADR_ACK_LIMIT ) {
763+
adr_ack_delay_counter_++;
764+
}
762765
NbTrans_counter = NbTrans;
763766
}
764767

@@ -1274,7 +1277,9 @@ int8_t SlimLoRa::ProcessJoinAccept(uint8_t window) {
12741277
rx_frame_counter_ = 0;
12751278
SetRxFrameCounter();
12761279

1277-
adr_ack_counter_ = 0;
1280+
// reset counters. EVAL: I use this twice. Maybe better a function?
1281+
adr_ack_limit_counter_ = 0;
1282+
adr_ack_delay_counter_ = 0;
12781283
NbTrans_counter = NbTrans;
12791284

12801285
has_joined_ = true;
@@ -1379,7 +1384,11 @@ void SlimLoRa::ProcessFrameOptions(uint8_t *options, uint8_t f_options_length) {
13791384
new_rx2_dr = options[i + 1] >> 4;
13801385
tx_power = options[i + 1] & 0xF;
13811386

1387+
#if defined(EU_DR6)
13821388
if (new_rx2_dr == 0xF || (new_rx2_dr >= SF12BW125 && new_rx2_dr <= SF7BW250)) { // Reversed table index
1389+
#else
1390+
if (new_rx2_dr == 0xF || (new_rx2_dr >= SF12BW125 && new_rx2_dr <= SF7BW125)) { // Reversed table index
1391+
#endif
13831392
status |= 0x2; // DR ACK
13841393
}
13851394
// tx_power = 15 or less than 7
@@ -1433,7 +1442,11 @@ void SlimLoRa::ProcessFrameOptions(uint8_t *options, uint8_t f_options_length) {
14331442
if (new_rx1_dr_offset <= LORAWAN_EU868_RX1_DR_OFFSET_MAX) {
14341443
status |= 0x4;
14351444
}
1445+
#if defined(EU_DR6)
14361446
if (new_rx2_dr >= SF12BW125 && new_rx2_dr <= SF7BW250) { // Reversed table index
1447+
#else
1448+
if (new_rx2_dr >= SF12BW125 && new_rx2_dr <= SF7BW125) { // Reversed table index
1449+
#endif
14371450
status |= 0x2;
14381451
}
14391452

@@ -1597,8 +1610,13 @@ int8_t SlimLoRa::ProcessDownlink(uint8_t window) {
15971610

15981611
if (window == 1) {
15991612
rx1_offset_dr = data_rate_ + rx1_data_rate_offset_; // Reversed table index
1613+
#if defined(EU_DR6)
16001614
if (rx1_offset_dr > SF7BW250) {
16011615
rx1_offset_dr = SF7BW250;
1616+
#else
1617+
if (rx1_offset_dr > SF7BW125) {
1618+
rx1_offset_dr = SF7BW125;
1619+
#endif
16021620
}
16031621
rx_delay = CalculateRxDelay(rx1_offset_dr, rx1_delay_micros_);
16041622
packet_length = RfmReceivePacket(packet, sizeof(packet), channel_, rx1_offset_dr, tx_done_micros_ + rx_delay);
@@ -1670,7 +1688,8 @@ int8_t SlimLoRa::ProcessDownlink(uint8_t window) {
16701688

16711689
// We received downlink. Reset some values.
16721690
// Reset ADR acknowledge counter
1673-
adr_ack_counter_ = 0;
1691+
adr_ack_limit_counter_ = 0;
1692+
adr_ack_delay_counter_ = 0;
16741693
// Reset NbTrans
16751694
NbTrans_counter = NbTrans;
16761695

@@ -1837,14 +1856,29 @@ void SlimLoRa::Transmit(uint8_t fport, uint8_t *payload, uint8_t payload_length)
18371856
EncryptPayload(payload, payload_length, tx_frame_counter_, LORAWAN_DIRECTION_UP);
18381857

18391858
// ADR back-off
1840-
if (adr_enabled_ && adr_ack_counter_ >= LORAWAN_ADR_ACK_LIMIT + LORAWAN_ADR_ACK_DELAY) {
1859+
// TODO p. 17, after first increase if DR, increase every LORAWAN_ADR_ACK_DELAY overflow
1860+
if (adr_enabled_ && adr_ack_limit_counter_ >= LORAWAN_ADR_ACK_LIMIT && adr_ack_delay_counter_ == 0 ) {
1861+
adr_ack_delay_counter_++; // this way, this if statement will be executed only once
1862+
if ( data_rate_ > SF12BW125 ) {
1863+
data_rate_--;
1864+
}
1865+
// EVAL: I think the order is important for the HOPE
1866+
SetPower(16);
1867+
#if DEBUG_SLIM >= 1
1868+
Serial.print(F("\nADR backoff 1st stage, DR: "));Serial.print(data_rate_);
1869+
#endif
1870+
}
1871+
1872+
// Increase DR every overflow of LORAWAN_ADR_ACK_DELAY
1873+
if (adr_enabled_ && adr_ack_delay_counter_ >= LORAWAN_ADR_ACK_DELAY ) {
1874+
adr_ack_delay_counter_ = 0; // reset overflow
18411875
if ( data_rate_ > SF12BW125 ) {
1842-
data_rate_++;
1876+
data_rate_--;
18431877
}
18441878
// EVAL: I think the order is important for the HOPE
18451879
SetPower(16);
18461880
#if DEBUG_SLIM >= 1
1847-
Serial.print(F("\nADR backoff, DR: "));Serial.print(data_rate_);
1881+
Serial.print(F("\nADR backoff 2nd stage, DR: "));Serial.print(data_rate_);
18481882
#endif
18491883
}
18501884

@@ -1870,9 +1904,9 @@ void SlimLoRa::Transmit(uint8_t fport, uint8_t *payload, uint8_t payload_length)
18701904
if (adr_enabled_) {
18711905
packet[packet_length] |= LORAWAN_FCTRL_ADR;
18721906

1873-
// Request ADR if adr_ack_counter_ is over the limit of LORAWAN_ADR_ACK_LIMIT
1907+
// Request ADR if adr_ack_limit_counter_ is over the limit of LORAWAN_ADR_ACK_LIMIT
18741908
// p. 17
1875-
if (adr_ack_counter_ >= LORAWAN_ADR_ACK_LIMIT ) {
1909+
if (adr_ack_limit_counter_ >= LORAWAN_ADR_ACK_LIMIT ) {
18761910
packet[packet_length] |= LORAWAN_FCTRL_ADR_ACK_REQ;
18771911
}
18781912
}

SlimLoRa.h

+12-8
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@
266266
// LoRaWAN spreading factors
267267
// TODO for other regions. Example: DR0 for US902 is SF10BW125 and DR8 is SF12BW500
268268
// check https://www.thethingsnetwork.org/docs/lorawan/regional-parameters/
269-
#define FSK 7 // TODO only 868.8 Mhz
269+
//#define FSK 7 // TODO only 868.8 Mhz
270270
#define SF7BW250 6 // only 868.3 Mhz
271271
#define SF7BW125 5
272272
#define SF8BW125 4
@@ -299,11 +299,15 @@ class SlimLoRa {
299299
void SetPower(uint8_t power);
300300
bool GetHasJoined();
301301
void GetDevAddr(uint8_t *dev_addr);
302-
bool adr_enabled_ = true;
303-
uint8_t data_rate_ = SF7BW125;
304-
uint16_t tx_frame_counter_ = 0;
305-
uint16_t rx_frame_counter_ = 0;
306-
uint8_t adr_ack_counter_ = 0;
302+
303+
// ADR variables
304+
bool adr_enabled_;
305+
uint8_t adr_ack_limit_counter_;
306+
uint8_t adr_ack_delay_counter_;
307+
308+
uint8_t data_rate_;
309+
uint16_t tx_frame_counter_;
310+
uint16_t rx_frame_counter_;
307311
uint8_t NbTrans = NBTRANS; // changed by the LNS or by DEFINE
308312
uint8_t NbTrans_counter; // if NbTrans_counter is the same with NbTrans, send new message
309313
uint8_t pseudo_byte_;
@@ -342,8 +346,8 @@ class SlimLoRa {
342346
private:
343347
#endif
344348
uint8_t pin_nss_; // TODO TinyLoRa irg_, rst_ bat_; bat=battery level pin
345-
uint8_t channel_ = 0;
346-
uint8_t rx1_data_rate_offset_ = 0;
349+
uint8_t channel_;
350+
uint8_t rx1_data_rate_offset_;
347351
uint8_t rx2_data_rate_ = RX_SECOND_WINDOW;
348352
uint32_t rx1_delay_micros_;
349353
bool has_joined_ = false;

library.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "SlimLoRa",
3-
"version": "0.6.11",
3+
"version": "0.6.12",
44
"description": "LoRaWAN library with OTAA join, ADR support and most important MAC commands for EU868 suitable for AVR's with 32Kbytes. It uses 14kBytes of program flash instead of 52kBytes of RadioLib or 32kBytes of LMIC. Tested avr32u4 / ATmega32u4 and HopeRF 95w (SX1276) on Adafruit Feather. ABP untested. Session is stored to EEPROM. Testers wanted and PR's for other regions. It supports downlinks.",
55
"keywords": "LoRaWAN, rfm95w, hoperf, SX1276, radio, communication",
66
"repository":

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=SlimLoRa
2-
version=0.6.11
2+
version=0.6.12
33
author=clavisound
44
maintainer=clavisound
55
sentence=SlimLoRa Library

0 commit comments

Comments
 (0)