/** * @brief Main program * @param None * @retval None */ int main(void) { int16_t accData[3]; #ifdef WITH_USART char msg[3] = {'0','\r','\n'}; #endif HAL_Init(); HW_Init(); BlueNRG_Init(); while (1) { HCI_Process(); if (set_connectable) { set_bluenrg_connectable(); set_connectable = 0; } /* Blink the orange LED */ if (HAL_GetTick() % 500 == 0) BSP_LED_Toggle(LED3); if (HAL_GetTick() % 100 == 0) { BSP_ACCELERO_GetXYZ(accData); Acc_Update(accData); } #ifdef WITH_USART msg[0] = (msg[0] == '9')? '0' : msg[0]+1; if (HAL_USART_Transmit(&UsartHandle, (uint8_t *)msg, 3, 5000) != HAL_OK) ColorfulRingOfDeath(); #endif } }
int main(void) { bsp_init(); dispatch_init(); on_ready(); while(1) { HCI_Process(); dispatch(); loop(); } }
/***************************************************************************** * FUNCTION * EVM_Process * DESCRIPTION * Process all outstanding events. * PARAMETERS * void * RETURNS * void *****************************************************************************/ void EVM_Process(void) { /* Do not permit stack API calls to occur until we're done */ OS_LockStack(); /* Let HCI process any events */ HCI_Process(); /* Look for timers that might have fired */ CheckTimers(); /* Permit stack API calls again */ OS_UnlockStack(); }
/** * @brief Background task * * @note * @param None * @retval None */ void Background (void) { if(TaskExecutionRequested & (1<< eMAIN_Profile_Event_Id)) { if(Hci_Cmd_Lock == FALSE) { TaskExecuted(eMAIN_Profile_Event_Id); BlueNRG_Profile_Event_Callback(); } } if(TaskExecutionRequested & (1<< eMAIN_Profile_Measurement_update_Id)) { if(Hci_Cmd_Lock == FALSE) { TaskExecuted(eMAIN_Profile_Measurement_update_Id); profileApplContext.profileProcessMeasurementFunc(); } } if(TaskExecutionRequested & (1<< eMAIN_HCI_Process_Request_Id)) { /** * It shall be controlled in the application that no HCI command are sent * if one is already pending */ if(Hci_Cmd_Lock == FALSE) { TaskExecuted(eMAIN_HCI_Process_Request_Id); HCI_Process(); } } if(TaskExecutionRequested & (1<< eMAIN_Profile_Process_Request_Id)) { if(Hci_Cmd_Lock == FALSE) { TaskExecuted(eMAIN_Profile_Process_Request_Id); Profile_Process_Q(); } } if(TaskExecutionRequested & (1<< eMAIN_Profile_StateMachine_update_Id)) { if(Hci_Cmd_Lock == FALSE) { TaskExecuted(eMAIN_Profile_StateMachine_update_Id); profileApplContext.profileStateMachineFunc(); } } if(TaskExecutionRequested & (1<< eMAIN_Profile_App_DeviceState_update_Id)) { if(Hci_Cmd_Lock == FALSE) { TaskExecuted(eMAIN_Profile_App_DeviceState_update_Id); profileApplContext.profileApplicationProcessFunc(); } } /** * Power management */ #if LOW_POWER_MODE __disable_irq(); if((TaskExecutionRequested == 0) || (((TaskExecutionRequested & EVENT_NOT_REQUIRING_SENDING_HCI_COMMAND) == 0) && (Hci_Cmd_Lock == TRUE))) { LPM_Enter_Mode(); } __enable_irq(); #endif /* LOW_POWER_MODE */ return; }
/** * @brief Main function to show how to use the BlueNRG Bluetooth Low Energy * expansion board to send data from a Nucleo board to a smartphone * with the support BLE and the "BlueNRG" app freely available on both * GooglePlay and iTunes. * The URL to the iTunes for the "BlueNRG" app is * http://itunes.apple.com/app/bluenrg/id705873549?uo=5 * The URL to the GooglePlay is * https://play.google.com/store/apps/details?id=com.st.bluenrg * The source code of the "BlueNRG" app, both for iOS and Android, is * freely downloadable from the developer website at * http://software.g-maps.it/ * The board will act as Server-Peripheral. * * After connection has been established: * - by pressing the USER button on the board, the cube showed by * the app on the smartphone will rotate. * * The communication is done using a vendor specific profile. * * @param None * @retval None */ int main(void) { const char *name = "BlueNRG"; uint8_t SERVER_BDADDR[] = {0x12, 0x34, 0x00, 0xE1, 0x80, 0x03}; uint8_t bdaddr[BDADDR_SIZE]; uint16_t service_handle, dev_name_char_handle, appearance_char_handle; uint8_t hwVersion; uint16_t fwVersion; int ret; /* STM32Cube HAL library initialization: * - Configure the Flash prefetch, Flash preread and Buffer caches * - Systick timer is configured by default as source of time base, but user * can eventually implement his proper time base source (a general purpose * timer for example or other time source), keeping in mind that Time base * duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and * handled in milliseconds basis. * - Low Level Initialization */ HAL_Init(); #if NEW_SERVICES /* Configure LED2 */ BSP_LED_Init(LED2); #endif /* Configure the User Button in GPIO Mode */ BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_GPIO); /* Configure the system clock */ SystemClock_Config(); /* Initialize the BlueNRG SPI driver */ BNRG_SPI_Init(); /* Initialize the BlueNRG HCI */ HCI_Init(); /* Reset BlueNRG hardware */ BlueNRG_RST(); /* get the BlueNRG HW and FW versions */ getBlueNRGVersion(&hwVersion, &fwVersion); /* * Reset BlueNRG again otherwise we won't * be able to change its MAC address. * aci_hal_write_config_data() must be the first * command after reset otherwise it will fail. */ BlueNRG_RST(); PRINTF("HWver %d, FWver %d", hwVersion, fwVersion); if (hwVersion > 0x30) { /* X-NUCLEO-IDB05A1 expansion board is used */ bnrg_expansion_board = IDB05A1; /* * Change the MAC address to avoid issues with Android cache: * if different boards have the same MAC address, Android * applications unless you restart Bluetooth on tablet/phone */ SERVER_BDADDR[5] = 0x02; } /* The Nucleo board must be configured as SERVER */ Osal_MemCpy(bdaddr, SERVER_BDADDR, sizeof(SERVER_BDADDR)); ret = aci_hal_write_config_data(CONFIG_DATA_PUBADDR_OFFSET, CONFIG_DATA_PUBADDR_LEN, bdaddr); if(ret){ PRINTF("Setting BD_ADDR failed.\n"); } ret = aci_gatt_init(); if(ret){ PRINTF("GATT_Init failed.\n"); } if (bnrg_expansion_board == IDB05A1) { ret = aci_gap_init_IDB05A1(GAP_PERIPHERAL_ROLE_IDB05A1, 0, 0x07, &service_handle, &dev_name_char_handle, &appearance_char_handle); } else { ret = aci_gap_init_IDB04A1(GAP_PERIPHERAL_ROLE_IDB04A1, &service_handle, &dev_name_char_handle, &appearance_char_handle); } if(ret != BLE_STATUS_SUCCESS){ PRINTF("GAP_Init failed.\n"); } ret = aci_gatt_update_char_value(service_handle, dev_name_char_handle, 0, strlen(name), (uint8_t *)name); if(ret){ PRINTF("aci_gatt_update_char_value failed.\n"); while(1); } ret = aci_gap_set_auth_requirement(MITM_PROTECTION_REQUIRED, OOB_AUTH_DATA_ABSENT, NULL, 7, 16, USE_FIXED_PIN_FOR_PAIRING, 123456, BONDING); if (ret == BLE_STATUS_SUCCESS) { PRINTF("BLE Stack Initialized.\n"); } PRINTF("SERVER: BLE Stack Initialized\n"); ret = Add_Acc_Service(); if(ret == BLE_STATUS_SUCCESS) PRINTF("Acc service added successfully.\n"); else PRINTF("Error while adding Acc service.\n"); ret = Add_Environmental_Sensor_Service(); if(ret == BLE_STATUS_SUCCESS) PRINTF("Environmental Sensor service added successfully.\n"); else PRINTF("Error while adding Environmental Sensor service.\n"); #if NEW_SERVICES /* Instantiate Timer Service with two characteristics: * - seconds characteristic (Readable only) * - minutes characteristics (Readable and Notifiable ) */ ret = Add_Time_Service(); if(ret == BLE_STATUS_SUCCESS) PRINTF("Time service added successfully.\n"); else PRINTF("Error while adding Time service.\n"); /* Instantiate LED Button Service with one characteristic: * - LED characteristic (Readable and Writable) */ ret = Add_LED_Service(); if(ret == BLE_STATUS_SUCCESS) PRINTF("LED service added successfully.\n"); else PRINTF("Error while adding LED service.\n"); #endif /* Set output power level */ ret = aci_hal_set_tx_power_level(1,4); while(1) { HCI_Process(); User_Process(&axes_data); #if NEW_SERVICES Update_Time_Characteristics(); #endif } }
//TBR void Host_Profile_Test_Application(void) { uint8_t ret; while(1) { HAL_StatusTypeDef uart_status; uart_status = HAL_UART_Receive(&UartHandle, (uint8_t *)uart_header, UARTHEADERSIZE, 10); if( uart_status != HAL_OK && uart_status != HAL_TIMEOUT) { Error_Handler(); } if (uart_status == HAL_OK) { // Process the command handle_uart_cmd(uart_header, UARTHEADERSIZE, PROFILE_CENTRAL); } if(profileApplContext.deviceMasterInited) { HCI_Process(); /* process the timer Q */ Blue_NRG_Timer_Process_Q(); /* Call the Profile Master role state machine */ Master_Process(); if (TimeClientContext.fullConf) TimeClient_StateMachine(); /* Start the connection procedure */ if (profileApplContext.startDeviceConn) { ret = Device_Connection_Procedure(profileApplContext.peerAddr); notify_uart(&ret, sizeof(ret), CONNECTION_STATUS); profileApplContext.startDeviceConn = FALSE; } /* Services Discovery */ if(profileApplContext.deviceState == APPL_CONNECTED){ ret = Device_ServicesDiscovery(); if(ret != BLE_STATUS_SUCCESS) { APPL_MESG_INFO(profiledbgfile,"Disconnecting...\n"); Device_Disconnection(); } profileApplContext.deviceState = APPL_INIT_DONE; // Transitory } /* Chars Discovery */ if(profileApplContext.deviceState == APPL_SERVICES_DISCOVERED){ ret = Device_Discovery_CharacServ(CURRENT_TIME_SERVICE_UUID); if(ret != BLE_STATUS_SUCCESS) { APPL_MESG_INFO(profiledbgfile,"Disconnecting...\n"); Device_Disconnection(); } profileApplContext.deviceState = APPL_INIT_DONE; // Transitory } /* Get TS Current Time Char Descriptor */ if(profileApplContext.deviceState == APPL_CHARS_DISCOVERED){ ret = TimeClient_Start_Current_Time_Characteristic_Descriptor_Discovery(); if(ret != BLE_STATUS_SUCCESS) { APPL_MESG_INFO(profiledbgfile,"Disconnecting...\n"); Device_Disconnection(); } profileApplContext.deviceState = APPL_INIT_DONE; // Transitory } /* Enable Notification */ if(profileApplContext.deviceState == APPL_GOT_CHAR_DESC){ ret = TimeClient_Set_Current_Time_Char_Notification(TRUE); if(ret != BLE_STATUS_SUCCESS) { APPL_MESG_INFO(profiledbgfile,"Disconnecting...\n"); Device_Disconnection(); profileApplContext.deviceState = APPL_INIT_DONE; } else { profileApplContext.deviceState = APPL_NOTIFICATION_ENABLED; } } #if 0 /* if the profiles have nothing more to process, then * wait for application input */ if(deviceState >= APPL_INIT_DONE) //TBR { uint8_t input = APPL_DISCOVER_TIME_SERVER;//200; deviceState = input; if (input>0) { APPL_MESG_INFO(profiledbgfile,"io--- input: %c\n",input); switch(input) { case DISPLAY_PTS_MENU: case APPL_DISCOVER_TIME_SERVER: case APPL_CONNECT_TIME_SERVER: case APPL_DISCONNECT_TIME_SERVER: case APPL_PAIR_WITH_TIME_SERVER: case APPL_CLEAR_SECURITY_DATABASE: case APPL_DISCOVER_TIME_SERVICES: case APPL_DISCOVER_CTS_CHARACTERISTICS: case APPL_DISCOVER_CT_CHAR_DESCRIPTOR: case APPL_DISCOVER_NEXT_DST_CHARACTERISTICS: case APPL_DISCOVER_RTU_CHARACTERISTICS: case APPL_GET_REF_TIME_UPDATE: case APPL_TIME_UPDATE_NOTIFICATION: case APPL_READ_LOCAL_TIME_INFORM: case APPL_READ_CURRENT_TIME: case APPL_GET_REF_TIME_INFO_ON_SERVER: case APPL_READ_NEXT_DST_CHANGE_TIME: case APPL_GET_SERV_TIME_UPDATE_STATE: case APPL_CANCEL_REF_TIME_UPDATE: case APPL_START_FULL_CONFIGURATION: { deviceState = input; } break; default: break; //continue }/* end switch(input) */ } /* end if _IO.... */ }/* end if(deviceState >= APPL_CONNECTED) */ /* application specific processing */ switch(deviceState) { case DISPLAY_PTS_MENU: { Display_Appl_Menu(); } break; case APPL_DISCOVER_TIME_SERVER: { APPL_MESG_DBG(profiledbgfile,"APPL_DISCOVER_TIME_SERVER: call Device_Discovery_Procedure() \n"); Device_Discovery_Procedure(); } break; case APPL_CONNECT_TIME_SERVER: { APPL_MESG_DBG(profiledbgfile,"APPL_CONNECT_TIME_SERVER: call Device_Connection_Procedure() \n"); Device_Connection_Procedure(); } break; case APPL_DISCONNECT_TIME_SERVER: { APPL_MESG_DBG(profiledbgfile,"APPL_DISCONNECT_TIME_SERVER: call Device_Disconnection() \n"); Device_Disconnection(); } break; case APPL_PAIR_WITH_TIME_SERVER: { APPL_MESG_DBG(profiledbgfile,"APPL_PAIR_WITH_TIME_SERVER: call Device_StartPairing() \n"); Device_StartPairing(); } break; case APPL_CLEAR_SECURITY_DATABASE: { APPL_MESG_DBG(profiledbgfile,"APPL_CLEAR_SECURITY_DATABASE: call Clear_Security_Database() \n"); status = TimeClient_Clear_Security_Database(); if (status == BLE_STATUS_SUCCESS) { APPL_MESG_DBG(profiledbgfile,"TimeClient_Clear_Security_Database() Call: OK\n" ); } else { APPL_MESG_DBG(profiledbgfile,"TimeClient_Clear_Security_Database() Error: %02X\n", status); } } break; case APPL_DISCOVER_TIME_SERVICES : { /* It discover all the primary services of the connected device device */ APPL_MESG_DBG(profiledbgfile,"Call Device_ServicesDiscovery() x connected device device\n"); Device_ServicesDiscovery(); } break; case APPL_DISCOVER_CTS_CHARACTERISTICS : { /* It discovers all the characteristics of the connected current time service */ APPL_MESG_DBG(profiledbgfile,"Call Device_Discovery_CharacServ() x Current Time Service\n"); Device_Discovery_CharacServ(CURRENT_TIME_SERVICE_UUID); } break; case APPL_DISCOVER_NEXT_DST_CHARACTERISTICS: { /* It discovers all the characteristics of the connected Next DST Service */ APPL_MESG_DBG(profiledbgfile,"Call Device_Discovery_CharacServ() x Next DST Service\n"); Device_Discovery_CharacServ(NEXT_DST_CHANGE_SERVICE_UUID); } break; case APPL_DISCOVER_RTU_CHARACTERISTICS : { /* It discovers all the characteristics of the connected Reference Update Time service */ APPL_MESG_DBG(profiledbgfile,"Call Device_Discovery_CharacServ() x Reference Update Time Service\n"); Device_Discovery_CharacServ(REFERENCE_UPDATE_TIME_SERVICE_UUID); } break; case APPL_DISCOVER_CT_CHAR_DESCRIPTOR: { /* It discovers the characteristic descriptors of the connected device current time characteristic */ APPL_MESG_DBG(profiledbgfile,"Call TimeClient_Start_Glucose_Measurement_Characteristic_Descriptor_Discovery\n"); status = TimeClient_Start_Current_Time_Characteristic_Descriptor_Discovery(); if (status!= BLE_STATUS_SUCCESS) { APPL_MESG_DBG(profiledbgfile,"Error in the TimeClient_Start_Current_Time_Characteristic_Descriptor_Discovery() call: %02X\n", status); } } break; case APPL_GET_REF_TIME_UPDATE: { APPL_MESG_DBG(profiledbgfile,"Call TimeClient_Update_Reference_Time_On_Server\n"); status = TimeClient_Update_Reference_Time_On_Server(0x01); if (status!= BLE_STATUS_SUCCESS) { APPL_MESG_DBG(profiledbgfile,"Error in the TimeClient_Update_Reference_Time_On_Server() call: %02X\n", status); } } break; case APPL_CANCEL_REF_TIME_UPDATE: { APPL_MESG_DBG(profiledbgfile,"Call TimeClient_Update_Reference_Time_On_Server\n"); status = TimeClient_Update_Reference_Time_On_Server(0x02); if (status!= BLE_STATUS_SUCCESS) { APPL_MESG_DBG(profiledbgfile,"Error in the TimeClient_Update_Reference_Time_On_Server() call: %02X\n", status); } } break; case APPL_TIME_UPDATE_NOTIFICATION: { APPL_MESG_DBG(profiledbgfile,"Call TimeClient_Set_Current_Time_Char_Notification\n"); status = TimeClient_Set_Current_Time_Char_Notification(TRUE); if (status!= BLE_STATUS_SUCCESS) { APPL_MESG_DBG(profiledbgfile,"Error in the TimeClient_Set_Current_Time_Char_Notification() call: %02X\n", status); } } break; case APPL_GET_SERV_TIME_UPDATE_STATE: { APPL_MESG_DBG(profiledbgfile,"Call TimeClient_ReadServerTimeUpdateStatusChar\n"); status = TimeClient_ReadServerTimeUpdateStatusChar(); if (status!= BLE_STATUS_SUCCESS) { APPL_MESG_DBG(profiledbgfile,"Error in the TimeClient_ReadServerTimeUpdateStatusChar() call: %02X\n", status); } } break; case APPL_READ_NEXT_DST_CHANGE_TIME: { APPL_MESG_DBG(profiledbgfile,"Call TimeClient_ReadNextDSTChangeTimeChar\n"); status = TimeClient_ReadNextDSTChangeTimeChar(); if (status!= BLE_STATUS_SUCCESS) { APPL_MESG_DBG(profiledbgfile,"Error in the TimeClient_ReadNextDSTChangeTimeChar() call: %02X\n", status); } } break; case APPL_READ_LOCAL_TIME_INFORM: { APPL_MESG_DBG(profiledbgfile,"Call TimeClient_Get_Local_Time_Information\n"); status = TimeClient_ReadLocalTimeChar(); if (status!= BLE_STATUS_SUCCESS) { APPL_MESG_DBG(profiledbgfile,"Error in the TimeClient_ReadLocalTimeChar() call: %02X\n", status); } } break; case APPL_READ_CURRENT_TIME: { APPL_MESG_DBG(profiledbgfile,"Call TimeClient_ReadCurrentTimeChar\n"); status = TimeClient_ReadCurrentTimeChar(); if (status!= BLE_STATUS_SUCCESS) { APPL_MESG_DBG(profiledbgfile,"Error in the TimeClient_ReadCurrentTimeChar() call: %02X\n", status); } } break; case APPL_GET_REF_TIME_INFO_ON_SERVER: //APPL_GET_TIME_ACCU_INFO_SERVER: { APPL_MESG_DBG(profiledbgfile,"Call TimeClient_ReadReferenceTimeInfoChar\n"); status = TimeClient_ReadReferenceTimeInfoChar(); if (status!= BLE_STATUS_SUCCESS) { APPL_MESG_DBG(profiledbgfile,"Error in the TimeClient_ReadReferenceTimeInfoChar() call: %02X\n", status); } } break; case APPL_START_FULL_CONFIGURATION: APPL_MESG_DBG(profiledbgfile,"Call Device_StartFullConfig()\n"); Device_StartFullConfig(); break; }/* end switch(devicestate) */ #endif /* 0 */ } } /* end while(1) */ }/* end Host_Profile_Test_Application() */
void btle_handler(void) { btle_handler_pending = 0; BlueNRGGap::getInstance().Process(); HCI_Process(); }
int main(void) { //int ret; NVIC_SetVectorTable(NVIC_VectTab_FLASH,VECTOR_TABLE_BASE_ADDRESS); /* Identify the BlueNRG platform */ SdkEvalIdentification(); RCC_Configuration(); /* Basic button init function for handling application jumping */ Configure_Button(); #if 0 /* TBR */ PWR_PVDCmd(DISABLE); /* Disable FLASH during Sleep */ FLASH_SLEEPPowerDownCmd(ENABLE); /* Enable Ultra low power mode */ PWR_UltraLowPowerCmd(ENABLE); PWR_FastWakeUpCmd(DISABLE); #endif NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); Clock_Init(); HCI_Init(); /* Init SPI interface */ SdkEvalSpiInit(SPI_MODE_EXTI); /* Reset BlueNRG SPI interface */ BlueNRG_RST(); /* Init leds */ SdkEvalLedInit(LED1); SdkEvalLedInit(LED2); { tHalUint8 bdaddr[] = {0x12, 0x34, 0x00, 0xE1, 0x80, 0x02}; aci_hal_write_config_data(CONFIG_DATA_PUBADDR_OFFSET, CONFIG_DATA_PUBADDR_LEN, bdaddr); } aci_gatt_init(); { uint16_t service_handle, dev_name_char_handle, appearance_char_handle; aci_gap_init(1, &service_handle, &dev_name_char_handle, &appearance_char_handle); } #if 0/* TBR */ aci_gap_set_auth_requirement(MITM_PROTECTION_REQUIRED, OOB_AUTH_DATA_ABSENT, NULL, 7, 16, USE_FIXED_PIN_FOR_PAIRING, 123456, BONDING); #endif //PRINTF("BLE Stack Initialized.\n"); #ifdef ST_OTA_BTL /* Add OTA bootloader service */ Add_Btl_Service(); #endif /* -2 dBm output power */ aci_hal_set_tx_power_level(1,4); while(1) { #ifdef ST_OTA_BTL static tClockTime startTime = 0; if (Clock_Time() - startTime >led_blinking_rate) { /* LED D1 is toggling on OTA_Service Manager */ SdkEvalLedToggle(LED1); startTime = Clock_Time(); } #endif /* end ST_OTA_BTL */ HCI_Process(); if(set_connectable){ setConnectable(); set_connectable = 0; } /* Use button to switch to the basic Reset Manager */ if (GPIO_ReadInputDataBit(ButtonPort,ButtonPin) == RESET) { /* Add delay to avoid conlict with DFU activation */ Clock_Wait(2000); /* Check if an application has been loaded previously through OTA service manager */ if (*((uint32_t*) NEW_APP_MEM_INFO)!= 0) /* Service Manager will jump to the Application previously loaded at address APPLICATION_JUMP_ADDRESS */ Switch_To_OTA_Service_Manager_Application(APPLICATION_JUMP_ADDRESS); } } }