1

Dependencies:   X_NUCLEO_IKS01A1 LoRaWAN-lib SX1276Lib mbed

Committer:
ubhat
Date:
Tue Nov 28 08:20:09 2017 +0000
Revision:
6:711a4e008afa
Parent:
1:80c1daf19aa4
Child:
7:730764a067dc
Create project for case study using SX1276 shield

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ubhat 0:42863a11464a 1 /*
ubhat 0:42863a11464a 2 / _____) _ | |
ubhat 0:42863a11464a 3 ( (____ _____ ____ _| |_ _____ ____| |__
ubhat 0:42863a11464a 4 \____ \| ___ | (_ _) ___ |/ ___) _ \
ubhat 0:42863a11464a 5 _____) ) ____| | | || |_| ____( (___| | | |
ubhat 0:42863a11464a 6 (______/|_____)_|_|_| \__)_____)\____)_| |_|
ubhat 0:42863a11464a 7 (C)2015 Semtech
ubhat 0:42863a11464a 8
ubhat 0:42863a11464a 9 Description: LoRaMac classA device implementation
ubhat 0:42863a11464a 10
ubhat 0:42863a11464a 11 License: Revised BSD License, see LICENSE.TXT file include in the project
ubhat 0:42863a11464a 12
ubhat 0:42863a11464a 13 Maintainer: Miguel Luis and Gregory Cristian
ubhat 0:42863a11464a 14 */
ubhat 0:42863a11464a 15 #include "mbed.h"
ubhat 0:42863a11464a 16 #include "board.h"
ubhat 0:42863a11464a 17 #include "radio.h"
ubhat 0:42863a11464a 18
ubhat 0:42863a11464a 19 #include "LoRaMac.h"
ubhat 0:42863a11464a 20 #include "Comissioning.h"
ubhat 0:42863a11464a 21 #include "SerialDisplay.h"
ubhat 0:42863a11464a 22 #include "ComplianceTest.h"
ubhat 0:42863a11464a 23 #include "LoRaDeviceStateProc.h"
ubhat 0:42863a11464a 24 #include "LoRaEventProc.h"
ubhat 0:42863a11464a 25 #include "LoRaApp.h"
ubhat 0:42863a11464a 26
ubhat 0:42863a11464a 27 /*!
ubhat 0:42863a11464a 28 * Defines a random delay for application data transmission duty cycle. 1s,
ubhat 0:42863a11464a 29 * value in [us].
ubhat 0:42863a11464a 30 */
ubhat 0:42863a11464a 31
ubhat 0:42863a11464a 32
ubhat 0:42863a11464a 33 #if( OVER_THE_AIR_ACTIVATION != 0 )
ubhat 0:42863a11464a 34
ubhat 0:42863a11464a 35 uint8_t DevEui[] = LORAWAN_DEVICE_EUI;
ubhat 0:42863a11464a 36 uint8_t AppEui[] = LORAWAN_APPLICATION_EUI;
ubhat 0:42863a11464a 37 uint8_t AppKey[] = LORAWAN_APPLICATION_KEY;
ubhat 0:42863a11464a 38
ubhat 0:42863a11464a 39 #else
ubhat 0:42863a11464a 40
ubhat 0:42863a11464a 41 uint8_t NwkSKey[] = LORAWAN_NWKSKEY;
ubhat 0:42863a11464a 42 uint8_t AppSKey[] = LORAWAN_APPSKEY;
ubhat 0:42863a11464a 43
ubhat 0:42863a11464a 44 /*!
ubhat 0:42863a11464a 45 * Device address
ubhat 0:42863a11464a 46 */
ubhat 0:42863a11464a 47 uint32_t DevAddr = LORAWAN_DEVICE_ADDRESS;
ubhat 0:42863a11464a 48
ubhat 0:42863a11464a 49 #endif
ubhat 0:42863a11464a 50
ubhat 0:42863a11464a 51 /*!
ubhat 0:42863a11464a 52 * Application port
ubhat 0:42863a11464a 53 */
ubhat 0:42863a11464a 54 uint8_t AppPort = LORAWAN_APP_PORT;
ubhat 0:42863a11464a 55
ubhat 0:42863a11464a 56 /*!
ubhat 0:42863a11464a 57 * User application data size
ubhat 0:42863a11464a 58 */
ubhat 0:42863a11464a 59 uint8_t AppDataSize = LORAWAN_APP_DATA_SIZE;
ubhat 0:42863a11464a 60
ubhat 0:42863a11464a 61 /*!
ubhat 0:42863a11464a 62 * User application data
ubhat 0:42863a11464a 63 */
ubhat 0:42863a11464a 64 uint8_t AppData[LORAWAN_APP_DATA_MAX_SIZE];
ubhat 0:42863a11464a 65
ubhat 0:42863a11464a 66 /*!
ubhat 0:42863a11464a 67 * Application to handle functions
ubhat 0:42863a11464a 68 */
ubhat 0:42863a11464a 69 Application LoRaApp( AppData );
ubhat 0:42863a11464a 70
ubhat 0:42863a11464a 71 /*!
ubhat 0:42863a11464a 72 * Indicates if the node is sending confirmed or unconfirmed messages
ubhat 0:42863a11464a 73 */
ubhat 0:42863a11464a 74 uint8_t IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON;
ubhat 0:42863a11464a 75
ubhat 0:42863a11464a 76 /*!
ubhat 0:42863a11464a 77 * Timer to handle the application data transmission duty cycle
ubhat 0:42863a11464a 78 */
ubhat 0:42863a11464a 79 TimerEvent_t TxNextPacketTimer;
ubhat 0:42863a11464a 80
ubhat 0:42863a11464a 81 /*!
ubhat 0:42863a11464a 82 * Indicates if a new transmit interrupt can be set
ubhat 0:42863a11464a 83 */
ubhat 0:42863a11464a 84 bool IsTxIntUpdate = false;
ubhat 0:42863a11464a 85
ubhat 0:42863a11464a 86 /*!
ubhat 6:711a4e008afa 87 * Indicates if a new transmit interrupt can be set
ubhat 6:711a4e008afa 88 */
ubhat 6:711a4e008afa 89 bool IsDutyCycleUpdate = false;
ubhat 6:711a4e008afa 90
ubhat 6:711a4e008afa 91 /*!
ubhat 6:711a4e008afa 92 * Timer to handle the duty cycle
ubhat 6:711a4e008afa 93 */
ubhat 6:711a4e008afa 94 TimerEvent_t TxDutyCycleTimer;
ubhat 6:711a4e008afa 95
ubhat 6:711a4e008afa 96 /*!
ubhat 6:711a4e008afa 97 * Indicates if a new transmit interrupt can be set
ubhat 6:711a4e008afa 98 */
ubhat 6:711a4e008afa 99 uint16_t DutyCycleCntr = 0;
ubhat 6:711a4e008afa 100
ubhat 6:711a4e008afa 101 /*!
ubhat 6:711a4e008afa 102 * Indicates if a new transmit interrupt can be set
ubhat 6:711a4e008afa 103 */
ubhat 6:711a4e008afa 104 uint16_t DutyCycleIndx = 0;
ubhat 6:711a4e008afa 105
ubhat 6:711a4e008afa 106 /*!
ubhat 0:42863a11464a 107 * Indicates if a new packet can be sent
ubhat 0:42863a11464a 108 */
ubhat 0:42863a11464a 109 bool NextTx = true;
ubhat 0:42863a11464a 110
ubhat 0:42863a11464a 111 /*!
ubhat 0:42863a11464a 112 * LoRaWAN compliance tests support data
ubhat 0:42863a11464a 113 */
ubhat 0:42863a11464a 114 ComplianceTest_s ComplianceTest;
ubhat 0:42863a11464a 115
ubhat 0:42863a11464a 116 /*!
ubhat 0:42863a11464a 117 * Indicates if the MAC layer network join status has changed.
ubhat 0:42863a11464a 118 */
ubhat 0:42863a11464a 119 bool IsNetworkJoinedStatusUpdate = false;
ubhat 0:42863a11464a 120
ubhat 0:42863a11464a 121 /*!
ubhat 0:42863a11464a 122 * Indicates if the message sent.
ubhat 0:42863a11464a 123 */
ubhat 0:42863a11464a 124 bool IsTxUpdate = false;
ubhat 0:42863a11464a 125
ubhat 0:42863a11464a 126 /*!
ubhat 0:42863a11464a 127 * Indicates if the message received in the RX window.
ubhat 0:42863a11464a 128 */
ubhat 0:42863a11464a 129 bool IsRxUpdate = false;
ubhat 0:42863a11464a 130
ubhat 0:42863a11464a 131
ubhat 0:42863a11464a 132 /**
ubhat 0:42863a11464a 133 * Main application entry point.
ubhat 0:42863a11464a 134 */
ubhat 0:42863a11464a 135 int main( void )
ubhat 0:42863a11464a 136 {
ubhat 0:42863a11464a 137
ubhat 0:42863a11464a 138 // Initialize board peripherals
ubhat 0:42863a11464a 139 BoardInit( );
ubhat 0:42863a11464a 140
ubhat 0:42863a11464a 141 // Initialize Device state
ubhat 0:42863a11464a 142 DeviceState = DEVICE_STATE_INIT;
ubhat 0:42863a11464a 143
ubhat 0:42863a11464a 144 while( 1 )
ubhat 1:80c1daf19aa4 145 {
ubhat 0:42863a11464a 146 if( IsNetworkJoinedStatusUpdate == true )
ubhat 0:42863a11464a 147 {
ubhat 0:42863a11464a 148 IsNetworkJoinedStatusUpdate = false;
ubhat 0:42863a11464a 149
ubhat 0:42863a11464a 150 DeviceJoinUpdate( );
ubhat 0:42863a11464a 151 }
ubhat 0:42863a11464a 152
ubhat 0:42863a11464a 153 if( IsTxUpdate == true )
ubhat 0:42863a11464a 154 {
ubhat 0:42863a11464a 155 // If downlink received then update Serial Terminal and execute Rx event
ubhat 0:42863a11464a 156 IsTxUpdate = false;
ubhat 0:42863a11464a 157
ubhat 0:42863a11464a 158 // Update serial terminal
ubhat 0:42863a11464a 159 SerialDisplayTxUpdate( );
ubhat 0:42863a11464a 160 }
ubhat 0:42863a11464a 161
ubhat 0:42863a11464a 162 if( IsTxIntUpdate == true )
ubhat 0:42863a11464a 163 {
ubhat 0:42863a11464a 164 IsTxIntUpdate = false;
ubhat 0:42863a11464a 165
ubhat 0:42863a11464a 166 // Initialize next Tx Interrupt
ubhat 0:42863a11464a 167 InitNextTxInterrupt( AppPort );
ubhat 0:42863a11464a 168 }
ubhat 0:42863a11464a 169
ubhat 6:711a4e008afa 170 if( IsDutyCycleUpdate == true )
ubhat 6:711a4e008afa 171 {
ubhat 6:711a4e008afa 172 IsDutyCycleUpdate = false;
ubhat 6:711a4e008afa 173
ubhat 6:711a4e008afa 174 // Initialize next Tx Interrupt
ubhat 6:711a4e008afa 175 InitDutyCycleTimer( );
ubhat 6:711a4e008afa 176 }
ubhat 6:711a4e008afa 177
ubhat 0:42863a11464a 178 if( IsRxUpdate == true )
ubhat 0:42863a11464a 179 {
ubhat 0:42863a11464a 180 // If downlink received then update Serial Terminal and execute Rx event
ubhat 0:42863a11464a 181 IsRxUpdate = false;
ubhat 0:42863a11464a 182 RxEvent( );
ubhat 0:42863a11464a 183 SerialDisplayRxUpdate( );
ubhat 0:42863a11464a 184 }
ubhat 0:42863a11464a 185
ubhat 0:42863a11464a 186 switch( DeviceState )
ubhat 0:42863a11464a 187 {
ubhat 0:42863a11464a 188 case DEVICE_STATE_INIT:
ubhat 0:42863a11464a 189 {
ubhat 0:42863a11464a 190 // Initialize MAC, MAC services, Primitives
ubhat 0:42863a11464a 191 DeviceInit( );
ubhat 0:42863a11464a 192
ubhat 0:42863a11464a 193 // Change Device state
ubhat 0:42863a11464a 194 DeviceState = DEVICE_STATE_JOIN;
ubhat 0:42863a11464a 195 break;
ubhat 0:42863a11464a 196 }
ubhat 0:42863a11464a 197 case DEVICE_STATE_JOIN:
ubhat 0:42863a11464a 198 {
ubhat 0:42863a11464a 199 #if( OVER_THE_AIR_ACTIVATION != 0 ) // OTA
ubhat 0:42863a11464a 200
ubhat 0:42863a11464a 201 // Generate DevEUI if not defined by User
ubhat 0:42863a11464a 202 BoardGetDevEUI( DevEui );
ubhat 0:42863a11464a 203
ubhat 0:42863a11464a 204 // Join N/w server
ubhat 0:42863a11464a 205 DeviceJoin( );
ubhat 0:42863a11464a 206
ubhat 0:42863a11464a 207 // Show on serial terminal
ubhat 0:42863a11464a 208 SerialDisplayJoinUpdate( );
ubhat 0:42863a11464a 209
ubhat 0:42863a11464a 210 // Execute Join event
ubhat 0:42863a11464a 211 JoinEvent( );
ubhat 0:42863a11464a 212
ubhat 0:42863a11464a 213 DeviceState = DEVICE_STATE_SLEEP;
ubhat 0:42863a11464a 214
ubhat 0:42863a11464a 215 #else // ABP
ubhat 0:42863a11464a 216 DeviceJoin( );
ubhat 0:42863a11464a 217
ubhat 0:42863a11464a 218 DeviceState = DEVICE_STATE_SEND;
ubhat 0:42863a11464a 219 #endif
ubhat 0:42863a11464a 220 IsNetworkJoinedStatusUpdate = true;
ubhat 0:42863a11464a 221 break;
ubhat 0:42863a11464a 222 }
ubhat 0:42863a11464a 223 case DEVICE_STATE_SEND:
ubhat 0:42863a11464a 224 {
ubhat 0:42863a11464a 225 if( NextTx == true )
ubhat 0:42863a11464a 226 {
ubhat 0:42863a11464a 227 // Prepare payload frame based on application port
ubhat 0:42863a11464a 228 PrepareTxFrame( AppPort );
ubhat 0:42863a11464a 229
ubhat 0:42863a11464a 230 // Send payload over the air
ubhat 0:42863a11464a 231 NextTx = SendFrame( );
ubhat 0:42863a11464a 232
ubhat 0:42863a11464a 233 // Execute transmit event
ubhat 0:42863a11464a 234 TxEvent( );
ubhat 0:42863a11464a 235 }
ubhat 0:42863a11464a 236
ubhat 0:42863a11464a 237 if( NextTx == false )
ubhat 0:42863a11464a 238 {
ubhat 0:42863a11464a 239 IsTxUpdate = true;
ubhat 0:42863a11464a 240 }
ubhat 0:42863a11464a 241
ubhat 0:42863a11464a 242 DeviceState = DEVICE_STATE_SLEEP;
ubhat 0:42863a11464a 243 break;
ubhat 0:42863a11464a 244 }
ubhat 0:42863a11464a 245 case DEVICE_STATE_SLEEP:
ubhat 0:42863a11464a 246 {
ubhat 0:42863a11464a 247 // Wake up through events
ubhat 0:42863a11464a 248 break;
ubhat 0:42863a11464a 249 }
ubhat 0:42863a11464a 250 default:
ubhat 0:42863a11464a 251 {
ubhat 0:42863a11464a 252 DeviceState = DEVICE_STATE_INIT;
ubhat 0:42863a11464a 253 break;
ubhat 0:42863a11464a 254 }
ubhat 0:42863a11464a 255 }
ubhat 0:42863a11464a 256 }
ubhat 0:42863a11464a 257 }