@@ -1611,15 +1611,17 @@ int uart_tx_chars(uart_port_t uart_num, const char *buffer, uint32_t len)
16111611
16121612// Per transaction in the ring buffer:
16131613// A data description item, followed by one or more data chunk items
1614- static int uart_tx_all (uart_port_t uart_num , const char * src , size_t size , bool brk_en , int brk_len )
1614+ static int uart_tx_all (uart_port_t uart_num , const char * src , size_t size , bool brk_en , int brk_len , TickType_t ticks_to_wait )
16151615{
16161616 if (size == 0 ) {
16171617 return 0 ;
16181618 }
1619- size_t original_size = size ;
1619+ size_t bytes_transmitted = 0 ;
16201620
16211621 //lock for uart_tx
1622- xSemaphoreTake (p_uart_obj [uart_num ]-> tx_mux , (TickType_t )portMAX_DELAY );
1622+ if (xSemaphoreTake (p_uart_obj [uart_num ]-> tx_mux , (TickType_t )ticks_to_wait ) != pdTRUE ) {
1623+ return -1 ;
1624+ }
16231625#if PROTECT_APB
16241626 esp_pm_lock_acquire (p_uart_obj [uart_num ]-> pm_lock );
16251627#endif
@@ -1647,7 +1649,10 @@ static int uart_tx_all(uart_port_t uart_num, const char *src, size_t size, bool
16471649 // Accuire space for data description and data chunk, this gives a strong
16481650 // guarantee that data chunk can be copied in one go
16491651 uart_tx_data_t * tx_data = NULL ;
1650- xRingbufferSendAcquire (p_uart_obj [uart_num ]-> tx_ring_buf , (void * * )& tx_data , acquire_size , portMAX_DELAY );
1652+ BaseType_t res = xRingbufferSendAcquire (p_uart_obj [uart_num ]-> tx_ring_buf , (void * * )& tx_data , acquire_size , ticks_to_wait );
1653+ if (res != pdTRUE ) {
1654+ break ; // Timeout
1655+ }
16511656
16521657 size_t send_size = acquire_size - sizeof (uart_tx_data_t );
16531658
@@ -1670,11 +1675,14 @@ static int uart_tx_all(uart_port_t uart_num, const char *src, size_t size, bool
16701675 bytes_transmitted += send_size ;
16711676 }
16721677
1673- uart_enable_tx_intr (uart_num , 1 , UART_THRESHOLD_NUM (uart_num , UART_EMPTY_THRESH_DEFAULT ));
1678+ // If at least some data has been sent to the ring buffer, enable TX interrupt to start sending
1679+ if (bytes_transmitted > 0 ) {
1680+ uart_enable_tx_intr (uart_num , 1 , UART_THRESHOLD_NUM (uart_num , UART_EMPTY_THRESH_DEFAULT ));
1681+ }
16741682 } else {
16751683 while (size ) {
16761684 //semaphore for tx_fifo available
1677- if (pdTRUE == xSemaphoreTake (p_uart_obj [uart_num ]-> tx_fifo_sem , ( TickType_t ) portMAX_DELAY )) {
1685+ if (pdTRUE == xSemaphoreTake (p_uart_obj [uart_num ]-> tx_fifo_sem , ticks_to_wait )) {
16781686 uint32_t sent = uart_enable_tx_write_fifo (uart_num , (const uint8_t * ) src , size );
16791687 if (sent < size ) {
16801688 p_uart_obj [uart_num ]-> tx_waiting_fifo = true;
@@ -1693,23 +1701,40 @@ static int uart_tx_all(uart_port_t uart_num, const char *src, size_t size, bool
16931701 uart_hal_tx_break (& (uart_context [uart_num ].hal ), brk_len );
16941702 uart_hal_ena_intr_mask (& (uart_context [uart_num ].hal ), UART_INTR_TX_BRK_DONE );
16951703 UART_EXIT_CRITICAL (& (uart_context [uart_num ].spinlock ));
1696- xSemaphoreTake (p_uart_obj [uart_num ]-> tx_brk_sem , (TickType_t )portMAX_DELAY );
1704+ if (pdFALSE == xSemaphoreTake (p_uart_obj [uart_num ]-> tx_brk_sem , ticks_to_wait )) {
1705+ uart_hal_disable_intr_mask (& (uart_context [uart_num ].hal ), UART_INTR_TX_BRK_DONE );
1706+ bytes_transmitted = -1 ; // Indicate failure due to timeout waiting for break to complete
1707+ }
16971708 }
16981709 xSemaphoreGive (p_uart_obj [uart_num ]-> tx_fifo_sem );
16991710 }
1711+
17001712#if PROTECT_APB
17011713 esp_pm_lock_release (p_uart_obj [uart_num ]-> pm_lock );
17021714#endif
17031715 xSemaphoreGive (p_uart_obj [uart_num ]-> tx_mux );
1704- return original_size ;
1716+
1717+ // If no bytes were transmitted due to timeout, return -1 to indicate failure
1718+ if (bytes_transmitted == 0 ) {
1719+ return -1 ;
1720+ }
1721+ return bytes_transmitted ;
17051722}
17061723
17071724int uart_write_bytes (uart_port_t uart_num , const void * src , size_t size )
17081725{
17091726 ESP_RETURN_ON_FALSE ((uart_num < UART_NUM_MAX ), (-1 ), UART_TAG , "uart_num error" );
17101727 ESP_RETURN_ON_FALSE ((p_uart_obj [uart_num ] != NULL ), (-1 ), UART_TAG , "uart driver error" );
17111728 ESP_RETURN_ON_FALSE (src , (-1 ), UART_TAG , "buffer null" );
1712- return uart_tx_all (uart_num , src , size , 0 , 0 );
1729+ return uart_tx_all (uart_num , src , size , 0 , 0 , portMAX_DELAY );
1730+ }
1731+
1732+ int uart_write_bytes_with_timeout (uart_port_t uart_num , const void * src , size_t size , TickType_t ticks_to_wait )
1733+ {
1734+ ESP_RETURN_ON_FALSE ((uart_num < UART_NUM_MAX ), (-1 ), UART_TAG , "uart_num error" );
1735+ ESP_RETURN_ON_FALSE ((p_uart_obj [uart_num ] != NULL ), (-1 ), UART_TAG , "uart driver error" );
1736+ ESP_RETURN_ON_FALSE (src , (-1 ), UART_TAG , "buffer null" );
1737+ return uart_tx_all (uart_num , src , size , 0 , 0 , ticks_to_wait );
17131738}
17141739
17151740int uart_write_bytes_with_break (uart_port_t uart_num , const void * src , size_t size , int brk_len )
@@ -1719,7 +1744,7 @@ int uart_write_bytes_with_break(uart_port_t uart_num, const void *src, size_t si
17191744 ESP_RETURN_ON_FALSE ((size > 0 ), (-1 ), UART_TAG , "uart size error" );
17201745 ESP_RETURN_ON_FALSE ((src ), (-1 ), UART_TAG , "uart data null" );
17211746 ESP_RETURN_ON_FALSE ((brk_len > 0 && brk_len < 256 ), (-1 ), UART_TAG , "break_num error" );
1722- return uart_tx_all (uart_num , src , size , 1 , brk_len );
1747+ return uart_tx_all (uart_num , src , size , 1 , brk_len , portMAX_DELAY );
17231748}
17241749
17251750static bool uart_check_buf_full (uart_port_t uart_num )
0 commit comments