@@ -325,19 +325,22 @@ size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data,
325
325
if (self -> tx == NULL ) {
326
326
mp_raise_ValueError (translate ("No TX pin" ));
327
327
}
328
- bool write_err = false; // write error shouldn't disable interrupts
329
328
329
+ // Disable UART IRQ to avoid resource hazards in Rx IRQ handler
330
330
HAL_NVIC_DisableIRQ (self -> irq );
331
- HAL_StatusTypeDef ret = HAL_UART_Transmit (& self -> handle , (uint8_t * )data , len , HAL_MAX_DELAY );
332
- if (ret != HAL_OK ) {
333
- write_err = true;
334
- }
335
- HAL_UART_Receive_IT (& self -> handle , & self -> rx_char , 1 );
331
+ HAL_StatusTypeDef ret = HAL_UART_Transmit_IT (& self -> handle , (uint8_t * )data , len );
336
332
HAL_NVIC_EnableIRQ (self -> irq );
337
333
338
- if (write_err ) {
334
+ if (HAL_OK == ret ) {
335
+ HAL_UART_StateTypeDef Status = HAL_UART_GetState (& self -> handle );
336
+ while ((Status & HAL_UART_STATE_BUSY_TX ) == HAL_UART_STATE_BUSY_TX ) {
337
+ RUN_BACKGROUND_TASKS ;
338
+ Status = HAL_UART_GetState (& self -> handle );
339
+ }
340
+ } else {
339
341
mp_raise_ValueError (translate ("UART write error" ));
340
342
}
343
+
341
344
return len ;
342
345
}
343
346
@@ -359,6 +362,14 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *handle) {
359
362
}
360
363
}
361
364
365
+ #if (1 )
366
+ // TODO: Implement error handling here
367
+ #else
368
+ while (HAL_BUSY == errflag ) {
369
+ errflag = HAL_UART_Receive_IT (handle , & context -> rx_char , 1 );
370
+ }
371
+ #endif
372
+
362
373
return ;
363
374
}
364
375
}
@@ -436,6 +447,10 @@ STATIC void call_hal_irq(int uart_num) {
436
447
if (context != NULL ) {
437
448
HAL_NVIC_ClearPendingIRQ (context -> irq );
438
449
HAL_UART_IRQHandler (& context -> handle );
450
+
451
+ if (HAL_UART_ERROR_NONE != context -> handle .ErrorCode ) {
452
+ // TODO: Implement error handling here
453
+ }
439
454
}
440
455
}
441
456
0 commit comments