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() { CyGlobalIntEnable; #if (DEBUG_UART_ENABLED == ENABLED) UART_DEB_Start(); #endif /* (DEBUG_UART_ENABLED == ENABLED) */ DBG_PRINTF("BLE HID Keyboard Example Project \r\n"); LED_RED_Write(LED_OFF); LED_BLU_Write(LED_OFF); LED_GRN_Write(LED_OFF); /* Start CYBLE component and register generic event handler */ CyBle_Start(AppCallBack); #if (BAS_MEASURE_ENABLE != 0) ADC_Start(); #endif /* BAS_MEASURE_ENABLE != 0 */ while(1) { /* CyBle_ProcessEvents() allows BLE stack to process pending events */ CyBle_ProcessEvents(); /* To achieve low power in the device */ LowPowerImplementation(); if((CyBle_GetState() == CYBLE_STATE_CONNECTED) && (suspend != CYBLE_HIDS_CP_SUSPEND)) { if(mainTimer != 0u) { mainTimer = 0u; #if (BAS_SIMULATE_ENABLE != 0) SimulateBattery(); CyBle_ProcessEvents(); #endif /* BAS_SIMULATE_ENABLE != 0 */ #if (BAS_MEASURE_ENABLE != 0) MeasureBattery(); CyBle_ProcessEvents(); #endif /* BAS_MEASURE_ENABLE != 0 */ if(keyboardSimulation == ENABLED) { SimulateKeyboard(); } } /* Store bonding data to flash only when all debug information has been sent */ #if(CYBLE_BONDING_REQUIREMENT == CYBLE_BONDING_YES) #if (DEBUG_UART_ENABLED == ENABLED) if((cyBle_pendingFlashWrite != 0u) && ((UART_DEB_SpiUartGetTxBufferSize() + UART_DEB_GET_TX_FIFO_SR_VALID) == 0u)) #else if(cyBle_pendingFlashWrite != 0u) #endif /* (DEBUG_UART_ENABLED == ENABLED) */ { CYBLE_API_RESULT_T apiResult; apiResult = CyBle_StoreBondingData(0u); (void)apiResult; DBG_PRINTF("Store bonding data, status: %x \r\n", apiResult); } #endif /* CYBLE_BONDING_REQUIREMENT == CYBLE_BONDING_YES */ } } }
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(); } }
/******************************************************************************* * Function Name: main() ******************************************************************************** * Summary: * Main function for the project. * * Parameters: * None * * Return: * None * * Theory: * The function starts BLE and UART components. * This function process all BLE events and also implements the low power * functionality. * *******************************************************************************/ int main() { CyGlobalIntEnable; #if (DEBUG_UART_ENABLED == ENABLED) UART_DEB_Start(); #endif /* (DEBUG_UART_ENABLED == ENABLED) */ DBG_PRINTF("BLE Cycling Sensor Example Project \r\n"); Disconnect_LED_Write(LED_OFF); Advertising_LED_Write(LED_OFF); CyBle_Start(AppCallback); /* Start CYBLE component and register generic event handler */ CyBle_Start(AppCallback); /* Register service specific callback functions */ CscsInit(); CpsInit(); WDT_Start(); /*************************************************************************** * Main polling loop ***************************************************************************/ while(1) { /* CyBle_ProcessEvents() allows BLE stack to process pending events */ CyBle_ProcessEvents(); /* To achieve low power in the device */ LowPowerImplementation(); /*********************************************************************** * Wait for connection established with Central device ***********************************************************************/ if(CyBle_GetState() == CYBLE_STATE_CONNECTED) { /******************************************************************* * Periodically simulate Cycling characteristics and send * results to the Client *******************************************************************/ if(mainTimer != 0u) { mainTimer = 0u; SimulateCyclingPower(); CyBle_ProcessEvents(); SimulateCyclingSpeed(); } /* Store bounding data to flash only when all debug information has been sent */ #if (DEBUG_UART_ENABLED == ENABLED) if((cyBle_pendingFlashWrite != 0u) && ((UART_DEB_SpiUartGetTxBufferSize() + UART_DEB_GET_TX_FIFO_SR_VALID) == 0u)) #else if(cyBle_pendingFlashWrite != 0u) #endif /* (DEBUG_UART_ENABLED == ENABLED) */ { CYBLE_API_RESULT_T apiResult; apiResult = CyBle_StoreBondingData(0u); (void)apiResult; DBG_PRINTF("Store bonding data, status: %x \r\n", apiResult); } } } }
int main() { CyGlobalIntEnable; SCB_Start(); UART_DEB_Start(); DBG_PRINTF("BLE HID Keyboard Example Project \r\n"); LED_RED_Write(LED_OFF); LED_GRN_Write(LED_OFF); LED_BLU_Write(LED_OFF); /* Start CYBLE component and register generic event handler */ CyBle_Start(AppCallBack); Sup_Pdu_t i2c_inbox; while(1) { SCB_I2CMasterClearStatus(); /* CyBle_ProcessEvents() allows BLE stack to process pending events */ CyBle_ProcessEvents(); /* uint8_t buf; uint32_t result = SCB_I2CMasterReadBuf(8, &buf, 1, SCB_I2C_MODE_COMPLETE_XFER); if (0 == (result & SCB_I2C_MSTR_NOT_READY)) { uint32_t cnt = 100; while (--cnt && 0 == (SCB_I2CMasterStatus() & SCB_I2C_MSTAT_RD_CMPLT)) {} if (!cnt) { uint32_t tmp = SCB_I2CMasterStatus(); DBG_PRINTF("stuck in I2C RX: %d\r\n", tmp); } else { if (0 == (SCB_I2CMasterStatus() & SCB_I2C_MSTAT_ERR_MASK) && buf != 0xff) { i2c_inbox.command = buf; result = SCB_I2C_MSTR_NOT_READY; while (result & SCB_I2C_MSTR_NOT_READY) { result = SCB_I2CMasterReadBuf(8, &buf, 1, SCB_I2C_MODE_COMPLETE_XFER); } while (0 == (SCB_I2CMasterStatus() & SCB_I2C_MSTAT_RD_CMPLT)) {} if (0 == (SCB_I2CMasterStatus() & SCB_I2C_MSTAT_ERR_MASK)) { i2c_inbox.data = buf; process_inbox(&i2c_inbox); } } } } */ /* To achieve low power in the device */ LowPowerImplementation(); if((CyBle_GetState() == CYBLE_STATE_CONNECTED) && (suspend != CYBLE_HIDS_CP_SUSPEND)) { if(mainTimer != 0u) { mainTimer = 0u; } /* Store bonding data to flash only when all debug information has been sent */ #if(CYBLE_BONDING_REQUIREMENT == CYBLE_BONDING_YES) #if (DEBUG_UART_ENABLED == ENABLED) if((cyBle_pendingFlashWrite != 0u) && ((UART_DEB_SpiUartGetTxBufferSize() + UART_DEB_GET_TX_FIFO_SR_VALID) == 0u)) #else if(cyBle_pendingFlashWrite != 0u) #endif /* (DEBUG_UART_ENABLED == ENABLED) */ { CYBLE_API_RESULT_T apiResult; apiResult = CyBle_StoreBondingData(0u); (void)apiResult; DBG_PRINTF("Store bonding data, status: %x \r\n", apiResult); } #endif /* CYBLE_BONDING_REQUIREMENT == CYBLE_BONDING_YES */ } } }
/******************************************************************************* * Function Name: BLE_Run ******************************************************************************** * * Summary: * BLE interface processing engine. This API should be continuously called by * the application in its main loop. * * Parameters: * None * * Return: * None * *******************************************************************************/ uint8 BLE_Run(void) { #if (BLE_GATT_CLIENT_ENABLE) CYBLE_API_RESULT_T status; switch(bleStatus) { case BLE_CONNECTED: status = StartTimeServiceDiscovery(); if(CYBLE_ERROR_OK == status) { bleStatus = BLE_DISCOVEER_GATT_SERVICES; } else { bleStatus = BLE_INVALID_OPERATION; } #if(CONSOLE_LOG_ENABLED) printf("BLE connection established \r\n\n"); #endif /* End of #if(CONSOLE_LOG_ENABLED) */ break; case BLE_READ_TIME: if(CyBle_GetClientState() == CYBLE_CLIENT_STATE_DISCOVERED) { status = SyncTimeFromBleTimeServer(); if(CYBLE_ERROR_OK == status) { bleStatus = BLE_IDLE; } else { bleStatus = BLE_INVALID_OPERATION; } } break; case BLE_INITIATE_AUTHENTICATION: CyBle_GapAuthReq(cyBle_connHandle.bdHandle, &cyBle_authInfo); bleStatus = BLE_AUTHENTICATION_IN_PROGRESS; break; default: break; } #endif CyBle_ProcessEvents(); /* Store bonding data to flash only when all debug information has been sent */ if((cyBle_pendingFlashWrite != 0u) #if(CONSOLE_LOG_ENABLED) &&((Debug_Console_SpiUartGetTxBufferSize() + Debug_Console_GET_TX_FIFO_SR_VALID) == 0u) #endif ) { status = CyBle_StoreBondingData(0u); } #if DISCONNECT_BLE_AFTER_TIME_SYNC if(BLE_IsDisconnectionRequestPending()) { BLE_SendDisconnection(); } #endif return bleStatus; }
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: 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); } } }
/******************************************************************************* * Function Name: BondingImplementation() ******************************************************************************** * Summary: * Implements bonding of peer BLE device information. * * Parameters: * None * * Return: * None * * Theory: * The function stores the peer BLE device information to flash (called bonding) * when the device is ready. When the user wants to clear the bond information, * it is cleared in this function. * *******************************************************************************/ static void BondingImplementation(void) { uint8 command; CYBLE_GAP_BD_ADDR_T clearAllDevices = {{0,0,0,0,0,0},0}; switch(authState) { case AUTHENTICATION_COMPLETE_BONDING_REQD: /* Store bonding data of the current connection */ while(CYBLE_ERROR_OK != CyBle_StoreBondingData(1)); authState = AUTHENTICATION_BONDING_COMPLETE; UART_UartPutString("Bonding complete. "); break; case AUTHENTICATION_BONDING_COMPLETE: /* See if the user pressed 'R' button to clear the bond list. */ command = UART_UartGetChar(); if(command != 0u) { if((command == 'r') || (command == 'R')) { /* User wants the bond to be removed */ UART_UartPutString("\n\rClear the bond list. "); if(CyBle_GetState() == CYBLE_STATE_CONNECTED) { /* Disconnect */ authState = AUTHENTICATION_BONDING_REMOVE_WAITING_EVENT; CyBle_GapDisconnect(cyBle_connHandle.bdHandle); } else if(CyBle_GetState() == CYBLE_STATE_ADVERTISING) { /* Stop advertisement */ authState = AUTHENTICATION_BONDING_REMOVE_WAITING_EVENT; CyBle_GappStopAdvertisement(); } else { authState = AUTHENTICATION_BONDING_REMOVE_GO_AHEAD; } } } break; case AUTHENTICATION_BONDING_REMOVE_GO_AHEAD: /* Remove all bonded devices in the list */ CyBle_GapRemoveDeviceFromWhiteList(&clearAllDevices); while(CYBLE_ERROR_OK != CyBle_StoreBondingData(1)); UART_UartPutString("Cleared the list of bonded devices. \n\n\r"); authState = AUTHENTICATION_NOT_CONNECTED; /* Start advertisement again */ UART_UartPutString("Advertising. "); CyBle_GappStartAdvertisement(CYBLE_ADVERTISING_FAST); break; default: break; } }