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
Parent:
188:bcfe06ba3d64
mbed library release version 165

Who changed what in which revision?

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