Dataloger

Committer:
jhon309
Date:
Thu Aug 20 00:37:14 2015 +0000
Revision:
0:666850d06c9f
ok

Who changed what in which revision?

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