Beispiel #1
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();
            }
        }
    }
}
Beispiel #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);
    }
}
Beispiel #3
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);
}
Beispiel #4
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);
    }
}
Beispiel #5
0
/*******************************************************************************
* Function Name: main
********************************************************************************
*
* Summary:
*  Main function.
*
* Parameters:
*  None
*
* Return:
*  None
*
*******************************************************************************/
int main()
{
    CYBLE_API_RESULT_T apiResult;
    CYBLE_STATE_T bleState;

    CyGlobalIntEnable;
	
    PWM_Start();
	UART_Start();
	UART_UartPutString("Welcome to BLE OOB Pairing Demo\r\n");

    apiResult = CyBle_Start(StackEventHandler);

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

    CyBle_IasRegisterAttrCallback(IasEventHandler);

    for(;;)
    {
        /* Single API call to service all the BLE stack events. Must be
         * called at least once in a BLE connection interval */
        CyBle_ProcessEvents();

        bleState = CyBle_GetState();

        if(bleState != CYBLE_STATE_STOPPED &&
            bleState != CYBLE_STATE_INITIALIZING)
        {
            /* Configure BLESS in DeepSleep mode  */
            CyBle_EnterLPM(CYBLE_BLESS_DEEPSLEEP);

            /* Configure PSoC 4 BLE system in sleep mode */
            CySysPmSleep();

            /* BLE link layer timing interrupt will wake up the system */
        }
    }
}
Beispiel #6
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);
}
Beispiel #7
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);
}
Beispiel #8
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();
    }
}
int main()
{
    CYBLE_API_RESULT_T apiResult;
  
    CYBLE_LP_MODE_T lpMode;

    CyGlobalIntEnable;
    
    CommInit();               /* Start communication component */
    printf("BLE Uart Transmission Collector Example Project \r\n");
    
    Scanning_LED_Write(LED_OFF);

    apiResult = CyBle_Start(AppCallBack);
    if(apiResult != CYBLE_ERROR_OK)
    {
        printf("CyBle_Start API Error: %xd \r\n", apiResult);
    }
		else
			{
				printf("CyBle_Start API ok \r\n");
			}
    
		/* Enable the Interrupt component connected to interrupt */
		TC_CC_ISR_StartEx(InterruptHandler);
		
		/* Start the components */
		Timer_Start();
    
    while(1)
    {
        if(CyBle_GetState() != CYBLE_STATE_INITIALIZING)
        {
            /* Enter DeepSleep mode between connection intervals */
            lpMode = CyBle_EnterLPM(CYBLE_BLESS_DEEPSLEEP);
            if(lpMode == CYBLE_BLESS_DEEPSLEEP) 
            {
                /* Put the device into the Deep Sleep mode only when all debug information has been sent 
			                if(UART_DEB_SpiUartGetTxBufferSize() == 0u)
			                {
			                    CySysPmDeepSleep();
			                }
			                else
			                {
			                    CySysPmSleep();
			                }*/
                CySysPmSleep();
                /* Handle scanning led blinking */
                HandleLEDs(ble_state);
            }
            HandleLEDs(ble_state);
        }
		/***********************************************************************
        * Wait for connection established with Central device
        ***********************************************************************/
        if(CyBle_GetState() == CYBLE_STATE_CONNECTED)
        {
            /*******************************************************************
            *  Periodically measure a battery level and temperature and send 
            *  results to the Client
            *******************************************************************/    
            CommMonitorUart();
            CommMonitorBLE();
            
            #if 0
            if(mainTimer != 0u)
            {
                mainTimer = 0u;

                if(storeBondingData == ENABLED)
                {
                    cystatus retValue;
                    retValue = CyBle_StoreBondingData(0u);
                    printf("Store bonding data, status: %lx \r\n", retValue);
                    storeBondingData = DISABLED;
                }
    
            }
            #endif
            
            
        }
        
        
        /*******************************************************************
        *  Processes all pending BLE events in the stack
        *******************************************************************/        
        CyBle_ProcessEvents();
    }
}
Beispiel #10
0
/*****************************************************************************
* Function Name: main()
******************************************************************************
* Summary:
*   Central function which controls the application flow.
*
* Parameters:
*   None.
*
* Return:
*   None.
*
* Note:
*
*****************************************************************************/
int main()
{
    // Init.
    InitializeSystem();

    // Wait for BLE component to finish initialization.
    while (CyBle_GetState() == CYBLE_STATE_INITIALIZING) {
        CyBle_ProcessEvents();
    } 

    //////////////////////////////////////////////////////////////////////////
    // Main Loop
    //////////////////////////////////////////////////////////////////////////
    for(;;)
    {

        // Process all the events in the stack.
        CyBle_ProcessEvents();


        #if LOWPOWERMODE_ENABLED
            // Put the BLESS in deepest sleep possible.
            CyBle_EnterLPM(CYBLE_BLESS_DEEPSLEEP);
        #endif

        //////////////////////////////////////////////////////////////////////
        // Device connected
        //////////////////////////////////////////////////////////////////////
        if (_BLE_deviceConnected) {
            
            // Update the CapSense CCCD.
            _BLE_UpdateCCCD();
            
            // Update the Control values.
            _BLE_UpdateControl();
            
            // If asked by client, send the status flags by notification or
            // indication.
            if (_BLE_sendStatus)
                _BLE_sendStatusFlags();
                

            #if LOWPOWERMODE_ENABLED
                // Handle the Capsense low power mode.
                LowPowerMode_Capsense();
            #endif
            
            // If asked by client, send the content of the vector containing
            // the CapSense data.
            if (_BLE_sendData) {
                SendData();
                // Allow DeepSleep between connection intervals.
                LowPowerMode_System_DeepSleepAllowed = TRUE;
            }
            
            // If asked by client, acquire CapSense data and store it in
            // the vector.
            else if (_BLE_acquireData || TEST_USB) {
                AcquireData();
                // Keep system in Active mode during scanning.
                LowPowerMode_System_DeepSleepAllowed = FALSE;
            }

            // If the 'acquireData' request is not present, but the vector
            // isn't empty, then no more data can be acquired.
            else if (!_BLE_acquireData && !vectorIsEmpty()) {
                ReadyToSendData();
                // Allow DeepSleep between connection intervals.
                LowPowerMode_System_DeepSleepAllowed = TRUE;
            }
            
            // If the 'sendData' and 'acquireData' requests are not present,
            // but the vector is empty, then data has been all sent and
            // it's ready to acquire new data.
            else if (!_BLE_sendData && !_BLE_acquireData && vectorIsEmpty()) {
                ReadyToAcquireData();
                // Allow DeepSleep between connection intervals.
                LowPowerMode_System_DeepSleepAllowed = TRUE;
            }
        }
        
        else
            // Allow DeepSleep between advertisement intervals.
            LowPowerMode_System_DeepSleepAllowed = TRUE;
        
        //////////////////////////////////////////////////////////////////////
        // Advertisement
        //////////////////////////////////////////////////////////////////////
        // Start advertisement if the flag is set in the BLE event handler.
        if (_BLE_restartAdvertisement) {
            // Reset the flag.
            _BLE_restartAdvertisement = FALSE;
            // Start advertisement.
            CyBle_GappStartAdvertisement(CYBLE_ADVERTISING_FAST);
        }
        
        //////////////////////////////////////////////////////////////////////
        // Reset
        //////////////////////////////////////////////////////////////////////
        // If the client is disconnected, reset everything as the program
        // would be is first started.
        if (_BLE_resetNeeded) {
            // Reset the flag.
            _BLE_resetNeeded = FALSE;
            // Reset everything that needs to.
            ResetSystem();
        }

        //////////////////////////////////////////////////////////////////////
        // Low Power Mode (Sleep)
        //////////////////////////////////////////////////////////////////////
        #if LOWPOWERMODE_ENABLED
            // Put the system in the deepest sleep possible.
            LowPowerMode_Sleep();
            // Reset flag.
            LowPowerMode_System_DeepSleepAllowed = FALSE;
        #endif
    }
}
Beispiel #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();
            }
        }
    }
}
Beispiel #12
0
/*******************************************************************************
* 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);
        }
    }
}
Beispiel #13
0
Datei: main.c Projekt: 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);
    }
}
Beispiel #14
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();
    }
}
Beispiel #15
0
int main()
{
    #ifdef LOW_POWER_MODE    
        CYBLE_LP_MODE_T         lpMode;
        CYBLE_BLESS_STATE_T     blessState;
    #endif
    
    CYBLE_API_RESULT_T      bleApiResult;
   
    CyGlobalIntEnable; 
    
    /* Start UART and BLE component and display project information */
    UART_Start();   
    bleApiResult = CyBle_Start(AppCallBack); 
    
    if(bleApiResult == CYBLE_ERROR_OK)
    {
        #ifdef PRINT_MESSAGE_LOG
            UART_UartPutString("\n\r************************************************************");
            UART_UartPutString("\n\r***************** BLE UART example project *****************");
            UART_UartPutString("\n\r************************************************************\n\r");
            UART_UartPutString("\n\rDevice role \t: CENTRAL");
            
            #ifdef LOW_POWER_MODE
                UART_UartPutString("\n\rLow Power Mode \t: ENABLED");
            #else
                UART_UartPutString("\n\rLow Power Mode \t: DISABLED");
            #endif
            
            #ifdef FLOW_CONTROL
                UART_UartPutString("\n\rFlow Control \t: ENABLED");  
            #else
                UART_UartPutString("\n\rFlow Control \t: DISABLED");
            #endif
            
        #endif
    }
    else
    {
        #ifdef PRINT_MESSAGE_LOG   
            UART_UartPutString("\n\r\t\tCyBle stack initilization FAILED!!! \n\r ");
        #endif
        
        /* Enter infinite loop */
        while(1);
    }
    
    CyBle_ProcessEvents();
    
    /***************************************************************************
    * Main polling loop
    ***************************************************************************/
    while(1)
    {               
        #ifdef LOW_POWER_MODE
            
            if((CyBle_GetState() != CYBLE_STATE_INITIALIZING) && (CyBle_GetState() != CYBLE_STATE_DISCONNECTED))
            {
                /* 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) && \
                            (UART_SpiUartGetTxBufferSize() + UART_GET_TX_FIFO_SR_VALID) == 0u)
                    {
                        #ifdef FLOW_CONTROL
                            EnableUartRxInt();
                        #endif
                        
                        CySysPmSleep();
                        
                        #ifdef FLOW_CONTROL
                            DisableUartRxInt();
                        #endif
                    }
                }
                else
                {
                    if((blessState != CYBLE_BLESS_STATE_EVENT_CLOSE) && \
                            (UART_SpiUartGetTxBufferSize() + UART_GET_TX_FIFO_SR_VALID) == 0u)
                    {
                        #ifdef FLOW_CONTROL
                            EnableUartRxInt();
                        #endif
                        
                        CySysPmSleep();
                        
                        #ifdef FLOW_CONTROL
                            DisableUartRxInt();
                        #endif
                    }
                }
                CyGlobalIntEnable;
                
                /* Handle advertising led blinking */
                HandleLeds();
            }
            
        #else
            HandleLeds();
        #endif
        
        /*******************************************************************
        *  Process all pending BLE events in the stack
        *******************************************************************/      
        HandleBleProcessing();
        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
}
Beispiel #17
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();
                        
        }   
    
    }
    
 }