Hex-current-measurements

Dependencies:   mbed LoRaWAN-lib SX1276Lib

Committer:
mluis
Date:
Tue Oct 20 13:23:35 2015 +0000
Revision:
0:92bca02df485
Child:
1:352f608c3337
LoRaWAN-demo project creation using LoRaWAN-lib library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mluis 0:92bca02df485 1 /*
mluis 0:92bca02df485 2 / _____) _ | |
mluis 0:92bca02df485 3 ( (____ _____ ____ _| |_ _____ ____| |__
mluis 0:92bca02df485 4 \____ \| ___ | (_ _) ___ |/ ___) _ \
mluis 0:92bca02df485 5 _____) ) ____| | | || |_| ____( (___| | | |
mluis 0:92bca02df485 6 (______/|_____)_|_|_| \__)_____)\____)_| |_|
mluis 0:92bca02df485 7 (C)2015 Semtech
mluis 0:92bca02df485 8
mluis 0:92bca02df485 9 Description: LoRaMac classA device implementation
mluis 0:92bca02df485 10
mluis 0:92bca02df485 11 License: Revised BSD License, see LICENSE.TXT file include in the project
mluis 0:92bca02df485 12
mluis 0:92bca02df485 13 Maintainer: Miguel Luis and Gregory Cristian
mluis 0:92bca02df485 14 */
mluis 0:92bca02df485 15 #include "mbed.h"
mluis 0:92bca02df485 16 #include "board.h"
mluis 0:92bca02df485 17 #include "radio.h"
mluis 0:92bca02df485 18
mluis 0:92bca02df485 19 #include "LoRaMac.h"
mluis 0:92bca02df485 20
mluis 0:92bca02df485 21 #include "SerialDisplay.h"
mluis 0:92bca02df485 22
mluis 0:92bca02df485 23 /*!
mluis 0:92bca02df485 24 * When set to 1 the application uses the Over-the-Air activation procedure
mluis 0:92bca02df485 25 * When set to 0 the application uses the Personalization activation procedure
mluis 0:92bca02df485 26 */
mluis 0:92bca02df485 27 #define OVER_THE_AIR_ACTIVATION 1
mluis 0:92bca02df485 28
mluis 0:92bca02df485 29 /*!
mluis 0:92bca02df485 30 * Indicates if the end-device is to be connected to a private or public network
mluis 0:92bca02df485 31 */
mluis 0:92bca02df485 32 #define LORAWAN_PUBLIC_NETWORK true
mluis 0:92bca02df485 33
mluis 0:92bca02df485 34 #if( OVER_THE_AIR_ACTIVATION != 0 )
mluis 0:92bca02df485 35
mluis 0:92bca02df485 36 /*!
mluis 0:92bca02df485 37 * Join requests trials duty cycle.
mluis 0:92bca02df485 38 */
mluis 0:92bca02df485 39 #define OVER_THE_AIR_ACTIVATION_DUTYCYCLE 10000000 // 10 [s] value in us
mluis 0:92bca02df485 40
mluis 0:92bca02df485 41 /*!
mluis 0:92bca02df485 42 * Mote device IEEE EUI
mluis 0:92bca02df485 43 *
mluis 0:92bca02df485 44 * \remark must be written as a little endian value (reverse order of normal reading)
mluis 0:92bca02df485 45 */
mluis 0:92bca02df485 46 #define LORAWAN_DEVICE_EUI { 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 }
mluis 0:92bca02df485 47
mluis 0:92bca02df485 48 /*!
mluis 0:92bca02df485 49 * Application IEEE EUI
mluis 0:92bca02df485 50 *
mluis 0:92bca02df485 51 * \remark must be written as a little endian value (reverse order of normal reading)
mluis 0:92bca02df485 52 */
mluis 0:92bca02df485 53 #define LORAWAN_APPLICATION_EUI { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }
mluis 0:92bca02df485 54
mluis 0:92bca02df485 55 /*!
mluis 0:92bca02df485 56 * AES encryption/decryption cipher application key
mluis 0:92bca02df485 57 */
mluis 0:92bca02df485 58 #define LORAWAN_APPLICATION_KEY { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00 }
mluis 0:92bca02df485 59
mluis 0:92bca02df485 60 #else
mluis 0:92bca02df485 61
mluis 0:92bca02df485 62 /*!
mluis 0:92bca02df485 63 * Current network ID
mluis 0:92bca02df485 64 */
mluis 0:92bca02df485 65 #define LORAWAN_NETWORK_ID ( uint32_t )0
mluis 0:92bca02df485 66
mluis 0:92bca02df485 67 /*!
mluis 0:92bca02df485 68 * Device address on the network
mluis 0:92bca02df485 69 *
mluis 0:92bca02df485 70 * \remark must be written as a big endian value (normal reading order)
mluis 0:92bca02df485 71 */
mluis 0:92bca02df485 72 #define LORAWAN_DEVICE_ADDRESS ( uint32_t )0x12345678
mluis 0:92bca02df485 73
mluis 0:92bca02df485 74 /*!
mluis 0:92bca02df485 75 * AES encryption/decryption cipher network session key
mluis 0:92bca02df485 76 */
mluis 0:92bca02df485 77 #define LORAWAN_NWKSKEY { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C }
mluis 0:92bca02df485 78
mluis 0:92bca02df485 79 /*!
mluis 0:92bca02df485 80 * AES encryption/decryption cipher application session key
mluis 0:92bca02df485 81 */
mluis 0:92bca02df485 82 #define LORAWAN_APPSKEY { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C }
mluis 0:92bca02df485 83
mluis 0:92bca02df485 84 #endif
mluis 0:92bca02df485 85
mluis 0:92bca02df485 86 /*!
mluis 0:92bca02df485 87 * Defines the application data transmission duty cycle
mluis 0:92bca02df485 88 */
mluis 0:92bca02df485 89 #define APP_TX_DUTYCYCLE 5000000 // 5 [s] value in us
mluis 0:92bca02df485 90 #define APP_TX_DUTYCYCLE_RND 1000000 // 1 [s] value in us
mluis 0:92bca02df485 91
mluis 0:92bca02df485 92 /*!
mluis 0:92bca02df485 93 * LoRaWAN confirmed messages
mluis 0:92bca02df485 94 */
mluis 0:92bca02df485 95 #define LORAWAN_CONFIRMED_MSG_ON true
mluis 0:92bca02df485 96
mluis 0:92bca02df485 97 /*!
mluis 0:92bca02df485 98 * LoRaWAN Adaptative Data Rate
mluis 0:92bca02df485 99 *
mluis 0:92bca02df485 100 * \remark Please note that when ADR is enabled the end-device should be static
mluis 0:92bca02df485 101 */
mluis 0:92bca02df485 102 #define LORAWAN_ADR_ON 1
mluis 0:92bca02df485 103
mluis 0:92bca02df485 104 /*!
mluis 0:92bca02df485 105 * LoRaWAN ETSI duty cycle control enable/disable
mluis 0:92bca02df485 106 *
mluis 0:92bca02df485 107 * \remark Please note that ETSI mandates duty cycled transmissions. Use only for test purposes
mluis 0:92bca02df485 108 */
mluis 0:92bca02df485 109 #define LORAWAN_DUTYCYCLE_ON false
mluis 0:92bca02df485 110
mluis 0:92bca02df485 111 /*!
mluis 0:92bca02df485 112 * LoRaWAN application port
mluis 0:92bca02df485 113 */
mluis 0:92bca02df485 114 #define LORAWAN_APP_PORT 15
mluis 0:92bca02df485 115
mluis 0:92bca02df485 116 /*!
mluis 0:92bca02df485 117 * User application data buffer size
mluis 0:92bca02df485 118 */
mluis 0:92bca02df485 119 #if ( LORAWAN_CONFIRMED_MSG_ON == 1 )
mluis 0:92bca02df485 120 #define LORAWAN_APP_DATA_SIZE 6
mluis 0:92bca02df485 121
mluis 0:92bca02df485 122 #else
mluis 0:92bca02df485 123 #define LORAWAN_APP_DATA_SIZE 1
mluis 0:92bca02df485 124
mluis 0:92bca02df485 125 #endif
mluis 0:92bca02df485 126
mluis 0:92bca02df485 127 #if( OVER_THE_AIR_ACTIVATION != 0 )
mluis 0:92bca02df485 128
mluis 0:92bca02df485 129 static uint8_t DevEui[] = LORAWAN_DEVICE_EUI;
mluis 0:92bca02df485 130 static uint8_t AppEui[] = LORAWAN_APPLICATION_EUI;
mluis 0:92bca02df485 131 static uint8_t AppKey[] = LORAWAN_APPLICATION_KEY;
mluis 0:92bca02df485 132
mluis 0:92bca02df485 133 #else
mluis 0:92bca02df485 134
mluis 0:92bca02df485 135 static uint8_t NwkSKey[] = LORAWAN_NWKSKEY;
mluis 0:92bca02df485 136 static uint8_t AppSKey[] = LORAWAN_APPSKEY;
mluis 0:92bca02df485 137
mluis 0:92bca02df485 138 #endif
mluis 0:92bca02df485 139
mluis 0:92bca02df485 140 /*!
mluis 0:92bca02df485 141 * Indicates if the MAC layer has already joined a network.
mluis 0:92bca02df485 142 */
mluis 0:92bca02df485 143 static bool IsNetworkJoined = false;
mluis 0:92bca02df485 144 static bool IsNetworkJoinedStatusUpdate = false;
mluis 0:92bca02df485 145
mluis 0:92bca02df485 146 /*!
mluis 0:92bca02df485 147 * User application data
mluis 0:92bca02df485 148 */
mluis 0:92bca02df485 149 static uint8_t AppData[LORAWAN_APP_DATA_SIZE];
mluis 0:92bca02df485 150
mluis 0:92bca02df485 151 /*!
mluis 0:92bca02df485 152 * Defines the application data transmission duty cycle
mluis 0:92bca02df485 153 */
mluis 0:92bca02df485 154 static uint32_t TxDutyCycleTime;
mluis 0:92bca02df485 155
mluis 0:92bca02df485 156 TimerEvent_t TxNextPacketTimer;
mluis 0:92bca02df485 157
mluis 0:92bca02df485 158 #if( OVER_THE_AIR_ACTIVATION != 0 )
mluis 0:92bca02df485 159
mluis 0:92bca02df485 160 /*!
mluis 0:92bca02df485 161 * Defines the join request timer
mluis 0:92bca02df485 162 */
mluis 0:92bca02df485 163 TimerEvent_t JoinReqTimer;
mluis 0:92bca02df485 164
mluis 0:92bca02df485 165 #endif
mluis 0:92bca02df485 166
mluis 0:92bca02df485 167 /*!
mluis 0:92bca02df485 168 * Indicates if a new packet can be sent
mluis 0:92bca02df485 169 */
mluis 0:92bca02df485 170 static bool TxNextPacket = true;
mluis 0:92bca02df485 171 static bool ScheduleNextTx = false;
mluis 0:92bca02df485 172
mluis 0:92bca02df485 173 static bool AppLedStateOn = false;
mluis 0:92bca02df485 174
mluis 0:92bca02df485 175 static LoRaMacEvent_t LoRaMacEvents;
mluis 0:92bca02df485 176
mluis 0:92bca02df485 177 TimerEvent_t Led1Timer;
mluis 0:92bca02df485 178 volatile bool Led1StateChanged = false;
mluis 0:92bca02df485 179
mluis 0:92bca02df485 180 TimerEvent_t Led2Timer;
mluis 0:92bca02df485 181 volatile bool Led2StateChanged = false;
mluis 0:92bca02df485 182
mluis 0:92bca02df485 183 volatile bool Led3StateChanged = false;
mluis 0:92bca02df485 184
mluis 0:92bca02df485 185 volatile bool LinkStatusUpdated = false;
mluis 0:92bca02df485 186
mluis 0:92bca02df485 187 struct sLoRaMacUplinkStatus
mluis 0:92bca02df485 188 {
mluis 0:92bca02df485 189 uint8_t Acked;
mluis 0:92bca02df485 190 int8_t Datarate;
mluis 0:92bca02df485 191 uint16_t UplinkCounter;
mluis 0:92bca02df485 192 uint8_t Port;
mluis 0:92bca02df485 193 uint8_t *Buffer;
mluis 0:92bca02df485 194 uint8_t BufferSize;
mluis 0:92bca02df485 195 }LoRaMacUplinkStatus;
mluis 0:92bca02df485 196
mluis 0:92bca02df485 197 struct sLoRaMacDownlinkStatus
mluis 0:92bca02df485 198 {
mluis 0:92bca02df485 199 int16_t Rssi;
mluis 0:92bca02df485 200 int8_t Snr;
mluis 0:92bca02df485 201 uint16_t DownlinkCounter;
mluis 0:92bca02df485 202 bool RxData;
mluis 0:92bca02df485 203 uint8_t Port;
mluis 0:92bca02df485 204 uint8_t *Buffer;
mluis 0:92bca02df485 205 uint8_t BufferSize;
mluis 0:92bca02df485 206 }LoRaMacDownlinkStatus;
mluis 0:92bca02df485 207
mluis 0:92bca02df485 208 void SerialDisplayRefresh( void )
mluis 0:92bca02df485 209 {
mluis 0:92bca02df485 210 SerialDisplayInit( );
mluis 0:92bca02df485 211 SerialDisplayUpdateActivationMode( OVER_THE_AIR_ACTIVATION );
mluis 0:92bca02df485 212
mluis 0:92bca02df485 213 #if( OVER_THE_AIR_ACTIVATION == 0 )
mluis 0:92bca02df485 214 SerialDisplayUpdateNwkId( LORAWAN_NETWORK_ID );
mluis 0:92bca02df485 215 SerialDisplayUpdateDevAddr( LORAWAN_DEVICE_ADDRESS );
mluis 0:92bca02df485 216 SerialDisplayUpdateKey( 12, NwkSKey );
mluis 0:92bca02df485 217 SerialDisplayUpdateKey( 13, AppSKey );
mluis 0:92bca02df485 218 #else
mluis 0:92bca02df485 219 SerialDisplayUpdateEui( 5, DevEui );
mluis 0:92bca02df485 220 SerialDisplayUpdateEui( 6, AppEui );
mluis 0:92bca02df485 221 SerialDisplayUpdateKey( 7, AppKey );
mluis 0:92bca02df485 222 #endif
mluis 0:92bca02df485 223 SerialDisplayUpdateNetworkIsJoined( IsNetworkJoined );
mluis 0:92bca02df485 224
mluis 0:92bca02df485 225 SerialDisplayUpdateAdr( LORAWAN_ADR_ON );
mluis 0:92bca02df485 226 SerialDisplayUpdateDutyCycle( LORAWAN_DUTYCYCLE_ON );
mluis 0:92bca02df485 227 SerialDisplayUpdatePublicNetwork( LORAWAN_PUBLIC_NETWORK );
mluis 0:92bca02df485 228
mluis 0:92bca02df485 229 SerialDisplayUpdateLedState( 3, AppLedStateOn );
mluis 0:92bca02df485 230 }
mluis 0:92bca02df485 231
mluis 0:92bca02df485 232 void SerialRxProcess( void )
mluis 0:92bca02df485 233 {
mluis 0:92bca02df485 234 if( SerialDisplayReadable( ) == true )
mluis 0:92bca02df485 235 {
mluis 0:92bca02df485 236 switch( SerialDisplayGetChar( ) )
mluis 0:92bca02df485 237 {
mluis 0:92bca02df485 238 case 'R':
mluis 0:92bca02df485 239 case 'r':
mluis 0:92bca02df485 240 // Refresh Serial screen
mluis 0:92bca02df485 241 SerialDisplayRefresh( );
mluis 0:92bca02df485 242 break;
mluis 0:92bca02df485 243 default:
mluis 0:92bca02df485 244 break;
mluis 0:92bca02df485 245 }
mluis 0:92bca02df485 246 }
mluis 0:92bca02df485 247 }
mluis 0:92bca02df485 248
mluis 0:92bca02df485 249 /*!
mluis 0:92bca02df485 250 * Prepares the frame buffer to be sent
mluis 0:92bca02df485 251 */
mluis 0:92bca02df485 252 static void PrepareTxFrame( uint8_t port )
mluis 0:92bca02df485 253 {
mluis 0:92bca02df485 254 AppData[0] = AppLedStateOn;
mluis 0:92bca02df485 255 #if ( LORAWAN_CONFIRMED_MSG_ON == true )
mluis 0:92bca02df485 256 AppData[1] = LoRaMacDownlinkStatus.DownlinkCounter >> 8;
mluis 0:92bca02df485 257 AppData[2] = LoRaMacDownlinkStatus.DownlinkCounter;
mluis 0:92bca02df485 258 AppData[3] = LoRaMacDownlinkStatus.Rssi >> 8;
mluis 0:92bca02df485 259 AppData[4] = LoRaMacDownlinkStatus.Rssi;
mluis 0:92bca02df485 260 AppData[5] = LoRaMacDownlinkStatus.Snr;
mluis 0:92bca02df485 261 #endif
mluis 0:92bca02df485 262 }
mluis 0:92bca02df485 263
mluis 0:92bca02df485 264 static void ProcessRxFrame( LoRaMacEventFlags_t *flags, LoRaMacEventInfo_t *info )
mluis 0:92bca02df485 265 {
mluis 0:92bca02df485 266 switch( info->RxPort ) // Check Rx port number
mluis 0:92bca02df485 267 {
mluis 0:92bca02df485 268 case 1: // The application LED can be controlled on port 1 or 2
mluis 0:92bca02df485 269 case 2:
mluis 0:92bca02df485 270 if( info->RxBufferSize == 1 )
mluis 0:92bca02df485 271 {
mluis 0:92bca02df485 272 AppLedStateOn = info->RxBuffer[0] & 0x01;
mluis 0:92bca02df485 273 Led3StateChanged = true;
mluis 0:92bca02df485 274 }
mluis 0:92bca02df485 275 break;
mluis 0:92bca02df485 276 default:
mluis 0:92bca02df485 277 break;
mluis 0:92bca02df485 278 }
mluis 0:92bca02df485 279 }
mluis 0:92bca02df485 280
mluis 0:92bca02df485 281 static bool SendFrame( void )
mluis 0:92bca02df485 282 {
mluis 0:92bca02df485 283 uint8_t sendFrameStatus = 0;
mluis 0:92bca02df485 284
mluis 0:92bca02df485 285 LoRaMacUplinkStatus.Acked = false;
mluis 0:92bca02df485 286 LoRaMacUplinkStatus.Port = LORAWAN_APP_PORT;
mluis 0:92bca02df485 287 LoRaMacUplinkStatus.Buffer = AppData;
mluis 0:92bca02df485 288 LoRaMacUplinkStatus.BufferSize = LORAWAN_APP_DATA_SIZE;
mluis 0:92bca02df485 289
mluis 0:92bca02df485 290 SerialDisplayUpdateFrameType( LORAWAN_CONFIRMED_MSG_ON );
mluis 0:92bca02df485 291 #if( LORAWAN_CONFIRMED_MSG_ON == false )
mluis 0:92bca02df485 292 sendFrameStatus = LoRaMacSendFrame( LORAWAN_APP_PORT, AppData, LORAWAN_APP_DATA_SIZE );
mluis 0:92bca02df485 293 #else
mluis 0:92bca02df485 294 sendFrameStatus = LoRaMacSendConfirmedFrame( LORAWAN_APP_PORT, AppData, LORAWAN_APP_DATA_SIZE, 8 );
mluis 0:92bca02df485 295 #endif
mluis 0:92bca02df485 296 switch( sendFrameStatus )
mluis 0:92bca02df485 297 {
mluis 0:92bca02df485 298 case 5: // NO_FREE_CHANNEL
mluis 0:92bca02df485 299 // Try again later
mluis 0:92bca02df485 300 return true;
mluis 0:92bca02df485 301 default:
mluis 0:92bca02df485 302 return false;
mluis 0:92bca02df485 303 }
mluis 0:92bca02df485 304 }
mluis 0:92bca02df485 305
mluis 0:92bca02df485 306 #if( OVER_THE_AIR_ACTIVATION != 0 )
mluis 0:92bca02df485 307
mluis 0:92bca02df485 308 /*!
mluis 0:92bca02df485 309 * \brief Function executed on JoinReq Timeout event
mluis 0:92bca02df485 310 */
mluis 0:92bca02df485 311 static void OnJoinReqTimerEvent( void )
mluis 0:92bca02df485 312 {
mluis 0:92bca02df485 313 TimerStop( &JoinReqTimer );
mluis 0:92bca02df485 314 TxNextPacket = true;
mluis 0:92bca02df485 315 }
mluis 0:92bca02df485 316
mluis 0:92bca02df485 317 #endif
mluis 0:92bca02df485 318
mluis 0:92bca02df485 319 /*!
mluis 0:92bca02df485 320 * \brief Function executed on TxNextPacket Timeout event
mluis 0:92bca02df485 321 */
mluis 0:92bca02df485 322 static void OnTxNextPacketTimerEvent( void )
mluis 0:92bca02df485 323 {
mluis 0:92bca02df485 324 TimerStop( &TxNextPacketTimer );
mluis 0:92bca02df485 325 TxNextPacket = true;
mluis 0:92bca02df485 326 }
mluis 0:92bca02df485 327
mluis 0:92bca02df485 328 /*!
mluis 0:92bca02df485 329 * \brief Function executed on Led 1 Timeout event
mluis 0:92bca02df485 330 */
mluis 0:92bca02df485 331 static void OnLed1TimerEvent( void )
mluis 0:92bca02df485 332 {
mluis 0:92bca02df485 333 TimerStop( &Led1Timer );
mluis 0:92bca02df485 334 Led1StateChanged = true;
mluis 0:92bca02df485 335 }
mluis 0:92bca02df485 336
mluis 0:92bca02df485 337 /*!
mluis 0:92bca02df485 338 * \brief Function executed on Led 2 Timeout event
mluis 0:92bca02df485 339 */
mluis 0:92bca02df485 340 static void OnLed2TimerEvent( void )
mluis 0:92bca02df485 341 {
mluis 0:92bca02df485 342 TimerStop( &Led2Timer );
mluis 0:92bca02df485 343 Led2StateChanged = true;
mluis 0:92bca02df485 344 }
mluis 0:92bca02df485 345
mluis 0:92bca02df485 346 /*!
mluis 0:92bca02df485 347 * \brief Function to be executed on MAC layer event
mluis 0:92bca02df485 348 */
mluis 0:92bca02df485 349 static void OnMacEvent( LoRaMacEventFlags_t *flags, LoRaMacEventInfo_t *info )
mluis 0:92bca02df485 350 {
mluis 0:92bca02df485 351 if( flags->Bits.JoinAccept == 1 )
mluis 0:92bca02df485 352 {
mluis 0:92bca02df485 353 #if( OVER_THE_AIR_ACTIVATION != 0 )
mluis 0:92bca02df485 354 TimerStop( &JoinReqTimer );
mluis 0:92bca02df485 355 #endif
mluis 0:92bca02df485 356 IsNetworkJoined = true;
mluis 0:92bca02df485 357 IsNetworkJoinedStatusUpdate = true;
mluis 0:92bca02df485 358 }
mluis 0:92bca02df485 359 else
mluis 0:92bca02df485 360 {
mluis 0:92bca02df485 361 if( flags->Bits.Tx == 1 )
mluis 0:92bca02df485 362 {
mluis 0:92bca02df485 363 }
mluis 0:92bca02df485 364
mluis 0:92bca02df485 365 if( flags->Bits.Rx == 1 )
mluis 0:92bca02df485 366 {
mluis 0:92bca02df485 367 if( flags->Bits.RxData == true )
mluis 0:92bca02df485 368 {
mluis 0:92bca02df485 369 ProcessRxFrame( flags, info );
mluis 0:92bca02df485 370 }
mluis 0:92bca02df485 371
mluis 0:92bca02df485 372 LoRaMacDownlinkStatus.Rssi = info->RxRssi;
mluis 0:92bca02df485 373 if( info->RxSnr & 0x80 ) // The SNR sign bit is 1
mluis 0:92bca02df485 374 {
mluis 0:92bca02df485 375 // Invert and divide by 4
mluis 0:92bca02df485 376 LoRaMacDownlinkStatus.Snr = ( ( ~info->RxSnr + 1 ) & 0xFF ) >> 2;
mluis 0:92bca02df485 377 LoRaMacDownlinkStatus.Snr = -LoRaMacDownlinkStatus.Snr;
mluis 0:92bca02df485 378 }
mluis 0:92bca02df485 379 else
mluis 0:92bca02df485 380 {
mluis 0:92bca02df485 381 // Divide by 4
mluis 0:92bca02df485 382 LoRaMacDownlinkStatus.Snr = ( info->RxSnr & 0xFF ) >> 2;
mluis 0:92bca02df485 383 }
mluis 0:92bca02df485 384 LoRaMacDownlinkStatus.DownlinkCounter++;
mluis 0:92bca02df485 385 LoRaMacDownlinkStatus.RxData = flags->Bits.RxData;
mluis 0:92bca02df485 386 LoRaMacDownlinkStatus.Port = info->RxPort;
mluis 0:92bca02df485 387 LoRaMacDownlinkStatus.Buffer = info->RxBuffer;
mluis 0:92bca02df485 388 LoRaMacDownlinkStatus.BufferSize = info->RxBufferSize;
mluis 0:92bca02df485 389
mluis 0:92bca02df485 390 LoRaMacUplinkStatus.Acked = info->TxAckReceived;
mluis 0:92bca02df485 391 LoRaMacUplinkStatus.Datarate = info->TxDatarate;
mluis 0:92bca02df485 392 LoRaMacUplinkStatus.UplinkCounter = LoRaMacGetUpLinkCounter( ) - 1;
mluis 0:92bca02df485 393
mluis 0:92bca02df485 394 LinkStatusUpdated = true;
mluis 0:92bca02df485 395 TimerStart( &Led2Timer );
mluis 0:92bca02df485 396 }
mluis 0:92bca02df485 397 }
mluis 0:92bca02df485 398 // Schedule a new transmission
mluis 0:92bca02df485 399 ScheduleNextTx = true;
mluis 0:92bca02df485 400 }
mluis 0:92bca02df485 401
mluis 0:92bca02df485 402 /**
mluis 0:92bca02df485 403 * Main application entry point.
mluis 0:92bca02df485 404 */
mluis 0:92bca02df485 405 int main( void )
mluis 0:92bca02df485 406 {
mluis 0:92bca02df485 407 SerialDisplayInit( );
mluis 0:92bca02df485 408
mluis 0:92bca02df485 409 #if( OVER_THE_AIR_ACTIVATION != 0 )
mluis 0:92bca02df485 410 uint8_t sendFrameStatus = 0;
mluis 0:92bca02df485 411 #endif
mluis 0:92bca02df485 412 bool trySendingFrameAgain = false;
mluis 0:92bca02df485 413
mluis 0:92bca02df485 414 BoardInit( );
mluis 0:92bca02df485 415
mluis 0:92bca02df485 416 LoRaMacEvents.MacEvent = OnMacEvent;
mluis 0:92bca02df485 417 LoRaMacInit( &LoRaMacEvents, &BoardGetBatterieLevel );
mluis 0:92bca02df485 418
mluis 0:92bca02df485 419 IsNetworkJoined = false;
mluis 0:92bca02df485 420
mluis 0:92bca02df485 421 SerialDisplayUpdateActivationMode( OVER_THE_AIR_ACTIVATION );
mluis 0:92bca02df485 422
mluis 0:92bca02df485 423 #if( OVER_THE_AIR_ACTIVATION == 0 )
mluis 0:92bca02df485 424 LoRaMacInitNwkIds( LORAWAN_NETWORK_ID, LORAWAN_DEVICE_ADDRESS, NwkSKey, AppSKey );
mluis 0:92bca02df485 425 IsNetworkJoined = true;
mluis 0:92bca02df485 426
mluis 0:92bca02df485 427 SerialDisplayUpdateNwkId( LORAWAN_NETWORK_ID );
mluis 0:92bca02df485 428 SerialDisplayUpdateDevAddr( LORAWAN_DEVICE_ADDRESS );
mluis 0:92bca02df485 429 SerialDisplayUpdateKey( 12, NwkSKey );
mluis 0:92bca02df485 430 SerialDisplayUpdateKey( 13, AppSKey );
mluis 0:92bca02df485 431 #else
mluis 0:92bca02df485 432 // Sends a JoinReq Command every 5 seconds until the network is joined
mluis 0:92bca02df485 433 TimerInit( &JoinReqTimer, OnJoinReqTimerEvent );
mluis 0:92bca02df485 434 TimerSetValue( &JoinReqTimer, OVER_THE_AIR_ACTIVATION_DUTYCYCLE );
mluis 0:92bca02df485 435
mluis 0:92bca02df485 436 SerialDisplayUpdateEui( 5, DevEui );
mluis 0:92bca02df485 437 SerialDisplayUpdateEui( 6, AppEui );
mluis 0:92bca02df485 438 SerialDisplayUpdateKey( 7, AppKey );
mluis 0:92bca02df485 439
mluis 0:92bca02df485 440 #endif
mluis 0:92bca02df485 441
mluis 0:92bca02df485 442 SerialDisplayUpdateNetworkIsJoined( IsNetworkJoined );
mluis 0:92bca02df485 443
mluis 0:92bca02df485 444 TxNextPacket = true;
mluis 0:92bca02df485 445 TimerInit( &TxNextPacketTimer, OnTxNextPacketTimerEvent );
mluis 0:92bca02df485 446
mluis 0:92bca02df485 447 TimerInit( &Led1Timer, OnLed1TimerEvent );
mluis 0:92bca02df485 448 TimerSetValue( &Led1Timer, 500000 );
mluis 0:92bca02df485 449
mluis 0:92bca02df485 450 TimerInit( &Led2Timer, OnLed2TimerEvent );
mluis 0:92bca02df485 451 TimerSetValue( &Led2Timer, 500000 );
mluis 0:92bca02df485 452
mluis 0:92bca02df485 453 LoRaMacSetAdrOn( LORAWAN_ADR_ON );
mluis 0:92bca02df485 454 LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON );
mluis 0:92bca02df485 455 LoRaMacSetPublicNetwork( LORAWAN_PUBLIC_NETWORK );
mluis 0:92bca02df485 456
mluis 0:92bca02df485 457 SerialDisplayUpdateAdr( LORAWAN_ADR_ON );
mluis 0:92bca02df485 458 SerialDisplayUpdateDutyCycle( LORAWAN_DUTYCYCLE_ON );
mluis 0:92bca02df485 459 SerialDisplayUpdatePublicNetwork( LORAWAN_PUBLIC_NETWORK );
mluis 0:92bca02df485 460
mluis 0:92bca02df485 461 LoRaMacDownlinkStatus.DownlinkCounter = 0;
mluis 0:92bca02df485 462
mluis 0:92bca02df485 463 while( 1 )
mluis 0:92bca02df485 464 {
mluis 0:92bca02df485 465 while( IsNetworkJoined == false )
mluis 0:92bca02df485 466 {
mluis 0:92bca02df485 467 #if( OVER_THE_AIR_ACTIVATION != 0 )
mluis 0:92bca02df485 468 if( TxNextPacket == true )
mluis 0:92bca02df485 469 {
mluis 0:92bca02df485 470 TxNextPacket = false;
mluis 0:92bca02df485 471
mluis 0:92bca02df485 472 sendFrameStatus = LoRaMacJoinReq( DevEui, AppEui, AppKey );
mluis 0:92bca02df485 473 switch( sendFrameStatus )
mluis 0:92bca02df485 474 {
mluis 0:92bca02df485 475 case 1: // BUSY
mluis 0:92bca02df485 476 break;
mluis 0:92bca02df485 477 case 0: // OK
mluis 0:92bca02df485 478 case 2: // NO_NETWORK_JOINED
mluis 0:92bca02df485 479 case 3: // LENGTH_PORT_ERROR
mluis 0:92bca02df485 480 case 4: // MAC_CMD_ERROR
mluis 0:92bca02df485 481 case 6: // DEVICE_OFF
mluis 0:92bca02df485 482 default:
mluis 0:92bca02df485 483 // Relaunch timer for next trial
mluis 0:92bca02df485 484 TimerStart( &JoinReqTimer );
mluis 0:92bca02df485 485 break;
mluis 0:92bca02df485 486 }
mluis 0:92bca02df485 487 }
mluis 0:92bca02df485 488 SerialRxProcess( );
mluis 0:92bca02df485 489 #endif
mluis 0:92bca02df485 490 }
mluis 0:92bca02df485 491
mluis 0:92bca02df485 492 SerialRxProcess( );
mluis 0:92bca02df485 493
mluis 0:92bca02df485 494 if( IsNetworkJoinedStatusUpdate == true )
mluis 0:92bca02df485 495 {
mluis 0:92bca02df485 496 IsNetworkJoinedStatusUpdate = false;
mluis 0:92bca02df485 497 SerialDisplayUpdateNetworkIsJoined( IsNetworkJoined );
mluis 0:92bca02df485 498 }
mluis 0:92bca02df485 499 if( Led1StateChanged == true )
mluis 0:92bca02df485 500 {
mluis 0:92bca02df485 501 Led1StateChanged = false;
mluis 0:92bca02df485 502 SerialDisplayUpdateLedState( 1, 0 );
mluis 0:92bca02df485 503 }
mluis 0:92bca02df485 504 if( Led2StateChanged == true )
mluis 0:92bca02df485 505 {
mluis 0:92bca02df485 506 Led2StateChanged = false;
mluis 0:92bca02df485 507 SerialDisplayUpdateLedState( 2, 0 );
mluis 0:92bca02df485 508 }
mluis 0:92bca02df485 509 if( Led3StateChanged == true )
mluis 0:92bca02df485 510 {
mluis 0:92bca02df485 511 Led3StateChanged = false;
mluis 0:92bca02df485 512 SerialDisplayUpdateLedState( 3, AppLedStateOn );
mluis 0:92bca02df485 513 }
mluis 0:92bca02df485 514 if( LinkStatusUpdated == true )
mluis 0:92bca02df485 515 {
mluis 0:92bca02df485 516 LinkStatusUpdated = false;
mluis 0:92bca02df485 517 SerialDisplayUpdateLedState( 2, 1 );
mluis 0:92bca02df485 518 SerialDisplayUpdateUplink( LoRaMacUplinkStatus.Acked, LoRaMacUplinkStatus.Datarate, LoRaMacUplinkStatus.UplinkCounter, LoRaMacUplinkStatus.Port, LoRaMacUplinkStatus.Buffer, LoRaMacUplinkStatus.BufferSize );
mluis 0:92bca02df485 519 SerialDisplayUpdateDownlink( LoRaMacDownlinkStatus.RxData, LoRaMacDownlinkStatus.Rssi, LoRaMacDownlinkStatus.Snr, LoRaMacDownlinkStatus.DownlinkCounter, LoRaMacDownlinkStatus.Port, LoRaMacDownlinkStatus.Buffer, LoRaMacDownlinkStatus.BufferSize );
mluis 0:92bca02df485 520 }
mluis 0:92bca02df485 521
mluis 0:92bca02df485 522 if( ScheduleNextTx == true )
mluis 0:92bca02df485 523 {
mluis 0:92bca02df485 524 ScheduleNextTx = false;
mluis 0:92bca02df485 525 // Schedule next packet transmission
mluis 0:92bca02df485 526 TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND );
mluis 0:92bca02df485 527 TimerSetValue( &TxNextPacketTimer, TxDutyCycleTime );
mluis 0:92bca02df485 528 TimerStart( &TxNextPacketTimer );
mluis 0:92bca02df485 529 }
mluis 0:92bca02df485 530
mluis 0:92bca02df485 531 if( trySendingFrameAgain == true )
mluis 0:92bca02df485 532 {
mluis 0:92bca02df485 533 trySendingFrameAgain = SendFrame( );
mluis 0:92bca02df485 534 }
mluis 0:92bca02df485 535 if( TxNextPacket == true )
mluis 0:92bca02df485 536 {
mluis 0:92bca02df485 537 TxNextPacket = false;
mluis 0:92bca02df485 538
mluis 0:92bca02df485 539 SerialDisplayUpdateDonwlinkRxData( false );
mluis 0:92bca02df485 540
mluis 0:92bca02df485 541 PrepareTxFrame( LORAWAN_APP_PORT );
mluis 0:92bca02df485 542
mluis 0:92bca02df485 543 SerialDisplayUpdateLedState( 1, 1 );
mluis 0:92bca02df485 544 TimerStart( &Led1Timer );
mluis 0:92bca02df485 545
mluis 0:92bca02df485 546 trySendingFrameAgain = SendFrame( );
mluis 0:92bca02df485 547
mluis 0:92bca02df485 548 SerialDisplayUpdateUplink( LoRaMacUplinkStatus.Acked, LoRaMacUplinkStatus.Datarate, LoRaMacUplinkStatus.UplinkCounter, LoRaMacUplinkStatus.Port, LoRaMacUplinkStatus.Buffer, LoRaMacUplinkStatus.BufferSize );
mluis 0:92bca02df485 549 }
mluis 0:92bca02df485 550 }
mluis 0:92bca02df485 551 }