svn / mbed / trunk / Serial.h

Revision 29, 4.9 kB (checked in by emilmont, 6 months ago)

New Libraries 11.11

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