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
        }
    }
}

radio/radio.h

Committer:
Wayne Roberts
Date:
2018-06-11
Revision:
9:fe8e08792ae9
Parent:
8:5a5ea7cc946f

File content as of revision 9:fe8e08792ae9:

#include "sx12xx.h"
#ifdef SX127x_H 
#include "sx127x_lora.h"
#include "sx127x_fsk.h"
#endif /* SX127x_H */

#define RADIO_OSC_STARTUP_us                           1000 // [ms]
#define RADIO_SLEEP_TO_RX_us                           2000 // [ms]
#define RADIO_WAKEUP_TIME_us                           ( RADIO_OSC_STARTUP_us + RADIO_SLEEP_TO_RX_us )

typedef enum
{
    MODEM_FSK = 0,
    MODEM_LORA,
} RadioModems_t;

typedef enum
{
    RF_IDLE = 0,
    RF_RX_RUNNING,
    RF_TX_RUNNING,
    RF_CAD,
} RadioState_t;

/*!
 * \brief Radio driver callback functions
 */
typedef struct
{
    void (*Dio0_top_half)(us_timestamp_t curTime);
    /*!
     * \brief  Tx Done callback prototype.
     */
    void    ( *TxDone )(us_timestamp_t curTime);
    /*!
     * \brief  Tx Timeout callback prototype.
     */
    void    ( *TxTimeout )( void );
    /*!
     * \brief Rx Done callback prototype.
     *
     * \param [IN] payload Received buffer pointer
     * \param [IN] size    Received buffer size
     * \param [IN] rssi    RSSI value computed while receiving the frame [dBm]
     * \param [IN] snr     Raw SNR value given by the radio hardware
     *                     FSK : N/A ( set to 0 )
     *                     LoRa: SNR value in dB
     * \param [IN] curTime captured time at RxDone event occurance
     */
    void    ( *RxDone )( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr, us_timestamp_t curTime);
    /*!
     * \brief  Rx Timeout callback prototype.
     */
    void    ( *RxTimeout )( void );
    /*!
     * \brief Rx Error callback prototype.
     */
    void    ( *RxError )( void );
    /*!
     * \brief  FHSS Change Channel callback prototype.
     *
     * \param [IN] currentChannel   Index number of the current channel
     */
    void ( *FhssChangeChannel )( uint8_t currentChannel );

    /*!
     * \brief CAD Done callback prototype.
     *
     * \param [IN] channelDetected    Channel Activity detected during the CAD
     */
    void ( *CadDone ) ( bool channelActivityDetected );
} RadioEvents_t;

class Radio {
    public:
        static void SetTxContinuousWave(unsigned hz, int8_t txPower, unsigned timeout);
        static uint32_t Random(void);
        static void SetPublicNetwork(bool);
        static void Sleep(void);
        static void SetChannel(unsigned hz);
        static void SetRxMaxPayloadLength(RadioModems_t, uint8_t);
        static void Rx(unsigned timeout);
        static void Standby(void);
        static bool CheckRfFrequency(unsigned hz);
        static void Init(const RadioEvents_t*);
        static int Send(uint8_t size, timestamp_t maxListenTime, timestamp_t channelFreeTime, int rssiThresh);
        static void PrintStatus(void);
        static void UserContext(void);
#ifdef DUTY_ENABLE
        static us_timestamp_t TimeOnAir(RadioModems_t, uint8_t);
#endif /* DUTY_ENABLE */

        static void SetRxConfig(
            RadioModems_t modem,
            uint32_t bandwidth,
            uint32_t datarate,
            uint8_t coderate,
            uint32_t bandwidthAfc,
            uint16_t preambleLen,
            uint16_t symbTimeout,
            bool fixLen,
            uint8_t payloadLen,
            bool crcOn,
            bool iqInverted
        );


        static void SetTxConfig(
            RadioModems_t modem,
            int8_t power,
            uint32_t fdev, 
            uint32_t bandwidth,
            uint32_t datarate,
            uint8_t coderate,
            uint16_t preambleLen,
            bool fixLen,
            bool crcOn,
            bool iqInverted
        );

        static LowPowerTimer lpt;
#ifdef SX127x_H 
        static SX127x radio;
#elif defined(SX126x_H)
        static SX126x radio;
#endif

    private:
        static void LoRaConfig(
            uint32_t bandwidth,
            uint8_t datarate,
            uint8_t coderate,
            uint16_t preambleLen,
            bool fixLen,
            bool crcOn
#if defined(SX126x_H)
            , bool invIQ
#endif /* SX126x_H */
        );
        static void FSKConfig(uint32_t bandwidth, uint32_t datarate, uint32_t fdev, uint32_t preambleLen, bool fixLen, bool crcOn);
        static void boardInit(void);
#ifdef SX127x_H 
        static SX127x_lora lora;
        static SX127x_fsk fsk;
#endif /* SX127x_H */
#ifdef SX126x_H 
        static RadioModems_t _m_;
        static void rx_done(uint8_t, float, float);
        static void timeout_callback(bool);
        static void dio1_top_half(void);
        static PacketParams_t pp;
#endif /* SX126x_H */
        static void rfsw_callback(void);
        static void set_tx_dbm(int8_t dbm);
        static void ocp(uint8_t ma);

        static InterruptIn dio0;
        static InterruptIn dio1;
        static void dio0isr(void);
        static void dio1isr(void);
        static void dio0UserContext(void);
        static void dio1UserContext(void);
};