UnbufferedSerial
UnbufferedSerial
class hierarchy
The UnbufferedSerial class provides UART functionality with an API similar to the BufferedSerial class. The classes also share the same default configurations.
Unlike the BufferedSerial class, the UnbufferedSerial class does not use intermediary buffers to store bytes to transmit to or read from the hardware. The user application is responsible for processing each byte as it is received. The method to read data returns only one byte for every call. Therefore, we recommend you use this class when you need more control and for use in interrupt handlers with the RTOS. You can also use this class to write multiple bytes at once. Because it does not acquire a mutex lock, you must ensure only one instance uses the serial port.
For normal blocking applications that require a serial channel for something other than the console, BufferedSerial performs better than UnbufferedSerial
and causes less CPU load and fewer latency issues. Only applications that are short of RAM and cannot afford buffering or that need more control of the serial port and use it from IRQ should use UnbufferedSerial.
You can view more information about the configurable settings and functions in the class reference.
Class reference
Public Member Functions | |
UnbufferedSerial (PinName tx, PinName rx, int baud=MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE) | |
Create a serial port instance connected to the specified transmit and receive pins, with the specified baud rate. More... | |
UnbufferedSerial (const serial_pinmap_t &static_pinmap, int baud=MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE) | |
Create a UnbufferedSerial port, connected to the specified transmit and receive pins, with a particular baud rate. More... | |
ssize_t | write (const void *buffer, size_t size) override |
Write the contents of a buffer to a file. More... | |
ssize_t | read (void *buffer, size_t size) override |
Read the contents of a file into a buffer. More... | |
off_t | seek (off_t offset, int whence=SEEK_SET) override |
Move the file position to a given offset from from a given location. More... | |
off_t | size () override |
Get the size of the file. More... | |
int | isatty () override |
Check if the file in an interactive terminal device. More... | |
int | close () override |
Close a file. More... | |
short | poll (short events) const override |
Check for poll event flags Check the events listed in events to see if data can be read or written without blocking. More... | |
void | set_flow_control (Flow type, PinName flow1=NC, PinName flow2=NC) |
Set the flow control type on the serial port. More... | |
virtual int | sync () |
Flush any buffers associated with the file. 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 | truncate (off_t length) |
Truncate or extend a 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... | |
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... |
Private Member Functions | |
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... | |
int | readable () |
Determine if there is a character available to read. More... | |
int | writeable () |
Determine if there is space available to write a character. More... | |
void | attach (Callback< void()> func, IrqType type=RxIrq) |
Attach a 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 | enable_input (bool enable=true) |
Enable serial input. More... | |
void | enable_output (bool enable=true) |
Enable serial output. More... | |
void | set_flow_control (Flow type, const serial_fc_pinmap_t &static_pinmap) |
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... |
UnbufferedSerial Example
/*
* Copyright (c) 2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/
#include "mbed.h"
// Create a DigitalOutput object to toggle an LED whenever data is received.
static DigitalOut led(LED1);
// Create a UnbufferedSerial object with a default baud rate.
static UnbufferedSerial serial_port(USBTX, USBRX);
void on_rx_interrupt()
{
char c;
// Toggle the LED.
led = !led;
// Read the data to clear the receive interrupt.
if (serial_port.read(&c, 1)) {
// Echo the input back to the terminal.
serial_port.write(&c, 1);
}
}
int main(void)
{
// Set desired properties (9600-8-N-1).
serial_port.baud(9600);
serial_port.format(
/* bits */ 8,
/* parity */ SerialBase::None,
/* stop bit */ 1
);
// Register a callback to process a Rx (receive) interrupt.
serial_port.attach(&on_rx_interrupt, SerialBase::RxIrq);
}
Mbed OS use
Common use cases for UnbufferedSerial
are IRQ heavy UART operations, such as BLE Cordio in the transport driver.