Exemplo n.º 1
0
//*****************************************************************************
//
//! \brief Task Created by main fucntion.This task starts simpleink, set NWP
//!        power policy, connects to an AP. Give Signal to the other task about
//!        the connection.wait for the message form the interrupt handlers and
//!        the other task. Accordingly print the wake up cause from the low
//!        power modes.
//!
//! \param pvParameters is a general void pointer (not used here).
//!
//! \return none
//
//*****************************************************************************
void TimerGPIOTask(void *pvParameters)
{
    cc_hndl tTimerHndl = NULL; 
    cc_hndl tGPIOHndl = NULL;
    unsigned char ucQueueMsg = 0;
    unsigned char ucSyncMsg = 0;
    int iRetVal = 0;
    //
    // Displays the Application Banner
    //
    DisplayBanner();
    
    //
    // creating the queue for signalling about connection events
    //
    iRetVal = osi_MsgQCreate(&g_tConnection, NULL, sizeof( unsigned char ), 3);
    if (iRetVal < 0)
    {
        UART_PRINT("unable to create the msg queue\n\r");
        LOOP_FOREVER();
    }
    
    //
    // starting the simplelink
    //
    iRetVal = sl_Start(NULL, NULL, NULL);
    if (iRetVal < 0)
    {
        UART_PRINT("Failed to start the device \n\r");
        LOOP_FOREVER();
    }

    //
    // Swtich to STA mode if device is not
    //
    SwitchToStaMode(iRetVal);
    
    //
    // Set the power management policy of NWP
    //
    iRetVal = sl_WlanPolicySet(SL_POLICY_PM, SL_NORMAL_POLICY, NULL, 0);
    if (iRetVal < 0)
    {
        UART_PRINT("unable to configure network power policy\n\r");
        LOOP_FOREVER();
    }

    //
    // connecting to the Access Point
    //
    if(-1 == WlanConnect())
    {
        sl_Stop(SL_STOP_TIMEOUT);
        UART_PRINT("Connection to AP failed\n\r");
    }
    else
    {
        UART_PRINT("Connected to AP\n\r");
        //
        //signal the other task about the sl start and connection to the AP
        //
        iRetVal = osi_MsgQWrite(&g_tConnectionFlag, &ucSyncMsg,
                                OSI_WAIT_FOREVER);
        if (iRetVal < 0)
        {
            UART_PRINT("unable to create the msg queue\n\r");
            LOOP_FOREVER();
        }
    }
    
    //
    // Queue management related configurations
    //
    iRetVal = osi_MsgQCreate(&g_tWkupSignalQueue, NULL,
                             sizeof( unsigned char ), 10);
    if (iRetVal < 0)
    {
        UART_PRINT("unable to create the msg queue\n\r");
        LOOP_FOREVER();
    }

    //
    // setting Timer as one of the wakeup source
    //
    tTimerHndl = SetTimerAsWkUp();
    
    //
    // setting some GPIO as one of the wakeup source
    //
    tGPIOHndl = SetGPIOAsWkUp();
    
    /* handles, if required, can be used to stop the timer, but not used here*/
    UNUSED(tTimerHndl);
    UNUSED(tGPIOHndl);
    //
    // setting Apps power policy
    //
    lp3p0_setup_power_policy(POWER_POLICY_STANDBY);
    
    while(FOREVER)
    {
        //
        // waits for the message from the various interrupt handlers(GPIO,
        // Timer) and the UDPServerTask.
        //
        osi_MsgQRead(&g_tWkupSignalQueue, &ucQueueMsg, OSI_WAIT_FOREVER);
        switch(ucQueueMsg){
        case 1:
            UART_PRINT("timer\n\r");
            break;
        case 2:
            UART_PRINT("GPIO\n\r");
            break;
        case 3:
            UART_PRINT("host irq\n\r");
            break;
        default:
            UART_PRINT("invalid msg\n\r");
            break;
        }
    }
}
Exemplo n.º 2
0
//*****************************************************************************
//
//! \brief Task Created by main fucntion. This task prints the wake up reason
//!        (from hibernate or from restart). start simplelink, set NWP power
//!        policy and connects to an AP. Creates UDP client and send UDP
//!        packets at around 1Mbit/sec for certain time. Disconnect form AP
//!        and stops the simplelink.Setup GPIO and Timer as wakeup source from
//!        low power modes. Go into HIBernate.
//!
//! \param pvParameters is a general void pointer (not used here).
//!
//! \return none
//
//*****************************************************************************
void TimerGPIOTask(void *pvParameters)
{
    cc_hndl tTimerHndl;
    cc_hndl tGPIOHndl;
    int iSockDesc = 0;
    int iRetVal = 0;
    int iCounter = 0;
    sockaddr_in sServerAddr;
    unsigned char *pcSendBuff;
    unsigned char cSyncMsg;

    //
    // creating the queue for signalling about connection events
    //
    iRetVal = osi_MsgQCreate(&g_tConnection, NULL, sizeof( unsigned char ), 3);
    if (iRetVal < 0)
    {
        UART_PRINT("unable to create the msg queue\n\r");
        LOOP_FOREVER();
    }
    
    // filling the buffer
    for (iCounter=0 ; iCounter<BUFF_SIZE ; iCounter++)
    {
        g_cBsdBuf[iCounter] = (char)(iCounter % 10);
    }
    pcSendBuff = g_cBsdBuf;

    if(MAP_PRCMSysResetCauseGet() == PRCM_POWER_ON)
    {
        //
        // Displays the Application Banner
        //
        DisplayBanner();
        
        //
        // starting the simplelink
        //
        iRetVal = sl_Start(NULL, NULL, NULL);
        if (iRetVal < 0)
        {
            UART_PRINT("Failed to start the device \n\r");
            LOOP_FOREVER();
        }

        //
        // Switch to STA mode if device is not in this mode
        //
        SwitchToStaMode(iRetVal);

        //
        // Set the power management policy of NWP
        //
        iRetVal = sl_WlanPolicySet(SL_POLICY_PM, SL_NORMAL_POLICY, NULL, 0);
        if (iRetVal < 0)
        {
            UART_PRINT("unable to configure network power policy\n\r");
            LOOP_FOREVER();
        }
    }
    else if(MAP_PRCMSysResetCauseGet() == PRCM_HIB_EXIT)
    {
        UART_PRINT("woken from hib\n\r");
        //
        // starting the simplelink
        //
        iRetVal = sl_Start(NULL, NULL, NULL);
        if (iRetVal < 0)
        {
            UART_PRINT("Failed to start the device \n\r");
            LOOP_FOREVER();
        }
    }
    else if(MAP_PRCMSysResetCauseGet() == PRCM_WDT_RESET)
    {
        UART_PRINT("woken from WDT Reset\n\r");
        //
        // starting the simplelink
        //
        iRetVal = sl_Start(NULL, NULL, NULL);
        if (iRetVal < 0)
        {
            UART_PRINT("Failed to start the device \n\r");
            LOOP_FOREVER();
        }
    }
    else
    {
        UART_PRINT("woken cause unknown\n\r");
    }
    
    //
    // connecting to the Access Point
    //
    if(-1 == WlanConnect())
    {
        UART_PRINT("Connection to AP failed\n\r");
        goto no_network_connection;
    }else{
        UART_PRINT("Connected to AP\n\r");
    }
    
    //
    // creating a UDP socket
    //
    iSockDesc = sl_Socket(SL_AF_INET,SL_SOCK_DGRAM, 0);

    if(iSockDesc < 0)
    {
        UART_PRINT("sock error\n\r");
        LOOP_FOREVER();
    }

    //
    // configure the UDP Server address
    //
    sServerAddr.sin_family = SL_AF_INET;
    sServerAddr.sin_port = sl_Htons(APP_UDP_PORT);
    sServerAddr.sin_addr.s_addr = sl_Htonl(SERVER_IP_ADDRESS);

    //
    // Set 5 sec timer allowing 5 sec of UDP Tx.
    //
    tTimerHndl = SetTimer();

    g_ucTrafficEnable = 1;
    while(g_ucTrafficEnable == 1)
    {
        //
        // sending message
        //
        iRetVal = sendto(iSockDesc, pcSendBuff,BUFF_SIZE, 0,
                        (struct sockaddr *)&sServerAddr,sizeof(sServerAddr));
        if(iRetVal < 0)
        {
            UART_PRINT("send error\n\r");
            LOOP_FOREVER();
        }
        ManageDelay(128,BUFF_SIZE);
    }
    UART_PRINT("sent\n\r");

    //
    // stop and delete the timer
    //
    cc_timer_stop(tTimerHndl);
    cc_timer_delete(tTimerHndl);

    //
    //close the socket
    //
    close(iSockDesc);

    if(iRetVal < 0)
    {
        UART_PRINT("could not close the socket\n\r");
    }
    
    //
    // disconnect from the Access Point
    //
    WlanDisconnect();

no_network_connection:
    //
    // stop the simplelink with reqd. timeout value (30 ms)
    //
    sl_Stop(SL_STOP_TIMEOUT);

    //
    // setting Timer as one of the wakeup source
    //
    tTimerHndl = SetTimerAsWkUp();

    //
    // setting some GPIO as one of the wakeup source
    //
    tGPIOHndl = SetGPIOAsWkUp();

    /* handles, if required, can be used to stop the timer, but not used here*/
    UNUSED(tTimerHndl);
    UNUSED(tGPIOHndl);

    //
    // Setting up HIBERNATE as the lowest power mode for the system.
    //
    lp3p0_setup_power_policy(POWER_POLICY_HIBERNATE);

    //
    // idle wait will push the system into the lowest power mode(HIBERNATE).
    //
    iRetVal = osi_MsgQCreate(&g_tWaitForHib, NULL, sizeof( unsigned char ), 1);
    if (iRetVal < 0)
    {
           UART_PRINT("unable to create the msg queue\n\r");
           LOOP_FOREVER();
    }
    osi_MsgQRead(&g_tWaitForHib, &cSyncMsg, OSI_WAIT_FOREVER);

    //
    // infinite loop (must not reach here)
    //
    LOOP_FOREVER();

}