Mistake on this page?
Report an issue in GitHub or email us
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 
32 namespace 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> {
51 public:
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  *
60  * @note
61  * Either tx or rx may be specified as NC if unused
62  */
64  PinName tx,
65  PinName rx,
66  int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE
67  );
68 
69  /** Create a UnbufferedSerial port, connected to the specified transmit and
70  * receive pins, with a particular baud rate.
71  * @param static_pinmap reference to structure which holds static pinmap
72  * @param baud The baud rate of the serial port (optional, defaults to
73  * MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE)
74  */
76  const serial_pinmap_t &static_pinmap,
77  int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE
78  );
79 
80  /** Write the contents of a buffer to a file
81  *
82  * Blocks until all data is written
83  *
84  * @param buffer The buffer to write from
85  * @param size The number of bytes to write
86  * @return The number of bytes written
87  */
88  ssize_t write(const void *buffer, size_t size) override;
89 
90  /** Read the contents of a file into a buffer
91  *
92  * Blocks and reads exactly one character
93  *
94  * @param buffer The buffer to read in to
95  * @param size The number of bytes to read
96  * @return The number of bytes read
97  */
98  ssize_t read(void *buffer, size_t size) override;
99 
100  /** Move the file position to a given offset from from a given location
101  *
102  * Not valid for a device type FileHandle like UnbufferedSerial.
103  * In case of UnbufferedSerial, returns ESPIPE
104  *
105  * @param offset The offset from whence to move to
106  * @param whence The start of where to seek
107  * SEEK_SET to start from beginning of file,
108  * SEEK_CUR to start from current position in file,
109  * SEEK_END to start from end of file
110  * @return The new offset of the file, negative error code on failure
111  */
112  off_t seek(off_t offset, int whence = SEEK_SET) override
113  {
114  return -ESPIPE;
115  }
116 
117  /** Get the size of the file
118  *
119  * @return Size of the file in bytes
120  */
121  off_t size() override
122  {
123  return -EINVAL;
124  }
125 
126  /** Check if the file in an interactive terminal device
127  *
128  * @return True if the file is a terminal
129  * @return False if the file is not a terminal
130  * @return Negative error code on failure
131  */
132  int isatty() override
133  {
134  return true;
135  }
136 
137  /** Close a file
138  *
139  * @return 0 on success, negative error code on failure
140  */
141  int close() override
142  {
143  return 0;
144  }
145 
146  /** Enable or disable input
147  *
148  * Control enabling of device for input. This is primarily intended
149  * for temporary power-saving; the overall ability of the device to operate
150  * for input and/or output may be fixed at creation time, but this call can
151  * allow input to be temporarily disabled to permit power saving without
152  * losing device state.
153  *
154  * @param enabled true to enable input, false to disable.
155  *
156  * @return 0 on success
157  * @return Negative error code on failure
158  */
159  int enable_input(bool enabled) override;
160 
161  /** Enable or disable output
162  *
163  * Control enabling of device for output. This is primarily intended
164  * for temporary power-saving; the overall ability of the device to operate
165  * for input and/or output may be fixed at creation time, but this call can
166  * allow output to be temporarily disabled to permit power saving without
167  * losing device state.
168  *
169  * @param enabled true to enable output, false to disable.
170  *
171  * @return 0 on success
172  * @return Negative error code on failure
173  */
174  int enable_output(bool enabled) override;
175 
176  /** Check for poll event flags
177  * Check the events listed in events to see if data can be read or written
178  * without blocking.
179  * Call is nonblocking - returns state of events.
180  *
181  * @param events bitmask of poll events we're interested in - POLLIN/POLLOUT etc.
182  *
183  * @returns bitmask of poll events that have occurred.
184  */
185  short poll(short events) const override;
186 
187  using SerialBase::attach;
188  using SerialBase::baud;
189  using SerialBase::format;
190  using SerialBase::readable;
191  using SerialBase::writeable;
192  using SerialBase::IrqCnt;
193  using SerialBase::RxIrq;
194  using SerialBase::TxIrq;
195 
196 #if DEVICE_SERIAL_FC
197  // For now use the base enum - but in future we may have extra options
198  // such as XON/XOFF or manual GPIO RTSCTS.
199  using SerialBase::Flow;
200  // In C++11, we wouldn't need to also have using directives for each value
201  using SerialBase::Disabled;
202  using SerialBase::RTS;
203  using SerialBase::CTS;
204  using SerialBase::RTSCTS;
205 
206  /** Set the flow control type on the serial port
207  *
208  * @param type the flow control type (Disabled, RTS, CTS, RTSCTS)
209  * @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS)
210  * @param flow2 the second flow control pin (CTS for RTSCTS)
211  */
212  void set_flow_control(Flow type, PinName flow1 = NC, PinName flow2 = NC);
213 #endif // DEVICE_SERIAL_FC
214 };
215 
216 } // namespace mbed
217 
218 #endif // DEVICE_SERIAL || defined(DOXYGEN_ONLY)
219 
220 #endif // MBED_UNBUFFERED_SERIAL_H
int enable_output(bool enabled) override
Enable or disable output.
int enable_input(bool enabled) override
Enable or disable input.
ssize_t write(const void *buffer, size_t size) override
Write the contents of a buffer to a file.
off_t size() override
Get the size of the file.
void baud(int baudrate)
Set the baud rate of the serial port.
Class FileHandle.
Definition: FileHandle.h:46
int close() override
Close a file.
Class implementation for unbuffered I/O for an interrupt driven application or one that needs to have...
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, with the specified baud rate.
Prevents generation of copy constructor and copy assignment operator in derived classes.
Definition: NonCopyable.h:162
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 writeable()
Determine if there is space available to write a character.
A base class for serial port implementations Can&#39;t be instantiated directly (use UnbufferedSerial or ...
Definition: SerialBase.h:46
int isatty() override
Check if the file in an interactive terminal device.
int readable()
Determine if there is a character available to read.
ssize_t read(void *buffer, size_t size) override
Read the contents of a file into a buffer.
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.
Definition: ATHandler.h:46
void set_flow_control(Flow type, PinName flow1=NC, PinName flow2=NC)
Set the flow control type on the serial port.
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...
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.