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 
77  /** Create a Serial port, connected to the specified transmit and receive pins, with the specified baud
78  *
79  * @param tx Transmit pin
80  * @param rx Receive pin
81  * @param baud The baud rate of the serial port
82  *
83  * @note
84  * Either tx or rx may be specified as NC (Not Connected) if unused
85  */
86  Serial(PinName tx, PinName rx, int baud);
87 
88  /* Stream gives us a FileHandle with non-functional poll()/readable()/writable. Pass through
89  * the calls from the SerialBase instead for backwards compatibility. This problem is
90  * part of why Stream and Serial should be deprecated.
91  */
92  bool readable()
93  {
94  return SerialBase::readable();
95  }
96  bool writable()
97  {
98  return SerialBase::writeable();
99  }
100  bool writeable()
101  {
102  return SerialBase::writeable();
103  }
104 
105 #if !(DOXYGEN_ONLY)
106 protected:
107  virtual int _getc();
108  virtual int _putc(int c);
109  virtual void lock();
110  virtual void unlock();
111 
112  PlatformMutex _mutex;
113 #endif
114 };
115 
116 /** @}*/
117 
118 } // namespace mbed
119 
120 #endif
121 
122 #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:87
virtual void lock()
Acquire exclusive access to this object.
Definition: Stream.h:80
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.