mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Serial.h Source File

Serial.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #ifndef MBED_SERIAL_H
00018 #define MBED_SERIAL_H
00019 
00020 #include "platform/platform.h"
00021 
00022 #if DEVICE_SERIAL || defined(DOXYGEN_ONLY)
00023 
00024 #include "platform/Stream.h"
00025 #include "SerialBase.h"
00026 #include "platform/PlatformMutex.h"
00027 #include "hal/serial_api.h"
00028 #include "platform/NonCopyable.h"
00029 
00030 namespace mbed {
00031 /** \addtogroup drivers */
00032 
00033 /** A serial port (UART) for communication with other serial devices
00034  *
00035  * Can be used for Full Duplex communication, or Simplex by specifying
00036  * one pin as NC (Not Connected)
00037  *
00038  * @note Synchronization level: Thread safe
00039  *
00040  * Example:
00041  * @code
00042  * // Print "Hello World" to the PC
00043  *
00044  * #include "mbed.h"
00045  *
00046  * Serial pc(USBTX, USBRX);
00047  *
00048  * int main() {
00049  *     pc.printf("Hello World\n");
00050  * }
00051  * @endcode
00052  * @ingroup drivers
00053  */
00054 class Serial : public SerialBase, public Stream, private NonCopyable<Serial> {
00055 
00056 public:
00057 #if DEVICE_SERIAL_ASYNCH
00058     using SerialBase::read;
00059     using SerialBase::write;
00060 #endif
00061 
00062     /** Create a Serial port, connected to the specified transmit and receive pins
00063      *
00064      *  @param tx Transmit pin
00065      *  @param rx Receive pin
00066      *  @param name The name of the stream associated with this serial port (optional)
00067      *  @param baud The baud rate of the serial port (optional, defaults to MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE or 9600)
00068      *
00069      *  @note
00070      *    Either tx or rx may be specified as NC (Not Connected) if unused
00071      */
00072     Serial(PinName tx, PinName rx, const char *name = NULL, int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE);
00073 
00074 
00075     /** Create a Serial port, connected to the specified transmit and receive pins, with the specified baud
00076      *
00077      *  @param tx Transmit pin
00078      *  @param rx Receive pin
00079      *  @param baud The baud rate of the serial port
00080      *
00081      *  @note
00082      *    Either tx or rx may be specified as NC (Not Connected) if unused
00083      */
00084     Serial(PinName tx, PinName rx, int baud);
00085 
00086     /* Stream gives us a FileHandle with non-functional poll()/readable()/writable. Pass through
00087      * the calls from the SerialBase instead for backwards compatibility. This problem is
00088      * part of why Stream and Serial should be deprecated.
00089      */
00090     bool readable()
00091     {
00092         return SerialBase::readable();
00093     }
00094     bool writable()
00095     {
00096         return SerialBase::writeable();
00097     }
00098     bool writeable()
00099     {
00100         return SerialBase::writeable();
00101     }
00102 
00103 #if !(DOXYGEN_ONLY)
00104 protected:
00105     virtual int _getc();
00106     virtual int _putc(int c);
00107     virtual void lock();
00108     virtual void unlock();
00109 
00110     PlatformMutex _mutex;
00111 #endif
00112 };
00113 
00114 } // namespace mbed
00115 
00116 #endif
00117 
00118 #endif