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