Application LORAWAN : Device STM32L073 +SX1272MB2xAS board

Dependencies:   mbed LoRaWAN-lib SX1272Lib

Fork of LoRaWAN-demo-72 by Semtech

Files at this revision

API Documentation at this revision

Comitter:
mluis
Date:
Mon Apr 24 13:29:34 2017 +0000
Parent:
6:0e8d45c5cbbc
Child:
8:99ba7eb13376
Commit message:
WARNING: Radio API timings changed from micro-seconds to milliseconds; ; Synchronized with https://github.com/Lora-net/LoRaMac-node git revision e506c246652fa44c3f24cecb89d0707b49ece739; Updated all libraries to the latest versions

Changed in this revision

app/Comissioning.h Show diff for this revision Revisions of this file
app/Commissioning.h Show annotated file Show diff for this revision Revisions of this file
app/main.cpp Show annotated file Show diff for this revision Revisions of this file
board/board.cpp Show annotated file Show diff for this revision Revisions of this file
board/board.h Show annotated file Show diff for this revision Revisions of this file
mac/LoRaWAN-lib.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
radio/SX1272Lib.lib Show annotated file Show diff for this revision Revisions of this file
system/timer.cpp Show annotated file Show diff for this revision Revisions of this file
system/timer.h Show annotated file Show diff for this revision Revisions of this file
--- a/app/Comissioning.h	Tue Apr 04 13:36:56 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,69 +0,0 @@
-/*
- / _____)             _              | |
-( (____  _____ ____ _| |_ _____  ____| |__
- \____ \| ___ |    (_   _) ___ |/ ___)  _ \
- _____) ) ____| | | || |_| ____( (___| | | |
-(______/|_____)_|_|_| \__)_____)\____)_| |_|
-    (C)2015 Semtech
-
-Description: End device commissioning parameters
-
-License: Revised BSD License, see LICENSE.TXT file include in the project
-
-Maintainer: Miguel Luis and Gregory Cristian
-*/
-#ifndef __LORA_COMMISSIONING_H__
-#define __LORA_COMMISSIONING_H__
-
-/*!
- * When set to 1 the application uses the Over-the-Air activation procedure
- * When set to 0 the application uses the Personalization activation procedure
- */
-#define OVER_THE_AIR_ACTIVATION                     1
-
-/*!
- * Indicates if the end-device is to be connected to a private or public network
- */
-#define LORAWAN_PUBLIC_NETWORK                      true
-
-/*!
- * IEEE Organizationally Unique Identifier ( OUI ) (big endian)
- */
-#define IEEE_OUI                                    0x11, 0x22, 0x33
-
-/*!
- * Mote device IEEE EUI (big endian)
- */
-#define LORAWAN_DEVICE_EUI                          { IEEE_OUI, 0x44, 0x55, 0x66, 0x77, 0x88 }
-
-/*!
- * Application IEEE EUI (big endian)
- */
-#define LORAWAN_APPLICATION_EUI                     { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
-
-/*!
- * AES encryption/decryption cipher application key
- */
-#define LORAWAN_APPLICATION_KEY                     { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C }
-
-/*!
- * Current network ID
- */
-#define LORAWAN_NETWORK_ID                          ( uint32_t )0
-
-/*!
- * Device address on the network (big endian)
- */
-#define LORAWAN_DEVICE_ADDRESS                      ( uint32_t )0x12345678
-
-/*!
- * AES encryption/decryption cipher network session key
- */
-#define LORAWAN_NWKSKEY                             { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C }
-
-/*!
- * AES encryption/decryption cipher application session key
- */
-#define LORAWAN_APPSKEY                             { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C }
-
-#endif // __LORA_COMMISSIONING_H__
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/Commissioning.h	Mon Apr 24 13:29:34 2017 +0000
@@ -0,0 +1,70 @@
+/*
+ / _____)             _              | |
+( (____  _____ ____ _| |_ _____  ____| |__
+ \____ \| ___ |    (_   _) ___ |/ ___)  _ \
+ _____) ) ____| | | || |_| ____( (___| | | |
+(______/|_____)_|_|_| \__)_____)\____)_| |_|
+    (C)2015 Semtech
+
+Description: End device commissioning parameters
+
+License: Revised BSD License, see LICENSE.TXT file include in the project
+
+Maintainer: Miguel Luis and Gregory Cristian
+*/
+#ifndef __LORA_COMMISSIONING_H__
+#define __LORA_COMMISSIONING_H__
+
+/*!
+ * When set to 1 the application uses the Over-the-Air activation procedure
+ * When set to 0 the application uses the Personalization activation procedure
+ */
+#define OVER_THE_AIR_ACTIVATION                     1
+
+/*!
+ * Indicates if the end-device is to be connected to a private or public network
+ */
+#define LORAWAN_PUBLIC_NETWORK                      true
+
+/*!
+ * IEEE Organizationally Unique Identifier ( OUI ) (big endian)
+ * \remark This is unique to a company or organization
+ */
+#define IEEE_OUI                                    0x11, 0x22, 0x33
+
+/*!
+ * Mote device IEEE EUI (big endian)
+ */
+#define LORAWAN_DEVICE_EUI                          { IEEE_OUI, 0x44, 0x55, 0x66, 0x77, 0x88 }
+
+/*!
+ * Application IEEE EUI (big endian)
+ */
+#define LORAWAN_APPLICATION_EUI                     { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
+
+/*!
+ * AES encryption/decryption cipher application key
+ */
+#define LORAWAN_APPLICATION_KEY                     { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C }
+
+/*!
+ * Current network ID
+ */
+#define LORAWAN_NETWORK_ID                          ( uint32_t )0
+
+/*!
+ * Device address on the network (big endian)
+ */
+#define LORAWAN_DEVICE_ADDRESS                      ( uint32_t )0x12345678
+
+/*!
+ * AES encryption/decryption cipher network session key
+ */
+#define LORAWAN_NWKSKEY                             { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C }
+
+/*!
+ * AES encryption/decryption cipher application session key
+ */
+#define LORAWAN_APPSKEY                             { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C }
+
+#endif // __LORA_COMMISSIONING_H__
--- a/app/main.cpp	Tue Apr 04 13:36:56 2017 +0000
+++ b/app/main.cpp	Mon Apr 24 13:29:34 2017 +0000
@@ -17,19 +17,19 @@
 #include "radio.h"
 
 #include "LoRaMac.h"
-#include "Comissioning.h"
+#include "Commissioning.h"
 #include "SerialDisplay.h"
 
 /*!
- * Defines the application data transmission duty cycle. 5s, value in [us].
+ * Defines the application data transmission duty cycle. 5s, value in [ms].
  */
-#define APP_TX_DUTYCYCLE                            5000000
+#define APP_TX_DUTYCYCLE                            5000
 
 /*!
  * Defines a random delay for application data transmission duty cycle. 1s,
- * value in [us].
+ * value in [ms].
  */
-#define APP_TX_DUTYCYCLE_RND                        1000000
+#define APP_TX_DUTYCYCLE_RND                        1000
 
 /*!
  * Default datarate
@@ -57,11 +57,11 @@
  *
  * \remark Please note that ETSI mandates duty cycled transmissions. Use only for test purposes
  */
-#define LORAWAN_DUTYCYCLE_ON                        true
+#define LORAWAN_DUTYCYCLE_ON                        false
 
 #define USE_SEMTECH_DEFAULT_CHANNEL_LINEUP          1
 
-#if( USE_SEMTECH_DEFAULT_CHANNEL_LINEUP == 1 ) 
+#if( USE_SEMTECH_DEFAULT_CHANNEL_LINEUP == 1 )
 
 #define LC4                { 867100000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
 #define LC5                { 867300000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
@@ -168,7 +168,7 @@
 /*!
  * Device states
  */
-static enum eDevicState
+static enum eDeviceState
 {
     DEVICE_STATE_INIT,
     DEVICE_STATE_JOIN,
@@ -340,7 +340,7 @@
 {
     McpsReq_t mcpsReq;
     LoRaMacTxInfo_t txInfo;
-    
+
     if( LoRaMacQueryTxPossible( AppDataSize, &txInfo ) != LORAMAC_STATUS_OK )
     {
         // Send empty frame in order to flush MAC commands
@@ -583,7 +583,7 @@
                     ComplianceTest.NbGateways = 0;
                     ComplianceTest.Running = true;
                     ComplianceTest.State = 1;
-                    
+
                     MibRequestConfirm_t mibReq;
                     mibReq.Type = MIB_ADR;
                     mibReq.Param.AdrEnable = true;
@@ -605,7 +605,7 @@
                     AppDataSize = LORAWAN_APP_DATA_SIZE;
                     ComplianceTest.DownLinkCounter = 0;
                     ComplianceTest.Running = false;
-                    
+
                     MibRequestConfirm_t mibReq;
                     mibReq.Type = MIB_ADR;
                     mibReq.Param.AdrEnable = LORAWAN_ADR_ON;
@@ -645,16 +645,53 @@
                     {
                         MlmeReq_t mlmeReq;
 
+                        // Disable TestMode and revert back to normal operation
+                        IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON;
+                        AppPort = LORAWAN_APP_PORT;
+                        AppDataSize = LORAWAN_APP_DATA_SIZE;
+                        ComplianceTest.DownLinkCounter = 0;
+                        ComplianceTest.Running = false;
+
+                        MibRequestConfirm_t mibReq;
+                        mibReq.Type = MIB_ADR;
+                        mibReq.Param.AdrEnable = LORAWAN_ADR_ON;
+                        LoRaMacMibSetRequestConfirm( &mibReq );
+#if defined( USE_BAND_868 )
+                        LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON );
+#endif
+
                         mlmeReq.Type = MLME_JOIN;
 
                         mlmeReq.Req.Join.DevEui = DevEui;
                         mlmeReq.Req.Join.AppEui = AppEui;
                         mlmeReq.Req.Join.AppKey = AppKey;
+                        mlmeReq.Req.Join.NbTrials = 3;
 
                         LoRaMacMlmeRequest( &mlmeReq );
                         DeviceState = DEVICE_STATE_SLEEP;
                     }
                     break;
+                case 7: // (x)
+                    {
+                        if( mcpsIndication->BufferSize == 3 )
+                        {
+                            MlmeReq_t mlmeReq;
+                            mlmeReq.Type = MLME_TXCW;
+                            mlmeReq.Req.TxCw.Timeout = ( uint16_t )( ( mcpsIndication->Buffer[1] << 8 ) | mcpsIndication->Buffer[2] );
+                            LoRaMacMlmeRequest( &mlmeReq );
+                        }
+                        else if( mcpsIndication->BufferSize == 7 )
+                        {
+                            MlmeReq_t mlmeReq;
+                            mlmeReq.Type = MLME_TXCW_1;
+                            mlmeReq.Req.TxCw.Timeout = ( uint16_t )( ( mcpsIndication->Buffer[1] << 8 ) | mcpsIndication->Buffer[2] );
+                            mlmeReq.Req.TxCw.Frequency = ( uint32_t )( ( mcpsIndication->Buffer[3] << 16 ) | ( mcpsIndication->Buffer[4] << 8 ) | mcpsIndication->Buffer[5] ) * 100;
+                            mlmeReq.Req.TxCw.Power = mcpsIndication->Buffer[6];
+                            LoRaMacMlmeRequest( &mlmeReq );
+                        }
+                        ComplianceTest.State = 1;
+                    }
+                    break;
                 default:
                     break;
                 }
@@ -680,19 +717,26 @@
  */
 static void MlmeConfirm( MlmeConfirm_t *mlmeConfirm )
 {
-    if( mlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
+    switch( mlmeConfirm->MlmeRequest )
     {
-        switch( mlmeConfirm->MlmeRequest )
+        case MLME_JOIN:
         {
-            case MLME_JOIN:
+            if( mlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
             {
                 // Status is OK, node has joined the network
                 IsNetworkJoinedStatusUpdate = true;
                 DeviceState = DEVICE_STATE_SEND;
-                NextTx = true;
-                break;
+            }
+            else
+            {
+                // Join was not successful. Try to join again
+                DeviceState = DEVICE_STATE_JOIN;
             }
-            case MLME_LINK_CHECK:
+            break;
+        }
+        case MLME_LINK_CHECK:
+        {
+            if( mlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
             {
                 // Check DemodMargin
                 // Check NbGateways
@@ -702,11 +746,11 @@
                     ComplianceTest.DemodMargin = mlmeConfirm->DemodMargin;
                     ComplianceTest.NbGateways = mlmeConfirm->NbGateways;
                 }
-                break;
             }
-            default:
-                break;
+            break;
         }
+        default:
+            break;
     }
     NextTx = true;
     UplinkStatusUpdated = true;
@@ -787,10 +831,10 @@
                 TimerInit( &TxNextPacketTimer, OnTxNextPacketTimerEvent );
 
                 TimerInit( &Led1Timer, OnLed1TimerEvent );
-                TimerSetValue( &Led1Timer, 25000 );
+                TimerSetValue( &Led1Timer, 25 );
 
                 TimerInit( &Led2Timer, OnLed2TimerEvent );
-                TimerSetValue( &Led2Timer, 25000 );
+                TimerSetValue( &Led2Timer, 25 );
 
                 mibReq.Type = MIB_ADR;
                 mibReq.Param.AdrEnable = LORAWAN_ADR_ON;
@@ -804,7 +848,7 @@
                 LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON );
                 SerialDisplayUpdateDutyCycle( LORAWAN_DUTYCYCLE_ON );
 
-#if( USE_SEMTECH_DEFAULT_CHANNEL_LINEUP == 1 ) 
+#if( USE_SEMTECH_DEFAULT_CHANNEL_LINEUP == 1 )
                 LoRaMacChannelAdd( 3, ( ChannelParams_t )LC4 );
                 LoRaMacChannelAdd( 4, ( ChannelParams_t )LC5 );
                 LoRaMacChannelAdd( 5, ( ChannelParams_t )LC6 );
@@ -813,6 +857,10 @@
                 LoRaMacChannelAdd( 8, ( ChannelParams_t )LC9 );
                 LoRaMacChannelAdd( 9, ( ChannelParams_t )LC10 );
 
+                mibReq.Type = MIB_RX2_DEFAULT_CHANNEL;
+                mibReq.Param.Rx2DefaultChannel = ( Rx2ChannelParams_t ){ 869525000, DR_3 };
+                LoRaMacMibSetRequestConfirm( &mibReq );
+
                 mibReq.Type = MIB_RX2_CHANNEL;
                 mibReq.Param.Rx2Channel = ( Rx2ChannelParams_t ){ 869525000, DR_3 };
                 LoRaMacMibSetRequestConfirm( &mibReq );
@@ -883,7 +931,7 @@
                 if( ComplianceTest.Running == true )
                 {
                     // Schedule next packet transmission
-                    TxDutyCycleTime = 5000000; // 5000000 us
+                    TxDutyCycleTime = 5000; // 5000 ms
                 }
                 else
                 {
--- a/board/board.cpp	Tue Apr 04 13:36:56 2017 +0000
+++ b/board/board.cpp	Mon Apr 24 13:29:34 2017 +0000
@@ -17,6 +17,28 @@
 
 SX1272MB2xAS Radio( NULL );
 
+/*!
+ * Nested interrupt counter.
+ *
+ * \remark Interrupt should only be fully disabled once the value is 0
+ */
+static uint8_t IrqNestLevel = 0;
+
+void BoardDisableIrq( void )
+{
+    __disable_irq( );
+    IrqNestLevel++;
+}
+
+void BoardEnableIrq( void )
+{
+    IrqNestLevel--;
+    if( IrqNestLevel == 0 )
+    {
+        __enable_irq( );
+    }
+}
+
 void BoardInit( void )
 {
     TimerTimeCounterInit( );
--- a/board/board.h	Tue Apr 04 13:36:56 2017 +0000
+++ b/board/board.h	Mon Apr 24 13:29:34 2017 +0000
@@ -26,6 +26,20 @@
 extern SX1272MB2xAS Radio;
 
 /*!
+ * \brief Disable interrupts
+ *
+ * \remark IRQ nesting is managed
+ */
+void BoardDisableIrq( void );
+
+/*!
+ * \brief Enable interrupts
+ *
+ * \remark IRQ nesting is managed
+ */
+void BoardEnableIrq( void );
+
+/*!
  * \brief Initializes the target board peripherals.
  */
 void BoardInit( void );
--- a/mac/LoRaWAN-lib.lib	Tue Apr 04 13:36:56 2017 +0000
+++ b/mac/LoRaWAN-lib.lib	Mon Apr 24 13:29:34 2017 +0000
@@ -1,1 +1,1 @@
-http://developer.mbed.org/teams/Semtech/code/LoRaWAN-lib/#c16969e0f70f
+http://developer.mbed.org/teams/Semtech/code/LoRaWAN-lib/#26002607de9c
--- a/mbed.bld	Tue Apr 04 13:36:56 2017 +0000
+++ b/mbed.bld	Mon Apr 24 13:29:34 2017 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/856d2700e60b
\ No newline at end of file
+https://mbed.org/users/mbed_official/code/mbed/builds/97feb9bacc10
\ No newline at end of file
--- a/radio/SX1272Lib.lib	Tue Apr 04 13:36:56 2017 +0000
+++ b/radio/SX1272Lib.lib	Mon Apr 24 13:29:34 2017 +0000
@@ -1,1 +1,1 @@
-http://developer.mbed.org/teams/Semtech/code/SX1272Lib/#69d5976b504d
+http://developer.mbed.org/teams/Semtech/code/SX1272Lib/#b988b60083a1
--- a/system/timer.cpp	Tue Apr 04 13:36:56 2017 +0000
+++ b/system/timer.cpp	Mon Apr 24 13:29:34 2017 +0000
@@ -17,11 +17,11 @@
 Timer TimeCounter;
 Ticker LoadTimeCounter;
 
-volatile uint64_t CurrentTime = 0;
+volatile uint32_t CurrentTime = 0;
 
 void TimerResetTimeCounter( void )
 {
-    CurrentTime = CurrentTime + TimeCounter.read_us( );
+    CurrentTime = CurrentTime + TimeCounter.read_us( ) / 1e3;
     TimeCounter.reset( );
     TimeCounter.start( );
 }
@@ -29,12 +29,12 @@
 void TimerTimeCounterInit( void )
 {
     TimeCounter.start( );
-    LoadTimeCounter.attach( &TimerResetTimeCounter, 10 );
+    LoadTimeCounter.attach( mbed::callback( &TimerResetTimeCounter ), 10 );
 }
 
 TimerTime_t TimerGetCurrentTime( void )
 {
-    CurrentTime += TimeCounter.read_us( );
+    CurrentTime += TimeCounter.read_us( ) / 1e3;
     TimeCounter.reset( );
     TimeCounter.start( );
     return ( ( TimerTime_t )CurrentTime );
@@ -42,7 +42,7 @@
 
 TimerTime_t TimerGetElapsedTime( TimerTime_t savedTime )
 {
-    CurrentTime += TimeCounter.read_us( );
+    CurrentTime += TimeCounter.read_us( ) / 1e3;
     TimeCounter.reset( );
     TimeCounter.start( );
     return ( TimerTime_t )( CurrentTime - savedTime );
@@ -50,7 +50,7 @@
 
 TimerTime_t TimerGetFutureTime( TimerTime_t eventInFuture )
 {
-    CurrentTime += TimeCounter.read_us( );
+    CurrentTime += TimeCounter.read_us( ) / 1e3;
     TimeCounter.reset( );
     TimeCounter.start( );
     return ( TimerTime_t )( CurrentTime + eventInFuture );
@@ -64,7 +64,7 @@
 
 void TimerStart( TimerEvent_t *obj )
 {
-    obj->Timer.attach_us( obj->Callback, obj->value );
+    obj->Timer.attach_us( mbed::callback( obj->Callback ), obj->value * 1e3 );
 }
 
 void TimerStop( TimerEvent_t *obj )
--- a/system/timer.h	Tue Apr 04 13:36:56 2017 +0000
+++ b/system/timer.h	Mon Apr 24 13:29:34 2017 +0000
@@ -22,7 +22,7 @@
  */
 typedef struct TimerEvent_s
 {
-    uint64_t value;
+    uint32_t value;
     void ( *Callback )( void );
     Ticker Timer;
 }TimerEvent_t;
@@ -31,7 +31,7 @@
  * \brief Timer time variable definition
  */
 #ifndef TimerTime_t
-typedef uint64_t TimerTime_t;
+typedef uint32_t TimerTime_t;
 #endif
 
 /*!