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

Fork of mbed by mbed official

Committer:
bogdanm
Date:
Mon Aug 12 13:17:46 2013 +0300
Revision:
65:5798e58a58b1
Parent:
64:e3affc9e7238
Child:
66:9c8f0e3462fb
New target (LPC4088), new features (interrupt chaining), bug fixes (KL25Z I2C).

Who changed what in which revision?

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