常用稳定,易操作的实现
static uint8_t gString[200]; // 开的越大传的越慢 COM_PORT_INFO g_uart_struct[COM_PORT_NUM]; void COMx_Send(uint8_t data[],uint16_t len, COMM_PORT_TYPE chID) { uint16_t i; UART_HandleTypeDef UsartCom; switch(chID) { case GPS_UART_INDEX: UsartCom = huart4; break; case TTL_UART_INDEX: UsartCom = huart3; break; case BLE_UART_INDEX: UsartCom = huart1; break; default: return; } for(i=0;i<len;i++) { while((__HAL_UART_GET_FLAG((&UsartCom), UART_FLAG_TXE) ? SET : RESET) == RESET); (&UsartCom)->Instance->DR = data[i]; } } void COMx_Revd(COMM_PORT_TYPE chID) { uint16_t timeout; uint16_t max_len; UART_HandleTypeDef UsartCom; if (chID == GPS_UART_INDEX) { UsartCom = huart4; timeout = GPS_RX_FRAME_TIMEOUT; max_len = USART_GPS_MAX_Rx_BUF_LEN; } else if(chID == TTL_UART_INDEX) { UsartCom = huart3; timeout = TTL_RX_FRAME_TIMEOUT; max_len = USART_TTL_MAX_Rx_BUF_LEN; } else if(chID == BLE_UART_INDEX) { UsartCom = huart1; timeout = BLE_RX_FRAME_TIMEOUT; max_len = USART_BLE_MAX_Rx_BUF_LEN; } if(__HAL_UART_GET_FLAG((&UsartCom), USART_SR_RXNE) != RESET) { if(g_uart_struct[chID].Rx_counter >= max_len) { g_uart_struct[chID].Rx_counter--; } g_uart_struct[chID].RecvDataBuff[g_uart_struct[chID].Rx_counter++] = READ_REG((&UsartCom)->Instance->DR); g_uart_struct[chID].Rx_timeout = timeout; } __HAL_UART_CLEAR_FLAG((&UsartCom), USART_SR_PE|USART_SR_FE|USART_SR_NE|USART_SR_ORE); } void CommProc_uart(void) { uint8_t i; uint8_t *data_buf; uint16_t data_len; for(i=GPS_UART_INDEX;i<=BLE_UART_INDEX;i++) { if((i == TTL_UART_INDEX) && (g_uart_struct[i].RxDoneFlag)) { g_uart_struct[i].RxDoneFlag = false; data_len = g_uart_struct[i].Rx_counter; data_buf = g_uart_struct[i].RecvDataBuff; /* user add */ memset(&g_uart_struct[i].RecvDataBuff,0,data_len); g_uart_struct[i].Rx_counter = 0; } } } void CheckUartRxDone(void) { static uint32_t sys_tick_pre_val=0; static uint32_t sys_tick_cur_val=0; uint32_t i,past_ms_val; sys_tick_cur_val = HAL_GetTick(); past_ms_val = sys_tick_cur_val-sys_tick_pre_val; for(i=GPS_UART_INDEX;i<=BLE_UART_INDEX;i++) { if(g_uart_struct[i].Rx_timeout > 0) { if(g_uart_struct[i].Rx_timeout >= past_ms_val) { g_uart_struct[i].Rx_timeout -= past_ms_val; } else { g_uart_struct[i].Rx_timeout = 0; } if(g_uart_struct[i].Rx_timeout == 0) { g_uart_struct[i].RxDoneFlag = true; } } } sys_tick_pre_val = sys_tick_cur_val; } void LocalPrintf(const char *fmt,...) { va_list ap; memset(gString, 0, sizeof(gString)); va_start(ap,fmt); vsprintf((char*)gString,fmt,ap); COMx_Send(gString, sizeof(gString), TTL_UART_INDEX); va_end(ap); }
#define USART_GPS_MAX_Rx_BUF_LEN ((uint16_t)512) #define GPS_RX_FRAME_TIMEOUT ((uint16_t)50) #define USART_TTL_MAX_Rx_BUF_LEN ((uint16_t)512) #define TTL_RX_FRAME_TIMEOUT ((uint16_t)50) #define USART_BLE_MAX_Rx_BUF_LEN ((uint16_t)512) #define BLE_RX_FRAME_TIMEOUT ((uint16_t)150)///帧超时时间 #define COM_PORT_NUM ((uint8_t)3) typedef struct { uint16_t Rx_counter; uint16_t Rx_timeout; //ms bool RxDoneFlag; //断包标志 uint8_t RecvDataBuff[1000]; //包缓存 }COM_PORT_INFO; typedef enum { GPS_UART_INDEX = 0, /* GPS口 */ TTL_UART_INDEX, /* 维护口*/ BLE_UART_INDEX, /* 蓝牙口 */ }COMM_PORT_TYPE; /* 端口类型 */ extern COM_PORT_INFO g_uart_struct[COM_PORT_NUM]; void COMx_Revd(COMM_PORT_TYPE chID); void COMx_Send(uint8_t data[],uint16_t len, COMM_PORT_TYPE chID); void CommProc_uart(void); void CheckUartRxDone(void); void LocalPrintf(const char *fmt,...);