AnsweredAssumed Answered

STM32 USART Tx inaccuracy issue

Question asked by james.a on Sep 22, 2013
Latest reply on Sep 26, 2013 by Clive One
I'm seeing an issue when I transmit a byte using the USART (USART 2 in my case).
I need an accurate delay between bytes of 5ms. So when a 5ms timer expires I transmit a byte. However with the code below I'm seeing a small variable time (0-120us) between instructing the USART to transmit and when it actually starts.

        TimerInterrupt(TRUE); 
        TransmitByte(IsoTxBuffer[TxByteCounter++]);           
        TestPinToggle();    // Uncomment this line when performing timing checks with a scope 
        while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
        TestPinToggle();   // Uncomment this line when performing timing checks with a scope

Now a way around it I have found is to disable the USART and immediate re-enable it. This process takes a constant time of around 1ms so I could do this each time and reduce my delay:

    TimerInterrupt(TRUE); 
    UartEnable( FALSE ); // Having these two line in adds a 1ms delay...
    UartEnable( TRUE ); // But stops the time inaccuarcy waiting for the TXE flag
    TransmitByte(IsoTxBuffer[TxByteCounter++]); 
    TestPinToggle(); // Uncomment this line when performing timing checks with a scope 
    while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
    TestPinToggle(); // Uncomment this line when performing timing checks with a scope 
 
However I would like to understand why I'm getting this issue and if there's a better solution

KR
Adam

Outcomes