From 05216a2cb155747e9d93a15519c7df88836cdb54 Mon Sep 17 00:00:00 2001 From: kyz Date: Tue, 1 Mar 2022 15:51:46 +0800 Subject: [PATCH] uart send add timeout --- Core/Src/user_rs485.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/Core/Src/user_rs485.c b/Core/Src/user_rs485.c index 3d79175..66f13c6 100644 --- a/Core/Src/user_rs485.c +++ b/Core/Src/user_rs485.c @@ -38,10 +38,12 @@ void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandle) int32_t UART2_Send( uint8_t *data, uint16_t data_size ) { - - while (Uart2Ready != SET) - { - } + uint32_t tick; + tick = HAL_GetTick() + 1000; + while (Uart2Ready != SET){ + if( tick < HAL_GetTick() ) + break; + } Uart2Ready = RESET; RS485_RTS1_TX(); @@ -63,11 +65,14 @@ int32_t UART2_Send( uint8_t *data, uint16_t data_size ) int32_t UART1_Send( uint8_t *data, uint16_t data_size ) { - - while (Uart1Ready != SET) + uint32_t tick; + tick = HAL_GetTick() + 1000; + while (Uart2Ready != SET) { + if( tick < HAL_GetTick() ) + break; } - + Uart1Ready = RESET; if (HAL_UART_Transmit_IT(&huart1, data, data_size) != HAL_OK) { @@ -117,6 +122,7 @@ void UART2_Recv_Restart( void ) memset(rx_buffer, 0, sizeof(rx_buffer)); rx_count=0; UART2_Recv_Start(); + Uart2Ready = SET; } int32_t get_rx_buffer( uint8_t **rx_buf, uint16_t *rx_cnt ) -- 2.34.1