Use LoRaWAN on mDot with IKS01A1 sensor board plugged into UDK board.

Dependencies:   X_NUCLEO_IKS01A1 mbed LoRaWAN-lib SX1272Lib

Fork of LoRaWAN-demo-72 by Semtech

This project uses IKS01A1 driver library with pin definitions for mDot UDK arduino headers.
The mDot platform doesnt define arduino header pins, so they must be defined in the IKS01A1 driver library header.

Committer:
dudmuck
Date:
Wed Aug 17 23:56:43 2016 +0000
Revision:
6:72ea69843556
Parent:
5:62862ef9480b
use LoRaWAN on mDot with IKS01A1 sensor board

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mluis 0:45496a70a8a5 1 /*
mluis 0:45496a70a8a5 2 / _____) _ | |
mluis 0:45496a70a8a5 3 ( (____ _____ ____ _| |_ _____ ____| |__
mluis 0:45496a70a8a5 4 \____ \| ___ | (_ _) ___ |/ ___) _ \
mluis 0:45496a70a8a5 5 _____) ) ____| | | || |_| ____( (___| | | |
mluis 0:45496a70a8a5 6 (______/|_____)_|_|_| \__)_____)\____)_| |_|
mluis 0:45496a70a8a5 7 (C)2015 Semtech
mluis 0:45496a70a8a5 8
mluis 0:45496a70a8a5 9 Description: LoRaMac classA device implementation
mluis 0:45496a70a8a5 10
mluis 0:45496a70a8a5 11 License: Revised BSD License, see LICENSE.TXT file include in the project
mluis 0:45496a70a8a5 12
mluis 0:45496a70a8a5 13 Maintainer: Miguel Luis and Gregory Cristian
mluis 0:45496a70a8a5 14 */
mluis 0:45496a70a8a5 15 #include "mbed.h"
mluis 0:45496a70a8a5 16 #include "board.h"
mluis 0:45496a70a8a5 17 #include "radio.h"
mluis 0:45496a70a8a5 18
mluis 0:45496a70a8a5 19 #include "LoRaMac.h"
mluis 0:45496a70a8a5 20 #include "Comissioning.h"
mluis 0:45496a70a8a5 21 #include "SerialDisplay.h"
dudmuck 6:72ea69843556 22 #include "x_nucleo_iks01a1.h"
dudmuck 6:72ea69843556 23 #include "vt100.h"
dudmuck 6:72ea69843556 24 extern VT100 vt;
mluis 0:45496a70a8a5 25
mluis 0:45496a70a8a5 26 /*!
mluis 0:45496a70a8a5 27 * Defines the application data transmission duty cycle. 5s, value in [us].
mluis 0:45496a70a8a5 28 */
dudmuck 6:72ea69843556 29 #define APP_TX_DUTYCYCLE 15000000
mluis 0:45496a70a8a5 30
mluis 0:45496a70a8a5 31 /*!
mluis 0:45496a70a8a5 32 * Defines a random delay for application data transmission duty cycle. 1s,
mluis 0:45496a70a8a5 33 * value in [us].
mluis 0:45496a70a8a5 34 */
dudmuck 6:72ea69843556 35 #define APP_TX_DUTYCYCLE_RND 3000000
mluis 0:45496a70a8a5 36
mluis 0:45496a70a8a5 37 /*!
mluis 3:3152aa75c58d 38 * Default datarate
mluis 0:45496a70a8a5 39 */
mluis 0:45496a70a8a5 40 #define LORAWAN_DEFAULT_DATARATE DR_0
mluis 0:45496a70a8a5 41
mluis 0:45496a70a8a5 42 /*!
mluis 0:45496a70a8a5 43 * LoRaWAN confirmed messages
mluis 0:45496a70a8a5 44 */
mluis 0:45496a70a8a5 45 #define LORAWAN_CONFIRMED_MSG_ON true
mluis 0:45496a70a8a5 46
mluis 0:45496a70a8a5 47 /*!
mluis 0:45496a70a8a5 48 * LoRaWAN Adaptive Data Rate
mluis 0:45496a70a8a5 49 *
mluis 0:45496a70a8a5 50 * \remark Please note that when ADR is enabled the end-device should be static
mluis 0:45496a70a8a5 51 */
mluis 0:45496a70a8a5 52 #define LORAWAN_ADR_ON 1
mluis 0:45496a70a8a5 53
mluis 0:45496a70a8a5 54 #if defined( USE_BAND_868 )
mluis 0:45496a70a8a5 55
mluis 0:45496a70a8a5 56 #include "LoRaMacTest.h"
mluis 0:45496a70a8a5 57
mluis 0:45496a70a8a5 58 /*!
mluis 0:45496a70a8a5 59 * LoRaWAN ETSI duty cycle control enable/disable
mluis 0:45496a70a8a5 60 *
mluis 0:45496a70a8a5 61 * \remark Please note that ETSI mandates duty cycled transmissions. Use only for test purposes
mluis 0:45496a70a8a5 62 */
mluis 0:45496a70a8a5 63 #define LORAWAN_DUTYCYCLE_ON true
mluis 0:45496a70a8a5 64
mluis 3:3152aa75c58d 65 #define USE_SEMTECH_DEFAULT_CHANNEL_LINEUP 1
mluis 3:3152aa75c58d 66
mluis 3:3152aa75c58d 67 #if( USE_SEMTECH_DEFAULT_CHANNEL_LINEUP == 1 )
mluis 3:3152aa75c58d 68
mluis 3:3152aa75c58d 69 #define LC4 { 867100000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 3:3152aa75c58d 70 #define LC5 { 867300000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 3:3152aa75c58d 71 #define LC6 { 867500000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 3:3152aa75c58d 72 #define LC7 { 867700000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 3:3152aa75c58d 73 #define LC8 { 867900000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 3:3152aa75c58d 74 #define LC9 { 868800000, { ( ( DR_7 << 4 ) | DR_7 ) }, 2 }
mluis 3:3152aa75c58d 75 #define LC10 { 868300000, { ( ( DR_6 << 4 ) | DR_6 ) }, 1 }
mluis 3:3152aa75c58d 76
mluis 3:3152aa75c58d 77 #endif
mluis 3:3152aa75c58d 78
mluis 0:45496a70a8a5 79 #endif
mluis 0:45496a70a8a5 80
mluis 0:45496a70a8a5 81 /*!
mluis 0:45496a70a8a5 82 * LoRaWAN application port
mluis 0:45496a70a8a5 83 */
dudmuck 6:72ea69843556 84 #define LORAWAN_APP_PORT 5 //15
mluis 0:45496a70a8a5 85
mluis 0:45496a70a8a5 86 /*!
mluis 0:45496a70a8a5 87 * User application data buffer size
mluis 0:45496a70a8a5 88 */
mluis 0:45496a70a8a5 89 #if ( LORAWAN_CONFIRMED_MSG_ON == 1 )
mluis 0:45496a70a8a5 90 #define LORAWAN_APP_DATA_SIZE 6
mluis 0:45496a70a8a5 91
mluis 0:45496a70a8a5 92 #else
mluis 0:45496a70a8a5 93 #define LORAWAN_APP_DATA_SIZE 1
mluis 0:45496a70a8a5 94
mluis 0:45496a70a8a5 95 #endif
mluis 0:45496a70a8a5 96
mluis 0:45496a70a8a5 97 static uint8_t DevEui[] = LORAWAN_DEVICE_EUI;
mluis 0:45496a70a8a5 98 static uint8_t AppEui[] = LORAWAN_APPLICATION_EUI;
mluis 0:45496a70a8a5 99 static uint8_t AppKey[] = LORAWAN_APPLICATION_KEY;
mluis 0:45496a70a8a5 100
mluis 5:62862ef9480b 101 #if( OVER_THE_AIR_ACTIVATION == 0 )
mluis 0:45496a70a8a5 102
mluis 0:45496a70a8a5 103 static uint8_t NwkSKey[] = LORAWAN_NWKSKEY;
mluis 0:45496a70a8a5 104 static uint8_t AppSKey[] = LORAWAN_APPSKEY;
mluis 0:45496a70a8a5 105
mluis 0:45496a70a8a5 106 /*!
mluis 0:45496a70a8a5 107 * Device address
mluis 0:45496a70a8a5 108 */
mluis 0:45496a70a8a5 109 static uint32_t DevAddr = LORAWAN_DEVICE_ADDRESS;
mluis 0:45496a70a8a5 110
mluis 0:45496a70a8a5 111 #endif
mluis 0:45496a70a8a5 112
mluis 0:45496a70a8a5 113 /*!
mluis 0:45496a70a8a5 114 * Application port
mluis 0:45496a70a8a5 115 */
mluis 0:45496a70a8a5 116 static uint8_t AppPort = LORAWAN_APP_PORT;
mluis 0:45496a70a8a5 117
mluis 0:45496a70a8a5 118 /*!
mluis 0:45496a70a8a5 119 * User application data size
mluis 0:45496a70a8a5 120 */
mluis 0:45496a70a8a5 121 static uint8_t AppDataSize = LORAWAN_APP_DATA_SIZE;
mluis 0:45496a70a8a5 122
mluis 0:45496a70a8a5 123 /*!
mluis 0:45496a70a8a5 124 * User application data buffer size
mluis 0:45496a70a8a5 125 */
mluis 0:45496a70a8a5 126 #define LORAWAN_APP_DATA_MAX_SIZE 64
mluis 0:45496a70a8a5 127
mluis 0:45496a70a8a5 128 /*!
mluis 0:45496a70a8a5 129 * User application data
mluis 0:45496a70a8a5 130 */
mluis 0:45496a70a8a5 131 static uint8_t AppData[LORAWAN_APP_DATA_MAX_SIZE];
mluis 0:45496a70a8a5 132
mluis 0:45496a70a8a5 133 /*!
mluis 0:45496a70a8a5 134 * Indicates if the node is sending confirmed or unconfirmed messages
mluis 0:45496a70a8a5 135 */
mluis 0:45496a70a8a5 136 static uint8_t IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON;
mluis 0:45496a70a8a5 137
mluis 0:45496a70a8a5 138 /*!
mluis 0:45496a70a8a5 139 * Defines the application data transmission duty cycle
mluis 0:45496a70a8a5 140 */
mluis 0:45496a70a8a5 141 static uint32_t TxDutyCycleTime;
mluis 0:45496a70a8a5 142
mluis 0:45496a70a8a5 143 /*!
mluis 0:45496a70a8a5 144 * Timer to handle the application data transmission duty cycle
mluis 0:45496a70a8a5 145 */
mluis 0:45496a70a8a5 146 static TimerEvent_t TxNextPacketTimer;
mluis 0:45496a70a8a5 147
mluis 0:45496a70a8a5 148 /*!
mluis 0:45496a70a8a5 149 * Specifies the state of the application LED
mluis 0:45496a70a8a5 150 */
mluis 0:45496a70a8a5 151 static bool AppLedStateOn = false;
mluis 0:45496a70a8a5 152 volatile bool Led3StateChanged = false;
mluis 0:45496a70a8a5 153 /*!
mluis 0:45496a70a8a5 154 * Timer to handle the state of LED1
mluis 0:45496a70a8a5 155 */
mluis 0:45496a70a8a5 156 static TimerEvent_t Led1Timer;
mluis 0:45496a70a8a5 157 volatile bool Led1State = false;
mluis 0:45496a70a8a5 158 volatile bool Led1StateChanged = false;
mluis 0:45496a70a8a5 159 /*!
mluis 0:45496a70a8a5 160 * Timer to handle the state of LED2
mluis 0:45496a70a8a5 161 */
mluis 0:45496a70a8a5 162 static TimerEvent_t Led2Timer;
mluis 0:45496a70a8a5 163 volatile bool Led2State = false;
mluis 0:45496a70a8a5 164 volatile bool Led2StateChanged = false;
mluis 0:45496a70a8a5 165
mluis 0:45496a70a8a5 166 /*!
mluis 0:45496a70a8a5 167 * Indicates if a new packet can be sent
mluis 0:45496a70a8a5 168 */
mluis 0:45496a70a8a5 169 static bool NextTx = true;
mluis 0:45496a70a8a5 170
mluis 0:45496a70a8a5 171 /*!
mluis 0:45496a70a8a5 172 * Device states
mluis 0:45496a70a8a5 173 */
mluis 0:45496a70a8a5 174 static enum eDevicState
mluis 0:45496a70a8a5 175 {
mluis 0:45496a70a8a5 176 DEVICE_STATE_INIT,
mluis 0:45496a70a8a5 177 DEVICE_STATE_JOIN,
mluis 0:45496a70a8a5 178 DEVICE_STATE_SEND,
mluis 0:45496a70a8a5 179 DEVICE_STATE_CYCLE,
mluis 0:45496a70a8a5 180 DEVICE_STATE_SLEEP
mluis 0:45496a70a8a5 181 }DeviceState;
mluis 0:45496a70a8a5 182
mluis 0:45496a70a8a5 183 /*!
mluis 0:45496a70a8a5 184 * LoRaWAN compliance tests support data
mluis 0:45496a70a8a5 185 */
mluis 0:45496a70a8a5 186 struct ComplianceTest_s
mluis 0:45496a70a8a5 187 {
mluis 0:45496a70a8a5 188 bool Running;
mluis 0:45496a70a8a5 189 uint8_t State;
mluis 0:45496a70a8a5 190 bool IsTxConfirmed;
mluis 0:45496a70a8a5 191 uint8_t AppPort;
mluis 0:45496a70a8a5 192 uint8_t AppDataSize;
mluis 0:45496a70a8a5 193 uint8_t *AppDataBuffer;
mluis 0:45496a70a8a5 194 uint16_t DownLinkCounter;
mluis 0:45496a70a8a5 195 bool LinkCheck;
mluis 0:45496a70a8a5 196 uint8_t DemodMargin;
mluis 0:45496a70a8a5 197 uint8_t NbGateways;
mluis 0:45496a70a8a5 198 }ComplianceTest;
mluis 0:45496a70a8a5 199
mluis 0:45496a70a8a5 200 /*
mluis 0:45496a70a8a5 201 * SerialDisplay managment variables
mluis 0:45496a70a8a5 202 */
mluis 0:45496a70a8a5 203
mluis 0:45496a70a8a5 204 /*!
mluis 0:45496a70a8a5 205 * Indicates if the MAC layer network join status has changed.
mluis 0:45496a70a8a5 206 */
mluis 0:45496a70a8a5 207 static bool IsNetworkJoinedStatusUpdate = false;
mluis 0:45496a70a8a5 208
mluis 0:45496a70a8a5 209 /*!
mluis 0:45496a70a8a5 210 * Strucure containing the Uplink status
mluis 0:45496a70a8a5 211 */
mluis 0:45496a70a8a5 212 struct sLoRaMacUplinkStatus
mluis 0:45496a70a8a5 213 {
mluis 0:45496a70a8a5 214 uint8_t Acked;
mluis 0:45496a70a8a5 215 int8_t Datarate;
mluis 0:45496a70a8a5 216 uint16_t UplinkCounter;
mluis 0:45496a70a8a5 217 uint8_t Port;
mluis 0:45496a70a8a5 218 uint8_t *Buffer;
mluis 0:45496a70a8a5 219 uint8_t BufferSize;
mluis 0:45496a70a8a5 220 }LoRaMacUplinkStatus;
mluis 0:45496a70a8a5 221 volatile bool UplinkStatusUpdated = false;
mluis 0:45496a70a8a5 222
mluis 0:45496a70a8a5 223 /*!
mluis 0:45496a70a8a5 224 * Strucure containing the Downlink status
mluis 0:45496a70a8a5 225 */
mluis 0:45496a70a8a5 226 struct sLoRaMacDownlinkStatus
mluis 0:45496a70a8a5 227 {
mluis 0:45496a70a8a5 228 int16_t Rssi;
mluis 0:45496a70a8a5 229 int8_t Snr;
mluis 0:45496a70a8a5 230 uint16_t DownlinkCounter;
mluis 0:45496a70a8a5 231 bool RxData;
mluis 0:45496a70a8a5 232 uint8_t Port;
mluis 0:45496a70a8a5 233 uint8_t *Buffer;
mluis 0:45496a70a8a5 234 uint8_t BufferSize;
mluis 0:45496a70a8a5 235 }LoRaMacDownlinkStatus;
mluis 0:45496a70a8a5 236 volatile bool DownlinkStatusUpdated = false;
mluis 0:45496a70a8a5 237
mluis 0:45496a70a8a5 238 void SerialDisplayRefresh( void )
mluis 0:45496a70a8a5 239 {
mluis 0:45496a70a8a5 240 MibRequestConfirm_t mibReq;
mluis 0:45496a70a8a5 241
mluis 0:45496a70a8a5 242 SerialDisplayInit( );
mluis 0:45496a70a8a5 243 SerialDisplayUpdateActivationMode( OVER_THE_AIR_ACTIVATION );
mluis 0:45496a70a8a5 244
mluis 0:45496a70a8a5 245 #if( OVER_THE_AIR_ACTIVATION == 0 )
mluis 0:45496a70a8a5 246 SerialDisplayUpdateNwkId( LORAWAN_NETWORK_ID );
mluis 0:45496a70a8a5 247 SerialDisplayUpdateDevAddr( DevAddr );
mluis 0:45496a70a8a5 248 SerialDisplayUpdateKey( 12, NwkSKey );
mluis 0:45496a70a8a5 249 SerialDisplayUpdateKey( 13, AppSKey );
mluis 5:62862ef9480b 250 #endif
mluis 0:45496a70a8a5 251 SerialDisplayUpdateEui( 5, DevEui );
mluis 0:45496a70a8a5 252 SerialDisplayUpdateEui( 6, AppEui );
mluis 0:45496a70a8a5 253 SerialDisplayUpdateKey( 7, AppKey );
mluis 0:45496a70a8a5 254
mluis 0:45496a70a8a5 255 mibReq.Type = MIB_NETWORK_JOINED;
mluis 0:45496a70a8a5 256 LoRaMacMibGetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 257 SerialDisplayUpdateNetworkIsJoined( mibReq.Param.IsNetworkJoined );
mluis 0:45496a70a8a5 258
mluis 0:45496a70a8a5 259 SerialDisplayUpdateAdr( LORAWAN_ADR_ON );
mluis 0:45496a70a8a5 260 #if defined( USE_BAND_868 )
mluis 0:45496a70a8a5 261 SerialDisplayUpdateDutyCycle( LORAWAN_DUTYCYCLE_ON );
mluis 0:45496a70a8a5 262 #else
mluis 0:45496a70a8a5 263 SerialDisplayUpdateDutyCycle( false );
mluis 0:45496a70a8a5 264 #endif
mluis 0:45496a70a8a5 265 SerialDisplayUpdatePublicNetwork( LORAWAN_PUBLIC_NETWORK );
mluis 0:45496a70a8a5 266
mluis 0:45496a70a8a5 267 SerialDisplayUpdateLedState( 3, AppLedStateOn );
mluis 0:45496a70a8a5 268 }
mluis 0:45496a70a8a5 269
mluis 0:45496a70a8a5 270 void SerialRxProcess( void )
mluis 0:45496a70a8a5 271 {
mluis 0:45496a70a8a5 272 if( SerialDisplayReadable( ) == true )
mluis 0:45496a70a8a5 273 {
mluis 0:45496a70a8a5 274 switch( SerialDisplayGetChar( ) )
mluis 0:45496a70a8a5 275 {
mluis 0:45496a70a8a5 276 case 'R':
mluis 0:45496a70a8a5 277 case 'r':
mluis 0:45496a70a8a5 278 // Refresh Serial screen
mluis 0:45496a70a8a5 279 SerialDisplayRefresh( );
mluis 0:45496a70a8a5 280 break;
mluis 0:45496a70a8a5 281 default:
mluis 0:45496a70a8a5 282 break;
mluis 0:45496a70a8a5 283 }
mluis 0:45496a70a8a5 284 }
mluis 0:45496a70a8a5 285 }
mluis 0:45496a70a8a5 286
dudmuck 6:72ea69843556 287 static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance();
dudmuck 6:72ea69843556 288 static TempSensor *temp_sensor1 = mems_expansion_board->ht_sensor;
mluis 0:45496a70a8a5 289 /*!
mluis 0:45496a70a8a5 290 * \brief Prepares the payload of the frame
mluis 0:45496a70a8a5 291 */
mluis 0:45496a70a8a5 292 static void PrepareTxFrame( uint8_t port )
mluis 0:45496a70a8a5 293 {
mluis 0:45496a70a8a5 294 switch( port )
mluis 0:45496a70a8a5 295 {
dudmuck 6:72ea69843556 296 case 5:
dudmuck 6:72ea69843556 297 unsigned int ret;
dudmuck 6:72ea69843556 298 float TEMPERATURE_Value;
dudmuck 6:72ea69843556 299 AppDataSize = 11;
dudmuck 6:72ea69843556 300 AppData[0] = AppLedStateOn;
dudmuck 6:72ea69843556 301 ret |= (!CALL_METH(temp_sensor1, GetTemperature, &TEMPERATURE_Value, 0.0f) ? 0x0 : 0x1);
dudmuck 6:72ea69843556 302 vt.SetCursorPos( 43, 1 );
dudmuck 6:72ea69843556 303 vt.printf( "temp:%f", TEMPERATURE_Value );
dudmuck 6:72ea69843556 304 AppData[1] = (int)TEMPERATURE_Value * 2;
dudmuck 6:72ea69843556 305 break;
mluis 0:45496a70a8a5 306 case 15:
mluis 0:45496a70a8a5 307 {
mluis 0:45496a70a8a5 308 AppData[0] = AppLedStateOn;
mluis 0:45496a70a8a5 309 if( IsTxConfirmed == true )
mluis 0:45496a70a8a5 310 {
mluis 0:45496a70a8a5 311 AppData[1] = LoRaMacDownlinkStatus.DownlinkCounter >> 8;
mluis 0:45496a70a8a5 312 AppData[2] = LoRaMacDownlinkStatus.DownlinkCounter;
mluis 0:45496a70a8a5 313 AppData[3] = LoRaMacDownlinkStatus.Rssi >> 8;
mluis 0:45496a70a8a5 314 AppData[4] = LoRaMacDownlinkStatus.Rssi;
mluis 0:45496a70a8a5 315 AppData[5] = LoRaMacDownlinkStatus.Snr;
mluis 0:45496a70a8a5 316 }
mluis 0:45496a70a8a5 317 }
mluis 0:45496a70a8a5 318 break;
mluis 0:45496a70a8a5 319 case 224:
mluis 0:45496a70a8a5 320 if( ComplianceTest.LinkCheck == true )
mluis 0:45496a70a8a5 321 {
mluis 0:45496a70a8a5 322 ComplianceTest.LinkCheck = false;
mluis 0:45496a70a8a5 323 AppDataSize = 3;
mluis 0:45496a70a8a5 324 AppData[0] = 5;
mluis 0:45496a70a8a5 325 AppData[1] = ComplianceTest.DemodMargin;
mluis 0:45496a70a8a5 326 AppData[2] = ComplianceTest.NbGateways;
mluis 0:45496a70a8a5 327 ComplianceTest.State = 1;
mluis 0:45496a70a8a5 328 }
mluis 0:45496a70a8a5 329 else
mluis 0:45496a70a8a5 330 {
mluis 0:45496a70a8a5 331 switch( ComplianceTest.State )
mluis 0:45496a70a8a5 332 {
mluis 0:45496a70a8a5 333 case 4:
mluis 0:45496a70a8a5 334 ComplianceTest.State = 1;
mluis 0:45496a70a8a5 335 break;
mluis 0:45496a70a8a5 336 case 1:
mluis 0:45496a70a8a5 337 AppDataSize = 2;
mluis 0:45496a70a8a5 338 AppData[0] = ComplianceTest.DownLinkCounter >> 8;
mluis 0:45496a70a8a5 339 AppData[1] = ComplianceTest.DownLinkCounter;
mluis 0:45496a70a8a5 340 break;
mluis 0:45496a70a8a5 341 }
mluis 0:45496a70a8a5 342 }
mluis 0:45496a70a8a5 343 break;
mluis 0:45496a70a8a5 344 default:
mluis 0:45496a70a8a5 345 break;
mluis 0:45496a70a8a5 346 }
mluis 0:45496a70a8a5 347 }
mluis 0:45496a70a8a5 348
mluis 0:45496a70a8a5 349 /*!
mluis 0:45496a70a8a5 350 * \brief Prepares the payload of the frame
mluis 0:45496a70a8a5 351 *
mluis 0:45496a70a8a5 352 * \retval [0: frame could be send, 1: error]
mluis 0:45496a70a8a5 353 */
mluis 0:45496a70a8a5 354 static bool SendFrame( void )
mluis 0:45496a70a8a5 355 {
mluis 0:45496a70a8a5 356 McpsReq_t mcpsReq;
mluis 0:45496a70a8a5 357 LoRaMacTxInfo_t txInfo;
mluis 0:45496a70a8a5 358
mluis 0:45496a70a8a5 359 if( LoRaMacQueryTxPossible( AppDataSize, &txInfo ) != LORAMAC_STATUS_OK )
mluis 0:45496a70a8a5 360 {
mluis 0:45496a70a8a5 361 // Send empty frame in order to flush MAC commands
mluis 0:45496a70a8a5 362 mcpsReq.Type = MCPS_UNCONFIRMED;
mluis 0:45496a70a8a5 363 mcpsReq.Req.Unconfirmed.fBuffer = NULL;
mluis 0:45496a70a8a5 364 mcpsReq.Req.Unconfirmed.fBufferSize = 0;
mluis 0:45496a70a8a5 365 mcpsReq.Req.Unconfirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
mluis 3:3152aa75c58d 366
mluis 0:45496a70a8a5 367 LoRaMacUplinkStatus.Acked = false;
mluis 0:45496a70a8a5 368 LoRaMacUplinkStatus.Port = 0;
mluis 0:45496a70a8a5 369 LoRaMacUplinkStatus.Buffer = NULL;
mluis 0:45496a70a8a5 370 LoRaMacUplinkStatus.BufferSize = 0;
mluis 0:45496a70a8a5 371 SerialDisplayUpdateFrameType( false );
mluis 0:45496a70a8a5 372 }
mluis 0:45496a70a8a5 373 else
mluis 0:45496a70a8a5 374 {
mluis 0:45496a70a8a5 375 LoRaMacUplinkStatus.Acked = false;
mluis 0:45496a70a8a5 376 LoRaMacUplinkStatus.Port = AppPort;
mluis 0:45496a70a8a5 377 LoRaMacUplinkStatus.Buffer = AppData;
mluis 0:45496a70a8a5 378 LoRaMacUplinkStatus.BufferSize = AppDataSize;
mluis 0:45496a70a8a5 379 SerialDisplayUpdateFrameType( IsTxConfirmed );
mluis 0:45496a70a8a5 380
mluis 0:45496a70a8a5 381 if( IsTxConfirmed == false )
mluis 0:45496a70a8a5 382 {
mluis 0:45496a70a8a5 383 mcpsReq.Type = MCPS_UNCONFIRMED;
mluis 0:45496a70a8a5 384 mcpsReq.Req.Unconfirmed.fPort = AppPort;
mluis 0:45496a70a8a5 385 mcpsReq.Req.Unconfirmed.fBuffer = AppData;
mluis 0:45496a70a8a5 386 mcpsReq.Req.Unconfirmed.fBufferSize = AppDataSize;
mluis 0:45496a70a8a5 387 mcpsReq.Req.Unconfirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
mluis 0:45496a70a8a5 388 }
mluis 0:45496a70a8a5 389 else
mluis 0:45496a70a8a5 390 {
mluis 0:45496a70a8a5 391 mcpsReq.Type = MCPS_CONFIRMED;
mluis 0:45496a70a8a5 392 mcpsReq.Req.Confirmed.fPort = AppPort;
mluis 0:45496a70a8a5 393 mcpsReq.Req.Confirmed.fBuffer = AppData;
mluis 0:45496a70a8a5 394 mcpsReq.Req.Confirmed.fBufferSize = AppDataSize;
mluis 2:2eeaeae2c725 395 mcpsReq.Req.Confirmed.NbTrials = 8;
mluis 0:45496a70a8a5 396 mcpsReq.Req.Confirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
mluis 0:45496a70a8a5 397 }
mluis 0:45496a70a8a5 398 }
mluis 0:45496a70a8a5 399
mluis 0:45496a70a8a5 400 if( LoRaMacMcpsRequest( &mcpsReq ) == LORAMAC_STATUS_OK )
mluis 0:45496a70a8a5 401 {
mluis 0:45496a70a8a5 402 return false;
mluis 0:45496a70a8a5 403 }
mluis 0:45496a70a8a5 404 return true;
mluis 0:45496a70a8a5 405 }
mluis 0:45496a70a8a5 406
mluis 0:45496a70a8a5 407 /*!
mluis 0:45496a70a8a5 408 * \brief Function executed on TxNextPacket Timeout event
mluis 0:45496a70a8a5 409 */
mluis 0:45496a70a8a5 410 static void OnTxNextPacketTimerEvent( void )
mluis 0:45496a70a8a5 411 {
mluis 0:45496a70a8a5 412 MibRequestConfirm_t mibReq;
mluis 0:45496a70a8a5 413 LoRaMacStatus_t status;
mluis 0:45496a70a8a5 414
mluis 0:45496a70a8a5 415 TimerStop( &TxNextPacketTimer );
mluis 0:45496a70a8a5 416
mluis 0:45496a70a8a5 417 mibReq.Type = MIB_NETWORK_JOINED;
mluis 0:45496a70a8a5 418 status = LoRaMacMibGetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 419
mluis 0:45496a70a8a5 420 if( status == LORAMAC_STATUS_OK )
mluis 0:45496a70a8a5 421 {
mluis 0:45496a70a8a5 422 if( mibReq.Param.IsNetworkJoined == true )
mluis 0:45496a70a8a5 423 {
mluis 0:45496a70a8a5 424 DeviceState = DEVICE_STATE_SEND;
mluis 0:45496a70a8a5 425 NextTx = true;
mluis 0:45496a70a8a5 426 }
mluis 0:45496a70a8a5 427 else
mluis 0:45496a70a8a5 428 {
mluis 0:45496a70a8a5 429 DeviceState = DEVICE_STATE_JOIN;
mluis 0:45496a70a8a5 430 }
mluis 0:45496a70a8a5 431 }
mluis 0:45496a70a8a5 432 }
mluis 0:45496a70a8a5 433
mluis 0:45496a70a8a5 434 /*!
mluis 0:45496a70a8a5 435 * \brief Function executed on Led 1 Timeout event
mluis 0:45496a70a8a5 436 */
mluis 0:45496a70a8a5 437 static void OnLed1TimerEvent( void )
mluis 0:45496a70a8a5 438 {
mluis 0:45496a70a8a5 439 TimerStop( &Led1Timer );
mluis 0:45496a70a8a5 440 // Switch LED 1 OFF
mluis 0:45496a70a8a5 441 Led1State = false;
mluis 0:45496a70a8a5 442 Led1StateChanged = true;
mluis 0:45496a70a8a5 443 }
mluis 0:45496a70a8a5 444
mluis 0:45496a70a8a5 445 /*!
mluis 0:45496a70a8a5 446 * \brief Function executed on Led 2 Timeout event
mluis 0:45496a70a8a5 447 */
mluis 0:45496a70a8a5 448 static void OnLed2TimerEvent( void )
mluis 0:45496a70a8a5 449 {
mluis 0:45496a70a8a5 450 TimerStop( &Led2Timer );
mluis 0:45496a70a8a5 451 // Switch LED 2 OFF
mluis 0:45496a70a8a5 452 Led2State = false;
mluis 0:45496a70a8a5 453 Led2StateChanged = true;
mluis 0:45496a70a8a5 454 }
mluis 0:45496a70a8a5 455
mluis 0:45496a70a8a5 456 /*!
mluis 0:45496a70a8a5 457 * \brief MCPS-Confirm event function
mluis 0:45496a70a8a5 458 *
mluis 3:3152aa75c58d 459 * \param [IN] mcpsConfirm - Pointer to the confirm structure,
mluis 0:45496a70a8a5 460 * containing confirm attributes.
mluis 0:45496a70a8a5 461 */
mluis 3:3152aa75c58d 462 static void McpsConfirm( McpsConfirm_t *mcpsConfirm )
mluis 0:45496a70a8a5 463 {
mluis 3:3152aa75c58d 464 if( mcpsConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
mluis 0:45496a70a8a5 465 {
mluis 3:3152aa75c58d 466 switch( mcpsConfirm->McpsRequest )
mluis 0:45496a70a8a5 467 {
mluis 0:45496a70a8a5 468 case MCPS_UNCONFIRMED:
mluis 0:45496a70a8a5 469 {
mluis 0:45496a70a8a5 470 // Check Datarate
mluis 0:45496a70a8a5 471 // Check TxPower
mluis 0:45496a70a8a5 472 break;
mluis 0:45496a70a8a5 473 }
mluis 0:45496a70a8a5 474 case MCPS_CONFIRMED:
mluis 0:45496a70a8a5 475 {
mluis 0:45496a70a8a5 476 // Check Datarate
mluis 0:45496a70a8a5 477 // Check TxPower
mluis 0:45496a70a8a5 478 // Check AckReceived
mluis 3:3152aa75c58d 479 // Check NbTrials
mluis 3:3152aa75c58d 480 LoRaMacUplinkStatus.Acked = mcpsConfirm->AckReceived;
mluis 0:45496a70a8a5 481 break;
mluis 0:45496a70a8a5 482 }
mluis 0:45496a70a8a5 483 case MCPS_PROPRIETARY:
mluis 0:45496a70a8a5 484 {
mluis 0:45496a70a8a5 485 break;
mluis 0:45496a70a8a5 486 }
mluis 0:45496a70a8a5 487 default:
mluis 0:45496a70a8a5 488 break;
mluis 0:45496a70a8a5 489 }
mluis 3:3152aa75c58d 490 LoRaMacUplinkStatus.Datarate = mcpsConfirm->Datarate;
mluis 3:3152aa75c58d 491 LoRaMacUplinkStatus.UplinkCounter = mcpsConfirm->UpLinkCounter;
mluis 3:3152aa75c58d 492
mluis 5:62862ef9480b 493 // Switch LED 1 ON
mluis 5:62862ef9480b 494 Led1State = true;
mluis 5:62862ef9480b 495 Led1StateChanged = true;
mluis 5:62862ef9480b 496 TimerStart( &Led1Timer );
mluis 5:62862ef9480b 497
mluis 0:45496a70a8a5 498 UplinkStatusUpdated = true;
mluis 0:45496a70a8a5 499 }
mluis 0:45496a70a8a5 500 NextTx = true;
mluis 0:45496a70a8a5 501 }
mluis 0:45496a70a8a5 502
mluis 0:45496a70a8a5 503 /*!
mluis 0:45496a70a8a5 504 * \brief MCPS-Indication event function
mluis 0:45496a70a8a5 505 *
mluis 3:3152aa75c58d 506 * \param [IN] mcpsIndication - Pointer to the indication structure,
mluis 0:45496a70a8a5 507 * containing indication attributes.
mluis 0:45496a70a8a5 508 */
mluis 3:3152aa75c58d 509 static void McpsIndication( McpsIndication_t *mcpsIndication )
mluis 0:45496a70a8a5 510 {
mluis 3:3152aa75c58d 511 if( mcpsIndication->Status != LORAMAC_EVENT_INFO_STATUS_OK )
mluis 0:45496a70a8a5 512 {
mluis 0:45496a70a8a5 513 return;
mluis 0:45496a70a8a5 514 }
mluis 0:45496a70a8a5 515
mluis 3:3152aa75c58d 516 switch( mcpsIndication->McpsIndication )
mluis 0:45496a70a8a5 517 {
mluis 0:45496a70a8a5 518 case MCPS_UNCONFIRMED:
mluis 0:45496a70a8a5 519 {
mluis 0:45496a70a8a5 520 break;
mluis 0:45496a70a8a5 521 }
mluis 0:45496a70a8a5 522 case MCPS_CONFIRMED:
mluis 0:45496a70a8a5 523 {
mluis 0:45496a70a8a5 524 break;
mluis 0:45496a70a8a5 525 }
mluis 0:45496a70a8a5 526 case MCPS_PROPRIETARY:
mluis 0:45496a70a8a5 527 {
mluis 0:45496a70a8a5 528 break;
mluis 0:45496a70a8a5 529 }
mluis 0:45496a70a8a5 530 case MCPS_MULTICAST:
mluis 0:45496a70a8a5 531 {
mluis 0:45496a70a8a5 532 break;
mluis 0:45496a70a8a5 533 }
mluis 0:45496a70a8a5 534 default:
mluis 0:45496a70a8a5 535 break;
mluis 0:45496a70a8a5 536 }
mluis 0:45496a70a8a5 537
mluis 0:45496a70a8a5 538 // Check Multicast
mluis 0:45496a70a8a5 539 // Check Port
mluis 0:45496a70a8a5 540 // Check Datarate
mluis 0:45496a70a8a5 541 // Check FramePending
mluis 0:45496a70a8a5 542 // Check Buffer
mluis 0:45496a70a8a5 543 // Check BufferSize
mluis 0:45496a70a8a5 544 // Check Rssi
mluis 0:45496a70a8a5 545 // Check Snr
mluis 0:45496a70a8a5 546 // Check RxSlot
mluis 3:3152aa75c58d 547 LoRaMacDownlinkStatus.Rssi = mcpsIndication->Rssi;
mluis 3:3152aa75c58d 548 if( mcpsIndication->Snr & 0x80 ) // The SNR sign bit is 1
mluis 0:45496a70a8a5 549 {
mluis 0:45496a70a8a5 550 // Invert and divide by 4
mluis 3:3152aa75c58d 551 LoRaMacDownlinkStatus.Snr = ( ( ~mcpsIndication->Snr + 1 ) & 0xFF ) >> 2;
mluis 0:45496a70a8a5 552 LoRaMacDownlinkStatus.Snr = -LoRaMacDownlinkStatus.Snr;
mluis 0:45496a70a8a5 553 }
mluis 0:45496a70a8a5 554 else
mluis 0:45496a70a8a5 555 {
mluis 0:45496a70a8a5 556 // Divide by 4
mluis 3:3152aa75c58d 557 LoRaMacDownlinkStatus.Snr = ( mcpsIndication->Snr & 0xFF ) >> 2;
mluis 0:45496a70a8a5 558 }
mluis 0:45496a70a8a5 559 LoRaMacDownlinkStatus.DownlinkCounter++;
mluis 3:3152aa75c58d 560 LoRaMacDownlinkStatus.RxData = mcpsIndication->RxData;
mluis 3:3152aa75c58d 561 LoRaMacDownlinkStatus.Port = mcpsIndication->Port;
mluis 3:3152aa75c58d 562 LoRaMacDownlinkStatus.Buffer = mcpsIndication->Buffer;
mluis 3:3152aa75c58d 563 LoRaMacDownlinkStatus.BufferSize = mcpsIndication->BufferSize;
mluis 0:45496a70a8a5 564
mluis 0:45496a70a8a5 565 if( ComplianceTest.Running == true )
mluis 0:45496a70a8a5 566 {
mluis 0:45496a70a8a5 567 ComplianceTest.DownLinkCounter++;
mluis 0:45496a70a8a5 568 }
mluis 0:45496a70a8a5 569
mluis 3:3152aa75c58d 570 if( mcpsIndication->RxData == true )
mluis 0:45496a70a8a5 571 {
mluis 3:3152aa75c58d 572 switch( mcpsIndication->Port )
mluis 0:45496a70a8a5 573 {
mluis 0:45496a70a8a5 574 case 1: // The application LED can be controlled on port 1 or 2
mluis 0:45496a70a8a5 575 case 2:
mluis 3:3152aa75c58d 576 if( mcpsIndication->BufferSize == 1 )
mluis 0:45496a70a8a5 577 {
mluis 3:3152aa75c58d 578 AppLedStateOn = mcpsIndication->Buffer[0] & 0x01;
mluis 0:45496a70a8a5 579 Led3StateChanged = true;
mluis 0:45496a70a8a5 580 }
mluis 0:45496a70a8a5 581 break;
mluis 0:45496a70a8a5 582 case 224:
mluis 0:45496a70a8a5 583 if( ComplianceTest.Running == false )
mluis 0:45496a70a8a5 584 {
mluis 0:45496a70a8a5 585 // Check compliance test enable command (i)
mluis 3:3152aa75c58d 586 if( ( mcpsIndication->BufferSize == 4 ) &&
mluis 3:3152aa75c58d 587 ( mcpsIndication->Buffer[0] == 0x01 ) &&
mluis 3:3152aa75c58d 588 ( mcpsIndication->Buffer[1] == 0x01 ) &&
mluis 3:3152aa75c58d 589 ( mcpsIndication->Buffer[2] == 0x01 ) &&
mluis 3:3152aa75c58d 590 ( mcpsIndication->Buffer[3] == 0x01 ) )
mluis 0:45496a70a8a5 591 {
mluis 0:45496a70a8a5 592 IsTxConfirmed = false;
mluis 0:45496a70a8a5 593 AppPort = 224;
mluis 0:45496a70a8a5 594 AppDataSize = 2;
mluis 0:45496a70a8a5 595 ComplianceTest.DownLinkCounter = 0;
mluis 0:45496a70a8a5 596 ComplianceTest.LinkCheck = false;
mluis 0:45496a70a8a5 597 ComplianceTest.DemodMargin = 0;
mluis 0:45496a70a8a5 598 ComplianceTest.NbGateways = 0;
mluis 0:45496a70a8a5 599 ComplianceTest.Running = true;
mluis 0:45496a70a8a5 600 ComplianceTest.State = 1;
mluis 0:45496a70a8a5 601
mluis 0:45496a70a8a5 602 MibRequestConfirm_t mibReq;
mluis 0:45496a70a8a5 603 mibReq.Type = MIB_ADR;
mluis 0:45496a70a8a5 604 mibReq.Param.AdrEnable = true;
mluis 0:45496a70a8a5 605 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 606
mluis 0:45496a70a8a5 607 #if defined( USE_BAND_868 )
mluis 0:45496a70a8a5 608 LoRaMacTestSetDutyCycleOn( false );
mluis 0:45496a70a8a5 609 #endif
mluis 0:45496a70a8a5 610 }
mluis 0:45496a70a8a5 611 }
mluis 0:45496a70a8a5 612 else
mluis 0:45496a70a8a5 613 {
mluis 3:3152aa75c58d 614 ComplianceTest.State = mcpsIndication->Buffer[0];
mluis 0:45496a70a8a5 615 switch( ComplianceTest.State )
mluis 0:45496a70a8a5 616 {
mluis 0:45496a70a8a5 617 case 0: // Check compliance test disable command (ii)
mluis 0:45496a70a8a5 618 IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON;
mluis 0:45496a70a8a5 619 AppPort = LORAWAN_APP_PORT;
mluis 0:45496a70a8a5 620 AppDataSize = LORAWAN_APP_DATA_SIZE;
mluis 0:45496a70a8a5 621 ComplianceTest.DownLinkCounter = 0;
mluis 0:45496a70a8a5 622 ComplianceTest.Running = false;
mluis 0:45496a70a8a5 623
mluis 0:45496a70a8a5 624 MibRequestConfirm_t mibReq;
mluis 0:45496a70a8a5 625 mibReq.Type = MIB_ADR;
mluis 0:45496a70a8a5 626 mibReq.Param.AdrEnable = LORAWAN_ADR_ON;
mluis 0:45496a70a8a5 627 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 628 #if defined( USE_BAND_868 )
mluis 0:45496a70a8a5 629 LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON );
mluis 0:45496a70a8a5 630 #endif
mluis 0:45496a70a8a5 631 break;
mluis 0:45496a70a8a5 632 case 1: // (iii, iv)
mluis 0:45496a70a8a5 633 AppDataSize = 2;
mluis 0:45496a70a8a5 634 break;
mluis 0:45496a70a8a5 635 case 2: // Enable confirmed messages (v)
mluis 0:45496a70a8a5 636 IsTxConfirmed = true;
mluis 0:45496a70a8a5 637 ComplianceTest.State = 1;
mluis 0:45496a70a8a5 638 break;
mluis 0:45496a70a8a5 639 case 3: // Disable confirmed messages (vi)
mluis 0:45496a70a8a5 640 IsTxConfirmed = false;
mluis 0:45496a70a8a5 641 ComplianceTest.State = 1;
mluis 0:45496a70a8a5 642 break;
mluis 0:45496a70a8a5 643 case 4: // (vii)
mluis 3:3152aa75c58d 644 AppDataSize = mcpsIndication->BufferSize;
mluis 0:45496a70a8a5 645
mluis 0:45496a70a8a5 646 AppData[0] = 4;
mluis 0:45496a70a8a5 647 for( uint8_t i = 1; i < AppDataSize; i++ )
mluis 0:45496a70a8a5 648 {
mluis 3:3152aa75c58d 649 AppData[i] = mcpsIndication->Buffer[i] + 1;
mluis 0:45496a70a8a5 650 }
mluis 0:45496a70a8a5 651 break;
mluis 0:45496a70a8a5 652 case 5: // (viii)
mluis 0:45496a70a8a5 653 {
mluis 0:45496a70a8a5 654 MlmeReq_t mlmeReq;
mluis 0:45496a70a8a5 655 mlmeReq.Type = MLME_LINK_CHECK;
mluis 0:45496a70a8a5 656 LoRaMacMlmeRequest( &mlmeReq );
mluis 0:45496a70a8a5 657 }
mluis 0:45496a70a8a5 658 break;
mluis 5:62862ef9480b 659 case 6: // (ix)
mluis 5:62862ef9480b 660 {
mluis 5:62862ef9480b 661 MlmeReq_t mlmeReq;
mluis 5:62862ef9480b 662
mluis 5:62862ef9480b 663 mlmeReq.Type = MLME_JOIN;
mluis 5:62862ef9480b 664
mluis 5:62862ef9480b 665 mlmeReq.Req.Join.DevEui = DevEui;
mluis 5:62862ef9480b 666 mlmeReq.Req.Join.AppEui = AppEui;
mluis 5:62862ef9480b 667 mlmeReq.Req.Join.AppKey = AppKey;
mluis 5:62862ef9480b 668
mluis 5:62862ef9480b 669 LoRaMacMlmeRequest( &mlmeReq );
mluis 5:62862ef9480b 670 DeviceState = DEVICE_STATE_SLEEP;
mluis 5:62862ef9480b 671 }
mluis 5:62862ef9480b 672 break;
mluis 0:45496a70a8a5 673 default:
mluis 0:45496a70a8a5 674 break;
mluis 0:45496a70a8a5 675 }
mluis 0:45496a70a8a5 676 }
mluis 0:45496a70a8a5 677 break;
mluis 0:45496a70a8a5 678 default:
mluis 0:45496a70a8a5 679 break;
mluis 0:45496a70a8a5 680 }
mluis 0:45496a70a8a5 681 }
mluis 0:45496a70a8a5 682
mluis 0:45496a70a8a5 683 // Switch LED 2 ON for each received downlink
mluis 0:45496a70a8a5 684 Led2State = true;
mluis 0:45496a70a8a5 685 Led2StateChanged = true;
mluis 0:45496a70a8a5 686 TimerStart( &Led2Timer );
mluis 0:45496a70a8a5 687 DownlinkStatusUpdated = true;
mluis 0:45496a70a8a5 688 }
mluis 0:45496a70a8a5 689
mluis 0:45496a70a8a5 690 /*!
mluis 0:45496a70a8a5 691 * \brief MLME-Confirm event function
mluis 0:45496a70a8a5 692 *
mluis 3:3152aa75c58d 693 * \param [IN] mlmeConfirm - Pointer to the confirm structure,
mluis 0:45496a70a8a5 694 * containing confirm attributes.
mluis 0:45496a70a8a5 695 */
mluis 3:3152aa75c58d 696 static void MlmeConfirm( MlmeConfirm_t *mlmeConfirm )
mluis 0:45496a70a8a5 697 {
mluis 3:3152aa75c58d 698 if( mlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
mluis 0:45496a70a8a5 699 {
mluis 3:3152aa75c58d 700 switch( mlmeConfirm->MlmeRequest )
mluis 0:45496a70a8a5 701 {
mluis 0:45496a70a8a5 702 case MLME_JOIN:
mluis 0:45496a70a8a5 703 {
mluis 0:45496a70a8a5 704 // Status is OK, node has joined the network
mluis 0:45496a70a8a5 705 IsNetworkJoinedStatusUpdate = true;
mluis 5:62862ef9480b 706 DeviceState = DEVICE_STATE_SEND;
mluis 5:62862ef9480b 707 NextTx = true;
mluis 0:45496a70a8a5 708 break;
mluis 0:45496a70a8a5 709 }
mluis 0:45496a70a8a5 710 case MLME_LINK_CHECK:
mluis 0:45496a70a8a5 711 {
mluis 0:45496a70a8a5 712 // Check DemodMargin
mluis 0:45496a70a8a5 713 // Check NbGateways
mluis 0:45496a70a8a5 714 if( ComplianceTest.Running == true )
mluis 0:45496a70a8a5 715 {
mluis 0:45496a70a8a5 716 ComplianceTest.LinkCheck = true;
mluis 3:3152aa75c58d 717 ComplianceTest.DemodMargin = mlmeConfirm->DemodMargin;
mluis 3:3152aa75c58d 718 ComplianceTest.NbGateways = mlmeConfirm->NbGateways;
mluis 0:45496a70a8a5 719 }
mluis 0:45496a70a8a5 720 break;
mluis 0:45496a70a8a5 721 }
mluis 0:45496a70a8a5 722 default:
mluis 0:45496a70a8a5 723 break;
mluis 0:45496a70a8a5 724 }
mluis 0:45496a70a8a5 725 }
mluis 0:45496a70a8a5 726 NextTx = true;
mluis 0:45496a70a8a5 727 UplinkStatusUpdated = true;
mluis 0:45496a70a8a5 728 }
mluis 0:45496a70a8a5 729
mluis 0:45496a70a8a5 730 /**
mluis 0:45496a70a8a5 731 * Main application entry point.
mluis 0:45496a70a8a5 732 */
mluis 0:45496a70a8a5 733 int main( void )
mluis 0:45496a70a8a5 734 {
mluis 0:45496a70a8a5 735 LoRaMacPrimitives_t LoRaMacPrimitives;
mluis 0:45496a70a8a5 736 LoRaMacCallback_t LoRaMacCallbacks;
mluis 0:45496a70a8a5 737 MibRequestConfirm_t mibReq;
mluis 0:45496a70a8a5 738
mluis 0:45496a70a8a5 739 BoardInit( );
mluis 0:45496a70a8a5 740 SerialDisplayInit( );
mluis 0:45496a70a8a5 741
mluis 5:62862ef9480b 742 SerialDisplayUpdateEui( 5, DevEui );
mluis 5:62862ef9480b 743 SerialDisplayUpdateEui( 6, AppEui );
mluis 5:62862ef9480b 744 SerialDisplayUpdateKey( 7, AppKey );
mluis 5:62862ef9480b 745
mluis 5:62862ef9480b 746 #if( OVER_THE_AIR_ACTIVATION == 0 )
mluis 5:62862ef9480b 747 SerialDisplayUpdateNwkId( LORAWAN_NETWORK_ID );
mluis 5:62862ef9480b 748 SerialDisplayUpdateDevAddr( DevAddr );
mluis 5:62862ef9480b 749 SerialDisplayUpdateKey( 12, NwkSKey );
mluis 5:62862ef9480b 750 SerialDisplayUpdateKey( 13, AppSKey );
mluis 5:62862ef9480b 751 #endif
mluis 5:62862ef9480b 752
mluis 0:45496a70a8a5 753 DeviceState = DEVICE_STATE_INIT;
mluis 0:45496a70a8a5 754
mluis 0:45496a70a8a5 755 while( 1 )
mluis 0:45496a70a8a5 756 {
mluis 0:45496a70a8a5 757 SerialRxProcess( );
mluis 0:45496a70a8a5 758 if( IsNetworkJoinedStatusUpdate == true )
mluis 0:45496a70a8a5 759 {
mluis 0:45496a70a8a5 760 IsNetworkJoinedStatusUpdate = false;
mluis 0:45496a70a8a5 761 mibReq.Type = MIB_NETWORK_JOINED;
mluis 0:45496a70a8a5 762 LoRaMacMibGetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 763 SerialDisplayUpdateNetworkIsJoined( mibReq.Param.IsNetworkJoined );
mluis 0:45496a70a8a5 764 }
mluis 0:45496a70a8a5 765 if( Led1StateChanged == true )
mluis 0:45496a70a8a5 766 {
mluis 0:45496a70a8a5 767 Led1StateChanged = false;
mluis 0:45496a70a8a5 768 SerialDisplayUpdateLedState( 1, Led1State );
mluis 0:45496a70a8a5 769 }
mluis 0:45496a70a8a5 770 if( Led2StateChanged == true )
mluis 0:45496a70a8a5 771 {
mluis 0:45496a70a8a5 772 Led2StateChanged = false;
mluis 0:45496a70a8a5 773 SerialDisplayUpdateLedState( 2, Led2State );
mluis 0:45496a70a8a5 774 }
mluis 0:45496a70a8a5 775 if( Led3StateChanged == true )
mluis 0:45496a70a8a5 776 {
mluis 0:45496a70a8a5 777 Led3StateChanged = false;
mluis 0:45496a70a8a5 778 SerialDisplayUpdateLedState( 3, AppLedStateOn );
mluis 0:45496a70a8a5 779 }
mluis 0:45496a70a8a5 780 if( UplinkStatusUpdated == true )
mluis 0:45496a70a8a5 781 {
mluis 0:45496a70a8a5 782 UplinkStatusUpdated = false;
mluis 0:45496a70a8a5 783 SerialDisplayUpdateUplink( LoRaMacUplinkStatus.Acked, LoRaMacUplinkStatus.Datarate, LoRaMacUplinkStatus.UplinkCounter, LoRaMacUplinkStatus.Port, LoRaMacUplinkStatus.Buffer, LoRaMacUplinkStatus.BufferSize );
mluis 0:45496a70a8a5 784 }
mluis 0:45496a70a8a5 785 if( DownlinkStatusUpdated == true )
mluis 0:45496a70a8a5 786 {
mluis 0:45496a70a8a5 787 DownlinkStatusUpdated = false;
mluis 0:45496a70a8a5 788 SerialDisplayUpdateLedState( 2, Led2State );
mluis 0:45496a70a8a5 789 SerialDisplayUpdateDownlink( LoRaMacDownlinkStatus.RxData, LoRaMacDownlinkStatus.Rssi, LoRaMacDownlinkStatus.Snr, LoRaMacDownlinkStatus.DownlinkCounter, LoRaMacDownlinkStatus.Port, LoRaMacDownlinkStatus.Buffer, LoRaMacDownlinkStatus.BufferSize );
mluis 0:45496a70a8a5 790 }
mluis 0:45496a70a8a5 791
mluis 0:45496a70a8a5 792 switch( DeviceState )
mluis 0:45496a70a8a5 793 {
mluis 0:45496a70a8a5 794 case DEVICE_STATE_INIT:
mluis 0:45496a70a8a5 795 {
dudmuck 6:72ea69843556 796 uint8_t id2;
mluis 0:45496a70a8a5 797 LoRaMacPrimitives.MacMcpsConfirm = McpsConfirm;
mluis 0:45496a70a8a5 798 LoRaMacPrimitives.MacMcpsIndication = McpsIndication;
mluis 0:45496a70a8a5 799 LoRaMacPrimitives.MacMlmeConfirm = MlmeConfirm;
mluis 0:45496a70a8a5 800 LoRaMacCallbacks.GetBatteryLevel = BoardGetBatteryLevel;
mluis 0:45496a70a8a5 801 LoRaMacInitialization( &LoRaMacPrimitives, &LoRaMacCallbacks );
mluis 0:45496a70a8a5 802
mluis 0:45496a70a8a5 803 TimerInit( &TxNextPacketTimer, OnTxNextPacketTimerEvent );
mluis 0:45496a70a8a5 804
mluis 0:45496a70a8a5 805 TimerInit( &Led1Timer, OnLed1TimerEvent );
mluis 0:45496a70a8a5 806 TimerSetValue( &Led1Timer, 25000 );
mluis 0:45496a70a8a5 807
mluis 0:45496a70a8a5 808 TimerInit( &Led2Timer, OnLed2TimerEvent );
mluis 0:45496a70a8a5 809 TimerSetValue( &Led2Timer, 25000 );
mluis 0:45496a70a8a5 810
mluis 0:45496a70a8a5 811 mibReq.Type = MIB_ADR;
mluis 0:45496a70a8a5 812 mibReq.Param.AdrEnable = LORAWAN_ADR_ON;
mluis 0:45496a70a8a5 813 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 814
mluis 0:45496a70a8a5 815 mibReq.Type = MIB_PUBLIC_NETWORK;
mluis 0:45496a70a8a5 816 mibReq.Param.EnablePublicNetwork = LORAWAN_PUBLIC_NETWORK;
mluis 0:45496a70a8a5 817 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 818
mluis 0:45496a70a8a5 819 #if defined( USE_BAND_868 )
mluis 0:45496a70a8a5 820 LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON );
mluis 0:45496a70a8a5 821 SerialDisplayUpdateDutyCycle( LORAWAN_DUTYCYCLE_ON );
mluis 3:3152aa75c58d 822
mluis 3:3152aa75c58d 823 #if( USE_SEMTECH_DEFAULT_CHANNEL_LINEUP == 1 )
mluis 3:3152aa75c58d 824 LoRaMacChannelAdd( 3, ( ChannelParams_t )LC4 );
mluis 3:3152aa75c58d 825 LoRaMacChannelAdd( 4, ( ChannelParams_t )LC5 );
mluis 3:3152aa75c58d 826 LoRaMacChannelAdd( 5, ( ChannelParams_t )LC6 );
mluis 3:3152aa75c58d 827 LoRaMacChannelAdd( 6, ( ChannelParams_t )LC7 );
mluis 3:3152aa75c58d 828 LoRaMacChannelAdd( 7, ( ChannelParams_t )LC8 );
mluis 3:3152aa75c58d 829 LoRaMacChannelAdd( 8, ( ChannelParams_t )LC9 );
mluis 3:3152aa75c58d 830 LoRaMacChannelAdd( 9, ( ChannelParams_t )LC10 );
mluis 5:62862ef9480b 831
mluis 5:62862ef9480b 832 mibReq.Type = MIB_RX2_CHANNEL;
mluis 5:62862ef9480b 833 mibReq.Param.Rx2Channel = ( Rx2ChannelParams_t ){ 869525000, DR_3 };
mluis 5:62862ef9480b 834 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 3:3152aa75c58d 835 #endif
mluis 3:3152aa75c58d 836
mluis 0:45496a70a8a5 837 #endif
mluis 0:45496a70a8a5 838 SerialDisplayUpdateActivationMode( OVER_THE_AIR_ACTIVATION );
mluis 0:45496a70a8a5 839 SerialDisplayUpdateAdr( LORAWAN_ADR_ON );
mluis 0:45496a70a8a5 840 SerialDisplayUpdatePublicNetwork( LORAWAN_PUBLIC_NETWORK );
mluis 0:45496a70a8a5 841
mluis 0:45496a70a8a5 842 LoRaMacDownlinkStatus.DownlinkCounter = 0;
dudmuck 6:72ea69843556 843
dudmuck 6:72ea69843556 844 CALL_METH(temp_sensor1, ReadID, &id2, 0x0);
mluis 0:45496a70a8a5 845 DeviceState = DEVICE_STATE_JOIN;
mluis 0:45496a70a8a5 846 break;
mluis 0:45496a70a8a5 847 }
mluis 0:45496a70a8a5 848 case DEVICE_STATE_JOIN:
mluis 0:45496a70a8a5 849 {
mluis 0:45496a70a8a5 850 #if( OVER_THE_AIR_ACTIVATION != 0 )
mluis 0:45496a70a8a5 851 MlmeReq_t mlmeReq;
mluis 0:45496a70a8a5 852
mluis 0:45496a70a8a5 853 mlmeReq.Type = MLME_JOIN;
mluis 0:45496a70a8a5 854
mluis 0:45496a70a8a5 855 mlmeReq.Req.Join.DevEui = DevEui;
mluis 0:45496a70a8a5 856 mlmeReq.Req.Join.AppEui = AppEui;
mluis 0:45496a70a8a5 857 mlmeReq.Req.Join.AppKey = AppKey;
mluis 0:45496a70a8a5 858
mluis 0:45496a70a8a5 859 if( NextTx == true )
mluis 0:45496a70a8a5 860 {
mluis 0:45496a70a8a5 861 LoRaMacMlmeRequest( &mlmeReq );
mluis 0:45496a70a8a5 862 }
mluis 5:62862ef9480b 863 DeviceState = DEVICE_STATE_SLEEP;
mluis 0:45496a70a8a5 864 #else
mluis 0:45496a70a8a5 865 mibReq.Type = MIB_NET_ID;
mluis 0:45496a70a8a5 866 mibReq.Param.NetID = LORAWAN_NETWORK_ID;
mluis 0:45496a70a8a5 867 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 868
mluis 0:45496a70a8a5 869 mibReq.Type = MIB_DEV_ADDR;
mluis 0:45496a70a8a5 870 mibReq.Param.DevAddr = DevAddr;
mluis 0:45496a70a8a5 871 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 872
mluis 0:45496a70a8a5 873 mibReq.Type = MIB_NWK_SKEY;
mluis 0:45496a70a8a5 874 mibReq.Param.NwkSKey = NwkSKey;
mluis 0:45496a70a8a5 875 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 876
mluis 0:45496a70a8a5 877 mibReq.Type = MIB_APP_SKEY;
mluis 0:45496a70a8a5 878 mibReq.Param.AppSKey = AppSKey;
mluis 0:45496a70a8a5 879 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 880
mluis 0:45496a70a8a5 881 mibReq.Type = MIB_NETWORK_JOINED;
mluis 0:45496a70a8a5 882 mibReq.Param.IsNetworkJoined = true;
mluis 0:45496a70a8a5 883 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 884
mluis 0:45496a70a8a5 885 DeviceState = DEVICE_STATE_SEND;
mluis 0:45496a70a8a5 886 #endif
mluis 0:45496a70a8a5 887 IsNetworkJoinedStatusUpdate = true;
mluis 0:45496a70a8a5 888 break;
mluis 0:45496a70a8a5 889 }
mluis 0:45496a70a8a5 890 case DEVICE_STATE_SEND:
mluis 0:45496a70a8a5 891 {
mluis 0:45496a70a8a5 892 if( NextTx == true )
mluis 0:45496a70a8a5 893 {
mluis 0:45496a70a8a5 894 SerialDisplayUpdateUplinkAcked( false );
mluis 0:45496a70a8a5 895 SerialDisplayUpdateDonwlinkRxData( false );
mluis 0:45496a70a8a5 896 PrepareTxFrame( AppPort );
mluis 0:45496a70a8a5 897
mluis 0:45496a70a8a5 898 NextTx = SendFrame( );
mluis 0:45496a70a8a5 899 }
mluis 0:45496a70a8a5 900 if( ComplianceTest.Running == true )
mluis 0:45496a70a8a5 901 {
mluis 3:3152aa75c58d 902 // Schedule next packet transmission
mluis 3:3152aa75c58d 903 TxDutyCycleTime = 5000000; // 5000000 us
mluis 0:45496a70a8a5 904 }
mluis 0:45496a70a8a5 905 else
mluis 0:45496a70a8a5 906 {
mluis 0:45496a70a8a5 907 // Schedule next packet transmission
mluis 0:45496a70a8a5 908 TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND );
mluis 0:45496a70a8a5 909 }
mluis 0:45496a70a8a5 910 DeviceState = DEVICE_STATE_CYCLE;
mluis 0:45496a70a8a5 911 break;
mluis 0:45496a70a8a5 912 }
mluis 0:45496a70a8a5 913 case DEVICE_STATE_CYCLE:
mluis 0:45496a70a8a5 914 {
mluis 3:3152aa75c58d 915 DeviceState = DEVICE_STATE_SLEEP;
mluis 3:3152aa75c58d 916
mluis 0:45496a70a8a5 917 // Schedule next packet transmission
mluis 0:45496a70a8a5 918 TimerSetValue( &TxNextPacketTimer, TxDutyCycleTime );
mluis 0:45496a70a8a5 919 TimerStart( &TxNextPacketTimer );
mluis 0:45496a70a8a5 920 break;
mluis 0:45496a70a8a5 921 }
mluis 0:45496a70a8a5 922 case DEVICE_STATE_SLEEP:
mluis 0:45496a70a8a5 923 {
mluis 0:45496a70a8a5 924 // Wake up through events
mluis 0:45496a70a8a5 925 break;
mluis 0:45496a70a8a5 926 }
mluis 0:45496a70a8a5 927 default:
mluis 0:45496a70a8a5 928 {
mluis 0:45496a70a8a5 929 DeviceState = DEVICE_STATE_INIT;
mluis 0:45496a70a8a5 930 break;
mluis 0:45496a70a8a5 931 }
mluis 0:45496a70a8a5 932 }
mluis 0:45496a70a8a5 933 }
mluis 0:45496a70a8a5 934 }