...

Dependents:   2doejemplo Labo_TRSE_Drone

Fork of mbed by mbed official

Committer:
bogdanm
Date:
Mon Aug 19 13:34:54 2013 +0300
Revision:
66:9c8f0e3462fb
Parent:
65:5798e58a58b1
Child:
68:f37f3b9c9f0b
New mbed library build with support for LPC1114.

Built from github tag 'mbed_lib_rev66'

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bogdanm 66:9c8f0e3462fb 1 /* mbed Microcontroller Library
bogdanm 66:9c8f0e3462fb 2 * Copyright (c) 2006-2013 ARM Limited
bogdanm 66:9c8f0e3462fb 3 *
bogdanm 66:9c8f0e3462fb 4 * Licensed under the Apache License, Version 2.0 (the "License");
bogdanm 66:9c8f0e3462fb 5 * you may not use this file except in compliance with the License.
bogdanm 66:9c8f0e3462fb 6 * You may obtain a copy of the License at
bogdanm 66:9c8f0e3462fb 7 *
bogdanm 66:9c8f0e3462fb 8 * http://www.apache.org/licenses/LICENSE-2.0
bogdanm 66:9c8f0e3462fb 9 *
bogdanm 66:9c8f0e3462fb 10 * Unless required by applicable law or agreed to in writing, software
bogdanm 66:9c8f0e3462fb 11 * distributed under the License is distributed on an "AS IS" BASIS,
bogdanm 66:9c8f0e3462fb 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
bogdanm 66:9c8f0e3462fb 13 * See the License for the specific language governing permissions and
bogdanm 66:9c8f0e3462fb 14 * limitations under the License.
bogdanm 66:9c8f0e3462fb 15 */
bogdanm 66:9c8f0e3462fb 16 #ifndef MBED_SERIAL_H
bogdanm 66:9c8f0e3462fb 17 #define MBED_SERIAL_H
bogdanm 66:9c8f0e3462fb 18
bogdanm 66:9c8f0e3462fb 19 #include "platform.h"
bogdanm 66:9c8f0e3462fb 20
bogdanm 66:9c8f0e3462fb 21 #if DEVICE_SERIAL
bogdanm 66:9c8f0e3462fb 22
bogdanm 66:9c8f0e3462fb 23 #include "Stream.h"
bogdanm 66:9c8f0e3462fb 24 #include "FunctionPointer.h"
bogdanm 66:9c8f0e3462fb 25 #include "serial_api.h"
bogdanm 66:9c8f0e3462fb 26 #include "CallChain.h"
bogdanm 66:9c8f0e3462fb 27
bogdanm 66:9c8f0e3462fb 28 namespace mbed {
bogdanm 66:9c8f0e3462fb 29
bogdanm 66:9c8f0e3462fb 30 /** A serial port (UART) for communication with other serial devices
bogdanm 66:9c8f0e3462fb 31 *
bogdanm 66:9c8f0e3462fb 32 * Can be used for Full Duplex communication, or Simplex by specifying
bogdanm 66:9c8f0e3462fb 33 * one pin as NC (Not Connected)
bogdanm 66:9c8f0e3462fb 34 *
bogdanm 66:9c8f0e3462fb 35 * Example:
bogdanm 66:9c8f0e3462fb 36 * @code
bogdanm 66:9c8f0e3462fb 37 * // Print "Hello World" to the PC
bogdanm 66:9c8f0e3462fb 38 *
bogdanm 66:9c8f0e3462fb 39 * #include "mbed.h"
bogdanm 66:9c8f0e3462fb 40 *
bogdanm 66:9c8f0e3462fb 41 * Serial pc(USBTX, USBRX);
bogdanm 66:9c8f0e3462fb 42 *
bogdanm 66:9c8f0e3462fb 43 * int main() {
bogdanm 66:9c8f0e3462fb 44 * pc.printf("Hello World\n");
bogdanm 66:9c8f0e3462fb 45 * }
bogdanm 66:9c8f0e3462fb 46 * @endcode
bogdanm 66:9c8f0e3462fb 47 */
bogdanm 66:9c8f0e3462fb 48 class Serial : public Stream {
bogdanm 66:9c8f0e3462fb 49
bogdanm 66:9c8f0e3462fb 50 public:
bogdanm 66:9c8f0e3462fb 51 /** Create a Serial port, connected to the specified transmit and receive pins
bogdanm 66:9c8f0e3462fb 52 *
bogdanm 66:9c8f0e3462fb 53 * @param tx Transmit pin
bogdanm 66:9c8f0e3462fb 54 * @param rx Receive pin
bogdanm 66:9c8f0e3462fb 55 *
bogdanm 66:9c8f0e3462fb 56 * @note
bogdanm 66:9c8f0e3462fb 57 * Either tx or rx may be specified as NC if unused
bogdanm 66:9c8f0e3462fb 58 */
bogdanm 66:9c8f0e3462fb 59 Serial(PinName tx, PinName rx, const char *name=NULL);
bogdanm 66:9c8f0e3462fb 60
bogdanm 66:9c8f0e3462fb 61 /** Set the baud rate of the serial port
bogdanm 66:9c8f0e3462fb 62 *
bogdanm 66:9c8f0e3462fb 63 * @param baudrate The baudrate of the serial port (default = 9600).
bogdanm 66:9c8f0e3462fb 64 */
bogdanm 66:9c8f0e3462fb 65 void baud(int baudrate);
bogdanm 66:9c8f0e3462fb 66
bogdanm 66:9c8f0e3462fb 67 enum Parity {
bogdanm 66:9c8f0e3462fb 68 None = 0,
bogdanm 66:9c8f0e3462fb 69 Odd,
bogdanm 66:9c8f0e3462fb 70 Even,
bogdanm 66:9c8f0e3462fb 71 Forced1,
bogdanm 66:9c8f0e3462fb 72 Forced0
bogdanm 66:9c8f0e3462fb 73 };
bogdanm 66:9c8f0e3462fb 74
bogdanm 66:9c8f0e3462fb 75 enum IrqType {
bogdanm 66:9c8f0e3462fb 76 RxIrq = 0,
bogdanm 66:9c8f0e3462fb 77 TxIrq
bogdanm 66:9c8f0e3462fb 78 };
bogdanm 66:9c8f0e3462fb 79
bogdanm 66:9c8f0e3462fb 80 /** Set the transmission format used by the Serial port
bogdanm 66:9c8f0e3462fb 81 *
bogdanm 66:9c8f0e3462fb 82 * @param bits The number of bits in a word (5-8; default = 8)
bogdanm 66:9c8f0e3462fb 83 * @param parity The parity used (Serial::None, Serial::Odd, Serial::Even, Serial::Forced1, Serial::Forced0; default = Serial::None)
bogdanm 66:9c8f0e3462fb 84 * @param stop The number of stop bits (1 or 2; default = 1)
bogdanm 66:9c8f0e3462fb 85 */
bogdanm 66:9c8f0e3462fb 86 void format(int bits=8, Parity parity=Serial::None, int stop_bits=1);
bogdanm 66:9c8f0e3462fb 87
bogdanm 66:9c8f0e3462fb 88 /** Determine if there is a character available to read
bogdanm 66:9c8f0e3462fb 89 *
bogdanm 66:9c8f0e3462fb 90 * @returns
bogdanm 66:9c8f0e3462fb 91 * 1 if there is a character available to read,
bogdanm 66:9c8f0e3462fb 92 * 0 otherwise
bogdanm 66:9c8f0e3462fb 93 */
bogdanm 66:9c8f0e3462fb 94 int readable();
bogdanm 66:9c8f0e3462fb 95
bogdanm 66:9c8f0e3462fb 96 /** Determine if there is space available to write a character
bogdanm 66:9c8f0e3462fb 97 *
bogdanm 66:9c8f0e3462fb 98 * @returns
bogdanm 66:9c8f0e3462fb 99 * 1 if there is space to write a character,
bogdanm 66:9c8f0e3462fb 100 * 0 otherwise
bogdanm 66:9c8f0e3462fb 101 */
bogdanm 66:9c8f0e3462fb 102 int writeable();
bogdanm 66:9c8f0e3462fb 103
bogdanm 66:9c8f0e3462fb 104 /** Attach a function to call whenever a serial interrupt is generated
bogdanm 66:9c8f0e3462fb 105 *
bogdanm 66:9c8f0e3462fb 106 * @param fptr A pointer to a void function, or 0 to set as none
bogdanm 66:9c8f0e3462fb 107 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
bogdanm 66:9c8f0e3462fb 108 *
bogdanm 66:9c8f0e3462fb 109 * @returns
bogdanm 66:9c8f0e3462fb 110 * The function object created for 'fptr'
bogdanm 66:9c8f0e3462fb 111 */
bogdanm 66:9c8f0e3462fb 112 pFunctionPointer_t attach(void (*fptr)(void), IrqType type=RxIrq);
bogdanm 66:9c8f0e3462fb 113
bogdanm 66:9c8f0e3462fb 114 /** Add a function to be called when a serial interrupt is generated at the end of the call chain
bogdanm 66:9c8f0e3462fb 115 *
bogdanm 66:9c8f0e3462fb 116 * @param fptr the function to add
bogdanm 66:9c8f0e3462fb 117 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
bogdanm 66:9c8f0e3462fb 118 *
bogdanm 66:9c8f0e3462fb 119 * @returns
bogdanm 66:9c8f0e3462fb 120 * The function object created for 'fptr'
bogdanm 66:9c8f0e3462fb 121 */
bogdanm 66:9c8f0e3462fb 122 pFunctionPointer_t add_handler(void (*fptr)(void), IrqType type=RxIrq) {
bogdanm 66:9c8f0e3462fb 123 return add_handler_helper(fptr, type);
bogdanm 66:9c8f0e3462fb 124 }
bogdanm 66:9c8f0e3462fb 125
bogdanm 66:9c8f0e3462fb 126 /** Add a function to be called when a serial interrupt is generated at the beginning of the call chain
bogdanm 66:9c8f0e3462fb 127 *
bogdanm 66:9c8f0e3462fb 128 * @param fptr the function to add
bogdanm 66:9c8f0e3462fb 129 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
bogdanm 66:9c8f0e3462fb 130 *
bogdanm 66:9c8f0e3462fb 131 * @returns
bogdanm 66:9c8f0e3462fb 132 * The function object created for 'fptr'
bogdanm 66:9c8f0e3462fb 133 */
bogdanm 66:9c8f0e3462fb 134 pFunctionPointer_t add_handler_front(void (*fptr)(void), IrqType type=RxIrq) {
bogdanm 66:9c8f0e3462fb 135 return add_handler_helper(fptr, type, true);
bogdanm 66:9c8f0e3462fb 136 }
bogdanm 66:9c8f0e3462fb 137
bogdanm 66:9c8f0e3462fb 138 /** Attach a member function to call whenever a serial interrupt is generated
bogdanm 66:9c8f0e3462fb 139 *
bogdanm 66:9c8f0e3462fb 140 * @param tptr pointer to the object to call the member function on
bogdanm 66:9c8f0e3462fb 141 * @param mptr pointer to the member function to be called
bogdanm 66:9c8f0e3462fb 142 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
bogdanm 66:9c8f0e3462fb 143 *
bogdanm 66:9c8f0e3462fb 144 * @param
bogdanm 66:9c8f0e3462fb 145 * The function object created for 'tptr' and 'mptr'
bogdanm 66:9c8f0e3462fb 146 */
bogdanm 66:9c8f0e3462fb 147 template<typename T>
bogdanm 66:9c8f0e3462fb 148 pFunctionPointer_t attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) {
bogdanm 66:9c8f0e3462fb 149 if((mptr != NULL) && (tptr != NULL)) {
bogdanm 66:9c8f0e3462fb 150 _irq[type].clear();
bogdanm 66:9c8f0e3462fb 151 pFunctionPointer_t pf = _irq[type].add(tptr, mptr);
bogdanm 66:9c8f0e3462fb 152 serial_irq_set(&_serial, (SerialIrq)type, 1);
bogdanm 66:9c8f0e3462fb 153 return pf;
bogdanm 66:9c8f0e3462fb 154 }
bogdanm 66:9c8f0e3462fb 155 else
bogdanm 66:9c8f0e3462fb 156 return NULL;
bogdanm 66:9c8f0e3462fb 157 }
bogdanm 66:9c8f0e3462fb 158
bogdanm 66:9c8f0e3462fb 159 /** Add a function to be called when a serial interrupt is generated at the end of the call chain
bogdanm 66:9c8f0e3462fb 160 *
bogdanm 66:9c8f0e3462fb 161 * @param tptr pointer to the object to call the member function on
bogdanm 66:9c8f0e3462fb 162 * @param mptr pointer to the member function to be called
bogdanm 66:9c8f0e3462fb 163 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
bogdanm 66:9c8f0e3462fb 164 *
bogdanm 66:9c8f0e3462fb 165 * @returns
bogdanm 66:9c8f0e3462fb 166 * The function object created for 'fptr'
bogdanm 66:9c8f0e3462fb 167 */
bogdanm 66:9c8f0e3462fb 168 template<typename T>
bogdanm 66:9c8f0e3462fb 169 pFunctionPointer_t add_handler(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) {
bogdanm 66:9c8f0e3462fb 170 return add_handler_helper(tptr, mptr, type);
bogdanm 66:9c8f0e3462fb 171 }
bogdanm 66:9c8f0e3462fb 172
bogdanm 66:9c8f0e3462fb 173 /** Add a function to be called when a serial interrupt is generated at the beginning of the call chain
bogdanm 66:9c8f0e3462fb 174 *
bogdanm 66:9c8f0e3462fb 175 * @param tptr pointer to the object to call the member function on
bogdanm 66:9c8f0e3462fb 176 * @param mptr pointer to the member function to be called
bogdanm 66:9c8f0e3462fb 177 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
bogdanm 66:9c8f0e3462fb 178 *
bogdanm 66:9c8f0e3462fb 179 * @returns
bogdanm 66:9c8f0e3462fb 180 * The function object created for 'fptr'
bogdanm 66:9c8f0e3462fb 181 */
bogdanm 66:9c8f0e3462fb 182 template<typename T>
bogdanm 66:9c8f0e3462fb 183 pFunctionPointer_t add_handler_front(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) {
bogdanm 66:9c8f0e3462fb 184 return add_handler_helper(tptr, mptr, type, true);
bogdanm 66:9c8f0e3462fb 185 }
bogdanm 66:9c8f0e3462fb 186
bogdanm 66:9c8f0e3462fb 187 /** Remove a function from the list of functions to be called when a serial interrupt is generated
bogdanm 66:9c8f0e3462fb 188 *
bogdanm 66:9c8f0e3462fb 189 * @param pf the function object to remove
bogdanm 66:9c8f0e3462fb 190 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
bogdanm 66:9c8f0e3462fb 191 *
bogdanm 66:9c8f0e3462fb 192 * @returns
bogdanm 66:9c8f0e3462fb 193 * true if the function was found and removed, false otherwise
bogdanm 66:9c8f0e3462fb 194 */
bogdanm 66:9c8f0e3462fb 195 bool remove_handler(pFunctionPointer_t pf, IrqType type=RxIrq);
bogdanm 66:9c8f0e3462fb 196
bogdanm 66:9c8f0e3462fb 197 /** Generate a break condition on the serial line
bogdanm 66:9c8f0e3462fb 198 */
bogdanm 66:9c8f0e3462fb 199 void send_break();
bogdanm 66:9c8f0e3462fb 200
bogdanm 66:9c8f0e3462fb 201 static void _irq_handler(uint32_t id, SerialIrq irq_type);
bogdanm 66:9c8f0e3462fb 202
bogdanm 66:9c8f0e3462fb 203 protected:
bogdanm 66:9c8f0e3462fb 204 virtual int _getc();
bogdanm 66:9c8f0e3462fb 205 virtual int _putc(int c);
bogdanm 66:9c8f0e3462fb 206 pFunctionPointer_t add_handler_helper(void (*function)(void), IrqType type, bool front=false);
bogdanm 66:9c8f0e3462fb 207
bogdanm 66:9c8f0e3462fb 208 template<typename T>
bogdanm 66:9c8f0e3462fb 209 pFunctionPointer_t add_handler_helper(T* tptr, void (T::*mptr)(void), IrqType type, bool front=false) {
bogdanm 66:9c8f0e3462fb 210 if ((mptr != NULL) && (tptr != NULL)) {
bogdanm 66:9c8f0e3462fb 211 pFunctionPointer_t pf = front ? _irq[type].add_front(tptr, mptr) : _irq[type].add(tptr, mptr);
bogdanm 66:9c8f0e3462fb 212 serial_irq_set(&_serial, (SerialIrq)type, 1);
bogdanm 66:9c8f0e3462fb 213 return pf;
bogdanm 66:9c8f0e3462fb 214 }
bogdanm 66:9c8f0e3462fb 215 else
bogdanm 66:9c8f0e3462fb 216 return NULL;
bogdanm 66:9c8f0e3462fb 217 }
bogdanm 66:9c8f0e3462fb 218
bogdanm 66:9c8f0e3462fb 219 serial_t _serial;
bogdanm 66:9c8f0e3462fb 220 CallChain _irq[2];
bogdanm 66:9c8f0e3462fb 221 int _baud;
bogdanm 66:9c8f0e3462fb 222 };
bogdanm 66:9c8f0e3462fb 223
bogdanm 66:9c8f0e3462fb 224 } // namespace mbed
bogdanm 66:9c8f0e3462fb 225
bogdanm 66:9c8f0e3462fb 226 #endif
bogdanm 66:9c8f0e3462fb 227
bogdanm 66:9c8f0e3462fb 228 #endif