mbed libraries for KL25Z

Dependents:   FRDM_RGBLED

Committer:
emilmont
Date:
Fri Oct 05 09:16:41 2012 +0000
Revision:
0:8024c367e29f
Child:
2:e9a661555b58
First release of the mbed libraries for KL25Z

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emilmont 0:8024c367e29f 1 /* mbed Microcontroller Library - Serial
emilmont 0:8024c367e29f 2 * Copyright (c) 2007-2011 ARM Limited. All rights reserved.
emilmont 0:8024c367e29f 3 */
emilmont 0:8024c367e29f 4
emilmont 0:8024c367e29f 5 #ifndef MBED_SERIAL_H
emilmont 0:8024c367e29f 6 #define MBED_SERIAL_H
emilmont 0:8024c367e29f 7
emilmont 0:8024c367e29f 8 #include "device.h"
emilmont 0:8024c367e29f 9
emilmont 0:8024c367e29f 10 #if DEVICE_SERIAL
emilmont 0:8024c367e29f 11
emilmont 0:8024c367e29f 12 #include "platform.h"
emilmont 0:8024c367e29f 13 #include "PinNames.h"
emilmont 0:8024c367e29f 14 #include "PeripheralNames.h"
emilmont 0:8024c367e29f 15 #include "Stream.h"
emilmont 0:8024c367e29f 16 #include "FunctionPointer.h"
emilmont 0:8024c367e29f 17
emilmont 0:8024c367e29f 18 namespace mbed {
emilmont 0:8024c367e29f 19
emilmont 0:8024c367e29f 20 /* Class: Serial
emilmont 0:8024c367e29f 21 * A serial port (UART) for communication with other serial devices
emilmont 0:8024c367e29f 22 *
emilmont 0:8024c367e29f 23 * Can be used for Full Duplex communication, or Simplex by specifying
emilmont 0:8024c367e29f 24 * one pin as NC (Not Connected)
emilmont 0:8024c367e29f 25 *
emilmont 0:8024c367e29f 26 * Example:
emilmont 0:8024c367e29f 27 * > // Print "Hello World" to the PC
emilmont 0:8024c367e29f 28 * >
emilmont 0:8024c367e29f 29 * > #include "mbed.h"
emilmont 0:8024c367e29f 30 * >
emilmont 0:8024c367e29f 31 * > Serial pc(USBTX, USBRX);
emilmont 0:8024c367e29f 32 * >
emilmont 0:8024c367e29f 33 * > int main() {
emilmont 0:8024c367e29f 34 * > pc.printf("Hello World\n");
emilmont 0:8024c367e29f 35 * > }
emilmont 0:8024c367e29f 36 */
emilmont 0:8024c367e29f 37 class Serial : public Stream {
emilmont 0:8024c367e29f 38
emilmont 0:8024c367e29f 39 public:
emilmont 0:8024c367e29f 40
emilmont 0:8024c367e29f 41 /* Constructor: Serial
emilmont 0:8024c367e29f 42 * Create a Serial port, connected to the specified transmit and receive pins
emilmont 0:8024c367e29f 43 *
emilmont 0:8024c367e29f 44 * Variables:
emilmont 0:8024c367e29f 45 * tx - Transmit pin
emilmont 0:8024c367e29f 46 * rx - Receive pin
emilmont 0:8024c367e29f 47 *
emilmont 0:8024c367e29f 48 * Note: Either tx or rx may be specified as NC if unused
emilmont 0:8024c367e29f 49 */
emilmont 0:8024c367e29f 50 Serial(PinName tx, PinName rx, const char *name = NULL);
emilmont 0:8024c367e29f 51
emilmont 0:8024c367e29f 52 /* Function: baud
emilmont 0:8024c367e29f 53 * Set the baud rate of the serial port
emilmont 0:8024c367e29f 54 *
emilmont 0:8024c367e29f 55 * Variables:
emilmont 0:8024c367e29f 56 * baudrate - The baudrate of the serial port (default = 9600).
emilmont 0:8024c367e29f 57 */
emilmont 0:8024c367e29f 58 void baud(int baudrate);
emilmont 0:8024c367e29f 59
emilmont 0:8024c367e29f 60 enum Parity {
emilmont 0:8024c367e29f 61 None = 0
emilmont 0:8024c367e29f 62 , Odd
emilmont 0:8024c367e29f 63 , Even
emilmont 0:8024c367e29f 64 , Forced1
emilmont 0:8024c367e29f 65 , Forced0
emilmont 0:8024c367e29f 66 };
emilmont 0:8024c367e29f 67
emilmont 0:8024c367e29f 68 enum IrqType {
emilmont 0:8024c367e29f 69 RxIrq = 0
emilmont 0:8024c367e29f 70 , TxIrq
emilmont 0:8024c367e29f 71 };
emilmont 0:8024c367e29f 72
emilmont 0:8024c367e29f 73 /* Function: format
emilmont 0:8024c367e29f 74 * Set the transmission format used by the Serial port
emilmont 0:8024c367e29f 75 *
emilmont 0:8024c367e29f 76 * Variables:
emilmont 0:8024c367e29f 77 * bits - The number of bits in a word (5-8; default = 8)
emilmont 0:8024c367e29f 78 * parity - The parity used (Serial::None, Serial::Odd, Serial::Even, Serial::Forced1, Serial::Forced0; default = Serial::None)
emilmont 0:8024c367e29f 79 * stop - The number of stop bits (1 or 2; default = 1)
emilmont 0:8024c367e29f 80 */
emilmont 0:8024c367e29f 81 void format(int bits = 8, Parity parity = Serial::None, int stop_bits = 1);
emilmont 0:8024c367e29f 82
emilmont 0:8024c367e29f 83 #if 0 // Inhereted from Stream, for documentation only
emilmont 0:8024c367e29f 84
emilmont 0:8024c367e29f 85 /* Function: putc
emilmont 0:8024c367e29f 86 * Write a character
emilmont 0:8024c367e29f 87 *
emilmont 0:8024c367e29f 88 * Variables:
emilmont 0:8024c367e29f 89 * c - The character to write to the serial port
emilmont 0:8024c367e29f 90 */
emilmont 0:8024c367e29f 91 int putc(int c);
emilmont 0:8024c367e29f 92
emilmont 0:8024c367e29f 93 /* Function: getc
emilmont 0:8024c367e29f 94 * Read a character
emilmont 0:8024c367e29f 95 *
emilmont 0:8024c367e29f 96 * Reads a character from the serial port. This will block until
emilmont 0:8024c367e29f 97 * a character is available. To see if a character is available,
emilmont 0:8024c367e29f 98 * see <readable>
emilmont 0:8024c367e29f 99 *
emilmont 0:8024c367e29f 100 * Variables:
emilmont 0:8024c367e29f 101 * returns - The character read from the serial port
emilmont 0:8024c367e29f 102 */
emilmont 0:8024c367e29f 103 int getc();
emilmont 0:8024c367e29f 104
emilmont 0:8024c367e29f 105 /* Function: printf
emilmont 0:8024c367e29f 106 * Write a formated string
emilmont 0:8024c367e29f 107 *
emilmont 0:8024c367e29f 108 * Variables:
emilmont 0:8024c367e29f 109 * format - A printf-style format string, followed by the
emilmont 0:8024c367e29f 110 * variables to use in formating the string.
emilmont 0:8024c367e29f 111 */
emilmont 0:8024c367e29f 112 int printf(const char* format, ...);
emilmont 0:8024c367e29f 113
emilmont 0:8024c367e29f 114 /* Function: scanf
emilmont 0:8024c367e29f 115 * Read a formated string
emilmont 0:8024c367e29f 116 *
emilmont 0:8024c367e29f 117 * Variables:
emilmont 0:8024c367e29f 118 * format - A scanf-style format string,
emilmont 0:8024c367e29f 119 * followed by the pointers to variables to store the results.
emilmont 0:8024c367e29f 120 */
emilmont 0:8024c367e29f 121 int scanf(const char* format, ...);
emilmont 0:8024c367e29f 122
emilmont 0:8024c367e29f 123 #endif
emilmont 0:8024c367e29f 124
emilmont 0:8024c367e29f 125 /* Function: readable
emilmont 0:8024c367e29f 126 * Determine if there is a character available to read
emilmont 0:8024c367e29f 127 *
emilmont 0:8024c367e29f 128 * Variables:
emilmont 0:8024c367e29f 129 * returns - 1 if there is a character available to read, else 0
emilmont 0:8024c367e29f 130 */
emilmont 0:8024c367e29f 131 int readable();
emilmont 0:8024c367e29f 132
emilmont 0:8024c367e29f 133 /* Function: writeable
emilmont 0:8024c367e29f 134 * Determine if there is space available to write a character
emilmont 0:8024c367e29f 135 *
emilmont 0:8024c367e29f 136 * Variables:
emilmont 0:8024c367e29f 137 * returns - 1 if there is space to write a character, else 0
emilmont 0:8024c367e29f 138 */
emilmont 0:8024c367e29f 139 int writeable();
emilmont 0:8024c367e29f 140
emilmont 0:8024c367e29f 141 /* Function: attach
emilmont 0:8024c367e29f 142 * Attach a function to call whenever a serial interrupt is generated
emilmont 0:8024c367e29f 143 *
emilmont 0:8024c367e29f 144 * Variables:
emilmont 0:8024c367e29f 145 * fptr - A pointer to a void function, or 0 to set as none
emilmont 0:8024c367e29f 146 * type - Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
emilmont 0:8024c367e29f 147 */
emilmont 0:8024c367e29f 148 void attach(void (*fptr)(void), IrqType type = RxIrq);
emilmont 0:8024c367e29f 149
emilmont 0:8024c367e29f 150 /* Function: attach
emilmont 0:8024c367e29f 151 * Attach a member function to call whenever a serial interrupt is generated
emilmont 0:8024c367e29f 152 *
emilmont 0:8024c367e29f 153 * Variables:
emilmont 0:8024c367e29f 154 * tptr - pointer to the object to call the member function on
emilmont 0:8024c367e29f 155 * mptr - pointer to the member function to be called
emilmont 0:8024c367e29f 156 * type - Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
emilmont 0:8024c367e29f 157 */
emilmont 0:8024c367e29f 158 template<typename T>
emilmont 0:8024c367e29f 159 void attach(T* tptr, void (T::*mptr)(void), IrqType type = RxIrq) {
emilmont 0:8024c367e29f 160 if((mptr != NULL) && (tptr != NULL)) {
emilmont 0:8024c367e29f 161 _irq[type].attach(tptr, mptr);
emilmont 0:8024c367e29f 162 setup_interrupt(type);
emilmont 0:8024c367e29f 163 }
emilmont 0:8024c367e29f 164 }
emilmont 0:8024c367e29f 165
emilmont 0:8024c367e29f 166 #ifdef MBED_RPC
emilmont 0:8024c367e29f 167 virtual const struct rpc_method *get_rpc_methods();
emilmont 0:8024c367e29f 168 static struct rpc_class *get_rpc_class();
emilmont 0:8024c367e29f 169 #endif
emilmont 0:8024c367e29f 170
emilmont 0:8024c367e29f 171 protected:
emilmont 0:8024c367e29f 172
emilmont 0:8024c367e29f 173 void setup_interrupt(IrqType type);
emilmont 0:8024c367e29f 174 void remove_interrupt(IrqType type);
emilmont 0:8024c367e29f 175
emilmont 0:8024c367e29f 176 virtual int _getc();
emilmont 0:8024c367e29f 177 virtual int _putc(int c);
emilmont 0:8024c367e29f 178
emilmont 0:8024c367e29f 179 UARTName _uart;
emilmont 0:8024c367e29f 180 FunctionPointer _irq[2];
emilmont 0:8024c367e29f 181 int _uidx;
emilmont 0:8024c367e29f 182
emilmont 0:8024c367e29f 183 };
emilmont 0:8024c367e29f 184
emilmont 0:8024c367e29f 185 } // namespace mbed
emilmont 0:8024c367e29f 186
emilmont 0:8024c367e29f 187 #endif
emilmont 0:8024c367e29f 188
emilmont 0:8024c367e29f 189 #endif