Skip to content
This repository was archived by the owner on Dec 23, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 39 additions & 23 deletions SlimLoRa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* along with this program. If not, see <http:// www.gnu.org/licenses/>.
*/
#include <avr/pgmspace.h>
#include <avr/eeprom.h>
#include <Arduino.h>
#include <SPI.h>

Expand All @@ -24,15 +25,15 @@
#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];
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] = {
Expand Down Expand Up @@ -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
Expand All @@ -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;
}

Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -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.
*
Expand Down Expand Up @@ -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_);
}

/**
Expand Down
27 changes: 10 additions & 17 deletions SlimLoRa.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@

#include <stddef.h>
#include <stdint.h>
#include <util/atomic.h>

#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

Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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);
Expand Down
24 changes: 0 additions & 24 deletions examples/simple/config.example.h

This file was deleted.

11 changes: 11 additions & 0 deletions examples/simple/config.ino
Original file line number Diff line number Diff line change
@@ -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
9 changes: 4 additions & 5 deletions examples/simple/simple.ino
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <stdint.h>

#include "config.h"
#include "SlimLoRa.h"

void setup() {
Expand All @@ -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() { }
void loop() { }