V1.5: 485查询前打印USART2硬件状态(CR1/SR/RxState/字节计数/缓冲索引),定位接收死因
This commit is contained in:
parent
7339ce31d9
commit
d2104fd90f
|
|
@ -148,6 +148,12 @@ static int feed_scale_parse_weight(const uint8_t *buf, uint16_t len, uint16_t *w
|
||||||
/* 通过 USART2 发送 485 查询命令 */
|
/* 通过 USART2 发送 485 查询命令 */
|
||||||
static void feed_scale_send_query(void)
|
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(); /* 只清软件缓冲(残留回声/垃圾),硬件接收常开不动 */
|
USART2_FlushRxBuf(); /* 只清软件缓冲(残留回声/垃圾),硬件接收常开不动 */
|
||||||
HAL_UART_Transmit(&huart2, (uint8_t *)RS485_WEIGHT_CMD, sizeof(RS485_WEIGHT_CMD), 100);
|
HAL_UART_Transmit(&huart2, (uint8_t *)RS485_WEIGHT_CMD, sizeof(RS485_WEIGHT_CMD), 100);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,7 @@ static uint8_t uart1_cmd_index = 0;
|
||||||
static uint32_t uart1_last_rx_tick = 0;
|
static uint32_t uart1_last_rx_tick = 0;
|
||||||
|
|
||||||
static uint8_t uart2_rx_byte = 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 uart5_rx_byte = 0;
|
||||||
static uint8_t uart2_cmd_buf[UART2_RX_BUF_SIZE] = {0};
|
static uint8_t uart2_cmd_buf[UART2_RX_BUF_SIZE] = {0};
|
||||||
static uint8_t uart2_cmd_index = 0;
|
static uint8_t uart2_cmd_index = 0;
|
||||||
|
|
@ -269,6 +270,8 @@ void USART2_StartRx(void)
|
||||||
* 485 查询前调用,丢弃缓冲里残留的回声/垃圾,让应答帧从干净状态开始。
|
* 485 查询前调用,丢弃缓冲里残留的回声/垃圾,让应答帧从干净状态开始。
|
||||||
* 注意:不要在这里 AbortReceive/重挂中断——实测会把接收搞死,
|
* 注意:不要在这里 AbortReceive/重挂中断——实测会把接收搞死,
|
||||||
* 硬件接收从开机起常开(ISR 逐字节重挂 + ORE 自愈),不需要动 */
|
* 硬件接收从开机起常开(ISR 逐字节重挂 + ORE 自愈),不需要动 */
|
||||||
|
uint16_t USART2_GetRxIndex(void) { return uart2_cmd_index; } /* 排障用 */
|
||||||
|
|
||||||
void USART2_FlushRxBuf(void)
|
void USART2_FlushRxBuf(void)
|
||||||
{
|
{
|
||||||
uart2_rx_ready = 0;
|
uart2_rx_ready = 0;
|
||||||
|
|
@ -317,6 +320,7 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
|
||||||
}
|
}
|
||||||
else if (huart->Instance == USART2) {
|
else if (huart->Instance == USART2) {
|
||||||
uint8_t ch = uart2_rx_byte;
|
uint8_t ch = uart2_rx_byte;
|
||||||
|
g_uart2_rx_byte_cnt++;
|
||||||
if (uart2_cmd_index < UART2_RX_BUF_SIZE - 1) {
|
if (uart2_cmd_index < UART2_RX_BUF_SIZE - 1) {
|
||||||
uart2_cmd_buf[uart2_cmd_index++] = ch;
|
uart2_cmd_buf[uart2_cmd_index++] = ch;
|
||||||
uart2_last_rx_tick = HAL_GetTick();
|
uart2_last_rx_tick = HAL_GetTick();
|
||||||
|
|
|
||||||
|
|
@ -36,7 +36,9 @@ void USART2_StartRx(void); /*
|
||||||
void UART5_StartRx(void); /* 启动 UART5 中断接收(HLW8032) */
|
void UART5_StartRx(void); /* 启动 UART5 中断接收(HLW8032) */
|
||||||
void USART2_Tick(void); /* 触发 USART2 帧超时自动提交 */
|
void USART2_Tick(void); /* 触发 USART2 帧超时自动提交 */
|
||||||
uint16_t USART2_GetFrame(uint8_t *out, uint16_t out_size);
|
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); /* 检查是否收到指定命令(不区分大小写) */
|
int USART1_CheckCommand(const char *cmd); /* 检查是否收到指定命令(不区分大小写) */
|
||||||
const char *USART1_GetCommand(void); /* 获取当前接收到的命令字符串 */
|
const char *USART1_GetCommand(void); /* 获取当前接收到的命令字符串 */
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue