NuMaker RS485 lib
Dependents: NuMaker-mbed-modbus-sample
MyUnbufferedSerial.h@6:531888b928d6, 4 months ago (annotated)
- Committer:
- morgandu
- Date:
- Mon May 13 08:51:47 2024 +0000
- Revision:
- 6:531888b928d6
- Parent:
- 4:3bdf456890b6
Add for NuMaker-IoT-M487 board
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cyliang | 4:3bdf456890b6 | 1 | /* mbed Microcontroller Library |
cyliang | 4:3bdf456890b6 | 2 | * Copyright (c) 2019 ARM Limited |
cyliang | 4:3bdf456890b6 | 3 | * SPDX-License-Identifier: Apache-2.0 |
cyliang | 4:3bdf456890b6 | 4 | * |
cyliang | 4:3bdf456890b6 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
cyliang | 4:3bdf456890b6 | 6 | * you may not use this file except in compliance with the License. |
cyliang | 4:3bdf456890b6 | 7 | * You may obtain a copy of the License at |
cyliang | 4:3bdf456890b6 | 8 | * |
cyliang | 4:3bdf456890b6 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
cyliang | 4:3bdf456890b6 | 10 | * |
cyliang | 4:3bdf456890b6 | 11 | * Unless required by applicable law or agreed to in writing, software |
cyliang | 4:3bdf456890b6 | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
cyliang | 4:3bdf456890b6 | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
cyliang | 4:3bdf456890b6 | 14 | * See the License for the specific language governing permissions and |
cyliang | 4:3bdf456890b6 | 15 | * limitations under the License. |
cyliang | 4:3bdf456890b6 | 16 | */ |
cyliang | 4:3bdf456890b6 | 17 | #ifndef MBED_MYUNBUFFERED_SERIAL_H |
cyliang | 4:3bdf456890b6 | 18 | #define MBED_MYUNBUFFERED_SERIAL_H |
cyliang | 4:3bdf456890b6 | 19 | #include "mbed.h" |
cyliang | 4:3bdf456890b6 | 20 | #if (MBED_MAJOR_VERSION >= 6) |
cyliang | 4:3bdf456890b6 | 21 | |
cyliang | 4:3bdf456890b6 | 22 | #include "platform/platform.h" |
cyliang | 4:3bdf456890b6 | 23 | |
cyliang | 4:3bdf456890b6 | 24 | #if DEVICE_SERIAL || defined(DOXYGEN_ONLY) |
cyliang | 4:3bdf456890b6 | 25 | |
cyliang | 4:3bdf456890b6 | 26 | #include <cstdarg> |
cyliang | 4:3bdf456890b6 | 27 | |
cyliang | 4:3bdf456890b6 | 28 | #include "drivers/SerialBase.h" |
cyliang | 4:3bdf456890b6 | 29 | #include "platform/FileHandle.h" |
cyliang | 4:3bdf456890b6 | 30 | #include "platform/mbed_toolchain.h" |
cyliang | 4:3bdf456890b6 | 31 | #include "platform/NonCopyable.h" |
cyliang | 4:3bdf456890b6 | 32 | |
cyliang | 4:3bdf456890b6 | 33 | |
cyliang | 4:3bdf456890b6 | 34 | namespace mbed { |
cyliang | 4:3bdf456890b6 | 35 | /** \defgroup drivers-public-api-uart UART |
cyliang | 4:3bdf456890b6 | 36 | * \ingroup drivers-public-api |
cyliang | 4:3bdf456890b6 | 37 | */ |
cyliang | 4:3bdf456890b6 | 38 | |
cyliang | 4:3bdf456890b6 | 39 | /** |
cyliang | 4:3bdf456890b6 | 40 | * \defgroup drivers_MyUnbufferedSerial MyUnbufferedSerial class |
cyliang | 4:3bdf456890b6 | 41 | * \ingroup drivers-public-api-uart |
cyliang | 4:3bdf456890b6 | 42 | * @{ |
cyliang | 4:3bdf456890b6 | 43 | */ |
cyliang | 4:3bdf456890b6 | 44 | |
cyliang | 4:3bdf456890b6 | 45 | /** |
cyliang | 4:3bdf456890b6 | 46 | * Class implementation for unbuffered I/O for an interrupt driven application |
cyliang | 4:3bdf456890b6 | 47 | * or one that needs to have more control. |
cyliang | 4:3bdf456890b6 | 48 | */ |
cyliang | 4:3bdf456890b6 | 49 | class MyUnbufferedSerial: |
cyliang | 4:3bdf456890b6 | 50 | public SerialBase, |
cyliang | 4:3bdf456890b6 | 51 | public FileHandle, |
cyliang | 4:3bdf456890b6 | 52 | private NonCopyable<MyUnbufferedSerial> { |
cyliang | 4:3bdf456890b6 | 53 | public: |
cyliang | 4:3bdf456890b6 | 54 | /** |
cyliang | 4:3bdf456890b6 | 55 | * Create a serial port instance connected to the specified transmit and |
cyliang | 4:3bdf456890b6 | 56 | * receive pins, with the specified baud rate. |
cyliang | 4:3bdf456890b6 | 57 | * |
cyliang | 4:3bdf456890b6 | 58 | * @param tx Transmit pin |
cyliang | 4:3bdf456890b6 | 59 | * @param rx Receive pin |
cyliang | 4:3bdf456890b6 | 60 | * @param baud The baud rate of the serial port (optional, defaults to MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE) |
cyliang | 4:3bdf456890b6 | 61 | * |
cyliang | 4:3bdf456890b6 | 62 | * @note |
cyliang | 4:3bdf456890b6 | 63 | * Either tx or rx may be specified as NC if unused |
cyliang | 4:3bdf456890b6 | 64 | */ |
cyliang | 4:3bdf456890b6 | 65 | MyUnbufferedSerial( |
cyliang | 4:3bdf456890b6 | 66 | PinName tx, |
cyliang | 4:3bdf456890b6 | 67 | PinName rx, |
cyliang | 4:3bdf456890b6 | 68 | int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE |
cyliang | 4:3bdf456890b6 | 69 | ); |
cyliang | 4:3bdf456890b6 | 70 | |
cyliang | 4:3bdf456890b6 | 71 | /** Create a MyUnbufferedSerial port, connected to the specified transmit and |
cyliang | 4:3bdf456890b6 | 72 | * receive pins, with a particular baud rate. |
cyliang | 4:3bdf456890b6 | 73 | * @param static_pinmap reference to structure which holds static pinmap |
cyliang | 4:3bdf456890b6 | 74 | * @param baud The baud rate of the serial port (optional, defaults to |
cyliang | 4:3bdf456890b6 | 75 | * MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE) |
cyliang | 4:3bdf456890b6 | 76 | */ |
cyliang | 4:3bdf456890b6 | 77 | MyUnbufferedSerial( |
cyliang | 4:3bdf456890b6 | 78 | const serial_pinmap_t &static_pinmap, |
cyliang | 4:3bdf456890b6 | 79 | int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE |
cyliang | 4:3bdf456890b6 | 80 | ); |
cyliang | 4:3bdf456890b6 | 81 | |
cyliang | 4:3bdf456890b6 | 82 | /** Write the contents of a buffer to a file |
cyliang | 4:3bdf456890b6 | 83 | * |
cyliang | 4:3bdf456890b6 | 84 | * Blocks until all data is written |
cyliang | 4:3bdf456890b6 | 85 | * |
cyliang | 4:3bdf456890b6 | 86 | * @param buffer The buffer to write from |
cyliang | 4:3bdf456890b6 | 87 | * @param size The number of bytes to write |
cyliang | 4:3bdf456890b6 | 88 | * @return The number of bytes written |
cyliang | 4:3bdf456890b6 | 89 | */ |
cyliang | 4:3bdf456890b6 | 90 | ssize_t write(const void *buffer, size_t size) override; |
cyliang | 4:3bdf456890b6 | 91 | |
cyliang | 4:3bdf456890b6 | 92 | /** Read the contents of a file into a buffer |
cyliang | 4:3bdf456890b6 | 93 | * |
cyliang | 4:3bdf456890b6 | 94 | * Blocks and reads exactly one character |
cyliang | 4:3bdf456890b6 | 95 | * |
cyliang | 4:3bdf456890b6 | 96 | * @param buffer The buffer to read in to |
cyliang | 4:3bdf456890b6 | 97 | * @param size The number of bytes to read |
cyliang | 4:3bdf456890b6 | 98 | * @return The number of bytes read |
cyliang | 4:3bdf456890b6 | 99 | */ |
cyliang | 4:3bdf456890b6 | 100 | ssize_t read(void *buffer, size_t size) override; |
cyliang | 4:3bdf456890b6 | 101 | |
cyliang | 4:3bdf456890b6 | 102 | /** Move the file position to a given offset from from a given location |
cyliang | 4:3bdf456890b6 | 103 | * |
cyliang | 4:3bdf456890b6 | 104 | * Not valid for a device type FileHandle like MyUnbufferedSerial. |
cyliang | 4:3bdf456890b6 | 105 | * In case of MyUnbufferedSerial, returns ESPIPE |
cyliang | 4:3bdf456890b6 | 106 | * |
cyliang | 4:3bdf456890b6 | 107 | * @param offset The offset from whence to move to |
cyliang | 4:3bdf456890b6 | 108 | * @param whence The start of where to seek |
cyliang | 4:3bdf456890b6 | 109 | * SEEK_SET to start from beginning of file, |
cyliang | 4:3bdf456890b6 | 110 | * SEEK_CUR to start from current position in file, |
cyliang | 4:3bdf456890b6 | 111 | * SEEK_END to start from end of file |
cyliang | 4:3bdf456890b6 | 112 | * @return The new offset of the file, negative error code on failure |
cyliang | 4:3bdf456890b6 | 113 | */ |
cyliang | 4:3bdf456890b6 | 114 | off_t seek(off_t offset, int whence = SEEK_SET) override |
cyliang | 4:3bdf456890b6 | 115 | { |
cyliang | 4:3bdf456890b6 | 116 | return -ESPIPE; |
cyliang | 4:3bdf456890b6 | 117 | } |
cyliang | 4:3bdf456890b6 | 118 | |
cyliang | 4:3bdf456890b6 | 119 | /** Get the size of the file |
cyliang | 4:3bdf456890b6 | 120 | * |
cyliang | 4:3bdf456890b6 | 121 | * @return Size of the file in bytes |
cyliang | 4:3bdf456890b6 | 122 | */ |
cyliang | 4:3bdf456890b6 | 123 | off_t size() override |
cyliang | 4:3bdf456890b6 | 124 | { |
cyliang | 4:3bdf456890b6 | 125 | return -EINVAL; |
cyliang | 4:3bdf456890b6 | 126 | } |
cyliang | 4:3bdf456890b6 | 127 | |
cyliang | 4:3bdf456890b6 | 128 | /** Check if the file in an interactive terminal device |
cyliang | 4:3bdf456890b6 | 129 | * |
cyliang | 4:3bdf456890b6 | 130 | * @return True if the file is a terminal |
cyliang | 4:3bdf456890b6 | 131 | * @return False if the file is not a terminal |
cyliang | 4:3bdf456890b6 | 132 | * @return Negative error code on failure |
cyliang | 4:3bdf456890b6 | 133 | */ |
cyliang | 4:3bdf456890b6 | 134 | int isatty() override |
cyliang | 4:3bdf456890b6 | 135 | { |
cyliang | 4:3bdf456890b6 | 136 | return true; |
cyliang | 4:3bdf456890b6 | 137 | } |
cyliang | 4:3bdf456890b6 | 138 | |
cyliang | 4:3bdf456890b6 | 139 | /** Close a file |
cyliang | 4:3bdf456890b6 | 140 | * |
cyliang | 4:3bdf456890b6 | 141 | * @return 0 on success, negative error code on failure |
cyliang | 4:3bdf456890b6 | 142 | */ |
cyliang | 4:3bdf456890b6 | 143 | int close() override |
cyliang | 4:3bdf456890b6 | 144 | { |
cyliang | 4:3bdf456890b6 | 145 | return 0; |
cyliang | 4:3bdf456890b6 | 146 | } |
cyliang | 4:3bdf456890b6 | 147 | |
cyliang | 4:3bdf456890b6 | 148 | /** Enable or disable input |
cyliang | 4:3bdf456890b6 | 149 | * |
cyliang | 4:3bdf456890b6 | 150 | * Control enabling of device for input. This is primarily intended |
cyliang | 4:3bdf456890b6 | 151 | * for temporary power-saving; the overall ability of the device to operate |
cyliang | 4:3bdf456890b6 | 152 | * for input and/or output may be fixed at creation time, but this call can |
cyliang | 4:3bdf456890b6 | 153 | * allow input to be temporarily disabled to permit power saving without |
cyliang | 4:3bdf456890b6 | 154 | * losing device state. |
cyliang | 4:3bdf456890b6 | 155 | * |
cyliang | 4:3bdf456890b6 | 156 | * @param enabled true to enable input, false to disable. |
cyliang | 4:3bdf456890b6 | 157 | * |
cyliang | 4:3bdf456890b6 | 158 | * @return 0 on success |
cyliang | 4:3bdf456890b6 | 159 | * @return Negative error code on failure |
cyliang | 4:3bdf456890b6 | 160 | */ |
cyliang | 4:3bdf456890b6 | 161 | int enable_input(bool enabled) override; |
cyliang | 4:3bdf456890b6 | 162 | |
cyliang | 4:3bdf456890b6 | 163 | /** Enable or disable output |
cyliang | 4:3bdf456890b6 | 164 | * |
cyliang | 4:3bdf456890b6 | 165 | * Control enabling of device for output. This is primarily intended |
cyliang | 4:3bdf456890b6 | 166 | * for temporary power-saving; the overall ability of the device to operate |
cyliang | 4:3bdf456890b6 | 167 | * for input and/or output may be fixed at creation time, but this call can |
cyliang | 4:3bdf456890b6 | 168 | * allow output to be temporarily disabled to permit power saving without |
cyliang | 4:3bdf456890b6 | 169 | * losing device state. |
cyliang | 4:3bdf456890b6 | 170 | * |
cyliang | 4:3bdf456890b6 | 171 | * @param enabled true to enable output, false to disable. |
cyliang | 4:3bdf456890b6 | 172 | * |
cyliang | 4:3bdf456890b6 | 173 | * @return 0 on success |
cyliang | 4:3bdf456890b6 | 174 | * @return Negative error code on failure |
cyliang | 4:3bdf456890b6 | 175 | */ |
cyliang | 4:3bdf456890b6 | 176 | int enable_output(bool enabled) override; |
cyliang | 4:3bdf456890b6 | 177 | |
cyliang | 4:3bdf456890b6 | 178 | /** Check for poll event flags |
cyliang | 4:3bdf456890b6 | 179 | * Check the events listed in events to see if data can be read or written |
cyliang | 4:3bdf456890b6 | 180 | * without blocking. |
cyliang | 4:3bdf456890b6 | 181 | * Call is nonblocking - returns state of events. |
cyliang | 4:3bdf456890b6 | 182 | * |
cyliang | 4:3bdf456890b6 | 183 | * @param events bitmask of poll events we're interested in - POLLIN/POLLOUT etc. |
cyliang | 4:3bdf456890b6 | 184 | * |
cyliang | 4:3bdf456890b6 | 185 | * @returns bitmask of poll events that have occurred. |
cyliang | 4:3bdf456890b6 | 186 | */ |
cyliang | 4:3bdf456890b6 | 187 | short poll(short events) const override; |
cyliang | 4:3bdf456890b6 | 188 | |
cyliang | 4:3bdf456890b6 | 189 | using SerialBase::attach; |
cyliang | 4:3bdf456890b6 | 190 | using SerialBase::baud; |
cyliang | 4:3bdf456890b6 | 191 | using SerialBase::format; |
cyliang | 4:3bdf456890b6 | 192 | using SerialBase::readable; |
cyliang | 4:3bdf456890b6 | 193 | using SerialBase::writeable; |
cyliang | 4:3bdf456890b6 | 194 | using SerialBase::IrqCnt; |
cyliang | 4:3bdf456890b6 | 195 | using SerialBase::RxIrq; |
cyliang | 4:3bdf456890b6 | 196 | using SerialBase::TxIrq; |
cyliang | 4:3bdf456890b6 | 197 | |
cyliang | 4:3bdf456890b6 | 198 | #if DEVICE_SERIAL_FC |
cyliang | 4:3bdf456890b6 | 199 | // For now use the base enum - but in future we may have extra options |
cyliang | 4:3bdf456890b6 | 200 | // such as XON/XOFF or manual GPIO RTSCTS. |
cyliang | 4:3bdf456890b6 | 201 | using SerialBase::Flow; |
cyliang | 4:3bdf456890b6 | 202 | // In C++11, we wouldn't need to also have using directives for each value |
cyliang | 4:3bdf456890b6 | 203 | using SerialBase::Disabled; |
cyliang | 4:3bdf456890b6 | 204 | using SerialBase::RTS; |
cyliang | 4:3bdf456890b6 | 205 | using SerialBase::CTS; |
cyliang | 4:3bdf456890b6 | 206 | using SerialBase::RTSCTS; |
cyliang | 4:3bdf456890b6 | 207 | |
cyliang | 4:3bdf456890b6 | 208 | /** Set the flow control type on the serial port |
cyliang | 4:3bdf456890b6 | 209 | * |
cyliang | 4:3bdf456890b6 | 210 | * @param type the flow control type (Disabled, RTS, CTS, RTSCTS) |
cyliang | 4:3bdf456890b6 | 211 | * @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS) |
cyliang | 4:3bdf456890b6 | 212 | * @param flow2 the second flow control pin (CTS for RTSCTS) |
cyliang | 4:3bdf456890b6 | 213 | */ |
cyliang | 4:3bdf456890b6 | 214 | void set_flow_control(Flow type, PinName flow1 = NC, PinName flow2 = NC); |
cyliang | 4:3bdf456890b6 | 215 | #endif // DEVICE_SERIAL_FC |
cyliang | 4:3bdf456890b6 | 216 | }; |
cyliang | 4:3bdf456890b6 | 217 | |
cyliang | 4:3bdf456890b6 | 218 | } // namespace mbed |
cyliang | 4:3bdf456890b6 | 219 | |
cyliang | 4:3bdf456890b6 | 220 | #endif // DEVICE_SERIAL || defined(DOXYGEN_ONLY) |
cyliang | 4:3bdf456890b6 | 221 | |
cyliang | 4:3bdf456890b6 | 222 | #endif // (MBED_MAJOR_VERSION >= 6 ) |
cyliang | 4:3bdf456890b6 | 223 | #endif // MBED_MYUNBUFFERED_SERIAL_H |