Search code examples
cstm32interruptuartstm32f4discovery

STM32 Uart Interrupt Burst transmit problem


I'm trying to implement uart in interrupt mode, but something go wrong obviously. Here is my problem: I want to send some strings as soon as possible (example: want to send 10 times string "test123") but for some reason that isn't possible (I make some mistake but can't understand where is that mistake). I use STM32CubeIDE, mcu is stm32f407vgt6. After first successful transmit code fall into Error_Handler() which is not acceptable. When I use delays between each transmit all string will be successful transmitted but why that can be done in this way.Here is code

uint8_t TxData[] = "test123\n";
bool flagTxCmpltUsart = true;

for(i = 0; i < 10; i++){`

    if(HAL_UART_Transmit_IT(&huart3, TxData, strlen(TxData)) != HAL_OK)
    {
        Error_Handler();
    } 
        Wait_Unit_Uart_Tx_Is_Complete();
        Reset_Uart_Tx_Complete_Flag();}
void Reset_Uart_Tx_Complete_Flag(void) 
{
    flagTxCmpltUsart = false;
}

void Wait_Unit_Uart_Tx_Is_Complete(void)
{
    while(!flagTxCmpltUsart){}
}

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

Solution

  • Since void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) is called in the interrupt context, you should set your complete flag as

    volatile bool flagTxCmpltUsart = true;

    To make sure the compiler knows that is could change outside of the normal program flow.