Fixed lost UART data.

Use non-blocking HAL call to allow reception of data over UART while
transmission is active.
This commit is contained in:
EmergReanimator 2022-02-12 18:51:21 +01:00
parent ddac37add7
commit 69aa0b4edb

View File

@ -325,19 +325,23 @@ size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data,
if (self->tx == NULL) { if (self->tx == NULL) {
mp_raise_ValueError(translate("No TX pin")); mp_raise_ValueError(translate("No TX pin"));
} }
bool write_err = false; // write error shouldn't disable interrupts
// Disable UART IRQ to avoid resource hazards in Rx IRQ handler
HAL_NVIC_DisableIRQ(self->irq); HAL_NVIC_DisableIRQ(self->irq);
HAL_StatusTypeDef ret = HAL_UART_Transmit(&self->handle, (uint8_t *)data, len, HAL_MAX_DELAY); HAL_StatusTypeDef ret = HAL_UART_Transmit_IT(&self->handle, (uint8_t *)data, len);
if (ret != HAL_OK) {
write_err = true;
}
HAL_UART_Receive_IT(&self->handle, &self->rx_char, 1);
HAL_NVIC_EnableIRQ(self->irq); HAL_NVIC_EnableIRQ(self->irq);
if (write_err) { if (HAL_OK == ret) {
HAL_UART_StateTypeDef Status = HAL_UART_GetState(&self->handle);
while ((Status & HAL_UART_STATE_BUSY_TX) == HAL_UART_STATE_BUSY_TX) {
RUN_BACKGROUND_TASKS;
Status = HAL_UART_GetState(&self->handle);
}
}
else {
mp_raise_ValueError(translate("UART write error")); mp_raise_ValueError(translate("UART write error"));
} }
return len; return len;
} }
@ -359,6 +363,14 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *handle) {
} }
} }
#if (1)
// TODO: Implement error handling here
#else
while (HAL_BUSY == errflag) {
errflag = HAL_UART_Receive_IT(handle, &context->rx_char, 1);
}
#endif
return; return;
} }
} }
@ -436,6 +448,10 @@ STATIC void call_hal_irq(int uart_num) {
if (context != NULL) { if (context != NULL) {
HAL_NVIC_ClearPendingIRQ(context->irq); HAL_NVIC_ClearPendingIRQ(context->irq);
HAL_UART_IRQHandler(&context->handle); HAL_UART_IRQHandler(&context->handle);
if (HAL_UART_ERROR_NONE != context->handle.ErrorCode) {
// TODO: Implement error handling here
}
} }
} }