Mbed OS Reference
Loading...
Searching...
No Matches
UnbufferedSerial.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#ifndef MBED_UNBUFFERED_SERIAL_H
18#define MBED_UNBUFFERED_SERIAL_H
19
20#include "platform/platform.h"
21
22#if DEVICE_SERIAL || defined(DOXYGEN_ONLY)
23
24#include <cstdarg>
25
26#include "drivers/SerialBase.h"
27#include "platform/FileHandle.h"
28#include "platform/mbed_toolchain.h"
29#include "platform/NonCopyable.h"
30
31
32namespace mbed {
33/** \defgroup drivers-public-api-uart UART
34 * \ingroup drivers-public-api
35 */
36
37/**
38 * \defgroup drivers_UnbufferedSerial UnbufferedSerial class
39 * \ingroup drivers-public-api-uart
40 * @{
41 */
42
43/**
44 * Class implementation for unbuffered I/O for an interrupt driven application
45 * or one that needs to have more control.
46 */
48 private SerialBase,
49 public FileHandle,
50 private NonCopyable<UnbufferedSerial> {
51public:
52 /**
53 * Create a serial port instance connected to the specified transmit and
54 * receive pins, with the specified baud rate.
55 *
56 * @param tx Transmit pin
57 * @param rx Receive pin
58 * @param baud The baud rate of the serial port (optional, defaults to MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE,
59 * which is set via platform.default-serial-baud-rate in mbed_app.json)
60 *
61 * @note
62 * Either tx or rx may be specified as NC if unused
63 */
65 PinName tx,
66 PinName rx,
67 int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE
68 );
69
70 /** Create a UnbufferedSerial port, connected to the specified transmit and
71 * receive pins, with a particular baud rate.
72 * @param static_pinmap reference to structure which holds static pinmap
73 * @param baud The baud rate of the serial port (optional, defaults to
74 * MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE, which is set via
75 * platform.default-serial-baud-rate in mbed_app.json)
76 */
78 const serial_pinmap_t &static_pinmap,
79 int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE
80 );
81
82 /** Write the contents of a buffer to a file
83 *
84 * Blocks until all data is written
85 *
86 * @param buffer The buffer to write from
87 * @param size The number of bytes to write
88 * @return The number of bytes written
89 */
90 ssize_t write(const void *buffer, size_t size) override;
91
92 /** Read the contents of a file into a buffer
93 *
94 * Blocks and reads exactly one character
95 *
96 * @param buffer The buffer to read in to
97 * @param size The number of bytes to read
98 * @return The number of bytes read
99 */
100 ssize_t read(void *buffer, size_t size) override;
101
102 /** Move the file position to a given offset from from a given location
103 *
104 * Not valid for a device type FileHandle like UnbufferedSerial.
105 * In case of UnbufferedSerial, returns ESPIPE
106 *
107 * @param offset The offset from whence to move to
108 * @param whence The start of where to seek
109 * SEEK_SET to start from beginning of file,
110 * SEEK_CUR to start from current position in file,
111 * SEEK_END to start from end of file
112 * @return The new offset of the file, negative error code on failure
113 */
114 off_t seek(off_t offset, int whence = SEEK_SET) override
115 {
116 return -ESPIPE;
117 }
118
119 /** Get the size of the file
120 *
121 * @return Size of the file in bytes
122 */
123 off_t size() override
124 {
125 return -EINVAL;
126 }
127
128 /** Check if the file in an interactive terminal device
129 *
130 * @return True if the file is a terminal
131 * @return False if the file is not a terminal
132 * @return Negative error code on failure
133 */
134 int isatty() override
135 {
136 return true;
137 }
138
139 /** Close a file
140 *
141 * @return 0 on success, negative error code on failure
142 */
143 int close() override
144 {
145 return 0;
146 }
147
148 /** Enable or disable input
149 *
150 * Control enabling of device for input. This is primarily intended
151 * for temporary power-saving; the overall ability of the device to operate
152 * for input and/or output may be fixed at creation time, but this call can
153 * allow input to be temporarily disabled to permit power saving without
154 * losing device state.
155 *
156 * @param enabled true to enable input, false to disable.
157 *
158 * @return 0 on success
159 * @return Negative error code on failure
160 */
161 int enable_input(bool enabled) override;
162
163 /** Enable or disable output
164 *
165 * Control enabling of device for output. This is primarily intended
166 * for temporary power-saving; the overall ability of the device to operate
167 * for input and/or output may be fixed at creation time, but this call can
168 * allow output to be temporarily disabled to permit power saving without
169 * losing device state.
170 *
171 * @param enabled true to enable output, false to disable.
172 *
173 * @return 0 on success
174 * @return Negative error code on failure
175 */
176 int enable_output(bool enabled) override;
177
178 /** Check for poll event flags
179 * Check the events listed in events to see if data can be read or written
180 * without blocking.
181 * Call is nonblocking - returns state of events.
182 *
183 * @param events bitmask of poll events we're interested in - POLLIN/POLLOUT etc.
184 *
185 * @returns bitmask of poll events that have occurred.
186 */
187 short poll(short events) const override;
188
189 using SerialBase::attach;
190 using SerialBase::baud;
191 using SerialBase::format;
194 using SerialBase::IrqCnt;
195 using SerialBase::RxIrq;
196 using SerialBase::TxIrq;
197
198#if DEVICE_SERIAL_FC
199 // For now use the base enum - but in future we may have extra options
200 // such as XON/XOFF or manual GPIO RTSCTS.
201 using SerialBase::Flow;
202 // In C++11, we wouldn't need to also have using directives for each value
203 using SerialBase::Disabled;
204 using SerialBase::RTS;
205 using SerialBase::CTS;
206 using SerialBase::RTSCTS;
207
208 /** Set the flow control type on the serial port
209 *
210 * @param type the flow control type (Disabled, RTS, CTS, RTSCTS)
211 * @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS)
212 * @param flow2 the second flow control pin (CTS for RTSCTS)
213 */
214 void set_flow_control(Flow type, PinName flow1 = NC, PinName flow2 = NC);
215#endif // DEVICE_SERIAL_FC
216};
217
218} // namespace mbed
219
220#endif // DEVICE_SERIAL || defined(DOXYGEN_ONLY)
221
222#endif // MBED_UNBUFFERED_SERIAL_H
Class FileHandle.
Definition: FileHandle.h:46
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
int readable()
Determine if there is a character available to read.
void attach(Callback< void()> func, IrqType type=RxIrq)
Attach a function to call whenever a serial interrupt is generated.
void format(int bits=8, Parity parity=SerialBase::None, int stop_bits=1)
Set the transmission format used by the serial port.
void baud(int baudrate)
Set the baud rate of the serial port.
int writeable()
Determine if there is space available to write a character.
Class implementation for unbuffered I/O for an interrupt driven application or one that needs to have...
void baud(int baudrate)
Set the baud rate of the serial port.
int enable_input(bool enabled) override
Enable or disable input.
UnbufferedSerial(PinName tx, PinName rx, int baud=MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE)
Create a serial port instance connected to the specified transmit and receive pins,...
off_t size() override
Get the size of the file.
void set_flow_control(Flow type, PinName flow1=NC, PinName flow2=NC)
Set the flow control type on the serial port.
ssize_t write(const void *buffer, size_t size) override
Write the contents of a buffer to a file.
off_t seek(off_t offset, int whence=SEEK_SET) override
Move the file position to a given offset from from a given location.
int enable_output(bool enabled) override
Enable or disable output.
int close() override
Close a file.
ssize_t read(void *buffer, size_t size) override
Read the contents of a file into a buffer.
UnbufferedSerial(const serial_pinmap_t &static_pinmap, int baud=MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE)
Create a UnbufferedSerial port, connected to the specified transmit and receive pins,...
int isatty() override
Check if the file in an interactive terminal device.
short poll(short events) const override
Check for poll event flags Check the events listed in events to see if data can be read or written wi...