Mistake on this page?
Report an issue in GitHub or email us
UARTSerial.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-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_UARTSERIAL_H
19 #define MBED_UARTSERIAL_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 "platform/PlatformMutex.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 
40 namespace mbed {
41 /**
42  * \defgroup drivers_UARTSerial UARTSerial class
43  * \ingroup drivers-public-api-uart
44  * @{
45  */
46 
47 /** Class providing buffered UART communication functionality using separate circular buffer for send and receive channels
48  *
49  */
50 
51 class UARTSerial : private SerialBase, public FileHandle, private NonCopyable<UARTSerial> {
52 
53 public:
54 
55  /** Create a UARTSerial port, connected to the specified transmit and receive pins, with a particular baud rate.
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  UARTSerial(PinName tx, PinName rx, int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE);
61 
62  /** Create a UARTSerial port, connected to the specified transmit and receive pins, with a particular baud rate.
63  * @param static_pinmap reference to structure which holds static pinmap
64  * @param baud The baud rate of the serial port (optional, defaults to MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE)
65  */
66  UARTSerial(const serial_pinmap_t &static_pinmap, int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE);
67 
68  virtual ~UARTSerial();
69 
70  /** Equivalent to POSIX poll(). Derived from FileHandle.
71  * Provides a mechanism to multiplex input/output over a set of file handles.
72  */
73  virtual short poll(short events) const;
74 
75  /* Resolve ambiguities versus our private SerialBase
76  * (for writable, spelling differs, but just in case)
77  */
80 
81  /** Write the contents of a buffer to a file
82  *
83  * Follows POSIX semantics:
84  *
85  * * if blocking, block until all data is written
86  * * if no data can be written, and non-blocking set, return -EAGAIN
87  * * if some data can be written, and non-blocking set, write partial
88  *
89  * @param buffer The buffer to write from
90  * @param length The number of bytes to write
91  * @return The number of bytes written, negative error on failure
92  */
93  virtual ssize_t write(const void *buffer, size_t length);
94 
95  /** Read the contents of a file into a buffer
96  *
97  * Follows POSIX semantics:
98  *
99  * * if no data is available, and non-blocking set return -EAGAIN
100  * * if no data is available, and blocking set, wait until data is available
101  * * If any data is available, call returns immediately
102  *
103  * @param buffer The buffer to read in to
104  * @param length The number of bytes to read
105  * @return The number of bytes read, 0 at end of file, negative error on failure
106  */
107  virtual ssize_t read(void *buffer, size_t length);
108 
109  /** Close a file
110  *
111  * @return 0 on success, negative error code on failure
112  */
113  virtual int close();
114 
115  /** Check if the file in an interactive terminal device
116  *
117  * @return True if the file is a terminal
118  * @return False if the file is not a terminal
119  * @return Negative error code on failure
120  */
121  virtual int isatty();
122 
123  /** Move the file position to a given offset from from a given location
124  *
125  * Not valid for a device type FileHandle like UARTSerial.
126  * In case of UARTSerial, returns ESPIPE
127  *
128  * @param offset The offset from whence to move to
129  * @param whence The start of where to seek
130  * SEEK_SET to start from beginning of file,
131  * SEEK_CUR to start from current position in file,
132  * SEEK_END to start from end of file
133  * @return The new offset of the file, negative error code on failure
134  */
135  virtual off_t seek(off_t offset, int whence);
136 
137  /** Flush any buffers associated with the file
138  *
139  * @return 0 on success, negative error code on failure
140  */
141  virtual int sync();
142 
143  /** Set blocking or non-blocking mode
144  * The default is blocking.
145  *
146  * @param blocking true for blocking mode, false for non-blocking mode.
147  */
148  virtual int set_blocking(bool blocking)
149  {
150  _blocking = blocking;
151  return 0;
152  }
153 
154  /** Check current blocking or non-blocking mode for file operations.
155  *
156  * @return true for blocking mode, false for non-blocking mode.
157  */
158  virtual bool is_blocking() const
159  {
160  return _blocking;
161  }
162 
163  /** Enable or disable input
164  *
165  * Control enabling of device for input. This is primarily intended
166  * for temporary power-saving; the overall ability of the device to operate for
167  * input and/or output may be fixed at creation time, but this call can
168  * allow input to be temporarily disabled to permit power saving without
169  * losing device state.
170  *
171  * @param enabled true to enable input, false to disable.
172  *
173  * @return 0 on success
174  * @return Negative error code on failure
175  */
176  virtual int enable_input(bool enabled);
177 
178  /** Enable or disable output
179  *
180  * Control enabling of device for output. This is primarily intended
181  * for temporary power-saving; the overall ability of the device to operate for
182  * input and/or output may be fixed at creation time, but this call can
183  * allow output to be temporarily disabled to permit power saving without
184  * losing device state.
185  *
186  * @param enabled true to enable output, false to disable.
187  *
188  * @return 0 on success
189  * @return Negative error code on failure
190  */
191  virtual int enable_output(bool enabled);
192 
193  /** Register a callback on state change of the file.
194  *
195  * The specified callback will be called on state changes such as when
196  * the file can be written to or read from.
197  *
198  * The callback may be called in an interrupt context and should not
199  * perform expensive operations.
200  *
201  * Note! This is not intended as an attach-like asynchronous api, but rather
202  * as a building block for constructing such functionality.
203  *
204  * The exact timing of when the registered function
205  * is called is not guaranteed and susceptible to change. It should be used
206  * as a cue to make read/write/poll calls to find the current state.
207  *
208  * @param func Function to call on state change
209  */
210  virtual void sigio(Callback<void()> func);
211 
212  /** Setup interrupt handler for DCD line
213  *
214  * If DCD line is connected, an IRQ handler will be setup.
215  * Does nothing if DCD is NC, i.e., not connected.
216  *
217  * @param dcd_pin Pin-name for DCD
218  * @param active_high a boolean set to true if DCD polarity is active low
219  */
220  void set_data_carrier_detect(PinName dcd_pin, bool active_high = false);
221 
222  /** Set the baud rate
223  *
224  * @param baud The baud rate
225  */
226  void set_baud(int baud);
227 
228  // Expose private SerialBase::Parity as UARTSerial::Parity
229  using SerialBase::Parity;
230  // In C++11, we wouldn't need to also have using directives for each value
231  using SerialBase::None;
232  using SerialBase::Odd;
233  using SerialBase::Even;
234  using SerialBase::Forced1;
235  using SerialBase::Forced0;
236 
237  /** Set the transmission format used by the serial port
238  *
239  * @param bits The number of bits in a word (5-8; default = 8)
240  * @param parity The parity used (None, Odd, Even, Forced1, Forced0; default = None)
241  * @param stop_bits The number of stop bits (1 or 2; default = 1)
242  */
243  void set_format(int bits = 8, Parity parity = UARTSerial::None, int stop_bits = 1);
244 
245 #if DEVICE_SERIAL_FC
246  // For now use the base enum - but in future we may have extra options
247  // such as XON/XOFF or manual GPIO RTSCTS.
248  using SerialBase::Flow;
249  // In C++11, we wouldn't need to also have using directives for each value
250  using SerialBase::Disabled;
251  using SerialBase::RTS;
252  using SerialBase::CTS;
253  using SerialBase::RTSCTS;
254 
255  /** Set the flow control type on the serial port
256  *
257  * @param type the flow control type (Disabled, RTS, CTS, RTSCTS)
258  * @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS)
259  * @param flow2 the second flow control pin (CTS for RTSCTS)
260  */
261  void set_flow_control(Flow type, PinName flow1 = NC, PinName flow2 = NC);
262 #endif
263 
264 private:
265 
266  /** SerialBase lock override */
267  virtual void lock(void);
268 
269  /** SerialBase unlock override */
270  virtual void unlock(void);
271 
272  /** Acquire mutex */
273  virtual void api_lock(void);
274 
275  /** Release mutex */
276  virtual void api_unlock(void);
277 
278  /** Unbuffered write - invoked when write called from critical section */
279  ssize_t write_unbuffered(const char *buf_ptr, size_t length);
280 
281  void enable_rx_irq();
282  void disable_rx_irq();
283  void enable_tx_irq();
284  void disable_tx_irq();
285 
286  /** Software serial buffers
287  * By default buffer size is 256 for TX and 256 for RX. Configurable through mbed_app.json
288  */
291 
292  PlatformMutex _mutex;
293 
294  Callback<void()> _sigio_cb;
295 
296  bool _blocking;
297  bool _tx_irq_enabled;
298  bool _rx_irq_enabled;
299  bool _tx_enabled;
300  bool _rx_enabled;
301  InterruptIn *_dcd_irq;
302 
303  /** Device Hanged up
304  * Determines if the device hanged up on us.
305  *
306  * @return True, if hanged up
307  */
308  bool hup() const;
309 
310  /** ISRs for serial
311  * Routines to handle interrupts on serial pins.
312  * Copies data into Circular Buffer.
313  * Reports the state change to File handle.
314  */
315  void tx_irq(void);
316  void rx_irq(void);
317 
318  void wake(void);
319 
320  void dcd_irq(void);
321 
322 };
323 
324 /** @}*/
325 
326 } //namespace mbed
327 
328 #endif //(DEVICE_SERIAL && DEVICE_INTERRUPTIN) || defined(DOXYGEN_ONLY)
329 #endif //MBED_UARTSERIAL_H
virtual int sync()
Flush any buffers associated with the file.
void baud(int baudrate)
Set the baud rate of the serial port.
virtual int enable_input(bool enabled)
Enable or disable input.
bool readable() const
Definition depends on the subclass implementing FileHandle.
Definition: FileHandle.h:292
Templated Circular buffer class.
virtual bool is_blocking() const
Check current blocking or non-blocking mode for file operations.
Definition: UARTSerial.h:158
virtual int enable_output(bool enabled)
Enable or disable output.
virtual ssize_t read(void *buffer, size_t length)
Read the contents of a file into a buffer.
void set_format(int bits=8, Parity parity=UARTSerial::None, int stop_bits=1)
Set the transmission format used by the serial port.
Class FileHandle.
Definition: FileHandle.h:46
Class providing buffered UART communication functionality using separate circular buffer for send and...
Definition: UARTSerial.h:51
Prevents generation of copy constructor and copy assignment operator in derived classes.
Definition: NonCopyable.h:169
void set_data_carrier_detect(PinName dcd_pin, bool active_high=false)
Setup interrupt handler for DCD line.
virtual void sigio(Callback< void()> func)
Register a callback on state change of the file.
virtual off_t seek(off_t offset, int whence)
Move the file position to a given offset from from a given location.
The PlatformMutex class is used to synchronize the execution of threads.
Definition: PlatformMutex.h:47
virtual int set_blocking(bool blocking)
Set blocking or non-blocking mode The default is blocking.
Definition: UARTSerial.h:148
A base class for serial port implementations Can&#39;t be instantiated directly (use Serial or RawSerial)...
Definition: SerialBase.h:46
void set_baud(int baud)
Set the baud rate.
A digital interrupt input, used to call a function on a rising or falling edge.
Definition: InterruptIn.h:65
virtual int isatty()
Check if the file in an interactive terminal device.
UARTSerial(PinName tx, PinName rx, int baud=MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE)
Create a UARTSerial port, connected to the specified transmit and receive pins, with a particular bau...
virtual short poll(short events) const
Equivalent to POSIX poll().
bool writable() const
Definition depends on the subclass implementing FileHandle.
Definition: FileHandle.h:281
virtual int close()
Close a file.
virtual ssize_t write(const void *buffer, size_t length)
Write the contents of a buffer to a file.
Callback class based on template specialization.
Definition: Callback.h:39
void set_flow_control(Flow type, PinName flow1=NC, PinName flow2=NC)
Set the flow control type on the serial port.
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.