Semtech / LoRaWAN-NAMote72-Application-Demo

Dependencies:   LoRaWAN-lib mbed lib_mpl3115a2 lib_mma8451q lib_gps SX1272Lib

Dependents:   LoRaWAN-NAMote72-BVS-confirmed-tester-0-7v1_copy

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 "Commissioning.h"
00021 #include "SerialDisplay.h"
00022 #include "ComplianceTest.h"
00023 #include "LoRaDeviceStateProc.h"
00024 #include "LoRaEventProc.h"
00025 #include "LoRaApp.h"
00026 
00027 bool Otaa = true;
00028 
00029 uint8_t DevEui[] = LORAWAN_DEVICE_EUI;
00030 uint8_t AppEui[] = LORAWAN_APPLICATION_EUI;
00031 uint8_t AppKey[] = LORAWAN_APPLICATION_KEY;
00032 
00033 #if( OVER_THE_AIR_ACTIVATION == 0 )
00034 
00035 uint8_t NwkSKey[] = LORAWAN_NWKSKEY;
00036 uint8_t AppSKey[] = LORAWAN_APPSKEY;
00037 
00038 /*!
00039  * Device address
00040  */
00041 uint32_t DevAddr = LORAWAN_DEVICE_ADDRESS;
00042 
00043 #endif
00044 
00045 /*!
00046  * Application port
00047  */
00048 uint8_t AppPort = LORAWAN_APP_PORT;
00049 
00050 /*!
00051  * User application data size
00052  */
00053 uint8_t AppDataSize = LORAWAN_APP_DATA_SIZE;
00054 
00055 /*!
00056  * User application data
00057  */
00058 uint8_t AppData[LORAWAN_APP_DATA_MAX_SIZE];
00059 
00060 /*!
00061  * Application to handle functions
00062  */
00063 Application  LoRaApp( AppData );
00064 
00065 /*!
00066  * Indicates if the node is sending confirmed or unconfirmed messages
00067  */
00068 uint8_t IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON;
00069 
00070 /*!
00071  * Timer to handle the application data transmission duty cycle
00072  */
00073 TimerEvent_t TxNextPacketTimer;
00074 
00075 /*!
00076  * Indicates if a new transmit interrupt can be set
00077  */
00078 bool IsTxIntUpdate = false;
00079 
00080 /*!
00081  * Indicates if a new packet can be sent
00082  */
00083 bool NextTx = true;
00084 
00085 /*!
00086  * LoRaWAN compliance tests support data
00087  */
00088 ComplianceTest_s ComplianceTest;
00089 
00090 /*!
00091  * Indicates if the MAC layer network join status has changed.
00092  */
00093 bool IsNetworkJoinedStatusUpdate = false;
00094 
00095 /*!
00096  * Indicates if the message sent.
00097  */
00098 bool IsTxUpdate = false;
00099 
00100 /*!
00101  * Indicates if the message received in the RX window.
00102  */
00103 bool IsRxUpdate = false;
00104 
00105 
00106 /**
00107  * Main application entry point.
00108  */
00109 int main( void )
00110 {
00111 
00112     // Initialize board peripherals
00113     BoardInit( );
00114 
00115     // Display Sw Version
00116     DisplaySwVersion( );
00117 
00118     // Initialize Device state
00119     DeviceState = DEVICE_STATE_INIT;
00120 
00121     while( 1 )
00122     {
00123         Gps.service( );
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                 // Join N/w server
00175                 DeviceJoin( );
00176 
00177                 // Show on serial terminal
00178                 SerialDisplayJoinUpdate( );
00179 
00180                 // Execute Join event
00181                 JoinEvent( );
00182 
00183                 DeviceState = DEVICE_STATE_SLEEP;
00184 
00185 #else                                           // ABP
00186                 DeviceJoin( );
00187 
00188                 DeviceState = DEVICE_STATE_SEND;
00189 #endif
00190                 IsNetworkJoinedStatusUpdate = true;
00191                 break;
00192             }
00193             case DEVICE_STATE_SEND:
00194             {
00195                 if( NextTx == true )
00196                 {
00197                     // Prepare payload frame based on application port
00198                     PrepareTxFrame( AppPort );
00199 
00200                     // Send payload over the air
00201                     NextTx = SendFrame( );
00202 
00203                     // Execute transmit event
00204                     TxEvent( );
00205                 }
00206 
00207                 if( NextTx == false )
00208                 {
00209                     IsTxUpdate = true;
00210                 }
00211 
00212                 DeviceState = DEVICE_STATE_SLEEP;
00213                 break;
00214             }
00215             case DEVICE_STATE_SLEEP:
00216             {
00217                 // Wake up through events
00218                 break;
00219             }
00220             default:
00221             {
00222                 DeviceState = DEVICE_STATE_INIT;
00223                 break;
00224             }
00225         }
00226     }
00227 }