Skip to content

Commit 69aa0b4

Browse files
Fixed lost UART data.
Use non-blocking HAL call to allow reception of data over UART while transmission is active.
1 parent ddac37a commit 69aa0b4

File tree

1 file changed

+23
-7
lines changed
  • ports/stm/common-hal/busio

1 file changed

+23
-7
lines changed

ports/stm/common-hal/busio/UART.c

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -325,19 +325,23 @@ size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data,
325325
if (self->tx == NULL) {
326326
mp_raise_ValueError(translate("No TX pin"));
327327
}
328-
bool write_err = false; // write error shouldn't disable interrupts
329328

329+
// Disable UART IRQ to avoid resource hazards in Rx IRQ handler
330330
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);
336332
HAL_NVIC_EnableIRQ(self->irq);
337333

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 {
339342
mp_raise_ValueError(translate("UART write error"));
340343
}
344+
341345
return len;
342346
}
343347

@@ -359,6 +363,14 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *handle) {
359363
}
360364
}
361365

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+
362374
return;
363375
}
364376
}
@@ -436,6 +448,10 @@ STATIC void call_hal_irq(int uart_num) {
436448
if (context != NULL) {
437449
HAL_NVIC_ClearPendingIRQ(context->irq);
438450
HAL_UART_IRQHandler(&context->handle);
451+
452+
if (HAL_UART_ERROR_NONE != context->handle.ErrorCode) {
453+
// TODO: Implement error handling here
454+
}
439455
}
440456
}
441457

0 commit comments

Comments
 (0)