学习st官方例程STM32CubeFunctionPack_STBOX1_V1.1.0\Projects\STM32L4R9ZI-SensorTile.box\Applications\BLESensors,刷进去后,蓝牙不稳定,连接上后,能显示各个环境传感器数据,但是没过一分钟,蓝牙就连接失败。可能跟手机的蓝牙兼容性不好,索性自己改代码,通过预留的USB虚拟调试串口进行数据查看。
主要修改了主函数,将蓝牙相关的初始化函数都屏蔽掉了。下面是代码,修改的地方都用中文标注了。
调试注意事项,启动调试时,一定在USB虚拟串口map成功后打开串口,结束调试时,先关调试助手的串口,再停止程序运行。如果忘关调试助手的串口就停止程序运行,助手软件会疯狂报错,需要强制终止程序才行。
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();
}
}
下图示最终的串口输出结果:
下面计划改造STM32CubeFunctionPack_STBOX1_V1.1.0\Projects\STM32L4R9ZI-SensorTile.box\Applications\BLEMLC 这个例程,实现人员活动信息识别和虚拟串口输出。
这个例程在app上也是连上一会儿就断,这个蓝牙体验不好。
|