diff --git a/cores/arduino/Serial.cpp b/cores/arduino/Serial.cpp index d905edd9..245260cf 100644 --- a/cores/arduino/Serial.cpp +++ b/cores/arduino/Serial.cpp @@ -58,12 +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 { - //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()){ + 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: @@ -109,25 +119,30 @@ 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; + while(txBuffer.isFull()){;} + 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 { - return 0; + 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; + 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..48e4b80f 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: @@ -78,7 +79,8 @@ class UART : public arduino::HardwareSerial { arduino::SafeRingBufferN rxBuffer; arduino::SafeRingBufferN txBuffer; - volatile bool tx_done; + volatile bool tx_done = true; + volatile char txc[BSP_FEATURE_SCI_UART_FIFO_DEPTH]; sci_uart_instance_ctrl_t uart_ctrl; uart_cfg_t uart_cfg; @@ -107,4 +109,4 @@ extern UART _UART4_; extern UART _UART5_; #endif -#endif +#endif \ No newline at end of file