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 
147  /** Check for poll event flags
148  * Check the events listed in events to see if data can be read or written
149  * without blocking.
150  * Call is nonblocking - returns state of events.
151  *
152  * @param events bitmask of poll events we're interested in - POLLIN/POLLOUT etc.
153  *
154  * @returns bitmask of poll events that have occurred.
155  */
156  short poll(short events) const override;
157 
158  using SerialBase::readable;
159  using SerialBase::writeable;
160  using SerialBase::format;
161  using SerialBase::attach;
162  using SerialBase::baud;
163  using SerialBase::RxIrq;
164  using SerialBase::TxIrq;
165  using SerialBase::IrqCnt;
166 
167 #if DEVICE_SERIAL_FC
168  // For now use the base enum - but in future we may have extra options
169  // such as XON/XOFF or manual GPIO RTSCTS.
170  using SerialBase::Flow;
171  // In C++11, we wouldn't need to also have using directives for each value
172  using SerialBase::Disabled;
173  using SerialBase::RTS;
174  using SerialBase::CTS;
175  using SerialBase::RTSCTS;
176 
177  /** Set the flow control type on the serial port
178  *
179  * @param type the flow control type (Disabled, RTS, CTS, RTSCTS)
180  * @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS)
181  * @param flow2 the second flow control pin (CTS for RTSCTS)
182  */
183  void set_flow_control(Flow type, PinName flow1 = NC, PinName flow2 = NC);
184 #endif // DEVICE_SERIAL_FC
185 };
186 
187 } // namespace mbed
188 
189 #endif // DEVICE_SERIAL || defined(DOXYGEN_ONLY)
190 
191 #endif // MBED_UNBUFFERED_SERIAL_H
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.