mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
AnnaBridge
Date:
Wed Feb 20 22:31:08 2019 +0000
Revision:
189:f392fc9709a3
mbed library release version 165

Who changed what in which revision?

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