.

Dependents:   RTC

Committer:
jhon309
Date:
Thu Aug 13 00:20:09 2015 +0000
Revision:
0:88e313c910d0
RTC Example

Who changed what in which revision?

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