|
| 1 | +# BufferedSerial |
| 2 | + |
| 3 | +<span class="images"><span>`BufferedSerial` class hierarchy</span></span> |
| 4 | + |
| 5 | +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. |
| 6 | + |
| 7 | +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. |
| 8 | + |
| 9 | +Data is sent/received at a pre-defined speed called baud rate. Standard baud rate include 9600, 119200, 115200 or others. |
| 10 | + |
| 11 | +Pieces of data are transmitted using data packets of configurable sizes divided in different sections which include: |
| 12 | + |
| 13 | +* Start bit: Indicates the start of UART data transmission |
| 14 | +* Data frame: Can be 5 to 8 (or 9 if a parity bit is not used) bits long for the actual data being transferred. |
| 15 | +* Parity bit: Optional bit, used for data error detection. |
| 16 | +* Stop bits: Can be 1 to 2 bits long to signal the end of a data packet. |
| 17 | + |
| 18 | +The `BufferedSerial` class maps targets underlying serial communication functions. See the [porting guide](../porting/serial-port.html) for target serial support. |
| 19 | + |
| 20 | +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. |
| 21 | +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. |
| 22 | +Using intermediary buffers allows it to be used reliably for input from non-interrupt context whilst avoiding excess spinning waiting for transmission buffer space. |
| 23 | + |
| 24 | +## Configuration |
| 25 | + |
| 26 | +The following parameters can be configured at object instantiation: |
| 27 | + |
| 28 | + - _TX_ |
| 29 | + - _RX_ |
| 30 | + - _Baud rate_ - defaults to the value configured in `mbed-os/platform/mbed_lib.json`. |
| 31 | + |
| 32 | +The following parameters can be configured after an `BufferedSerial` object instantiation. |
| 33 | + |
| 34 | + - _Baud rate_ |
| 35 | + - _Data frame length_ |
| 36 | + - _Parity bit_ |
| 37 | + - _Stop bits_ |
| 38 | + |
| 39 | +The default settings for a microcontroller are described as _9600-8-N-1_, a common notation for serial port settings. |
| 40 | + |
| 41 | +Additionally, hardware flow control can also be configured if necessary. |
| 42 | + |
| 43 | +You can view more information about the configurable settings and functions in the class reference. |
| 44 | + |
| 45 | +## Class reference |
| 46 | + |
| 47 | +[](https://os.mbed.com/docs/mbed-os/development/mbed-os-api-doxy/classmbed_1_1_buffered_serial.html) |
| 48 | + |
| 49 | +<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> |
| 50 | + |
| 51 | +## Example |
| 52 | + |
| 53 | +[](https://os.mbed.com/teams/mbed_example/code/BufferedSerial/file/112a40a5991a/main.cpp) |
| 54 | + |
| 55 | + |
| 56 | +### Mbed OS usage |
| 57 | + |
| 58 | +Common use cases for `BufferedSerial` 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). |
0 commit comments