UC GEE
UC GEE
DIIDevHeads IoT Integration Server
Created by Sterling on 7/11/2024 in #code-review
How can I resolve UART transmission errors on my STM32F407VGT6 MCU during rapid string transmission?
The issue seems to be that you're trying to start a new transmission before the previous one has completed, which is why adding a delay works. The key is to ensure that the transmission is complete before starting a new one. Your approach to wait for the transmission to complete is correct, but it might not be working as intended because the flagTxCmpltUsart flag is reset immediately after starting the transmission. Try modifying your code to only reset the flag within the callback function. This ensures the flag correctly reflects the completion status of the transmission. Here's the modified code:
uint8_t TxData[] = "test123\n";
bool flagTxCmpltUsart = true;

for(int i = 0; i < 10; i++) {
if(HAL_UART_Transmit_IT(&huart3, TxData, strlen(TxData)) != HAL_OK) {
Error_Handler();
}
Wait_Unit_Uart_Tx_Is_Complete();
}

void Wait_Unit_Uart_Tx_Is_Complete(void) {
while(!flagTxCmpltUsart) {}
flagTxCmpltUsart = false;
}

void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) {
if(huart->Instance == USART3) {
flagTxCmpltUsart = true;
}
}
uint8_t TxData[] = "test123\n";
bool flagTxCmpltUsart = true;

for(int i = 0; i < 10; i++) {
if(HAL_UART_Transmit_IT(&huart3, TxData, strlen(TxData)) != HAL_OK) {
Error_Handler();
}
Wait_Unit_Uart_Tx_Is_Complete();
}

void Wait_Unit_Uart_Tx_Is_Complete(void) {
while(!flagTxCmpltUsart) {}
flagTxCmpltUsart = false;
}

void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) {
if(huart->Instance == USART3) {
flagTxCmpltUsart = true;
}
}
@Sterling
7 replies