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