inport from local

Dependents:   Hobbyking_Cheetah_0511

Committer:
NYX
Date:
Mon Mar 16 06:35:48 2020 +0000
Revision:
0:85b3fd62ea1a
reinport to mbed;

Who changed what in which revision?

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