test

Dependencies:   mbed Watchdog

Dependents:   STM32-MC_node

Committer:
ommpy
Date:
Mon Jul 06 17:18:59 2020 +0530
Revision:
0:d383e2dee0f7
first commit

Who changed what in which revision?

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