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