mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Embed: (wiki syntax)

« Back to documentation index

UARTSerial Class Reference

Class providing buffered UART communication functionality using separate circular buffer for send and receive channels. More...

#include <UARTSerial.h>

Inherits mbed::SerialBase, mbed::FileHandle, NonCopyable< UARTSerial >, mbed::SerialBase, mbed::FileHandle, and NonCopyable< UARTSerial >.

Public Member Functions

 UARTSerial (PinName tx, PinName rx, int baud=MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE)
 Create a UARTSerial port, connected to the specified transmit and receive pins, with a particular baud rate.
virtual short poll (short events) const
 Equivalent to POSIX poll().
virtual ssize_t write (const void *buffer, size_t length)
 Write the contents of a buffer to a file.
virtual ssize_t read (void *buffer, size_t length)
 Read the contents of a file into a buffer.
virtual int close ()
 Close a file.
virtual int isatty ()
 Check if the file in an interactive terminal device.
virtual off_t seek (off_t offset, int whence)
 Move the file position to a given offset from from a given location.
virtual int sync ()
 Flush any buffers associated with the file.
virtual int set_blocking (bool blocking)
 Set blocking or non-blocking mode The default is blocking.
virtual bool is_blocking () const
 Check current blocking or non-blocking mode for file operations.
virtual void sigio (Callback< void()> func)
 Register a callback on state change of the file.
void set_data_carrier_detect (PinName dcd_pin, bool active_high=false)
 Setup interrupt handler for DCD line.
void set_baud (int baud)
 Set the baud rate.
void set_format (int bits=8, Parity parity=UARTSerial::None, int stop_bits=1)
 Set the transmission format used by the serial port.
void set_flow_control (Flow type, PinName flow1=NC, PinName flow2=NC)
 Set the flow control type on the serial port.
 UARTSerial (PinName tx, PinName rx, int baud=MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE)
 Create a UARTSerial port, connected to the specified transmit and receive pins, with a particular baud rate.
virtual short poll (short events) const
 Equivalent to POSIX poll().
virtual ssize_t write (const void *buffer, size_t length)
 Write the contents of a buffer to a file.
virtual ssize_t read (void *buffer, size_t length)
 Read the contents of a file into a buffer.
virtual int close ()
 Close a file.
virtual int isatty ()
 Check if the file in an interactive terminal device.
virtual off_t seek (off_t offset, int whence)
 Move the file position to a given offset from from a given location.
virtual int sync ()
 Flush any buffers associated with the file.
virtual int set_blocking (bool blocking)
 Set blocking or non-blocking mode The default is blocking.
virtual bool is_blocking () const
 Check current blocking or non-blocking mode for file operations.
virtual void sigio (Callback< void()> func)
 Register a callback on state change of the file.
void set_data_carrier_detect (PinName dcd_pin, bool active_high=false)
 Setup interrupt handler for DCD line.
void set_baud (int baud)
 Set the baud rate.
void set_format (int bits=8, Parity parity=UARTSerial::None, int stop_bits=1)
 Set the transmission format used by the serial port.
void set_flow_control (Flow type, PinName flow1=NC, PinName flow2=NC)
 Set the flow control type on the serial port.
virtual off_t tell ()
 Get the file position of the file.
virtual off_t tell ()
 Get the file position of the file.
virtual void rewind ()
 Rewind the file position to the beginning of the file.
virtual void rewind ()
 Rewind the file position to the beginning of the file.
virtual off_t size ()
 Get the size of the file.
virtual off_t size ()
 Get the size of the file.
 MBED_DEPRECATED_SINCE ("mbed-os-5.4","Replaced by FileHandle::seek") virtual off_t lseek(off_t offset
 Move the file position to a given offset from a given location.

Protected Member Functions

 MBED_DEPRECATED ("Invalid copy construction of a NonCopyable resource.") NonCopyable(const NonCopyable &)
 NonCopyable copy constructor.
 MBED_DEPRECATED ("Invalid copy assignment of a NonCopyable resource.") NonCopyable &operator
 NonCopyable copy assignment operator.

Private Member Functions

void baud (int baudrate)
 Set the baud rate of the serial port.
void baud (int baudrate)
 Set the baud rate of the serial port.
void format (int bits=8, Parity parity=SerialBase::None, int stop_bits=1)
 Set the transmission format used by the serial port.
void format (int bits=8, Parity parity=SerialBase::None, int stop_bits=1)
 Set the transmission format used by the serial port.
int readable ()
 Determine if there is a character available to read.
int readable ()
 Determine if there is a character available to read.
int writeable ()
 Determine if there is space available to write a character.
int writeable ()
 Determine if there is space available to write a character.
void attach (Callback< void()> func, IrqType type=RxIrq)
 Attach a function to call whenever a serial interrupt is generated.
void attach (Callback< void()> func, IrqType type=RxIrq)
 Attach a function to call whenever a serial interrupt is generated.
template<typename T >
 MBED_DEPRECATED_SINCE ("mbed-os-5.1","The attach function does not support cv-qualifiers. Replaced by ""attach(callback(obj, method), type).") void attach(T *obj
 Attach a member function to call whenever a serial interrupt is generated.
template<typename T >
 MBED_DEPRECATED_SINCE ("mbed-os-5.1","The attach function does not support cv-qualifiers. Replaced by ""attach(callback(obj, method), type).") void attach(T *obj
 Attach a member function to call whenever a serial interrupt is generated.
void clear_break ()
 Clear a break condition on the serial line NOTE: Should be run at least one frame after set_break is called.
void clear_break ()
 Clear a break condition on the serial line NOTE: Should be run at least one frame after set_break is called.
void send_break ()
 Generate a break condition on the serial line.
void send_break ()
 Generate a break condition on the serial line.
int write (const uint8_t *buffer, int length, const event_callback_t &callback, int event=SERIAL_EVENT_TX_COMPLETE)
 Begin asynchronous write using 8bit buffer.
int write (const uint16_t *buffer, int length, const event_callback_t &callback, int event=SERIAL_EVENT_TX_COMPLETE)
 Begin asynchronous write using 16bit buffer.
void abort_write ()
 Abort the on-going write transfer.
void abort_write ()
 Abort the on-going write transfer.
int read (uint8_t *buffer, int length, const event_callback_t &callback, int event=SERIAL_EVENT_RX_COMPLETE, unsigned char char_match=SERIAL_RESERVED_CHAR_MATCH)
 Begin asynchronous reading using 8bit buffer.
int read (uint16_t *buffer, int length, const event_callback_t &callback, int event=SERIAL_EVENT_RX_COMPLETE, unsigned char char_match=SERIAL_RESERVED_CHAR_MATCH)
 Begin asynchronous reading using 16bit buffer.
void abort_read ()
 Abort the on-going read transfer.
void abort_read ()
 Abort the on-going read transfer.
int set_dma_usage_tx (DMAUsage usage)
 Configure DMA usage suggestion for non-blocking TX transfers.
int set_dma_usage_tx (DMAUsage usage)
 Configure DMA usage suggestion for non-blocking TX transfers.
int set_dma_usage_rx (DMAUsage usage)
 Configure DMA usage suggestion for non-blocking RX transfers.
int set_dma_usage_rx (DMAUsage usage)
 Configure DMA usage suggestion for non-blocking RX transfers.
 MBED_DEPRECATED ("Invalid copy construction of a NonCopyable resource.") NonCopyable(const NonCopyable &)
 NonCopyable copy constructor.
 MBED_DEPRECATED ("Invalid copy assignment of a NonCopyable resource.") NonCopyable &operator
 NonCopyable copy assignment operator.
 MBED_DEPRECATED ("Invalid copy construction of a NonCopyable resource.") NonCopyable(const NonCopyable &)
 NonCopyable copy constructor.
 MBED_DEPRECATED ("Invalid copy assignment of a NonCopyable resource.") NonCopyable &operator
 NonCopyable copy assignment operator.

Detailed Description

Class providing buffered UART communication functionality using separate circular buffer for send and receive channels.

Definition at line 50 of file cmsis/BUILD/mbed/drivers/UARTSerial.h.


Constructor & Destructor Documentation

UARTSerial ( PinName  tx,
PinName  rx,
int  baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE 
)

Create a UARTSerial port, connected to the specified transmit and receive pins, with a particular baud rate.

Parameters:
txTransmit pin
rxReceive pin
baudThe baud rate of the serial port (optional, defaults to MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE)

Definition at line 31 of file UARTSerial.cpp.

UARTSerial ( PinName  tx,
PinName  rx,
int  baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE 
)

Create a UARTSerial port, connected to the specified transmit and receive pins, with a particular baud rate.

Parameters:
txTransmit pin
rxReceive pin
baudThe baud rate of the serial port (optional, defaults to MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE)

Member Function Documentation

int close (  ) [virtual]

Close a file.

Returns:
0 on success, negative error code on failure

Implements FileHandle.

Definition at line 88 of file UARTSerial.cpp.

virtual int close (  ) [virtual]

Close a file.

Returns:
0 on success, negative error code on failure

Implements FileHandle.

virtual bool is_blocking (  ) const [virtual]

Check current blocking or non-blocking mode for file operations.

Returns:
true for blocking mode, false for non-blocking mode.

Definition at line 150 of file cmsis/BUILD/mbed/drivers/UARTSerial.h.

virtual bool is_blocking (  ) const [virtual]

Check current blocking or non-blocking mode for file operations.

Returns:
true for blocking mode, false for non-blocking mode.

Definition at line 150 of file drivers/UARTSerial.h.

int isatty (  ) [virtual]

Check if the file in an interactive terminal device.

Returns:
True if the file is a terminal
False if the file is not a terminal
Negative error code on failure

Reimplemented from FileHandle.

Definition at line 95 of file UARTSerial.cpp.

virtual int isatty (  ) [virtual]

Check if the file in an interactive terminal device.

Returns:
True if the file is a terminal
False if the file is not a terminal
Negative error code on failure

Reimplemented from FileHandle.

MBED_DEPRECATED ( "Invalid copy construction of a NonCopyable< FileHandle > resource."   ) const [protected, inherited]

NonCopyable copy constructor.

A compile time warning is issued when this function is used, and a runtime warning is printed when the copy construction of the noncopyable happens.

If you see this warning, your code is probably doing something unspecified. Copying of noncopyable resources can lead to resource leak and random error.

Definition at line 190 of file cmsis/BUILD/mbed/platform/NonCopyable.h.

MBED_DEPRECATED ( "Invalid copy assignment of a NonCopyable< FileHandle > resource."   ) [protected, inherited]

NonCopyable copy assignment operator.

A compile time warning is issued when this function is used, and a runtime warning is printed when the copy construction of the noncopyable happens.

If you see this warning, your code is probably doing something unspecified. Copying of noncopyable resources can lead to resource leak and random error.

MBED_DEPRECATED_SINCE ( "mbed-os-5.4"  ,
"Replaced by FileHandle::seek"   
) [inherited]

Move the file position to a given offset from a given location.

Parameters:
offsetThe offset from whence to move to
whenceSEEK_SET for the start of the file, SEEK_CUR for the current file position, or SEEK_END for the end of the file.
Returns:
new file position on success, -1 on failure or unsupported
short poll ( short  events ) const [virtual]

Equivalent to POSIX poll().

Derived from FileHandle. Provides a mechanism to multiplex input/output over a set of file handles.

Definition at line 260 of file UARTSerial.cpp.

virtual short poll ( short  events ) const [virtual]

Equivalent to POSIX poll().

Derived from FileHandle. Provides a mechanism to multiplex input/output over a set of file handles.

virtual ssize_t read ( void *  buffer,
size_t  length 
) [virtual]

Read the contents of a file into a buffer.

Follows POSIX semantics:

* if no data is available, and non-blocking set return -EAGAIN * if no data is available, and blocking set, wait until data is available * If any data is available, call returns immediately

Parameters:
bufferThe buffer to read in to
lengthThe number of bytes to read
Returns:
The number of bytes read, 0 at end of file, negative error on failure

Implements FileHandle.

ssize_t read ( void *  buffer,
size_t  length 
) [virtual]

Read the contents of a file into a buffer.

Follows POSIX semantics:

* if no data is available, and non-blocking set return -EAGAIN * if no data is available, and blocking set, wait until data is available * If any data is available, call returns immediately

Parameters:
bufferThe buffer to read in to
lengthThe number of bytes to read
Returns:
The number of bytes read, 0 at end of file, negative error on failure

Implements FileHandle.

Definition at line 206 of file UARTSerial.cpp.

virtual void rewind (  ) [virtual, inherited]

Rewind the file position to the beginning of the file.

Note:
This is equivalent to seek(0, SEEK_SET)

Reimplemented in Stream, and Stream.

Definition at line 130 of file platform/FileHandle.h.

virtual void rewind (  ) [virtual, inherited]

Rewind the file position to the beginning of the file.

Note:
This is equivalent to seek(0, SEEK_SET)

Reimplemented in Stream, and Stream.

Definition at line 130 of file cmsis/BUILD/mbed/platform/FileHandle.h.

virtual off_t seek ( off_t  offset,
int  whence 
) [virtual]

Move the file position to a given offset from from a given location.

Not valid for a device type FileHandle like UARTSerial. In case of UARTSerial, returns ESPIPE

Parameters:
offsetThe offset from whence to move to
whenceThe start of where to seek SEEK_SET to start from beginning of file, SEEK_CUR to start from current position in file, SEEK_END to start from end of file
Returns:
The new offset of the file, negative error code on failure

Implements FileHandle.

off_t seek ( off_t  offset,
int  whence 
) [virtual]

Move the file position to a given offset from from a given location.

Not valid for a device type FileHandle like UARTSerial. In case of UARTSerial, returns ESPIPE

Parameters:
offsetThe offset from whence to move to
whenceThe start of where to seek SEEK_SET to start from beginning of file, SEEK_CUR to start from current position in file, SEEK_END to start from end of file
Returns:
The new offset of the file, negative error code on failure

Implements FileHandle.

Definition at line 101 of file UARTSerial.cpp.

void set_baud ( int  baud )

Set the baud rate.

Parameters:
baudThe baud rate

Definition at line 52 of file UARTSerial.cpp.

void set_baud ( int  baud )

Set the baud rate.

Parameters:
baudThe baud rate
virtual int set_blocking ( bool  blocking ) [virtual]

Set blocking or non-blocking mode The default is blocking.

Parameters:
blockingtrue for blocking mode, false for non-blocking mode.

Definition at line 140 of file cmsis/BUILD/mbed/drivers/UARTSerial.h.

virtual int set_blocking ( bool  blocking ) [virtual]

Set blocking or non-blocking mode The default is blocking.

Parameters:
blockingtrue for blocking mode, false for non-blocking mode.

Definition at line 140 of file drivers/UARTSerial.h.

void set_data_carrier_detect ( PinName  dcd_pin,
bool  active_high = false 
)

Setup interrupt handler for DCD line.

If DCD line is connected, an IRQ handler will be setup. Does nothing if DCD is NC, i.e., not connected.

Parameters:
dcd_pinPin-name for DCD
active_higha boolean set to true if DCD polarity is active low
void set_data_carrier_detect ( PinName  dcd_pin,
bool  active_high = false 
)

Setup interrupt handler for DCD line.

If DCD line is connected, an IRQ handler will be setup. Does nothing if DCD is NC, i.e., not connected.

Parameters:
dcd_pinPin-name for DCD
active_higha boolean set to true if DCD polarity is active low

Definition at line 57 of file UARTSerial.cpp.

void set_flow_control ( Flow  type,
PinName  flow1 = NC,
PinName  flow2 = NC 
)

Set the flow control type on the serial port.

Parameters:
typethe flow control type (Disabled, RTS, CTS, RTSCTS)
flow1the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS)
flow2the second flow control pin (CTS for RTSCTS)

Reimplemented from SerialBase.

Definition at line 80 of file UARTSerial.cpp.

void set_flow_control ( Flow  type,
PinName  flow1 = NC,
PinName  flow2 = NC 
)

Set the flow control type on the serial port.

Parameters:
typethe flow control type (Disabled, RTS, CTS, RTSCTS)
flow1the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS)
flow2the second flow control pin (CTS for RTSCTS)

Reimplemented from SerialBase.

void set_format ( int  bits = 8,
Parity  parity = UARTSerial::None,
int  stop_bits = 1 
)

Set the transmission format used by the serial port.

Parameters:
bitsThe number of bits in a word (5-8; default = 8)
parityThe parity used (None, Odd, Even, Forced1, Forced0; default = None)
stop_bitsThe number of stop bits (1 or 2; default = 1)
void set_format ( int  bits = 8,
Parity  parity = UARTSerial::None,
int  stop_bits = 1 
)

Set the transmission format used by the serial port.

Parameters:
bitsThe number of bits in a word (5-8; default = 8)
parityThe parity used (None, Odd, Even, Forced1, Forced0; default = None)
stop_bitsThe number of stop bits (1 or 2; default = 1)

Definition at line 72 of file UARTSerial.cpp.

void sigio ( Callback< void()>  func ) [virtual]

Register a callback on state change of the file.

The specified callback will be called on state changes such as when the file can be written to or read from.

The callback may be called in an interrupt context and should not perform expensive operations.

Note! This is not intended as an attach-like asynchronous api, but rather as a building block for constructing such functionality.

The exact timing of when the registered function is called is not guaranteed and susceptible to change. It should be used as a cue to make read/write/poll calls to find the current state.

Parameters:
funcFunction to call on state change

Definition at line 124 of file UARTSerial.cpp.

virtual void sigio ( Callback< void()>  func ) [virtual]

Register a callback on state change of the file.

The specified callback will be called on state changes such as when the file can be written to or read from.

The callback may be called in an interrupt context and should not perform expensive operations.

Note! This is not intended as an attach-like asynchronous api, but rather as a building block for constructing such functionality.

The exact timing of when the registered function is called is not guaranteed and susceptible to change. It should be used as a cue to make read/write/poll calls to find the current state.

Parameters:
funcFunction to call on state change
off_t size (  ) [virtual, inherited]

Get the size of the file.

Returns:
Size of the file in bytes

Reimplemented in Stream, and Stream.

Definition at line 23 of file FileHandle.cpp.

virtual off_t size (  ) [virtual, inherited]

Get the size of the file.

Returns:
Size of the file in bytes

Reimplemented in Stream, and Stream.

int sync (  ) [virtual]

Flush any buffers associated with the file.

Returns:
0 on success, negative error code on failure

Reimplemented from FileHandle.

Definition at line 108 of file UARTSerial.cpp.

virtual int sync (  ) [virtual]

Flush any buffers associated with the file.

Returns:
0 on success, negative error code on failure

Reimplemented from FileHandle.

virtual off_t tell (  ) [virtual, inherited]

Get the file position of the file.

Note:
This is equivalent to seek(0, SEEK_CUR)
Returns:
The current offset in the file, negative error code on failure

Reimplemented in Stream, and Stream.

Definition at line 121 of file cmsis/BUILD/mbed/platform/FileHandle.h.

virtual off_t tell (  ) [virtual, inherited]

Get the file position of the file.

Note:
This is equivalent to seek(0, SEEK_CUR)
Returns:
The current offset in the file, negative error code on failure

Reimplemented in Stream, and Stream.

Definition at line 121 of file platform/FileHandle.h.

virtual ssize_t write ( const void *  buffer,
size_t  length 
) [virtual]

Write the contents of a buffer to a file.

Follows POSIX semantics:

* if blocking, block until all data is written * if no data can be written, and non-blocking set, return -EAGAIN * if some data can be written, and non-blocking set, write partial

Parameters:
bufferThe buffer to write from
lengthThe number of bytes to write
Returns:
The number of bytes written, negative error on failure

Implements FileHandle.

ssize_t write ( const void *  buffer,
size_t  length 
) [virtual]

Write the contents of a buffer to a file.

Follows POSIX semantics:

* if blocking, block until all data is written * if no data can be written, and non-blocking set, return -EAGAIN * if some data can be written, and non-blocking set, write partial

Parameters:
bufferThe buffer to write from
lengthThe number of bytes to write
Returns:
The number of bytes written, negative error on failure

Implements FileHandle.

Definition at line 154 of file UARTSerial.cpp.