1232
drivers/SerialBase.h@0:06036f8bee2d, 2022-10-24 (annotated)
- Committer:
- ganlikun
- Date:
- Mon Oct 24 15:19:39 2022 +0000
- Revision:
- 0:06036f8bee2d
11
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ganlikun | 0:06036f8bee2d | 1 | /* mbed Microcontroller Library |
ganlikun | 0:06036f8bee2d | 2 | * Copyright (c) 2006-2013 ARM Limited |
ganlikun | 0:06036f8bee2d | 3 | * |
ganlikun | 0:06036f8bee2d | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
ganlikun | 0:06036f8bee2d | 5 | * you may not use this file except in compliance with the License. |
ganlikun | 0:06036f8bee2d | 6 | * You may obtain a copy of the License at |
ganlikun | 0:06036f8bee2d | 7 | * |
ganlikun | 0:06036f8bee2d | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
ganlikun | 0:06036f8bee2d | 9 | * |
ganlikun | 0:06036f8bee2d | 10 | * Unless required by applicable law or agreed to in writing, software |
ganlikun | 0:06036f8bee2d | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
ganlikun | 0:06036f8bee2d | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
ganlikun | 0:06036f8bee2d | 13 | * See the License for the specific language governing permissions and |
ganlikun | 0:06036f8bee2d | 14 | * limitations under the License. |
ganlikun | 0:06036f8bee2d | 15 | */ |
ganlikun | 0:06036f8bee2d | 16 | #ifndef MBED_SERIALBASE_H |
ganlikun | 0:06036f8bee2d | 17 | #define MBED_SERIALBASE_H |
ganlikun | 0:06036f8bee2d | 18 | |
ganlikun | 0:06036f8bee2d | 19 | #include "platform/platform.h" |
ganlikun | 0:06036f8bee2d | 20 | |
ganlikun | 0:06036f8bee2d | 21 | #if defined (DEVICE_SERIAL) || defined(DOXYGEN_ONLY) |
ganlikun | 0:06036f8bee2d | 22 | |
ganlikun | 0:06036f8bee2d | 23 | #include "Callback.h" |
ganlikun | 0:06036f8bee2d | 24 | #include "serial_api.h" |
ganlikun | 0:06036f8bee2d | 25 | #include "mbed_toolchain.h" |
ganlikun | 0:06036f8bee2d | 26 | #include "platform/NonCopyable.h" |
ganlikun | 0:06036f8bee2d | 27 | |
ganlikun | 0:06036f8bee2d | 28 | #if DEVICE_SERIAL_ASYNCH |
ganlikun | 0:06036f8bee2d | 29 | #include "CThunk.h" |
ganlikun | 0:06036f8bee2d | 30 | #include "dma_api.h" |
ganlikun | 0:06036f8bee2d | 31 | #endif |
ganlikun | 0:06036f8bee2d | 32 | |
ganlikun | 0:06036f8bee2d | 33 | namespace mbed { |
ganlikun | 0:06036f8bee2d | 34 | /** \addtogroup drivers */ |
ganlikun | 0:06036f8bee2d | 35 | |
ganlikun | 0:06036f8bee2d | 36 | /** A base class for serial port implementations |
ganlikun | 0:06036f8bee2d | 37 | * Can't be instantiated directly (use Serial or RawSerial) |
ganlikun | 0:06036f8bee2d | 38 | * |
ganlikun | 0:06036f8bee2d | 39 | * @note Synchronization level: Set by subclass |
ganlikun | 0:06036f8bee2d | 40 | * @ingroup drivers |
ganlikun | 0:06036f8bee2d | 41 | */ |
ganlikun | 0:06036f8bee2d | 42 | class SerialBase : private NonCopyable<SerialBase> { |
ganlikun | 0:06036f8bee2d | 43 | |
ganlikun | 0:06036f8bee2d | 44 | public: |
ganlikun | 0:06036f8bee2d | 45 | /** Set the baud rate of the serial port |
ganlikun | 0:06036f8bee2d | 46 | * |
ganlikun | 0:06036f8bee2d | 47 | * @param baudrate The baudrate of the serial port (default = 9600). |
ganlikun | 0:06036f8bee2d | 48 | */ |
ganlikun | 0:06036f8bee2d | 49 | void baud(int baudrate); |
ganlikun | 0:06036f8bee2d | 50 | |
ganlikun | 0:06036f8bee2d | 51 | enum Parity { |
ganlikun | 0:06036f8bee2d | 52 | None = 0, |
ganlikun | 0:06036f8bee2d | 53 | Odd, |
ganlikun | 0:06036f8bee2d | 54 | Even, |
ganlikun | 0:06036f8bee2d | 55 | Forced1, |
ganlikun | 0:06036f8bee2d | 56 | Forced0 |
ganlikun | 0:06036f8bee2d | 57 | }; |
ganlikun | 0:06036f8bee2d | 58 | |
ganlikun | 0:06036f8bee2d | 59 | enum IrqType { |
ganlikun | 0:06036f8bee2d | 60 | RxIrq = 0, |
ganlikun | 0:06036f8bee2d | 61 | TxIrq, |
ganlikun | 0:06036f8bee2d | 62 | |
ganlikun | 0:06036f8bee2d | 63 | IrqCnt |
ganlikun | 0:06036f8bee2d | 64 | }; |
ganlikun | 0:06036f8bee2d | 65 | |
ganlikun | 0:06036f8bee2d | 66 | enum Flow { |
ganlikun | 0:06036f8bee2d | 67 | Disabled = 0, |
ganlikun | 0:06036f8bee2d | 68 | RTS, |
ganlikun | 0:06036f8bee2d | 69 | CTS, |
ganlikun | 0:06036f8bee2d | 70 | RTSCTS |
ganlikun | 0:06036f8bee2d | 71 | }; |
ganlikun | 0:06036f8bee2d | 72 | |
ganlikun | 0:06036f8bee2d | 73 | /** Set the transmission format used by the serial port |
ganlikun | 0:06036f8bee2d | 74 | * |
ganlikun | 0:06036f8bee2d | 75 | * @param bits The number of bits in a word (5-8; default = 8) |
ganlikun | 0:06036f8bee2d | 76 | * @param parity The parity used (SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0; default = SerialBase::None) |
ganlikun | 0:06036f8bee2d | 77 | * @param stop_bits The number of stop bits (1 or 2; default = 1) |
ganlikun | 0:06036f8bee2d | 78 | */ |
ganlikun | 0:06036f8bee2d | 79 | void format(int bits=8, Parity parity=SerialBase::None, int stop_bits=1); |
ganlikun | 0:06036f8bee2d | 80 | |
ganlikun | 0:06036f8bee2d | 81 | /** Determine if there is a character available to read |
ganlikun | 0:06036f8bee2d | 82 | * |
ganlikun | 0:06036f8bee2d | 83 | * @returns |
ganlikun | 0:06036f8bee2d | 84 | * 1 if there is a character available to read, |
ganlikun | 0:06036f8bee2d | 85 | * 0 otherwise |
ganlikun | 0:06036f8bee2d | 86 | */ |
ganlikun | 0:06036f8bee2d | 87 | int readable(); |
ganlikun | 0:06036f8bee2d | 88 | |
ganlikun | 0:06036f8bee2d | 89 | /** Determine if there is space available to write a character |
ganlikun | 0:06036f8bee2d | 90 | * |
ganlikun | 0:06036f8bee2d | 91 | * @returns |
ganlikun | 0:06036f8bee2d | 92 | * 1 if there is space to write a character, |
ganlikun | 0:06036f8bee2d | 93 | * 0 otherwise |
ganlikun | 0:06036f8bee2d | 94 | */ |
ganlikun | 0:06036f8bee2d | 95 | int writeable(); |
ganlikun | 0:06036f8bee2d | 96 | |
ganlikun | 0:06036f8bee2d | 97 | /** Attach a function to call whenever a serial interrupt is generated |
ganlikun | 0:06036f8bee2d | 98 | * |
ganlikun | 0:06036f8bee2d | 99 | * @param func A pointer to a void function, or 0 to set as none |
ganlikun | 0:06036f8bee2d | 100 | * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
ganlikun | 0:06036f8bee2d | 101 | */ |
ganlikun | 0:06036f8bee2d | 102 | void attach(Callback<void()> func, IrqType type=RxIrq); |
ganlikun | 0:06036f8bee2d | 103 | |
ganlikun | 0:06036f8bee2d | 104 | /** Attach a member function to call whenever a serial interrupt is generated |
ganlikun | 0:06036f8bee2d | 105 | * |
ganlikun | 0:06036f8bee2d | 106 | * @param obj pointer to the object to call the member function on |
ganlikun | 0:06036f8bee2d | 107 | * @param method pointer to the member function to be called |
ganlikun | 0:06036f8bee2d | 108 | * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
ganlikun | 0:06036f8bee2d | 109 | * @deprecated |
ganlikun | 0:06036f8bee2d | 110 | * The attach function does not support cv-qualifiers. Replaced by |
ganlikun | 0:06036f8bee2d | 111 | * attach(callback(obj, method), type). |
ganlikun | 0:06036f8bee2d | 112 | */ |
ganlikun | 0:06036f8bee2d | 113 | template<typename T> |
ganlikun | 0:06036f8bee2d | 114 | MBED_DEPRECATED_SINCE("mbed-os-5.1", |
ganlikun | 0:06036f8bee2d | 115 | "The attach function does not support cv-qualifiers. Replaced by " |
ganlikun | 0:06036f8bee2d | 116 | "attach(callback(obj, method), type).") |
ganlikun | 0:06036f8bee2d | 117 | void attach(T *obj, void (T::*method)(), IrqType type=RxIrq) { |
ganlikun | 0:06036f8bee2d | 118 | attach(callback(obj, method), type); |
ganlikun | 0:06036f8bee2d | 119 | } |
ganlikun | 0:06036f8bee2d | 120 | |
ganlikun | 0:06036f8bee2d | 121 | /** Attach a member function to call whenever a serial interrupt is generated |
ganlikun | 0:06036f8bee2d | 122 | * |
ganlikun | 0:06036f8bee2d | 123 | * @param obj pointer to the object to call the member function on |
ganlikun | 0:06036f8bee2d | 124 | * @param method pointer to the member function to be called |
ganlikun | 0:06036f8bee2d | 125 | * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
ganlikun | 0:06036f8bee2d | 126 | * @deprecated |
ganlikun | 0:06036f8bee2d | 127 | * The attach function does not support cv-qualifiers. Replaced by |
ganlikun | 0:06036f8bee2d | 128 | * attach(callback(obj, method), type). |
ganlikun | 0:06036f8bee2d | 129 | */ |
ganlikun | 0:06036f8bee2d | 130 | template<typename T> |
ganlikun | 0:06036f8bee2d | 131 | MBED_DEPRECATED_SINCE("mbed-os-5.1", |
ganlikun | 0:06036f8bee2d | 132 | "The attach function does not support cv-qualifiers. Replaced by " |
ganlikun | 0:06036f8bee2d | 133 | "attach(callback(obj, method), type).") |
ganlikun | 0:06036f8bee2d | 134 | void attach(T *obj, void (*method)(T*), IrqType type=RxIrq) { |
ganlikun | 0:06036f8bee2d | 135 | attach(callback(obj, method), type); |
ganlikun | 0:06036f8bee2d | 136 | } |
ganlikun | 0:06036f8bee2d | 137 | |
ganlikun | 0:06036f8bee2d | 138 | /** Generate a break condition on the serial line |
ganlikun | 0:06036f8bee2d | 139 | */ |
ganlikun | 0:06036f8bee2d | 140 | void send_break(); |
ganlikun | 0:06036f8bee2d | 141 | |
ganlikun | 0:06036f8bee2d | 142 | protected: |
ganlikun | 0:06036f8bee2d | 143 | |
ganlikun | 0:06036f8bee2d | 144 | /** Acquire exclusive access to this serial port |
ganlikun | 0:06036f8bee2d | 145 | */ |
ganlikun | 0:06036f8bee2d | 146 | virtual void lock(void); |
ganlikun | 0:06036f8bee2d | 147 | |
ganlikun | 0:06036f8bee2d | 148 | /** Release exclusive access to this serial port |
ganlikun | 0:06036f8bee2d | 149 | */ |
ganlikun | 0:06036f8bee2d | 150 | virtual void unlock(void); |
ganlikun | 0:06036f8bee2d | 151 | |
ganlikun | 0:06036f8bee2d | 152 | public: |
ganlikun | 0:06036f8bee2d | 153 | |
ganlikun | 0:06036f8bee2d | 154 | #if DEVICE_SERIAL_FC |
ganlikun | 0:06036f8bee2d | 155 | /** Set the flow control type on the serial port |
ganlikun | 0:06036f8bee2d | 156 | * |
ganlikun | 0:06036f8bee2d | 157 | * @param type the flow control type (Disabled, RTS, CTS, RTSCTS) |
ganlikun | 0:06036f8bee2d | 158 | * @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS) |
ganlikun | 0:06036f8bee2d | 159 | * @param flow2 the second flow control pin (CTS for RTSCTS) |
ganlikun | 0:06036f8bee2d | 160 | */ |
ganlikun | 0:06036f8bee2d | 161 | void set_flow_control(Flow type, PinName flow1=NC, PinName flow2=NC); |
ganlikun | 0:06036f8bee2d | 162 | #endif |
ganlikun | 0:06036f8bee2d | 163 | |
ganlikun | 0:06036f8bee2d | 164 | static void _irq_handler(uint32_t id, SerialIrq irq_type); |
ganlikun | 0:06036f8bee2d | 165 | |
ganlikun | 0:06036f8bee2d | 166 | #if DEVICE_SERIAL_ASYNCH |
ganlikun | 0:06036f8bee2d | 167 | |
ganlikun | 0:06036f8bee2d | 168 | /** Begin asynchronous write using 8bit buffer. The completition invokes registered TX event callback |
ganlikun | 0:06036f8bee2d | 169 | * |
ganlikun | 0:06036f8bee2d | 170 | * This function locks the deep sleep until any event has occured |
ganlikun | 0:06036f8bee2d | 171 | * |
ganlikun | 0:06036f8bee2d | 172 | * @param buffer The buffer where received data will be stored |
ganlikun | 0:06036f8bee2d | 173 | * @param length The buffer length in bytes |
ganlikun | 0:06036f8bee2d | 174 | * @param callback The event callback function |
ganlikun | 0:06036f8bee2d | 175 | * @param event The logical OR of TX events |
ganlikun | 0:06036f8bee2d | 176 | */ |
ganlikun | 0:06036f8bee2d | 177 | int write(const uint8_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_TX_COMPLETE); |
ganlikun | 0:06036f8bee2d | 178 | |
ganlikun | 0:06036f8bee2d | 179 | /** Begin asynchronous write using 16bit buffer. The completition invokes registered TX event callback |
ganlikun | 0:06036f8bee2d | 180 | * |
ganlikun | 0:06036f8bee2d | 181 | * This function locks the deep sleep until any event has occured |
ganlikun | 0:06036f8bee2d | 182 | * |
ganlikun | 0:06036f8bee2d | 183 | * @param buffer The buffer where received data will be stored |
ganlikun | 0:06036f8bee2d | 184 | * @param length The buffer length in bytes |
ganlikun | 0:06036f8bee2d | 185 | * @param callback The event callback function |
ganlikun | 0:06036f8bee2d | 186 | * @param event The logical OR of TX events |
ganlikun | 0:06036f8bee2d | 187 | */ |
ganlikun | 0:06036f8bee2d | 188 | int write(const uint16_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_TX_COMPLETE); |
ganlikun | 0:06036f8bee2d | 189 | |
ganlikun | 0:06036f8bee2d | 190 | /** Abort the on-going write transfer |
ganlikun | 0:06036f8bee2d | 191 | */ |
ganlikun | 0:06036f8bee2d | 192 | void abort_write(); |
ganlikun | 0:06036f8bee2d | 193 | |
ganlikun | 0:06036f8bee2d | 194 | /** Begin asynchronous reading using 8bit buffer. The completition invokes registred RX event callback. |
ganlikun | 0:06036f8bee2d | 195 | * |
ganlikun | 0:06036f8bee2d | 196 | * This function locks the deep sleep until any event has occured |
ganlikun | 0:06036f8bee2d | 197 | * |
ganlikun | 0:06036f8bee2d | 198 | * @param buffer The buffer where received data will be stored |
ganlikun | 0:06036f8bee2d | 199 | * @param length The buffer length in bytes |
ganlikun | 0:06036f8bee2d | 200 | * @param callback The event callback function |
ganlikun | 0:06036f8bee2d | 201 | * @param event The logical OR of RX events |
ganlikun | 0:06036f8bee2d | 202 | * @param char_match The matching character |
ganlikun | 0:06036f8bee2d | 203 | */ |
ganlikun | 0:06036f8bee2d | 204 | int read(uint8_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_RX_COMPLETE, unsigned char char_match = SERIAL_RESERVED_CHAR_MATCH); |
ganlikun | 0:06036f8bee2d | 205 | |
ganlikun | 0:06036f8bee2d | 206 | /** Begin asynchronous reading using 16bit buffer. The completition invokes registred RX event callback. |
ganlikun | 0:06036f8bee2d | 207 | * |
ganlikun | 0:06036f8bee2d | 208 | * This function locks the deep sleep until any event has occured |
ganlikun | 0:06036f8bee2d | 209 | * |
ganlikun | 0:06036f8bee2d | 210 | * @param buffer The buffer where received data will be stored |
ganlikun | 0:06036f8bee2d | 211 | * @param length The buffer length in bytes |
ganlikun | 0:06036f8bee2d | 212 | * @param callback The event callback function |
ganlikun | 0:06036f8bee2d | 213 | * @param event The logical OR of RX events |
ganlikun | 0:06036f8bee2d | 214 | * @param char_match The matching character |
ganlikun | 0:06036f8bee2d | 215 | */ |
ganlikun | 0:06036f8bee2d | 216 | int read(uint16_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_RX_COMPLETE, unsigned char char_match = SERIAL_RESERVED_CHAR_MATCH); |
ganlikun | 0:06036f8bee2d | 217 | |
ganlikun | 0:06036f8bee2d | 218 | /** Abort the on-going read transfer |
ganlikun | 0:06036f8bee2d | 219 | */ |
ganlikun | 0:06036f8bee2d | 220 | void abort_read(); |
ganlikun | 0:06036f8bee2d | 221 | |
ganlikun | 0:06036f8bee2d | 222 | /** Configure DMA usage suggestion for non-blocking TX transfers |
ganlikun | 0:06036f8bee2d | 223 | * |
ganlikun | 0:06036f8bee2d | 224 | * @param usage The usage DMA hint for peripheral |
ganlikun | 0:06036f8bee2d | 225 | * @return Zero if the usage was set, -1 if a transaction is on-going |
ganlikun | 0:06036f8bee2d | 226 | */ |
ganlikun | 0:06036f8bee2d | 227 | int set_dma_usage_tx(DMAUsage usage); |
ganlikun | 0:06036f8bee2d | 228 | |
ganlikun | 0:06036f8bee2d | 229 | /** Configure DMA usage suggestion for non-blocking RX transfers |
ganlikun | 0:06036f8bee2d | 230 | * |
ganlikun | 0:06036f8bee2d | 231 | * @param usage The usage DMA hint for peripheral |
ganlikun | 0:06036f8bee2d | 232 | * @return Zero if the usage was set, -1 if a transaction is on-going |
ganlikun | 0:06036f8bee2d | 233 | */ |
ganlikun | 0:06036f8bee2d | 234 | int set_dma_usage_rx(DMAUsage usage); |
ganlikun | 0:06036f8bee2d | 235 | |
ganlikun | 0:06036f8bee2d | 236 | protected: |
ganlikun | 0:06036f8bee2d | 237 | void start_read(void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event, unsigned char char_match); |
ganlikun | 0:06036f8bee2d | 238 | void start_write(const void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event); |
ganlikun | 0:06036f8bee2d | 239 | void interrupt_handler_asynch(void); |
ganlikun | 0:06036f8bee2d | 240 | #endif |
ganlikun | 0:06036f8bee2d | 241 | |
ganlikun | 0:06036f8bee2d | 242 | protected: |
ganlikun | 0:06036f8bee2d | 243 | SerialBase(PinName tx, PinName rx, int baud); |
ganlikun | 0:06036f8bee2d | 244 | virtual ~SerialBase(); |
ganlikun | 0:06036f8bee2d | 245 | |
ganlikun | 0:06036f8bee2d | 246 | int _base_getc(); |
ganlikun | 0:06036f8bee2d | 247 | int _base_putc(int c); |
ganlikun | 0:06036f8bee2d | 248 | |
ganlikun | 0:06036f8bee2d | 249 | #if DEVICE_SERIAL_ASYNCH |
ganlikun | 0:06036f8bee2d | 250 | CThunk<SerialBase> _thunk_irq; |
ganlikun | 0:06036f8bee2d | 251 | DMAUsage _tx_usage; |
ganlikun | 0:06036f8bee2d | 252 | DMAUsage _rx_usage; |
ganlikun | 0:06036f8bee2d | 253 | event_callback_t _tx_callback; |
ganlikun | 0:06036f8bee2d | 254 | event_callback_t _rx_callback; |
ganlikun | 0:06036f8bee2d | 255 | #endif |
ganlikun | 0:06036f8bee2d | 256 | |
ganlikun | 0:06036f8bee2d | 257 | serial_t _serial; |
ganlikun | 0:06036f8bee2d | 258 | Callback<void()> _irq[IrqCnt]; |
ganlikun | 0:06036f8bee2d | 259 | int _baud; |
ganlikun | 0:06036f8bee2d | 260 | |
ganlikun | 0:06036f8bee2d | 261 | }; |
ganlikun | 0:06036f8bee2d | 262 | |
ganlikun | 0:06036f8bee2d | 263 | } // namespace mbed |
ganlikun | 0:06036f8bee2d | 264 | |
ganlikun | 0:06036f8bee2d | 265 | #endif |
ganlikun | 0:06036f8bee2d | 266 | |
ganlikun | 0:06036f8bee2d | 267 | #endif |
ganlikun | 0:06036f8bee2d | 268 |