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