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