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