Mbed OS Reference
Loading...
Searching...
No Matches
BufferedSerial.h
1/* mbed Microcontroller Library
2 * Copyright (c) 2019 ARM Limited
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef MBED_BUFFEREDSERIAL_H
19#define MBED_BUFFEREDSERIAL_H
20
21#include "platform/platform.h"
22
23#if (DEVICE_SERIAL && DEVICE_INTERRUPTIN) || defined(DOXYGEN_ONLY)
24
25#include "platform/FileHandle.h"
26#include "drivers/SerialBase.h"
27#include "drivers/InterruptIn.h"
28#include "rtos/Mutex.h"
29#include "platform/CircularBuffer.h"
30#include "platform/NonCopyable.h"
31
32#ifndef MBED_CONF_DRIVERS_UART_SERIAL_RXBUF_SIZE
33#define MBED_CONF_DRIVERS_UART_SERIAL_RXBUF_SIZE 256
34#endif
35
36#ifndef MBED_CONF_DRIVERS_UART_SERIAL_TXBUF_SIZE
37#define MBED_CONF_DRIVERS_UART_SERIAL_TXBUF_SIZE 256
38#endif
39
40namespace mbed {
41/**
42 * \defgroup drivers_BufferedSerial BufferedSerial class
43 * \ingroup drivers-public-api-uart
44 * @{
45 */
46
47/** Class providing buffered UART communication functionality using separate
48 * circular buffer for send and receive channels
49 *
50 */
51
53 private SerialBase,
54 public FileHandle,
55 private NonCopyable<BufferedSerial> {
56
57public:
58
59 /** Create a BufferedSerial port, connected to the specified transmit and
60 * receive pins, with a particular baud rate.
61 * @param tx Transmit pin
62 * @param rx Receive pin
63 * @param baud The baud rate of the serial port (optional, defaults to
64 * MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE, which is set via
65 * platform.default-serial-baud-rate in mbed_app.json)
66 */
68 PinName tx,
69 PinName rx,
70 int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE
71 );
72
73 /** Create a BufferedSerial port, connected to the specified transmit and
74 * receive pins, with a particular baud rate.
75 * @param static_pinmap reference to structure which holds static pinmap
76 * @param baud The baud rate of the serial port (optional, defaults to
77 * MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE, which is set via
78 * platform.default-serial-baud-rate in mbed_app.json)
79 */
81 const serial_pinmap_t &static_pinmap,
82 int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE
83 );
84
85 ~BufferedSerial() override;
86
87 /** Equivalent to POSIX poll(). Derived from FileHandle.
88 * Provides a mechanism to multiplex input/output over a set of file
89 * handles.
90 * The events that can be reported are POLLIN, POLLOUT, POLLHUP.
91 */
92 short poll(short events) const final;
93
94 /* Resolve ambiguities versus our private SerialBase
95 * (for writable, spelling differs, but just in case)
96 */
99
100 /** Write the contents of a buffer to a file
101 *
102 * Follows POSIX semantics:
103 *
104 * * if blocking, block until all data is written
105 * * if no data can be written, and non-blocking set, return -EAGAIN
106 * * if some data can be written, and non-blocking set, write partial
107 *
108 * @param buffer The buffer to write from
109 * @param length The number of bytes to write
110 * @return The number of bytes written, negative error on failure
111 */
112 ssize_t write(const void *buffer, size_t length) override;
113
114 /** Read the contents of a file into a buffer
115 *
116 * Follows POSIX semantics:
117 *
118 * * if no data is available, and non-blocking set return -EAGAIN
119 * * if no data is available, and blocking set, wait until data is
120 * available
121 * * If any data is available, call returns immediately
122 *
123 * @param buffer The buffer to read in to
124 * @param length The number of bytes to read
125 * @return The number of bytes read, 0 at end of file, negative
126 * error on failure
127 */
128 ssize_t read(void *buffer, size_t length) override;
129
130 /** Close a file
131 *
132 * @return 0 on success, negative error code on failure
133 */
134 int close() override;
135
136 /** Check if the file in an interactive terminal device
137 *
138 * @return True if the file is a terminal
139 * @return False if the file is not a terminal
140 * @return Negative error code on failure
141 */
142 int isatty() override;
143
144 /** Move the file position to a given offset from from a given location
145 *
146 * Not valid for a device type FileHandle like BufferedSerial.
147 * In case of BufferedSerial, returns ESPIPE
148 *
149 * @param offset The offset from whence to move to
150 * @param whence The start of where to seek
151 * SEEK_SET to start from beginning of file,
152 * SEEK_CUR to start from current position in file,
153 * SEEK_END to start from end of file
154 * @return The new offset of the file, negative error code on
155 * failure
156 */
157 off_t seek(off_t offset, int whence) override;
158
159 /** Flush any buffers associated with the file
160 *
161 * @return 0 on success, negative error code on failure
162 */
163 int sync() override;
164
165 /** Set blocking or non-blocking mode
166 * The default is blocking.
167 *
168 * @param blocking true for blocking mode, false for non-blocking mode.
169 */
170 int set_blocking(bool blocking) override
171 {
172 _blocking = blocking;
173 return 0;
174 }
175
176 /** Check current blocking or non-blocking mode for file operations.
177 *
178 * @return true for blocking mode, false for non-blocking mode.
179 */
180 bool is_blocking() const override
181 {
182 return _blocking;
183 }
184
185 /** Enable or disable input
186 *
187 * Control enabling of device for input. This is primarily intended
188 * for temporary power-saving; the overall ability of the device to operate
189 * for input and/or output may be fixed at creation time, but this call can
190 * allow input to be temporarily disabled to permit power saving without
191 * losing device state.
192 *
193 * @param enabled true to enable input, false to disable.
194 *
195 * @return 0 on success
196 * @return Negative error code on failure
197 */
198 int enable_input(bool enabled) override;
199
200 /** Enable or disable output
201 *
202 * Control enabling of device for output. This is primarily intended
203 * for temporary power-saving; the overall ability of the device to operate
204 * for input and/or output may be fixed at creation time, but this call can
205 * allow output to be temporarily disabled to permit power saving without
206 * losing device state.
207 *
208 * @param enabled true to enable output, false to disable.
209 *
210 * @return 0 on success
211 * @return Negative error code on failure
212 */
213 int enable_output(bool enabled) override;
214
215 /** Register a callback on state change of the file.
216 *
217 * The specified callback will be called on state changes such as when
218 * the file can be written to or read from.
219 *
220 * The callback may be called in an interrupt context and should not
221 * perform expensive operations.
222 *
223 * Note! This is not intended as an attach-like asynchronous api, but
224 * rather as a building block for constructing such functionality.
225 *
226 * The exact timing of when the registered function
227 * is called is not guaranteed and susceptible to change. It should be
228 * used as a cue to make read/write/poll calls to find the current state.
229 *
230 * @param func Function to call on state change
231 */
232 void sigio(Callback<void()> func) override;
233
234 /** Setup interrupt handler for DCD line
235 *
236 * If DCD line is connected, an IRQ handler will be setup.
237 * Does nothing if DCD is NC, i.e., not connected.
238 *
239 * @param dcd_pin Pin-name for DCD
240 * @param active_high a boolean set to true if DCD polarity is active
241 * low
242 */
243 void set_data_carrier_detect(PinName dcd_pin, bool active_high = false);
244
245 /** Set the baud rate
246 *
247 * @param baud The baud rate
248 */
249 void set_baud(int baud);
250
251 // Expose private SerialBase::Parity as BufferedSerial::Parity
252 using SerialBase::Parity;
253 using SerialBase::None;
254 using SerialBase::Odd;
255 using SerialBase::Even;
256 using SerialBase::Forced1;
257 using SerialBase::Forced0;
258
259 /** Set the transmission format used by the serial port
260 *
261 * @param bits The number of bits in a word (5-8; default = 8)
262 * @param parity The parity used (None, Odd, Even, Forced1, Forced0;
263 * default = None)
264 * @param stop_bits The number of stop bits (1 or 2; default = 1)
265 */
267 int bits = 8, Parity parity = BufferedSerial::None, int stop_bits = 1
268 );
269
270#if DEVICE_SERIAL_FC
271 // For now use the base enum - but in future we may have extra options
272 // such as XON/XOFF or manual GPIO RTSCTS.
273 using SerialBase::Flow;
274 using SerialBase::Disabled;
275 using SerialBase::RTS;
276 using SerialBase::CTS;
277 using SerialBase::RTSCTS;
278
279 /** Set the flow control type on the serial port
280 *
281 * @param type the flow control type (Disabled, RTS, CTS, RTSCTS)
282 * @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for
283 * CTS)
284 * @param flow2 the second flow control pin (CTS for RTSCTS)
285 */
286 void set_flow_control(Flow type, PinName flow1 = NC, PinName flow2 = NC);
287#endif
288
289private:
290
291 /** Acquire mutex
292 */
293 void api_lock(void);
294
295 /** Release mutex
296 */
297 void api_unlock(void);
298
299 /** Unbuffered write - invoked when write called from critical section
300 * @param buf_ptr The buffer to write from
301 * @param length The number of bytes to write
302 * @return The number of bytes written, negative error on failure
303 */
304 ssize_t write_unbuffered(const char *buf_ptr, size_t length);
305
306 /** Enable processing of byte reception IRQs and register a callback to
307 * process them if the IRQs are not yet enabled and reception is enabled.
308 */
309 void update_rx_irq();
310
311 /** Disable processing of byte reception IRQs and de-register callback to
312 * process them.
313 */
314 void disable_rx_irq();
315
316 /** Enable processing of byte transmission IRQs and register a callback to
317 * process them if the IRQs are not yet enabled and transmission is enabled.
318 */
319 void update_tx_irq();
320
321 /** Disable processing of byte transmission IRQs and de-register callback to
322 * process them.
323 */
324 void disable_tx_irq();
325
326 /** Software serial buffers
327 * By default buffer size is 256 for TX and 256 for RX. Configurable
328 * through mbed_app.json
329 */
332
333 rtos::Mutex _mutex;
334
335 Callback<void()> _sigio_cb;
336
337 bool _blocking = true;
338 bool _tx_irq_enabled = false;
339 bool _rx_irq_enabled = false;
340 InterruptIn *_dcd_irq = nullptr;
341
342 /** Device Hanged up
343 * Determines if the device hanged up on us.
344 *
345 * @return True, if hanged up
346 */
347 bool hup() const;
348
349 /** ISRs for serial
350 * Routines to handle interrupts on serial pins.
351 * Copies data into Circular Buffer.
352 * Reports the state change to File handle.
353 */
354 void tx_irq(void);
355 void rx_irq(void);
356
357 /** Execute a callback previously registered for state change of the file.
358 */
359 void wake(void);
360
361 /** Wake on data carrier detected.
362 */
363 void dcd_irq(void);
364};
365
366/** @}*/
367
368} //namespace mbed
369
370#endif //(DEVICE_SERIAL && DEVICE_INTERRUPTIN) || defined(DOXYGEN_ONLY)
371#endif //MBED_BUFFEREDSERIAL_H
Class providing buffered UART communication functionality using separate circular buffer for send and...
short poll(short events) const final
Equivalent to POSIX poll().
int enable_input(bool enabled) override
Enable or disable input.
off_t seek(off_t offset, int whence) override
Move the file position to a given offset from from a given location.
BufferedSerial(const serial_pinmap_t &static_pinmap, int baud=MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE)
Create a BufferedSerial port, connected to the specified transmit and receive pins,...
ssize_t write(const void *buffer, size_t length) override
Write the contents of a buffer to a file.
int sync() override
Flush any buffers associated with the file.
bool is_blocking() const override
Check current blocking or non-blocking mode for file operations.
void set_flow_control(Flow type, PinName flow1=NC, PinName flow2=NC)
Set the flow control type on the serial port.
void sigio(Callback< void()> func) override
Register a callback on state change of the file.
BufferedSerial(PinName tx, PinName rx, int baud=MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE)
Create a BufferedSerial port, connected to the specified transmit and receive pins,...
void set_format(int bits=8, Parity parity=BufferedSerial::None, int stop_bits=1)
Set the transmission format used by the serial port.
void set_data_carrier_detect(PinName dcd_pin, bool active_high=false)
Setup interrupt handler for DCD line.
int enable_output(bool enabled) override
Enable or disable output.
int close() override
Close a file.
void set_baud(int baud)
Set the baud rate.
ssize_t read(void *buffer, size_t length) override
Read the contents of a file into a buffer.
int set_blocking(bool blocking) override
Set blocking or non-blocking mode The default is blocking.
int isatty() override
Check if the file in an interactive terminal device.
Callback class based on template specialization.
Definition: Callback.h:53
Templated Circular buffer class.
Class FileHandle.
Definition: FileHandle.h:46
bool writable() const
Definition depends on the subclass implementing FileHandle.
Definition: FileHandle.h:237
bool readable() const
Definition depends on the subclass implementing FileHandle.
Definition: FileHandle.h:248
A digital interrupt input, used to call a function on a rising or falling edge.
Definition: InterruptIn.h:65
Prevents generation of copy constructor and copy assignment operator in derived classes.
Definition: NonCopyable.h:162
A base class for serial port implementations Can't be instantiated directly (use UnbufferedSerial or ...
Definition: SerialBase.h:46
void baud(int baudrate)
Set the baud rate of the serial port.
The Mutex class is used to synchronize the execution of threads.
Definition: Mutex.h:70