|
| 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 | +Serial channels have the following characteristics: |
| 8 | + |
| 9 | +* 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. |
| 10 | + |
| 11 | +* Baud rate - pre-defined speed at which data is sent and received. Standard baud rates include 9600, 119200, 115200 or others. |
| 12 | + |
| 13 | +Data is transmitted using packets of configurable sizes divided in different sections which include: |
| 14 | + |
| 15 | +* Start bit: Indicates the start of UART data transmission. |
| 16 | +* Data frame: Can be 5 to 8 (or 9 if a parity bit is not used) bits long for the actual data being transferred. |
| 17 | +* Parity bit: Optional bit, used for data error detection. |
| 18 | +* Stop bits: Can be 1 to 2 bits long to signal the end of a data packet. |
| 19 | + |
| 20 | +The `BufferedSerial` calls the underlying HAL API functions. See the [porting guide](../porting/serial-port.html) for target serial support. |
| 21 | + |
| 22 | +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. |
| 23 | + |
| 24 | +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. |
| 25 | +Using intermediary buffers allows it to be used reliably for input from non-interrupt context whilst avoiding excess spinning waiting for transmission buffer space. |
| 26 | + |
| 27 | +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`. |
| 28 | + |
| 29 | +## Configuration |
| 30 | + |
| 31 | +The following parameters can be configured at object instantiation: |
| 32 | + |
| 33 | + - _TX_ |
| 34 | + - _RX_ |
| 35 | + - _Baud rate_ |
| 36 | + |
| 37 | +The default baud rate value is configured in `mbed-os/platform/mbed_lib.json`. |
| 38 | + |
| 39 | +The following parameters can be configured after an `BufferedSerial` object instantiation. |
| 40 | + |
| 41 | + - _Baud rate_ |
| 42 | + - _Data frame length_ |
| 43 | + - _Parity bit_ |
| 44 | + - _Stop bits_ |
| 45 | + |
| 46 | +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). |
| 47 | + |
| 48 | +Additionally, hardware flow control can also be configured if necessary. |
| 49 | + |
| 50 | +You can view more information about the configurable settings and functions in the class reference. |
| 51 | + |
| 52 | +## Class reference |
| 53 | + |
| 54 | +[](https://os.mbed.com/docs/mbed-os/development/mbed-os-api-doxy/classmbed_1_1_buffered_serial.html) |
| 55 | + |
| 56 | +<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> |
| 57 | + |
| 58 | +## Example |
| 59 | + |
| 60 | +[](https://os.mbed.com/teams/mbed_example/code/BufferedSerial/file/112a40a5991a/main.cpp) |
| 61 | + |
| 62 | + |
| 63 | +## Printing to the console |
| 64 | + |
| 65 | +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. |
| 66 | + |
| 67 | +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. |
| 68 | + |
| 69 | + |
| 70 | +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: |
| 71 | + |
| 72 | +[](https://os.mbed.com/teams/mbed_example/code/BufferedSerial_printf/file/112a40a5991a/main.cpp) |
| 73 | + |
| 74 | + |
| 75 | +Using the standard C I/O library directly is the recommended way to print to the console in Mbed OS. |
0 commit comments