From c77faa4ef16b357fe20d913e7d51560edf4cb2f9 Mon Sep 17 00:00:00 2001 From: Hsubtnarg <101956419+Hsubtnarg@users.noreply.github.com> Date: Wed, 29 May 2024 09:00:58 +1200 Subject: [PATCH 1/4] Update Serial.h Renamed tx_done to tx_empty Created tx_complete --- cores/arduino/Serial.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cores/arduino/Serial.h b/cores/arduino/Serial.h index cc818d466..29a41d2c8 100644 --- a/cores/arduino/Serial.h +++ b/cores/arduino/Serial.h @@ -78,7 +78,8 @@ class UART : public arduino::HardwareSerial { arduino::SafeRingBufferN rxBuffer; arduino::SafeRingBufferN 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; From 96a369f32f9f32eb632f4aad2598a82e54d3cf87 Mon Sep 17 00:00:00 2001 From: Hsubtnarg <101956419+Hsubtnarg@users.noreply.github.com> Date: Wed, 29 May 2024 09:06:42 +1200 Subject: [PATCH 2/4] Update Serial.cpp Separated call back events UART_EVENT_TX_COMPLETE and UART_EVENT_TX_DATA_EMPTY and used UART_EVEN_TX_COMPLETE to determine when flush() should return --- cores/arduino/Serial.cpp | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/cores/arduino/Serial.cpp b/cores/arduino/Serial.cpp index d905edd90..229ea21fe 100644 --- a/cores/arduino/Serial.cpp +++ b/cores/arduino/Serial.cpp @@ -58,12 +58,16 @@ 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 + { + uart_ptr->tx_complete = true; + break; + } + case UART_EVENT_TX_DATA_EMPTY: // This is called when the buffer is empty { //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_empty = true; break; } case UART_EVENT_RX_CHAR: @@ -109,9 +113,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 { @@ -121,9 +126,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 { @@ -322,7 +328,7 @@ int UART::read() { /* -------------------------------------------------------------------------- */ void UART::flush() { /* -------------------------------------------------------------------------- */ - while(txBuffer.available()); + while(!tx_complete); } /* -------------------------------------------------------------------------- */ @@ -335,4 +341,4 @@ size_t UART::write_raw(uint8_t* c, size_t len) { i++; } return len; -} \ No newline at end of file +} From f35ad4ea1925f935ee851e2e13f5238ce40c9e7e Mon Sep 17 00:00:00 2001 From: Hsubtnarg <101956419+Hsubtnarg@users.noreply.github.com> Date: Thu, 30 May 2024 08:28:28 +1200 Subject: [PATCH 3/4] Update Serial.cpp Removed old comment --- cores/arduino/Serial.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/cores/arduino/Serial.cpp b/cores/arduino/Serial.cpp index 229ea21fe..2f65f8ace 100644 --- a/cores/arduino/Serial.cpp +++ b/cores/arduino/Serial.cpp @@ -65,8 +65,6 @@ void UART::WrapperCallback(uart_callback_args_t *p_args) { } case UART_EVENT_TX_DATA_EMPTY: // This is called when the buffer is empty { - //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_empty = true; break; } From d54ce05525e6ff2d8c1f1f3ce136453c716b0673 Mon Sep 17 00:00:00 2001 From: Hsubtnarg <101956419+Hsubtnarg@users.noreply.github.com> Date: Wed, 5 Jun 2024 15:31:18 +1200 Subject: [PATCH 4/4] Update Serial.cpp Added a timeout to flush() because BLE.begin() on the Uno R4 Wifi, would hang without it. --- cores/arduino/Serial.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cores/arduino/Serial.cpp b/cores/arduino/Serial.cpp index 2f65f8ace..d66f2f23e 100644 --- a/cores/arduino/Serial.cpp +++ b/cores/arduino/Serial.cpp @@ -326,7 +326,8 @@ int UART::read() { /* -------------------------------------------------------------------------- */ void UART::flush() { /* -------------------------------------------------------------------------- */ - while(!tx_complete); + unsigned long time = millis(); + while(!tx_complete && (millis() - time < _timeout)) {} } /* -------------------------------------------------------------------------- */