SPKT

Dependencies:   F746_GUI SD_PlayerSkeleton F746_SAI_IO

Committer:
phungductung
Date:
Tue Jun 04 21:37:21 2019 +0000
Revision:
0:8ede47d38d10
SPKT

Who changed what in which revision?

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