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