svn / mbed / trunk / SerialHalfDuplex.h

Revision 32, 4.9 kB (checked in by emilmont, 5 months ago)

Add latest ARM GCC toolchain build

Line 
1/* mbed Microcontroller Library - SerialHalfDuplex
2 * Copyright (c) 2010-2011 ARM Limited. All rights reserved.
3 */
4
5#ifndef MBED_SERIALHALFDUPLEX_H
6#define MBED_SERIALHALFDUPLEX_H
7
8#include "device.h"
9
10#if DEVICE_SERIAL
11
12#include "Serial.h"
13#include "PinNames.h"
14#include "PeripheralNames.h"
15
16namespace mbed {
17
18/* Class: SerialHalfDuplex
19 * A serial port (UART) for communication with other devices using 
20 * Half-Duplex, allowing transmit and receive on a single
21 * shared transmit and receive line. Only one end should be transmitting
22 * at a time.
23 *
24 * Both the tx and rx pin should be defined, and wired together.
25 * This is in addition to them being wired to the other serial
26 * device to allow both read and write functions to operate.
27 *
28 *  Example:
29 *  > // Send a byte to a second HalfDuplex device, and read the response
30 *  >
31 *  > #include "mbed.h"
32 *  >
33 *  > // p9 and p10 should be wired together to form "a"
34 *  > // p28 and p27 should be wired together to form "b"
35 *  > // p9/p10 should be wired to p28/p27 as the Half Duplex connection
36 *  >
37 *  > SerialHalfDuplex a(p9, p10);
38 *  > SerialHalfDuplex b(p28, p27);
39 *  >
40 *  > void b_rx() { // second device response
41 *  >     b.putc(b.getc() + 4);
42 *  > }
43 *  >   
44 *  > int main() {
45 *  >     b.attach(&b_rx);
46 *  >     for(int c = 'A'; c < 'Z'; c++) {
47 *  >         a.putc(c);
48 *  >         printf("sent [%c]\n", c);
49 *  >         wait(0.5);   // b should respond
50 *  >         if(a.readable()) {
51 *  >             printf("received [%c]\n", a.getc());
52 *  >         }
53 *  >     }
54 *  > }
55 *
56 * For Simplex and Full-Duplex Serial communication, see <Serial>
57 */
58class SerialHalfDuplex : public Serial {
59
60public:
61    /* Constructor: SerialHalfDuplex
62     * Create a half-duplex serial port, connected to the specified transmit
63     * and receive pins.
64     *
65     * These pins should be wired together, as well as to the target device
66     *
67     * Variables:
68     *  tx - Transmit pin
69     *  rx - Receive pin
70     */
71    SerialHalfDuplex(PinName tx, PinName rx, const char *name = NULL);
72
73#if 0       // Inherited from Serial class, for documentation
74    /* Function: baud
75     *  Set the baud rate of the serial port
76     *
77     * Variables:
78     *  baudrate - The baudrate of the serial port (default = 9600).
79     */
80    void baud(int baudrate);
81
82    enum Parity {
83        None = 0
84        , Odd
85        , Even
86        , Forced1
87        , Forced0
88    };
89
90    /* Function: format
91     *  Set the transmission format used by the Serial port
92     *
93     * Variables:
94     *  bits - The number of bits in a word (5-8; default = 8)
95     *  parity - The parity used (Serial::None, Serial::Odd,
96Serial::Even, Serial::Forced1, Serial::Forced0; default = Serial::None)
97     *  stop - The number of stop bits (1 or 2; default = 1)
98     */
99    void format(int bits = 8, Parity parity = Serial::None, int stop_bits
100= 1);
101
102    /* Function: putc
103     *  Write a character
104     *
105     * Variables:
106     *  c - The character to write to the serial port
107     */
108    int putc(int c);
109
110    /* Function: getc
111     *  Read a character
112     *
113     * Read a character from the serial port. This call will block
114     * until a character is available. For testing if a character is
115     * available for reading, see <readable>.
116     *
117     * Variables:
118     *  returns - The character read from the serial port
119     */
120    int getc();
121
122    /* Function: printf
123     *  Write a formated string
124     *
125     * Variables:
126     *  format - A printf-style format string, followed by the
127     *      variables to use in formating the string.
128     */
129    int printf(const char* format, ...);
130
131    /* Function: scanf
132     *  Read a formated string
133     *
134     * Variables:
135     *  format - A scanf-style format string,
136     *      followed by the pointers to variables to store the results.
137     */
138    int scanf(const char* format, ...);
139
140    /* Function: readable
141     *  Determine if there is a character available to read
142     *
143     * Variables:
144     *  returns - 1 if there is a character available to read, else 0
145     */
146    int readable();
147
148    /* Function: writeable
149     *  Determine if there is space available to write a character
150     *
151     * Variables:
152     *  returns - 1 if there is space to write a character, else 0
153     */
154    int writeable();
155
156    /* Function: attach
157     *  Attach a function to call whenever a serial interrupt is generated
158     *
159     * Variables:
160     *  fptr - A pointer to a void function, or 0 to set as none
161     */
162    void attach(void (*fptr)(void));
163
164    /* Function: attach
165     *  Attach a member function to call whenever a serial interrupt is generated
166     *
167     * Variables:
168     *  tptr - pointer to the object to call the member function on
169     *  mptr - pointer to the member function to be called
170     */
171    template<typename T>
172    void attach(T* tptr, void (T::*mptr)(void));
173
174#endif
175
176protected:
177    PinName     _txpin;
178
179    virtual int _putc(int c);
180    virtual int _getc(void);
181
182}; // End class SerialHalfDuplex
183
184} // End namespace
185
186#endif
187
188#endif
Note: See TracBrowser for help on using the browser.