Nagaraj Krishnamurthy / LoRaWAN-NAMote72-Application-Demo_IoTium

Dependencies:   LoRaWAN-lib SX1272Lib lib_gps lib_mma8451q lib_mpl3115a2 mbed

Fork of LoRaWAN-NAMote72-Application-Demo_Multitech by Nagaraj Krishnamurthy

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 /**
00113  * Main application entry point.
00114  */
00115 int main( void )
00116 {
00117 
00118     // Initialize board peripherals
00119     BoardInit( );
00120 
00121     // Initialize Device state
00122     DeviceState = DEVICE_STATE_INIT;
00123     volatile int tmp1 = 9000;
00124     volatile int tmp2=0;
00125     while( 1 )
00126     {       
00127         Gps.service( );   
00128         //while(tmp1-- !=0)
00129         //{
00130         //    tmp2=tmp1/3;
00131         //}  
00132         //tmp1=9000;
00133         if( IsNetworkJoinedStatusUpdate == true )
00134         {
00135             IsNetworkJoinedStatusUpdate = false;
00136 
00137             DeviceJoinUpdate( );
00138         }
00139 
00140         if( IsTxUpdate == true )
00141         {
00142             // If downlink received then update Serial Terminal and execute Rx event
00143             IsTxUpdate = false;
00144             
00145             // Update serial terminal
00146             SerialDisplayTxUpdate( );           
00147         }
00148 
00149         if( IsTxIntUpdate == true )
00150         {
00151             IsTxIntUpdate = false;
00152             
00153             // Initialize next Tx Interrupt
00154             InitNextTxInterrupt( AppPort );
00155         }
00156 
00157         if( IsRxUpdate == true )
00158         {
00159             // If downlink received then update Serial Terminal and execute Rx event
00160             IsRxUpdate = false;
00161             RxEvent( );
00162             SerialDisplayRxUpdate( );            
00163         }
00164         
00165         switch( DeviceState )
00166         {
00167             case DEVICE_STATE_INIT:
00168             {
00169                 // Initialize MAC, MAC services, Primitives
00170                 DeviceInit( );
00171 
00172                 // Change Device state
00173                 DeviceState = DEVICE_STATE_JOIN;
00174                 break;
00175             }
00176             case DEVICE_STATE_JOIN:
00177             {
00178 #if( OVER_THE_AIR_ACTIVATION != 0 )                // OTA 
00179 
00180                 // Generate DevEUI if not defined by User
00181                 BoardGetDevEUI( DevEui );
00182 
00183                 // Join N/w server
00184                 DeviceJoin( );                
00185 
00186                 // Show on serial terminal
00187                 SerialDisplayJoinUpdate( );
00188 
00189                 // Execute Join event
00190                 JoinEvent( );
00191                 //DeviceState = DEVICE_STATE_SEND;
00192                 DeviceState = DEVICE_STATE_SLEEP;
00193 
00194 #else                                           // ABP
00195                 DeviceJoin( );
00196 
00197                 DeviceState = DEVICE_STATE_SEND;
00198 #endif
00199                 //IsNetworkJoinedStatusUpdate = true;
00200                 break;
00201             }
00202             case DEVICE_STATE_SEND:
00203             {               
00204                 if( NextTx == true )
00205                 {
00206                     // Prepare payload frame based on application port
00207                     PrepareTxFrame( AppPort );
00208 
00209                     // Send payload over the air
00210                     NextTx = SendFrame( );
00211 
00212                     // Execute transmit event
00213                     TxEvent( );                 
00214                 }
00215 
00216                 if( NextTx == false )
00217                 {
00218                     IsTxUpdate = true;
00219                 }
00220                 
00221                 DeviceState = DEVICE_STATE_SLEEP;
00222                 break;
00223             }            
00224             case DEVICE_STATE_SLEEP:
00225             {
00226                 // Wake up through events
00227                 break;
00228             }
00229             default:
00230             {
00231                 DeviceState = DEVICE_STATE_INIT;
00232                 break;
00233             }
00234         }
00235     }
00236 }