In programming mode, output sensor data from the USB virtual serial port
[Copy link]
After studying the official ST routine STM32CubeFunctionPack_STBOX1_V1.1.0\Projects\STM32L4R9ZI-SensorTile.box\Applications\BLESensors, the Bluetooth is unstable. After connecting, the data of various environmental sensors can be displayed, but within a minute, the Bluetooth connection fails. It may be that the Bluetooth compatibility with the mobile phone is not good, so I simply modified the code myself and viewed the data through the reserved USB virtual debugging serial port.
The main modification is to block the Bluetooth related initialization functions. The following is the code, and the modified parts are marked in Chinese.
Notes on debugging: When starting debugging, be sure to open the serial port after the USB virtual serial port is successfully mapped. When ending debugging, first close the serial port of the debugging assistant and then stop the program. If you forget to close the serial port of the debugging assistant and stop the program, the assistant software will report errors and you will need to force the program to terminate.
int main(void)
{
#ifdef STBOX1_RESTART_DFU
/* Check if we need to go to DFU */
if(DFU_Var == DFU_MAGIC_NUM) {
typedef void (*pFunction)(void);
pFunction JumpToDFU;
/* Reset the Magic Number */
DFU_Var = 0x00000000;
HAL_RCC_DeInit();
SysTick->CTRL = 0;
SysTick->LOAD = 0;
SysTick->VAL = 0;
__disable_irq();
__HAL_SYSCFG_REMAPMEMORY_SYSTEMFLASH();
JumpToDFU = (void (*)(void)) (*((uint32_t *)(0x1FFF0000 + 4)));
/* Initialize user application's Stack Pointer */
__set_MSP(*(__IO uint32_t*) 0x1FFF0000);
JumpToDFU();
}
#endif /* STBOX1_RESTART_DFU */
/* HAL_Init */
HAL_Init();
/* Configure the System clock */
SystemClock_Config();
InitTargetPlatform();
#ifdef STBOX1_ENABLE_PRINTF
DisplayFirmwareInfo();
#endif /* STBOX1_ENABLE_PRINTF */
// /* Initialize the BlueNRG */屏蔽掉
// Init_BlueNRG_Stack();
//
// /* Initialize the BlueNRG Custom services */
// Init_BlueNRG_Custom_Services();
/* Reset the volatile for avoid to power off the board */
PowerButtonPressed =0;
uint32_t uhCapture = __HAL_TIM_GET_COUNTER(&TimCCHandle);
/* Start the TIM Base generation in interrupt mode 1Hz中断,用于输出电池电量*/
if(HAL_TIM_OC_Start_IT(&TimCCHandle, TIM_CHANNEL_1) != HAL_OK){
/* Starting Error */
Error_Handler();
}
/* Set the Capture Compare Register value */
__HAL_TIM_SET_COMPARE(&TimCCHandle, TIM_CHANNEL_1, (uhCapture + STBOX1_UPDATE_LED_BATTERY));
/* Start the TIM Base generation in interrupt mode 2Hz中断,用于输出环境传感器数据,后来把加速度传感器也改过来了*/
if(HAL_TIM_OC_Start_IT(&TimCCHandle, TIM_CHANNEL_3) != HAL_OK){
/* Starting Error */
Error_Handler();
}
/* Set the Capture Compare Register value */
__HAL_TIM_SET_COMPARE(&TimCCHandle, TIM_CHANNEL_3, (uhCapture + STBOX1_UPDATE_ENV));
// /* Start the TIM Base generation in interrupt mode */这个20Hz更新太快,看的眼花,我把触发信号改到通道3上了
// if(HAL_TIM_OC_Start_IT(&TimCCHandle, TIM_CHANNEL_4) != HAL_OK){
// /* Starting Error */
// Error_Handler();
// }
// /* Set the Capture Compare Register value */
// __HAL_TIM_SET_COMPARE(&TimCCHandle, TIM_CHANNEL_4, (uhCapture + STBOX1_UPDATE_INV));
while (1){
// if(set_connectable){蓝牙的全部注释掉
// uint32_t uhCapture = __HAL_TIM_GET_COUNTER(&TimCCHandle);
// set_connectable =0;
// setConnectable();
//
// /* Start the TIM Base generation in interrupt mode */
// if(HAL_TIM_OC_Start_IT(&TimCCHandle, TIM_CHANNEL_1) != HAL_OK){
// /* Starting Error */
// Error_Handler();
// }
//
// /* Set the Capture Compare Register value */
// __HAL_TIM_SET_COMPARE(&TimCCHandle, TIM_CHANNEL_1, (uhCapture + STBOX1_UPDATE_LED_BATTERY));
// }
/* Handle BLE event */
if(hci_event) {
hci_event =0;
hci_user_evt_proc();
}
/* Handle user button */
if(ButtonPressed) {
ButtonCallback();
ButtonPressed=0;
}
/* Power Off the SensorTile.box */
if(PowerButtonPressed){
BSP_BC_CmdSend(SHIPPING_MODE_ON);
PowerButtonPressed =0;
}
/* Reboot the Board */
if(RebootBoard) {
RebootBoard=0;
HAL_NVIC_SystemReset();
}
/* Battery Info Data */
if(SendBatteryInfo) {
SendBatteryInfo=0;
SendBatteryInfoData();
}
/* Environmental Data */
if(SendEnv) {
int32_t PressToSend;
uint16_t HumToSend;
int16_t TempToSend;
SendEnv=0;
/* Read all the Environmental Sensors */
ReadEnvironmentalData(&PressToSend,&HumToSend, &TempToSend);
STBOX1_PRINTF("Press:%d,Hum:%d,Temp:%d\r\n",PressToSend,HumToSend,TempToSend);
/* Send the Data with BLE */
// Environmental_Update(PressToSend,HumToSend,TempToSend);蓝牙发送注释掉,前面加上虚拟串口发送
}
/* Motion Data */
if(SendAccGyroMag) {
BSP_MOTION_SENSOR_Axes_t ACC_Value;
BSP_MOTION_SENSOR_Axes_t GYR_Value;
BSP_MOTION_SENSOR_Axes_t MAG_Value;
SendAccGyroMag=0;
/* Read the Inertial Sensors */
ReadInertialData(&ACC_Value,&GYR_Value,&MAG_Value);
STBOX1_PRINTF("ACC.x:%d,y:%d,z:%d\r\n",ACC_Value.x,ACC_Value.y,ACC_Value.z);
/* Send the Data with BLE */
// AccGyroMag_Update(&ACC_Value,&GYR_Value,&MAG_Value);蓝牙发送注释掉,前面加上虚拟串口发送
}
/* Blinking the Led */
if(BlinkLed) {
BlinkLed = 0;
LedToggleTargetPlatform();
}
/* Wait next event */
__WFI();
}
}
The following figure shows the final serial port output result:
The following plan is to modify the STM32CubeFunctionPack_STBOX1_V1.1.0\Projects\STM32L4R9ZI-SensorTile.box\Applications\BLEMLC routine to realize personnel activity information recognition and virtual serial port output.
This example also disconnects after a while on the app, which is a bad Bluetooth experience.
|