You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/design-documents/drivers/serial/serial.md
+52-34Lines changed: 52 additions & 34 deletions
Original file line number
Diff line number
Diff line change
@@ -30,7 +30,7 @@
30
30
31
31
### Overview and background
32
32
33
-
Mbed OS contains multiple serial classes and multiple ways of printing information to the console. Serial classes have evolved over the years to add new functinality to existing classes and compensate for limitations. The result is that there are now multiple variants, not always well documented; therefore it is not clear which class shoud be used for what purpose. Futhermore, some of the classes pull substantial dependencies whose benefits are not very clear, for example standard library like objects and abstractions, and parts of the retarget layer.
33
+
Mbed OS contains multiple serial classes and multiple ways of printing information to the console. Serial classes have evolved over the years to add new functionality to existing classes and compensate for limitations. The result is that there are now multiple variants, not always well documented; therefore it is not clear which class should be used for what purpose. Furthermore, some of the classes pull substantial dependencies whose benefits are not very clear, for example standard library like objects and abstractions, and parts of the retarget layer.
34
34
35
35
This document collects proposals made by various contributors (see [references](#references)) and presents a simplified serial class hierarchy. The aim is to offer two public classes, one for unbuffered I/O and the other for buffered I/O, with a clear definition of which one should be used when. All other serial classes are deprecated and should be removed at a later stage. In addition, the serial classes dependencies will be optimised to only include components that provide valuable functionality.
36
36
@@ -45,16 +45,17 @@ Its main limitations are:
45
45
- It wastes a lot of CPU time spinning waiting for serial output and it cannot be used reliably for input because it needs interrupt-speed response on read calls but its high-level API cannot be used from interrupts.
46
46
- It lacks buffering so it cannot be used reliably for input and output without flow control.
47
47
- It pulls in the C library stdio system because it uses `Stream`.
48
+
- Although it is a `FileHandle`, its implementation is blocking only and has incorrect read semantics.
48
49
49
50
**UARTSerial**
50
51
51
52
`UARTSerial` provides buffered I/O. It can be used reliably for input from non-interrupt context and to avoid excess spinning waiting for transmission buffer space. It also does better blocking and correct POSIX-like `FileHandle` semantics.
52
53
53
-
It does not use `Stream` so it has the advantage of not pulling in the C stdio library. The drawback is that it loses the ability to call printf directly.
54
+
It does not use `Stream` so it has the advantage of not pulling in the C stdio library. The drawback is that it doesn't include built-in printf methods, see [Usage scenarios and examples](#usage-scenarios-and-examples) for an example on how to print to a serial port.
54
55
55
56
**RawSerial**
56
57
57
-
`RawSerial` provides unbuffered I/O. It has no locks so it is safe to use in interrupts provided that it is the only instance that is using that serial port.
58
+
`RawSerial` provides unbuffered I/O. It has no locks so it is safe to use in interrupts provided that it is the only instance that is using that serial port. Unlike `Serial` and `UARTSerial`, it is not a `FileHandle`.
58
59
59
60
### Requirements and assumptions
60
61
@@ -84,23 +85,50 @@ The below diagram shows the detailed inheritance hierarchy for `BufferedSerial`.
class BufferedSerial : private SerialBase, public FileHandle, private NonCopyable<BufferedSerial>
90
+
```
91
+
92
+
`BufferedSerial` privately inherits from `SerialBase`. It means that applications are forced to use the transmit and receive buffers since they cannot access `SerialBase` member functions directly.
93
+
87
94
**API description**
88
95
89
-
This is the new constructor for `BufferedSerial` to pass in the serial buffers size.
96
+
These are the new constructors for `BufferedSerial`. The first constructor creates the buffers dynamically.
90
97
91
98
```C
92
99
/** Create a BufferedSerial port, connected to the specified transmit and receive pins, with a particular baud rate.
93
100
* @param tx Transmit pin
94
101
* @param rx Receive pin
95
102
* @param baud The baud rate of the serial port (optional, defaults to MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE)
96
-
* @param rx_buffer_size the size of the receive buffer (optional, defaults to MBED_CONF_DRIVERS_UART_SERIAL_RXBUF_SIZE)
97
-
* @param tx_buffer_size the size of the transmit buffer (optional, defaults to MBED_CONF_DRIVERS_UART_SERIAL_TXBUF_SIZE)
103
+
* @param rx_buffer_size The size of the receive buffer (optional, defaults to MBED_CONF_DRIVERS_UART_SERIAL_RXBUF_SIZE)
104
+
* @param tx_buffer_size The size of the transmit buffer (optional, defaults to MBED_CONF_DRIVERS_UART_SERIAL_TXBUF_SIZE)
98
105
*/
99
106
BufferedSerial(PinName tx,
100
107
PinName rx,
101
108
int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE,
The second constructor takes buffer pointers as arguments.
114
+
115
+
```C
116
+
/** Create a BufferedSerial port, connected to the specified transmit and receive pins, with a particular baud rate.
117
+
* @param tx Transmit pin
118
+
* @param rx Receive pin
119
+
* @param baud The baud rate of the serial port (optional, defaults to MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE)
120
+
* @param rx_buffer The receive buffer
121
+
* @param rx_buffer_size The size of the receive buffer
122
+
* @param tx_buffer The transmit buffer
123
+
* @param tx_buffer_size The size of the transmit buffer
124
+
*/
125
+
BufferedSerial(PinName tx,
126
+
PinName rx,
127
+
int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE,
128
+
void *rx_buffer,
129
+
size_t rx_buffer_size,
130
+
void *tx_buffer,
131
+
size_t tx_buffer_size);
104
132
```
105
133
106
134
The rest of the class API is that of `UARTSerial` and is described [here](https://os.mbed.com/docs/mbed-os/v5.14/mbed-os-api-doxy/classmbed_1_1_u_a_r_t_serial.html).
@@ -133,28 +161,6 @@ public:
133
161
*/
134
162
UnbufferedSerial(PinName tx, PinName rx, int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE);
135
163
136
-
/** Write a char to the serial port
137
-
*
138
-
* @param c The char to write
139
-
*
140
-
* @returns The written char or -1 if an error occurred
virtual off_t seek(off_t offset, int whence = SEEK_SET);
@@ -179,7 +185,7 @@ protected:
179
185
180
186
### Detailed design : MinimalSerial
181
187
182
-
The `MinimalSerial` class is a minimal abstract base class that provides basics I/O functionality. All other serial classes derive from it.
188
+
The `MinimalSerial` class is a minimal abstract base class that provides basics I/O functionality. It is not part of the public interface. All other serial classes derive from it.
183
189
184
190
**API description**
185
191
@@ -217,7 +223,19 @@ The constructor provides arguments to create a serial port connected to the spec
217
223
218
224
### Scenario 1 `Printing to stdout`
219
225
220
-
The below example shows pseudocode for how to print `Hello` to stdout using the new API. Printf cannot be called directly; instead the application first calls _fdopen_ to get a _FILE*_ to use the printf function.
226
+
There is no change in the current APi in regard to printing to stdout. Applications should call `printf` directly as shown below. The console baud rate can be configured via `platform.stdio-baud-rate` in `mbed_app.json`.
227
+
228
+
```C
229
+
printf("Hello");
230
+
```
231
+
or
232
+
```C
233
+
fprintf(stdout, "Hello");
234
+
```
235
+
236
+
### Scenario 2 `Printing to a serial port other than stdout`
237
+
238
+
The below example shows pseudocode for how to print `Hello` to `serial` port using the new API. `serial.printf` cannot be called directly; instead the application first calls _fdopen_ to get a _FILE*_ to use the printf function.
221
239
222
240
```C
223
241
BufferedSerial serial(RX, TX, 115200);
@@ -243,7 +261,7 @@ The relationship of `FileHandle` and other APIs is as follows:
243
261
244
262

245
263
246
-
The POSIX layerand the static array of `FileHandle*` allocated by the retarget code for file handling consume a lot of memory. A constrained target that only uses a console does not need file handling and should be able to turn this functionality off.
264
+
The POSIX layer, the memory cost associated with the vtable and virtual functions of the `FileHandle` class, and the static array of `FileHandle*` allocated by the retarget code for file handling has a significant impact on memory. A constrained target that only uses a console does not need file handling and should be able to turn this functionality off.
247
265
248
266
A new configuration parameter is introduced to compile the file handling code out. The application can override the default configuration in mbed_app.json as follows:
249
267
@@ -290,7 +308,7 @@ Phase 1:
290
308
- [Deprecation of APIs](#Deprecations)
291
309
- Implementation of new class `UnbufferedSerial`
292
310
- Implementation of new class `BufferedSerial`
293
-
- Use `UnbufferedSerial` instead of `DirectSerial` in retarget code
311
+
- Use `UnbufferedSerial` instead of `DirectSerial` in retarget code (as `DirectSerial` is not a public API it can be directly removed)
0 commit comments