V1.5: 485取帧改原子操作USART2_PollFrame,消除Tick/GetFrame交接丢帧窗口

This commit is contained in:
helei 2026-08-22 13:06:50 +08:00
parent 05700826d1
commit c5b0922004
4 changed files with 27 additions and 5 deletions

View File

@ -163,9 +163,7 @@ static void feed_scale_send_query(void)
/* 读取 485 返回帧 */
static int feed_scale_read_frame(uint8_t *buf, uint16_t max_len)
{
uint16_t len = USART2_GetFrame(buf, max_len);
if (len == 0) return 0;
return (int)len;
return (int)USART2_PollFrame(buf, max_len); /* 原子取帧,无交接状态 */
}
/* 上报当前状态到平台: 开关、重量、单次投料量、去皮重量。返回 0=已发出 */
@ -379,8 +377,7 @@ void feedScaleProcess(void)
uint16_t cur_weight;
int32_t diff;
/* 触发 USART2 帧超时自动提交 */
USART2_Tick();
/* 原子取帧在 feed_scale_read_frame 内完成,无需再 Tick */
/* 空闲时 10s 查询一次; 投喂时 1s 查询一次;
*/

View File

@ -368,6 +368,28 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
}
}
/* 原子取帧20ms 静默判帧完整 → 直接拷出并清缓冲,一步完成。
* Tick()+GetFrame() /
* 485 */
uint16_t USART2_PollFrame(uint8_t *out, uint16_t out_size)
{
if (uart2_cmd_index > 0 &&
(HAL_GetTick() - uart2_last_rx_tick) > UART2_CMD_TIMEOUT_MS) {
/* 排障帧提交时打印内容485 丢帧定位) */
char hex[3 * 24 + 1] = {0};
uint16_t n0 = uart2_cmd_index < 24 ? uart2_cmd_index : 24;
for (uint16_t i = 0; i < n0; i++) snprintf(hex + i * 3, 4, "%02X ", uart2_cmd_buf[i]);
log_info("> UART2: 帧提交 %d 字节: %s", (int)uart2_cmd_index, hex);
uint16_t len = uart2_cmd_index < out_size ? uart2_cmd_index : out_size - 1;
memcpy(out, uart2_cmd_buf, len);
out[len] = 0;
uart2_cmd_index = 0;
uart2_rx_ready = 0;
return len;
}
return 0;
}
/* 在 USART2_GetFrame 被轮询前20ms 无新字节自动提交帧 */
void USART2_Tick(void)
{

View File

@ -36,6 +36,7 @@ void USART2_StartRx(void); /*
void UART5_StartRx(void); /* 启动 UART5 中断接收HLW8032 */
void USART2_Tick(void); /* 触发 USART2 帧超时自动提交 */
uint16_t USART2_GetFrame(uint8_t *out, uint16_t out_size);
uint16_t USART2_PollFrame(uint8_t *out, uint16_t out_size); /* 原子取帧(判静默+拷出一步完成) */
void USART2_FlushRxBuf(void);
extern volatile uint32_t g_uart2_rx_byte_cnt;
uint16_t USART2_GetRxIndex(void); /* USART2 收字节计数485 排障) */ /* 清 USART2 软件接收缓冲485 查询前调用,不动硬件) */ /* 从 USART2 取一帧原始字节 */

View File

@ -195,3 +195,5 @@
USART1/USART5 自愈同样加固
- 485 丢帧根因定位并加固:上位机秒回时回复帧头被 485 收发器切换延迟切掉(真秤有处理延迟不受影响);
现 400ms 无好帧或收到坏帧自动补发一次查询,仍不行才报无应答
- 485 取帧改原子操作USART2_PollFrame 判静默+拷出+清缓冲一步完成,替代 Tick 置标志/GetFrame 取帧
的两步交接(交接窗口会被查询补发的清缓冲打断丢帧)