mbed library sources. Supersedes mbed-src.

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

Committer:
AnnaBridge
Date:
Thu Nov 08 11:46:34 2018 +0000
Revision:
188:bcfe06ba3d64
Parent:
187:0387e8f68319
Child:
189:f392fc9709a3
mbed-dev library. Release version 164

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