dhgdh

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by joey shelton

Committer:
nexpaq
Date:
Sat Sep 17 16:32:05 2016 +0000
Revision:
1:55a6170b404f
checking in for sharing

Who changed what in which revision?

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