Serial Half Duplex implementation
Fork of SerialHalfDuplex by
SerialHalfDuplex.h@0:7802a25daf3b, 2012-12-06 (annotated)
- Committer:
- mbed_unsupported
- Date:
- Thu Dec 06 12:46:52 2012 +0000
- Revision:
- 0:7802a25daf3b
Serial Half-Duplex
Who changed what in which revision?
User | Revision | Line number | New 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" |
mbed_unsupported | 0:7802a25daf3b | 28 | |
mbed_unsupported | 0:7802a25daf3b | 29 | #if DEVICE_SERIAL |
mbed_unsupported | 0:7802a25daf3b | 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" |
mbed_unsupported | 0:7802a25daf3b | 34 | |
mbed_unsupported | 0:7802a25daf3b | 35 | namespace mbed { |
mbed_unsupported | 0:7802a25daf3b | 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 { |
mbed_unsupported | 0:7802a25daf3b | 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); |
mbed_unsupported | 0:7802a25daf3b | 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); |
mbed_unsupported | 0:7802a25daf3b | 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 | }; |
mbed_unsupported | 0:7802a25daf3b | 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 | */ |
mbed_unsupported | 0:7802a25daf3b | 118 | void format(int bits = 8, Parity parity = Serial::None, int stop_bits |
mbed_unsupported | 0:7802a25daf3b | 119 | = 1); |
mbed_unsupported | 0:7802a25daf3b | 120 | |
mbed_unsupported | 0:7802a25daf3b | 121 | /* Function: putc |
mbed_unsupported | 0:7802a25daf3b | 122 | * Write a character |
mbed_unsupported | 0:7802a25daf3b | 123 | * |
mbed_unsupported | 0:7802a25daf3b | 124 | * Variables: |
mbed_unsupported | 0:7802a25daf3b | 125 | * c - The character to write to the serial port |
mbed_unsupported | 0:7802a25daf3b | 126 | */ |
mbed_unsupported | 0:7802a25daf3b | 127 | int putc(int c); |
mbed_unsupported | 0:7802a25daf3b | 128 | |
mbed_unsupported | 0:7802a25daf3b | 129 | /* Function: getc |
mbed_unsupported | 0:7802a25daf3b | 130 | * Read a character |
mbed_unsupported | 0:7802a25daf3b | 131 | * |
mbed_unsupported | 0:7802a25daf3b | 132 | * Read a character from the serial port. This call will block |
mbed_unsupported | 0:7802a25daf3b | 133 | * until a character is available. For testing if a character is |
mbed_unsupported | 0:7802a25daf3b | 134 | * available for reading, see <readable>. |
mbed_unsupported | 0:7802a25daf3b | 135 | * |
mbed_unsupported | 0:7802a25daf3b | 136 | * Variables: |
mbed_unsupported | 0:7802a25daf3b | 137 | * returns - The character read from the serial port |
mbed_unsupported | 0:7802a25daf3b | 138 | */ |
mbed_unsupported | 0:7802a25daf3b | 139 | int getc(); |
mbed_unsupported | 0:7802a25daf3b | 140 | |
mbed_unsupported | 0:7802a25daf3b | 141 | /* Function: printf |
mbed_unsupported | 0:7802a25daf3b | 142 | * Write a formated string |
mbed_unsupported | 0:7802a25daf3b | 143 | * |
mbed_unsupported | 0:7802a25daf3b | 144 | * Variables: |
mbed_unsupported | 0:7802a25daf3b | 145 | * format - A printf-style format string, followed by the |
mbed_unsupported | 0:7802a25daf3b | 146 | * variables to use in formating the string. |
mbed_unsupported | 0:7802a25daf3b | 147 | */ |
mbed_unsupported | 0:7802a25daf3b | 148 | int printf(const char* format, ...); |
mbed_unsupported | 0:7802a25daf3b | 149 | |
mbed_unsupported | 0:7802a25daf3b | 150 | /* Function: scanf |
mbed_unsupported | 0:7802a25daf3b | 151 | * Read a formated string |
mbed_unsupported | 0:7802a25daf3b | 152 | * |
mbed_unsupported | 0:7802a25daf3b | 153 | * Variables: |
mbed_unsupported | 0:7802a25daf3b | 154 | * format - A scanf-style format string, |
mbed_unsupported | 0:7802a25daf3b | 155 | * followed by the pointers to variables to store the results. |
mbed_unsupported | 0:7802a25daf3b | 156 | */ |
mbed_unsupported | 0:7802a25daf3b | 157 | int scanf(const char* format, ...); |
mbed_unsupported | 0:7802a25daf3b | 158 | |
mbed_unsupported | 0:7802a25daf3b | 159 | /* Function: readable |
mbed_unsupported | 0:7802a25daf3b | 160 | * Determine if there is a character available to read |
mbed_unsupported | 0:7802a25daf3b | 161 | * |
mbed_unsupported | 0:7802a25daf3b | 162 | * Variables: |
mbed_unsupported | 0:7802a25daf3b | 163 | * returns - 1 if there is a character available to read, else 0 |
mbed_unsupported | 0:7802a25daf3b | 164 | */ |
mbed_unsupported | 0:7802a25daf3b | 165 | int readable(); |
mbed_unsupported | 0:7802a25daf3b | 166 | |
mbed_unsupported | 0:7802a25daf3b | 167 | /* Function: writeable |
mbed_unsupported | 0:7802a25daf3b | 168 | * Determine if there is space available to write a character |
mbed_unsupported | 0:7802a25daf3b | 169 | * |
mbed_unsupported | 0:7802a25daf3b | 170 | * Variables: |
mbed_unsupported | 0:7802a25daf3b | 171 | * returns - 1 if there is space to write a character, else 0 |
mbed_unsupported | 0:7802a25daf3b | 172 | */ |
mbed_unsupported | 0:7802a25daf3b | 173 | int writeable(); |
mbed_unsupported | 0:7802a25daf3b | 174 | |
mbed_unsupported | 0:7802a25daf3b | 175 | /* Function: attach |
mbed_unsupported | 0:7802a25daf3b | 176 | * Attach a function to call whenever a serial interrupt is generated |
mbed_unsupported | 0:7802a25daf3b | 177 | * |
mbed_unsupported | 0:7802a25daf3b | 178 | * Variables: |
mbed_unsupported | 0:7802a25daf3b | 179 | * fptr - A pointer to a void function, or 0 to set as none |
mbed_unsupported | 0:7802a25daf3b | 180 | */ |
mbed_unsupported | 0:7802a25daf3b | 181 | void attach(void (*fptr)(void)); |
mbed_unsupported | 0:7802a25daf3b | 182 | |
mbed_unsupported | 0:7802a25daf3b | 183 | /* Function: attach |
mbed_unsupported | 0:7802a25daf3b | 184 | * Attach a member function to call whenever a serial interrupt is generated |
mbed_unsupported | 0:7802a25daf3b | 185 | * |
mbed_unsupported | 0:7802a25daf3b | 186 | * Variables: |
mbed_unsupported | 0:7802a25daf3b | 187 | * tptr - pointer to the object to call the member function on |
mbed_unsupported | 0:7802a25daf3b | 188 | * mptr - pointer to the member function to be called |
mbed_unsupported | 0:7802a25daf3b | 189 | */ |
mbed_unsupported | 0:7802a25daf3b | 190 | template<typename T> |
mbed_unsupported | 0:7802a25daf3b | 191 | void attach(T* tptr, void (T::*mptr)(void)); |
mbed_unsupported | 0:7802a25daf3b | 192 | |
mbed_unsupported | 0:7802a25daf3b | 193 | #endif |
mbed_unsupported | 0:7802a25daf3b | 194 | |
mbed_unsupported | 0:7802a25daf3b | 195 | protected: |
mbed_unsupported | 0:7802a25daf3b | 196 | PinName _txpin; |
mbed_unsupported | 0:7802a25daf3b | 197 | |
mbed_unsupported | 0:7802a25daf3b | 198 | virtual int _putc(int c); |
mbed_unsupported | 0:7802a25daf3b | 199 | virtual int _getc(void); |
mbed_unsupported | 0:7802a25daf3b | 200 | |
mbed_unsupported | 0:7802a25daf3b | 201 | }; // End class SerialHalfDuplex |
mbed_unsupported | 0:7802a25daf3b | 202 | |
mbed_unsupported | 0:7802a25daf3b | 203 | } // End namespace |
mbed_unsupported | 0:7802a25daf3b | 204 | |
mbed_unsupported | 0:7802a25daf3b | 205 | #endif |
mbed_unsupported | 0:7802a25daf3b | 206 | |
mbed_unsupported | 0:7802a25daf3b | 207 | #endif |