Long Range / Mbed 2 deprecated SX1272_LoRaWAN_App_LR

Dependencies:   X_NUCLEO_IKS01A2 driver_mbed_TH02 mbed LoRaWAN-lib-v1_0_1 SX1272Lib

Fork of Training-Aug2018-SX1272-X-NUCLEO-IKS01A2 by Uttam Bhat

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  / _____)             _              | |
00003 ( (____  _____ ____ _| |_ _____  ____| |__
00004  \____ \| ___ |    (_   _) ___ |/ ___)  _ \
00005  _____) ) ____| | | || |_| ____( (___| | | |
00006 (______/|_____)_|_|_| \__)_____)\____)_| |_|
00007     (C)2015 Semtech
00008 
00009 Description: LoRaMac classA device implementation
00010 
00011 License: Revised BSD License, see LICENSE.TXT file include in the project
00012 
00013 Maintainer: Miguel Luis and Gregory Cristian
00014 */
00015 #include "mbed.h"
00016 #include "board.h"
00017 #include "radio.h"
00018 
00019 #include "LoRaMac.h"
00020 #include "Comissioning.h"
00021 #include "SerialDisplay.h"
00022 #include "ComplianceTest.h"
00023 #include "LoRaDeviceStateProc.h"
00024 #include "LoRaEventProc.h"
00025 #include "LoRaApp.h"
00026 
00027 /*!
00028  * Defines a random delay for application data transmission duty cycle. 1s,
00029  * value in [us].
00030  */
00031 
00032 
00033 #if( OVER_THE_AIR_ACTIVATION != 0 )
00034 
00035 uint8_t DevEui[] = LORAWAN_DEVICE_EUI;
00036 uint8_t AppEui[] = LORAWAN_APPLICATION_EUI;
00037 uint8_t AppKey[] = LORAWAN_APPLICATION_KEY;
00038 
00039 #else
00040 
00041 uint8_t NwkSKey[] = LORAWAN_NWKSKEY;
00042 uint8_t AppSKey[] = LORAWAN_APPSKEY;
00043 
00044 /*!
00045  * Device address
00046  */
00047 uint32_t DevAddr = LORAWAN_DEVICE_ADDRESS;
00048 
00049 #endif
00050 
00051 /*!
00052  * Application port
00053  */
00054 uint8_t AppPort = LORAWAN_APP_PORT;
00055 
00056 /*!
00057  * User application data size
00058  */
00059 uint8_t AppDataSize = LORAWAN_APP_DATA_SIZE;
00060 
00061 /*!
00062  * User application data
00063  */
00064 uint8_t AppData[LORAWAN_APP_DATA_MAX_SIZE];
00065 
00066 /*!
00067  * Application to handle functions
00068  */
00069 Application  LoRaApp( AppData );
00070 
00071 /*!
00072  * Indicates if the node is sending confirmed or unconfirmed messages
00073  */
00074 uint8_t IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON;
00075 
00076 /*!
00077  * Timer to handle the application data transmission duty cycle
00078  */
00079 TimerEvent_t TxNextPacketTimer;
00080 
00081 /*!
00082  * Indicates if a new transmit interrupt can be set
00083  */
00084 bool IsTxIntUpdate = false;
00085 
00086 /*!
00087  * Indicates if a new packet can be sent
00088  */
00089 bool NextTx = true;
00090 
00091 /*!
00092  * LoRaWAN compliance tests support data
00093  */
00094 ComplianceTest_s ComplianceTest;
00095 
00096 /*!
00097  * Indicates if the MAC layer network join status has changed.
00098  */
00099 bool IsNetworkJoinedStatusUpdate = false;
00100 
00101 /*!
00102  * Indicates if the message sent.
00103  */
00104 bool IsTxUpdate = false;
00105 
00106 /*!
00107  * Indicates if the message received in the RX window.
00108  */
00109 bool IsRxUpdate = false;
00110 
00111 /**
00112  * Main application entry point.
00113  */
00114 int main( void )
00115 {
00116     // Initialize board peripherals
00117     BoardInit( );
00118 
00119     // Initialize Device state
00120     DeviceState = DEVICE_STATE_INIT;
00121     
00122     while( 1 )
00123     {               
00124         if( IsNetworkJoinedStatusUpdate == true )
00125         {
00126             IsNetworkJoinedStatusUpdate = false;
00127 
00128             DeviceJoinUpdate( );
00129         }
00130 
00131         if( IsTxUpdate == true )
00132         {
00133             // If downlink received then update Serial Terminal and execute Rx event
00134             IsTxUpdate = false;
00135             
00136             // Update serial terminal
00137             SerialDisplayTxUpdate( );           
00138         }
00139 
00140         if( IsTxIntUpdate == true )
00141         {
00142             IsTxIntUpdate = false;
00143             
00144             // Initialize next Tx Interrupt
00145             InitNextTxInterrupt( AppPort );
00146         }
00147 
00148         if( IsRxUpdate == true )
00149         {
00150             // If downlink received then update Serial Terminal and execute Rx event
00151             IsRxUpdate = false;
00152             RxEvent( );
00153             SerialDisplayRxUpdate( );            
00154         }
00155         
00156         switch( DeviceState )
00157         {
00158             case DEVICE_STATE_INIT:
00159             {
00160                 // Initialize MAC, MAC services, Primitives
00161                 DeviceInit( );
00162 
00163                 // Change Device state
00164                 DeviceState = DEVICE_STATE_JOIN;
00165                 break;
00166             }
00167             case DEVICE_STATE_JOIN:
00168             {
00169 #if( OVER_THE_AIR_ACTIVATION != 0 )                // OTA 
00170 
00171                 // Generate DevEUI if not defined by User
00172                 BoardGetDevEUI( DevEui );
00173                 
00174                 Led = 1;
00175 
00176                 // Join N/w server
00177                 DeviceJoin( );                
00178 
00179                 // Show on serial terminal
00180                 SerialDisplayJoinUpdate( );
00181 
00182                 // Execute Join event
00183                 JoinEvent( );
00184 
00185                 DeviceState = DEVICE_STATE_SLEEP;
00186 
00187 #else                                           // ABP
00188                 DeviceJoin( );
00189 
00190                 DeviceState = DEVICE_STATE_SEND;
00191 #endif
00192                 IsNetworkJoinedStatusUpdate = true;
00193                 break;
00194             }
00195             case DEVICE_STATE_SEND:
00196             {   
00197                 Led = 0;
00198                             
00199                 if( NextTx == true )
00200                 {
00201                     // Prepare payload frame based on application port
00202                     PrepareTxFrame( AppPort );
00203 
00204                     // Send payload over the air
00205                     NextTx = SendFrame( );
00206 
00207                     // Execute transmit event
00208                     TxEvent( );                 
00209                 }
00210 
00211                 if( NextTx == false )
00212                 {
00213                     IsTxUpdate = true;
00214                 }
00215                 
00216                 DeviceState = DEVICE_STATE_SLEEP;
00217                 break;
00218             }            
00219             case DEVICE_STATE_SLEEP:
00220             {
00221                 // Wake up through events
00222                 break;
00223             }
00224             default:
00225             {
00226                 DeviceState = DEVICE_STATE_INIT;
00227                 break;
00228             }
00229         }
00230     }
00231 }