Skip to content
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

Modify UART Class to Make Use of the txBuffer #304

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
59 changes: 37 additions & 22 deletions cores/arduino/Serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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; i<len; i++){
write(c[i]);
}
return len;
}
else {
return 0;
return 0;
}
}

Expand Down Expand Up @@ -307,6 +322,12 @@ int UART::available() {
return rxBuffer.available();
}

/* -------------------------------------------------------------------------- */
int UART::availableForWrite() {
/* -------------------------------------------------------------------------- */
return txBuffer.availableForStore();
}

/* -------------------------------------------------------------------------- */
int UART::peek() {
/* -------------------------------------------------------------------------- */
Expand All @@ -328,11 +349,5 @@ void UART::flush() {
/* -------------------------------------------------------------------------- */
size_t UART::write_raw(uint8_t* c, size_t len) {
/* -------------------------------------------------------------------------- */
size_t i = 0;
while (i < len) {
uart_ctrl.p_reg->TDR = *(c+i);
while (uart_ctrl.p_reg->SSR_b.TEND == 0) {}
i++;
}
return len;
return write(c, len);
}
6 changes: 4 additions & 2 deletions cores/arduino/Serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -78,7 +79,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_done = true;
volatile char txc[BSP_FEATURE_SCI_UART_FIFO_DEPTH];

sci_uart_instance_ctrl_t uart_ctrl;
uart_cfg_t uart_cfg;
Expand Down Expand Up @@ -107,4 +109,4 @@ extern UART _UART4_;
extern UART _UART5_;

#endif
#endif
#endif
Loading