001

Committer:
ganlikun
Date:
Sun Jun 12 14:02:44 2022 +0000
Revision:
0:13413ea9a877
00

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ganlikun 0:13413ea9a877 1 /* mbed Microcontroller Library
ganlikun 0:13413ea9a877 2 * Copyright (c) 2006-2013 ARM Limited
ganlikun 0:13413ea9a877 3 *
ganlikun 0:13413ea9a877 4 * Licensed under the Apache License, Version 2.0 (the "License");
ganlikun 0:13413ea9a877 5 * you may not use this file except in compliance with the License.
ganlikun 0:13413ea9a877 6 * You may obtain a copy of the License at
ganlikun 0:13413ea9a877 7 *
ganlikun 0:13413ea9a877 8 * http://www.apache.org/licenses/LICENSE-2.0
ganlikun 0:13413ea9a877 9 *
ganlikun 0:13413ea9a877 10 * Unless required by applicable law or agreed to in writing, software
ganlikun 0:13413ea9a877 11 * distributed under the License is distributed on an "AS IS" BASIS,
ganlikun 0:13413ea9a877 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ganlikun 0:13413ea9a877 13 * See the License for the specific language governing permissions and
ganlikun 0:13413ea9a877 14 * limitations under the License.
ganlikun 0:13413ea9a877 15 */
ganlikun 0:13413ea9a877 16 #ifndef MBED_SERIAL_H
ganlikun 0:13413ea9a877 17 #define MBED_SERIAL_H
ganlikun 0:13413ea9a877 18
ganlikun 0:13413ea9a877 19 #include "platform/platform.h"
ganlikun 0:13413ea9a877 20
ganlikun 0:13413ea9a877 21 #if defined (DEVICE_SERIAL) || defined(DOXYGEN_ONLY)
ganlikun 0:13413ea9a877 22
ganlikun 0:13413ea9a877 23 #include "Stream.h"
ganlikun 0:13413ea9a877 24 #include "SerialBase.h"
ganlikun 0:13413ea9a877 25 #include "PlatformMutex.h"
ganlikun 0:13413ea9a877 26 #include "serial_api.h"
ganlikun 0:13413ea9a877 27 #include "platform/NonCopyable.h"
ganlikun 0:13413ea9a877 28
ganlikun 0:13413ea9a877 29 namespace mbed {
ganlikun 0:13413ea9a877 30 /** \addtogroup drivers */
ganlikun 0:13413ea9a877 31
ganlikun 0:13413ea9a877 32 /** A serial port (UART) for communication with other serial devices
ganlikun 0:13413ea9a877 33 *
ganlikun 0:13413ea9a877 34 * Can be used for Full Duplex communication, or Simplex by specifying
ganlikun 0:13413ea9a877 35 * one pin as NC (Not Connected)
ganlikun 0:13413ea9a877 36 *
ganlikun 0:13413ea9a877 37 * @note Synchronization level: Thread safe
ganlikun 0:13413ea9a877 38 *
ganlikun 0:13413ea9a877 39 * Example:
ganlikun 0:13413ea9a877 40 * @code
ganlikun 0:13413ea9a877 41 * // Print "Hello World" to the PC
ganlikun 0:13413ea9a877 42 *
ganlikun 0:13413ea9a877 43 * #include "mbed.h"
ganlikun 0:13413ea9a877 44 *
ganlikun 0:13413ea9a877 45 * Serial pc(USBTX, USBRX);
ganlikun 0:13413ea9a877 46 *
ganlikun 0:13413ea9a877 47 * int main() {
ganlikun 0:13413ea9a877 48 * pc.printf("Hello World\n");
ganlikun 0:13413ea9a877 49 * }
ganlikun 0:13413ea9a877 50 * @endcode
ganlikun 0:13413ea9a877 51 * @ingroup drivers
ganlikun 0:13413ea9a877 52 */
ganlikun 0:13413ea9a877 53 class Serial : public SerialBase, public Stream, private NonCopyable<Serial> {
ganlikun 0:13413ea9a877 54
ganlikun 0:13413ea9a877 55 public:
ganlikun 0:13413ea9a877 56 #if DEVICE_SERIAL_ASYNCH
ganlikun 0:13413ea9a877 57 using SerialBase::read;
ganlikun 0:13413ea9a877 58 using SerialBase::write;
ganlikun 0:13413ea9a877 59 #endif
ganlikun 0:13413ea9a877 60
ganlikun 0:13413ea9a877 61 /** Create a Serial port, connected to the specified transmit and receive pins
ganlikun 0:13413ea9a877 62 *
ganlikun 0:13413ea9a877 63 * @param tx Transmit pin
ganlikun 0:13413ea9a877 64 * @param rx Receive pin
ganlikun 0:13413ea9a877 65 * @param name The name of the stream associated with this serial port (optional)
ganlikun 0:13413ea9a877 66 * @param baud The baud rate of the serial port (optional, defaults to MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE)
ganlikun 0:13413ea9a877 67 *
ganlikun 0:13413ea9a877 68 * @note
ganlikun 0:13413ea9a877 69 * Either tx or rx may be specified as NC if unused
ganlikun 0:13413ea9a877 70 */
ganlikun 0:13413ea9a877 71 Serial(PinName tx, PinName rx, const char *name=NULL, int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE);
ganlikun 0:13413ea9a877 72
ganlikun 0:13413ea9a877 73
ganlikun 0:13413ea9a877 74 /** Create a Serial port, connected to the specified transmit and receive pins, with the specified baud
ganlikun 0:13413ea9a877 75 *
ganlikun 0:13413ea9a877 76 * @param tx Transmit pin
ganlikun 0:13413ea9a877 77 * @param rx Receive pin
ganlikun 0:13413ea9a877 78 * @param baud The baud rate of the serial port
ganlikun 0:13413ea9a877 79 *
ganlikun 0:13413ea9a877 80 * @note
ganlikun 0:13413ea9a877 81 * Either tx or rx may be specified as NC if unused
ganlikun 0:13413ea9a877 82 */
ganlikun 0:13413ea9a877 83 Serial(PinName tx, PinName rx, int baud);
ganlikun 0:13413ea9a877 84
ganlikun 0:13413ea9a877 85 /* Stream gives us a FileHandle with non-functional poll()/readable()/writable. Pass through
ganlikun 0:13413ea9a877 86 * the calls from the SerialBase instead for backwards compatibility. This problem is
ganlikun 0:13413ea9a877 87 * part of why Stream and Serial should be deprecated.
ganlikun 0:13413ea9a877 88 */
ganlikun 0:13413ea9a877 89 bool readable()
ganlikun 0:13413ea9a877 90 {
ganlikun 0:13413ea9a877 91 return SerialBase::readable();
ganlikun 0:13413ea9a877 92 }
ganlikun 0:13413ea9a877 93 bool writable()
ganlikun 0:13413ea9a877 94 {
ganlikun 0:13413ea9a877 95 return SerialBase::writeable();
ganlikun 0:13413ea9a877 96 }
ganlikun 0:13413ea9a877 97 bool writeable()
ganlikun 0:13413ea9a877 98 {
ganlikun 0:13413ea9a877 99 return SerialBase::writeable();
ganlikun 0:13413ea9a877 100 }
ganlikun 0:13413ea9a877 101
ganlikun 0:13413ea9a877 102 protected:
ganlikun 0:13413ea9a877 103 virtual int _getc();
ganlikun 0:13413ea9a877 104 virtual int _putc(int c);
ganlikun 0:13413ea9a877 105 virtual void lock();
ganlikun 0:13413ea9a877 106 virtual void unlock();
ganlikun 0:13413ea9a877 107
ganlikun 0:13413ea9a877 108 PlatformMutex _mutex;
ganlikun 0:13413ea9a877 109 };
ganlikun 0:13413ea9a877 110
ganlikun 0:13413ea9a877 111 } // namespace mbed
ganlikun 0:13413ea9a877 112
ganlikun 0:13413ea9a877 113 #endif
ganlikun 0:13413ea9a877 114
ganlikun 0:13413ea9a877 115 #endif
ganlikun 0:13413ea9a877 116