Mistake on this page?
Report an issue in GitHub or email us
Serial.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2013 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 "SerialBase.h"
26 #include "platform/PlatformMutex.h"
27 #include "hal/serial_api.h"
28 #include "platform/NonCopyable.h"
29 
30 namespace mbed {
31 /** \addtogroup drivers */
32 
33 /** A serial port (UART) for communication with other serial devices
34  *
35  * Can be used for Full Duplex communication, or Simplex by specifying
36  * one pin as NC (Not Connected)
37  *
38  * @note Synchronization level: Thread safe
39  *
40  * Example:
41  * @code
42  * // Print "Hello World" to the PC
43  *
44  * #include "mbed.h"
45  *
46  * Serial pc(USBTX, USBRX);
47  *
48  * int main() {
49  * pc.printf("Hello World\n");
50  * }
51  * @endcode
52  * @ingroup drivers
53  */
54 class Serial : public SerialBase, public Stream, private NonCopyable<Serial> {
55 
56 public:
57 #if DEVICE_SERIAL_ASYNCH
58  using SerialBase::read;
59  using SerialBase::write;
60 #endif
61 
62  /** Create a Serial port, connected to the specified transmit and receive pins
63  *
64  * @param tx Transmit pin
65  * @param rx Receive pin
66  * @param name The name of the stream associated with this serial port (optional)
67  * @param baud The baud rate of the serial port (optional, defaults to MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE or 9600)
68  *
69  * @note
70  * Either tx or rx may be specified as NC (Not Connected) if unused
71  */
72  Serial(PinName tx, PinName rx, const char *name = NULL, int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE);
73 
74 
75  /** Create a Serial port, connected to the specified transmit and receive pins, with the specified baud
76  *
77  * @param tx Transmit pin
78  * @param rx Receive pin
79  * @param baud The baud rate of the serial port
80  *
81  * @note
82  * Either tx or rx may be specified as NC (Not Connected) if unused
83  */
84  Serial(PinName tx, PinName rx, int baud);
85 
86  /* Stream gives us a FileHandle with non-functional poll()/readable()/writable. Pass through
87  * the calls from the SerialBase instead for backwards compatibility. This problem is
88  * part of why Stream and Serial should be deprecated.
89  */
90  bool readable()
91  {
92  return SerialBase::readable();
93  }
94  bool writable()
95  {
96  return SerialBase::writeable();
97  }
98  bool writeable()
99  {
100  return SerialBase::writeable();
101  }
102 
103 #if !(DOXYGEN_ONLY)
104 protected:
105  virtual int _getc();
106  virtual int _putc(int c);
107  virtual void lock();
108  virtual void unlock();
109 
110  PlatformMutex _mutex;
111 #endif
112 };
113 
114 } // namespace mbed
115 
116 #endif
117 
118 #endif
A serial port (UART) for communication with other serial devices.
Definition: Serial.h:54
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:168
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:43
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.