Backup 1

Committer:
borlanic
Date:
Tue Apr 24 11:45:18 2018 +0000
Revision:
0:02dd72d1d465
BaBoRo_test2 - backup 1

Who changed what in which revision?

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