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