From 86b01870490b690a7766da481e194a7f0424215f Mon Sep 17 00:00:00 2001 From: Chris Vig Date: Tue, 26 Aug 2025 10:23:52 -0500 Subject: [PATCH] Block when there's not enough room in USART TX buffer, instead of failing. --- src/main/drivers/usart.c | 20 ++++++++++++-------- src/main/drivers/usart.h | 14 ++++++++------ 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/main/drivers/usart.c b/src/main/drivers/usart.c index e11609f..6538ea9 100644 --- a/src/main/drivers/usart.c +++ b/src/main/drivers/usart.c @@ -133,13 +133,13 @@ _Static_assert( DOR0 == DOR1 && // Asynchronous receive buffer static byte_t s_rx_buf[ USART_COUNT ][ RX_BUF_SIZE ]; -static size_t s_rx_head[ USART_COUNT ]; -static size_t s_rx_tail[ USART_COUNT ]; +static volatile size_t s_rx_head[ USART_COUNT ]; +static volatile size_t s_rx_tail[ USART_COUNT ]; // Asynchronous transmit buffer static byte_t s_tx_buf[ USART_COUNT ][ TX_BUF_SIZE ]; -static size_t s_tx_head[ USART_COUNT ]; -static size_t s_tx_tail[ USART_COUNT ]; +static volatile size_t s_tx_head[ USART_COUNT ]; +static volatile size_t s_tx_tail[ USART_COUNT ]; /* ---------------------------------------------- PROCEDURE PROTOTYPES ---------------------------------------------- */ @@ -380,14 +380,18 @@ bool usart_tx( usart_t usart, byte_t const * data, size_t size ) { validate_usart( usart ); - // Do nothing if there's no data + // Validate size if( size == 0 ) return( true ); - - // Ensure we have enough room in the TX buffer - if( tx_buf_avail( usart ) < size ) + if( size >= TX_BUF_SIZE ) return( false ); + // Block until there's enough room in the buffer, allowing interrupts + bool intrpt_en = sys_intrpt_enabled(); + while( tx_buf_avail( usart ) < size ) + sys_sei(); + sys_set_intrpt_enabled( intrpt_en ); + // Copy data into TX buffer for( size_t idx = 0; idx < size; idx++ ) { diff --git a/src/main/drivers/usart.h b/src/main/drivers/usart.h index b9dc42d..cb8cea1 100644 --- a/src/main/drivers/usart.h +++ b/src/main/drivers/usart.h @@ -138,30 +138,32 @@ size_t usart_rx( usart_t usart, byte_t * data, size_t max_size ); /** * @fn usart_tx( usart_t, byte_t const *, size_t ) * @brief Asynchronously tranmits the specified data buffer. - * @returns `true` if the buffer was successfully queued for transmission, or `false` if there was not enough space in - * the transmit buffer for the message. + * @returns `true` if the buffer was successfully queued for transmission, or `false` if the data buffer is too large. + * @note Blocks until the data has been successfully queued for transmission. */ bool usart_tx( usart_t usart, byte_t const * data, size_t size ); /** * @fn usart_tx_str( usart_t, char const * ) * @brief Asynchronously transmits the specified string. - * @returns `true` if the string was successfully queued for transmission, or `false` if there was not enough space in - * the transmit buffer for the message. + * @returns `true` if the string was successfully queued for transmission, or `false` if the string is too long. + * @note Blocks until the string has been successfully queued for transmission. */ bool usart_tx_str( usart_t usart, char const * str ); /** * @fn usart_tx_sync( usart_t, byte_t const *, size_t ) * @brief Synchronously transmits the specified data buffer. - * @note This function is intended for debugging. The `usart_tx()` function should be used for normal purposes. + * @note This function blocks until the data buffer has been completely transmitted. This is intended for debugging - + * the `usart_tx()` function should be used for normal purposes. */ void usart_tx_sync( usart_t usart, byte_t const * data, size_t size ); /** * @fn usart_tx_sync_str( usart_t, char const * ) * @brief Synchronously transmits the specified string. - * @note This function is intended for debugging. The `usart_tx_str()` function should be used for normal purposes. + * @note This function blocks until the string has been completely transmitted. This is intended for debugging - the + * `usart_tx_str()` function should be used for normal purposes. */ void usart_tx_sync_str( usart_t usart, char const * str );