@@ -331,13 +331,37 @@ int uart_rx_char(pyb_uart_obj_t *self) {
331
331
}
332
332
}
333
333
334
+ // Waits at most timeout milliseconds for TX register to become empty.
335
+ // Returns true if can write, false if can't.
336
+ STATIC bool uart_tx_wait (pyb_uart_obj_t * self , uint32_t timeout ) {
337
+ uint32_t start = HAL_GetTick ();
338
+ for (;;) {
339
+ if (__HAL_UART_GET_FLAG (& self -> uart , UART_FLAG_TXE )) {
340
+ return true; // tx register is empty
341
+ }
342
+ if (HAL_GetTick () - start >= timeout ) {
343
+ return false; // timeout
344
+ }
345
+ __WFI ();
346
+ }
347
+ }
348
+
349
+ STATIC HAL_StatusTypeDef uart_tx_data (pyb_uart_obj_t * self , uint8_t * data , uint16_t len ) {
350
+ // The timeout specified here is for waiting for the TX data register to
351
+ // become empty (ie between chars), as well as for the final char to be
352
+ // completely transferred. The default value for timeout_char is long
353
+ // enough for 1 char, but we need to double it to wait for the last char
354
+ // to be transferred to the data register, and then to be transmitted.
355
+ return HAL_UART_Transmit (& self -> uart , data , len , 2 * self -> timeout_char );
356
+ }
357
+
334
358
STATIC void uart_tx_char (pyb_uart_obj_t * uart_obj , int c ) {
335
359
uint8_t ch = c ;
336
- HAL_UART_Transmit ( & uart_obj -> uart , & ch , 1 , uart_obj -> timeout );
360
+ uart_tx_data ( uart_obj , & ch , 1 );
337
361
}
338
362
339
363
void uart_tx_strn (pyb_uart_obj_t * uart_obj , const char * str , uint len ) {
340
- HAL_UART_Transmit ( & uart_obj -> uart , (uint8_t * )str , len , uart_obj -> timeout );
364
+ uart_tx_data ( uart_obj , (uint8_t * )str , len );
341
365
}
342
366
343
367
void uart_tx_strn_cooked (pyb_uart_obj_t * uart_obj , const char * str , uint len ) {
@@ -480,9 +504,16 @@ STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, mp_uint_t n_args, con
480
504
nlr_raise (mp_obj_new_exception_msg_varg (& mp_type_ValueError , "UART(%d) does not exist" , self -> uart_id ));
481
505
}
482
506
483
- // set timeouts
507
+ // set timeout
484
508
self -> timeout = args [5 ].u_int ;
509
+
510
+ // set timeout_char
511
+ // make sure it is at least as long as a whole character (13 bits to be safe)
485
512
self -> timeout_char = args [6 ].u_int ;
513
+ uint32_t min_timeout_char = 13000 / init -> BaudRate + 1 ;
514
+ if (self -> timeout_char < min_timeout_char ) {
515
+ self -> timeout_char = min_timeout_char ;
516
+ }
486
517
487
518
// setup the read buffer
488
519
m_del (byte , self -> read_buf , self -> read_buf_len << self -> char_width );
@@ -689,8 +720,13 @@ STATIC mp_obj_t pyb_uart_writechar(mp_obj_t self_in, mp_obj_t char_in) {
689
720
// get the character to write (might be 9 bits)
690
721
uint16_t data = mp_obj_get_int (char_in );
691
722
692
- // write the data
693
- HAL_StatusTypeDef status = HAL_UART_Transmit (& self -> uart , (uint8_t * )& data , 1 , self -> timeout );
723
+ // write the character
724
+ HAL_StatusTypeDef status ;
725
+ if (uart_tx_wait (self , self -> timeout )) {
726
+ status = uart_tx_data (self , (uint8_t * )& data , 1 );
727
+ } else {
728
+ status = HAL_TIMEOUT ;
729
+ }
694
730
695
731
if (status != HAL_OK ) {
696
732
mp_hal_raise (status );
@@ -807,8 +843,14 @@ STATIC mp_uint_t pyb_uart_write(mp_obj_t self_in, const void *buf_in, mp_uint_t
807
843
return MP_STREAM_ERROR ;
808
844
}
809
845
846
+ // wait to be able to write the first character
847
+ if (!uart_tx_wait (self , self -> timeout )) {
848
+ * errcode = EAGAIN ;
849
+ return MP_STREAM_ERROR ;
850
+ }
851
+
810
852
// write the data
811
- HAL_StatusTypeDef status = HAL_UART_Transmit ( & self -> uart , (uint8_t * )buf , size >> self -> char_width , self -> timeout );
853
+ HAL_StatusTypeDef status = uart_tx_data ( self , (uint8_t * )buf , size >> self -> char_width );
812
854
813
855
if (status == HAL_OK ) {
814
856
// return number of bytes written
0 commit comments