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