diff --git a/SlimLoRa.cpp b/SlimLoRa.cpp
index 3bd89f7..331a864 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,10 @@ 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] = {
+ ((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 +91,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 +130,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
*
@@ -355,7 +371,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;
@@ -390,13 +406,13 @@ 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]));
offset = CalculateRxWindowOffset(micros_per_half_symbol);
- return CaluclateDriftAdjustment(delay + offset, micros_per_half_symbol);
+ return CalculateDriftAdjustment(delay + offset, micros_per_half_symbol);
}
/**
@@ -406,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.
@@ -465,7 +492,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;
@@ -474,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.
*
@@ -1083,7 +1099,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 ab13d13..ed15f00 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,13 +187,13 @@ 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 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);
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);
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..0efaee4
--- /dev/null
+++ b/examples/simple/config.ino
@@ -0,0 +1,11 @@
+#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 };
+ // 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
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() { }