@@ -325,19 +325,23 @@ 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
+ }
341
+ else {
339
342
mp_raise_ValueError (translate ("UART write error" ));
340
343
}
344
+
341
345
return len ;
342
346
}
343
347
@@ -359,6 +363,14 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *handle) {
359
363
}
360
364
}
361
365
366
+ #if (1 )
367
+ // TODO: Implement error handling here
368
+ #else
369
+ while (HAL_BUSY == errflag ) {
370
+ errflag = HAL_UART_Receive_IT (handle , & context -> rx_char , 1 );
371
+ }
372
+ #endif
373
+
362
374
return ;
363
375
}
364
376
}
@@ -436,6 +448,10 @@ STATIC void call_hal_irq(int uart_num) {
436
448
if (context != NULL ) {
437
449
HAL_NVIC_ClearPendingIRQ (context -> irq );
438
450
HAL_UART_IRQHandler (& context -> handle );
451
+
452
+ if (HAL_UART_ERROR_NONE != context -> handle .ErrorCode ) {
453
+ // TODO: Implement error handling here
454
+ }
439
455
}
440
456
}
441
457
0 commit comments