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