Exemplo n.º 1
0
void run_star_demo(void)
{
    #if defined(PROTOCOL_STAR)
        t1 = MiWi_TickGet();
        LCDDisplay((char *)"Sleeping!!", 0, false);
        while(1)
        {
           
            t2 = MiWi_TickGet();    // Calculate the Time for Sleeping device

            if((MiWi_TickGetDiff(t2,t1) > (ONE_SECOND * 20)))
            {
              awake = true;
              #if !defined(EIGHT_BIT_WIRELESS_BOARD)
              LCD_BacklightON();
              #endif
      
              MiApp_TransceiverPowerState(POWER_STATE_WAKEUP_DR);
              LCDDisplay((char *)"Woke Up!!!", 0, false);
              DELAY_ms(1000);
              STAR_DEMO_OPTIONS_MESSAGE (false);
              tt1 = MiWi_TickGet();  
            }
            while(awake)   
            {
                tt2 = MiWi_TickGet();    
                if((MiWi_TickGetDiff(tt2,tt1) > (ONE_SECOND * 10)))   
                {
                    // The Indirect Message for a Sleeping RFD is stored in PAN CO 
                    // We periodically send Data Requests for the Indirect Message to 
                    // not loose a message. 
                    CheckForData();
                    tt1 = MiWi_TickGet();
                }    
                /*******************************************************************/
                // Function MiApp_MessageAvailable returns a boolean to indicate if 
                // a packet has been received by the transceiver. If a packet has 
                // been received, all information will be stored in the rxFrame, 
                // structure of RECEIVED_MESSAGE.
                /*******************************************************************/

                if( MiApp_MessageAvailable())
                {

                    /*******************************************************************/
                    // If a packet has been received, update the information available 
                    // in rxMessage.
                    /*******************************************************************/
                    DemoOutput_UpdateTxRx(TxNum, ++RxNum);
                    DELAY_ms(2000);
                    // Toggle LED2 to indicate receiving a packet.
                    LED_2 ^= 1;

                    /*******************************************************************/
                    // Function MiApp_DiscardMessage is used to release the current 
                    //  received packet.
                    // After calling this function, the stack can start to process the
                    //  next received frame 
                    /*******************************************************************/        
                    MiApp_DiscardMessage();



                    /****************************************/
                }
                else
                {
                    /*******************************************************************/
                    // If no packet received, now we can check if we want to send out
                    // any information.
                    // Function ButtonPressed will return if any of the two buttons
                    // has been pushed.
                    /*******************************************************************/
                    uint8_t PressedButton = ButtonPressed();
                    if ( PressedButton == 1 || PressedButton == 2)
                    {
                        
                        uint8_t select_ed =0;
                        bool update_ed = true;
                        while(update_ed == true)
                        {

                            //User Selected Change end device
                            LCD_Erase();
                            if (myConnectionIndex_in_PanCo  == select_ed)
                            {   // if END_device displays itself , "me" is added in display to denote itself 
                                sprintf((char *)LCDText, (char*)"RB0:%02d-%02x%02x%02x-me",END_DEVICES_Short_Address[select_ed].connection_slot,END_DEVICES_Short_Address[select_ed].Address[0],
                                        END_DEVICES_Short_Address[select_ed].Address[1],END_DEVICES_Short_Address[select_ed].Address[2] );
                            }
                            else
                            {
                                sprintf((char *)LCDText, (char*)"RB0:%02d-%02x%02x%02x",END_DEVICES_Short_Address[select_ed].connection_slot,END_DEVICES_Short_Address[select_ed].Address[0],
                                        END_DEVICES_Short_Address[select_ed].Address[1],END_DEVICES_Short_Address[select_ed].Address[2] );
                            }
                            sprintf((char *)LCDText, (char*)"RB0:%02d-%02x%02x%02x",END_DEVICES_Short_Address[select_ed].connection_slot,END_DEVICES_Short_Address[select_ed].Address[0],
                                        END_DEVICES_Short_Address[select_ed].Address[1],END_DEVICES_Short_Address[select_ed].Address[2] );
                            sprintf((char *)&(LCDText[16]), (char*)"RB2: Change node");
                            LCD_Update();
                            chk_sel_status = true;
                            bool sw_layer_ack_status , mac_ack_status;
                            while(chk_sel_status)
                            {
                                uint8_t switch_val = ButtonPressed();
                                if(switch_val == 1)
                                {
                                    update_ed = false;
                                    chk_sel_status = false;
                                    update_ed = false;
                                    // Star_User_Data is defined in star_demo.c 
                                    // Its user data  , in case of Star Network 60 bytes of 
                                    // Data can be sent at a time from one END_DEVICE_TO_ANOTHER 
                                    // Edx --> Pan CO --> EDy
                                    if (myConnectionIndex_in_PanCo == select_ed)
                                    {
                                        MiApp_FlushTx();
                                        for (i = 0 ; i < 21 ; i++)
                                        {
                                            MiApp_WriteData(MiWi[(TxSynCount%6)][i]);
                                        }

                                        // IF on the demo , a END_Device displays its own Connection Detail
                                        // We unicast data packet to just PAN COR , No forwarding
                                        #if defined(ENABLE_SECURITY)
                                            mac_ack_status = MiApp_UnicastConnection (0, true);
                                        #else
                                            mac_ack_status = MiApp_UnicastConnection (0, false);
                                        #endif
                                            TxNum++;
                                    }
                                    else
                                    {
                                        // Data can be sent at a time from one END_DEVICE_TO_ANOTHER 
                                        // Edx --> Pan CO --> EDy
                                        // To forward a Packet from one ED to another ED , the first 4 bytes should holding
                                        // a CMD and end dest device short address (3 bytes)
                                        MiApp_FlushTx();
                                        MiApp_WriteData(CMD_FORWRD_PACKET);
                                        MiApp_WriteData(END_DEVICES_Short_Address[select_ed].Address[0]);// sending the first byte payload as the destination nodes
                                        MiApp_WriteData(END_DEVICES_Short_Address[select_ed].Address[1]);// sending the first byte payload as the destination nodes
                                        MiApp_WriteData(END_DEVICES_Short_Address[select_ed].Address[2]);// sending the first byte payload as the destination nodes
                                        for (i = 4 ; i < 25 ; i++)
                                        {
                                            MiApp_WriteData(MiWi[(TxSynCount%6)][i-4]);
                                        }
                                        #if defined(ENABLE_SECURITY)
                                            sw_layer_ack_status = MiApp_UnicastStar (true);
                                        #else
                                            sw_layer_ack_status = MiApp_UnicastStar (false);
                                        #endif


                                        #if defined(ENABLE_APP_LAYER_ACK)
                                            if (sw_layer_ack_status)
                                            {
                                                TxNum++;     // Tx was successful
                                            }
                                            else
                                            {
                                                LCDDisplay((char *)"Data_Sending_Fail!!", 0, false); 
                                            }
                                        #else    
                                            TxNum++;
                                        #endif

                                    }

                                } // end of switch_val == 1
                                else if(switch_val == 2)
                                {
                                    if (select_ed > end_nodes-1)
                                    {    
                                        select_ed = 0;
                                    }
                                    else
                                    {
                                        select_ed = select_ed+1;
                                    }
                                    chk_sel_status = false;

                                } // end of switch_val == 2
                            }  // end of chk_sel_status

                        } // end of updating the LCD info
                        STAR_DEMO_OPTIONS_MESSAGE (false);
                    } // end of actions on button press
                } // end of check for user inputs (button press check)

                t3 = MiWi_TickGet();
                if((MiWi_TickGetDiff(t3,t2) > (ONE_SECOND * 40)))
                {
                    awake = false;
                    #if !defined(EIGHT_BIT_WIRELESS_BOARD)
                              LCD_BacklightOFF();
                    #endif
                    MiApp_TransceiverPowerState(POWER_STATE_SLEEP);
                    LCDDisplay((char *)"Sleeping!!!", 0, false);
                    DELAY_ms(1000);
                    t1 = t3;

                }
                
                if (lost_connection && !role)
                {
                    MiApp_EstablishConnection(0xFF, CONN_MODE_DIRECT);
                    lost_connection = false;

                }

            }  // end of actions performed while node is awake
            
           

            
        } // end of while(1)
    #endif
}
Exemplo n.º 2
0
int main(void)
#endif
{   
    BYTE i, j;
    BYTE OperatingChannel = 0xFF;
    BYTE TxSynCount = 0;
    BYTE TxSynCount2 = 0;
    BYTE TxPersistFailures = 0;
    BOOL ReadyToSleep = FALSE;
    BYTE TxNum = 0;
    BYTE RxNum = 0;
    BYTE PressedButton = 0;
    
    /*******************************************************************/
    // Initialize the system
    /*******************************************************************/
    BoardInit();         
    ConsoleInit();  
    DemoOutput_Greeting();

    /*********************************************************************/
    // Function MiApp_ProtocolInit intialize the protocol stack.
    // The return value is a boolean to indicate the status of the 
    //      operation.
    // The only parameter indicates if Network Freezer should be invoked.
    //      When Network Freezer feature is invoked, all previous network
    //      configurations will be restored to the states before the 
    //      reset or power cycle
    //
    // In this example, we assume that the user wants to apply Network
    //      Freezer feature and restore the network configuration if
    //      button 1 is pressed and hold when powering up.
    /*********************************************************************/
    if( (PUSH_BUTTON_1 == 0) && ( MiApp_ProtocolInit(TRUE) == TRUE ) )
    {  
        DemoOutput_NetworkFreezer();
        while(PUSH_BUTTON_1 == 0 );
    }
    else
    {
        /*********************************************************************/
        // Function MiApp_ProtocolInit intialize the protocol stack.
        // The return value is a boolean to indicate the status of the 
        //      operation.
        // The only parameter indicates if Network Freezer should be invoked.
        //      When Network Freezer feature is invoked, all previous network
        //      configurations will be restored to the states before the 
        //      reset or power cycle
        //
        // In this example, we assume that the user wants to start from
        // scratch and ignore previous network configuration if button 1
        // is released when powering up.
        /*********************************************************************/
        MiApp_ProtocolInit(FALSE);   
        DemoOutput_StartActiveScan();
            
        while(1)
        {
            /*********************************************************************/
            // Function MiApp_SearchConnection will return the number of existing 
            // connections in all channels. It will help to decide which channel 
            // to operate on and which connection to add
            // The return value is the number of connections. The connection data
            //     are stored in global variable ActiveScanResults. Maximum active
            //     scan result is defined as ACTIVE_SCAN_RESULT_SIZE
            // The first parameter is the scan duration, which has the same 
            //     definition in Energy Scan. 10 is roughly 1 second. 9 is a half 
            //     second and 11 is 2 seconds. Maximum scan duration is 14, or 
            //     roughly 16 seconds.
            // The second parameter is the channel map. Bit 0 of the double
            //     word parameter represents channel 0. For the 2.4GHz frequency
            //     band, all possible channels are channel 11 to channel 26.
            //     As the result, the bit map is 0x07FFF800. Stack will filter
            //     out all invalid channels, so the application only needs to pay
            //     attention to the channels that are not preferred.
            /*********************************************************************/
            j = MiApp_SearchConnection(10, 0xFFFFFFFF);
            OperatingChannel = DemoOutput_ActiveScanResults(j);
        
            if( OperatingChannel != 0xFF )
            {
                /*******************************************************************/
                // Function MiApp_SetChannel assign the operation channel(frequency)
                // for the transceiver. Channels 0 - 31 has been defined for the 
                // wireless protocols, but not all channels are valid for all 
                // transceivers, depending on their hardware design, operating
                // frequency band, data rate and other RF parameters. Check the
                // definition of FULL_CHANNEL_MAP for details.
                /*******************************************************************/
                MiApp_SetChannel(OperatingChannel);
                break;
            }
            DemoOutput_Rescan();
        }

        
        /*********************************************************************/
        // Function MiApp_ConnectionMode sets the connection mode for the 
        // protocol stack. Possible connection modes are:
        //  - ENABLE_ALL_CONN       accept all connection request
        //  - ENABLE_PREV_CONN      accept only known device to connect
        //  - ENABL_ACTIVE_SCAN_RSP do not accept connection request, but allow
        //                          response to active scan
        //  - DISABLE_ALL_CONN      disable all connection request, including
        //                          active scan request
        /*********************************************************************/
        MiApp_ConnectionMode(ENABLE_ALL_CONN);
        
        /*******************************************************************/
        // Function MiApp_EstablishConnection establish connections between
        // two devices. It has two input parameters:
        // The first parameter is the index of the target device in the 
        //     active scan table. It requires a MiApp_SearchConnection call
        //     before hand. If seraching connection is not performed in 
        //     advance, user can apply 0xFF to the first parameter to 
        //     indicate that it is OK to establish connection with any 
        //     device.
        // The second parameter is the connection mode, either directly or
        //     indirectly. Direct connection is a connection in the radio
        //     range. All protocol stack support this connection mode. 
        //     Indirect connection is the connection out of radio range. 
        //     An indirect connection has to rely on other device to route 
        //     the messages between two connected devices. Indirect 
        //     connection is also called "Socket" connection in MiWi 
        //     Protocol. Since MiWi P2P protocol only handles connection 
        //     of one hop, indirect connection is not supported in MiWi 
        //     P2P protocol, but supported in other networking protocols.
        // Function MiApp_EstablishConnection returns the index of the 
        //     connected device in the connection table. If no connection 
        //     is established after predefined retry times 
        //     CONNECTION_RETRY_TIMES, it will return 0xFF. If multiple 
        //     connections have been established, it will return the one 
        //     of the indexes of the connected device.
        /*******************************************************************/
        i = MiApp_EstablishConnection(0, CONN_MODE_DIRECT);
        DemoOutput_Connected();
    }
    
    // Turn on LED 1 to indicate connection established
    LED_1 = 1;
    
    /*******************************************************************/
    // Function DumpConnection is used to print out the content of the
    // P2P Connection Entry on the hyperterminal. It may be useful in 
    // the debugging phase.
    // The only parameter of this function is the index of the 
    // Connection Entry. The value of 0xFF means to print out all
    // valid Connection Entries; otherwise, the Connection Entry
    // of the input index will be printed out.
    /*******************************************************************/
    DumpConnection(0xFF);
    
    DemoOutput_Instruction();

    while(1)
    {   
        /*******************************************************************/
        // Function MiApp_MessageAvailable will return a boolean to indicate 
        // if a message for application layer has been received by the 
        // transceiver. If a message has been received, all information will 
        // be stored in rxMessage, structure of RECEIVED_MESSAGE.
        /*******************************************************************/
        if( MiApp_MessageAvailable() )
        {
            DemoOutput_HandleMessage();
            
            /*******************************************************************/
            // Function MiApp_DiscardMessage is used to release the current 
            // received message. After calling this function, the stack can 
            // start to process the next received message.
            /*******************************************************************/ 
            MiApp_DiscardMessage();
            
            // Toggle LED2 to indicate receiving a packet.
            LED_2 ^= 1;
            
            DemoOutput_UpdateTxRx(TxNum, ++RxNum);
        }
        else 
        {
            /***********************************************************************/
            // TxPersistFailures is the local variable to track the transmission 
            // failure because no acknowledgement frame is received. Typically,
            // this is the indication of either very strong noise, or the PAN
            // has hopped to another channel. 
            /***********************************************************************/
            if( TxPersistFailures > 3 )
            {
                DemoOutput_StartResync();

                /*******************************************************************/
                // Function MiApp_TransceiverPowerState is used to set the power state
                // of RF transceiver. There are three possible states:
                //   - POWER_STATE_SLEEP        Put transceiver into sleep
                //   - POWER_STATE_WAKEUP       Wake up the transceiver only
                //   - POWER_STATE_WAKEUP_DR    Wake up and send Data Request command
                /*******************************************************************/
                MiApp_TransceiverPowerState(POWER_STATE_WAKEUP);
                
                /***********************************************************************/
                // Function MiApp_ResyncConnection is used to synchronized connection
                // if one side of communication jumped to another channel, when 
                // frequency agility is performed. Usually, this is done by the 
                // sleeping device, since the sleeping device cannot hear the broadcast 
                // of channel hopping command.
                //
                // The first parameter is the index of connection table for the peer 
                // node, which we would like to resynchronize to.
                // The second parameter is the bit map of channels to be scanned
                /***********************************************************************/
                MiApp_ResyncConnection(0, 0xFFFFFFFF);    
                TxPersistFailures = 0;
                ReadyToSleep = FALSE;
                
                DemoOutput_EndResync();
            }
            else
            {
                ReadyToSleep = TRUE;
            }    
            
            /*******************************************************************/
            // If Data Request command and data transmision has been handled,
            // as the RFD device, it is time to consider put both the radio and 
            // MCU into sleep mode to conserve power.
            /*******************************************************************/
            if( ReadyToSleep )
            {   
                ReadyToSleep = FALSE;
                
                /*******************************************************************/
                // Function MiApp_TransceiverPowerState is used to set the power 
                // state of RF transceiver. There are three possible states:
                //   - POWER_STATE_SLEEP        Put transceiver into sleep
                //   - POWER_STATE_WAKEUP       Wake up the transceiver only
                //   - POWER_STATE_WAKEUP_DR    Wake up and send Data Request 
                //                              command
                /*******************************************************************/
                MiApp_TransceiverPowerState(POWER_STATE_SLEEP);
                                    
                /*******************************************************************/
                // Prepare the condition to wake up the MCU. The MCU can either be
                // waken up by the timeout of watch dog timer (Time Synchronization 
                // off), timeout of counter with external 32KHz crystal (Time 
                // Synchronization on), or by the pin change notification interrupt 
                // by pushing the button. MCU handling of sleep preparing for 
                // different demo boards can be found in HardwareProfile.c
                /*******************************************************************/
                PrepareWakeup();
                
                // Put MCU into sleep mode
                Sleep();
                
                /*******************************************************************/
                // Handling the wakeup procedure. The steps differ for different
                // demo boards and families of MCUs. Details can be found in 
                // HardwareProfile.c
                /*******************************************************************/
                AfterWakeup();
                
                PressedButton = ButtonPressed();   
                
                /*******************************************************************/
                // Function MiApp_TransceiverPowerState is used to set the power 
                // state of RF transceiver. There are three possible states:
                //   - POWER_STATE_SLEEP        Put transceiver into sleep
                //   - POWER_STATE_WAKEUP       Wake up the transceiver only
                //   - POWER_STATE_WAKEUP_DR    Wake up and send Data Request 
                //                              command
                /*******************************************************************/
                if( MiApp_TransceiverPowerState(POWER_STATE_WAKEUP_DR) > SUCCESS )
                {
                    TxPersistFailures++;
                    DemoOutput_ConnectionLost(TxPersistFailures);
                }
                else
                {
                    if( TxPersistFailures > 0 )
                    {
                        DemoOutput_UpdateTxRx(TxNum, RxNum);
                    }
                    TxPersistFailures = 0;
                }
                
                switch( PressedButton )
                {
                    case 1: 
                        /*******************************************************************/                
                        // Button 1 pressed.
                        // First use MiApp_FlushTx to reset the Transmit buffer. Then fill 
                        // the TX buffer by calling function MiApp_WriteData
                        /*******************************************************************/
                        MiApp_FlushTx();
                        for(i = 0; i < 21; i++)
                        {
                            MiApp_WriteData(MiWi[(TxSynCount%6)][i]);
                        }
                        TxSynCount++;
                        
                        /*******************************************************************/
                        // Function MiApp_BroadcastPacket is used to broadcast a message
                        //    The only parameter is the boolean to indicate if we need to
                        //    secure the message
                        /*******************************************************************/
                        MiApp_BroadcastPacket(FALSE);
                        DemoOutput_UpdateTxRx(++TxNum, RxNum);   
                        break;
      
                    case 2:
                        /*******************************************************************/                
                        // Button 2 pressed.
                        // First use MiApp_FlushTx to reset the Transmit buffer. Then fill 
                        // the TX buffer by calling function MiApp_WriteData
                        /*******************************************************************/
                        MiApp_FlushTx();                   
                        for(i = 0; i < 11; i++)
                        {
                            MiApp_WriteData(DE[(TxSynCount2%6)][i]);
                        }
                        TxSynCount2++;
                        
                        /*******************************************************************/
                        // Function MiApp_UnicastConnection is one of the functions to 
                        // unicast a message.
                        //    The first parameter is the index of connection table for 
                        //       the peer device. In this demo, since there are only two
                        //       devices involved, the peer device must be stored in the 
                        //       first Connection Entry in the connection table.
                        //    The second parameter is the boolean to indicate if we need 
                        //       to secure the frame. If encryption is applied, the 
                        //       security level and security key are defined in the 
                        //       configuration file for the transceiver
                        //
                        // Another way to unicast a message is by calling function
                        // MiApp_UnicastAddress. Instead of supplying the index of the 
                        // connection table of the peer device, this function requires the 
                        // input parameter of destination address directly.
                        /*******************************************************************/
                        if( MiApp_UnicastConnection(0, TRUE) == FALSE )
                        {
                            DemoOutput_UnicastFail();
                        }
                        else
                        {
                            TxNum++;
                        }

                        DemoOutput_UpdateTxRx(TxNum, RxNum);
                        break;
                        
                    default:
                        break;
                }
            }
        }
    }
}