TUKS MCU Introductory course / TUKS-COURSE-TIMER
Committer:
elmot
Date:
Fri Feb 24 21:13:56 2017 +0000
Revision:
1:d0dfbce63a89
Ready-to-copy

Who changed what in which revision?

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