From 3ceb9e9c8dc2d6d3728f2bf3c973976fe1e23a47 Mon Sep 17 00:00:00 2001 From: clavisound-X40 Date: Fri, 19 Mar 2021 14:44:55 +0200 Subject: [PATCH 1/4] Spelling correction: CaluclateDrift vs CalculateDrift. --- SlimLoRa.cpp | 4 ++-- SlimLoRa.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/SlimLoRa.cpp b/SlimLoRa.cpp index 3bd89f7..ef4c914 100644 --- a/SlimLoRa.cpp +++ b/SlimLoRa.cpp @@ -355,7 +355,7 @@ uint8_t SlimLoRa::RfmRead(uint8_t address) { /** * Calculates the clock drift adjustment(+-5%). */ -uint32_t SlimLoRa::CaluclateDriftAdjustment(uint32_t delay, uint16_t micros_per_half_symbol) { +uint32_t SlimLoRa::CalculateDriftAdjustment(uint32_t delay, uint16_t micros_per_half_symbol) { // Clock drift uint32_t drift = delay * 5 / 100; delay -= drift; @@ -396,7 +396,7 @@ uint32_t SlimLoRa::CalculateRxDelay(uint8_t data_rate, uint32_t delay) { micros_per_half_symbol = pgm_read_word(&(kDRMicrosPerHalfSymbol[data_rate])); offset = CalculateRxWindowOffset(micros_per_half_symbol); - return CaluclateDriftAdjustment(delay + offset, micros_per_half_symbol); + return CalculateDriftAdjustment(delay + offset, micros_per_half_symbol); } /** diff --git a/SlimLoRa.h b/SlimLoRa.h index ab13d13..ff42203 100644 --- a/SlimLoRa.h +++ b/SlimLoRa.h @@ -200,7 +200,7 @@ class SlimLoRa { void RfmSendPacket(uint8_t *packet, uint8_t packet_length, uint8_t channel, uint8_t dri); void RfmWrite(uint8_t address, uint8_t data); uint8_t RfmRead(uint8_t address); - uint32_t CaluclateDriftAdjustment(uint32_t delay, uint16_t micros_per_half_symbol); + uint32_t CalculateDriftAdjustment(uint32_t delay, uint16_t micros_per_half_symbol); int32_t CalculateRxWindowOffset(int16_t micros_per_half_symbol); uint32_t CalculateRxDelay(uint8_t data_rate, uint32_t delay); bool CheckMic(uint8_t *cmic, uint8_t *rmic); From 7f36505af205fbf4dfcd12caeeb4fe94161c56ce Mon Sep 17 00:00:00 2001 From: clavisound-X40 Date: Sat, 20 Mar 2021 20:55:46 +0200 Subject: [PATCH 2/4] Compiles with arduino 1.8.12 on 32bit host for feather 32u4 target. Untested the binary. EPIC BUG with SF10-11-12 --- SlimLoRa.cpp | 37 +++++++++++++++++++++++--------- SlimLoRa.h | 25 ++++++++------------- examples/simple/SimpleLoRa.ino | 31 ++++++++++++++++++++++++++ examples/simple/config.example.h | 24 --------------------- examples/simple/config.ino | 11 ++++++++++ 5 files changed, 78 insertions(+), 50 deletions(-) create mode 100644 examples/simple/SimpleLoRa.ino delete mode 100644 examples/simple/config.example.h create mode 100644 examples/simple/config.ino diff --git a/SlimLoRa.cpp b/SlimLoRa.cpp index ef4c914..327d812 100644 --- a/SlimLoRa.cpp +++ b/SlimLoRa.cpp @@ -16,6 +16,7 @@ * along with this program. If not, see . */ #include +#include #include #include @@ -24,7 +25,7 @@ #if LORAWAN_OTAA_ENABLED extern const uint8_t DevEUI[8]; extern const uint8_t JoinEUI[8]; -extern const uint8_t NwkKey[16]; +extern const uint8_t NwkKey[16]; // For LoRaWAN-1.1 extern const uint8_t AppKey[16]; #else extern const uint8_t NwkSKey[16]; @@ -32,7 +33,7 @@ extern const uint8_t AppSKey[16]; extern const uint8_t DevAddr[4]; #endif -static SPISettings spi_settings = SPISettings(4000000, MSBFIRST, SPI_MODE0); +static SPISettings RFM_spisettings = SPISettings(4000000, MSBFIRST, SPI_MODE0); // Frequency band for europe const uint8_t PROGMEM SlimLoRa::kFrequencyTable[9][3] = { @@ -60,10 +61,11 @@ const uint8_t PROGMEM SlimLoRa::kDataRateTable[7][3] = { }; // Half symbol times -const uint16_t PROGMEM SlimLoRa::kDRMicrosPerHalfSymbol[7] = { - ((128 << 7) * MICROS_PER_SECOND + 500000) / 1000000, // SF12BW125 - ((128 << 6) * MICROS_PER_SECOND + 500000) / 1000000, // SF11BW125 - ((128 << 5) * MICROS_PER_SECOND + 500000) / 1000000, // SF10BW125 +//const uint32_t PROGMEM SlimLoRa::kDRMicrosPerHalfSymbol[7] = { +const long PROGMEM SlimLoRa::kDRMicrosPerHalfSymbol[7] = { + ((128 << 7) * MICROS_PER_SECOND + 500000) / 1000000, // SF12BW125 BUG with overflow. + ((128 << 6) * MICROS_PER_SECOND + 500000) / 1000000, // SF11BW125 BUG with overflow. + ((128 << 5) * MICROS_PER_SECOND + 500000) / 1000000, // SF10BW125 BUG with overflow. ((128 << 4) * MICROS_PER_SECOND + 500000) / 1000000, // SF9BW125 ((128 << 3) * MICROS_PER_SECOND + 500000) / 1000000, // SF8BW125 ((128 << 2) * MICROS_PER_SECOND + 500000) / 1000000, // SF7BW125 @@ -90,7 +92,7 @@ const uint8_t PROGMEM SlimLoRa::kSTable[16][16] = { {0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, 0x41, 0x99, 0x2D, 0x0F, 0xB0, 0x54, 0xBB, 0x16} }; -TinyLoRa::TinyLoRa(uint8_t pin_nss) { +SlimLoRa::SlimLoRa(uint8_t pin_nss) { pin_nss_ = pin_nss; } @@ -129,13 +131,28 @@ void SlimLoRa::Begin() { RfmWrite(RFM_REG_FIFO_RX_BASE_ADDR, 0x00); // Init MAC state +#if LORAWAN_KEEP_SESSION has_joined_ = GetHasJoined(); +#endif tx_frame_counter_ = GetTxFrameCounter(); rx_frame_counter_ = GetRxFrameCounter(); rx2_data_rate_ = GetRx2DataRate(); rx1_delay_micros_ = GetRx1Delay() * MICROS_PER_SECOND; } +void wait_until(unsigned long microsstamp) { + long delta; + + while (1) { + ATOMIC_BLOCK(ATOMIC_FORCEON) { + delta = microsstamp - micros(); + } + if (delta <= 0) { + break; + } + } +} + /** * Function for receiving a packet using the RFM * @@ -390,7 +407,7 @@ int32_t SlimLoRa::CalculateRxWindowOffset(int16_t micros_per_half_symbol) { * @return The RX delay in micros. */ uint32_t SlimLoRa::CalculateRxDelay(uint8_t data_rate, uint32_t delay) { - uint16_t micros_per_half_symbol; + uint32_t micros_per_half_symbol; int32_t offset; micros_per_half_symbol = pgm_read_word(&(kDRMicrosPerHalfSymbol[data_rate])); @@ -465,7 +482,7 @@ int8_t SlimLoRa::Join() { packet_length += 4; channel_ = pseudo_byte_ & 0x01; - RfmSendPacket(packet, packet_length, channel_, data_rate_, true); + RfmSendPacket(packet, packet_length, channel_, data_rate_); if (!ProcessJoinAccept(1)) { return 0; @@ -1083,7 +1100,7 @@ void SlimLoRa::Transmit(uint8_t fport, uint8_t *payload, uint8_t payload_length) } channel_ = pseudo_byte_ & 0x03; - RfmSendPacket(packet, packet_length, channel_, data_rate_, true); + RfmSendPacket(packet, packet_length, channel_, data_rate_); } /** diff --git a/SlimLoRa.h b/SlimLoRa.h index ff42203..5042d0f 100644 --- a/SlimLoRa.h +++ b/SlimLoRa.h @@ -3,8 +3,15 @@ #include #include +#include -#include "config.h" +// LoRaWAN ADR +#define LORAWAN_ADR_ACK_LIMIT 48 +#define LORAWAN_ADR_ACK_DELAY 16 + +// Enable LoRaWAN Over-The-Air Activation +#define LORAWAN_OTAA_ENABLED 1 +#define LORAWAN_KEEP_SESSION 1 #define MICROS_PER_SECOND 1000000 @@ -141,20 +148,6 @@ #define SF12BW125 0 -void wait_until(unsigned long microsstamp) { - long delta; - - while (1) { - ATOMIC_BLOCK(ATOMIC_FORCEON) { - delta = microsstamp - micros(); - } - - if (delta <= 0) { - break; - } - } -} - typedef struct { uint8_t length; uint8_t fopts[LORAWAN_FOPTS_MAX_SIZE]; @@ -194,7 +187,7 @@ class SlimLoRa { int8_t last_packet_snr_; static const uint8_t kFrequencyTable[9][3]; static const uint8_t kDataRateTable[7][3]; - static const uint16_t kDRMicrosPerHalfSymbol[7]; + static const long kDRMicrosPerHalfSymbol[7]; static const uint8_t kSTable[16][16]; int8_t RfmReceivePacket(uint8_t *packet, uint8_t packet_max_length, uint8_t channel, uint8_t dri, uint32_t rx_tickstamp); void RfmSendPacket(uint8_t *packet, uint8_t packet_length, uint8_t channel, uint8_t dri); diff --git a/examples/simple/SimpleLoRa.ino b/examples/simple/SimpleLoRa.ino new file mode 100644 index 0000000..391923d --- /dev/null +++ b/examples/simple/SimpleLoRa.ino @@ -0,0 +1,31 @@ +#include + +#include "SlimLoRa.h" + +#define LORAWAN_OTAA 1 // 0 for ABP. Check config.ino file in tab. + +void setup() { + uint8_t payload[3], payload_length; + uint8_t fport = 1; + SlimLoRa lora = SlimLoRa(8); + + delay(1000); + + lora.Begin(); + +#if LORAWAN_OTAA_ENABLED + while (!lora.HasJoined()) { + lora.Join(); + delay(1000); + } +#endif // LORAWAN_OTAA_ENABLED + + payload_length = sizeof(payload); + payload[0] = 0x01; + payload[1] = 0x10; + payload[2] = 0xFF; + + lora.SendData(fport, payload, payload_length); +} + +void loop() { } diff --git a/examples/simple/config.example.h b/examples/simple/config.example.h deleted file mode 100644 index 6017382..0000000 --- a/examples/simple/config.example.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef CONFIG_H -#define CONFIG_H - -#include - -// LoRaWAN ADR -#define LORAWAN_ADR_ACK_LIMIT 48 -#define LORAWAN_ADR_ACK_DELAY 16 - -// Enable LoRaWAN Over-The-Air Activation -#define LORAWAN_OTAA_ENABLED 1 -#define LORAWAN_KEEP_SESSION 0 - -#if LORAWAN_OTAA_ENABLED -const uint8_t DevEUI[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; -const uint8_t /*AppEUI*/ JoinEUI[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; -const uint8_t AppKey[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; -#else -const uint8_t NwkSKey[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; -const uint8_t AppSKey[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; -const uint8_t DevAddr[4] = { 0x00, 0x00, 0x00, 0x00 }; -#endif // LORAWAN_OTAA_ENABLED - -#endif diff --git a/examples/simple/config.ino b/examples/simple/config.ino new file mode 100644 index 0000000..369629d --- /dev/null +++ b/examples/simple/config.ino @@ -0,0 +1,11 @@ +#if LORAWAN_OTAA == 1 + uint8_t DevEUI[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; + uint8_t /*AppEUI*/ JoinEUI[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; + uint8_t AppKey[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; + // For LoRaWAN-1.1 + uint8_t NwkKey[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; +#else // ABP + uint8_t NwkSKey[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; + uint8_t AppSKey[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; + uint8_t DevAddr[4] = { 0x00, 0x00, 0x00, 0x00 }; +#endif // LORAWAN_OTAA_ENABLED From 9ac756c1858a5c72ea464d63722f0ea12a06e816 Mon Sep 17 00:00:00 2001 From: clavisound-X40 Date: Sat, 20 Mar 2021 21:42:04 +0200 Subject: [PATCH 3/4] Better housekeeping. --- examples/simple/SimpleLoRa.ino | 31 ------------------------------- examples/simple/config.ino | 2 +- examples/simple/simple.ino | 9 ++++----- 3 files changed, 5 insertions(+), 37 deletions(-) delete mode 100644 examples/simple/SimpleLoRa.ino diff --git a/examples/simple/SimpleLoRa.ino b/examples/simple/SimpleLoRa.ino deleted file mode 100644 index 391923d..0000000 --- a/examples/simple/SimpleLoRa.ino +++ /dev/null @@ -1,31 +0,0 @@ -#include - -#include "SlimLoRa.h" - -#define LORAWAN_OTAA 1 // 0 for ABP. Check config.ino file in tab. - -void setup() { - uint8_t payload[3], payload_length; - uint8_t fport = 1; - SlimLoRa lora = SlimLoRa(8); - - delay(1000); - - lora.Begin(); - -#if LORAWAN_OTAA_ENABLED - while (!lora.HasJoined()) { - lora.Join(); - delay(1000); - } -#endif // LORAWAN_OTAA_ENABLED - - payload_length = sizeof(payload); - payload[0] = 0x01; - payload[1] = 0x10; - payload[2] = 0xFF; - - lora.SendData(fport, payload, payload_length); -} - -void loop() { } diff --git a/examples/simple/config.ino b/examples/simple/config.ino index 369629d..0efaee4 100644 --- a/examples/simple/config.ino +++ b/examples/simple/config.ino @@ -1,4 +1,4 @@ -#if LORAWAN_OTAA == 1 +#if LORAWAN_OTAA_ENABLED uint8_t DevEUI[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; uint8_t /*AppEUI*/ JoinEUI[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; uint8_t AppKey[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; diff --git a/examples/simple/simple.ino b/examples/simple/simple.ino index a2503f5..169432d 100644 --- a/examples/simple/simple.ino +++ b/examples/simple/simple.ino @@ -1,6 +1,5 @@ #include -#include "config.h" #include "SlimLoRa.h" void setup() { @@ -20,11 +19,11 @@ void setup() { #endif // LORAWAN_OTAA_ENABLED payload_length = sizeof(payload); - payload[0] = 0xAB; - payload[1] = 0xCD; - payload[2] = 0xEF; + payload[0] = 0x01; + payload[1] = 0x10; + payload[2] = 0xFF; lora.SendData(fport, payload, payload_length); } -void loop() { } \ No newline at end of file +void loop() { } From 83dbe7258f7a7f82dd1a30b177f5c50aa795ca47 Mon Sep 17 00:00:00 2001 From: clavisound-X40 Date: Mon, 22 Mar 2021 13:52:29 +0200 Subject: [PATCH 4/4] 1. Revert kDRMicrosPerHalfSymbol to uint32_t. ABP now works: move CheckMic outside of #if OTAA_ENABLED. --- SlimLoRa.cpp | 25 ++++++++++++------------- SlimLoRa.h | 2 +- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/SlimLoRa.cpp b/SlimLoRa.cpp index 327d812..331a864 100644 --- a/SlimLoRa.cpp +++ b/SlimLoRa.cpp @@ -61,8 +61,7 @@ const uint8_t PROGMEM SlimLoRa::kDataRateTable[7][3] = { }; // Half symbol times -//const uint32_t PROGMEM SlimLoRa::kDRMicrosPerHalfSymbol[7] = { -const long PROGMEM SlimLoRa::kDRMicrosPerHalfSymbol[7] = { +const uint32_t PROGMEM SlimLoRa::kDRMicrosPerHalfSymbol[7] = { ((128 << 7) * MICROS_PER_SECOND + 500000) / 1000000, // SF12BW125 BUG with overflow. ((128 << 6) * MICROS_PER_SECOND + 500000) / 1000000, // SF11BW125 BUG with overflow. ((128 << 5) * MICROS_PER_SECOND + 500000) / 1000000, // SF10BW125 BUG with overflow. @@ -423,6 +422,17 @@ void SlimLoRa::SetAdrEnabled(bool enabled) { adr_enabled_ = enabled; } +/** + * Validates the calculated 4-byte MIC against the received 4-byte MIC. + * + * @param cmic Calculated 4-byte MIC. + * @param rmic Received 4-byte MIC. + */ +bool SlimLoRa::CheckMic(uint8_t *cmic, uint8_t *rmic) { + return cmic[0] == rmic[0] && cmic[1] == rmic[1] + && cmic[2] == rmic[2] && cmic[3] == rmic[3]; +} + #if LORAWAN_OTAA_ENABLED /** * Check if the device joined a LoRaWAN network. @@ -491,17 +501,6 @@ int8_t SlimLoRa::Join() { return ProcessJoinAccept(2); } -/** - * Validates the calculated 4-byte MIC against the received 4-byte MIC. - * - * @param cmic Calculated 4-byte MIC. - * @param rmic Received 4-byte MIC. - */ -bool SlimLoRa::CheckMic(uint8_t *cmic, uint8_t *rmic) { - return cmic[0] == rmic[0] && cmic[1] == rmic[1] - && cmic[2] == rmic[2] && cmic[3] == rmic[3]; -} - /** * Processes LoRaWAN 1.0 JoinAccept message. * diff --git a/SlimLoRa.h b/SlimLoRa.h index 5042d0f..ed15f00 100644 --- a/SlimLoRa.h +++ b/SlimLoRa.h @@ -187,7 +187,7 @@ class SlimLoRa { int8_t last_packet_snr_; static const uint8_t kFrequencyTable[9][3]; static const uint8_t kDataRateTable[7][3]; - static const long kDRMicrosPerHalfSymbol[7]; + static const uint32_t kDRMicrosPerHalfSymbol[7]; static const uint8_t kSTable[16][16]; int8_t RfmReceivePacket(uint8_t *packet, uint8_t packet_max_length, uint8_t channel, uint8_t dri, uint32_t rx_tickstamp); void RfmSendPacket(uint8_t *packet, uint8_t packet_length, uint8_t channel, uint8_t dri);