,,

Fork of Application by Daniel Sygut

Committer:
Zaitsev
Date:
Thu Feb 15 14:29:23 2018 +0000
Revision:
15:2a20c3d2616e
Parent:
10:41552d038a69
j

Who changed what in which revision?

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