Skip to content

Add UnbufferedSerial and BufferedSerial API documentation #1220

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

Merged
merged 6 commits into from
Feb 18, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
75 changes: 75 additions & 0 deletions docs/api/io/BufferedSerial.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# BufferedSerial

<span class="images">![](https://os.mbed.com/docs/development/mbed-os-api-doxy/classmbed_1_1_buffered_serial.png)<span>`BufferedSerial` class hierarchy</span></span>

The `BufferedSerial` class provides UART functionality, it is the recommended class for serial data transfers. It allows sending and receiving bytes of data in a sequence using separate transmit (TX) and receive pins (RX). Communication can be between two processors or for sending text to a console.

Serial channels have the following characteristics:

* TX and RX pins - can be specified as Not Connected (NC) for simplex (unidirectional) communication or as valid pins for full duplex (bi-directional) communication.

* Baud rate - pre-defined speed at which data is sent and received. Standard baud rates include 9600, 119200, 115200 or others.

Data is transmitted using packets of configurable sizes divided in different sections which include:

* Start bit: Indicates the start of UART data transmission.
* Data frame: Can be 5 to 8 (or 9 if a parity bit is not used) bits long for the actual data being transferred.
* Parity bit: Optional bit, used for data error detection.
* Stop bits: Can be 1 to 2 bits long to signal the end of a data packet.

The `BufferedSerial` calls the underlying HAL API functions. See the [porting guide](../porting/serial-port.html) for target serial support.

When the RX interrupt is trigged, the `BufferedSerial` class stores the byte(s) available to read from the hardware buffer to an internal intermediary buffer. When a read request is made, the `BufferedSerial` class uses a mutex lock and enters a critical section to read out the number of bytes requested if as many are available in the intermediary buffer.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do we mean by "the RX is trigged," and who or what trigged it?
Who or what makes the read request?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RX is explained in the first paragraph of the page. It is triggered when receiving data from whatever is connected to that interface (a sensor, a console, etc...).


To transmit multiple bytes, the class uses an intermediary buffer to store the bytes to send and monitors the serial interface to transfer them to the hardware buffer as soon as it is available. However, all bytes are written unbuffered if in a critical section.
Using intermediary buffers allows it to be used reliably for input from non-interrupt context whilst avoiding excess spinning waiting for transmission buffer space.

The RX and TX buffers are circular buffers with pre-allocated sizes configurable using the configuration parameters `uart-serial-rxbuf-size` and `uart-serial-txbuf-size` respectively. Both configuration parameters can be found in `drivers/mbed_lib.json`.

## Configuration
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these the same configuration parameters you get by running the --config -v command?

Copy link
Contributor Author

@hugueskamba hugueskamba Feb 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to configure a UART interface via the BufferedSerial class for the user application.
Nothing to do with Mbed CLI.


The following parameters can be configured at object instantiation:

- _TX_
- _RX_
- _Baud rate_

The default baud rate value is configured in `mbed-os/platform/mbed_lib.json`.

The following parameters can be configured after an `BufferedSerial` object instantiation.

- _Baud rate_
- _Data frame length_
- _Parity bit_
- _Stop bits_

The default settings for a microcontroller are set as _9600-8-N-1_, a common notation for serial port settings. This denotes a baud rate of nine thousand six hundred (9600), eight data frame length (8), no parity bit (N) and one stop bit (1).

Additionally, hardware flow control can also be configured if necessary.

You can view more information about the configurable settings and functions in the class reference.

## Class reference

[![View code](https://www.mbed.com/embed/?type=library)](https://os.mbed.com/docs/mbed-os/development/mbed-os-api-doxy/classmbed_1_1_buffered_serial.html)

<span class="notes">**Note**: On a Windows machine, you need to install a USB serial driver. See [Windows serial configuration](../tutorials/serial-communication.html#windows-serial-driver).</span>

## Example

[![View code](https://www.mbed.com/embed/?url=https://os.mbed.com/teams/mbed_example/code/BufferedSerial/)](https://os.mbed.com/teams/mbed_example/code/BufferedSerial/file/112a40a5991a/main.cpp)


## Printing to the console

Mbed OS redefines target-dependent I/O functions in the C library to allow the C standard I/O library functions (`s\v\f\n\printf`, `scanf`, etc) to be used in user application for printing to the console.

The system I/O retarget code can be configured to be buffered or unbuffered depending on the configuration of the parameter `stdio-buffered-serial` in `platform/mbed_lib.json`. When it is buffered, an instance of a `BufferedSerial` class is used by the retarget code to perform the actual printing. This is where `BufferedSerial`'s `Filehandle` inheritance is used.


Alternatively, if more configuration of the serial interface is needed, an instance of the `BufferedSerial` class can be passed to the system I/O retarget code at run time to be used for printing to the console as shown below:

[![View code](https://www.mbed.com/embed/?url=https://os.mbed.com/teams/mbed_example/code/BufferedSerial/)](https://os.mbed.com/teams/mbed_example/code/BufferedSerial_printf/file/112a40a5991a/main.cpp)


Using the standard C I/O library directly is the recommended way to print to the console in Mbed OS.
26 changes: 26 additions & 0 deletions docs/api/io/UnbufferedSerial.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# UnbufferedSerial

<span class="images">![](https://os.mbed.com/docs/development/mbed-os-api-doxy/classmbed_1_1_unbuffered_serial.png)<span>`UnbufferedSerial` class hierarchy</span></span>

The `UnbufferedSerial` class provides UART functionality with an API similar to the [`BufferedSerial`](./BufferedSerial.md) 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 they are received. The method to read data returns only one byte for every call. It is therefore suitable when more control is required and for use in interrupt handlers with the RTOS. The class can however be used to write multiple bytes at once. Since it does not acquire a mutex lock, care must be taken that only one instance is using the serial port.

For normal blocking application that require a serial channel for something other than the console, `BufferedSerial` will perform better than `UnbufferedSerial` and will cause less CPU load and latency issues. `UnbufferedSerial` should only be used by applications that are short of RAM and cannot afford buffering or need more control of the serial port and use it from IRQ.

You can view more information about the configurable settings and functions in the class reference.

## Class reference

[![View code](https://www.mbed.com/embed/?type=library)](https://os.mbed.com/docs/mbed-os/development/mbed-os-api-doxy/classmbed_1_1_unbuffered_serial.html)

<span class="notes">**Note**: On a Windows machine, you need to install a USB serial driver. See [Windows serial configuration](../tutorials/serial-communication.html#windows-serial-driver).</span>

## Example

[![View code](https://www.mbed.com/embed/?url=https://os.mbed.com/teams/mbed_example/code/UnbufferedSerial/)](https://os.mbed.com/teams/mbed_example/code/UnbufferedSerial/file/112a40a5991a/main.cpp)


### Mbed OS usage

Common use cases for `UnbufferedSerial` are IRQ heavy UART operations, such as the [BLE cordio in the transport driver](https://github.com/ARMmbed/mbed-os/blob/master/features/FEATURE_BLE/targets/TARGET_CORDIO/driver/H4TransportDriver.cpp#L62).