Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions src/main/drivers/usart.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 ---------------------------------------------- */

Expand Down Expand Up @@ -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++ )
{
Expand Down
14 changes: 8 additions & 6 deletions src/main/drivers/usart.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down