/*! * Main application entry point. */ int main( void ) { // Target board initialisation BoardInitMcu( ); BoardInitPeriph( ); // Radio initialization RadioEvents.RxDone = OnRxDone; Radio.Init( &RadioEvents ); Radio.SetChannel( RF_FREQUENCY ); #if defined( USE_MODEM_LORA ) Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH, LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, true, LORA_IQ_INVERSION_ON, true ); #elif defined( USE_MODEM_FSK ) Radio.SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE, 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH, 0, FSK_FIX_LENGTH_PAYLOAD_ON, true, false, true ); #else #error "Please define a frequency band in the compiler options." #endif Radio.Rx( 0 ); // Continuous Rx while( 1 ) { TimerLowPowerHandler( ); } }
/** * Main application entry point. */ int main( void ) { bool isMaster = true; uint8_t i; // Target board initialisation BoardInitMcu( ); BoardInitPeriph( ); // Radio initialization RadioEvents.TxDone = OnTxDone; RadioEvents.RxDone = OnRxDone; RadioEvents.TxTimeout = OnTxTimeout; RadioEvents.RxTimeout = OnRxTimeout; RadioEvents.RxError = OnRxError; Radio.Init( &RadioEvents ); Radio.SetChannel( RF_FREQUENCY ); #if defined( USE_MODEM_LORA ) Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, LORA_CODINGRATE, LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, true, 0, 0, LORA_IQ_INVERSION_ON, 3000 ); Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH, LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0, true, 0, 0, LORA_IQ_INVERSION_ON, true ); #elif defined( USE_MODEM_FSK ) Radio.SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0, FSK_DATARATE, 0, FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON, true, 0, 0, 0, 3000 ); Radio.SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE, 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH, 0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, true, 0, 0,false, true ); #else #error "Please define a frequency band in the compiler options." #endif Radio.Rx( RX_TIMEOUT_VALUE ); while( 1 ) { switch( State ) { case RX: if( isMaster == true ) { if( BufferSize > 0 ) { if( strncmp( ( const char* )Buffer, ( const char* )PongMsg, 4 ) == 0 ) { // Indicates on a LED that the received frame is a PONG GpioWrite( &Led1, GpioRead( &Led1 ) ^ 1 ); // Send the next PING frame Buffer[0] = 'P'; Buffer[1] = 'I'; Buffer[2] = 'N'; Buffer[3] = 'G'; // We fill the buffer with numbers for the payload for( i = 4; i < BufferSize; i++ ) { Buffer[i] = i - 4; } DelayMs( 1 ); Radio.Send( Buffer, BufferSize ); } else if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 ) { // A master already exists then become a slave isMaster = false; GpioWrite( &Led2, 1 ); // Set LED off Radio.Rx( RX_TIMEOUT_VALUE ); } else // valid reception but neither a PING or a PONG message { // Set device as master ans start again isMaster = true; Radio.Rx( RX_TIMEOUT_VALUE ); } } } else { if( BufferSize > 0 ) { if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 ) { // Indicates on a LED that the received frame is a PING GpioWrite( &Led1, GpioRead( &Led1 ) ^ 1 ); // Send the reply to the PONG string Buffer[0] = 'P'; Buffer[1] = 'O'; Buffer[2] = 'N'; Buffer[3] = 'G'; // We fill the buffer with numbers for the payload for( i = 4; i < BufferSize; i++ ) { Buffer[i] = i - 4; } DelayMs( 1 ); Radio.Send( Buffer, BufferSize ); } else // valid reception but not a PING as expected { // Set device as master and start again isMaster = true; Radio.Rx( RX_TIMEOUT_VALUE ); } } } State = LOWPOWER; break; case TX: // Indicates on a LED that we have sent a PING [Master] // Indicates on a LED that we have sent a PONG [Slave] GpioWrite( &Led2, GpioRead( &Led2 ) ^ 1 ); Radio.Rx( RX_TIMEOUT_VALUE ); State = LOWPOWER; break; case RX_TIMEOUT: case RX_ERROR: if( isMaster == true ) { // Send the next PING frame Buffer[0] = 'P'; Buffer[1] = 'I'; Buffer[2] = 'N'; Buffer[3] = 'G'; for( i = 4; i < BufferSize; i++ ) { Buffer[i] = i - 4; } DelayMs( 1 ); Radio.Send( Buffer, BufferSize ); } else { Radio.Rx( RX_TIMEOUT_VALUE ); } State = LOWPOWER; break; case TX_TIMEOUT: Radio.Rx( RX_TIMEOUT_VALUE ); State = LOWPOWER; break; case LOWPOWER: default: // Set low power break; } TimerLowPowerHandler( ); } }
/** * Main application entry point. */ int main( void ) { uint8_t sendFrameStatus = 0; uint8_t batteryLevel = 0; BoardInitMcu( ); BoardInitPeriph( ); // Initialize LoRaMac device unique ID BoardGetUniqueId( DevEui ); LoRaMacEvents.MacEvent = OnMacEvent; LoRaMacInit( &LoRaMacEvents ); IsNetworkJoined = false; #if( OVER_THE_AIR_ACTIVATION == 0 ) // Random seed initialization srand( RAND_SEED ); // Choose a random device address // NwkID = 0 // NwkAddr rand [0, 33554431] DevAddr = randr( 0, 0x01FFFFFF ); LoRaMacInitNwkIds( 0x000000, DevAddr, NwkSKey, AppSKey ); IsNetworkJoined = true; #else // Sends a JoinReq Command every 5 seconds until the network is joined TimerInit( &JoinReqTimer, OnJoinReqTimerEvent ); TimerSetValue( &JoinReqTimer, OVER_THE_AIR_ACTIVATION_DUTYCYCLE ); TimerStart( &JoinReqTimer ); #endif TxNextPacket = true; TimerInit( &TxNextPacketTimer, OnTxNextPacketTimerEvent ); TimerInit( &Led1Timer, OnLed1TimerEvent ); TimerSetValue( &Led1Timer, 25000 ); TimerInit( &Led2Timer, OnLed2TimerEvent ); TimerSetValue( &Led2Timer, 25000 ); LoRaMacSetAdrOn( true ); while( 1 ) { while( IsNetworkJoined == false ) { #if( OVER_THE_AIR_ACTIVATION != 0 ) if( TxNextPacket == true ) { TxNextPacket = false; LoRaMacJoinReq( DevEui, AppEui, AppKey ); // Relaunch timer for next trial TimerStart( &JoinReqTimer ); } TimerLowPowerHandler( ); #endif } if( Led1TimerEvent == true ) { Led1TimerEvent = false; // Switch LED 1 OFF GpioWrite( &Led1, 1 ); } if( Led2TimerEvent == true ) { Led2TimerEvent = false; // Switch LED 2 OFF GpioWrite( &Led2, 1 ); } if( TxAckReceived == true ) { TxAckReceived = false; // Switch LED 2 ON GpioWrite( &Led2, 0 ); TimerStart( &Led2Timer ); } if( RxDone == true ) { RxDone = false; // Switch LED 2 ON GpioWrite( &Led2, 0 ); TimerStart( &Led2Timer ); if( AppLedStateOn == true ) { // Switch LED 3 ON GpioWrite( &Led2, 0 ); } else { // Switch LED 3 OFF GpioWrite( &Led2, 1 ); } } if( TxDone == true ) { TxDone = false; // Schedule next packet transmission TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND ); TimerSetValue( &TxNextPacketTimer, TxDutyCycleTime ); TimerStart( &TxNextPacketTimer ); } if( TxNextPacket == true ) { TxNextPacket = false; batteryLevel = BoardMeasureBatterieLevel( ); // 1 (very low) to 254 (fully charged) // Switch LED 1 ON GpioWrite( &Led1, 0 ); TimerStart( &Led1Timer ); AppData[0] = AppLedStateOn; // // // AppData[7] = batteryLevel; sendFrameStatus = LoRaMacSendFrame( 2, AppData, APP_DATA_SIZE ); //sendFrameStatus = LoRaMacSendConfirmedFrame( 2, AppData, APP_DATA_SIZE, 8 ); switch( sendFrameStatus ) { case 3: // LENGTH_PORT_ERROR case 4: // MAC_CMD_ERROR case 5: // NO_FREE_CHANNEL // Schedule a new transmission TxDone = true; break; default: break; } } TimerLowPowerHandler( ); } }
/** * Main application entry point. */ int main( void ) { LoRaMacPrimitives_t LoRaMacPrimitives; LoRaMacCallback_t LoRaMacCallbacks; MibRequestConfirm_t mibReq; BoardInitMcu( ); BoardInitPeriph( ); DeviceState = DEVICE_STATE_INIT; while( 1 ) { switch( DeviceState ) { case DEVICE_STATE_INIT: { LoRaMacPrimitives.MacMcpsConfirm = McpsConfirm; LoRaMacPrimitives.MacMcpsIndication = McpsIndication; LoRaMacPrimitives.MacMlmeConfirm = MlmeConfirm; LoRaMacPrimitives.MacMlmeIndication = MlmeIndication; LoRaMacCallbacks.GetBatteryLevel = BoardGetBatteryLevel; LoRaMacInitialization( &LoRaMacPrimitives, &LoRaMacCallbacks, ACTIVE_REGION ); TimerInit( &TxNextPacketTimer, OnTxNextPacketTimerEvent ); TimerInit( &Led1Timer, OnLed1TimerEvent ); TimerSetValue( &Led1Timer, 25 ); TimerInit( &Led2Timer, OnLed2TimerEvent ); TimerSetValue( &Led2Timer, 25 ); mibReq.Type = MIB_ADR; mibReq.Param.AdrEnable = LORAWAN_ADR_ON; LoRaMacMibSetRequestConfirm( &mibReq ); mibReq.Type = MIB_PUBLIC_NETWORK; mibReq.Param.EnablePublicNetwork = LORAWAN_PUBLIC_NETWORK; LoRaMacMibSetRequestConfirm( &mibReq ); #if defined( REGION_EU868 ) LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON ); #endif mibReq.Type = MIB_DEVICE_CLASS; mibReq.Param.Class = CLASS_C; LoRaMacMibSetRequestConfirm( &mibReq ); DeviceState = DEVICE_STATE_JOIN; break; } case DEVICE_STATE_JOIN: { #if( OVER_THE_AIR_ACTIVATION != 0 ) MlmeReq_t mlmeReq; // Initialize LoRaMac device unique ID BoardGetUniqueId( DevEui ); mlmeReq.Type = MLME_JOIN; mlmeReq.Req.Join.DevEui = DevEui; mlmeReq.Req.Join.AppEui = AppEui; mlmeReq.Req.Join.AppKey = AppKey; mlmeReq.Req.Join.Datarate = LORAWAN_DEFAULT_DATARATE; if( LoRaMacMlmeRequest( &mlmeReq ) == LORAMAC_STATUS_OK ) { DeviceState = DEVICE_STATE_SLEEP; } else { DeviceState = DEVICE_STATE_CYCLE; } #else // Choose a random device address if not already defined in Commissioning.h if( DevAddr == 0 ) { // Random seed initialization srand1( BoardGetRandomSeed( ) ); // Choose a random device address DevAddr = randr( 0, 0x01FFFFFF ); } mibReq.Type = MIB_NET_ID; mibReq.Param.NetID = LORAWAN_NETWORK_ID; LoRaMacMibSetRequestConfirm( &mibReq ); mibReq.Type = MIB_DEV_ADDR; mibReq.Param.DevAddr = DevAddr; LoRaMacMibSetRequestConfirm( &mibReq ); mibReq.Type = MIB_NWK_SKEY; mibReq.Param.NwkSKey = NwkSKey; LoRaMacMibSetRequestConfirm( &mibReq ); mibReq.Type = MIB_APP_SKEY; mibReq.Param.AppSKey = AppSKey; LoRaMacMibSetRequestConfirm( &mibReq ); mibReq.Type = MIB_NETWORK_JOINED; mibReq.Param.IsNetworkJoined = true; LoRaMacMibSetRequestConfirm( &mibReq ); DeviceState = DEVICE_STATE_SEND; #endif break; } case DEVICE_STATE_SEND: { if( NextTx == true ) { PrepareTxFrame( AppPort ); NextTx = SendFrame( ); } if( ComplianceTest.Running == true ) { // Schedule next packet transmission TxDutyCycleTime = 5000; // 5000 ms } else { // Schedule next packet transmission TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND ); } DeviceState = DEVICE_STATE_CYCLE; break; } case DEVICE_STATE_CYCLE: { DeviceState = DEVICE_STATE_SLEEP; // Schedule next packet transmission TimerSetValue( &TxNextPacketTimer, TxDutyCycleTime ); TimerStart( &TxNextPacketTimer ); break; } case DEVICE_STATE_SLEEP: { // Wake up through events TimerLowPowerHandler( ); // Process Radio IRQ Radio.IrqProcess( ); break; } default: { DeviceState = DEVICE_STATE_INIT; break; } } } }
/** * Main application entry point. */ int main( void ) { // LC1 LC2 LC3 LC4 LC5 LC6 LC7 LC8 const uint32_t channelsFreq[] = { 868100000, 868300000, 868500000, 868650000, 868800000, 869100000, 869250000, 869400000 }; const uint8_t channelsDatarate[] = { DR_SF7, DR_SF10, DR_SF12 }; uint8_t channelNb = ( sizeof( channelsFreq ) / sizeof( uint32_t ) ); uint8_t tstState = 0; int16_t pktCnt = 15; ChannelParams_t channel; LoRaMacHeader_t macHdr; LoRaMacFrameCtrl_t fCtrl; uint8_t channelsIndex = 0; uint8_t datarateIndex = 0; BoardInitMcu( ); BoardInitPeriph( ); // Initialize LoRaMac device unique ID BoardGetUniqueId( DevEui ); LoRaMacEvents.MacEvent = OnMacEvent; LoRaMacInit( &LoRaMacEvents ); IsNetworkJoined = false; #if( OVER_THE_AIR_ACTIVATION == 0 ) // Random seed initialization srand( RAND_SEED ); // Choose a random device address // NwkID = 0 // NwkAddr rand [0, 33554431] DevAddr = randr( 0, 0x01FFFFFF ); LoRaMacInitNwkIds( 0x000000, DevAddr, NwkSKey, AppSKey ); IsNetworkJoined = true; #else // Sends a JoinReq Command every 5 seconds until the network is joined TimerInit( &JoinReqTimer, OnJoinReqTimerEvent ); TimerSetValue( &JoinReqTimer, OVER_THE_AIR_ACTIVATION_DUTYCYCLE ); #endif TxNextPacket = true; TimerInit( &TxNextPacketTimer, OnTxNextPacketTimerEvent ); TimerInit( &Led1Timer, OnLed1TimerEvent ); TimerSetValue( &Led1Timer, 25000 ); // Low power timer to be run when tests are finished. TimerInit( &StopTimer, OnStopTimerEvent ); TimerSetValue( &StopTimer, 3.6e9 ); // wakes up the microcontroller every hour // Initialize MAC frame macHdr.Value = 0; #if defined( LORAMAC_R3 ) macHdr.Bits.MType = FRAME_TYPE_DATA_UNCONFIRMED_UP; #else macHdr.Bits.MType = FRAME_TYPE_DATA_UNCONFIRMED; #endif fCtrl.Value = 0; fCtrl.Bits.OptionsLength = 0; fCtrl.Bits.FPending = 0; fCtrl.Bits.Ack = false; fCtrl.Bits.AdrAckReq = false; fCtrl.Bits.Adr = false; // Initialize channel channel.DrRange.Fields.Min = DR_SF12; channel.DrRange.Fields.Min = DR_SF7; channel.DutyCycle = 0; LoRaMacSetChannelsTxPower( TX_POWER_14_DBM ); // Disable reception windows opening LoRaMacTestRxWindowsOn( false ); while( 1 ) { while( IsNetworkJoined == false ) { #if( OVER_THE_AIR_ACTIVATION != 0 ) if( TxNextPacket == true ) { TxNextPacket = false; LoRaMacJoinReq( DevEui, AppEui, AppKey ); // Relaunch timer for next trial TimerStart( &JoinReqTimer ); } TimerLowPowerHandler( ); #endif } for( datarateIndex = 0; datarateIndex < 3; datarateIndex++ ) { //for( channelsIndex = 0; channelsIndex < 3; channelsIndex++ ) { pktCnt = 15 * channelNb; while( pktCnt > 0 ) { switch( tstState ) { case 0: // Init AppData[0] = SelectorGetValue( ); channel.Frequency = channelsFreq[channelsIndex]; LoRaMacSetChannelsDatarate( channelsDatarate[datarateIndex] ); LoRaMacSendOnChannel( channel, &macHdr, &fCtrl, NULL, 15, AppData, 1 ); // Switch LED 1 ON GpioWrite( &Led1, 0 ); TimerStart( &Led1Timer ); channelsIndex = ( channelsIndex + 1 ) % channelNb; tstState = 1; break; case 1: // Wait for end of transmission if( Led1TimerEvent == true ) { Led1TimerEvent = false; // Switch LED 1 OFF GpioWrite( &Led1, 1 ); } if( TxDone == true ) { TxDone = false; pktCnt--; // Schedule next packet transmission after 100 ms TimerSetValue( &TxNextPacketTimer, 100000 ); TimerStart( &TxNextPacketTimer ); tstState = 2; } break; case 2: // Wait for next packet timer to expire if( TxNextPacket == true ) { TxNextPacket = false; tstState = 0; } break; } TimerLowPowerHandler( ); } } } TimerStart( &StopTimer ); while( 1 ) // Reset device to restart { if( StopTimerEvent == true ) { StopTimerEvent = false; TimerStart( &StopTimer ); } TimerLowPowerHandler( ); } } }