Пример #1
0
/**
 * 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( );
	}
}
Пример #2
0
/**
 * Main application entry point.
 */
int main( void )
{
    bool isMaster = true;
    uint8_t i;

    // Target board initialization
    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
                        GpioToggle( &Led1 );

                        // 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;
                        GpioToggle( &Led2 ); // 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
                        GpioToggle( &Led1 );

                        // 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]
            GpioToggle( &Led2 );
            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( );
        // Process Radio IRQ
        Radio.IrqProcess( );
    }
}
Пример #3
0
/**
 * 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( );
        }
    }
}