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