Skip to content

Commit 8ce1771

Browse files
committed
Block when there's not enough room in USART TX buffer, instead of failing. (#7)
1 parent 7c247fd commit 8ce1771

2 files changed

Lines changed: 20 additions & 14 deletions

File tree

src/main/drivers/usart.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,13 @@ _Static_assert( DOR0 == DOR1 &&
133133

134134
// Asynchronous receive buffer
135135
static byte_t s_rx_buf[ USART_COUNT ][ RX_BUF_SIZE ];
136-
static size_t s_rx_head[ USART_COUNT ];
137-
static size_t s_rx_tail[ USART_COUNT ];
136+
static volatile size_t s_rx_head[ USART_COUNT ];
137+
static volatile size_t s_rx_tail[ USART_COUNT ];
138138

139139
// Asynchronous transmit buffer
140140
static byte_t s_tx_buf[ USART_COUNT ][ TX_BUF_SIZE ];
141-
static size_t s_tx_head[ USART_COUNT ];
142-
static size_t s_tx_tail[ USART_COUNT ];
141+
static volatile size_t s_tx_head[ USART_COUNT ];
142+
static volatile size_t s_tx_tail[ USART_COUNT ];
143143

144144
/* ---------------------------------------------- PROCEDURE PROTOTYPES ---------------------------------------------- */
145145

@@ -380,14 +380,18 @@ bool usart_tx( usart_t usart, byte_t const * data, size_t size )
380380
{
381381
validate_usart( usart );
382382

383-
// Do nothing if there's no data
383+
// Validate size
384384
if( size == 0 )
385385
return( true );
386-
387-
// Ensure we have enough room in the TX buffer
388-
if( tx_buf_avail( usart ) < size )
386+
if( size >= TX_BUF_SIZE )
389387
return( false );
390388

389+
// Block until there's enough room in the buffer, allowing interrupts
390+
bool intrpt_en = sys_intrpt_enabled();
391+
while( tx_buf_avail( usart ) < size )
392+
sys_sei();
393+
sys_set_intrpt_enabled( intrpt_en );
394+
391395
// Copy data into TX buffer
392396
for( size_t idx = 0; idx < size; idx++ )
393397
{

src/main/drivers/usart.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,30 +138,32 @@ size_t usart_rx( usart_t usart, byte_t * data, size_t max_size );
138138
/**
139139
* @fn usart_tx( usart_t, byte_t const *, size_t )
140140
* @brief Asynchronously tranmits the specified data buffer.
141-
* @returns `true` if the buffer was successfully queued for transmission, or `false` if there was not enough space in
142-
* the transmit buffer for the message.
141+
* @returns `true` if the buffer was successfully queued for transmission, or `false` if the data buffer is too large.
142+
* @note Blocks until the data has been successfully queued for transmission.
143143
*/
144144
bool usart_tx( usart_t usart, byte_t const * data, size_t size );
145145

146146
/**
147147
* @fn usart_tx_str( usart_t, char const * )
148148
* @brief Asynchronously transmits the specified string.
149-
* @returns `true` if the string was successfully queued for transmission, or `false` if there was not enough space in
150-
* the transmit buffer for the message.
149+
* @returns `true` if the string was successfully queued for transmission, or `false` if the string is too long.
150+
* @note Blocks until the string has been successfully queued for transmission.
151151
*/
152152
bool usart_tx_str( usart_t usart, char const * str );
153153

154154
/**
155155
* @fn usart_tx_sync( usart_t, byte_t const *, size_t )
156156
* @brief Synchronously transmits the specified data buffer.
157-
* @note This function is intended for debugging. The `usart_tx()` function should be used for normal purposes.
157+
* @note This function blocks until the data buffer has been completely transmitted. This is intended for debugging -
158+
* the `usart_tx()` function should be used for normal purposes.
158159
*/
159160
void usart_tx_sync( usart_t usart, byte_t const * data, size_t size );
160161

161162
/**
162163
* @fn usart_tx_sync_str( usart_t, char const * )
163164
* @brief Synchronously transmits the specified string.
164-
* @note This function is intended for debugging. The `usart_tx_str()` function should be used for normal purposes.
165+
* @note This function blocks until the string has been completely transmitted. This is intended for debugging - the
166+
* `usart_tx_str()` function should be used for normal purposes.
165167
*/
166168
void usart_tx_sync_str( usart_t usart, char const * str );
167169

0 commit comments

Comments
 (0)