Skip to content

Stm uart fix #6027

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions ports/stm/common-hal/busio/UART.c
Original file line number Diff line number Diff line change
Expand Up @@ -325,19 +325,22 @@ size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data,
if (self->tx == NULL) {
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_StatusTypeDef ret = HAL_UART_Transmit(&self->handle, (uint8_t *)data, len, HAL_MAX_DELAY);
if (ret != HAL_OK) {
write_err = true;
}
HAL_UART_Receive_IT(&self->handle, &self->rx_char, 1);
HAL_StatusTypeDef ret = HAL_UART_Transmit_IT(&self->handle, (uint8_t *)data, len);
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"));
}

return len;
}

Expand All @@ -359,6 +362,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;
}
}
Expand Down Expand Up @@ -436,6 +447,10 @@ STATIC void call_hal_irq(int uart_num) {
if (context != NULL) {
HAL_NVIC_ClearPendingIRQ(context->irq);
HAL_UART_IRQHandler(&context->handle);

if (HAL_UART_ERROR_NONE != context->handle.ErrorCode) {
// TODO: Implement error handling here
}
}
}

Expand Down