The official mbed C/C SDK provides the software platform and libraries to build your applications.

Fork of mbed by mbed official

Committer:
Mikchel
Date:
Sun May 03 16:04:42 2015 +0000
Revision:
99:7f6c6de930c0
Parent:
98:8ab26030e058
12

Who changed what in which revision?

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