Buffered Serial Port Driver for RTOS

Dependents:   nucleo_cannonball PiballNeoController

Embed: (wiki syntax)

« Back to documentation index

SerialDriver Class Reference

SerialDriver Class Reference

RTOS compatible buffered Serial port driver. More...

#include <SerialDriver.h>

Public Member Functions

 SerialDriver (PinName txPin, PinName rxPin, int txBufferLength_=256, int rxBufferLength_=256, unsigned char *txBuffer_=NULL, unsigned char *rxBuffer_=NULL)
 Prepares ring buffer and irq.
int putc (int c, unsigned int timeoutMs=osWaitForever)
 Put a byte to the TX buffer.
int getc (unsigned int timeoutMs=osWaitForever)
 Get a byte from the RX buffer.
int write (const unsigned char *buffer, const unsigned int length, bool block=true)
 write a buck of bytes
int read (unsigned char *buffer, const unsigned int length, bool block=true)
 read a buck of bytes
int puts (const char *str, bool block=true)
 Write a string (without terminating null)
int printf (const char *format,...)
 Print a formatted string.
bool isTxBufferFull ()
 Checks if TX buffer is full.
bool isRxBufferFull ()
 Checks if RX buffer is full.
bool isTxBufferEmtpy ()
 Checks if TX buffer is empty.
bool isRxBufferEmpty ()
 Checks if RX buffer is empty.
bool writeable ()
 Checks if TX buffer is writeable (= not full).
bool readable ()
 Checks if RX buffer is readable (= not empty).
int getNumTxDrops ()
 Returns number of dropped bytes that did not fit into TX buffer.
int getNumRxDrops ()
 Returns number of dropped bytes that did not fit into RX buffer.

Detailed Description

RTOS compatible buffered Serial port driver.

  • Based on RawSerial.
  • Can use external buffers.
  • ISR driven, ring buffered IO operation
  • Can Replace mbed RawSerial
  • IO operations are idle waiting, don't waste time in RTOS :D
  • Do not use attach methods for TxIrq or RxIrq! They are already in use.

Definition at line 32 of file SerialDriver.h.


Constructor & Destructor Documentation

SerialDriver ( PinName  txPin,
PinName  rxPin,
int  txBufferLength_ = 256,
int  rxBufferLength_ = 256,
unsigned char *  txBuffer_ = NULL,
unsigned char *  rxBuffer_ = NULL 
)

Prepares ring buffer and irq.

If no buffer was set, the buffer gets allocated.

Parameters:
txPinTX PinName, e.g. USBTX
rxPinRX PinName, e.g. USBRX
txBufferLength_size of TX buffer. Must be > 1!
rxBufferLength_size of RX buffer. Must be > 1!
txBuffer_TX buffer, if NULL, the buffer will be allocated
rxBuffer_RX buffer, if NULL, the buffer will be allocated

Definition at line 3 of file SerialDriver.cpp.


Member Function Documentation

int getc ( unsigned int  timeoutMs = osWaitForever )

Get a byte from the RX buffer.

If the RX buffer is empty, it waits the defined timeout.

Parameters:
timeoutMsgive RX buffer time to get readable.
Returns:
next byte from RX buffer or -1 after timeout.

Definition at line 113 of file SerialDriver.cpp.

int getNumRxDrops (  )

Returns number of dropped bytes that did not fit into RX buffer.

Returns:
number of dropped rx bytes

Definition at line 183 of file SerialDriver.h.

int getNumTxDrops (  )

Returns number of dropped bytes that did not fit into TX buffer.

Returns:
number of dropped tx bytes

Definition at line 179 of file SerialDriver.h.

bool isRxBufferEmpty (  )

Checks if RX buffer is empty.

Returns:
true - RX buffer is empty, false - else

Definition at line 161 of file SerialDriver.h.

bool isRxBufferFull (  )

Checks if RX buffer is full.

Returns:
true - RX buffer is full, false - else

Definition at line 153 of file SerialDriver.h.

bool isTxBufferEmtpy (  )

Checks if TX buffer is empty.

Returns:
true - TX buffer is empty, false - else

Definition at line 157 of file SerialDriver.h.

bool isTxBufferFull (  )

Checks if TX buffer is full.

Returns:
true - TX buffer is full, false - else

Definition at line 149 of file SerialDriver.h.

int printf ( const char *  format,
  ... 
)

Print a formatted string.

Idle blocking! Dynamically allocates needed buffer.

Parameters:
formatnull terminated format string
Returns:
written chars (without terminating null)

Definition at line 211 of file SerialDriver.cpp.

int putc ( int  c,
unsigned int  timeoutMs = osWaitForever 
)

Put a byte to the TX buffer.

If the TX buffer is full, it waits the defined timeout. Drops the byte, if TX buffer is still full after timeout.

Parameters:
cThe byte to write
timeoutMsgive TX buffer time to get writeable.
Returns:
1 - success, 0 - if TX Buffer was full all the time

Definition at line 50 of file SerialDriver.cpp.

int puts ( const char *  str,
bool  block = true 
)

Write a string (without terminating null)

For compatibility with mbed RawSerial

Parameters:
strnull terminated string
blockidle wait for every putc to complete
Returns:
written chars (without terminating null)

Definition at line 204 of file SerialDriver.cpp.

int read ( unsigned char *  buffer,
const unsigned int  length,
bool  block = true 
)

read a buck of bytes

No timeout! To block, or not to block. That is the question.

Parameters:
bufferbuck of bytes
lengthread how much bytes?
blockidle wait for every getc to complete
Returns:
read bytes. For non block read it could be < length!

Definition at line 188 of file SerialDriver.cpp.

bool readable (  )

Checks if RX buffer is readable (= not empty).

For compatibility with mbed Serial.

Returns:
true - RX buffer is readable, false - else

Definition at line 174 of file SerialDriver.h.

int write ( const unsigned char *  buffer,
const unsigned int  length,
bool  block = true 
)

write a buck of bytes

No timeout! To block, or not to block. That is the question.

Parameters:
bufferbuck of bytes
lengthwrite how much bytes?
blockidle wait for every putc to complete
Returns:
written bytes. For non block write it could be < length!

Definition at line 178 of file SerialDriver.cpp.

bool writeable (  )

Checks if TX buffer is writeable (= not full).

For compatibility with mbed Serial.

Returns:
true - TX buffer is writeable, false - else

Definition at line 168 of file SerialDriver.h.