LoRaWAN end device MAC layer for SX1272 and SX1276. Supports LoRaWAN-1.0 and LoRaWAN-1.1

Dependencies:   sx12xx_hal

Dependents:   LoRaWAN-SanJose_Bootcamp LoRaWAN-grove-cayenne LoRaWAN-classC-demo LoRaWAN-grove-cayenne ... more

radio chip selection

Radio chip driver is not included, because two options are available.
If you're using SX1272 or SX1276, then import sx127x driver into your program.
if you're using SX1261 or SX1262, then import sx126x driver into your program.
If you're using NAmote72 or Murata discovery, then you must import only sx127x driver.

application project requirements

This library requires mbed TLS to be enabled.
The file mbed_app.json must be present in the project using this library:

{
    "macros": [ "MBEDTLS_CMAC_C" ]
}

regional PHY selection

All end device configuration is done in Commissioning.h, define desired radio frequency band of operation in this header file.
Commissioning.h is located in the application using this library.

end device provisioning

End device is provisioned by editing Commissioning.h in the application which is using this library
To use LoRaWAN-1.0 OTA: make sure LORAWAN_ROOT_APPKEY is undefined.
To use LoRaWAN-1.1 OTA, define LORAWAN_ROOT_APPKEY.
To select OTA operation, define LORAWAN_JOIN_EUI, then LORAWAN_DEVICE_EUI must be defined, along with root key(s).
To select ABP operation, undefine LORAWAN_JOIN_EUI: then define session keys

LoRaWAN 1.0 nameLoRaWAN 1.1 nameComissioning.h defnedescription
OTADevEUIDevEUILORAWAN_DEVICE_EUIuniquely identifies end device
OTAAppEUIJoinEUILORAWAN_JOIN_EUI
OTAAppKeyNwkKeyLORAWAN_ROOT_NWKKEYroot key for network server
OTA(note 1)AppKeyLORAWAN_ROOT_APPKEYroot key for application server
ABPNwkSKey(note 3)LORAWAN_FNwkSIntKeynetwork session key
ABP(note 2)SNwkSIntKeyLORAWAN_SNwkSIntKeymac layer network integrity key
ABP(note 2)NwkSEncKeyLORAWAN_NwkSEncKeynetwork session encryption key
ABP(note 2)FNwkSIntKeyLORAWAN_FNwkSIntKeyforwarding network session integrity key
ABPAppSKeyAppSKeyLORAWAN_APPSKEYapplication session encryption key

(note 1): LoRaWAN-1.0 OTA uses a single root key for both network server and application server.

In LoRaWAN-1.0 OTA: the single root AppKey is used to generate NwkSkey and AppSKey.
(note 2): In LoRaWAN-1.0 (both OTA and ABP) SNwkSIntKey, NwkSEncKey. FNwkSIntKey are of same value and are collectively known as NwkSKey.
(note 3): LoRaWAN-1.0 uses single network session key, LoRaWAN-1.1 uses 3 network session keys. Both use a unique application session key.


In LoRaWAN-1.1 OTA: the root NwkKey is used to generate SNwkSIntKey, NwkSEncKey, FNwkSIntKey
In LoRaWAN-1.1 OTA: the root AppKey is used to generate AppSKey


in ABP mode, the DevAddr, and session keys are fixed (never change), and frame counters never reset to zero.
ABP operation has no concept of: root keys, or DevEUI or JoinEUI/AppEUI.
in OTA mode, the DevAddr and session keys are assigned at join procedure, and frame counters reset at join.

eeprom

This library includes eeprom driver to support non-volatile storage required by LoRaWAN specification.
Currently eeprom is implemented for STM32L1 family and STM32L0 family.
Writing of values are wear-leveled to increase endurance; each write operation circulates across several memory locations. A read operation returns the highest value found. This simple method is used for sequence numbers which only increase.

value nameused in
DevNonceOTAfor Join request (note 1)
RJcount1OTAfor ReJoin Type 1 request
FCntUpABPuplink frame counter
NFCntDownABPdownlink frame counter
AFCntDownABPdownlink frame counter

AFCntDown is only used in LoRaWAN-1.1 when application payload is present in downlink and FPort > 0.
NFCntDown is used in LoRaWAN-1.1 when FPort is zero in downlink or application payload not present.
NFCntDown is the only downlink frame counter used in LoRaWAN-1.0
(note 1) OTA DevNonce is random number in LoRaWAN-1.0, therefore not stored in eeprom. DevNonce in LoRaWAN-1.1 is forever increasing (non-volatile) number upon each join request,.
RJcount0 is only stored in RAM because the value resets upon new session from JoinAccept, therefore not stored in eeprom.
Frame counters in OTA mode reset upon new session in join request, therefore are stored in RAM instead of eeprom for OTA.

radio driver support

When SX127x driver is used, both SX1272 and SX1276 are supported without defining at compile time. The chip is detected at start-up.
Supported radio platforms:


Alternately, when SX126x driver is imported, the SX126xDVK1xAS board is used.

low-speed clock oscillator selection

LoRaWAN uses 32768Hz crystal to permit low-power operation.
However, some mbed targets might revert to low-speed internal oscillator, which is not accurate enough for LoRaWAN operation.
An oscillator check is performed at initialization; program will not start if internal oscillator is used.
To force LSE watch crystal, add to mbed_app.json

{
    "macros": [ "MBEDTLS_CMAC_C" ],
    "target_overrides": {
        "<your-target>": {
            "target.lse_available": true
        }
    }
}

TARGET_STM32L0/eeprom.c

Committer:
Wayne Roberts
Date:
2018-08-20
Revision:
12:0f28f2e7c35e
Parent:
0:6b3ac9c5a042

File content as of revision 12:0f28f2e7c35e:

#include <stdint.h>
#include "eeprom.h"
#include "Commissioning.h"

#include <stm32l0xx.h>
#define DEVNONCE_BASE           DATA_EEPROM_BASE

#define DEVNONCE_END            (DEVNONCE_BASE + 16)  /* eight uint16_t values */
#define RJCOUNT1_BASE           DEVNONCE_END
#define RJCOUNT1_END            (RJCOUNT1_BASE + 16)
#define FCNTUP_BASE             RJCOUNT1_END
#define FCNTUP_END              (FCNTUP_BASE + 32)     /* eight uint32_t values */
#define NFCNTDWN_BASE           FCNTUP_END
#define NFCNTDWN_END            (NFCNTDWN_BASE + 32)     /* eight uint32_t values */
#define AFCNTDWN_BASE           NFCNTDWN_END
#define AFCNTDWN_END            (AFCNTDWN_BASE + 32)     /* eight uint32_t values */

#if defined(LORAWAN_JOIN_EUI) && defined(LORAWAN_ROOT_APPKEY)
    #define JOINNONCE_BASE          AFCNTDWN_END
    #define JOINNONCE_END           (JOINNONCE_BASE + 32)
#endif  // 1v1 OTA

#ifdef LORAWAN_JOIN_EUI
static uint16_t eeprom_read_halfword(uint32_t baseAddr, uint32_t endAddr)
{
    uint16_t* u16_ptr = (uint16_t*)baseAddr;
    uint16_t max_val = 0;
    while (u16_ptr < (uint16_t*)endAddr) {
        if (*u16_ptr > max_val)
            max_val = *u16_ptr;

        u16_ptr++;
    }
    return max_val;
}
#endif /* LORAWAN_JOIN_EUI  */

#ifndef LORAWAN_JOIN_EUI
static uint32_t eeprom_read_word(uint32_t baseAddr, uint32_t endAddr)
{
    uint32_t* u32_ptr = (uint32_t*)baseAddr;
    uint32_t max_val = 0;
    while (u32_ptr < (uint32_t*)endAddr) {
        if (*u32_ptr > max_val)
            max_val = *u32_ptr;

        u32_ptr++;
    }
    return max_val;
}

#endif /* !LORAWAN_JOIN_EUI */

static void _getmax_word(uint32_t baseAddr, uint32_t endAddr, uint32_t* max_val_at, uint32_t* max_val)
{
    uint32_t* u32_ptr = (uint32_t*)baseAddr;

    while (u32_ptr < (uint32_t*)endAddr) {
        if (*u32_ptr > *max_val) {
            *max_val = *u32_ptr;
            *max_val_at = (uint32_t)u32_ptr;
        }

        u32_ptr++;
    }

    if (*max_val_at == 0) {
        /* first time */
        *max_val_at = baseAddr;
    } else {
        *max_val_at += sizeof(uint32_t);
        if (*max_val_at == endAddr)
            *max_val_at = baseAddr;
    }
}

static int _write_word(uint32_t baseAddr, uint32_t endAddr, uint32_t value)
{
    //bool erased;
    HAL_StatusTypeDef status;
    uint32_t max_val_at = 0;
    uint32_t max_val = 0;
    int i;

    _getmax_word(baseAddr, endAddr, &max_val_at, &max_val);

    if (HAL_FLASHEx_DATAEEPROM_Unlock() != HAL_OK) {
        return -2;
    }

    for (i = 0; i < 10; ) {
        status = HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_WORD, max_val_at, value);
        if (status != HAL_OK) {
            if (i == 9 || status != HAL_ERROR) {
                HAL_FLASHEx_DATAEEPROM_Lock();
                return -4;
            }
        } else
            break;
        if (++i == 10)
            return -6;
    }

    if (HAL_FLASHEx_DATAEEPROM_Lock() != HAL_OK)
        return -5;

    return 0;
}

uint32_t eeprom_read(eeprom_value_e ev)
{
    switch (ev) {
#ifdef LORAWAN_JOIN_EUI
        case EEPROM_DEVNONCE:
            return eeprom_read_halfword(DEVNONCE_BASE, DEVNONCE_END);
        case EEPROM_RJCOUNT1:
            return eeprom_read_halfword(RJCOUNT1_BASE, RJCOUNT1_END);
    #ifdef LORAWAN_ROOT_APPKEY
        case EEPROM_JOINNONCE:
            return eeprom_read_halfword(JOINNONCE_BASE, JOINNONCE_END);
    #endif /* LORAWAN_ROOT_APPKEY */
#else
        case EEPROM_FCNTUP:
            return eeprom_read_word(FCNTUP_BASE, FCNTUP_END);
        case EEPROM_NFCNTDWN:
            return eeprom_read_word(NFCNTDWN_BASE, NFCNTDWN_END);
        case EEPROM_AFCNTDWN:
            return eeprom_read_word(AFCNTDWN_BASE, AFCNTDWN_END);
#endif /* !LORAWAN_JOIN_EUI */
        default: return 0;
    }
}

static void _getmax_halfword(uint32_t baseAddr, uint32_t endAddr, uint32_t* _max_val_at, uint32_t* max_val)
{
    uint16_t* u16_ptr = (uint16_t*)baseAddr;
    uint16_t* max_val_at = NULL;

    while (u16_ptr < (uint16_t*)endAddr) {
        if (*u16_ptr > *max_val) {
            *max_val = *u16_ptr;
            max_val_at = u16_ptr;
        }

        u16_ptr++;
    }

    if (max_val_at == NULL) {
        /* first time */
        max_val_at = (uint16_t*)baseAddr;
    } else {
        if (++max_val_at == (uint16_t*)endAddr)
            max_val_at = (uint16_t*)baseAddr;
    }

    *_max_val_at = (uint32_t)max_val_at;
}


static int increment_eeprom(uint32_t type, uint32_t baseAddr, uint32_t endAddr)
{
    //bool erased;
    HAL_StatusTypeDef status;
    uint32_t max_val_at = 0;
    uint32_t max_val = 0;
    int i;

    if (type == FLASH_TYPEPROGRAMDATA_HALFWORD)
         _getmax_halfword(baseAddr, endAddr, &max_val_at, &max_val);
    else if (type == FLASH_TYPEPROGRAMDATA_WORD)
         _getmax_word(baseAddr, endAddr, &max_val_at, &max_val);
    else
        return -1;
    /* max_val_at now points to oldest */

    if (HAL_FLASHEx_DATAEEPROM_Unlock() != HAL_OK)
        return -2;

    for (i = 0; i < 10; ) {
        status = HAL_FLASHEx_DATAEEPROM_Program(type, max_val_at, max_val + 1);
        if (status != HAL_OK) {
            if (i == 9 || status != HAL_ERROR) {
                HAL_FLASHEx_DATAEEPROM_Lock();
                return -4;
            }
        } else
            break;
        if (++i == 10)
            return -6;
    }

    if (HAL_FLASHEx_DATAEEPROM_Lock() != HAL_OK)
        return -5;

    return 0;
}

int eeprom_increment_value(eeprom_value_e ev)
{
    switch (ev) {
#ifdef LORAWAN_JOIN_EUI
        case EEPROM_DEVNONCE:
            return increment_eeprom(FLASH_TYPEPROGRAMDATA_HALFWORD, DEVNONCE_BASE, DEVNONCE_END);
        case EEPROM_RJCOUNT1:
            return increment_eeprom(FLASH_TYPEPROGRAMDATA_HALFWORD, RJCOUNT1_BASE, RJCOUNT1_END);
    #ifdef LORAWAN_ROOT_APPKEY
        case EEPROM_JOINNONCE:
            return increment_eeprom(FLASH_TYPEPROGRAMDATA_WORD, JOINNONCE_BASE, JOINNONCE_END);
    #endif /* LORAWAN_ROOT_APPKEY */
#else
        case EEPROM_FCNTUP:
            return increment_eeprom(FLASH_TYPEPROGRAMDATA_WORD, FCNTUP_BASE, FCNTUP_END);
        case EEPROM_NFCNTDWN:
            return increment_eeprom(FLASH_TYPEPROGRAMDATA_WORD, NFCNTDWN_BASE, NFCNTDWN_END);
        case EEPROM_AFCNTDWN:
            return increment_eeprom(FLASH_TYPEPROGRAMDATA_WORD, AFCNTDWN_BASE, AFCNTDWN_END);
#endif /* !LORAWAN_JOIN_EUI */
        default:
            return -1;
    }
}

int eeprom_write_word(eeprom_value_e ev, uint32_t value)
{
    switch (ev) {
#ifdef LORAWAN_JOIN_EUI
        case EEPROM_DEVNONCE:
        case EEPROM_RJCOUNT1:
            return -1;
    #ifdef LORAWAN_ROOT_APPKEY
        case EEPROM_JOINNONCE:
            return _write_word(JOINNONCE_BASE, JOINNONCE_END, value);
    #endif /* LORAWAN_ROOT_APPKEY */
#else
        case EEPROM_FCNTUP:
            return _write_word(FCNTUP_BASE, FCNTUP_END, value);
        case EEPROM_NFCNTDWN:
            return _write_word(NFCNTDWN_BASE, NFCNTDWN_END, value);
        case EEPROM_AFCNTDWN:
            return _write_word(AFCNTDWN_BASE, AFCNTDWN_END, value);
#endif /* !LORAWAN_JOIN_EUI */
        default:
            return -1;
    }
}

static int _eeprom_clear(uint32_t baseAddr, uint32_t endAddr)
{
    int ret = 0;
    HAL_StatusTypeDef status;
    uint32_t* u32_ptr = (uint32_t*)baseAddr;

    if (HAL_FLASHEx_DATAEEPROM_Unlock() != HAL_OK) {
        HAL_FLASHEx_DATAEEPROM_Lock();
        return -1;
    }

    while (u32_ptr < (uint32_t*)endAddr) {
        if (*u32_ptr != 0) {
            status = HAL_FLASHEx_DATAEEPROM_Erase((uint32_t)u32_ptr);
            if (status != HAL_OK) {
                ret = -3;
            }
        }
        u32_ptr++;
    }

    HAL_FLASHEx_DATAEEPROM_Lock();

    return ret;
}

int eeprom_clear(eeprom_value_e ev)
{
    switch (ev) {
#ifdef LORAWAN_JOIN_EUI
        case EEPROM_DEVNONCE:
            return _eeprom_clear(DEVNONCE_BASE, DEVNONCE_END);
        case EEPROM_RJCOUNT1:
            return _eeprom_clear(RJCOUNT1_BASE, RJCOUNT1_END);
    #ifdef LORAWAN_ROOT_APPKEY
        case EEPROM_JOINNONCE:
            return _eeprom_clear(JOINNONCE_BASE, JOINNONCE_END);
    #endif /* LORAWAN_ROOT_APPKEY */
#else
        case EEPROM_FCNTUP:
            return _eeprom_clear(FCNTUP_BASE, FCNTUP_END);
        case EEPROM_NFCNTDWN:
            return _eeprom_clear(NFCNTDWN_BASE, NFCNTDWN_END);
        case EEPROM_AFCNTDWN:
            return _eeprom_clear(AFCNTDWN_BASE, AFCNTDWN_END);
#endif /* !LORAWAN_JOIN_EUI */
        default: return 0;
    }
}