mbed libraries for KL25Z

Dependents:   FRDM_RGBLED

Committer:
emilmont
Date:
Mon Feb 18 09:41:56 2013 +0000
Revision:
9:663789d7729f
Parent:
8:c14af7958ef5
Update mbed-KL25Z to latest build

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emilmont 9:663789d7729f 1 /* mbed Microcontroller Library
emilmont 9:663789d7729f 2 * Copyright (c) 2006-2013 ARM Limited
emilmont 9:663789d7729f 3 *
emilmont 9:663789d7729f 4 * Licensed under the Apache License, Version 2.0 (the "License");
emilmont 9:663789d7729f 5 * you may not use this file except in compliance with the License.
emilmont 9:663789d7729f 6 * You may obtain a copy of the License at
emilmont 9:663789d7729f 7 *
emilmont 9:663789d7729f 8 * http://www.apache.org/licenses/LICENSE-2.0
emilmont 9:663789d7729f 9 *
emilmont 9:663789d7729f 10 * Unless required by applicable law or agreed to in writing, software
emilmont 9:663789d7729f 11 * distributed under the License is distributed on an "AS IS" BASIS,
emilmont 9:663789d7729f 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
emilmont 9:663789d7729f 13 * See the License for the specific language governing permissions and
emilmont 9:663789d7729f 14 * limitations under the License.
emilmont 9:663789d7729f 15 */
emilmont 9:663789d7729f 16 #ifndef MBED_SERIAL_H
emilmont 9:663789d7729f 17 #define MBED_SERIAL_H
emilmont 9:663789d7729f 18
emilmont 9:663789d7729f 19 #include "platform.h"
emilmont 9:663789d7729f 20
emilmont 9:663789d7729f 21 #if DEVICE_SERIAL
emilmont 9:663789d7729f 22
emilmont 9:663789d7729f 23 #include "Stream.h"
emilmont 9:663789d7729f 24 #include "FunctionPointer.h"
emilmont 9:663789d7729f 25 #include "serial_api.h"
emilmont 9:663789d7729f 26
emilmont 9:663789d7729f 27 namespace mbed {
emilmont 9:663789d7729f 28
emilmont 9:663789d7729f 29 /** A serial port (UART) for communication with other serial devices
emilmont 9:663789d7729f 30 *
emilmont 9:663789d7729f 31 * Can be used for Full Duplex communication, or Simplex by specifying
emilmont 9:663789d7729f 32 * one pin as NC (Not Connected)
emilmont 9:663789d7729f 33 *
emilmont 9:663789d7729f 34 * Example:
emilmont 9:663789d7729f 35 * @code
emilmont 9:663789d7729f 36 * // Print "Hello World" to the PC
emilmont 9:663789d7729f 37 *
emilmont 9:663789d7729f 38 * #include "mbed.h"
emilmont 9:663789d7729f 39 *
emilmont 9:663789d7729f 40 * Serial pc(USBTX, USBRX);
emilmont 9:663789d7729f 41 *
emilmont 9:663789d7729f 42 * int main() {
emilmont 9:663789d7729f 43 * pc.printf("Hello World\n");
emilmont 9:663789d7729f 44 * }
emilmont 9:663789d7729f 45 * @endcode
emilmont 9:663789d7729f 46 */
emilmont 9:663789d7729f 47 class Serial : public Stream {
emilmont 9:663789d7729f 48
emilmont 9:663789d7729f 49 public:
emilmont 9:663789d7729f 50 /** Create a Serial port, connected to the specified transmit and receive pins
emilmont 9:663789d7729f 51 *
emilmont 9:663789d7729f 52 * @param tx Transmit pin
emilmont 9:663789d7729f 53 * @param rx Receive pin
emilmont 9:663789d7729f 54 *
emilmont 9:663789d7729f 55 * @note
emilmont 9:663789d7729f 56 * Either tx or rx may be specified as NC if unused
emilmont 9:663789d7729f 57 */
emilmont 9:663789d7729f 58 Serial(PinName tx, PinName rx, const char *name=NULL);
emilmont 9:663789d7729f 59
emilmont 9:663789d7729f 60 /** Set the baud rate of the serial port
emilmont 9:663789d7729f 61 *
emilmont 9:663789d7729f 62 * @param baudrate The baudrate of the serial port (default = 9600).
emilmont 9:663789d7729f 63 */
emilmont 9:663789d7729f 64 void baud(int baudrate);
emilmont 9:663789d7729f 65
emilmont 9:663789d7729f 66 enum Parity {
emilmont 9:663789d7729f 67 None = 0,
emilmont 9:663789d7729f 68 Odd,
emilmont 9:663789d7729f 69 Even,
emilmont 9:663789d7729f 70 Forced1,
emilmont 9:663789d7729f 71 Forced0
emilmont 9:663789d7729f 72 };
emilmont 9:663789d7729f 73
emilmont 9:663789d7729f 74 enum IrqType {
emilmont 9:663789d7729f 75 RxIrq = 0,
emilmont 9:663789d7729f 76 TxIrq
emilmont 9:663789d7729f 77 };
emilmont 9:663789d7729f 78
emilmont 9:663789d7729f 79 /** Set the transmission format used by the Serial port
emilmont 9:663789d7729f 80 *
emilmont 9:663789d7729f 81 * @param bits The number of bits in a word (5-8; default = 8)
emilmont 9:663789d7729f 82 * @param parity The parity used (Serial::None, Serial::Odd, Serial::Even, Serial::Forced1, Serial::Forced0; default = Serial::None)
emilmont 9:663789d7729f 83 * @param stop The number of stop bits (1 or 2; default = 1)
emilmont 9:663789d7729f 84 */
emilmont 9:663789d7729f 85 void format(int bits = 8, Parity parity=Serial::None, int stop_bits=1);
emilmont 9:663789d7729f 86
emilmont 9:663789d7729f 87 /** Determine if there is a character available to read
emilmont 9:663789d7729f 88 *
emilmont 9:663789d7729f 89 * @returns
emilmont 9:663789d7729f 90 * 1 if there is a character available to read,
emilmont 9:663789d7729f 91 * 0 otherwise
emilmont 9:663789d7729f 92 */
emilmont 9:663789d7729f 93 int readable();
emilmont 9:663789d7729f 94
emilmont 9:663789d7729f 95 /** Determine if there is space available to write a character
emilmont 9:663789d7729f 96 *
emilmont 9:663789d7729f 97 * @returns
emilmont 9:663789d7729f 98 * 1 if there is space to write a character,
emilmont 9:663789d7729f 99 * 0 otherwise
emilmont 9:663789d7729f 100 */
emilmont 9:663789d7729f 101 int writeable();
emilmont 9:663789d7729f 102
emilmont 9:663789d7729f 103 /** Attach a function to call whenever a serial interrupt is generated
emilmont 9:663789d7729f 104 *
emilmont 9:663789d7729f 105 * @param fptr A pointer to a void function, or 0 to set as none
emilmont 9:663789d7729f 106 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
emilmont 9:663789d7729f 107 */
emilmont 9:663789d7729f 108 void attach(void (*fptr)(void), IrqType type=RxIrq);
emilmont 9:663789d7729f 109
emilmont 9:663789d7729f 110 /** Attach a member function to call whenever a serial interrupt is generated
emilmont 9:663789d7729f 111 *
emilmont 9:663789d7729f 112 * @param tptr pointer to the object to call the member function on
emilmont 9:663789d7729f 113 * @param mptr pointer to the member function to be called
emilmont 9:663789d7729f 114 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
emilmont 9:663789d7729f 115 */
emilmont 9:663789d7729f 116 template<typename T>
emilmont 9:663789d7729f 117 void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) {
emilmont 9:663789d7729f 118 if((mptr != NULL) && (tptr != NULL)) {
emilmont 9:663789d7729f 119 _irq[type].attach(tptr, mptr);
emilmont 9:663789d7729f 120 serial_irq_set(&_serial, (SerialIrq)type, 1);
emilmont 9:663789d7729f 121 }
emilmont 9:663789d7729f 122 }
emilmont 9:663789d7729f 123
emilmont 9:663789d7729f 124 static void _irq_handler(uint32_t id, SerialIrq irq_type);
emilmont 9:663789d7729f 125
emilmont 9:663789d7729f 126 protected:
emilmont 9:663789d7729f 127 virtual int _getc();
emilmont 9:663789d7729f 128 virtual int _putc(int c);
emilmont 9:663789d7729f 129
emilmont 9:663789d7729f 130 serial_t _serial;
emilmont 9:663789d7729f 131 FunctionPointer _irq[2];
emilmont 9:663789d7729f 132 };
emilmont 9:663789d7729f 133
emilmont 9:663789d7729f 134 } // namespace mbed
emilmont 9:663789d7729f 135
emilmont 9:663789d7729f 136 #endif
emilmont 9:663789d7729f 137
emilmont 9:663789d7729f 138 #endif