mbed-os
Fork of mbed-os by
features/unsupported/tests/libs/SerialHalfDuplex/SerialHalfDuplex.h@1:3deb71413561, 2017-07-20 (annotated)
- Committer:
- xuaner
- Date:
- Thu Jul 20 14:26:57 2017 +0000
- Revision:
- 1:3deb71413561
- Parent:
- 0:f269e3021894
mbed_os
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elessair | 0:f269e3021894 | 1 | /* mbed Microcontroller Library - SerialHalfDuplex |
elessair | 0:f269e3021894 | 2 | * Copyright (c) 2010-2011 ARM Limited. All rights reserved. |
elessair | 0:f269e3021894 | 3 | */ |
elessair | 0:f269e3021894 | 4 | #ifndef MBED_SERIALHALFDUPLEX_H |
elessair | 0:f269e3021894 | 5 | #define MBED_SERIALHALFDUPLEX_H |
elessair | 0:f269e3021894 | 6 | |
elessair | 0:f269e3021894 | 7 | #include "platform.h" |
elessair | 0:f269e3021894 | 8 | |
elessair | 0:f269e3021894 | 9 | #if DEVICE_SERIAL |
elessair | 0:f269e3021894 | 10 | |
elessair | 0:f269e3021894 | 11 | #include "Serial.h" |
elessair | 0:f269e3021894 | 12 | #include "gpio_api.h" |
elessair | 0:f269e3021894 | 13 | |
elessair | 0:f269e3021894 | 14 | namespace mbed { |
elessair | 0:f269e3021894 | 15 | |
elessair | 0:f269e3021894 | 16 | /** A serial port (UART) for communication with other devices using |
elessair | 0:f269e3021894 | 17 | * Half-Duplex, allowing transmit and receive on a single |
elessair | 0:f269e3021894 | 18 | * shared transmit and receive line. Only one end should be transmitting |
elessair | 0:f269e3021894 | 19 | * at a time. |
elessair | 0:f269e3021894 | 20 | * |
elessair | 0:f269e3021894 | 21 | * Both the tx and rx pin should be defined, and wired together. |
elessair | 0:f269e3021894 | 22 | * This is in addition to them being wired to the other serial |
elessair | 0:f269e3021894 | 23 | * device to allow both read and write functions to operate. |
elessair | 0:f269e3021894 | 24 | * |
elessair | 0:f269e3021894 | 25 | * For Simplex and Full-Duplex Serial communication, see Serial() |
elessair | 0:f269e3021894 | 26 | * |
elessair | 0:f269e3021894 | 27 | * Example: |
elessair | 0:f269e3021894 | 28 | * @code |
elessair | 0:f269e3021894 | 29 | * // Send a byte to a second HalfDuplex device, and read the response |
elessair | 0:f269e3021894 | 30 | * |
elessair | 0:f269e3021894 | 31 | * #include "mbed.h" |
elessair | 0:f269e3021894 | 32 | * |
elessair | 0:f269e3021894 | 33 | * // p9 and p10 should be wired together to form "a" |
elessair | 0:f269e3021894 | 34 | * // p28 and p27 should be wired together to form "b" |
elessair | 0:f269e3021894 | 35 | * // p9/p10 should be wired to p28/p27 as the Half Duplex connection |
elessair | 0:f269e3021894 | 36 | * |
elessair | 0:f269e3021894 | 37 | * SerialHalfDuplex a(p9, p10); |
elessair | 0:f269e3021894 | 38 | * SerialHalfDuplex b(p28, p27); |
elessair | 0:f269e3021894 | 39 | * |
elessair | 0:f269e3021894 | 40 | * void b_rx() { // second device response |
elessair | 0:f269e3021894 | 41 | * b.putc(b.getc() + 4); |
elessair | 0:f269e3021894 | 42 | * } |
elessair | 0:f269e3021894 | 43 | * |
elessair | 0:f269e3021894 | 44 | * int main() { |
elessair | 0:f269e3021894 | 45 | * b.attach(&b_rx); |
elessair | 0:f269e3021894 | 46 | * for (int c = 'A'; c < 'Z'; c++) { |
elessair | 0:f269e3021894 | 47 | * a.putc(c); |
elessair | 0:f269e3021894 | 48 | * printf("sent [%c]\n", c); |
elessair | 0:f269e3021894 | 49 | * wait(0.5); // b should respond |
elessair | 0:f269e3021894 | 50 | * if (a.readable()) { |
elessair | 0:f269e3021894 | 51 | * printf("received [%c]\n", a.getc()); |
elessair | 0:f269e3021894 | 52 | * } |
elessair | 0:f269e3021894 | 53 | * } |
elessair | 0:f269e3021894 | 54 | * } |
elessair | 0:f269e3021894 | 55 | * @endcode |
elessair | 0:f269e3021894 | 56 | */ |
elessair | 0:f269e3021894 | 57 | class SerialHalfDuplex : public Serial { |
elessair | 0:f269e3021894 | 58 | |
elessair | 0:f269e3021894 | 59 | public: |
elessair | 0:f269e3021894 | 60 | /** Create a half-duplex serial port, connected to the specified transmit |
elessair | 0:f269e3021894 | 61 | * and receive pins. |
elessair | 0:f269e3021894 | 62 | * |
elessair | 0:f269e3021894 | 63 | * These pins should be wired together, as well as to the target device |
elessair | 0:f269e3021894 | 64 | * |
elessair | 0:f269e3021894 | 65 | * @param tx Transmit pin |
elessair | 0:f269e3021894 | 66 | * @param rx Receive pin |
elessair | 0:f269e3021894 | 67 | */ |
elessair | 0:f269e3021894 | 68 | SerialHalfDuplex(PinName tx, PinName rx); |
elessair | 0:f269e3021894 | 69 | |
elessair | 0:f269e3021894 | 70 | protected: |
elessair | 0:f269e3021894 | 71 | gpio_object gpio; |
elessair | 0:f269e3021894 | 72 | |
elessair | 0:f269e3021894 | 73 | virtual int _putc(int c); |
elessair | 0:f269e3021894 | 74 | virtual int _getc(void); |
elessair | 0:f269e3021894 | 75 | |
elessair | 0:f269e3021894 | 76 | }; // End class SerialHalfDuplex |
elessair | 0:f269e3021894 | 77 | |
elessair | 0:f269e3021894 | 78 | } // End namespace |
elessair | 0:f269e3021894 | 79 | |
elessair | 0:f269e3021894 | 80 | #endif |
elessair | 0:f269e3021894 | 81 | |
elessair | 0:f269e3021894 | 82 | #endif |