V1.5: 485查询前打印USART2硬件状态(CR1/SR/RxState/字节计数/缓冲索引),定位接收死因

This commit is contained in:
helei 2026-08-22 12:36:25 +08:00
parent 7339ce31d9
commit d2104fd90f
3 changed files with 13 additions and 1 deletions

View File

@ -148,6 +148,12 @@ static int feed_scale_parse_weight(const uint8_t *buf, uint16_t len, uint16_t *w
/* 通过 USART2 发送 485 查询命令 */
static void feed_scale_send_query(void)
{
/* 排障打印接收通道硬件状态CR1 含 RXNEIE 位、SR 含 ORE/FE/RXNE 标志、
* HAL */
log_info("> FEED: UART2 CR1=%04X SR=%04X st=%u cnt=%lu idx=%u",
(unsigned)USART2->CR1, (unsigned)USART2->SR,
(unsigned)huart2.RxState,
(unsigned long)g_uart2_rx_byte_cnt, (unsigned)USART2_GetRxIndex());
USART2_FlushRxBuf(); /* 只清软件缓冲(残留回声/垃圾),硬件接收常开不动 */
HAL_UART_Transmit(&huart2, (uint8_t *)RS485_WEIGHT_CMD, sizeof(RS485_WEIGHT_CMD), 100);
}

View File

@ -29,6 +29,7 @@ static uint8_t uart1_cmd_index = 0;
static uint32_t uart1_last_rx_tick = 0;
static uint8_t uart2_rx_byte = 0;
volatile uint32_t g_uart2_rx_byte_cnt = 0; /* USART2 累计收到的字节数485 排障用) */
static uint8_t uart5_rx_byte = 0;
static uint8_t uart2_cmd_buf[UART2_RX_BUF_SIZE] = {0};
static uint8_t uart2_cmd_index = 0;
@ -269,6 +270,8 @@ void USART2_StartRx(void)
* 485 /
* AbortReceive/
* ISR + ORE */
uint16_t USART2_GetRxIndex(void) { return uart2_cmd_index; } /* 排障用 */
void USART2_FlushRxBuf(void)
{
uart2_rx_ready = 0;
@ -317,6 +320,7 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
}
else if (huart->Instance == USART2) {
uint8_t ch = uart2_rx_byte;
g_uart2_rx_byte_cnt++;
if (uart2_cmd_index < UART2_RX_BUF_SIZE - 1) {
uart2_cmd_buf[uart2_cmd_index++] = ch;
uart2_last_rx_tick = HAL_GetTick();

View File

@ -36,7 +36,9 @@ 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);
void USART2_FlushRxBuf(void); /* 清 USART2 软件接收缓冲485 查询前调用,不动硬件) */ /* 从 USART2 取一帧原始字节 */
void USART2_FlushRxBuf(void);
extern volatile uint32_t g_uart2_rx_byte_cnt;
uint16_t USART2_GetRxIndex(void); /* USART2 收字节计数485 排障) */ /* 清 USART2 软件接收缓冲485 查询前调用,不动硬件) */ /* 从 USART2 取一帧原始字节 */
int USART1_CheckCommand(const char *cmd); /* 检查是否收到指定命令(不区分大小写) */
const char *USART1_GetCommand(void); /* 获取当前接收到的命令字符串 */