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