Skip to content

Commit b24c9e5

Browse files
sandeepmistrycmaglie
authored andcommitted
TX buffering for UART using RingBuffer
1 parent 57c432e commit b24c9e5

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

cores/arduino/Uart.cpp

+22-2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ void Uart::end()
5050
{
5151
sercom->resetUART();
5252
rxBuffer.clear();
53+
txBuffer.clear();
5354
}
5455

5556
void Uart::flush()
@@ -59,6 +60,16 @@ void Uart::flush()
5960

6061
void Uart::IrqHandler()
6162
{
63+
if (sercom->isDataRegisterEmptyUART()) {
64+
if (txBuffer.available()) {
65+
uint8_t data = txBuffer.read_char();
66+
67+
sercom->writeDataUART(data);
68+
} else {
69+
sercom->disableDataRegisterEmptyInterruptUART();
70+
}
71+
}
72+
6273
if (sercom->availableDataUART()) {
6374
rxBuffer.store_char(sercom->readDataUART());
6475
}
@@ -79,7 +90,7 @@ int Uart::available()
7990

8091
int Uart::availableForWrite()
8192
{
82-
return (sercom->isDataRegisterEmptyUART() ? 1 : 0);
93+
return txBuffer.availableForStore();
8394
}
8495

8596
int Uart::peek()
@@ -94,7 +105,16 @@ int Uart::read()
94105

95106
size_t Uart::write(const uint8_t data)
96107
{
97-
sercom->writeDataUART(data);
108+
if (sercom->isDataRegisterEmptyUART()) {
109+
sercom->writeDataUART(data);
110+
} else {
111+
while(txBuffer.isFull()); // spin lock until a spot opens up in the buffer
112+
113+
txBuffer.store_char(data);
114+
115+
sercom->enableDataRegisterEmptyInterruptUART();
116+
}
117+
98118
return 1;
99119
}
100120

cores/arduino/Uart.h

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class Uart : public HardwareSerial
4646
private:
4747
SERCOM *sercom;
4848
RingBuffer rxBuffer;
49+
RingBuffer txBuffer;
4950

5051
uint8_t uc_pinRX;
5152
uint8_t uc_pinTX;

0 commit comments

Comments
 (0)