Skip to content

Fix for Serial flush() returning before transmission has completed #328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions cores/arduino/Serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,14 @@ void UART::WrapperCallback(uart_callback_args_t *p_args) {
{
break;
}
case UART_EVENT_TX_COMPLETE:
case UART_EVENT_TX_DATA_EMPTY:
case UART_EVENT_TX_COMPLETE: // This is call when the transmission is complete
{
//uint8_t to_enqueue = uart_ptr->txBuffer.available() < uart_ptr->uart_ctrl.fifo_depth ? uart_ptr->txBuffer.available() : uart_ptr->uart_ctrl.fifo_depth;
//while (to_enqueue) {
uart_ptr->tx_done = true;
uart_ptr->tx_complete = true;
break;
}
case UART_EVENT_TX_DATA_EMPTY: // This is called when the buffer is empty
{
uart_ptr->tx_empty = true;
break;
}
case UART_EVENT_RX_CHAR:
Expand Down Expand Up @@ -109,9 +111,10 @@ bool UART::setUpUartIrqs(uart_cfg_t &cfg) {
size_t UART::write(uint8_t c) {
/* -------------------------------------------------------------------------- */
if(init_ok) {
tx_done = false;
tx_empty = false;
tx_complete = false;
R_SCI_UART_Write(&uart_ctrl, &c, 1);
while (!tx_done) {}
while (!tx_empty) {}
return 1;
}
else {
Expand All @@ -121,9 +124,10 @@ size_t UART::write(uint8_t c) {

size_t UART::write(uint8_t* c, size_t len) {
if(init_ok) {
tx_done = false;
tx_empty = false;
tx_complete = false;
R_SCI_UART_Write(&uart_ctrl, c, len);
while (!tx_done) {}
while (!tx_empty) {}
return len;
}
else {
Expand Down Expand Up @@ -322,7 +326,8 @@ int UART::read() {
/* -------------------------------------------------------------------------- */
void UART::flush() {
/* -------------------------------------------------------------------------- */
while(txBuffer.available());
unsigned long time = millis();
while(!tx_complete && (millis() - time < _timeout)) {}
}

/* -------------------------------------------------------------------------- */
Expand All @@ -335,4 +340,4 @@ size_t UART::write_raw(uint8_t* c, size_t len) {
i++;
}
return len;
}
}
3 changes: 2 additions & 1 deletion cores/arduino/Serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ class UART : public arduino::HardwareSerial {
arduino::SafeRingBufferN<SERIAL_BUFFER_SIZE> rxBuffer;
arduino::SafeRingBufferN<SERIAL_BUFFER_SIZE> txBuffer;

volatile bool tx_done;
volatile bool tx_empty;
volatile bool tx_complete;

sci_uart_instance_ctrl_t uart_ctrl;
uart_cfg_t uart_cfg;
Expand Down
Loading