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:
Kojto
Date:
Wed Jul 19 16:46:19 2017 +0100
Revision:
147:a97add6d7e64
Parent:
146:22da6e220af6
Child:
152:235179ab3f27
Release 147 of the mbed library.

Who changed what in which revision?

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