SerialHalfDuplex.h 2.0 KB

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