mbed libraries for KL25Z

Dependents:   FRDM_RGBLED

Committer:
emilmont
Date:
Tue Oct 23 09:20:18 2012 +0000
Revision:
7:73c5efe92a6c
Parent:
2:e9a661555b58
Child:
8:c14af7958ef5
Make the C++ library completely TARGET independent.; Implement "gpio_irq_api" and "port_api" to KL25Z.

Who changed what in which revision?

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