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