1

Dependencies:   X_NUCLEO_IKS01A1 LoRaWAN-lib SX1276Lib mbed

Committer:
emerette
Date:
Fri Nov 09 21:52:19 2018 +0000
Revision:
8:88e8a1c7b88a
Parent:
7:730764a067dc
1

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 7:730764a067dc 131 /*!
ubhat 7:730764a067dc 132 * Indicates if the device is ready for application.
ubhat 7:730764a067dc 133 */
ubhat 7:730764a067dc 134 bool IsDeviceReady = false;
ubhat 7:730764a067dc 135
ubhat 7:730764a067dc 136 /*!
ubhat 7:730764a067dc 137 * Timer to handle the application start delay
ubhat 7:730764a067dc 138 */
ubhat 7:730764a067dc 139 TimerEvent_t TxDelayTimer;
ubhat 7:730764a067dc 140
ubhat 0:42863a11464a 141
ubhat 0:42863a11464a 142 /**
ubhat 0:42863a11464a 143 * Main application entry point.
ubhat 0:42863a11464a 144 */
ubhat 0:42863a11464a 145 int main( void )
ubhat 0:42863a11464a 146 {
ubhat 0:42863a11464a 147
ubhat 0:42863a11464a 148 // Initialize board peripherals
ubhat 0:42863a11464a 149 BoardInit( );
ubhat 0:42863a11464a 150
ubhat 0:42863a11464a 151 // Initialize Device state
ubhat 0:42863a11464a 152 DeviceState = DEVICE_STATE_INIT;
ubhat 0:42863a11464a 153
ubhat 0:42863a11464a 154 while( 1 )
ubhat 1:80c1daf19aa4 155 {
ubhat 0:42863a11464a 156 if( IsNetworkJoinedStatusUpdate == true )
ubhat 0:42863a11464a 157 {
ubhat 0:42863a11464a 158 IsNetworkJoinedStatusUpdate = false;
ubhat 0:42863a11464a 159
ubhat 0:42863a11464a 160 DeviceJoinUpdate( );
ubhat 0:42863a11464a 161 }
ubhat 0:42863a11464a 162
ubhat 0:42863a11464a 163 if( IsTxUpdate == true )
ubhat 0:42863a11464a 164 {
ubhat 0:42863a11464a 165 // If downlink received then update Serial Terminal and execute Rx event
ubhat 0:42863a11464a 166 IsTxUpdate = false;
ubhat 0:42863a11464a 167
ubhat 0:42863a11464a 168 // Update serial terminal
ubhat 0:42863a11464a 169 SerialDisplayTxUpdate( );
ubhat 0:42863a11464a 170 }
ubhat 0:42863a11464a 171
ubhat 0:42863a11464a 172 if( IsTxIntUpdate == true )
ubhat 0:42863a11464a 173 {
ubhat 0:42863a11464a 174 IsTxIntUpdate = false;
ubhat 0:42863a11464a 175
ubhat 0:42863a11464a 176 // Initialize next Tx Interrupt
ubhat 0:42863a11464a 177 InitNextTxInterrupt( AppPort );
ubhat 0:42863a11464a 178 }
ubhat 0:42863a11464a 179
ubhat 6:711a4e008afa 180 if( IsDutyCycleUpdate == true )
ubhat 6:711a4e008afa 181 {
ubhat 6:711a4e008afa 182 IsDutyCycleUpdate = false;
ubhat 6:711a4e008afa 183
ubhat 6:711a4e008afa 184 // Initialize next Tx Interrupt
ubhat 6:711a4e008afa 185 InitDutyCycleTimer( );
ubhat 6:711a4e008afa 186 }
ubhat 6:711a4e008afa 187
ubhat 0:42863a11464a 188 if( IsRxUpdate == true )
ubhat 0:42863a11464a 189 {
ubhat 0:42863a11464a 190 // If downlink received then update Serial Terminal and execute Rx event
ubhat 0:42863a11464a 191 IsRxUpdate = false;
ubhat 0:42863a11464a 192 RxEvent( );
ubhat 0:42863a11464a 193 SerialDisplayRxUpdate( );
ubhat 0:42863a11464a 194 }
ubhat 0:42863a11464a 195
ubhat 0:42863a11464a 196 switch( DeviceState )
ubhat 0:42863a11464a 197 {
ubhat 0:42863a11464a 198 case DEVICE_STATE_INIT:
ubhat 0:42863a11464a 199 {
ubhat 0:42863a11464a 200 // Initialize MAC, MAC services, Primitives
ubhat 0:42863a11464a 201 DeviceInit( );
ubhat 0:42863a11464a 202
ubhat 7:730764a067dc 203 if( IsDeviceReady == false )
ubhat 7:730764a067dc 204 {
ubhat 7:730764a067dc 205 // Initialize delay until device ready
ubhat 7:730764a067dc 206 InitDelayTimer( );
ubhat 7:730764a067dc 207 }
ubhat 7:730764a067dc 208
ubhat 0:42863a11464a 209 // Change Device state
ubhat 0:42863a11464a 210 DeviceState = DEVICE_STATE_JOIN;
ubhat 0:42863a11464a 211 break;
ubhat 0:42863a11464a 212 }
ubhat 0:42863a11464a 213 case DEVICE_STATE_JOIN:
ubhat 7:730764a067dc 214 {
ubhat 0:42863a11464a 215 #if( OVER_THE_AIR_ACTIVATION != 0 ) // OTA
ubhat 0:42863a11464a 216
ubhat 0:42863a11464a 217 // Generate DevEUI if not defined by User
ubhat 0:42863a11464a 218 BoardGetDevEUI( DevEui );
ubhat 0:42863a11464a 219
ubhat 0:42863a11464a 220 // Join N/w server
ubhat 0:42863a11464a 221 DeviceJoin( );
ubhat 0:42863a11464a 222
ubhat 0:42863a11464a 223 // Show on serial terminal
ubhat 0:42863a11464a 224 SerialDisplayJoinUpdate( );
ubhat 0:42863a11464a 225
ubhat 0:42863a11464a 226 // Execute Join event
ubhat 0:42863a11464a 227 JoinEvent( );
ubhat 0:42863a11464a 228
ubhat 0:42863a11464a 229 DeviceState = DEVICE_STATE_SLEEP;
ubhat 0:42863a11464a 230
ubhat 0:42863a11464a 231 #else // ABP
ubhat 0:42863a11464a 232 DeviceJoin( );
ubhat 0:42863a11464a 233
ubhat 0:42863a11464a 234 DeviceState = DEVICE_STATE_SEND;
ubhat 0:42863a11464a 235 #endif
ubhat 0:42863a11464a 236 IsNetworkJoinedStatusUpdate = true;
ubhat 0:42863a11464a 237 break;
ubhat 0:42863a11464a 238 }
ubhat 0:42863a11464a 239 case DEVICE_STATE_SEND:
ubhat 0:42863a11464a 240 {
ubhat 0:42863a11464a 241 if( NextTx == true )
ubhat 0:42863a11464a 242 {
ubhat 0:42863a11464a 243 // Prepare payload frame based on application port
ubhat 0:42863a11464a 244 PrepareTxFrame( AppPort );
ubhat 0:42863a11464a 245
ubhat 0:42863a11464a 246 // Send payload over the air
ubhat 0:42863a11464a 247 NextTx = SendFrame( );
ubhat 0:42863a11464a 248
ubhat 0:42863a11464a 249 // Execute transmit event
ubhat 0:42863a11464a 250 TxEvent( );
ubhat 0:42863a11464a 251 }
ubhat 0:42863a11464a 252
ubhat 0:42863a11464a 253 if( NextTx == false )
ubhat 0:42863a11464a 254 {
ubhat 0:42863a11464a 255 IsTxUpdate = true;
ubhat 0:42863a11464a 256 }
ubhat 0:42863a11464a 257
ubhat 0:42863a11464a 258 DeviceState = DEVICE_STATE_SLEEP;
ubhat 0:42863a11464a 259 break;
ubhat 0:42863a11464a 260 }
ubhat 0:42863a11464a 261 case DEVICE_STATE_SLEEP:
ubhat 0:42863a11464a 262 {
ubhat 0:42863a11464a 263 // Wake up through events
ubhat 0:42863a11464a 264 break;
ubhat 0:42863a11464a 265 }
ubhat 0:42863a11464a 266 default:
ubhat 0:42863a11464a 267 {
ubhat 0:42863a11464a 268 DeviceState = DEVICE_STATE_INIT;
ubhat 0:42863a11464a 269 break;
ubhat 0:42863a11464a 270 }
ubhat 0:42863a11464a 271 }
ubhat 0:42863a11464a 272 }
ubhat 0:42863a11464a 273 }