00

Committer:
ganlikun
Date:
Sun Jun 12 14:02:44 2022 +0000
Revision:
0:13413ea9a877
00

Who changed what in which revision?

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