Skip to content

Commit

Permalink
Merge pull request #802 from adafruit/add-availableforwrite-hwserial
Browse files Browse the repository at this point in the history
add availableForWrite() for uart
  • Loading branch information
hathach authored Nov 30, 2023
2 parents e1f1884 + 124758a commit 6f5cee2
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
1 change: 1 addition & 0 deletions cores/nRF5/HardwareSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class HardwareSerial : public Stream
virtual void flush(void) = 0;
virtual size_t write(uint8_t) = 0;
virtual size_t write(const uint8_t *buffer, size_t size) = 0;
virtual int availableForWrite(void);
using Print::write; // pull in write(str) from Print
virtual operator bool() = 0;
};
Expand Down
8 changes: 8 additions & 0 deletions cores/nRF5/RingBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ int RingBuffer::available()
return delta;
}

int RingBuffer::availableForStore() {
if (_iHead >= _iTail) {
return SERIAL_BUFFER_SIZE - 1 - _iHead + _iTail;
} else {
return _iTail - _iHead - 1;
}
}

int RingBuffer::peek()
{
if(_iTail == _iHead)
Expand Down
13 changes: 7 additions & 6 deletions cores/nRF5/RingBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ class RingBuffer
public:
RingBuffer( void ) ;
void store_char( uint8_t c ) ;
void clear();
int read_char();
int available();
int peek();
bool isFull();
void clear();
int read_char();
int available();
int availableForStore();
int peek();
bool isFull();

private:
int nextIndex(int index);
int nextIndex(int index);
} ;

#endif /* _RING_BUFFER_ */
6 changes: 6 additions & 0 deletions cores/nRF5/Uart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,12 @@ size_t Uart::write(const uint8_t *buffer, size_t size)
return sent;
}

int Uart::availableForWrite(void) {
// UART does not use ring buffer for TX, therefore it is either busy or not
UBaseType_t available = uxSemaphoreGetCount(_end_tx_sem);
return available ? SERIAL_BUFFER_SIZE : 0;
}

//------------- Serial1 (or Serial in case of nRF52832) -------------//
#ifdef NRF52832_XXAA
Uart Serial( NRF_UARTE0, UARTE0_UART0_IRQn, PIN_SERIAL_RX, PIN_SERIAL_TX );
Expand Down
1 change: 1 addition & 0 deletions cores/nRF5/Uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Uart : public HardwareSerial
void begin(unsigned long baudrate, uint16_t config);
void end();
int available();
int availableForWrite(void);
int peek();
int read();
void flush();
Expand Down

0 comments on commit 6f5cee2

Please sign in to comment.