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