Add attach

Fork of SerialHalfDuplex by Aimen Al-Refai

Committer:
yusuke_kyo
Date:
Sun May 03 16:17:06 2015 +0000
Revision:
3:a5021aaf31d9
Parent:
2:5ecc72a47df0
add attach

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_unsupported 0:7802a25daf3b 1 /* mbed Microcontroller Library
mbed_unsupported 0:7802a25daf3b 2 * Copyright (c) 2006-2012 ARM Limited
mbed_unsupported 0:7802a25daf3b 3 *
mbed_unsupported 0:7802a25daf3b 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
mbed_unsupported 0:7802a25daf3b 5 * of this software and associated documentation files (the "Software"), to deal
mbed_unsupported 0:7802a25daf3b 6 * in the Software without restriction, including without limitation the rights
mbed_unsupported 0:7802a25daf3b 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
mbed_unsupported 0:7802a25daf3b 8 * copies of the Software, and to permit persons to whom the Software is
mbed_unsupported 0:7802a25daf3b 9 * furnished to do so, subject to the following conditions:
mbed_unsupported 0:7802a25daf3b 10 *
mbed_unsupported 0:7802a25daf3b 11 * The above copyright notice and this permission notice shall be included in
mbed_unsupported 0:7802a25daf3b 12 * all copies or substantial portions of the Software.
mbed_unsupported 0:7802a25daf3b 13 *
mbed_unsupported 0:7802a25daf3b 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
mbed_unsupported 0:7802a25daf3b 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
mbed_unsupported 0:7802a25daf3b 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
mbed_unsupported 0:7802a25daf3b 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
mbed_unsupported 0:7802a25daf3b 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
mbed_unsupported 0:7802a25daf3b 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
mbed_unsupported 0:7802a25daf3b 20 * SOFTWARE.
mbed_unsupported 0:7802a25daf3b 21 *
mbed_unsupported 0:7802a25daf3b 22 * NOTE: This is an unsupported legacy untested library.
mbed_unsupported 0:7802a25daf3b 23 */
mbed_unsupported 0:7802a25daf3b 24 #ifndef MBED_SERIALHALFDUPLEX_H
mbed_unsupported 0:7802a25daf3b 25 #define MBED_SERIALHALFDUPLEX_H
mbed_unsupported 0:7802a25daf3b 26
mbed_unsupported 0:7802a25daf3b 27 #include "device.h"
aimen 2:5ecc72a47df0 28
mbed_unsupported 0:7802a25daf3b 29 #if DEVICE_SERIAL
aimen 2:5ecc72a47df0 30
mbed_unsupported 0:7802a25daf3b 31 #include "Serial.h"
mbed_unsupported 0:7802a25daf3b 32 #include "PinNames.h"
mbed_unsupported 0:7802a25daf3b 33 #include "PeripheralNames.h"
aimen 2:5ecc72a47df0 34
mbed_unsupported 0:7802a25daf3b 35 namespace mbed {
aimen 2:5ecc72a47df0 36
mbed_unsupported 0:7802a25daf3b 37 /* Class: SerialHalfDuplex
mbed_unsupported 0:7802a25daf3b 38 * A serial port (UART) for communication with other devices using
mbed_unsupported 0:7802a25daf3b 39 * Half-Duplex, allowing transmit and receive on a single
mbed_unsupported 0:7802a25daf3b 40 * shared transmit and receive line. Only one end should be transmitting
mbed_unsupported 0:7802a25daf3b 41 * at a time.
mbed_unsupported 0:7802a25daf3b 42 *
mbed_unsupported 0:7802a25daf3b 43 * Both the tx and rx pin should be defined, and wired together.
mbed_unsupported 0:7802a25daf3b 44 * This is in addition to them being wired to the other serial
mbed_unsupported 0:7802a25daf3b 45 * device to allow both read and write functions to operate.
mbed_unsupported 0:7802a25daf3b 46 *
mbed_unsupported 0:7802a25daf3b 47 * Example:
mbed_unsupported 0:7802a25daf3b 48 * > // Send a byte to a second HalfDuplex device, and read the response
mbed_unsupported 0:7802a25daf3b 49 * >
mbed_unsupported 0:7802a25daf3b 50 * > #include "mbed.h"
mbed_unsupported 0:7802a25daf3b 51 * >
mbed_unsupported 0:7802a25daf3b 52 * > // p9 and p10 should be wired together to form "a"
mbed_unsupported 0:7802a25daf3b 53 * > // p28 and p27 should be wired together to form "b"
mbed_unsupported 0:7802a25daf3b 54 * > // p9/p10 should be wired to p28/p27 as the Half Duplex connection
mbed_unsupported 0:7802a25daf3b 55 * >
mbed_unsupported 0:7802a25daf3b 56 * > SerialHalfDuplex a(p9, p10);
mbed_unsupported 0:7802a25daf3b 57 * > SerialHalfDuplex b(p28, p27);
mbed_unsupported 0:7802a25daf3b 58 * >
mbed_unsupported 0:7802a25daf3b 59 * > void b_rx() { // second device response
mbed_unsupported 0:7802a25daf3b 60 * > b.putc(b.getc() + 4);
mbed_unsupported 0:7802a25daf3b 61 * > }
mbed_unsupported 0:7802a25daf3b 62 * >
mbed_unsupported 0:7802a25daf3b 63 * > int main() {
mbed_unsupported 0:7802a25daf3b 64 * > b.attach(&b_rx);
mbed_unsupported 0:7802a25daf3b 65 * > for(int c = 'A'; c < 'Z'; c++) {
mbed_unsupported 0:7802a25daf3b 66 * > a.putc(c);
mbed_unsupported 0:7802a25daf3b 67 * > printf("sent [%c]\n", c);
mbed_unsupported 0:7802a25daf3b 68 * > wait(0.5); // b should respond
mbed_unsupported 0:7802a25daf3b 69 * > if(a.readable()) {
mbed_unsupported 0:7802a25daf3b 70 * > printf("received [%c]\n", a.getc());
mbed_unsupported 0:7802a25daf3b 71 * > }
mbed_unsupported 0:7802a25daf3b 72 * > }
mbed_unsupported 0:7802a25daf3b 73 * > }
mbed_unsupported 0:7802a25daf3b 74 *
mbed_unsupported 0:7802a25daf3b 75 * For Simplex and Full-Duplex Serial communication, see <Serial>
mbed_unsupported 0:7802a25daf3b 76 */
mbed_unsupported 0:7802a25daf3b 77 class SerialHalfDuplex : public Serial {
aimen 2:5ecc72a47df0 78
mbed_unsupported 0:7802a25daf3b 79 public:
mbed_unsupported 0:7802a25daf3b 80 /* Constructor: SerialHalfDuplex
mbed_unsupported 0:7802a25daf3b 81 * Create a half-duplex serial port, connected to the specified transmit
mbed_unsupported 0:7802a25daf3b 82 * and receive pins.
mbed_unsupported 0:7802a25daf3b 83 *
mbed_unsupported 0:7802a25daf3b 84 * These pins should be wired together, as well as to the target device
mbed_unsupported 0:7802a25daf3b 85 *
mbed_unsupported 0:7802a25daf3b 86 * Variables:
mbed_unsupported 0:7802a25daf3b 87 * tx - Transmit pin
mbed_unsupported 0:7802a25daf3b 88 * rx - Receive pin
mbed_unsupported 0:7802a25daf3b 89 */
mbed_unsupported 0:7802a25daf3b 90 SerialHalfDuplex(PinName tx, PinName rx, const char *name = NULL);
aimen 2:5ecc72a47df0 91
mbed_unsupported 0:7802a25daf3b 92 #if 0 // Inherited from Serial class, for documentation
mbed_unsupported 0:7802a25daf3b 93 /* Function: baud
mbed_unsupported 0:7802a25daf3b 94 * Set the baud rate of the serial port
mbed_unsupported 0:7802a25daf3b 95 *
mbed_unsupported 0:7802a25daf3b 96 * Variables:
mbed_unsupported 0:7802a25daf3b 97 * baudrate - The baudrate of the serial port (default = 9600).
mbed_unsupported 0:7802a25daf3b 98 */
mbed_unsupported 0:7802a25daf3b 99 void baud(int baudrate);
aimen 2:5ecc72a47df0 100
mbed_unsupported 0:7802a25daf3b 101 enum Parity {
mbed_unsupported 0:7802a25daf3b 102 None = 0
mbed_unsupported 0:7802a25daf3b 103 , Odd
mbed_unsupported 0:7802a25daf3b 104 , Even
mbed_unsupported 0:7802a25daf3b 105 , Forced1
mbed_unsupported 0:7802a25daf3b 106 , Forced0
mbed_unsupported 0:7802a25daf3b 107 };
aimen 2:5ecc72a47df0 108
mbed_unsupported 0:7802a25daf3b 109 /* Function: format
mbed_unsupported 0:7802a25daf3b 110 * Set the transmission format used by the Serial port
mbed_unsupported 0:7802a25daf3b 111 *
mbed_unsupported 0:7802a25daf3b 112 * Variables:
mbed_unsupported 0:7802a25daf3b 113 * bits - The number of bits in a word (5-8; default = 8)
mbed_unsupported 0:7802a25daf3b 114 * parity - The parity used (Serial::None, Serial::Odd,
mbed_unsupported 0:7802a25daf3b 115 Serial::Even, Serial::Forced1, Serial::Forced0; default = Serial::None)
mbed_unsupported 0:7802a25daf3b 116 * stop - The number of stop bits (1 or 2; default = 1)
mbed_unsupported 0:7802a25daf3b 117 */
yusuke_kyo 3:a5021aaf31d9 118 void format(int bits = 8, Parity parity = Serial::None, int stop_bits = 1);
aimen 2:5ecc72a47df0 119
mbed_unsupported 0:7802a25daf3b 120 /* Function: putc
mbed_unsupported 0:7802a25daf3b 121 * Write a character
mbed_unsupported 0:7802a25daf3b 122 *
mbed_unsupported 0:7802a25daf3b 123 * Variables:
mbed_unsupported 0:7802a25daf3b 124 * c - The character to write to the serial port
mbed_unsupported 0:7802a25daf3b 125 */
mbed_unsupported 0:7802a25daf3b 126 int putc(int c);
aimen 2:5ecc72a47df0 127
mbed_unsupported 0:7802a25daf3b 128 /* Function: getc
mbed_unsupported 0:7802a25daf3b 129 * Read a character
mbed_unsupported 0:7802a25daf3b 130 *
mbed_unsupported 0:7802a25daf3b 131 * Read a character from the serial port. This call will block
mbed_unsupported 0:7802a25daf3b 132 * until a character is available. For testing if a character is
mbed_unsupported 0:7802a25daf3b 133 * available for reading, see <readable>.
mbed_unsupported 0:7802a25daf3b 134 *
mbed_unsupported 0:7802a25daf3b 135 * Variables:
mbed_unsupported 0:7802a25daf3b 136 * returns - The character read from the serial port
mbed_unsupported 0:7802a25daf3b 137 */
mbed_unsupported 0:7802a25daf3b 138 int getc();
aimen 2:5ecc72a47df0 139
mbed_unsupported 0:7802a25daf3b 140 /* Function: printf
mbed_unsupported 0:7802a25daf3b 141 * Write a formated string
mbed_unsupported 0:7802a25daf3b 142 *
mbed_unsupported 0:7802a25daf3b 143 * Variables:
mbed_unsupported 0:7802a25daf3b 144 * format - A printf-style format string, followed by the
mbed_unsupported 0:7802a25daf3b 145 * variables to use in formating the string.
mbed_unsupported 0:7802a25daf3b 146 */
mbed_unsupported 0:7802a25daf3b 147 int printf(const char* format, ...);
aimen 2:5ecc72a47df0 148
mbed_unsupported 0:7802a25daf3b 149 /* Function: scanf
mbed_unsupported 0:7802a25daf3b 150 * Read a formated string
mbed_unsupported 0:7802a25daf3b 151 *
mbed_unsupported 0:7802a25daf3b 152 * Variables:
mbed_unsupported 0:7802a25daf3b 153 * format - A scanf-style format string,
mbed_unsupported 0:7802a25daf3b 154 * followed by the pointers to variables to store the results.
mbed_unsupported 0:7802a25daf3b 155 */
mbed_unsupported 0:7802a25daf3b 156 int scanf(const char* format, ...);
aimen 2:5ecc72a47df0 157
mbed_unsupported 0:7802a25daf3b 158 /* Function: readable
mbed_unsupported 0:7802a25daf3b 159 * Determine if there is a character available to read
mbed_unsupported 0:7802a25daf3b 160 *
mbed_unsupported 0:7802a25daf3b 161 * Variables:
mbed_unsupported 0:7802a25daf3b 162 * returns - 1 if there is a character available to read, else 0
mbed_unsupported 0:7802a25daf3b 163 */
mbed_unsupported 0:7802a25daf3b 164 int readable();
aimen 2:5ecc72a47df0 165
mbed_unsupported 0:7802a25daf3b 166 /* Function: writeable
mbed_unsupported 0:7802a25daf3b 167 * Determine if there is space available to write a character
mbed_unsupported 0:7802a25daf3b 168 *
mbed_unsupported 0:7802a25daf3b 169 * Variables:
mbed_unsupported 0:7802a25daf3b 170 * returns - 1 if there is space to write a character, else 0
mbed_unsupported 0:7802a25daf3b 171 */
mbed_unsupported 0:7802a25daf3b 172 int writeable();
aimen 2:5ecc72a47df0 173
mbed_unsupported 0:7802a25daf3b 174 /* Function: attach
mbed_unsupported 0:7802a25daf3b 175 * Attach a function to call whenever a serial interrupt is generated
mbed_unsupported 0:7802a25daf3b 176 *
mbed_unsupported 0:7802a25daf3b 177 * Variables:
mbed_unsupported 0:7802a25daf3b 178 * fptr - A pointer to a void function, or 0 to set as none
mbed_unsupported 0:7802a25daf3b 179 */
mbed_unsupported 0:7802a25daf3b 180 void attach(void (*fptr)(void));
aimen 2:5ecc72a47df0 181
mbed_unsupported 0:7802a25daf3b 182 /* Function: attach
mbed_unsupported 0:7802a25daf3b 183 * Attach a member function to call whenever a serial interrupt is generated
mbed_unsupported 0:7802a25daf3b 184 *
mbed_unsupported 0:7802a25daf3b 185 * Variables:
mbed_unsupported 0:7802a25daf3b 186 * tptr - pointer to the object to call the member function on
mbed_unsupported 0:7802a25daf3b 187 * mptr - pointer to the member function to be called
mbed_unsupported 0:7802a25daf3b 188 */
mbed_unsupported 0:7802a25daf3b 189 template<typename T>
mbed_unsupported 0:7802a25daf3b 190 void attach(T* tptr, void (T::*mptr)(void));
yusuke_kyo 3:a5021aaf31d9 191
yusuke_kyo 3:a5021aaf31d9 192
mbed_unsupported 0:7802a25daf3b 193 #endif
aimen 2:5ecc72a47df0 194
mbed_unsupported 0:7802a25daf3b 195 protected:
mbed_unsupported 0:7802a25daf3b 196 PinName _txpin;
aimen 2:5ecc72a47df0 197
mbed_unsupported 0:7802a25daf3b 198 virtual int _putc(int c);
mbed_unsupported 0:7802a25daf3b 199 virtual int _getc(void);
yusuke_kyo 3:a5021aaf31d9 200
yusuke_kyo 3:a5021aaf31d9 201 void enable(void);
aimen 2:5ecc72a47df0 202
mbed_unsupported 0:7802a25daf3b 203 }; // End class SerialHalfDuplex
aimen 2:5ecc72a47df0 204
mbed_unsupported 0:7802a25daf3b 205 } // End namespace
aimen 2:5ecc72a47df0 206
mbed_unsupported 0:7802a25daf3b 207 #endif
aimen 2:5ecc72a47df0 208
aimen 2:5ecc72a47df0 209 #endif