Skip to content

Commit 065459c

Browse files
committed
Implement transmit buffering with interrupts for USART devices
1 parent 588f9f1 commit 065459c

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

hardware/arduino/sam/cores/arduino/USARTClass.cpp

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,17 @@
2323

2424
// Constructors ////////////////////////////////////////////////////////////////
2525

26-
USARTClass::USARTClass( Usart* pUsart, IRQn_Type dwIrq, uint32_t dwId, RingBuffer* pRx_buffer )
26+
USARTClass::USARTClass( Usart* pUsart, IRQn_Type dwIrq, uint32_t dwId, RingBuffer* pRx_buffer, volatile RingBuffer* pTx_buffer )
2727
{
28-
_rx_buffer = pRx_buffer ;
28+
_rx_buffer = pRx_buffer;
29+
_tx_buffer = pTx_buffer;
2930

3031
_pUsart=pUsart ;
3132
_dwIrq=dwIrq ;
3233
_dwId=dwId ;
3334
}
3435

36+
3537
// Public Methods //////////////////////////////////////////////////////////////
3638

3739
void USARTClass::begin( const uint32_t dwBaudRate )
@@ -73,6 +75,8 @@ void USARTClass::end( void )
7375
// clear any received data
7476
_rx_buffer->_iHead = _rx_buffer->_iTail ;
7577

78+
while (_tx_buffer->_iHead != _tx_buffer->_iTail); //wait for transmit data to be sent
79+
7680
// Disable UART interrupt in NVIC
7781
NVIC_DisableIRQ( _dwIrq ) ;
7882

@@ -115,12 +119,21 @@ void USARTClass::flush( void )
115119

116120
size_t USARTClass::write( const uint8_t uc_data )
117121
{
118-
// Check if the transmitter is ready
119-
while ((_pUsart->US_CSR & US_CSR_TXRDY) != US_CSR_TXRDY)
120-
;
122+
if ((_pUsart->US_CSR & US_CSR_TXRDY) != US_CSR_TXRDY) //is the hardware currently busy?
123+
{
124+
//if busy we buffer
125+
unsigned int l = (_tx_buffer->_iHead + 1) % SERIAL_BUFFER_SIZE;
126+
while (_tx_buffer->_iTail == l); //spin locks if we're about to overwrite the buffer. This continues once the data is sent
121127

122-
// Send character
123-
_pUsart->US_THR = uc_data ;
128+
_tx_buffer->_aucBuffer[_tx_buffer->_iHead] = uc_data;
129+
_tx_buffer->_iHead = l;
130+
_pUsart->US_IER = US_IER_TXRDY; //make sure TX interrupt is enabled
131+
}
132+
else
133+
{
134+
// Send character
135+
_pUsart->US_THR = uc_data ;
136+
}
124137
return 1;
125138
}
126139

@@ -132,6 +145,17 @@ void USARTClass::IrqHandler( void )
132145
if ((status & US_CSR_RXRDY) == US_CSR_RXRDY)
133146
_rx_buffer->store_char( _pUsart->US_RHR ) ;
134147

148+
//Do we need to keep sending data?
149+
if ((status & US_CSR_TXRDY) == US_CSR_TXRDY)
150+
{
151+
_pUsart->US_THR = _tx_buffer->_aucBuffer[_tx_buffer->_iTail];
152+
_tx_buffer->_iTail = (unsigned int)(_tx_buffer->_iTail + 1) % SERIAL_BUFFER_SIZE;
153+
if (_tx_buffer->_iTail == _tx_buffer->_iHead) //if this is true we have no more data to transmit
154+
{
155+
_pUsart->US_IDR = US_IDR_TXRDY; //mask off transmit interrupt so we don't get it anymore
156+
}
157+
}
158+
135159
// Acknowledge errors
136160
if ((status & US_CSR_OVRE) == US_CSR_OVRE ||
137161
(status & US_CSR_FRAME) == US_CSR_FRAME)

hardware/arduino/sam/cores/arduino/USARTClass.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,15 @@ class USARTClass : public HardwareSerial
6060
{
6161
protected:
6262
RingBuffer *_rx_buffer ;
63+
volatile RingBuffer *_tx_buffer;
6364

6465
protected:
6566
Usart* _pUsart ;
6667
IRQn_Type _dwIrq ;
6768
uint32_t _dwId ;
6869

6970
public:
70-
USARTClass( Usart* pUsart, IRQn_Type dwIrq, uint32_t dwId, RingBuffer* pRx_buffer ) ;
71+
USARTClass( Usart* pUsart, IRQn_Type dwIrq, uint32_t dwId, RingBuffer* pRx_buffer, volatile RingBuffer* pTx_buffer ) ;
7172

7273
void begin( const uint32_t dwBaudRate ) ;
7374
void begin( const uint32_t dwBaudRate , const uint32_t config ) ;

hardware/arduino/sam/variants/arduino_due_x/variant.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,14 +317,17 @@ void UART_Handler(void)
317317
RingBuffer rx_buffer2;
318318
RingBuffer rx_buffer3;
319319
RingBuffer rx_buffer4;
320+
volatile RingBuffer tx_buffer2;
321+
volatile RingBuffer tx_buffer3;
322+
volatile RingBuffer tx_buffer4;
320323

321-
USARTClass Serial1(USART0, USART0_IRQn, ID_USART0, &rx_buffer2);
324+
USARTClass Serial1(USART0, USART0_IRQn, ID_USART0, &rx_buffer2, &tx_buffer2);
322325
void serialEvent1() __attribute__((weak));
323326
void serialEvent1() { }
324-
USARTClass Serial2(USART1, USART1_IRQn, ID_USART1, &rx_buffer3);
327+
USARTClass Serial2(USART1, USART1_IRQn, ID_USART1, &rx_buffer3, &tx_buffer3);
325328
void serialEvent2() __attribute__((weak));
326329
void serialEvent2() { }
327-
USARTClass Serial3(USART3, USART3_IRQn, ID_USART3, &rx_buffer4);
330+
USARTClass Serial3(USART3, USART3_IRQn, ID_USART3, &rx_buffer4, &tx_buffer4);
328331
void serialEvent3() __attribute__((weak));
329332
void serialEvent3() { }
330333

0 commit comments

Comments
 (0)