Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

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