1 /* mbed Microcontroller Library - SerialHalfDuplex
2 * Copyright (c) 2010-2011 ARM Limited. All rights reserved.
5 #ifndef MBED_SERIALHALFDUPLEX_H
6 #define MBED_SERIALHALFDUPLEX_H
14 #include "PeripheralNames.h"
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
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.
29 * > // Send a byte to a second HalfDuplex device, and read the response
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
37 * > SerialHalfDuplex a(p9, p10);
38 * > SerialHalfDuplex b(p28, p27);
40 * > void b_rx() { // second device response
41 * > b.putc(b.getc() + 4);
46 * > for(int c = 'A'; c < 'Z'; 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());
56 * For Simplex and Full-Duplex Serial communication, see <Serial>
58 class SerialHalfDuplex
: public Serial
{
61 /* Constructor: SerialHalfDuplex
62 * Create a half-duplex serial port, connected to the specified transmit
65 * These pins should be wired together, as well as to the target device
71 SerialHalfDuplex(PinName tx
, PinName rx
, const char *name
= NULL
);
73 #if 0 // Inherited from Serial class, for documentation
75 * Set the baud rate of the serial port
78 * baudrate - The baudrate of the serial port (default = 9600).
80 void baud(int baudrate
);
91 * Set the transmission format used by the Serial port
94 * bits - The number of bits in a word (5-8; default = 8)
95 * parity - The parity used (Serial::None, Serial::Odd,
96 Serial::Even, Serial::Forced1, Serial::Forced0; default = Serial::None)
97 * stop - The number of stop bits (1 or 2; default = 1)
99 void format(int bits
= 8, Parity parity
= Serial::None
, int stop_bits
106 * c - The character to write to the serial port
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>.
118 * returns - The character read from the serial port
123 * Write a formated string
126 * format - A printf-style format string, followed by the
127 * variables to use in formating the string.
129 int printf(const char* format
, ...);
132 * Read a formated string
135 * format - A scanf-style format string,
136 * followed by the pointers to variables to store the results.
138 int scanf(const char* format
, ...);
140 /* Function: readable
141 * Determine if there is a character available to read
144 * returns - 1 if there is a character available to read, else 0
148 /* Function: writeable
149 * Determine if there is space available to write a character
152 * returns - 1 if there is space to write a character, else 0
157 * Attach a function to call whenever a serial interrupt is generated
160 * fptr - A pointer to a void function, or 0 to set as none
162 void attach(void (*fptr
)(void));
165 * Attach a member function to call whenever a serial interrupt is generated
168 * tptr - pointer to the object to call the member function on
169 * mptr - pointer to the member function to be called
172 void attach(T
* tptr
, void (T::*mptr
)(void));
179 virtual int _putc(int c
);
180 virtual int _getc(void);
182 }; // End class SerialHalfDuplex