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