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