Skip to content

Commit 03b4a91

Browse files
authored
Merge pull request #405 from pennam/fix-flush
Fix for Serial flush() returning before transmission has completed
2 parents 04d998d + 51e2905 commit 03b4a91

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

cores/arduino/Serial.cpp

+17-11
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,14 @@ void UART::WrapperCallback(uart_callback_args_t *p_args) {
5858
{
5959
break;
6060
}
61-
case UART_EVENT_TX_COMPLETE:
62-
case UART_EVENT_TX_DATA_EMPTY:
61+
case UART_EVENT_TX_COMPLETE: // This is call when the transmission is complete
6362
{
64-
//uint8_t to_enqueue = uart_ptr->txBuffer.available() < uart_ptr->uart_ctrl.fifo_depth ? uart_ptr->txBuffer.available() : uart_ptr->uart_ctrl.fifo_depth;
65-
//while (to_enqueue) {
66-
uart_ptr->tx_done = true;
63+
uart_ptr->tx_complete = true;
64+
break;
65+
}
66+
case UART_EVENT_TX_DATA_EMPTY: // This is called when the buffer is empty
67+
{ // Last byte is transmitting, but ready for more data
68+
uart_ptr->tx_empty = true;
6769
break;
6870
}
6971
case UART_EVENT_RX_CHAR:
@@ -87,6 +89,8 @@ UART::UART(int _pin_tx, int _pin_rx, int _pin_rts, int _pin_cts):
8789
rx_pin(_pin_rx),
8890
rts_pin(_pin_rts),
8991
cts_pin(_pin_cts),
92+
tx_empty(true),
93+
tx_complete(true),
9094
init_ok(false) {
9195
/* -------------------------------------------------------------------------- */
9296
uart_cfg.txi_irq = FSP_INVALID_VECTOR;
@@ -109,9 +113,10 @@ bool UART::setUpUartIrqs(uart_cfg_t &cfg) {
109113
size_t UART::write(uint8_t c) {
110114
/* -------------------------------------------------------------------------- */
111115
if(init_ok) {
112-
tx_done = false;
116+
tx_empty = false;
117+
tx_complete = false;
113118
R_SCI_UART_Write(&uart_ctrl, &c, 1);
114-
while (!tx_done) {}
119+
while (!tx_empty) {}
115120
return 1;
116121
}
117122
else {
@@ -121,9 +126,10 @@ size_t UART::write(uint8_t c) {
121126

122127
size_t UART::write(uint8_t* c, size_t len) {
123128
if(init_ok) {
124-
tx_done = false;
129+
tx_empty = false;
130+
tx_complete = false;
125131
R_SCI_UART_Write(&uart_ctrl, c, len);
126-
while (!tx_done) {}
132+
while (!tx_empty) {}
127133
return len;
128134
}
129135
else {
@@ -322,7 +328,7 @@ int UART::read() {
322328
/* -------------------------------------------------------------------------- */
323329
void UART::flush() {
324330
/* -------------------------------------------------------------------------- */
325-
while(txBuffer.available());
331+
while(!tx_complete);
326332
}
327333

328334
/* -------------------------------------------------------------------------- */
@@ -335,4 +341,4 @@ size_t UART::write_raw(uint8_t* c, size_t len) {
335341
i++;
336342
}
337343
return len;
338-
}
344+
}

cores/arduino/Serial.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ class UART : public arduino::HardwareSerial {
7878
arduino::SafeRingBufferN<SERIAL_BUFFER_SIZE> rxBuffer;
7979
arduino::SafeRingBufferN<SERIAL_BUFFER_SIZE> txBuffer;
8080

81-
volatile bool tx_done;
81+
volatile bool tx_empty;
82+
volatile bool tx_complete;
8283

8384
sci_uart_instance_ctrl_t uart_ctrl;
8485
uart_cfg_t uart_cfg;

0 commit comments

Comments
 (0)