Exemplo n.º 1
0
/*******************************************************************************
* Function Name: WCO_LowPowerStart
********************************************************************************
* Summary:
*  Start WCO in low power mode by configuring the system in DeepSleep mode during
*  WCO startup time(500ms)
*
* Parameters:
*  None
*
* Return:
*  None
*
*******************************************************************************/
void WCO_ECO_LowPowerStart(void)
{
    WDT_WcoEcoLpStartSetup();   /* Setup WDT counters to enable low power WCO & ECO startup */

    CySysClkWcoStart();         /* Start the WCO clock */

    WDT_EnableWcoCounter();     /* Enable WDT's WCO counter (counter 0) */

#if DEBUG_ENABLE
    DeepSleep_Write(1);
#endif /* End of #if DEBUG_ENABLE */
    CySysPmDeepSleep(); /* Wait for the WDT counter 0 interrupt to wake up the device. On wakeup WCO is up & running */
#if DEBUG_ENABLE
    DeepSleep_Write(0);
#endif /* End of #if DEBUG_ENABLE */

    (void)CySysClkWcoSetPowerMode(CY_SYS_CLK_WCO_LPM);      /* Switch WCO to the low power mode after startup */
    CySysClkSetLfclkSource(CY_SYS_CLK_LFCLK_SRC_WCO);       /* LFCLK is now driven by WCO */
    CySysClkIloStop();                                      /* WCO is running, shut down the ILO */

    (void)CySysClkEcoStart(0);  /* It's time to start ECO */

    WDT_EnableEcoCounter();     /* Enable WDT's ECO counter (counter 1) */

#if TIMING_DEBUG_ENABLE
    DeepSleep_Write(1);
#endif
    CySysPmDeepSleep();  /* Wait for the WDT counter 1 interrupt to wake up the device. On wakeup ECO is up & running */
#if TIMING_DEBUG_ENABLE
    DeepSleep_Write(0);
#endif

    WDT_DisableWcoEcoCounters();
}
Exemplo n.º 2
0
/*******************************************************************************
* Function Name: LowPowerImplementation()
********************************************************************************
* Summary:
* Implements low power in the project.
*
* Parameters:
* None
*
* Return:
* None
*
* Theory:
* The function tries to enter deep sleep as much as possible - whenever the 
* BLE is idle and the UART transmission/reception is not happening. At all other
* times, the function tries to enter CPU sleep.
*
*******************************************************************************/
static void LowPowerImplementation(void)
{
    CYBLE_LP_MODE_T bleMode;
    uint8 interruptStatus;
    
    /* For advertising and connected states, implement deep sleep 
     * functionality to achieve low power in the system. For more details
     * on the low power implementation, refer to the Low Power Application 
     * Note.
     */
    if((CyBle_GetState() == CYBLE_STATE_ADVERTISING) || 
       (CyBle_GetState() == CYBLE_STATE_CONNECTED))
    {
        /* Request BLE subsystem to enter into Deep-Sleep mode between connection and advertising intervals */
        bleMode = CyBle_EnterLPM(CYBLE_BLESS_DEEPSLEEP);
        /* Disable global interrupts */
        interruptStatus = CyEnterCriticalSection();
        /* When BLE subsystem has been put into Deep-Sleep mode */
        if(bleMode == CYBLE_BLESS_DEEPSLEEP)
        {
            /* And it is still there or ECO is on */
            if((CyBle_GetBleSsState() == CYBLE_BLESS_STATE_ECO_ON) || 
               (CyBle_GetBleSsState() == CYBLE_BLESS_STATE_DEEPSLEEP))
            {
            #if (DEBUG_UART_ENABLED == ENABLED)
                /* Put the CPU into the Deep-Sleep mode when all debug information has been sent */
                if((UART_DEB_SpiUartGetTxBufferSize() + UART_DEB_GET_TX_FIFO_SR_VALID) == 0u)
                {
                    CySysPmDeepSleep();
                }
                else /* Put the CPU into Sleep mode and let SCB to continue sending debug data */
                {
                    CySysPmSleep();
                }
            #else
                CySysPmDeepSleep();
            #endif /* (DEBUG_UART_ENABLED == ENABLED) */
            }
        }
        else /* When BLE subsystem has been put into Sleep mode or is active */
        {
            /* And hardware doesn't finish Tx/Rx opeation - put the CPU into Sleep mode */
            if(CyBle_GetBleSsState() != CYBLE_BLESS_STATE_EVENT_CLOSE)
            {
                CySysPmSleep();
            }
        }
        /* Enable global interrupt */
        CyExitCriticalSection(interruptStatus);
    }
}
Exemplo n.º 3
0
int main() {
    CYBLE_API_RESULT_T apiResult;
    uint32 count = 0;
    uint8   triggerNotification = 0;

    // Enable global interrupts
    CyGlobalIntEnable;
    
    // Initialize the watchdog timer
    CySysWdtSetIsrCallback(CY_SYS_WDT_COUNTER0, Watchdog0_cb);

    // Initialize the BLE device.
    apiResult = CyBle_Start(StackEventHandler);
    // Validate BLE stack initialization successed
    CYASSERT(apiResult == CYBLE_ERROR_OK);

    for (;;) {
        // Service all the BLE stack events.
        // Must be called at least once in a BLE connection interval
        CyBle_ProcessEvents();

        if (deviceConnected) {
            if (counterCccDescriptor.dirty) {
                // Update Counter CCCD
                updateCounterCccDescriptor();
            } else if (triggerNotification) {
                // Send notification if required
                if (enableCounterNotification) {
                    sendCounterNotification(count);
                }
                triggerNotification = 0;
            } else if (triggerUpdateCounter) {
                // Update counter value
                count++;
                updateCounter(count);
                triggerNotification = ((count & 0x0000000F) == 0);
                triggerUpdateCounter = 0;
            }
        }
        
        // Scan update queue
        if (rgbDescriptor.dirty) {
            // Update RGB Descriptor
            updateRgbDescriptor();
        }

        // Enter to deep sleep mode
        {
            CYBLE_LP_MODE_T state;

            state = CyBle_EnterLPM(CYBLE_BLESS_DEEPSLEEP);
            if (state == CYBLE_BLESS_DEEPSLEEP) {
                CySysPmDeepSleep();
            }
        }
    }
}
Exemplo n.º 4
0
int main() {
	Start_Tone_StartEx(Start_Tone_Custom);
	Buzzer_SetDriveMode(Buzzer_DM_DIG_HIZ);
	CyGlobalIntEnable;
    
	while (1) {
		CySysPmDeepSleep();
    }
}
Exemplo n.º 5
0
/*******************************************************************************
* Function Name: System_ManagePower
********************************************************************************
*
* Summary:
*  configures the peripherals used in the design and the device into possible
*  low power modes based on the state of the system.
*
* Parameters:  
*  None
*
* Return: 
*  None
*
*******************************************************************************/
void System_ManagePower (void)
{
    CYBLE_LP_MODE_T lpMode;
    CYBLE_BLESS_STATE_T blessState;
    uint32 intStatus;
    
    lpMode = CyBle_EnterLPM(CYBLE_BLESS_DEEPSLEEP); /* try configuring BLESS in DeepSleep mode */
        
    intStatus = CyEnterCriticalSection();
    
    blessState = CyBle_GetBleSsState(); /* Check the current state of the BLESS */

    if(lpMode == CYBLE_BLESS_DEEPSLEEP) /* BLESS had entered low power mode */
    {   
        /* and it continues to be in a state where BLESS can enter DeepSleep mode */
        if(blessState == CYBLE_BLESS_STATE_ECO_ON || blessState == CYBLE_BLESS_STATE_DEEPSLEEP)
        {
            #if(CONSOLE_LOG_ENABLED)
                /* Put the device into the Dee Sleep mode only when all debug information has been sent */
            if((Debug_Console_SpiUartGetTxBufferSize() + Debug_Console_GET_TX_FIFO_SR_VALID) == 0u)
            {
                CySysPmDeepSleep();
            }
            else
            {
                /* Wait for UART interface to finish data transfer */
                CySysPmSleep();
            }
            #else
            CySysPmDeepSleep();    
            #endif    
        }
    }
    else
    {
        /* BLESS can't enter DeepSleep as next BLE connection interval is close by, go to Sleep for now */
        if(blessState != CYBLE_BLESS_STATE_EVENT_CLOSE)
        {
            CySysPmSleep();
        }
    }
    
    CyExitCriticalSection(intStatus);
}
Exemplo n.º 6
0
/*******************************************************************************
* Function Name: LowPowerImplementation()
********************************************************************************
* Summary:
* Implements low power in the project.
*
* Parameters:
* None
*
* Return:
* None
*
* Theory:
* The function tries to enter deep sleep as much as possible - whenever the 
* BLE is idle and the UART transmission/reception is not happening. At all other
* times, the function tries to enter CPU sleep.
*
*******************************************************************************/
static void LowPowerImplementation(void)
{
    CYBLE_LP_MODE_T bleMode;
    uint8 interruptStatus;
    
    /* For advertising and connected states, implement deep sleep 
     * functionality to achieve low power in the system. For more details
     * on the low power implementation, refer to the Low Power Application 
     * Note.
     */
    if((CyBle_GetState() == CYBLE_STATE_ADVERTISING) || 
       (CyBle_GetState() == CYBLE_STATE_CONNECTED))
    {
        bleMode = CyBle_EnterLPM(CYBLE_BLESS_DEEPSLEEP);
        interruptStatus = CyEnterCriticalSection();
        if(bleMode == CYBLE_BLESS_DEEPSLEEP)
        {
            if((CyBle_GetBleSsState() == CYBLE_BLESS_STATE_ECO_ON) || 
               (CyBle_GetBleSsState() == CYBLE_BLESS_STATE_DEEPSLEEP))
            {
                /* Deep sleep only if UART completes transfer and we
                 * are not waiting for the user to enter anything.
                 */
                if(((UART_SpiUartGetTxBufferSize() + UART_GET_TX_FIFO_SR_VALID) == 0u) && 
                   (ancsUsageState != ANCS_USAGE_INCOMING_CALL_WAITING_FOR_INPUT))
                {
                    CySysPmDeepSleep();
                }
                else
                {
                    CySysPmSleep();
                }
            }
        }
        else
        {
            if(CyBle_GetBleSsState() != CYBLE_BLESS_STATE_EVENT_CLOSE)
            {
                CySysPmSleep();
            }
        }
        CyExitCriticalSection(interruptStatus);
    }
}
Exemplo n.º 7
0
/*******************************************************************************
* Function Name: EnterLowPowerMode
********************************************************************************
*
* Summary:
*  This configures the BLESS and system in low power mode whenever possible.
*
* Parameters:
*  None
*
* Return:
*  None
*
*******************************************************************************/
void EnterLowPowerMode(void)
{
    CYBLE_BLESS_STATE_T blessState;
    uint8 intrStatus;

    /* Configure BLESS in Deep-Sleep mode */
    CyBle_EnterLPM(CYBLE_BLESS_DEEPSLEEP);

    /* Prevent interrupts while entering system low power modes */
    intrStatus = CyEnterCriticalSection();

    /* Get the current state of BLESS block */
    blessState = CyBle_GetBleSsState();

    /* If BLESS is in Deep-Sleep mode or the XTAL oscillator is turning on,
     * then PSoC 4 BLE can enter Deep-Sleep mode (1.3uA current consumption) */
    if(blessState == CYBLE_BLESS_STATE_ECO_ON ||
            blessState == CYBLE_BLESS_STATE_DEEPSLEEP)
    {
        CySysPmDeepSleep();
    }
    else if(blessState != CYBLE_BLESS_STATE_EVENT_CLOSE)
    {
        /* If BLESS is active, then configure PSoC 4 BLE system in
         * Sleep mode (~1.6mA current consumption) */
        CySysClkWriteHfclkDirect(CY_SYS_CLK_HFCLK_ECO);
        CySysClkImoStop();
        CySysPmSleep();
        CySysClkImoStart();
        CySysClkWriteHfclkDirect(CY_SYS_CLK_HFCLK_IMO);
    }
    else
    {
        /* Keep trying to enter either Sleep or Deep-Sleep mode */
    }

    CyExitCriticalSection(intrStatus);
}
Exemplo n.º 8
0
void LowPower(void)
{
    CYBLE_LP_MODE_T pwrState;
    CYBLE_BLESS_STATE_T blessState;
    uint8 intStatus = 0;

    pwrState  = CyBle_EnterLPM(CYBLE_BLESS_DEEPSLEEP); /* Configure BLESS in Deep-Sleep mode */

    intStatus = CyEnterCriticalSection(); /* No interrupts allowed while entering system low power modes */
        blessState = CyBle_GetBleSsState();

        if(pwrState == CYBLE_BLESS_DEEPSLEEP) /* Make sure BLESS is in Deep-Sleep before configuring system in Deep-Sleep */
        {
            if(blessState == CYBLE_BLESS_STATE_ECO_ON || blessState == CYBLE_BLESS_STATE_DEEPSLEEP)
            {
                CySysPmDeepSleep(); /* System Deep-Sleep. 1.3uA mode */
            }
        }
        else if (blessState != CYBLE_BLESS_STATE_EVENT_CLOSE)
        {
             /* Change HF clock source from IMO to ECO, as IMO can be stopped to save power */
            CySysClkWriteHfclkDirect(CY_SYS_CLK_HFCLK_ECO); 
            
            /* Stop IMO for reducing power consumption */
            CySysClkImoStop(); 
            
            /* Put the CPU to Sleep. 1.1mA mode */
            CySysPmSleep();
            
            /* Starts execution after waking up, start IMO */
            CySysClkImoStart();
            
            /* Change HF clock source back to IMO */
            CySysClkWriteHfclkDirect(CY_SYS_CLK_HFCLK_IMO);
        }
    CyExitCriticalSection(intStatus);
}
Exemplo n.º 9
0
/*******************************************************************************
* Function Name: System_ManagePower()
********************************************************************************
*
* Summary:
*   This function puts the system in appropriate low power modes based on the 
*   state of BLESS and application power state.
*
* Parameters:
*  none
*
********************************************************************************/
inline void System_ManagePower()
{
    /* Variable declarations */
    CYBLE_BLESS_STATE_T blePower;
    uint8 interruptStatus ;
    
   /* Disable global interrupts to avoid any other tasks from interrupting this section of code*/
    interruptStatus  = CyEnterCriticalSection();
    
    /* Get current state of BLE sub system to check if it has successfully entered deep sleep state */
    blePower = CyBle_GetBleSsState();
    
    /* System can enter DeepSleep only when BLESS and rest of the application are in DeepSleep or equivalent
     * power modes */
    if((blePower == CYBLE_BLESS_STATE_DEEPSLEEP || blePower == CYBLE_BLESS_STATE_ECO_ON) && 
        Application_GetPowerState() == DEEPSLEEP)
    {
        Application_SetPowerState(WAKEUP_DEEPSLEEP);
        
#if DEBUG_ENABLE
        DeepSleep_Write(1);
#endif /* End of #if DEBUG_ENABLE */
        
        CySysPmDeepSleep();
        
#if DEBUG_ENABLE
        DeepSleep_Write(0);
#endif /* End of #if DEBUG_ENABLE */
    }
    else if((blePower != CYBLE_BLESS_STATE_EVENT_CLOSE))
    {
        if(Application_GetPowerState() == DEEPSLEEP)
        {
            Application_SetPowerState(WAKEUP_DEEPSLEEP);
            
            /* change HF clock source from IMO to ECO, as IMO can be stopped to save power */
            CySysClkWriteHfclkDirect(CY_SYS_CLK_HFCLK_ECO); 
            /* stop IMO for reducing power consumption */
            CySysClkImoStop(); 
            /* put the CPU to sleep */
#if DEBUG_ENABLE
            Sleep_Write(1);
#endif /* End of #if DEBUG_ENABLE */      

            CySysPmSleep();
            
#if DEBUG_ENABLE
            Sleep_Write(0);
#endif /* End of #if DEBUG_ENABLE */           
            /* starts execution after waking up, start IMO */
            CySysClkImoStart();
            /* change HF clock source back to IMO */
            CySysClkWriteHfclkDirect(CY_SYS_CLK_HFCLK_IMO);
        }
        else if(Application_GetPowerState() == SLEEP )
        {
            /* If the application is using IMO for its operation, we shouldn't switch over the HFCLK source */
            Application_SetPowerState(WAKEUP_SLEEP);
            
#if DEBUG_ENABLE
            Sleep_Write(1);
#endif /* End of #if DEBUG_ENABLE */     

            CySysPmSleep();
            
#if DEBUG_ENABLE
            Sleep_Write(0);
#endif /* End of #if DEBUG_ENABLE */
        }
    }
    
    /* Enable interrupts */
    CyExitCriticalSection(interruptStatus );
}
Exemplo n.º 10
0
int main()
{
    CYBLE_LP_MODE_T lpMode;
    CYBLE_BLESS_STATE_T blessState;
    CYBLE_STACK_LIB_VERSION_T stackVersion;

    CyGlobalIntEnable;

    UART_DEB_Start();               /* Start communication component */
    printf("BLE Heart Rate Collector Example Project \r\n");

    Disconnect_LED_Write(LED_OFF);
    Scanning_LED_Write(LED_OFF);
    Notification_LED_Write(LED_OFF);

    apiResult = CyBle_Start(AppCallBack);
    if(apiResult != CYBLE_ERROR_OK)
    {
        printf("CyBle_Start API Error: 0x%x \r\n", apiResult);
    }

    apiResult = CyBle_GetStackLibraryVersion(&stackVersion);
    if(apiResult != CYBLE_ERROR_OK)
    {
        printf("CyBle_GetStackLibraryVersion API Error: 0x%x \r\n", apiResult);
    }
    else
    {
        printf("Stack Version: %d.%d.%d.%d \r\n", stackVersion.majorVersion,
               stackVersion.minorVersion, stackVersion.patch, stackVersion.buildNumber);
    }

    CyBle_BasRegisterAttrCallback(BasCallBack);
    HrsInit();

    while(1)
    {
        if(CyBle_GetState() != CYBLE_STATE_INITIALIZING)
        {
            /* Enter DeepSleep mode between connection intervals */
            lpMode = CyBle_EnterLPM(CYBLE_BLESS_DEEPSLEEP);
            CyGlobalIntDisable;
            blessState = CyBle_GetBleSsState();

            if(lpMode == CYBLE_BLESS_DEEPSLEEP)
            {
                if(blessState == CYBLE_BLESS_STATE_ECO_ON || blessState == CYBLE_BLESS_STATE_DEEPSLEEP)
                {
                    /* Put the device into the DeepSleep mode only when all debug information has been sent */
                    if((UART_DEB_SpiUartGetTxBufferSize() + UART_DEB_GET_TX_FIFO_SR_VALID) == 0u)
                    {
                        CySysPmDeepSleep();
                    }
                    else
                    {
                        CySysPmSleep();
                    }
                }
            }
            else
            {
                if(blessState != CYBLE_BLESS_STATE_EVENT_CLOSE)
                {
                    CySysPmSleep();
                }
            }
            CyGlobalIntEnable;

            /* Handle advertising led blinking */
            HandleLeds();
        }

        /* Store bonding data to flash only when all debug information has been sent */
        if((cyBle_pendingFlashWrite != 0u) &&
                ((UART_DEB_SpiUartGetTxBufferSize() + UART_DEB_GET_TX_FIFO_SR_VALID) == 0u))
        {
            apiResult = CyBle_StoreBondingData(0u);
            printf("Store bonding data, status: %x \r\n", apiResult);
        }


        /*******************************************************************
        *  Processes all pending BLE events in the stack
        *******************************************************************/
        CyBle_ProcessEvents();
    }
}
Exemplo n.º 11
0
int main()
{
    const char8 serialNumber[] = SERIAL_NUMBER;
    CYBLE_LP_MODE_T lpMode;
    CYBLE_BLESS_STATE_T blessState;

    packetRXFlag = 0u;

    DBG_PRINT_TEXT("\r\n");
    DBG_PRINT_TEXT("\r\n");
    DBG_PRINT_TEXT("===============================================================================\r\n");
    DBG_PRINT_TEXT("=              BLE_External_Memory_Bootloadable Application Started            \r\n");
    DBG_PRINT_TEXT("=              Version: 1.0                                                    \r\n");
#if (LED_ADV_COLOR == LED_GREEN)
    DBG_PRINT_TEXT("=              Code: LED_GREEN                                                 \r\n");
#else
    DBG_PRINT_TEXT("=              Code: LED_BLUE                                                 \r\n");
#endif /*LED_ADV_COLOR == LED_GREEN*/
    DBG_PRINTF    ("=              Compile Date and Time : %s %s                                   \r\n", __DATE__,__TIME__);
#if (ENCRYPTION_ENABLED == YES)
    DBG_PRINT_TEXT("=              ENCRYPTION OPTION : ENABLED                                                \r\n");
#else
    DBG_PRINT_TEXT("=              ENCRYPTION OPTION : DISABLED                                               \r\n");
#endif /*LED_ADV_COLOR == LED_GREEN*/
#if (CI_PACKET_CHECKSUM_CRC == YES)
    DBG_PRINT_TEXT("=              PACKET CHECKSUM TYPE: CRC-16-CCITT                                         \r\n");
#else
    DBG_PRINT_TEXT("=              PACKET CHECKSUM TYPE: BASIC SUMMATION                                      \r\n");
#endif /*LED_ADV_COLOR == LED_GREEN*/

    DBG_PRINT_TEXT("===============================================================================\r\n");
    DBG_PRINT_TEXT("\r\n");
    DBG_PRINT_TEXT("\r\n");

    CyGlobalIntEnable;

    Bootloading_LED_Write(LED_OFF);
    Advertising_LED_1_Write(LED_OFF);
    Advertising_LED_2_Write(LED_OFF);


    CyBle_Start(AppCallBack);

    /*Initialization of encryption in BLE stack*/
#if (ENCRYPTION_ENABLED == YES)
    CR_Initialization();
#endif /*(ENCRYPTION_ENABLED == YES)*/


    /* Set Serial Number string not initialised in GUI */
    CyBle_DissSetCharacteristicValue(CYBLE_DIS_SERIAL_NUMBER, sizeof(serialNumber), (uint8 *)serialNumber);

    /* Disable bootloader service */
    CyBle_GattsDisableAttribute(cyBle_customs[0].customServiceHandle);

    /* Force client to rediscover services in range of bootloader service */
    WriteAttrServChanged();

    WDT_Start();

    while(1u == 1u)
    {
        if(CyBle_GetState() != CYBLE_STATE_INITIALIZING)
        {
            /* Enter DeepSleep mode between connection intervals */
            lpMode = CyBle_EnterLPM(CYBLE_BLESS_DEEPSLEEP);
            CyGlobalIntDisable;
            blessState = CyBle_GetBleSsState();

            if(lpMode == CYBLE_BLESS_DEEPSLEEP)
            {
                if(blessState == CYBLE_BLESS_STATE_ECO_ON || blessState == CYBLE_BLESS_STATE_DEEPSLEEP)
                {
                    CySysPmDeepSleep();
                }
            }
            else
            {
                if(blessState != CYBLE_BLESS_STATE_EVENT_CLOSE)
                {
                    CySysPmSleep();
                }
            }
            CyGlobalIntEnable;
        }

        CyBle_ProcessEvents();

        /* If key press event was detected - debounce it and switch to bootloader emulator mode */
        if (Bootloader_Service_Activation_Read() == 0u)
        {
            CyDelay(100u);

            if (Bootloader_Service_Activation_Read() == 0u)
            {
                DBG_PRINTF("Bootloader service activated!\r\n");
                CyBle_GattsEnableAttribute(cyBle_customs[0u].customServiceHandle);
                LED_WRITE_MACRO(LED_OFF);
                bootloadingMode = 1u;

                /* Force client to rediscover services in range of bootloader service */
                WriteAttrServChanged();

                BootloaderEmulator_Start();
            }
        }
    }
}
Exemplo n.º 12
0
Arquivo: main.c Projeto: bhwj/BLE
/*******************************************************************************
* Function Name: main
********************************************************************************
* Summary:
*        MyBeacon entry point. This calls the BLE and other peripheral Component
* APIs for achieving the desired system behaviour
*
* Parameters:
*  void
*
* Return:
*  int
*
*******************************************************************************/
int main()
{
    CyGlobalIntEnable;
    
    /* Set the divider for ECO, ECO will be used as source when IMO is switched off to save power, to drive the HFCLK */
    CySysClkWriteEcoDiv(CY_SYS_CLK_ECO_DIV8);
    
    /* If USE_WCO_FOR_TIMING is set, then do the following:
     * 1. Shut down the ECO (to reduce power consumption while WCO is starting)
     * 2. Enable WDT to wakeup the system after 500ms (WCO startup time). 
     * 3. Configure PSoC 4 BLE device in DeepSleep mode for the 500ms WCO startup time
     * 4. After WCO is enabled, restart the ECO so that BLESS interface can function */
#if USE_WCO_FOR_TIMING
    CySysClkEcoStop();
    
    WDT_Interrupt_StartEx(WDT_Handler);
    
    CySysClkWcoStart();
    
    CySysWdtUnlock(); /* Unlock the WDT registers for modification */
    
    CySysWdtWriteMode(SOURCE_COUNTER, CY_SYS_WDT_MODE_INT);
    
    CySysWdtWriteClearOnMatch(SOURCE_COUNTER, COUNTER_ENABLE);
    
    CySysWdtWriteMatch(SOURCE_COUNTER, COUNT_PERIOD);
    
    CySysWdtEnable(CY_SYS_WDT_COUNTER0_MASK);
    
    CySysWdtLock();
    
#if TIMING_DEBUG_ENABLE                    
    DeepSleep_Write(1);
#endif  

    CySysPmDeepSleep(); /* Wait for the WDT interrupt to wake up the device */
    
#if TIMING_DEBUG_ENABLE                    
    DeepSleep_Write(0);
#endif

    (void)CySysClkEcoStart(2000u);
    CyDelayUs(500u);

    (void)CySysClkWcoSetPowerMode(CY_SYS_CLK_WCO_LPM);    /* Switch to the low power mode */

    CySysClkSetLfclkSource(CY_SYS_CLK_LFCLK_SRC_WCO);

    CySysWdtUnlock();
    
    CySysWdtDisable(CY_SYS_WDT_COUNTER0_MASK);
    
    CySysWdtLock();
#endif
    
    CyBle_Start(BLE_AppEventHandler);
    
    for(;;)
    {
        CYBLE_LP_MODE_T pwrState;
        CYBLE_BLESS_STATE_T blessState;
        uint8 intStatus = 0;
        
        CyBle_ProcessEvents(); /* BLE stack processing state machine interface */
        
        pwrState  = CyBle_EnterLPM(CYBLE_BLESS_DEEPSLEEP); /* Configure BLESS in Deep-Sleep mode */

        intStatus = CyEnterCriticalSection(); /* No interrupts allowed while entering system low power modes */
        
        blessState = CyBle_GetBleSsState();

        if(pwrState == CYBLE_BLESS_DEEPSLEEP) /* Make sure BLESS is in Deep-Sleep before configuring system in Deep-Sleep */
        {
            if(blessState == CYBLE_BLESS_STATE_ECO_ON || blessState == CYBLE_BLESS_STATE_DEEPSLEEP)
            {
#if TIMING_DEBUG_ENABLE                    
                DeepSleep_Write(1);
#endif                

                CySysPmDeepSleep(); /* System Deep-Sleep. 1.3uA mode */
                
#if TIMING_DEBUG_ENABLE                    
                DeepSleep_Write(0);
#endif                 
            }
        }
        else if (blessState != CYBLE_BLESS_STATE_EVENT_CLOSE)
        {
             /* Change HF clock source from IMO to ECO, as IMO can be stopped to save power */
            CySysClkWriteHfclkDirect(CY_SYS_CLK_HFCLK_ECO); 
            
            /* Stop IMO for reducing power consumption */
            CySysClkImoStop(); 
            
#if TIMING_DEBUG_ENABLE            
            Sleep_Write(1);
#endif            
            /* Put the CPU to Sleep. 1.1mA mode */
            CySysPmSleep();
            
#if TIMING_DEBUG_ENABLE            
            Sleep_Write(0);
#endif            
            
            /* Starts execution after waking up, start IMO */
            CySysClkImoStart();
            
            /* Change HF clock source back to IMO */
            CySysClkWriteHfclkDirect(CY_SYS_CLK_HFCLK_IMO);
        }
        
        CyExitCriticalSection(intStatus);
    }
}
Exemplo n.º 13
0
int main()
{
    CYBLE_LP_MODE_T lpMode;
    CYBLE_BLESS_STATE_T blessState;

    CyGlobalIntEnable;

    UART_DEB_Start();               /* Start communication component */
    printf("BLE Secure Connection Example Project \r\n");

    Disconnect_LED_Write(LED_OFF);
    Advertising_LED_Write(LED_OFF);

    /* Start CYBLE component and register generic event handler */
    apiResult = CyBle_Start(AppCallBack);
    if(apiResult != CYBLE_ERROR_OK)
    {
        printf("CyBle_Start API Error: %x \r\n", apiResult);
    }

    /* Services initialization */
    HrsInit();

    /***************************************************************************
    * Main polling loop
    ***************************************************************************/
    while(1)
    {
        if(CyBle_GetState() != CYBLE_STATE_INITIALIZING)
        {
            /* Enter DeepSleep mode between connection intervals */
            lpMode = CyBle_EnterLPM(CYBLE_BLESS_DEEPSLEEP);
            CyGlobalIntDisable;
            blessState = CyBle_GetBleSsState();

            if(lpMode == CYBLE_BLESS_DEEPSLEEP)
            {
                if(blessState == CYBLE_BLESS_STATE_ECO_ON || blessState == CYBLE_BLESS_STATE_DEEPSLEEP)
                {
                    /* Put the device into the Deep Sleep mode only when all debug information has been sent */
                    if((UART_DEB_SpiUartGetTxBufferSize() + UART_DEB_GET_TX_FIFO_SR_VALID) == 0u)
                    {
                        CySysPmDeepSleep();
                    }
                    else
                    {
                        CySysPmSleep();
                    }
                }
            }
            else
            {
                if(blessState != CYBLE_BLESS_STATE_EVENT_CLOSE)
                {
                    CySysPmSleep();
                }
            }
            CyGlobalIntEnable;
        }


        /***********************************************************************
        * Wait for connection established with Central device
        ***********************************************************************/
        if(CyBle_GetState() == CYBLE_STATE_CONNECTED)
        {
            /*******************************************************************
            *  Periodically simulates heart beat and sends the results to the Client
            *******************************************************************/
            mainTimer++;

            if(mainTimer == MAIN_LOOP_SIMULATION_THRESHOLD)
            {
                mainTimer = 0u;
                if(heartRateSimulation == ENABLED)
                {
                    SimulateHeartRate();
                    CyBle_ProcessEvents();
                }
            }
            else if((cyBle_pendingFlashWrite != 0u) &&
                    ((UART_DEB_SpiUartGetTxBufferSize() + UART_DEB_GET_TX_FIFO_SR_VALID) == 0u))
            {
                apiResult = CyBle_StoreBondingData(0u);
                printf("Store bonding data, status: %x \r\n", apiResult);
            }
            else
            {
                /* nothing else */
            }
        }

        /*******************************************************************
        *  Process all pending BLE events in the stack
        *******************************************************************/
        CyBle_ProcessEvents();
    }
}
/*******************************************************************************
* Function Name: HandleLowPowerMode
********************************************************************************
* Summary:
*        This function puts the BLESS in deep sleep mode and CPU to sleep mode. 
* System will resume from here when it wakes from user button press.
*
* Parameters:
*  void
*
* Return:
*  void
*
*******************************************************************************/
void HandleLowPowerMode(uint8 lpmSel)
{
	#ifdef ENABLE_LOW_POWER_MODE
		/* Local variable to store the status of BLESS Hardware block */
		CYBLE_LP_MODE_T sleepMode;
		CYBLE_BLESS_STATE_T blessState;

        if (lpmSel == DEEPSLEEP)
        {
            /* Leave chip in Deep Sleep mode */
    		/* Put BLESS into Deep Sleep and check the return status */
    		sleepMode = CyBle_EnterLPM(CYBLE_BLESS_DEEPSLEEP);
    		
    		/* Disable global interrupt to prevent changes from any other interrupt ISR */
    		CyGlobalIntDisable;
    	
    		/* Check the Status of BLESS */
    		blessState = CyBle_GetBleSsState();

    		if(sleepMode == CYBLE_BLESS_DEEPSLEEP)
    		{
    		    /* If the ECO has started or the BLESS can go to Deep Sleep, then place CPU 
    			* to Deep Sleep */
    			if(blessState == CYBLE_BLESS_STATE_ECO_ON || blessState == CYBLE_BLESS_STATE_DEEPSLEEP)
    		    {
    				/* Place CPU to Deep sleep */
                    #ifdef ENABLE_LED_NOTIFICATION
                    GREEN_SetDriveMode(GREEN_DM_STRONG);
                    GREEN_Write(0);
                    #endif
                    
    		        CySysPmDeepSleep();
                    
                    #ifdef ENABLE_LED_NOTIFICATION
                    GREEN_SetDriveMode(GREEN_DM_STRONG);
                    GREEN_Write(1);
                    #endif
    		 	}
    		}
    		
    		/* Re-enable global interrupt mask after wakeup */
    		CyGlobalIntEnable;        
        }
#if 0
        else if (lpmSel == SLEEP)
        {
            /* Leave chip in Sleep mode */
            /* Leave chip in Deep Sleep mode */
    		/* Put BLESS into Deep Sleep and check the return status */
    		sleepMode = CyBle_EnterLPM(CYBLE_BLESS_SLEEP);
    		
    		/* Disable global interrupt to prevent changes from any other interrupt ISR */
    		CyGlobalIntDisable;
    	
    		/* Check the Status of BLESS */
    		blessState = CyBle_GetBleSsState();

    		if(sleepMode == CYBLE_BLESS_SLEEP)
    		{
    		    if(blessState != CYBLE_BLESS_STATE_EVENT_CLOSE)
    		    {
    				/* If BLE Event has not 
    				* closed yet, then place CPU to Sleep */
                    #ifdef ENABLE_LED_NOTIFICATION                   
                    BLUE_Write(0);
                    BLUE_SetDriveMode(BLUE_DM_STRONG);
                    #endif
                    
    		        CySysPmSleep();
                    
                    #ifdef ENABLE_LED_NOTIFICATION
                    BLUE_SetDriveMode(BLUE_DM_STRONG);
                    BLUE_Write(1);
                    #endif
    		    }
    		}
    		
    		/* Re-enable global interrupt mask after wakeup */
    		CyGlobalIntEnable;        
        }
        else if (lpmSel == ACTIVE)
        {
            /* Leave chip in Active mode */
            #ifdef ENABLE_LED_NOTIFICATION
            RED_Write(0);
            RED_SetDriveMode(RED_DM_STRONG);
            #endif
        }
#endif        
	#endif
}
Exemplo n.º 15
0
int main()
{
   
    /* Variable declarations */
    CYBLE_LP_MODE_T lpMode;
    CYBLE_BLESS_STATE_T blessState;
    uint8 InterruptsStatus;
      
   
    /* Start communication component */
    UART_Start();
    
    /* Enable global interrupts */
    CyGlobalIntEnable;
       
    /* Internal low power oscillator is stopped as it is not used in this project */
    CySysClkIloStop();
    
    /* Set the divider for ECO, ECO will be used as source when IMO is switched off to save power,
    **  to drive the HFCLK */
    CySysClkWriteEcoDiv(CY_SYS_CLK_ECO_DIV8);
    
    CyBle_Start(StackEventHandler);
   
    /*Infinite Loop*/
    for(;;)
    {
       
        if((UART_SpiUartGetTxBufferSize() + UART_GET_TX_FIFO_SR_VALID) == 0)
        {
            
           if(CyBle_GetState() != CYBLE_STATE_INITIALIZING)
           {
                /* Put BLE sub system in DeepSleep mode when it is idle */
                 lpMode = CyBle_EnterLPM(CYBLE_BLESS_DEEPSLEEP);
                
               /* Disable global interrupts to avoid any other tasks from interrupting this section of code*/
                InterruptsStatus = CyEnterCriticalSection();
                
                /* Get current state of BLE sub system to check if it has successfully entered deep sleep state */
                blessState = CyBle_GetBleSsState();

                /* If BLE sub system has entered deep sleep, put chip into deep sleep for reducing power consumption */
                if(lpMode == CYBLE_BLESS_DEEPSLEEP)
                {   
                    if(blessState == CYBLE_BLESS_STATE_ECO_ON || blessState == CYBLE_BLESS_STATE_DEEPSLEEP)
                    {
                       /* Put the chip into the deep sleep state as there are no pending tasks and BLE has also
                       ** successfully entered BLE DEEP SLEEP mode */
                       CySysPmDeepSleep();
                    }
                }
                
                /* BLE sub system has not entered deep sleep, wait for completion of radio operations */
                else if(blessState != CYBLE_BLESS_STATE_EVENT_CLOSE)
                {
                    
                    /* change HF clock source from IMO to ECO, as IMO can be stopped to save power */
                    CySysClkWriteHfclkDirect(CY_SYS_CLK_HFCLK_ECO); 
                    /* stop IMO for reducing power consumption */
                    CySysClkImoStop(); 
                    /* put the CPU to sleep */
                    CySysPmSleep();
                    /* starts execution after waking up, start IMO */
                    CySysClkImoStart();
                    /* change HF clock source back to IMO */
                    CySysClkWriteHfclkDirect(CY_SYS_CLK_HFCLK_IMO);
                    
                }
                
                /*Enable interrupts */
                CyExitCriticalSection(InterruptsStatus);
            
            }/*end of if(CyBle_GetState() != CYBLE_STATE_INITIALIZING)*/
             
            CyBle_ProcessEvents();
                        
        }   
    
    }
    
 }
Exemplo n.º 16
0
Arquivo: main.c Projeto: Shogun5/BLE
/*******************************************************************************
* Function Name: main
********************************************************************************
*
* Summary:
*  Main function.
*
* Parameters:  
*  None
*
* Return: 
*  None
*
*******************************************************************************/
int main()
{
    CYBLE_LP_MODE_T lpMode;
    CYBLE_BLESS_STATE_T blessState;
    CYBLE_API_RESULT_T apiResult = CYBLE_ERROR_OK;
  
    CyGlobalIntEnable; 

    /* ADD_CODE_TO - Start the BLE component and register StackEventHandler function */
    apiResult = CyBle_Start(StackEventHandler);

    if(apiResult != CYBLE_ERROR_OK)
    {
        /* BLE stack initialization failed, check your configuration */
        CYASSERT(0);
    }

    /* Register LLS event handler function */
    CyBle_LlsRegisterAttrCallback(LlsEventHandler);
        
    while(1)
    {
        /* Process all the pending BLE tasks. This single API call will service all 
         * the BLE stack events. This API MUST be called at least once per BLE connection interval */
        CyBle_ProcessEvents();

        if(CyBle_GetState() != CYBLE_STATE_INITIALIZING) /* Waiting for BLE component initialization */
        {
            static uint8 blinkTimeout = BLINK_TIMEOUT;

            /* Update link loss alert LED status based on IAS Alert level characteristic */
            if(CyBle_GetState() == CYBLE_STATE_DISCONNECTED || 
                (CyBle_GetState() == CYBLE_STATE_CONNECTED && CyBle_GetRssi() > RSSI_THRESHOLD))
            {
                switch(linkAlertLevel)
                {
                    case NO_ALERT:
                        Link_Alert_LED_Write(LED_OFF);
                    break;

                    case MILD_ALERT:
                    if(--blinkTimeout == 0)
                    {
                        Link_Alert_LED_Write(Link_Alert_LED_Read() ^ 0x01);
                        blinkTimeout = BLINK_TIMEOUT;
                    }
                    break;

                    case HIGH_ALERT:
                        Link_Alert_LED_Write(LED_ON);
                    break;
                }
            }
            else
            {
                Link_Alert_LED_Write(LED_OFF);
            }
        }
        
        lpMode = CyBle_EnterLPM(CYBLE_BLESS_DEEPSLEEP);
        CyGlobalIntDisable;
        blessState = CyBle_GetBleSsState();

        if(lpMode == CYBLE_BLESS_DEEPSLEEP) 
        {   
            if(blessState == CYBLE_BLESS_STATE_ECO_ON || blessState == CYBLE_BLESS_STATE_DEEPSLEEP)
            {
                /* Put the device into the Deep Sleep mode only when all debug information has been sent */
                if((UART_SpiUartGetTxBufferSize() + UART_GET_TX_FIFO_SR_VALID) == 0u)
                {
                    CySysPmDeepSleep();
                }
                else
                {
                    CySysPmSleep();
                }
            }
        }
        else
        {
            if(blessState != CYBLE_BLESS_STATE_EVENT_CLOSE)
            {
                CySysPmSleep();
            }
        }
        CyGlobalIntEnable;
        
        if((cyBle_pendingFlashWrite != 0u) &&
                    ((UART_SpiUartGetTxBufferSize() + UART_GET_TX_FIFO_SR_VALID) == 0u))
        {
            apiResult = CyBle_StoreBondingData(0u);
            printf("Store bonding data, status: %x \r\n", apiResult);
        }
    }
}