Fork from LoRa Alliance program. available here: https://developer.mbed.org/users/Alliance/code/LoRaWAN/

Dependencies:   LoRaMacLib SX1276Lib mbed Chainable_RGB_LED DigitDisplay

Fork of LoRaWAN by LoRa All

Committer:
GregCr
Date:
Mon Sep 21 07:55:43 2015 +0000
Revision:
0:fc538717c96e
Child:
1:1ef4f6cd800c
Demo code with sensor shield

Who changed what in which revision?

UserRevisionLine numberNew contents of line
GregCr 0:fc538717c96e 1 /*
GregCr 0:fc538717c96e 2 / _____) _ | |
GregCr 0:fc538717c96e 3 ( (____ _____ ____ _| |_ _____ ____| |__
GregCr 0:fc538717c96e 4 \____ \| ___ | (_ _) ___ |/ ___) _ \
GregCr 0:fc538717c96e 5 _____) ) ____| | | || |_| ____( (___| | | |
GregCr 0:fc538717c96e 6 (______/|_____)_|_|_| \__)_____)\____)_| |_|
GregCr 0:fc538717c96e 7 (C)2015 Semtech
GregCr 0:fc538717c96e 8
GregCr 0:fc538717c96e 9 Description: LoRaMac classA device implementation
GregCr 0:fc538717c96e 10
GregCr 0:fc538717c96e 11 License: Revised BSD License, see LICENSE.TXT file include in the project
GregCr 0:fc538717c96e 12
GregCr 0:fc538717c96e 13 Maintainer: Miguel Luis and Gregory Cristian
GregCr 0:fc538717c96e 14 */
GregCr 0:fc538717c96e 15 #include "mbed.h"
GregCr 0:fc538717c96e 16 #include "board.h"
GregCr 0:fc538717c96e 17 #include "LoRaMac.h"
GregCr 0:fc538717c96e 18 #include "utilities.h"
GregCr 0:fc538717c96e 19 #include "DigitDisplay.h"
GregCr 0:fc538717c96e 20 #include "ChainableLED.h"
GregCr 0:fc538717c96e 21
GregCr 0:fc538717c96e 22 /*!
GregCr 0:fc538717c96e 23 * When set to 1 the application uses the Over-the-Air activation procedure
GregCr 0:fc538717c96e 24 * When set to 0 the application uses the Personalization activation procedure
GregCr 0:fc538717c96e 25 */
GregCr 0:fc538717c96e 26 #define OVER_THE_AIR_ACTIVATION 0
GregCr 0:fc538717c96e 27
GregCr 0:fc538717c96e 28 /*!
GregCr 0:fc538717c96e 29 * Mote device IEEE EUI
GregCr 0:fc538717c96e 30 */
GregCr 0:fc538717c96e 31 static uint8_t DevEui[] =
GregCr 0:fc538717c96e 32 {
GregCr 0:fc538717c96e 33 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
GregCr 0:fc538717c96e 34 };
GregCr 0:fc538717c96e 35
GregCr 0:fc538717c96e 36 #if( OVER_THE_AIR_ACTIVATION != 0 )
GregCr 0:fc538717c96e 37
GregCr 0:fc538717c96e 38 #define OVER_THE_AIR_ACTIVATION_DUTYCYCLE 10000000 // 10 [s] value in us
GregCr 0:fc538717c96e 39
GregCr 0:fc538717c96e 40 /*!
GregCr 0:fc538717c96e 41 * Application IEEE EUI
GregCr 0:fc538717c96e 42 */
GregCr 0:fc538717c96e 43 static uint8_t AppEui[] =
GregCr 0:fc538717c96e 44 {
GregCr 0:fc538717c96e 45 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
GregCr 0:fc538717c96e 46 };
GregCr 0:fc538717c96e 47
GregCr 0:fc538717c96e 48 /*!
GregCr 0:fc538717c96e 49 * AES encryption/decryption cipher application key
GregCr 0:fc538717c96e 50 */
GregCr 0:fc538717c96e 51 static uint8_t AppKey[] =
GregCr 0:fc538717c96e 52 {
GregCr 0:fc538717c96e 53 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
GregCr 0:fc538717c96e 54 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
GregCr 0:fc538717c96e 55 };
GregCr 0:fc538717c96e 56
GregCr 0:fc538717c96e 57 #else
GregCr 0:fc538717c96e 58
GregCr 0:fc538717c96e 59 /*!
GregCr 0:fc538717c96e 60 * AES encryption/decryption cipher network session key
GregCr 0:fc538717c96e 61 */
GregCr 0:fc538717c96e 62 static uint8_t NwkSKey[] =
GregCr 0:fc538717c96e 63 {
GregCr 0:fc538717c96e 64 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
GregCr 0:fc538717c96e 65 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
GregCr 0:fc538717c96e 66 };
GregCr 0:fc538717c96e 67
GregCr 0:fc538717c96e 68 /*!
GregCr 0:fc538717c96e 69 * AES encryption/decryption cipher application session key
GregCr 0:fc538717c96e 70 */
GregCr 0:fc538717c96e 71 static uint8_t AppSKey[] =
GregCr 0:fc538717c96e 72 {
GregCr 0:fc538717c96e 73 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
GregCr 0:fc538717c96e 74 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
GregCr 0:fc538717c96e 75 };
GregCr 0:fc538717c96e 76
GregCr 0:fc538717c96e 77 /*!
GregCr 0:fc538717c96e 78 * Device address
GregCr 0:fc538717c96e 79 */
GregCr 0:fc538717c96e 80 static uint32_t DevAddr;
GregCr 0:fc538717c96e 81
GregCr 0:fc538717c96e 82 #endif
GregCr 0:fc538717c96e 83
GregCr 0:fc538717c96e 84 /*!
GregCr 0:fc538717c96e 85 * Indicates if the MAC layer has already joined a network.
GregCr 0:fc538717c96e 86 */
GregCr 0:fc538717c96e 87 static bool IsNetworkJoined = false;
GregCr 0:fc538717c96e 88
GregCr 0:fc538717c96e 89 /*!
GregCr 0:fc538717c96e 90 * Defines the application data transmission duty cycle
GregCr 0:fc538717c96e 91 */
GregCr 0:fc538717c96e 92 #define APP_TX_DUTYCYCLE 5000000 // 5 [s] value in us
GregCr 0:fc538717c96e 93 #define APP_TX_DUTYCYCLE_RND 1000000 // 1 [s] value in us
GregCr 0:fc538717c96e 94
GregCr 0:fc538717c96e 95 /*!
GregCr 0:fc538717c96e 96 * User application data buffer size
GregCr 0:fc538717c96e 97 */
GregCr 0:fc538717c96e 98 #define APP_DATA_SIZE 8
GregCr 0:fc538717c96e 99
GregCr 0:fc538717c96e 100 /*!
GregCr 0:fc538717c96e 101 * User application data
GregCr 0:fc538717c96e 102 */
GregCr 0:fc538717c96e 103 static uint8_t AppData[APP_DATA_SIZE];
GregCr 0:fc538717c96e 104
GregCr 0:fc538717c96e 105 /*!
GregCr 0:fc538717c96e 106 * Defines the application data transmission duty cycle
GregCr 0:fc538717c96e 107 */
GregCr 0:fc538717c96e 108 static uint32_t TxDutyCycleTime;
GregCr 0:fc538717c96e 109
GregCr 0:fc538717c96e 110 Ticker TxNextPacketTimer;
GregCr 0:fc538717c96e 111
GregCr 0:fc538717c96e 112 #if( OVER_THE_AIR_ACTIVATION != 0 )
GregCr 0:fc538717c96e 113
GregCr 0:fc538717c96e 114 /*!
GregCr 0:fc538717c96e 115 * Defines the join request timer
GregCr 0:fc538717c96e 116 */
GregCr 0:fc538717c96e 117 Ticker JoinReqTimer;
GregCr 0:fc538717c96e 118
GregCr 0:fc538717c96e 119 #endif
GregCr 0:fc538717c96e 120
GregCr 0:fc538717c96e 121 /*!
GregCr 0:fc538717c96e 122 * Indicates if a new packet can be sent
GregCr 0:fc538717c96e 123 */
GregCr 0:fc538717c96e 124 static bool TxNextPacket = true;
GregCr 0:fc538717c96e 125 static bool TxDone = false;
GregCr 0:fc538717c96e 126
GregCr 0:fc538717c96e 127 static uint8_t AppPort = 1;
GregCr 0:fc538717c96e 128 static uint8_t AppDataSize = APP_DATA_SIZE;
GregCr 0:fc538717c96e 129
GregCr 0:fc538717c96e 130 static bool AppLedStateOn = false;
GregCr 0:fc538717c96e 131
GregCr 0:fc538717c96e 132 static LoRaMacEvent_t LoRaMacEvents;
GregCr 0:fc538717c96e 133
GregCr 0:fc538717c96e 134 Ticker Led1Timer;
GregCr 0:fc538717c96e 135 Ticker Led2Timer;
GregCr 0:fc538717c96e 136
GregCr 0:fc538717c96e 137
GregCr 0:fc538717c96e 138 #define NUM_LED 3
GregCr 0:fc538717c96e 139
GregCr 0:fc538717c96e 140 // ChainableLED(clk, data, number_of_leds)
GregCr 0:fc538717c96e 141 ChainableLED color_led(D8, D9, NUM_LED);
GregCr 0:fc538717c96e 142
GregCr 0:fc538717c96e 143 DigitDisplay display(D6, D7);
GregCr 0:fc538717c96e 144
GregCr 0:fc538717c96e 145 DigitalOut buzzer(A2);
GregCr 0:fc538717c96e 146
GregCr 0:fc538717c96e 147
GregCr 0:fc538717c96e 148 /*!
GregCr 0:fc538717c96e 149 *
GregCr 0:fc538717c96e 150 */
GregCr 0:fc538717c96e 151 static void PrepareTxFrame( uint8_t port )
GregCr 0:fc538717c96e 152 {
GregCr 0:fc538717c96e 153 AppData[0] = 0x01;
GregCr 0:fc538717c96e 154 AppData[1] = 0x77;
GregCr 0:fc538717c96e 155 AppData[2] = 0x77;
GregCr 0:fc538717c96e 156 AppData[3] = 0x77;
GregCr 0:fc538717c96e 157 AppData[4] = 0x77;
GregCr 0:fc538717c96e 158 AppData[5] = 0x77;
GregCr 0:fc538717c96e 159 AppData[6] = 0x77;
GregCr 0:fc538717c96e 160 AppData[7] = 0x77;
GregCr 0:fc538717c96e 161 }
GregCr 0:fc538717c96e 162
GregCr 0:fc538717c96e 163 static void ProcessRxFrame( LoRaMacEventFlags_t *flags, LoRaMacEventInfo_t *info )
GregCr 0:fc538717c96e 164 {
GregCr 0:fc538717c96e 165 switch( info->RxPort ) // Check Rx port number
GregCr 0:fc538717c96e 166 {
GregCr 0:fc538717c96e 167 case 10:
GregCr 0:fc538717c96e 168 display.write( 0, info->RxBuffer[0] / 10 );
GregCr 0:fc538717c96e 169 display.write( 1, info->RxBuffer[1] % 10 );
GregCr 0:fc538717c96e 170 display.write( 2, info->RxBuffer[2] / 10 );
GregCr 0:fc538717c96e 171 display.write( 3, info->RxBuffer[3] % 10 );
GregCr 0:fc538717c96e 172 break;
GregCr 0:fc538717c96e 173
GregCr 0:fc538717c96e 174
GregCr 0:fc538717c96e 175 case 20:
GregCr 0:fc538717c96e 176 color_led.setColorRGB(0, info->RxBuffer[0], info->RxBuffer[1], 0x00 );
GregCr 0:fc538717c96e 177 break;
GregCr 0:fc538717c96e 178
GregCr 0:fc538717c96e 179 default:
GregCr 0:fc538717c96e 180 break;
GregCr 0:fc538717c96e 181 }
GregCr 0:fc538717c96e 182 }
GregCr 0:fc538717c96e 183
GregCr 0:fc538717c96e 184 static bool SendFrame( void )
GregCr 0:fc538717c96e 185 {
GregCr 0:fc538717c96e 186 uint8_t sendFrameStatus = 0;
GregCr 0:fc538717c96e 187
GregCr 0:fc538717c96e 188 sendFrameStatus = LoRaMacSendFrame( AppPort, AppData, AppDataSize );
GregCr 0:fc538717c96e 189 // sendFrameStatus = LoRaMacSendConfirmedFrame( AppPort, AppData, AppDataSize, 8 );
GregCr 0:fc538717c96e 190 switch( sendFrameStatus )
GregCr 0:fc538717c96e 191 {
GregCr 0:fc538717c96e 192 case 5: // NO_FREE_CHANNEL
GregCr 0:fc538717c96e 193 // Try again later
GregCr 0:fc538717c96e 194 return true;
GregCr 0:fc538717c96e 195 default:
GregCr 0:fc538717c96e 196 return false;
GregCr 0:fc538717c96e 197 }
GregCr 0:fc538717c96e 198 }
GregCr 0:fc538717c96e 199
GregCr 0:fc538717c96e 200
GregCr 0:fc538717c96e 201 #if( OVER_THE_AIR_ACTIVATION != 0 )
GregCr 0:fc538717c96e 202 /*!
GregCr 0:fc538717c96e 203 * \brief Function executed on JoinReq Timeout event
GregCr 0:fc538717c96e 204 */
GregCr 0:fc538717c96e 205 static void OnJoinReqTimerEvent( void )
GregCr 0:fc538717c96e 206 {
GregCr 0:fc538717c96e 207 TxNextPacket = true;
GregCr 0:fc538717c96e 208 }
GregCr 0:fc538717c96e 209 #endif
GregCr 0:fc538717c96e 210
GregCr 0:fc538717c96e 211
GregCr 0:fc538717c96e 212 /*!
GregCr 0:fc538717c96e 213 * \brief Function executed on TxNextPacket Timeout event
GregCr 0:fc538717c96e 214 */
GregCr 0:fc538717c96e 215 static void OnTxNextPacketTimerEvent( void )
GregCr 0:fc538717c96e 216 {
GregCr 0:fc538717c96e 217 debug( "OnTxNextPacketTimerEvent\n\n\r" );
GregCr 0:fc538717c96e 218
GregCr 0:fc538717c96e 219 TxNextPacket = true;
GregCr 0:fc538717c96e 220 }
GregCr 0:fc538717c96e 221
GregCr 0:fc538717c96e 222
GregCr 0:fc538717c96e 223 /*!
GregCr 0:fc538717c96e 224 * \brief Function to be executed on MAC layer event
GregCr 0:fc538717c96e 225 */
GregCr 0:fc538717c96e 226 static void OnMacEvent( LoRaMacEventFlags_t *flags, LoRaMacEventInfo_t *info )
GregCr 0:fc538717c96e 227 {
GregCr 0:fc538717c96e 228 if( flags->Bits.JoinAccept == 1 )
GregCr 0:fc538717c96e 229 {
GregCr 0:fc538717c96e 230 #if( OVER_THE_AIR_ACTIVATION != 0 )
GregCr 0:fc538717c96e 231 JoinReqTimer.detach( );
GregCr 0:fc538717c96e 232 #endif
GregCr 0:fc538717c96e 233 IsNetworkJoined = true;
GregCr 0:fc538717c96e 234 }
GregCr 0:fc538717c96e 235
GregCr 0:fc538717c96e 236 if( flags->Bits.Tx == 1 )
GregCr 0:fc538717c96e 237 {
GregCr 0:fc538717c96e 238 }
GregCr 0:fc538717c96e 239
GregCr 0:fc538717c96e 240 if( flags->Bits.Rx == 1 )
GregCr 0:fc538717c96e 241 {
GregCr 0:fc538717c96e 242 if( flags->Bits.RxData == true )
GregCr 0:fc538717c96e 243 {
GregCr 0:fc538717c96e 244 ProcessRxFrame( flags, info );
GregCr 0:fc538717c96e 245 }
GregCr 0:fc538717c96e 246 }
GregCr 0:fc538717c96e 247
GregCr 0:fc538717c96e 248 // Schedule a new transmission
GregCr 0:fc538717c96e 249 TxDone = true;
GregCr 0:fc538717c96e 250 }
GregCr 0:fc538717c96e 251
GregCr 0:fc538717c96e 252 /**
GregCr 0:fc538717c96e 253 * Main application entry point.
GregCr 0:fc538717c96e 254 */
GregCr 0:fc538717c96e 255 int main( void )
GregCr 0:fc538717c96e 256 {
GregCr 0:fc538717c96e 257 debug( "\n\n\r LoRaWAN Class A Demo code \n\n\r" );
GregCr 0:fc538717c96e 258
GregCr 0:fc538717c96e 259 #if( OVER_THE_AIR_ACTIVATION != 0 )
GregCr 0:fc538717c96e 260 uint8_t sendFrameStatus = 0;
GregCr 0:fc538717c96e 261 #endif
GregCr 0:fc538717c96e 262 bool trySendingFrameAgain = false;
GregCr 0:fc538717c96e 263
GregCr 0:fc538717c96e 264 BoardInitMcu( );
GregCr 0:fc538717c96e 265 BoardInitPeriph( );
GregCr 0:fc538717c96e 266
GregCr 0:fc538717c96e 267 // Initialize LoRaMac device unique ID
GregCr 0:fc538717c96e 268 BoardGetUniqueId( DevEui );
GregCr 0:fc538717c96e 269
GregCr 0:fc538717c96e 270 LoRaMacEvents.MacEvent = OnMacEvent;
GregCr 0:fc538717c96e 271 LoRaMacInit( &LoRaMacEvents );
GregCr 0:fc538717c96e 272
GregCr 0:fc538717c96e 273 IsNetworkJoined = false;
GregCr 0:fc538717c96e 274
GregCr 0:fc538717c96e 275 #if( OVER_THE_AIR_ACTIVATION == 0 )
GregCr 0:fc538717c96e 276 // Random seed initialization
GregCr 0:fc538717c96e 277 srand( RAND_SEED );
GregCr 0:fc538717c96e 278 // Choose a random device address
GregCr 0:fc538717c96e 279 // NwkID = 0
GregCr 0:fc538717c96e 280 // NwkAddr rand [0, 33554431]
GregCr 0:fc538717c96e 281 DevAddr = randr( 0, 0x01FFFFFF );
GregCr 0:fc538717c96e 282
GregCr 0:fc538717c96e 283 LoRaMacInitNwkIds( 0x000000, DevAddr, NwkSKey, AppSKey );
GregCr 0:fc538717c96e 284 IsNetworkJoined = true;
GregCr 0:fc538717c96e 285 #endif
GregCr 0:fc538717c96e 286
GregCr 0:fc538717c96e 287 TxNextPacket = true;
GregCr 0:fc538717c96e 288
GregCr 0:fc538717c96e 289 LoRaMacSetAdrOn( true );
GregCr 0:fc538717c96e 290
GregCr 0:fc538717c96e 291 while( 1 )
GregCr 0:fc538717c96e 292 {
GregCr 0:fc538717c96e 293 while( IsNetworkJoined == false )
GregCr 0:fc538717c96e 294 {
GregCr 0:fc538717c96e 295 #if( OVER_THE_AIR_ACTIVATION != 0 )
GregCr 0:fc538717c96e 296 if( TxNextPacket == true )
GregCr 0:fc538717c96e 297 {
GregCr 0:fc538717c96e 298 TxNextPacket = false;
GregCr 0:fc538717c96e 299
GregCr 0:fc538717c96e 300 sendFrameStatus = LoRaMacJoinReq( DevEui, AppEui, AppKey );
GregCr 0:fc538717c96e 301 switch( sendFrameStatus )
GregCr 0:fc538717c96e 302 {
GregCr 0:fc538717c96e 303 case 1: // BUSY
GregCr 0:fc538717c96e 304 break;
GregCr 0:fc538717c96e 305 case 0: // OK
GregCr 0:fc538717c96e 306 case 2: // NO_NETWORK_JOINED
GregCr 0:fc538717c96e 307 case 3: // LENGTH_PORT_ERROR
GregCr 0:fc538717c96e 308 case 4: // MAC_CMD_ERROR
GregCr 0:fc538717c96e 309 case 6: // DEVICE_OFF
GregCr 0:fc538717c96e 310 default:
GregCr 0:fc538717c96e 311 // Relaunch timer for next trial
GregCr 0:fc538717c96e 312 JoinReqTimer.attach_us( &OnJoinReqTimerEvent, OVER_THE_AIR_ACTIVATION_DUTYCYCLE );
GregCr 0:fc538717c96e 313 break;
GregCr 0:fc538717c96e 314 }
GregCr 0:fc538717c96e 315 }
GregCr 0:fc538717c96e 316 // TimerLowPowerHandler( );
GregCr 0:fc538717c96e 317 #endif
GregCr 0:fc538717c96e 318 }
GregCr 0:fc538717c96e 319
GregCr 0:fc538717c96e 320 if( TxDone == true )
GregCr 0:fc538717c96e 321 {
GregCr 0:fc538717c96e 322
GregCr 0:fc538717c96e 323 TxDone = false;
GregCr 0:fc538717c96e 324
GregCr 0:fc538717c96e 325 debug( "TxDone \n\n\r" );
GregCr 0:fc538717c96e 326 color_led.setColorRGB(0, randr( 0, 255 ), randr( 0, 255 ), randr( 0, 255 ) );
GregCr 0:fc538717c96e 327 // Schedule next packet transmission
GregCr 0:fc538717c96e 328 TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND );
GregCr 0:fc538717c96e 329 TxNextPacketTimer.attach_us( &OnTxNextPacketTimerEvent, TxDutyCycleTime );
GregCr 0:fc538717c96e 330 }
GregCr 0:fc538717c96e 331
GregCr 0:fc538717c96e 332 if( trySendingFrameAgain == true )
GregCr 0:fc538717c96e 333 {
GregCr 0:fc538717c96e 334 trySendingFrameAgain = SendFrame( );
GregCr 0:fc538717c96e 335 }
GregCr 0:fc538717c96e 336
GregCr 0:fc538717c96e 337 if( TxNextPacket == true )
GregCr 0:fc538717c96e 338 {
GregCr 0:fc538717c96e 339 TxNextPacketTimer.detach( );
GregCr 0:fc538717c96e 340
GregCr 0:fc538717c96e 341 TxNextPacket = false;
GregCr 0:fc538717c96e 342
GregCr 0:fc538717c96e 343 PrepareTxFrame( AppPort );
GregCr 0:fc538717c96e 344
GregCr 0:fc538717c96e 345 trySendingFrameAgain = SendFrame( );
GregCr 0:fc538717c96e 346 }
GregCr 0:fc538717c96e 347
GregCr 0:fc538717c96e 348 // TimerLowPowerHandler( );
GregCr 0:fc538717c96e 349 }
GregCr 0:fc538717c96e 350 }
GregCr 0:fc538717c96e 351