The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

mbed 2

This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.

Committer:
AnnaBridge
Date:
Wed Feb 20 20:53:29 2019 +0000
Revision:
172:65be27845400
Parent:
171:3a7713b1edbc
mbed library release version 165

Who changed what in which revision?

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