Committer:
borlanic
Date:
Fri Mar 30 14:07:05 2018 +0000
Revision:
4:75df35ef4fb6
Parent:
0:380207fcb5c1
commentar

Who changed what in which revision?

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