Semtech LoRa stack ver. 3.4 for ELMO

Dependencies:   SX1272lib mbed

Committer:
mleksio
Date:
Wed Feb 10 10:11:01 2016 +0000
Revision:
1:5310eff49dee
Parent:
0:c58229885f95
Semtech LoRa stack ver. 3.4 for ELMO

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mleksio 0:c58229885f95 1 /*
mleksio 0:c58229885f95 2
mleksio 0:c58229885f95 3 Description: MBED LoRaWAN example application
mleksio 0:c58229885f95 4
mleksio 0:c58229885f95 5 License: Revised BSD License, see LICENSE.TXT file include in the project
mleksio 0:c58229885f95 6
mleksio 0:c58229885f95 7 Maintainer: Espotel
mleksio 0:c58229885f95 8 */
mleksio 0:c58229885f95 9
mleksio 0:c58229885f95 10 #include "mbed.h"
mleksio 0:c58229885f95 11
mleksio 0:c58229885f95 12 #include <string.h>
mleksio 0:c58229885f95 13 #include <math.h>
mleksio 0:c58229885f95 14 //#include "board.h"
mleksio 0:c58229885f95 15
mleksio 0:c58229885f95 16 #include "LoRaMac.h"
mleksio 0:c58229885f95 17 #include "utilities.h"
mleksio 0:c58229885f95 18
mleksio 0:c58229885f95 19 #define USE_BAND_868
mleksio 0:c58229885f95 20 /*!
mleksio 0:c58229885f95 21 * When set to 1 the application uses the Over-the-Air activation procedure
mleksio 0:c58229885f95 22 * When set to 0 the application uses the Personalization activation procedure
mleksio 0:c58229885f95 23 */
mleksio 0:c58229885f95 24 #define OVER_THE_AIR_ACTIVATION 1
mleksio 0:c58229885f95 25
mleksio 0:c58229885f95 26 /*!
mleksio 0:c58229885f95 27 * Indicates if the end-device is to be connected to a private or public network
mleksio 0:c58229885f95 28 */
mleksio 0:c58229885f95 29 #define LORAWAN_PUBLIC_NETWORK true
mleksio 0:c58229885f95 30
mleksio 0:c58229885f95 31 #if( OVER_THE_AIR_ACTIVATION != 0 )
mleksio 0:c58229885f95 32
mleksio 0:c58229885f95 33 /*!
mleksio 0:c58229885f95 34 * Join requests trials duty cycle.
mleksio 0:c58229885f95 35 */
mleksio 0:c58229885f95 36 #define OVER_THE_AIR_ACTIVATION_DUTYCYCLE 10000000 // 10 [s] value in us
mleksio 0:c58229885f95 37
mleksio 0:c58229885f95 38 /*!
mleksio 0:c58229885f95 39 * Mote device IEEE EUI
mleksio 0:c58229885f95 40 *
mleksio 0:c58229885f95 41 * \remark must be written as a little endian value (reverse order of normal reading)
mleksio 0:c58229885f95 42 *
mleksio 0:c58229885f95 43 * \remark In this application the value is automatically generated by calling
mleksio 0:c58229885f95 44 * BoardGetUniqueId function
mleksio 0:c58229885f95 45 */
mleksio 0:c58229885f95 46 #define LORAWAN_DEVICE_EUI { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
mleksio 0:c58229885f95 47
mleksio 0:c58229885f95 48 /*!
mleksio 0:c58229885f95 49 * Application IEEE EUI
mleksio 0:c58229885f95 50 *
mleksio 0:c58229885f95 51 * \remark must be written as a little endian value (reverse order of normal reading)
mleksio 0:c58229885f95 52 */
mleksio 0:c58229885f95 53 //#define LORAWAN_APPLICATION_EUI { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 }
mleksio 0:c58229885f95 54 #define LORAWAN_APPLICATION_EUI { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
mleksio 0:c58229885f95 55
mleksio 0:c58229885f95 56 /*!
mleksio 0:c58229885f95 57 * AES encryption/decryption cipher application key
mleksio 0:c58229885f95 58 */
mleksio 0:c58229885f95 59
mleksio 0:c58229885f95 60 #define LORAWAN_APPLICATION_KEY { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00 }
mleksio 0:c58229885f95 61 //#define LORAWAN_APPLICATION_KEY { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
mleksio 0:c58229885f95 62
mleksio 0:c58229885f95 63 #else
mleksio 0:c58229885f95 64
mleksio 0:c58229885f95 65 /*!
mleksio 0:c58229885f95 66 * Current network ID
mleksio 0:c58229885f95 67 */
mleksio 0:c58229885f95 68 #define LORAWAN_NETWORK_ID ( uint32_t )0
mleksio 0:c58229885f95 69
mleksio 0:c58229885f95 70 /*!
mleksio 0:c58229885f95 71 * Device address on the network
mleksio 0:c58229885f95 72 *
mleksio 0:c58229885f95 73 * \remark must be written as a big endian value (normal reading order)
mleksio 0:c58229885f95 74 *
mleksio 0:c58229885f95 75 * \remark In this application the value is automatically generated using
mleksio 0:c58229885f95 76 * a pseudo random generator seeded with a value derived from
mleksio 0:c58229885f95 77 * BoardUniqueId value
mleksio 0:c58229885f95 78 */
mleksio 0:c58229885f95 79 #define LORAWAN_DEVICE_ADDRESS ( uint32_t )0x00000000
mleksio 0:c58229885f95 80
mleksio 0:c58229885f95 81 /*!
mleksio 0:c58229885f95 82 * AES encryption/decryption cipher network session key
mleksio 0:c58229885f95 83 */
mleksio 0:c58229885f95 84 #define LORAWAN_NWKSKEY { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C }
mleksio 0:c58229885f95 85
mleksio 0:c58229885f95 86 /*!
mleksio 0:c58229885f95 87 * AES encryption/decryption cipher application session key
mleksio 0:c58229885f95 88 */
mleksio 0:c58229885f95 89 #define LORAWAN_APPSKEY { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C }
mleksio 0:c58229885f95 90
mleksio 0:c58229885f95 91 #endif
mleksio 0:c58229885f95 92
mleksio 0:c58229885f95 93 /*!
mleksio 1:5310eff49dee 94 * Defines the application data transmission duty cycle. 5s, value in [us].
mleksio 0:c58229885f95 95 */
mleksio 1:5310eff49dee 96 #define APP_TX_DUTYCYCLE 5000000
mleksio 1:5310eff49dee 97 /*!
mleksio 1:5310eff49dee 98 * Defines a random delay for application data transmission duty cycle. 1s,
mleksio 1:5310eff49dee 99 * value in [us].
mleksio 1:5310eff49dee 100 */
mleksio 1:5310eff49dee 101 #define APP_TX_DUTYCYCLE_RND 1000000
mleksio 0:c58229885f95 102
mleksio 0:c58229885f95 103 /*!
mleksio 0:c58229885f95 104 * LoRaWAN confirmed messages
mleksio 0:c58229885f95 105 */
mleksio 1:5310eff49dee 106 #define LORAWAN_CONFIRMED_MSG_ON true
mleksio 0:c58229885f95 107
mleksio 0:c58229885f95 108 /*!
mleksio 0:c58229885f95 109 * LoRaWAN Adaptative Data Rate
mleksio 0:c58229885f95 110 *
mleksio 0:c58229885f95 111 * \remark Please note that when ADR is enabled the end-device should be static
mleksio 0:c58229885f95 112 */
mleksio 0:c58229885f95 113 #define LORAWAN_ADR_ON 1
mleksio 0:c58229885f95 114
mleksio 0:c58229885f95 115 /*!
mleksio 0:c58229885f95 116 * LoRaWAN ETSI duty cycle control enable/disable
mleksio 0:c58229885f95 117 *
mleksio 0:c58229885f95 118 * \remark Please note that ETSI mandates duty cycled transmissions. Use only for test purposes
mleksio 0:c58229885f95 119 */
mleksio 0:c58229885f95 120 #define LORAWAN_DUTYCYCLE_ON true
mleksio 0:c58229885f95 121 #define LORAWAN_DUTYCYCLE_OFF false
mleksio 0:c58229885f95 122
mleksio 0:c58229885f95 123 /*!
mleksio 0:c58229885f95 124 * LoRaWAN application port
mleksio 0:c58229885f95 125 */
mleksio 0:c58229885f95 126 #define LORAWAN_APP_PORT 2
mleksio 0:c58229885f95 127
mleksio 0:c58229885f95 128 /*!
mleksio 0:c58229885f95 129 * User application data buffer size
mleksio 0:c58229885f95 130 */
mleksio 0:c58229885f95 131 #define LORAWAN_APP_DATA_SIZE 16
mleksio 0:c58229885f95 132
mleksio 0:c58229885f95 133 #if( OVER_THE_AIR_ACTIVATION != 0 )
mleksio 0:c58229885f95 134
mleksio 0:c58229885f95 135 static uint8_t DevEui[] = LORAWAN_DEVICE_EUI;
mleksio 0:c58229885f95 136 static uint8_t AppEui[] = LORAWAN_APPLICATION_EUI;
mleksio 0:c58229885f95 137 static uint8_t AppKey[] = LORAWAN_APPLICATION_KEY;
mleksio 0:c58229885f95 138
mleksio 0:c58229885f95 139 #else
mleksio 0:c58229885f95 140
mleksio 0:c58229885f95 141 static uint8_t NwkSKey[] = LORAWAN_NWKSKEY;
mleksio 0:c58229885f95 142 static uint8_t AppSKey[] = LORAWAN_APPSKEY;
mleksio 0:c58229885f95 143
mleksio 0:c58229885f95 144 /*!
mleksio 0:c58229885f95 145 * Device address
mleksio 0:c58229885f95 146 */
mleksio 0:c58229885f95 147 static uint32_t DevAddr;
mleksio 0:c58229885f95 148
mleksio 0:c58229885f95 149 #endif
mleksio 0:c58229885f95 150
mleksio 0:c58229885f95 151 /*!
mleksio 0:c58229885f95 152 * Indicates if the MAC layer has already joined a network.
mleksio 0:c58229885f95 153 */
mleksio 0:c58229885f95 154 static bool IsNetworkJoined = false;
mleksio 0:c58229885f95 155
mleksio 0:c58229885f95 156 /*!
mleksio 0:c58229885f95 157 * Application port
mleksio 0:c58229885f95 158 */
mleksio 0:c58229885f95 159 static uint8_t AppPort = LORAWAN_APP_PORT;
mleksio 0:c58229885f95 160
mleksio 0:c58229885f95 161 /*!
mleksio 0:c58229885f95 162 * User application data size
mleksio 0:c58229885f95 163 */
mleksio 0:c58229885f95 164 static uint8_t AppDataSize = LORAWAN_APP_DATA_SIZE;
mleksio 0:c58229885f95 165
mleksio 0:c58229885f95 166 /*!
mleksio 0:c58229885f95 167 * User application data buffer size
mleksio 0:c58229885f95 168 */
mleksio 0:c58229885f95 169 #define LORAWAN_APP_DATA_MAX_SIZE 64
mleksio 0:c58229885f95 170
mleksio 0:c58229885f95 171 /*!
mleksio 0:c58229885f95 172 * User application data
mleksio 0:c58229885f95 173 */
mleksio 0:c58229885f95 174 static uint8_t AppData[LORAWAN_APP_DATA_MAX_SIZE];
mleksio 0:c58229885f95 175
mleksio 0:c58229885f95 176
mleksio 0:c58229885f95 177 /*!
mleksio 0:c58229885f95 178 * Indicates if the node is sending confirmed or unconfirmed messages
mleksio 0:c58229885f95 179 */
mleksio 0:c58229885f95 180 static uint8_t IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON;
mleksio 0:c58229885f95 181
mleksio 0:c58229885f95 182 /*!
mleksio 0:c58229885f95 183 * Defines the application data transmission duty cycle
mleksio 0:c58229885f95 184 */
mleksio 0:c58229885f95 185 static uint32_t TxDutyCycleTime;
mleksio 0:c58229885f95 186
mleksio 0:c58229885f95 187
mleksio 0:c58229885f95 188 static void OnTxNextPacketTimerEvent( void );
mleksio 0:c58229885f95 189 static Timeout TxNextPacketTimer;//(OnTxNextPacketTimerEvent, osTimerOnce);
mleksio 0:c58229885f95 190
mleksio 0:c58229885f95 191 #if( OVER_THE_AIR_ACTIVATION != 0 )
mleksio 0:c58229885f95 192
mleksio 0:c58229885f95 193 static void OnJoinReqTimerEvent( void );
mleksio 0:c58229885f95 194 /*!
mleksio 0:c58229885f95 195 * Defines the join request timer
mleksio 0:c58229885f95 196 */
mleksio 0:c58229885f95 197 static Timeout JoinReqTimer;//(OnJoinReqTimerEvent, osTimerOnce);
mleksio 0:c58229885f95 198
mleksio 0:c58229885f95 199 #endif
mleksio 0:c58229885f95 200
mleksio 0:c58229885f95 201 /*!
mleksio 0:c58229885f95 202 * Indicates if a new packet can be sent
mleksio 0:c58229885f95 203 */
mleksio 0:c58229885f95 204 static bool TxNextPacket = true;
mleksio 0:c58229885f95 205 static bool ScheduleNextTx = false;
mleksio 0:c58229885f95 206 static bool DownlinkStatusUpdate = false;
mleksio 0:c58229885f95 207
mleksio 0:c58229885f95 208 static bool AppLedStateOn = false;
mleksio 0:c58229885f95 209
mleksio 0:c58229885f95 210 static LoRaMacCallbacks_t LoRaMacCallbacks;
mleksio 0:c58229885f95 211 /*
mleksio 0:c58229885f95 212 static TimerEvent_t Led1Timer;
mleksio 0:c58229885f95 213 volatile bool Led1StateChanged = false;
mleksio 0:c58229885f95 214 static TimerEvent_t Led2Timer;
mleksio 0:c58229885f95 215 volatile bool Led2StateChanged = false;
mleksio 0:c58229885f95 216 static TimerEvent_t Led4Timer;
mleksio 0:c58229885f95 217 volatile bool Led4StateChanged = false;
mleksio 0:c58229885f95 218
mleksio 0:c58229885f95 219 volatile bool Led3StateChanged = false;
mleksio 0:c58229885f95 220 */
mleksio 0:c58229885f95 221 static bool ComplianceTestOn = false;
mleksio 0:c58229885f95 222 static uint8_t ComplianceTestState = 0;
mleksio 0:c58229885f95 223 static uint16_t ComplianceTestDownLinkCounter = 0;
mleksio 0:c58229885f95 224 static bool ComplianceTestLinkCheck = false;
mleksio 0:c58229885f95 225 static uint8_t ComplianceTestDemodMargin = 0;
mleksio 0:c58229885f95 226 static uint8_t ComplianceTestNbGateways = 0;
mleksio 0:c58229885f95 227
mleksio 0:c58229885f95 228 /*!
mleksio 0:c58229885f95 229 * Prepares the frame buffer to be sent
mleksio 0:c58229885f95 230 */
mleksio 0:c58229885f95 231 static void PrepareTxFrame( uint8_t port )
mleksio 0:c58229885f95 232 {
mleksio 0:c58229885f95 233 switch( port )
mleksio 0:c58229885f95 234 {
mleksio 0:c58229885f95 235 case 2:
mleksio 0:c58229885f95 236 {
mleksio 0:c58229885f95 237 uint16_t pressure = 0;
mleksio 0:c58229885f95 238 int16_t altitudeBar = 0;
mleksio 0:c58229885f95 239 int16_t temperature = 0;
mleksio 0:c58229885f95 240 int32_t latitude, longitude = 0;
mleksio 0:c58229885f95 241 uint16_t altitudeGps = 0xFFFF;
mleksio 0:c58229885f95 242 uint8_t batteryLevel = 0;
mleksio 0:c58229885f95 243
mleksio 0:c58229885f95 244 //pressure = ( uint16_t )( MPL3115ReadPressure( ) / 10 ); // in hPa / 10
mleksio 0:c58229885f95 245 //temperature = ( int16_t )( MPL3115ReadTemperature( ) * 100 ); // in °C * 100
mleksio 0:c58229885f95 246 //altitudeBar = ( int16_t )( MPL3115ReadAltitude( ) * 10 ); // in m * 10
mleksio 0:c58229885f95 247 //batteryLevel = BoardGetBatteryLevel( ); // 1 (very low) to 254 (fully charged)
mleksio 0:c58229885f95 248 //GpsGetLatestGpsPositionBinary( &latitude, &longitude );
mleksio 0:c58229885f95 249 //altitudeGps = GpsGetLatestGpsAltitude( ); // in m
mleksio 0:c58229885f95 250
mleksio 0:c58229885f95 251 AppData[0] = AppLedStateOn;
mleksio 0:c58229885f95 252 AppData[1] = ( pressure >> 8 ) & 0xFF;
mleksio 0:c58229885f95 253 AppData[2] = pressure & 0xFF;
mleksio 0:c58229885f95 254 AppData[3] = ( temperature >> 8 ) & 0xFF;
mleksio 0:c58229885f95 255 AppData[4] = temperature & 0xFF;
mleksio 0:c58229885f95 256 AppData[5] = ( altitudeBar >> 8 ) & 0xFF;
mleksio 0:c58229885f95 257 AppData[6] = altitudeBar & 0xFF;
mleksio 0:c58229885f95 258 AppData[7] = batteryLevel;
mleksio 0:c58229885f95 259 AppData[8] = ( latitude >> 16 ) & 0xFF;
mleksio 0:c58229885f95 260 AppData[9] = ( latitude >> 8 ) & 0xFF;
mleksio 0:c58229885f95 261 AppData[10] = latitude & 0xFF;
mleksio 0:c58229885f95 262 AppData[11] = ( longitude >> 16 ) & 0xFF;
mleksio 0:c58229885f95 263 AppData[12] = ( longitude >> 8 ) & 0xFF;
mleksio 0:c58229885f95 264 AppData[13] = longitude & 0xFF;
mleksio 0:c58229885f95 265 AppData[14] = ( altitudeGps >> 8 ) & 0xFF;
mleksio 0:c58229885f95 266 AppData[15] = altitudeGps & 0xFF;
mleksio 0:c58229885f95 267 }
mleksio 0:c58229885f95 268 break;
mleksio 0:c58229885f95 269 case 224:
mleksio 0:c58229885f95 270 if( ComplianceTestLinkCheck == true )
mleksio 0:c58229885f95 271 {
mleksio 0:c58229885f95 272 printf("ComplianceTestLinkCheck\r\n");
mleksio 0:c58229885f95 273 ComplianceTestLinkCheck = false;
mleksio 0:c58229885f95 274 AppDataSize = 3;
mleksio 0:c58229885f95 275 AppData[0] = 5;
mleksio 0:c58229885f95 276 AppData[1] = ComplianceTestDemodMargin;
mleksio 0:c58229885f95 277 AppData[2] = ComplianceTestNbGateways;
mleksio 0:c58229885f95 278 ComplianceTestState = 1;
mleksio 0:c58229885f95 279 }
mleksio 0:c58229885f95 280 else
mleksio 0:c58229885f95 281 {
mleksio 1:5310eff49dee 282 switch( ComplianceTestState )
mleksio 0:c58229885f95 283 {
mleksio 1:5310eff49dee 284 case 4:
mleksio 0:c58229885f95 285 printf("Sending downlink COUNTER!!!!\r\n");
mleksio 1:5310eff49dee 286 ComplianceTestState = 1;
mleksio 1:5310eff49dee 287 break;
mleksio 1:5310eff49dee 288 case 1:
mleksio 0:c58229885f95 289 AppDataSize = 2;
mleksio 0:c58229885f95 290 AppData[0] = ComplianceTestDownLinkCounter >> 8;
mleksio 0:c58229885f95 291 AppData[1] = ComplianceTestDownLinkCounter;
mleksio 1:5310eff49dee 292 break;
mleksio 0:c58229885f95 293 }
mleksio 0:c58229885f95 294 }
mleksio 0:c58229885f95 295 default:
mleksio 0:c58229885f95 296 break;
mleksio 0:c58229885f95 297 }
mleksio 0:c58229885f95 298 }
mleksio 0:c58229885f95 299
mleksio 0:c58229885f95 300 static void ProcessRxFrame( LoRaMacEventFlags_t *flags, LoRaMacEventInfo_t *info )
mleksio 0:c58229885f95 301 {
mleksio 0:c58229885f95 302 switch( info->RxPort ) // Check Rx port number
mleksio 0:c58229885f95 303 {
mleksio 0:c58229885f95 304 case 1: // The application LED can be controlled on port 1 or 2
mleksio 0:c58229885f95 305 case 2:
mleksio 0:c58229885f95 306 if( info->RxBufferSize == 1 )
mleksio 0:c58229885f95 307 {
mleksio 0:c58229885f95 308 AppLedStateOn = info->RxBuffer[0] & 0x01;
mleksio 0:c58229885f95 309 //Led3StateChanged = true;
mleksio 0:c58229885f95 310 }
mleksio 0:c58229885f95 311 break;
mleksio 0:c58229885f95 312 case 224:
mleksio 0:c58229885f95 313 if( ComplianceTestOn == false )
mleksio 0:c58229885f95 314 {
mleksio 0:c58229885f95 315 // Check compliance test enable command (i)
mleksio 0:c58229885f95 316 if( ( info->RxBufferSize == 4 ) &&
mleksio 0:c58229885f95 317 ( info->RxBuffer[0] == 0x01 ) &&
mleksio 0:c58229885f95 318 ( info->RxBuffer[1] == 0x01 ) &&
mleksio 0:c58229885f95 319 ( info->RxBuffer[2] == 0x01 ) &&
mleksio 0:c58229885f95 320 ( info->RxBuffer[3] == 0x01 ) )
mleksio 0:c58229885f95 321 {
mleksio 0:c58229885f95 322 IsTxConfirmed = false;
mleksio 0:c58229885f95 323 AppPort = 224;
mleksio 0:c58229885f95 324 AppDataSize = 2;
mleksio 0:c58229885f95 325 ComplianceTestDownLinkCounter = 0;
mleksio 0:c58229885f95 326 ComplianceTestLinkCheck = false;
mleksio 0:c58229885f95 327 ComplianceTestDemodMargin = 0;
mleksio 0:c58229885f95 328 ComplianceTestNbGateways = 0;
mleksio 0:c58229885f95 329 ComplianceTestOn = true;
mleksio 0:c58229885f95 330 ComplianceTestState = 1;
mleksio 0:c58229885f95 331
mleksio 0:c58229885f95 332 LoRaMacSetAdrOn( true );
mleksio 0:c58229885f95 333 LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_OFF );
mleksio 0:c58229885f95 334 }
mleksio 0:c58229885f95 335 }
mleksio 0:c58229885f95 336 else
mleksio 0:c58229885f95 337 {
mleksio 0:c58229885f95 338 ComplianceTestState = info->RxBuffer[0];
mleksio 0:c58229885f95 339 switch( ComplianceTestState )
mleksio 0:c58229885f95 340 {
mleksio 0:c58229885f95 341 case 0: // Check compliance test disable command (ii)
mleksio 0:c58229885f95 342 IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON;
mleksio 0:c58229885f95 343 AppPort = LORAWAN_APP_PORT;
mleksio 0:c58229885f95 344 AppDataSize = LORAWAN_APP_DATA_SIZE;
mleksio 0:c58229885f95 345 ComplianceTestDownLinkCounter = 0;
mleksio 0:c58229885f95 346 ComplianceTestOn = false;
mleksio 0:c58229885f95 347
mleksio 0:c58229885f95 348 LoRaMacSetAdrOn( LORAWAN_ADR_ON );
mleksio 0:c58229885f95 349 LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON );
mleksio 0:c58229885f95 350 break;
mleksio 0:c58229885f95 351 case 1: // (iii, iv)
mleksio 0:c58229885f95 352 AppDataSize = 2;
mleksio 0:c58229885f95 353 break;
mleksio 0:c58229885f95 354 case 2: // Enable confirmed messages (v)
mleksio 0:c58229885f95 355 IsTxConfirmed = true;
mleksio 0:c58229885f95 356 ComplianceTestState = 1;
mleksio 0:c58229885f95 357 break;
mleksio 0:c58229885f95 358 case 3: // Disable confirmed messages (vi)
mleksio 0:c58229885f95 359 IsTxConfirmed = false;
mleksio 0:c58229885f95 360 ComplianceTestState = 1;
mleksio 0:c58229885f95 361 break;
mleksio 0:c58229885f95 362 case 4: // (vii)
mleksio 0:c58229885f95 363 AppDataSize = info->RxBufferSize;
mleksio 0:c58229885f95 364 printf("VII <------------------- rozmiar %i \r\n", AppDataSize);
mleksio 0:c58229885f95 365 AppData[0] = 4;
mleksio 0:c58229885f95 366 for( uint8_t i = 1; i < AppDataSize; i++ )
mleksio 0:c58229885f95 367 {
mleksio 0:c58229885f95 368
mleksio 0:c58229885f95 369 printf("VII <-------------------dodaje \r\n");
mleksio 0:c58229885f95 370 AppData[i] = info->RxBuffer[i] + 1;
mleksio 0:c58229885f95 371 }
mleksio 0:c58229885f95 372 break;
mleksio 0:c58229885f95 373 case 5: // (viii)
mleksio 0:c58229885f95 374 LoRaMacLinkCheckReq( );
mleksio 0:c58229885f95 375 break;
mleksio 0:c58229885f95 376 default:
mleksio 0:c58229885f95 377 break;
mleksio 0:c58229885f95 378 }
mleksio 0:c58229885f95 379 }
mleksio 0:c58229885f95 380 break;
mleksio 0:c58229885f95 381 default:
mleksio 0:c58229885f95 382 break;
mleksio 0:c58229885f95 383 }
mleksio 0:c58229885f95 384 #warning needed extension to check counter scopes
mleksio 0:c58229885f95 385 }
mleksio 0:c58229885f95 386
mleksio 0:c58229885f95 387 static bool SendFrame( void )
mleksio 0:c58229885f95 388 {
mleksio 0:c58229885f95 389 uint8_t sendFrameStatus = 0;
mleksio 0:c58229885f95 390
mleksio 0:c58229885f95 391 if( IsTxConfirmed == false )
mleksio 0:c58229885f95 392 {
mleksio 0:c58229885f95 393 sendFrameStatus = LoRaMacSendFrame( AppPort, AppData, AppDataSize );
mleksio 0:c58229885f95 394 }
mleksio 0:c58229885f95 395 else
mleksio 0:c58229885f95 396 {
mleksio 0:c58229885f95 397 sendFrameStatus = LoRaMacSendConfirmedFrame( AppPort, AppData, AppDataSize, 8 );
mleksio 0:c58229885f95 398 }
mleksio 0:c58229885f95 399
mleksio 0:c58229885f95 400 switch( sendFrameStatus )
mleksio 0:c58229885f95 401 {
mleksio 0:c58229885f95 402 case 5: // NO_FREE_CHANNEL
mleksio 0:c58229885f95 403 // Try again later
mleksio 0:c58229885f95 404 return true;
mleksio 0:c58229885f95 405 default:
mleksio 0:c58229885f95 406 return false;
mleksio 0:c58229885f95 407 }
mleksio 0:c58229885f95 408 }
mleksio 0:c58229885f95 409
mleksio 0:c58229885f95 410 #if( OVER_THE_AIR_ACTIVATION != 0 )
mleksio 0:c58229885f95 411
mleksio 0:c58229885f95 412 /*!
mleksio 0:c58229885f95 413 * \brief Function executed on JoinReq Timeout event
mleksio 0:c58229885f95 414 */
mleksio 0:c58229885f95 415 static void OnJoinReqTimerEvent( void )
mleksio 0:c58229885f95 416 {
mleksio 0:c58229885f95 417 JoinReqTimer.detach();
mleksio 0:c58229885f95 418 TxNextPacket = true;
mleksio 0:c58229885f95 419 }
mleksio 0:c58229885f95 420
mleksio 0:c58229885f95 421 #endif
mleksio 0:c58229885f95 422
mleksio 0:c58229885f95 423 /*!
mleksio 0:c58229885f95 424 * \brief Function executed on TxNextPacket Timeout event
mleksio 0:c58229885f95 425 */
mleksio 0:c58229885f95 426 static void OnTxNextPacketTimerEvent( void )
mleksio 0:c58229885f95 427 {
mleksio 0:c58229885f95 428 //TimerStop( &TxNextPacketTimer );
mleksio 0:c58229885f95 429 //TxNextPacketTimer.stop();
mleksio 0:c58229885f95 430 TxNextPacketTimer.detach();
mleksio 0:c58229885f95 431 TxNextPacket = true;
mleksio 0:c58229885f95 432 }
mleksio 0:c58229885f95 433
mleksio 0:c58229885f95 434 /*!
mleksio 0:c58229885f95 435 * \brief Function executed on Led 1 Timeout event
mleksio 0:c58229885f95 436 */
mleksio 0:c58229885f95 437 /*static void OnLed1TimerEvent( void )
mleksio 0:c58229885f95 438 {
mleksio 0:c58229885f95 439 TimerStop( &Led1Timer );
mleksio 0:c58229885f95 440 Led1StateChanged = true;
mleksio 0:c58229885f95 441 }*/
mleksio 0:c58229885f95 442
mleksio 0:c58229885f95 443 /*!
mleksio 0:c58229885f95 444 * \brief Function executed on Led 2 Timeout event
mleksio 0:c58229885f95 445 */
mleksio 0:c58229885f95 446 /*static void OnLed2TimerEvent( void )
mleksio 0:c58229885f95 447 {
mleksio 0:c58229885f95 448 TimerStop( &Led2Timer );
mleksio 0:c58229885f95 449 Led2StateChanged = true;
mleksio 0:c58229885f95 450 }*/
mleksio 0:c58229885f95 451
mleksio 0:c58229885f95 452 /*!
mleksio 0:c58229885f95 453 * \brief Function executed on Led 4 Timeout event
mleksio 0:c58229885f95 454 */
mleksio 0:c58229885f95 455 /*void OnLed4TimerEvent( void )
mleksio 0:c58229885f95 456 {
mleksio 0:c58229885f95 457 TimerStop( &Led4Timer );
mleksio 0:c58229885f95 458 Led4StateChanged = true;
mleksio 0:c58229885f95 459 }*/
mleksio 0:c58229885f95 460
mleksio 0:c58229885f95 461 /*!
mleksio 0:c58229885f95 462 * \brief Function to be executed on MAC layer event
mleksio 0:c58229885f95 463 */
mleksio 0:c58229885f95 464 static void OnMacEvent( LoRaMacEventFlags_t *flags, LoRaMacEventInfo_t *info )
mleksio 0:c58229885f95 465 {
mleksio 0:c58229885f95 466 if( flags->Bits.JoinAccept == 1 )
mleksio 0:c58229885f95 467 {
mleksio 0:c58229885f95 468 #if( OVER_THE_AIR_ACTIVATION != 0 )
mleksio 0:c58229885f95 469 JoinReqTimer.detach();
mleksio 0:c58229885f95 470 #endif
mleksio 0:c58229885f95 471 IsNetworkJoined = true;
mleksio 0:c58229885f95 472 }
mleksio 0:c58229885f95 473 else
mleksio 0:c58229885f95 474 {
mleksio 0:c58229885f95 475 if( flags->Bits.Tx == 1 )
mleksio 0:c58229885f95 476 {
mleksio 0:c58229885f95 477 }
mleksio 0:c58229885f95 478
mleksio 0:c58229885f95 479 if( flags->Bits.Rx == 1 )
mleksio 0:c58229885f95 480 {
mleksio 0:c58229885f95 481 if( ComplianceTestOn == true )
mleksio 0:c58229885f95 482 {
mleksio 0:c58229885f95 483 ComplianceTestDownLinkCounter++;
mleksio 1:5310eff49dee 484 if( flags->Bits.LinkCheck == 1 )
mleksio 1:5310eff49dee 485 {
mleksio 1:5310eff49dee 486 ComplianceTestLinkCheck = true;
mleksio 1:5310eff49dee 487 ComplianceTestDemodMargin = info->DemodMargin;
mleksio 1:5310eff49dee 488 ComplianceTestNbGateways = info->NbGateways;
mleksio 1:5310eff49dee 489 }
mleksio 0:c58229885f95 490 }
mleksio 0:c58229885f95 491 if( flags->Bits.RxData == true )
mleksio 0:c58229885f95 492 {
mleksio 0:c58229885f95 493 ProcessRxFrame( flags, info );
mleksio 0:c58229885f95 494 }
mleksio 0:c58229885f95 495
mleksio 0:c58229885f95 496 DownlinkStatusUpdate = true;
mleksio 0:c58229885f95 497 //TimerStart( &Led2Timer );
mleksio 0:c58229885f95 498 }
mleksio 0:c58229885f95 499 }
mleksio 0:c58229885f95 500 // Schedule a new transmission
mleksio 0:c58229885f95 501 ScheduleNextTx = true;
mleksio 0:c58229885f95 502 }
mleksio 0:c58229885f95 503
mleksio 0:c58229885f95 504 /**
mleksio 0:c58229885f95 505 * Main application entry point.
mleksio 0:c58229885f95 506 */
mleksio 0:c58229885f95 507
mleksio 0:c58229885f95 508 int main( void )
mleksio 0:c58229885f95 509 {
mleksio 1:5310eff49dee 510 DigitalOut myled1(LED1);
mleksio 1:5310eff49dee 511 DigitalOut myled2(LED2);
mleksio 1:5310eff49dee 512
mleksio 1:5310eff49dee 513 myled1 = 1;
mleksio 1:5310eff49dee 514 myled2 = 1;
mleksio 0:c58229885f95 515 #if( OVER_THE_AIR_ACTIVATION != 0 )
mleksio 0:c58229885f95 516 uint8_t sendFrameStatus = 0;
mleksio 0:c58229885f95 517 #endif
mleksio 0:c58229885f95 518 bool trySendingFrameAgain = false;
mleksio 0:c58229885f95 519
mleksio 0:c58229885f95 520 #warning HW and RADIO CHIP must be configured!!!!
mleksio 0:c58229885f95 521 //BoardInitMcu( );
mleksio 0:c58229885f95 522 //BoardInitPeriph( );
mleksio 0:c58229885f95 523 //hal_init();
mleksio 0:c58229885f95 524 //radio_init();
mleksio 0:c58229885f95 525
mleksio 0:c58229885f95 526 LoRaMacCallbacks.MacEvent = OnMacEvent;
mleksio 0:c58229885f95 527 //LoRaMacCallbacks.GetBatteryLevel = BoardGetBatteryLevel;
mleksio 0:c58229885f95 528 LoRaMacInit( &LoRaMacCallbacks );
mleksio 0:c58229885f95 529
mleksio 0:c58229885f95 530 IsNetworkJoined = false;
mleksio 0:c58229885f95 531
mleksio 0:c58229885f95 532 #if( OVER_THE_AIR_ACTIVATION == 0 )
mleksio 0:c58229885f95 533 // Random seed initialization
mleksio 0:c58229885f95 534 #warning seed initialization must be implemented
mleksio 0:c58229885f95 535 srand1( 1 );
mleksio 0:c58229885f95 536 // Choose a random device address based on Board unique ID
mleksio 0:c58229885f95 537 // NwkAddr rand [0, 33554431]
mleksio 0:c58229885f95 538 DevAddr = randr( 0, 0x01FFFFFF );
mleksio 0:c58229885f95 539
mleksio 0:c58229885f95 540 LoRaMacInitNwkIds( LORAWAN_NETWORK_ID, DevAddr, NwkSKey, AppSKey );
mleksio 0:c58229885f95 541 IsNetworkJoined = true;
mleksio 0:c58229885f95 542 #else
mleksio 0:c58229885f95 543 // Initialize LoRaMac device unique ID
mleksio 0:c58229885f95 544 //BoardGetUniqueId( DevEui );
mleksio 0:c58229885f95 545 #endif
mleksio 0:c58229885f95 546
mleksio 0:c58229885f95 547 TxNextPacket = true;
mleksio 0:c58229885f95 548
mleksio 0:c58229885f95 549 //TimerInit( &Led1Timer, OnLed1TimerEvent );
mleksio 0:c58229885f95 550 //TimerSetValue( &Led1Timer, 25000 );
mleksio 0:c58229885f95 551
mleksio 0:c58229885f95 552 //TimerInit( &Led2Timer, OnLed2TimerEvent );
mleksio 0:c58229885f95 553 //TimerSetValue( &Led2Timer, 25000 );
mleksio 0:c58229885f95 554
mleksio 0:c58229885f95 555 //TimerInit( &Led4Timer, OnLed4TimerEvent );
mleksio 0:c58229885f95 556 //TimerSetValue( &Led4Timer, 25000 );
mleksio 0:c58229885f95 557
mleksio 0:c58229885f95 558 LoRaMacSetAdrOn( LORAWAN_ADR_ON );
mleksio 0:c58229885f95 559 #warning dutycycleON OFFFFFFFFFFFFF!!!!!!!!!!!!!!!!!!!!!!!
mleksio 0:c58229885f95 560 // LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON );
mleksio 0:c58229885f95 561 LoRaMacTestSetDutyCycleOn( 0 );
mleksio 0:c58229885f95 562 LoRaMacSetPublicNetwork( LORAWAN_PUBLIC_NETWORK );
mleksio 0:c58229885f95 563
mleksio 1:5310eff49dee 564 LoRaMacSetRx2Channel( ( Rx2ChannelParams_t ){ 869525000, DR_3 } );
mleksio 0:c58229885f95 565 while( 1 )
mleksio 0:c58229885f95 566 {
mleksio 0:c58229885f95 567 while( IsNetworkJoined == false )
mleksio 0:c58229885f95 568 {
mleksio 0:c58229885f95 569 #if( OVER_THE_AIR_ACTIVATION != 0 )
mleksio 0:c58229885f95 570 if( TxNextPacket == true )
mleksio 0:c58229885f95 571 {
mleksio 0:c58229885f95 572 printf("Trial to join network... txnextpacket %i \r\n", TxNextPacket);
mleksio 0:c58229885f95 573 TxNextPacket = false;
mleksio 0:c58229885f95 574
mleksio 0:c58229885f95 575 sendFrameStatus = LoRaMacJoinReq( DevEui, AppEui, AppKey );
mleksio 0:c58229885f95 576 printf("status = %i\r\n", sendFrameStatus);
mleksio 0:c58229885f95 577 switch( sendFrameStatus )
mleksio 0:c58229885f95 578 {
mleksio 0:c58229885f95 579 case 1: // BUSY
mleksio 0:c58229885f95 580 printf("busy...\r\n");
mleksio 0:c58229885f95 581 break;
mleksio 0:c58229885f95 582 case 0: // OK
mleksio 0:c58229885f95 583 case 2: // NO_NETWORK_JOINED
mleksio 0:c58229885f95 584 case 3: // LENGTH_PORT_ERROR
mleksio 0:c58229885f95 585 case 4: // MAC_CMD_ERROR
mleksio 0:c58229885f95 586 case 6: // DEVICE_OFF
mleksio 0:c58229885f95 587 default:
mleksio 0:c58229885f95 588 // Relaunch timer for next trial
mleksio 0:c58229885f95 589 JoinReqTimer.attach_us(OnJoinReqTimerEvent, OVER_THE_AIR_ACTIVATION_DUTYCYCLE);
mleksio 0:c58229885f95 590 break;
mleksio 0:c58229885f95 591 }
mleksio 0:c58229885f95 592 }
mleksio 0:c58229885f95 593 //TimerLowPowerHandler( );
mleksio 0:c58229885f95 594 #endif
mleksio 0:c58229885f95 595 }
mleksio 0:c58229885f95 596
mleksio 0:c58229885f95 597 /*
mleksio 0:c58229885f95 598 if( Led1StateChanged == true )
mleksio 0:c58229885f95 599 {
mleksio 0:c58229885f95 600 Led1StateChanged = false;
mleksio 0:c58229885f95 601 // Switch LED 1 OFF
mleksio 0:c58229885f95 602 GpioWrite( &Led1, 1 );
mleksio 0:c58229885f95 603 }
mleksio 0:c58229885f95 604 if( Led2StateChanged == true )
mleksio 0:c58229885f95 605 {
mleksio 0:c58229885f95 606 Led2StateChanged = false;
mleksio 0:c58229885f95 607 // Switch LED 2 OFF
mleksio 0:c58229885f95 608 GpioWrite( &Led2, 1 );
mleksio 0:c58229885f95 609 }
mleksio 0:c58229885f95 610 if( Led3StateChanged == true )
mleksio 0:c58229885f95 611 {
mleksio 0:c58229885f95 612 Led3StateChanged = false;
mleksio 0:c58229885f95 613 GpioWrite( &Led3, ( ( AppLedStateOn & 0x01 ) != 0 ) ? 0 : 1 );
mleksio 0:c58229885f95 614 }
mleksio 0:c58229885f95 615 if( Led4StateChanged == true )
mleksio 0:c58229885f95 616 {
mleksio 0:c58229885f95 617 Led4StateChanged = false;
mleksio 0:c58229885f95 618 // Switch LED 4 OFF
mleksio 0:c58229885f95 619 GpioWrite( &Led4, 1 );
mleksio 0:c58229885f95 620 }*/
mleksio 0:c58229885f95 621 if( DownlinkStatusUpdate == true )
mleksio 0:c58229885f95 622 {
mleksio 0:c58229885f95 623 DownlinkStatusUpdate = false;
mleksio 0:c58229885f95 624 // Switch LED 2 ON for each received downlink
mleksio 0:c58229885f95 625 //GpioWrite( &Led2, 0 );
mleksio 0:c58229885f95 626 }
mleksio 0:c58229885f95 627
mleksio 0:c58229885f95 628 if( ScheduleNextTx == true )
mleksio 0:c58229885f95 629 {
mleksio 0:c58229885f95 630 printf("Sending data...");
mleksio 0:c58229885f95 631 ScheduleNextTx = false;
mleksio 0:c58229885f95 632
mleksio 1:5310eff49dee 633 if( ComplianceTestOn == true )
mleksio 1:5310eff49dee 634 {
mleksio 1:5310eff49dee 635 TxNextPacket = true;
mleksio 1:5310eff49dee 636 }
mleksio 1:5310eff49dee 637 else
mleksio 1:5310eff49dee 638 {
mleksio 0:c58229885f95 639 // Schedule next packet transmission
mleksio 0:c58229885f95 640 TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND );
mleksio 0:c58229885f95 641 TxNextPacketTimer.attach_us(OnTxNextPacketTimerEvent, TxDutyCycleTime);
mleksio 1:5310eff49dee 642 }
mleksio 0:c58229885f95 643 }
mleksio 0:c58229885f95 644
mleksio 0:c58229885f95 645 if( trySendingFrameAgain == true )
mleksio 0:c58229885f95 646 {
mleksio 0:c58229885f95 647 trySendingFrameAgain = SendFrame( );
mleksio 0:c58229885f95 648 }
mleksio 0:c58229885f95 649 if( TxNextPacket == true )
mleksio 0:c58229885f95 650 {
mleksio 0:c58229885f95 651 TxNextPacket = false;
mleksio 0:c58229885f95 652
mleksio 0:c58229885f95 653 PrepareTxFrame( AppPort );
mleksio 0:c58229885f95 654
mleksio 0:c58229885f95 655 // Switch LED 1 ON
mleksio 0:c58229885f95 656 //GpioWrite( &Led1, 0 );
mleksio 0:c58229885f95 657 //TimerStart( &Led1Timer );
mleksio 0:c58229885f95 658
mleksio 0:c58229885f95 659 trySendingFrameAgain = SendFrame( );
mleksio 0:c58229885f95 660 }
mleksio 0:c58229885f95 661
mleksio 0:c58229885f95 662 //TimerLowPowerHandler( );
mleksio 0:c58229885f95 663 }
mleksio 0:c58229885f95 664 }