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