mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

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

Committer:
bogdanm
Date:
Wed Aug 07 16:43:59 2013 +0300
Revision:
15:4892fe388435
Parent:
13:0645d8841f51
Child:
36:ab3ee77451e7
Added LPC4088 target and interrupt chaining code

Who changed what in which revision?

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