예제 #1
0
int main( void )
{
    // Target board initialisation
    BoardInitMcu();
    LOG_DEBUG("Mcu initialized.");
    OSA_Init();
    LOG_DEBUG("OS initialized.");
    BoardInitPeriph();
    LOG_DEBUG("Peripherals initialized.");

    // These tasks will not start in BM.
    s_result = OSA_TaskCreate(task_led_rtos, (uint8_t *) "led_rtos", TASK_LED_RTOS_STACK_SIZE,
            task_led_rtos_stack, TASK_LED_RTOS_PRIO, (task_param_t) 0, false,
            &task_led_rtos_task_handler);
    if ( s_result != kStatus_OSA_Success ) {
        LOG_ERROR("Failed to create led_rtos task");
    }

    s_result = OSA_TaskCreate(task_fxos_rtos, (uint8_t *) "fxos_rtos", TASK_FXOS_RTOS_STACK_SIZE,
            task_fxos_rtos_stack, TASK_FXOS_RTOS_PRIO, (task_param_t) 0, false,
            &task_fxos_rtos_task_handler);
    if ( s_result != kStatus_OSA_Success ) {
        LOG_ERROR("Failed to create fxos_rtos task");
    }

    // Print the initial banner
    LOG_DEBUG("Hello World!\r\n");

    OSA_Start();

    for ( ;; ) {
    }                    // Should not achieve here
}
예제 #2
0
/*! Main application entry point. */
int main( void )
{
    BoardInitMcu();
    LOG_DEBUG_BARE("\r\n");
    LOG_DEBUG("Mcu initialized.");
    BoardInitPeriph();
    LOG_DEBUG("Peripherals initialized.");
#if defined( USE_SHELL )
    Shell_AppInit ();
#endif /* USE_SHELL */

    LoRaMesh_AppInit();

    /* Reset heartBeatCntr */
    heartBeatCntr = 0;
    heartBeatLedOn = false;

    vTaskStartScheduler();

    LOG_ERROR("Failed to create idle task. Probably out of memory.");

    for ( ;; ) {
        /* Should not be reached */
    }
}
예제 #3
0
파일: board.c 프로젝트: juliocdep/contiki
void BoardInitMcu( void )
{
    if( McuInitialized == false )
    {
        // We use IRQ priority group 4 for the entire project
        // When setting the IRQ, only the preemption priority is used
        NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );

        // Disable Systick
//        SysTick->CTRL  &= ~SysTick_CTRL_TICKINT_Msk;    // Systick IRQ off
//        SCB->ICSR |= SCB_ICSR_PENDSTCLR_Msk;            // Clear SysTick Exception pending flag

        /* Setup SysTick Timer for 1 ms interrupts (not too often to save power) */
        if (SysTick_Config(SystemCoreClock / 1000))
		{
			/* Capture error */
			while (1);
		}

        I2cInit( &I2c, I2C_SCL, I2C_SDA );

        SpiInit( &SX1276.Spi, RADIO_MOSI, RADIO_MISO, RADIO_SCLK, NC );
        SX1276IoInit( );

#if defined( USE_DEBUG_PINS )
        GpioInit( &DbgPin1, J1_1, PIN_OUTPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
        GpioInit( &DbgPin2, J1_2, PIN_OUTPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
        GpioInit( &DbgPin3, J1_3, PIN_OUTPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
        GpioInit( &DbgPin4, J1_4, PIN_OUTPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
#endif
        BoardInitPeriph( );

#if( LOW_POWER_MODE_ENABLE )
        TimerSetLowPowerEnable( true );
#else
        TimerSetLowPowerEnable( false );
#endif
        BoardUnusedIoInit( );

        if( TimerGetLowPowerEnable( ) == true )
        {
            RtcInit( );
        }
        else
        {
            TimerHwInit( );
        }
        McuInitialized = true;
    }
}
예제 #4
0
void BoardInitMcu( void )
{
    if( McuInitialized == false )
    {
        // We use IRQ priority group 4 for the entire project
        // When setting the IRQ, only the preemption priority is used
        NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );

        // Disable Systick
        SysTick->CTRL  &= ~SysTick_CTRL_TICKINT_Msk;    // Systick IRQ off 
        SCB->ICSR |= SCB_ICSR_PENDSTCLR_Msk;            // Clear SysTick Exception pending flag

        I2cInit( &I2c, I2C_SCL, I2C_SDA );

        SpiInit( &SX1272.Spi, RADIO_MOSI, RADIO_MISO, RADIO_SCLK, NC );
        SX1272IoInit( );

#if defined( USE_DEBUG_PINS )
        GpioInit( &DbgPin1, J5_1, PIN_OUTPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
        GpioInit( &DbgPin2, J5_2, PIN_OUTPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
        GpioInit( &DbgPin3, J5_3, PIN_OUTPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
        GpioInit( &DbgPin4, J5_4, PIN_OUTPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
#endif
        BoardInitPeriph( );

#if( LOW_POWER_MODE_ENABLE )
        TimerSetLowPowerEnable( true );
#else
        TimerSetLowPowerEnable( false );
#endif
        BoardUnusedIoInit( );

        if( TimerGetLowPowerEnable( ) == true )
        {
            RtcInit( );
        }
        else
        {
            TimerHwInit( );
        }
        McuInitialized = true;
    }
}
예제 #5
0
/*!
 * 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( );
    }
}
예제 #6
0
/**
 * Main application entry point.
 */
int main( void )
{
    // Target board initialisation
    BoardInitMcu( );
    BoardInitPeriph( );

    // Radio initialization
    Radio.Init( NULL );

    Radio.SetChannel( RF_FREQUENCY );
    /**********************************************/
    /*                  WARNING                   */
    /* The below settings can damage the chipset  */
    /* if wrongly used. DO NOT CHANGE THE VALUES! */
    /*                                            */
    /**********************************************/
#if ( defined( USE_BAND_433 ) || defined( USE_BAND_470 ) )

    Radio.Write( 0x01, 0x88 );
    Radio.Write( 0x3D, 0xA1 );
    Radio.Write( 0x36, 0x01 );
    Radio.Write( 0x1e, 0x08 );

#elif ( defined( USE_BAND_868 ) || defined( USE_BAND_915 ) )

    Radio.Write( 0x01, 0x80 );
    Radio.Write( 0x44, 0x7B );
    Radio.Write( 0x3D, 0xA1 );
    Radio.Write( 0x36, 0x01 );
    Radio.Write( 0x1e, 0x08 );
    Radio.Write( 0x45, 0xDF );
    Radio.Write( 0x46, 0x03 );
    Radio.Write( 0x4D, 0x87 );
    Radio.Write( 0x52, 0x60 );
        
#endif

    Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
                                   LORA_SPREADING_FACTOR, LORA_CODINGRATE,
                                   LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
                                   true, LORA_IQ_INVERSION_ON, 3000000 );

    TimerInit( &Led1Timer, OnLed1TimerEvent ); 
    TimerSetValue( &Led1Timer, 90000 );

    TimerInit( &Led2Timer, OnLed2TimerEvent ); 
    TimerSetValue( &Led2Timer, 90000 );

    TimerInit( &Led3Timer, OnLed3TimerEvent ); 
    TimerSetValue( &Led3Timer, 90000 );
               
    // Switch LED 1 ON
    GpioWrite( &Led1, 0 );
    TimerStart( &Led1Timer );
    
    // Sets the radio in Tx mode
    Radio.Send( NULL, 0 );

    // Blink LEDs just to show some activity
    while( 1 )
    {
        if( Led1TimerEvent == true )
        {
            Led1TimerEvent = false;
            
            // Switch LED 1 OFF
            GpioWrite( &Led1, 1 );
            // Switch LED 2 ON
            GpioWrite( &Led2, 0 );
            TimerStart( &Led2Timer );
        }

        if( Led2TimerEvent == true )
        {
            Led2TimerEvent = false;
            
            // Switch LED 2 OFF
            GpioWrite( &Led2, 1 );
            // Switch LED 3 ON
            GpioWrite( &Led3, 0 );
            TimerStart( &Led3Timer );
        }
    
        if( Led3TimerEvent == true )
        {
            Led3TimerEvent = false;
            
            // Switch LED 3 OFF
            GpioWrite( &Led3, 1 );
            // Switch LED 1 ON
            GpioWrite( &Led1, 0 );
            TimerStart( &Led1Timer );
        }    
    }
}
예제 #7
0
파일: main.c 프로젝트: ARMmbed/LoRaMac-node
/**
 * 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( );
    
    }
}
예제 #8
0
/**
 * Main application entry point.
 */
int main( void )

{

#if( OVER_THE_AIR_ACTIVATION != 0 )
    uint8_t sendFrameStatus = 0;
#endif
    bool trySendingFrameAgain = false;
 //   float tempLightValue = 0;   
    
//    LightMode = 0;      // 0: manual,   1: automatic
    buzzer = 0;         // 0: OFF,      1: ON
    bar.setLevel(0);
    debug( "\n\n\r    LoRaWAN Class A Demo code  \n\n\r" );
    
    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]
    if( ( DevAddr == 0 ) || ( DevAddr == 0xFFFFFFFF ) )
    {
        // Generate random DevAddr if it does not exist
        debug("Generate random DevAddr\n\r");
        DevAddr = randr( 0, 0x01FFFFFF );
    }
    debug( "- DevAddr = 0x%x\n\r" , DevAddr);    
    LoRaMacInitNwkIds( 0x000000, DevAddr, NwkSKey, AppSKey );
    IsNetworkJoined = true;
#endif

    TxNextPacket = true;

    LoRaMacSetAdrOn( false );    
    LoRaMacSetDutyCycleOn( false );   
    

     
while( 1 )
    {
        while( IsNetworkJoined == false )
        {
#if( OVER_THE_AIR_ACTIVATION != 0 )
            if( TxNextPacket == true )
            {
                TxNextPacket = false;
                
                sendFrameStatus = LoRaMacJoinReq( DevEui, AppEui, AppKey );
                debug("Req Sent\n\r");
                switch( sendFrameStatus )
                {
                case 1: // BUSY
                    break;
                case 0: // OK
                case 2: // NO_NETWORK_JOINED
                case 3: // LENGTH_PORT_ERROR
                case 4: // MAC_CMD_ERROR
                case 6: // DEVICE_OFF
                default:
                    // Relaunch timer for next trial
                    JoinReqTimer.attach_us( &OnJoinReqTimerEvent, OVER_THE_AIR_ACTIVATION_DUTYCYCLE );
                    break;
                }
            }
//            TimerLowPowerHandler( );
#endif
        }

        if( TxDone == true )
        {
            
            TxDone = false;
            
            debug( "TxDone \n\n\r" );
            // Schedule next packet transmission
            TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND );
            TxNextPacketTimer.attach_us( &OnTxNextPacketTimerEvent, TxDutyCycleTime );
        }

        if( trySendingFrameAgain == true )
        {
            trySendingFrameAgain = SendFrame( );
        }
        
        if( TxNextPacket == true )
        {       
            TxNextPacketTimer.detach( );
            
            TxNextPacket = false;
        
            PrepareTxFrame( AppPort );
            
            trySendingFrameAgain = SendFrame( );
        }




        /* Read light sensor
        tempLightValue = LightSens.read( ) * 1.65;
        
        LightValue = ( 1 - tempLightValue );
        
        // Set automatic RGB from light sensor
        if( LightMode == 0 )
        {
            color_led.setColorRGB( 0, ( uint8_t )( 255 * LightValue ), ( uint8_t )( 255 * LightValue ), ( uint8_t )( 255 * LightValue ) );
        }*/
//        TimerLowPowerHandler( );
    }
}
예제 #9
0
/**
 * 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;
            }
        }
    }
}
예제 #10
0
/**
 * Main application entry point.
 */
int main(void)
{
    static uint32_t cnt[] = { 0, 0, 0 };
// Target board initialisation
    BoardInitMcu();
    PRINTF("\r\n\r\nTRACE: Mcu initialized.\r\n");
    BoardInitPeriph();
    PRINTF("TRACE: Peripherals initialized.\r\n");

// Radio initialization
    RadioEvents.TxDone = OnTxDone;
    RadioEvents.RxDone = OnRxDone;
    RadioEvents.TxTimeout = OnTxTimeout;
    RadioEvents.RxTimeout = OnRxTimeout;
    RadioEvents.RxError = OnRxError;
    RadioEvents.CadDone = OnCadDone;

    txDone = false;
	cadDone = false;
	channelActivityDetected = false;
	rxDone = false;
	sendPacket = false;

	SelectedChannel = 1;

    Radio.Init(&RadioEvents);
    PRINTF("TRACE: Radio initialized.\r\n");

    Radio.SetChannel(Channels[SelectedChannel]);

#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, 3000000 );

    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, 3000000 );

    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

    srand1 (BoardGetRandomSeed() );
#if 0
    PRINTF("Start channel activity detection...\r\n");
    Radio.StartCad();

    while (1) {
        if (cadDone) {
            cadDone = false;
            if (channelActivityDetected) {
                PRINTF("Channel activity detected (%u. on channel %u).\r\n", ++cnt[SelectedChannel],
                        Channels[SelectedChannel]);
                DelayMs(100); /* Wait for Rx1 to open */
                Radio.SetChannel(Channels[0]); /* Set to default channel */
                Radio.Send(TxBuffer, TxBufferSize);
                while (!txDone) {
                };
                txDone = false;
                SelectedChannel = randr(0, 2);
                PRINTF("Channel activity detection on channel %u activated.\r\n",
                        Channels[SelectedChannel]);
                Radio.SetChannel(Channels[SelectedChannel]);
            }
            DelayMs(50);
            Radio.StartCad();
        }
    }
#else
    PRINTF("Start package receiving...\r\n");
	Radio.Rx(0);

	while(1){
		if(rxDone) {
			rxDone = false;
//			TimerSetValue(&TxPacketTimer, RECEIVE_DELAY1);
//			TimerStart(&TxPacketTimer);
			PRINTF("TRACE: %s - Incoming message. %u bytes. (%d/%d)\r\n", __FUNCTION__, RxBufferSize, Rssi, Snr);
			for(uint8_t i = 0; i < RxBufferSize; i++) {
				PRINTF("%02x", RxBuffer[i]);
			}
			PRINTF("\r\n");
			Radio.Rx(0);
		}
	}
#endif
}
예제 #11
0
파일: main.c 프로젝트: hollie/LoRaMac-node
/**
 * 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( );
	}
}
예제 #12
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( );
        }
    }
}