Why does UART transmit interrupt fail to work in this case?
<p>I am using stm32f0 MCU.</p>

<p>I have a simple UART echo code in which every byte received will be transmitted out. I tested that it works. Here it is;</p>

<pre><code>uint8_t Rx_data[5]; 
uint32_t tx_timeout = 0;
//Interrupt callback routine
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    if (huart-&gt;Instance == USART1)  //current UART
    {
        HAL_UART_Transmit(&amp;huart1, &amp;Rx_data[0], 1, tx_timeout);        
        HAL_UART_Receive_IT(&amp;huart1, Rx_data, 1);   //activate UART receive interrupt every time on receiving 1 byte
    }
}
</code></pre>

<p>I do not feel comfortable with the code even though it works. Firstly, <code>tx_timeout</code> is 0 and most code examples are non-zero. I do not know the side effect. Secondly, <code>HAL_UART_Transmit()</code> is a blocking call and it is not advisable to use blocking calls inside an interrupt. So, I decided to use an interrupt for uart transmission <code>HAL_UART_Transmit_IT()</code>instead of a blocking call. Here is the modified code;</p>

<pre><code>uint8_t Rx_data[5]; 
uint32_t tx_timeout = 0;
//Interrupt callback routine
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    if (huart-&gt;Instance == USART1)  //current UART
    {
        HAL_UART_Transmit_IT(&amp;huart1, &amp;Rx_data[0], 1);        
        HAL_UART_Receive_IT(&amp;huart1, Rx_data, 1);   //activate UART receive interrupt every time on receiving 1 byte
    }
}
</code></pre>

<p>However, it does not work as expected. My PC transmits ASCII 12345678 to stm32. If things work as expected, the PC should be receiving 12345678 back. However, the PC receives 1357 instead. What is wrong with this code that uses <code>HAL_UART_Transmit_IT()</code>?</p>