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

add availableForWrite() for uart #802

Merged
merged 1 commit into from
Nov 30, 2023
Merged
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
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