void Initialization(void) { /* 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); /* Do the following for achieving lowest possible WCO & ECO startup current: * 1. Shut down the ECO (to reduce power consumption while WCO is starting) * 2. Enable WDT counter 0 to wakeup the system after 500ms (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 * 5. Enable WDT counter 1 to wakeup the system after 1ms (1ms = ECO startup time) * 5. Configure PSoC 4 BLE device in DeepSleep mode for the 1ms ECO startup time */ CySysClkEcoStop(); /* Shutdown the ECO and later re-start in low power mode after WCO is turned on */ WDT_Interrupt_StartEx(WDT_Handler); /* Initialize WDT interrupt */ WCO_ECO_LowPowerStart(); /* Enable WCO & ECO in low power mode using WDT counter 0/1 as system wakeup sources respectively */ CyBle_Start(BLE_AppEventHandler); while (CyBle_GetState() == CYBLE_STATE_INITIALIZING) { CyBle_ProcessEvents(); } }
void WDT_Start(void) { /* Unlock the WDT registers for modification */ CySysWdtUnlock(); /* Setup ISR */ WDT_Interrupt_StartEx(&Timer_Interrupt); /* Write the mode to generate interrupt on match */ CySysWdtWriteMode(WDT_COUNTER, CY_SYS_WDT_MODE_INT); /* Configure the WDT counter clear on a match setting */ CySysWdtWriteClearOnMatch(WDT_COUNTER, WDT_COUNTER_ENABLE); /* Configure the WDT counter match comparison value */ CySysWdtWriteMatch(WDT_COUNTER, WDT_1SEC); /* Reset WDT counter */ CySysWdtResetCounters(WDT_COUNTER); /* Enable the specified WDT counter */ CySysWdtEnable(WDT_COUNTER_MASK); /* Lock out configuration changes to the Watchdog timer registers */ CySysWdtLock(); }
/******************************************************************************* * Function Name: InitializeSystem ******************************************************************************** * * Summary: * Systm initialization function. * * Parameters: * None * * Return: * None * *******************************************************************************/ void InitializeSystem(void) { CyGlobalIntEnable; /* Enable Global Interrupts*/ /* Set the divider for ECO, ECO will be used as source when IMO is switched off to save power */ CySysClkWriteEcoDiv(CY_SYS_CLK_ECO_DIV8); #if LOW_POWER_STARTUP_ENABLE /* If LOW_POWER_STARTUP_ENABLE is set, then do the following for achieving lowest possible WCO & ECO startup current: * 1. Shut down the ECO (to reduce power consumption while WCO is starting) * 2. Enable WDT counter 0 to wakeup the system after 500ms (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 * 5. Enable WDT counter 1 to wakeup the system after 1ms (1ms = ECO startup time) * 5. Configure PSoC 4 BLE device in DeepSleep mode for the 1ms ECO startup time */ CySysClkEcoStop(); /* Shutdown the ECO and later re-start in low power mode after WCO is turned on */ WDT_Interrupt_StartEx(WDT_Handler); /* Initialize WDT interrupt */ WCO_ECO_LowPowerStart(); /* Enable WCO & ECO in low power mode using WDT counter 0/1 as system wakeup sources respectively */ #endif /* End of #if LOW_POWER_STARTUP_ENABLE */ #if (CONSOLE_LOG_ENABLE) Console_Start(); /* Console log interface */ Console_UartPutString("Low power system startup complete\r\n"); #endif /* End of #if (CONSOLE_LOG_ENABLE) */ BLE_Engine_Start(); /* start the BLE interface */ /* Wait for BLE Component to Initialize */ while (CyBle_GetState() == CYBLE_STATE_INITIALIZING) { CyBle_ProcessEvents(); } }
/******************************************************************************* * 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); } }