mbed library for slider v2

Dependents:   kl46z_slider_v2

Committer:
mturner5
Date:
Wed Sep 14 07:04:27 2016 +0000
Revision:
0:b7116bd48af6
Tried to use the timer.

Who changed what in which revision?

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