4545
Fork of mbed-dev by
drivers/SerialBase.h@149:156823d33999, 2016-10-28 (annotated)
- Committer:
- <>
- Date:
- Fri Oct 28 11:17:30 2016 +0100
- Revision:
- 149:156823d33999
- Child:
- 160:d5399cc887bb
This updates the lib to the mbed lib v128
NOTE: This release includes a restructuring of the file and directory locations and thus some
include paths in your code may need updating accordingly.
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 | |
<> | 149:156823d33999 | 21 | #if DEVICE_SERIAL |
<> | 149:156823d33999 | 22 | |
<> | 149:156823d33999 | 23 | #include "Stream.h" |
<> | 149:156823d33999 | 24 | #include "Callback.h" |
<> | 149:156823d33999 | 25 | #include "serial_api.h" |
<> | 149:156823d33999 | 26 | #include "toolchain.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 | |
<> | 149:156823d33999 | 37 | /** A base class for serial port implementations |
<> | 149:156823d33999 | 38 | * Can't be instantiated directly (use Serial or RawSerial) |
<> | 149:156823d33999 | 39 | * |
<> | 149:156823d33999 | 40 | * @Note Synchronization level: Set by subclass |
<> | 149:156823d33999 | 41 | */ |
<> | 149:156823d33999 | 42 | class 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) |
<> | 149:156823d33999 | 77 | * @param stop 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 | * |
<> | 149:156823d33999 | 170 | * @param buffer The buffer where received data will be stored |
<> | 149:156823d33999 | 171 | * @param length The buffer length in bytes |
<> | 149:156823d33999 | 172 | * @param callback The event callback function |
<> | 149:156823d33999 | 173 | * @param event The logical OR of TX events |
<> | 149:156823d33999 | 174 | */ |
<> | 149:156823d33999 | 175 | int write(const uint8_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_TX_COMPLETE); |
<> | 149:156823d33999 | 176 | |
<> | 149:156823d33999 | 177 | /** Begin asynchronous write using 16bit buffer. The completition invokes registered TX event callback |
<> | 149:156823d33999 | 178 | * |
<> | 149:156823d33999 | 179 | * @param buffer The buffer where received data will be stored |
<> | 149:156823d33999 | 180 | * @param length The buffer length in bytes |
<> | 149:156823d33999 | 181 | * @param callback The event callback function |
<> | 149:156823d33999 | 182 | * @param event The logical OR of TX events |
<> | 149:156823d33999 | 183 | */ |
<> | 149:156823d33999 | 184 | int write(const uint16_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_TX_COMPLETE); |
<> | 149:156823d33999 | 185 | |
<> | 149:156823d33999 | 186 | /** Abort the on-going write transfer |
<> | 149:156823d33999 | 187 | */ |
<> | 149:156823d33999 | 188 | void abort_write(); |
<> | 149:156823d33999 | 189 | |
<> | 149:156823d33999 | 190 | /** Begin asynchronous reading using 8bit buffer. The completition invokes registred RX event callback. |
<> | 149:156823d33999 | 191 | * |
<> | 149:156823d33999 | 192 | * @param buffer The buffer where received data will be stored |
<> | 149:156823d33999 | 193 | * @param length The buffer length in bytes |
<> | 149:156823d33999 | 194 | * @param callback The event callback function |
<> | 149:156823d33999 | 195 | * @param event The logical OR of RX events |
<> | 149:156823d33999 | 196 | * @param char_match The matching character |
<> | 149:156823d33999 | 197 | */ |
<> | 149:156823d33999 | 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); |
<> | 149:156823d33999 | 199 | |
<> | 149:156823d33999 | 200 | /** Begin asynchronous reading using 16bit buffer. The completition invokes registred RX event callback. |
<> | 149:156823d33999 | 201 | * |
<> | 149:156823d33999 | 202 | * @param buffer The buffer where received data will be stored |
<> | 149:156823d33999 | 203 | * @param length The buffer length in bytes |
<> | 149:156823d33999 | 204 | * @param callback The event callback function |
<> | 149:156823d33999 | 205 | * @param event The logical OR of RX events |
<> | 149:156823d33999 | 206 | * @param char_match The matching character |
<> | 149:156823d33999 | 207 | */ |
<> | 149:156823d33999 | 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); |
<> | 149:156823d33999 | 209 | |
<> | 149:156823d33999 | 210 | /** Abort the on-going read transfer |
<> | 149:156823d33999 | 211 | */ |
<> | 149:156823d33999 | 212 | void abort_read(); |
<> | 149:156823d33999 | 213 | |
<> | 149:156823d33999 | 214 | /** Configure DMA usage suggestion for non-blocking TX transfers |
<> | 149:156823d33999 | 215 | * |
<> | 149:156823d33999 | 216 | * @param usage The usage DMA hint for peripheral |
<> | 149:156823d33999 | 217 | * @return Zero if the usage was set, -1 if a transaction is on-going |
<> | 149:156823d33999 | 218 | */ |
<> | 149:156823d33999 | 219 | int set_dma_usage_tx(DMAUsage usage); |
<> | 149:156823d33999 | 220 | |
<> | 149:156823d33999 | 221 | /** Configure DMA usage suggestion for non-blocking RX transfers |
<> | 149:156823d33999 | 222 | * |
<> | 149:156823d33999 | 223 | * @param usage The usage DMA hint for peripheral |
<> | 149:156823d33999 | 224 | * @return Zero if the usage was set, -1 if a transaction is on-going |
<> | 149:156823d33999 | 225 | */ |
<> | 149:156823d33999 | 226 | int set_dma_usage_rx(DMAUsage usage); |
<> | 149:156823d33999 | 227 | |
<> | 149:156823d33999 | 228 | protected: |
<> | 149:156823d33999 | 229 | void start_read(void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event, unsigned char char_match); |
<> | 149:156823d33999 | 230 | void start_write(const void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event); |
<> | 149:156823d33999 | 231 | void interrupt_handler_asynch(void); |
<> | 149:156823d33999 | 232 | #endif |
<> | 149:156823d33999 | 233 | |
<> | 149:156823d33999 | 234 | protected: |
<> | 149:156823d33999 | 235 | SerialBase(PinName tx, PinName rx, int baud); |
<> | 149:156823d33999 | 236 | virtual ~SerialBase() { |
<> | 149:156823d33999 | 237 | } |
<> | 149:156823d33999 | 238 | |
<> | 149:156823d33999 | 239 | int _base_getc(); |
<> | 149:156823d33999 | 240 | int _base_putc(int c); |
<> | 149:156823d33999 | 241 | |
<> | 149:156823d33999 | 242 | #if DEVICE_SERIAL_ASYNCH |
<> | 149:156823d33999 | 243 | CThunk<SerialBase> _thunk_irq; |
<> | 149:156823d33999 | 244 | event_callback_t _tx_callback; |
<> | 149:156823d33999 | 245 | event_callback_t _rx_callback; |
<> | 149:156823d33999 | 246 | DMAUsage _tx_usage; |
<> | 149:156823d33999 | 247 | DMAUsage _rx_usage; |
<> | 149:156823d33999 | 248 | #endif |
<> | 149:156823d33999 | 249 | |
<> | 149:156823d33999 | 250 | serial_t _serial; |
<> | 149:156823d33999 | 251 | Callback<void()> _irq[IrqCnt]; |
<> | 149:156823d33999 | 252 | int _baud; |
<> | 149:156823d33999 | 253 | |
<> | 149:156823d33999 | 254 | }; |
<> | 149:156823d33999 | 255 | |
<> | 149:156823d33999 | 256 | } // namespace mbed |
<> | 149:156823d33999 | 257 | |
<> | 149:156823d33999 | 258 | #endif |
<> | 149:156823d33999 | 259 | |
<> | 149:156823d33999 | 260 | #endif |
<> | 149:156823d33999 | 261 | |
<> | 149:156823d33999 | 262 | /** @}*/ |