Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:27:58 2016 +0000
Revision:
0:6c56fb4bc5f0
Moving to library for sharing updates

Who changed what in which revision?

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