2013-09-22 07:39 AM
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 Adam2013-09-22 08:15 AM
Perhaps you should be front testing for TXE, to know the buffer is empty, or be using the TC bit to appreciate when the last bit hit the wire?
2013-09-22 10:33 AM
2013-09-22 10:50 AM
Hi Clive1,
Thanks for your response - I have front tested for TXE, but it becomes 'un-empty' again for this random 0-120us period as soon as I call the transmit and then after this delay the transmit begins.2013-09-22 03:30 PM
Does the 120us relate at all to the baud rate? What is the baud rate? 9600?
2013-09-25 06:08 AM
Baud rate is 10k4. I don't see a connection.
2013-09-25 06:22 AM
The connection is, that the bit-duration counter might be free-running, not being reset from writing to the data register.
Try disabling/enabling the transmitter between bytes (USART_CR1.TE); and if it does not help (which might be the case, given note 2 in this bit's description), try rewriting the baudrate divider to 1 betwen bytes. JW2013-09-26 07:27 AM
Hi Jan,
Disabling an re-enabling does work and produces a fixed reliable delay of around 1ms (see my original post). I don't think this is how the chip designers originally intended it to work so really I'm just after confirmation that this is a necessary and acceptable work around for a little mistake in the chips design. Adam2013-09-26 08:50 AM
Not sure anyone here has a gate-level understanding of the design. I'm also not sure that the bit placement with a granularity of the baud clock dividers is a flaw or oversight, but rather just a simple/elegant implementation. If plotted against the baud clock, all output bits would likely appear synchronous, and the placement jitter you observe relates to the phase of the baud divider wrt the writing of DR.
Enabling may cause a synchronous reset of the divider, as might writing the BRR, or manipulating it, perhaps with 0 or 1, or just rewriting the current setting. You'll need to find someone inside ST to talk to about the gate-level design, try your ST or FAE contacts.