Mistake on this page?
Report an issue in GitHub or email us
Serial.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 #ifndef MBED_SERIAL_H
18 #define MBED_SERIAL_H
19 
20 #include "platform/platform.h"
21 
22 #if DEVICE_SERIAL || defined(DOXYGEN_ONLY)
23 
24 #include "platform/Stream.h"
25 #include "drivers/SerialBase.h"
26 #include "platform/PlatformMutex.h"
27 #include "platform/NonCopyable.h"
28 
29 namespace mbed {
30 /**
31  * \defgroup drivers_Serial Serial class
32  * \ingroup drivers-public-api-uart
33  * @{
34  */
35 
36 /** A serial port (UART) for communication with other serial devices
37  *
38  * Can be used for Full Duplex communication, or Simplex by specifying
39  * one pin as NC (Not Connected)
40  *
41  * @note Synchronization level: Thread safe
42  *
43  * Example:
44  * @code
45  * // Print "Hello World" to the PC
46  *
47  * #include "mbed.h"
48  *
49  * Serial pc(USBTX, USBRX);
50  *
51  * int main() {
52  * pc.printf("Hello World\n");
53  * }
54  * @endcode
55  */
56 class Serial : public SerialBase, public Stream, private NonCopyable<Serial> {
57 
58 public:
59 #if DEVICE_SERIAL_ASYNCH
60  using SerialBase::read;
61  using SerialBase::write;
62 #endif
63 
64  /** Create a Serial port, connected to the specified transmit and receive pins
65  *
66  * @param tx Transmit pin
67  * @param rx Receive pin
68  * @param name The name of the stream associated with this serial port (optional)
69  * @param baud The baud rate of the serial port (optional, defaults to MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE or 9600)
70  *
71  * @note
72  * Either tx or rx may be specified as NC (Not Connected) if unused
73  */
74  Serial(PinName tx, PinName rx, const char *name = NULL, int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE);
75 
76  /** Create a Serial port, connected to the specified transmit and receive pins
77  *
78  * @param static_pinmap reference to structure which holds static pinmap.
79  * @param name The name of the stream associated with this serial port (optional)
80  * @param baud The baud rate of the serial port (optional, defaults to MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE or 9600)
81  *
82  * @note
83  * Either tx or rx may be specified as NC (Not Connected) if unused
84  */
85  Serial(const serial_pinmap_t &static_pinmap, const char *name = NULL, int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE);
86  Serial(const serial_pinmap_t &&, const char * = NULL, int = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE) = delete; // prevent passing of temporary objects
87 
88  /** Create a Serial port, connected to the specified transmit and receive pins, with the specified baud
89  *
90  * @param tx Transmit pin
91  * @param rx Receive pin
92  * @param baud The baud rate of the serial port
93  *
94  * @note
95  * Either tx or rx may be specified as NC (Not Connected) if unused
96  */
97  Serial(PinName tx, PinName rx, int baud);
98 
99  /** Create a Serial port, connected to the specified transmit and receive pins, with the specified baud
100  *
101  * @param static_pinmap reference to structure which holds static pinmap.
102  * @param baud The baud rate of the serial port
103  *
104  * @note
105  * Either tx or rx may be specified as NC (Not Connected) if unused
106  */
107  Serial(const serial_pinmap_t &static_pinmap, int baud);
108  Serial(const serial_pinmap_t &&, int) = delete; // prevent passing of temporary objects
109 
110  /* Stream gives us a FileHandle with non-functional poll()/readable()/writable. Pass through
111  * the calls from the SerialBase instead for backwards compatibility. This problem is
112  * part of why Stream and Serial should be deprecated.
113  */
114  bool readable()
115  {
116  return SerialBase::readable();
117  }
118  bool writable()
119  {
120  return SerialBase::writeable();
121  }
122  bool writeable()
123  {
124  return SerialBase::writeable();
125  }
126 
127 #if !(DOXYGEN_ONLY)
128 protected:
129  virtual int _getc();
130  virtual int _putc(int c);
131  virtual void lock();
132  virtual void unlock();
133 
134  PlatformMutex _mutex;
135 #endif
136 };
137 
138 /** @}*/
139 
140 } // namespace mbed
141 
142 #endif
143 
144 #endif
A serial port (UART) for communication with other serial devices.
Definition: Serial.h:56
void baud(int baudrate)
Set the baud rate of the serial port.
File stream.
Definition: Stream.h:42
Serial(PinName tx, PinName rx, const char *name=NULL, int baud=MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE)
Create a Serial port, connected to the specified transmit and receive pins.
int write(const uint8_t *buffer, int length, const event_callback_t &callback, int event=SERIAL_EVENT_TX_COMPLETE)
Begin asynchronous write using 8bit buffer.
Prevents generation of copy constructor and copy assignment operator in derived classes.
Definition: NonCopyable.h:169
int writeable()
Determine if there is space available to write a character.
The PlatformMutex class is used to synchronize the execution of threads.
Definition: PlatformMutex.h:47
A base class for serial port implementations Can&#39;t be instantiated directly (use Serial or RawSerial)...
Definition: SerialBase.h:46
int readable()
Determine if there is a character available to read.
int read(uint8_t *buffer, int length, const event_callback_t &callback, int event=SERIAL_EVENT_RX_COMPLETE, unsigned char char_match=SERIAL_RESERVED_CHAR_MATCH)
Begin asynchronous reading using 8bit buffer.
virtual void unlock()
Release exclusive access to this object.
Definition: Stream.h:91
virtual void lock()
Acquire exclusive access to this object.
Definition: Stream.h:84
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.