Skip to content

Commit 59837fe

Browse files
authored
Merge pull request #6027 from EmergReanimator/stm-uart-fix
Stm uart fix
2 parents fd4164f + 4e0bd4a commit 59837fe

File tree

1 file changed

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

1 file changed

+22
-7
lines changed

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -325,19 +325,22 @@ 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+
} else {
339341
mp_raise_ValueError(translate("UART write error"));
340342
}
343+
341344
return len;
342345
}
343346

@@ -359,6 +362,14 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *handle) {
359362
}
360363
}
361364

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+
362373
return;
363374
}
364375
}
@@ -436,6 +447,10 @@ STATIC void call_hal_irq(int uart_num) {
436447
if (context != NULL) {
437448
HAL_NVIC_ClearPendingIRQ(context->irq);
438449
HAL_UART_IRQHandler(&context->handle);
450+
451+
if (HAL_UART_ERROR_NONE != context->handle.ErrorCode) {
452+
// TODO: Implement error handling here
453+
}
439454
}
440455
}
441456

0 commit comments

Comments
 (0)