@@ -323,29 +323,23 @@ size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data,
323323 struct io_descriptor * io ;
324324 usart_async_get_io_descriptor (usart_desc_p , & io );
325325
326+ // Start writing characters. This is non-blocking and will
327+ // return immediately after setting up the write.
326328 if (io_write (io , data , len ) < 0 ) {
327329 * errcode = MP_EAGAIN ;
328330 return MP_STREAM_ERROR ;
329331 }
330332
331- // Wait until write is complete or timeout.
332- bool done = false;
333- uint64_t start_ticks = ticks_ms ;
334- // Busy-wait for timeout.
335- while (ticks_ms - start_ticks < self -> timeout_ms ) {
336- if (usart_async_is_tx_empty (usart_desc_p )) {
337- done = true;
333+ // Busy-wait until all characters transmitted.
334+ struct usart_async_status async_status ;
335+ while (true) {
336+ usart_async_get_status (usart_desc_p , & async_status );
337+ if (async_status .txcnt >= len ) {
338338 break ;
339339 }
340340 RUN_BACKGROUND_TASKS ;
341341 }
342342
343- if (!done ) {
344- * errcode = MP_EAGAIN ;
345- return MP_STREAM_ERROR ;
346- }
347-
348- // All the characters got written.
349343 return len ;
350344}
351345
@@ -368,6 +362,14 @@ void common_hal_busio_uart_set_baudrate(busio_uart_obj_t *self, uint32_t baudrat
368362 self -> baudrate = baudrate ;
369363}
370364
365+ mp_float_t common_hal_busio_uart_get_timeout (busio_uart_obj_t * self ) {
366+ return (mp_float_t ) (self -> timeout_ms / 1000.0f );
367+ }
368+
369+ void common_hal_busio_uart_set_timeout (busio_uart_obj_t * self , mp_float_t timeout ) {
370+ self -> timeout_ms = timeout * 1000 ;
371+ }
372+
371373uint32_t common_hal_busio_uart_rx_characters_available (busio_uart_obj_t * self ) {
372374 // This assignment is only here because the usart_async routines take a *const argument.
373375 struct usart_async_descriptor * const usart_desc_p = (struct usart_async_descriptor * const ) & self -> usart_desc ;
@@ -383,12 +385,14 @@ void common_hal_busio_uart_clear_rx_buffer(busio_uart_obj_t *self) {
383385
384386}
385387
388+ // True if there are no characters still to be written.
386389bool common_hal_busio_uart_ready_to_tx (busio_uart_obj_t * self ) {
387390 if (self -> tx_pin == NO_PIN ) {
388391 return false;
389392 }
390393 // This assignment is only here because the usart_async routines take a *const argument.
391- const struct _usart_async_device * const usart_device_p =
392- (struct _usart_async_device * const ) & self -> usart_desc .device ;
393- return _usart_async_is_byte_sent (usart_device_p );
394+ struct usart_async_descriptor * const usart_desc_p = (struct usart_async_descriptor * const ) & self -> usart_desc ;
395+ struct usart_async_status async_status ;
396+ usart_async_get_status (usart_desc_p , & async_status );
397+ return !(async_status .flags & USART_ASYNC_STATUS_BUSY );
394398}
0 commit comments