Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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