From c180836714517efb0233a7743afbb866561195d1 Mon Sep 17 00:00:00 2001 From: David <6957239+delta-G@users.noreply.github.com> Date: Mon, 6 May 2024 01:10:43 -0500 Subject: [PATCH 1/5] Use txBuffer --- cores/arduino/Serial.cpp | 58 +++++++++++++++++++++------------------- cores/arduino/Serial.h | 5 ++-- 2 files changed, 33 insertions(+), 30 deletions(-) diff --git a/cores/arduino/Serial.cpp b/cores/arduino/Serial.cpp index d905edd9..7d7a0ad8 100644 --- a/cores/arduino/Serial.cpp +++ b/cores/arduino/Serial.cpp @@ -61,9 +61,13 @@ void UART::WrapperCallback(uart_callback_args_t *p_args) { case UART_EVENT_TX_COMPLETE: case UART_EVENT_TX_DATA_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; + if(uart_ptr->txBuffer.available()){ + static char txc; + txc = uart_ptr->txBuffer.read_char(); + R_SCI_UART_Write(&(uart_ptr->uart_ctrl), (uint8_t*)&txc , 1); + } else { + uart_ptr->tx_done = true; + } break; } case UART_EVENT_RX_CHAR: @@ -108,27 +112,31 @@ bool UART::setUpUartIrqs(uart_cfg_t &cfg) { /* -------------------------------------------------------------------------- */ size_t UART::write(uint8_t c) { /* -------------------------------------------------------------------------- */ - if(init_ok) { - tx_done = false; - R_SCI_UART_Write(&uart_ctrl, &c, 1); - while (!tx_done) {} - return 1; - } - else { - return 0; - } + if(init_ok) { + while(txBuffer.isFull()){;} + txBuffer.store_char(c); + if(tx_done){ + tx_done = false; + txc = txBuffer.read_char(); // clear out the char we just added and send it to start transmission. + R_SCI_UART_Write(&uart_ctrl, (uint8_t*)&txc , 1); + } + return 1; + } + else { + return 0; + } } size_t UART::write(uint8_t* c, size_t len) { - if(init_ok) { - tx_done = false; - R_SCI_UART_Write(&uart_ctrl, c, len); - while (!tx_done) {} - return len; - } - else { - return 0; - } + if(init_ok) { + for(int i = 0; iTDR = *(c+i); - while (uart_ctrl.p_reg->SSR_b.TEND == 0) {} - i++; - } - return len; + return write(c, len); } \ No newline at end of file diff --git a/cores/arduino/Serial.h b/cores/arduino/Serial.h index cc818d46..4242dd75 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_done = true; + char txc; sci_uart_instance_ctrl_t uart_ctrl; uart_cfg_t uart_cfg; @@ -107,4 +108,4 @@ extern UART _UART4_; extern UART _UART5_; #endif -#endif +#endif \ No newline at end of file From b4a4e37c59f2d3ceff941a654992e3135dacd9e8 Mon Sep 17 00:00:00 2001 From: David <6957239+delta-G@users.noreply.github.com> Date: Mon, 6 May 2024 01:41:02 -0500 Subject: [PATCH 2/5] fixed formatting --- cores/arduino/Serial.cpp | 56 ++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/cores/arduino/Serial.cpp b/cores/arduino/Serial.cpp index 7d7a0ad8..eb36f807 100644 --- a/cores/arduino/Serial.cpp +++ b/cores/arduino/Serial.cpp @@ -61,13 +61,13 @@ void UART::WrapperCallback(uart_callback_args_t *p_args) { case UART_EVENT_TX_COMPLETE: case UART_EVENT_TX_DATA_EMPTY: { - if(uart_ptr->txBuffer.available()){ - static char txc; - txc = uart_ptr->txBuffer.read_char(); - R_SCI_UART_Write(&(uart_ptr->uart_ctrl), (uint8_t*)&txc , 1); - } else { - uart_ptr->tx_done = true; - } + if(uart_ptr->txBuffer.available()){ + static char txc; + txc = uart_ptr->txBuffer.read_char(); + R_SCI_UART_Write(&(uart_ptr->uart_ctrl), (uint8_t*)&txc , 1); + } else { + uart_ptr->tx_done = true; + } break; } case UART_EVENT_RX_CHAR: @@ -112,31 +112,31 @@ bool UART::setUpUartIrqs(uart_cfg_t &cfg) { /* -------------------------------------------------------------------------- */ size_t UART::write(uint8_t c) { /* -------------------------------------------------------------------------- */ - if(init_ok) { - while(txBuffer.isFull()){;} - txBuffer.store_char(c); - if(tx_done){ - tx_done = false; - txc = txBuffer.read_char(); // clear out the char we just added and send it to start transmission. - R_SCI_UART_Write(&uart_ctrl, (uint8_t*)&txc , 1); - } - return 1; - } - else { - return 0; - } + if(init_ok) { + while(txBuffer.isFull()){;} + txBuffer.store_char(c); + if(tx_done){ + tx_done = false; + txc = txBuffer.read_char(); // clear out the char we just added and send it to start transmission. + R_SCI_UART_Write(&uart_ctrl, (uint8_t*)&txc , 1); + } + return 1; + } + else { + return 0; + } } size_t UART::write(uint8_t* c, size_t len) { - if(init_ok) { - for(int i = 0; i Date: Mon, 6 May 2024 16:12:53 -0500 Subject: [PATCH 3/5] added availableForWrite --- cores/arduino/Serial.cpp | 6 ++++++ cores/arduino/Serial.h | 1 + 2 files changed, 7 insertions(+) diff --git a/cores/arduino/Serial.cpp b/cores/arduino/Serial.cpp index eb36f807..270a9733 100644 --- a/cores/arduino/Serial.cpp +++ b/cores/arduino/Serial.cpp @@ -315,6 +315,12 @@ int UART::available() { return rxBuffer.available(); } +/* -------------------------------------------------------------------------- */ +int UART::availableForWrite() { +/* -------------------------------------------------------------------------- */ + return txBuffer.availableForStore(); +} + /* -------------------------------------------------------------------------- */ int UART::peek() { /* -------------------------------------------------------------------------- */ diff --git a/cores/arduino/Serial.h b/cores/arduino/Serial.h index 4242dd75..038218d9 100644 --- a/cores/arduino/Serial.h +++ b/cores/arduino/Serial.h @@ -65,6 +65,7 @@ class UART : public arduino::HardwareSerial { size_t write(uint8_t* c, size_t len); size_t write_raw(uint8_t* c, size_t len); using Print::write; + int availableForWrite(); operator bool(); // { return true; } private: From 2878c5a8257d3ba002c6545625e11b442cd51de0 Mon Sep 17 00:00:00 2001 From: david <6957239+delta-G@users.noreply.github.com> Date: Sat, 29 Jun 2024 18:29:01 -0500 Subject: [PATCH 4/5] Use member variable in ISR instead of static veriable --- cores/arduino/Serial.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/cores/arduino/Serial.cpp b/cores/arduino/Serial.cpp index 270a9733..260bb63a 100644 --- a/cores/arduino/Serial.cpp +++ b/cores/arduino/Serial.cpp @@ -62,9 +62,8 @@ void UART::WrapperCallback(uart_callback_args_t *p_args) { case UART_EVENT_TX_DATA_EMPTY: { if(uart_ptr->txBuffer.available()){ - static char txc; - txc = uart_ptr->txBuffer.read_char(); - R_SCI_UART_Write(&(uart_ptr->uart_ctrl), (uint8_t*)&txc , 1); + uart_ptr->txc = uart_ptr->txBuffer.read_char(); + R_SCI_UART_Write(&(uart_ptr->uart_ctrl), (uint8_t*)&(uart_ptr->txc) , 1); } else { uart_ptr->tx_done = true; } From 0a14d85dc2d14febd8d31523d6eb08289a5a6fd0 Mon Sep 17 00:00:00 2001 From: david <6957239+delta-G@users.noreply.github.com> Date: Wed, 3 Jul 2024 00:07:18 -0500 Subject: [PATCH 5/5] Loads characters 16 at a time in the interrupt callback --- cores/arduino/Serial.cpp | 32 ++++++++++++++++++++------------ cores/arduino/Serial.h | 2 +- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/cores/arduino/Serial.cpp b/cores/arduino/Serial.cpp index 260bb63a..245260cf 100644 --- a/cores/arduino/Serial.cpp +++ b/cores/arduino/Serial.cpp @@ -58,15 +58,22 @@ 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: // TEI interrupt { if(uart_ptr->txBuffer.available()){ - uart_ptr->txc = uart_ptr->txBuffer.read_char(); - R_SCI_UART_Write(&(uart_ptr->uart_ctrl), (uint8_t*)&(uart_ptr->txc) , 1); - } else { + int idx = 0; + while (uart_ptr->txBuffer.available() && (idx < 16)){ + uart_ptr->txc[idx++] = uart_ptr->txBuffer.read_char(); + } + R_SCI_UART_Write(&(uart_ptr->uart_ctrl), (uint8_t*)(uart_ptr->txc) , idx); + } + else { uart_ptr->tx_done = true; - } + } + break; + } + case UART_EVENT_TX_DATA_EMPTY: // TXI interrupt had no more data to write + { break; } case UART_EVENT_RX_CHAR: @@ -113,12 +120,13 @@ size_t UART::write(uint8_t c) { /* -------------------------------------------------------------------------- */ if(init_ok) { while(txBuffer.isFull()){;} - txBuffer.store_char(c); - if(tx_done){ - tx_done = false; - txc = txBuffer.read_char(); // clear out the char we just added and send it to start transmission. - R_SCI_UART_Write(&uart_ctrl, (uint8_t*)&txc , 1); - } + if (tx_done) { + tx_done = false; + txc[0] = c; + R_SCI_UART_Write(&uart_ctrl, (uint8_t*)txc , 1); + } else { + txBuffer.store_char(c); + } return 1; } else { diff --git a/cores/arduino/Serial.h b/cores/arduino/Serial.h index 038218d9..48e4b80f 100644 --- a/cores/arduino/Serial.h +++ b/cores/arduino/Serial.h @@ -80,7 +80,7 @@ class UART : public arduino::HardwareSerial { arduino::SafeRingBufferN txBuffer; volatile bool tx_done = true; - char txc; + volatile char txc[BSP_FEATURE_SCI_UART_FIFO_DEPTH]; sci_uart_instance_ctrl_t uart_ctrl; uart_cfg_t uart_cfg;