Hmm, not sure about this one, maybe someone who is more familiar with this can chime in. In the meantime I’m going to log a bug for our firmware guys to check it out.
It looks like the flush() command only waits for the current character to transmit, not for the entire tx ring buffer to flush.
void USARTSerial::flush()
{
// Loop until USART DR register is empty
while (transmitting && (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET));
transmitting = false;
}
This should probably wait for the tx ring buffer to be empty.
As a work-around @Mike43110 you could Serial1.write() and Serial1.flush each character that need to be sure are transmitted since it would work for one byte.
bko, as you said, flush() only looks a the status of the last char transmit status using the USART_FLAG_TXE flag. If I understand the specs correctly, using the USART_FLAG_TC flag indicates when there are not more bytes pending to be sent AND transmission is complete. Would changing the while to testing the USART_FLAG_TC flag work in your opinion?
I think it needs to wait for the tx buffer to empty and then wait for the last char to be sent, something like:
void USARTSerial::flush()
{
while ( _tx_buffer->head != _tx_buffer->tail );
// Loop until USART DR register is empty
while (transmitting && (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET));
transmitting = false;
}
since that would mean that the full tx ring buffer has been flushed and all bytes have been sent. I am not sure I know enough about the interrupt driven part to say for sure this is the exact right way, but something like this is needed.