*******************************************************************************/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() { 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: 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); } } } }
/******************************************************************************* * 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 - both deep sleep and hibernate. * *******************************************************************************/ int main() { CYBLE_GAP_BONDED_DEV_ADDR_LIST_T bondedDeviceList; /* Enable global interrupts */ CyGlobalIntEnable; /* Start a new UART session and clear screen */ UART_Start(); UART_UartPutChar(12); UART_UartPutString("=====ANCS Demo=====\n\r"); UART_UartPutString("\n\rPress 'R' at any time to clear the bonded device list. \n\r"); /* Initialize BLE component and ANCS related information */ CyBle_Start(StackEventHandler); Ancs_Reset(); /* Check whether we already have anything in the bonded device list */ CyBle_GapGetBondedDevicesList(&bondedDeviceList); if(bondedDeviceList.count != 0) { authState = AUTHENTICATION_BONDING_COMPLETE; } /* Custom service discovery is not implemented in the component yet. * So we need to enable all the events to come to the application * in order to process raw data for ANCS service discovery. */ cyBle_eventHandlerFlag |= CYBLE_ENABLE_ALL_EVENTS; /* Inifinite loop */ for(;;) { /* Process all the generated events. This includes the general events * for BLE initialization, advertisement, connection etc. It also * includes the events for service discovery and ANCS notifications. */ CyBle_ProcessEvents(); /* Handle the state machine for ANCS notifications */ Ancs_StateMachine(); /* Handle bonding of device information*/ BondingImplementation(); /* To achieve low power in the device */ LowPowerImplementation(); /* Enter hibernate mode when necessary. */ if(enterHibernateFlag == true) { WakeupPin_ClearInterrupt(); WakeupPinInterrupt_Start(); /* Wait for the UART to complete transfer */ while((UART_SpiUartGetTxBufferSize() + UART_GET_TX_FIFO_SR_VALID) != 0u); CySysPmHibernate(); } } }