A serial port (UART) for communication with other serial devices. More...
#include <Serial.h>
Public Member Functions | |
Serial (PinName tx, PinName rx, const char *name=NULL, int baud=MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE) | |
Create a Serial port, connected to the specified transmit and receive pins. More... | |
Serial (PinName tx, PinName rx, int baud) | |
Create a Serial port, connected to the specified transmit and receive pins, with the specified baud. More... | |
void | baud (int baudrate) |
Set the baud rate of the serial port. More... | |
void | format (int bits=8, Parity parity=SerialBase::None, int stop_bits=1) |
Set the transmission format used by the serial port. More... | |
void | attach (Callback< void()> func, IrqType type=RxIrq) |
Attach a function to call whenever a serial interrupt is generated. More... | |
template<typename T > | |
void | attach (T *obj, void(T::*method)(), IrqType type=RxIrq) |
Attach a member function to call whenever a serial interrupt is generated. More... | |
template<typename T > | |
void | attach (T *obj, void(*method)(T *), IrqType type=RxIrq) |
Attach a member function to call whenever a serial interrupt is generated. More... | |
void | set_break () |
Generate a break condition on the serial line NOTE: Clear break needs to run at least one frame after set_break is called. More... | |
void | clear_break () |
Clear a break condition on the serial line NOTE: Should be run at least one frame after set_break is called. More... | |
void | send_break () |
Generate a break condition on the serial line. More... | |
void | set_flow_control (Flow type, PinName flow1=NC, PinName flow2=NC) |
Set the flow control type on the serial port. More... | |
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. More... | |
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. More... | |
void | abort_write () |
Abort the on-going write transfer. More... | |
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. More... | |
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. More... | |
void | abort_read () |
Abort the on-going read transfer. More... | |
int | set_dma_usage_tx (DMAUsage usage) |
Configure DMA usage suggestion for non-blocking TX transfers. More... | |
int | set_dma_usage_rx (DMAUsage usage) |
Configure DMA usage suggestion for non-blocking RX transfers. More... | |
virtual int | truncate (off_t length) |
Truncate or extend a file. More... | |
virtual off_t | lseek (off_t offset, int whence) |
Move the file position to a given offset from a given location. More... | |
virtual int | fsync () |
Flush any buffers associated with the FileHandle, ensuring it is up to date on disk. More... | |
virtual off_t | flen () |
Find the length of the file. More... | |
virtual int | set_blocking (bool blocking) |
Set blocking or nonblocking mode of the file operation like read/write. More... | |
virtual bool | is_blocking () const |
Check current blocking or nonblocking mode for file operations. More... | |
virtual int | enable_input (bool enabled) |
Enable or disable input. More... | |
virtual int | enable_output (bool enabled) |
Enable or disable output. More... | |
virtual short | poll (short events) const |
Check for poll event flags You can use or ignore the input parameter. More... | |
bool | writable () const |
Definition depends on the subclass implementing FileHandle. More... | |
bool | readable () const |
Definition depends on the subclass implementing FileHandle. More... | |
virtual void | sigio (Callback< void()> func) |
Register a callback on state change of the file. More... | |
Protected Member Functions | |
virtual int | close () |
Close a file. More... | |
virtual ssize_t | write (const void *buffer, size_t length) |
Write the contents of a buffer to a file. More... | |
virtual ssize_t | read (void *buffer, size_t length) |
Read the contents of a file into a buffer. More... | |
virtual off_t | seek (off_t offset, int whence) |
Move the file position to a given offset from from a given location. More... | |
virtual off_t | tell () |
Get the file position of the file. More... | |
virtual void | rewind () |
Rewind the file position to the beginning of the file. More... | |
virtual int | isatty () |
Check if the file in an interactive terminal device. More... | |
virtual int | sync () |
Flush any buffers associated with the file. More... | |
virtual off_t | size () |
Get the size of the file. More... | |
virtual void | lock () |
Acquire exclusive access to this object. More... | |
virtual void | unlock () |
Release exclusive access to this object. More... | |
A serial port (UART) for communication with other serial devices.
Can be used for Full Duplex communication, or Simplex by specifying one pin as NC (Not Connected)
Example:
Serial | ( | PinName | tx, |
PinName | rx, | ||
const char * | name = NULL , |
||
int | baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE |
||
) |
Create a Serial port, connected to the specified transmit and receive pins.
tx | Transmit pin |
rx | Receive pin |
name | The name of the stream associated with this serial port (optional) |
baud | The baud rate of the serial port (optional, defaults to MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE or 9600) |
Serial | ( | PinName | tx, |
PinName | rx, | ||
int | baud | ||
) |
Create a Serial port, connected to the specified transmit and receive pins, with the specified baud.
tx | Transmit pin |
rx | Receive pin |
baud | The baud rate of the serial port |
|
inherited |
Abort the on-going read transfer.
It is safe to call abort_read() when there is no on-going transaction.
|
inherited |
Abort the on-going write transfer.
It is safe to call abort_write() when there is no on-going transaction.
|
inherited |
Attach a function to call whenever a serial interrupt is generated.
func | A pointer to a void function, or 0 to set as none |
type | Which serial interrupt to attach the member function to (Serial::RxIrq for receive, TxIrq for transmit buffer empty) |
|
inherited |
Attach a member function to call whenever a serial interrupt is generated.
obj | pointer to the object to call the member function on |
method | pointer to the member function to be called |
type | Which serial interrupt to attach the member function to (Serial::RxIrq for receive, TxIrq for transmit buffer empty) |
Definition at line 118 of file SerialBase.h.
|
inherited |
Attach a member function to call whenever a serial interrupt is generated.
obj | pointer to the object to call the member function on |
method | pointer to the member function to be called |
type | Which serial interrupt to attach the member function to (Serial::RxIrq for receive, TxIrq for transmit buffer empty) |
Definition at line 136 of file SerialBase.h.
|
inherited |
Set the baud rate of the serial port.
baudrate | The baudrate of the serial port (default = 9600). |
|
inherited |
Clear a break condition on the serial line NOTE: Should be run at least one frame after set_break is called.
|
protectedvirtualinherited |
|
virtualinherited |
Enable or disable input.
Control enabling of device for input. This is primarily intended for temporary power-saving; the overall ability of the device to operate for input and/or output may be fixed at creation time, but this call can allow input to be temporarily disabled to permit power saving without losing device state.
enabled | true to enable input, false to disable. |
Reimplemented in UARTSerial.
Definition at line 236 of file FileHandle.h.
|
virtualinherited |
Enable or disable output.
Control enabling of device for output. This is primarily intended for temporary power-saving; the overall ability of the device to operate for input and/or output may be fixed at creation time, but this call can allow output to be temporarily disabled to permit power saving without losing device state.
enabled | true to enable output, false to disable. |
Reimplemented in UARTSerial.
Definition at line 254 of file FileHandle.h.
|
virtualinherited |
Find the length of the file.
Definition at line 195 of file FileHandle.h.
|
inherited |
Set the transmission format used by the serial port.
bits | The number of bits in a word (5-8; default = 8) |
parity | The parity used (SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0; default = SerialBase::None) |
stop_bits | The number of stop bits (1 or 2; default = 1) |
|
virtualinherited |
Flush any buffers associated with the FileHandle, ensuring it is up to date on disk.
Definition at line 183 of file FileHandle.h.
|
virtualinherited |
Check current blocking or nonblocking mode for file operations.
Reimplemented in UARTSerial.
Definition at line 218 of file FileHandle.h.
|
protectedvirtualinherited |
Check if the file in an interactive terminal device.
Reimplemented from FileHandle.
|
protectedvirtualinherited |
|
virtualinherited |
Move the file position to a given offset from a given location.
offset | The offset from whence to move to |
whence | SEEK_SET for the start of the file, SEEK_CUR for the current file position, or SEEK_END for the end of the file. |
Definition at line 169 of file FileHandle.h.
|
virtualinherited |
Check for poll event flags You can use or ignore the input parameter.
You can return all events or check just the events listed in events. Call is nonblocking - returns instantaneous state of events. Whenever an event occurs, the derived class should call the sigio() callback).
events | bitmask of poll events we're interested in - POLLIN/POLLOUT etc. |
Reimplemented in UARTSerial.
Definition at line 269 of file FileHandle.h.
|
protectedvirtualinherited |
Read the contents of a file into a buffer.
Devices acting as FileHandles should follow POSIX semantics:
buffer | The buffer to read in to |
size | The number of bytes to read |
Implements FileHandle.
|
inherited |
Begin asynchronous reading using 8bit buffer.
The read operation ends with any of the enabled events and invokes registered callback function (which can be NULL to not receive callback at all). Events that are not enabled by event argument are simply ignored. Operation has to be ended explicitly by calling abort_read() when no events are enabled. This function locks the deep sleep until any event has occurred.
buffer | The buffer where received data will be stored |
length | The buffer length in bytes |
callback | The event callback function |
event | The logical OR of RX events that should end operation |
char_match | The matching character |
|
inherited |
Begin asynchronous reading using 16bit buffer.
The read operation ends with any of the enabled events and invokes registered callback function (which can be NULL to not receive callback at all). Events that are not enabled by event argument are simply ignored. Operation has to be ended explicitly by calling abort_read() when no events are enabled. This function locks the deep sleep until any event has occurred.
buffer | The buffer where received data will be stored |
length | The buffer length in bytes |
callback | The event callback function |
event | The logical OR of RX events that should end operation |
char_match | The matching character |
|
inherited |
Definition depends on the subclass implementing FileHandle.
For example, if the FileHandle is of type Stream, readable() could return true when there is something available to read.
Definition at line 292 of file FileHandle.h.
|
protectedvirtualinherited |
Rewind the file position to the beginning of the file.
Reimplemented from FileHandle.
|
protectedvirtualinherited |
Move the file position to a given offset from from a given location.
offset | The offset from whence to move to |
whence | The 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 |
Implements FileHandle.
|
inherited |
Generate a break condition on the serial line.
|
virtualinherited |
Set blocking or nonblocking mode of the file operation like read/write.
Definition depends on the subclass implementing FileHandle. The default is blocking.
blocking | true for blocking mode, false for nonblocking mode. |
Reimplemented in UARTSerial.
Definition at line 209 of file FileHandle.h.
|
inherited |
Generate a break condition on the serial line NOTE: Clear break needs to run at least one frame after set_break is called.
|
inherited |
Configure DMA usage suggestion for non-blocking RX transfers.
usage | The usage DMA hint for peripheral |
|
inherited |
Configure DMA usage suggestion for non-blocking TX transfers.
usage | The usage DMA hint for peripheral |
|
inherited |
Set the flow control type on the serial port.
type | the flow control type (Disabled, RTS, CTS, RTSCTS) |
flow1 | the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS) |
flow2 | the second flow control pin (CTS for RTSCTS) |
|
virtualinherited |
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 is susceptible to change. It should be used as a cue to make read/write/poll calls to find the current state.
func | Function to call on state change |
Reimplemented in UARTSerial.
Definition at line 314 of file FileHandle.h.
|
protectedvirtualinherited |
|
protectedvirtualinherited |
Flush any buffers associated with the file.
Reimplemented from FileHandle.
|
protectedvirtualinherited |
Get the file position of the file.
Reimplemented from FileHandle.
|
virtualinherited |
Truncate or extend a file.
The file's length is set to the specified value. The seek pointer is not changed. If the file is extended, the extended area appears as if it were zero-filled.
length | The requested new length for the file |
Reimplemented in File, and TestFile< FILE_SIZE >.
Definition at line 151 of file FileHandle.h.
|
protectedvirtualinherited |
|
inherited |
Definition depends on the subclass implementing FileHandle.
For example, if the FileHandle is of type Stream, writable() could return true when there is ample buffer space available for write() calls.
Definition at line 281 of file FileHandle.h.
|
protectedvirtualinherited |
Write the contents of a buffer to a file.
Devices acting as FileHandles should follow POSIX semantics:
if some data can be written, and nonblocking set, write partial
buffer | The buffer to write from |
size | The number of bytes to write |
Implements FileHandle.
|
inherited |
Begin asynchronous write using 8bit buffer.
The write operation ends with any of the enabled events and invokes registered callback function (which can be NULL to not receive callback at all). Events that are not enabled by event argument are simply ignored. Operation has to be ended explicitly by calling abort_write() when no events are enabled. This function locks the deep sleep until any event has occurred.
buffer | The buffer where received data will be stored |
length | The buffer length in bytes |
callback | The event callback function |
event | The logical OR of TX events that should end operation |
|
inherited |
Begin asynchronous write using 16bit buffer.
The write operation ends with any of the enabled events and invokes registered callback function (which can be NULL to not receive callback at all). Events that are not enabled by event argument are simply ignored. Operation has to be ended explicitly by calling abort_write() when no events are enabled. This function locks the deep sleep until any event has occurred.
buffer | The buffer where received data will be stored |
length | The buffer length in bytes |
callback | The event callback function |
event | The logical OR of TX events that should end operation |