|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 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_MINIMAL_SERIAL_H |
| 18 | +#define MBED_MINIMAL_SERIAL_H |
| 19 | + |
| 20 | +#include "platform/platform.h" |
| 21 | + |
| 22 | +#if DEVICE_SERIAL || defined(DOXYGEN_ONLY) |
| 23 | +#include "hal/serial_api.h" |
| 24 | +#include "platform/NonCopyable.h" |
| 25 | +#include "PinNames.h" |
| 26 | + |
| 27 | +namespace mbed { |
| 28 | + |
| 29 | +/** |
| 30 | + * \defgroup drivers_MinimalSerial MinimalSerial class |
| 31 | + * \ingroup drivers-public-api-uart |
| 32 | + * @{ |
| 33 | + */ |
| 34 | + |
| 35 | +/** A abstract class for serial port implementations |
| 36 | + */ |
| 37 | +class MinimalSerial : private NonCopyable<MinimalSerial> { |
| 38 | + |
| 39 | +public: |
| 40 | + |
| 41 | + enum Flow { |
| 42 | + Disabled = 0, |
| 43 | + RTS, |
| 44 | + CTS, |
| 45 | + RTSCTS |
| 46 | + }; |
| 47 | + |
| 48 | +#if DEVICE_SERIAL_FC |
| 49 | + /** Set the flow control type on the serial port |
| 50 | + * |
| 51 | + * @param type the flow control type (Disabled, RTS, CTS, RTSCTS) |
| 52 | + * @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS) |
| 53 | + * @param flow2 the second flow control pin (CTS for RTSCTS) |
| 54 | + */ |
| 55 | + void set_flow_control(Flow type, PinName flow1 = NC, PinName flow2 = NC); |
| 56 | +#endif // DEVICE_SERIAL_FC |
| 57 | + |
| 58 | + /** Set the baud rate of the serial port |
| 59 | + * |
| 60 | + * @param baudrate The baudrate of the serial port. |
| 61 | + */ |
| 62 | + void baud(int baudrate); |
| 63 | + |
| 64 | + /** Write a char to the serial port |
| 65 | + * |
| 66 | + * @param c The char to write |
| 67 | + * |
| 68 | + * @returns The written char |
| 69 | + */ |
| 70 | + int putc(int c); |
| 71 | + |
| 72 | + /** Read a char to the serial port |
| 73 | + * |
| 74 | + * @returns The char read from the serial port |
| 75 | + */ |
| 76 | + int getc(); |
| 77 | + |
| 78 | +protected: |
| 79 | + MinimalSerial( |
| 80 | + PinName tx, |
| 81 | + PinName rx, |
| 82 | + int baud |
| 83 | + ); |
| 84 | + |
| 85 | + MinimalSerial( |
| 86 | + serial_t *serial_ptr, |
| 87 | + bool serial_inited, |
| 88 | + PinName tx, |
| 89 | + PinName rx, |
| 90 | + int baud |
| 91 | + ); |
| 92 | + |
| 93 | + virtual ~MinimalSerial(); |
| 94 | + |
| 95 | + int _base_getc(); |
| 96 | + |
| 97 | + int _base_putc(int c); |
| 98 | + |
| 99 | + int _baud; |
| 100 | + |
| 101 | + serial_t _serial; |
| 102 | + |
| 103 | +#if !defined(DOXYGEN_ONLY) |
| 104 | + /** Acquire exclusive access to this serial port |
| 105 | + */ |
| 106 | + virtual void lock(void){}; |
| 107 | + |
| 108 | + /** Release exclusive access to this serial port |
| 109 | + */ |
| 110 | + virtual void unlock(void){}; |
| 111 | +#endif // !defined(DOXYGEN_ONLY) |
| 112 | + |
| 113 | +private: |
| 114 | + void init(PinName tx, PinName rx); |
| 115 | + serial_t *_serial_ptr; |
| 116 | +}; |
| 117 | + |
| 118 | +/** @}*/ |
| 119 | + |
| 120 | +} // namespace mbed |
| 121 | + |
| 122 | +#endif // DEVICE_SERIAL || defined(DOXYGEN_ONLY) |
| 123 | + |
| 124 | +#endif // MBED_MINIMAL_SERIAL_H |
0 commit comments