SerialHalfDuplex.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /* mbed Microcontroller Library - SerialHalfDuplex
  2. * Copyright (c) 2010-2011 ARM Limited. All rights reserved.
  3. */
  4. #include "SerialHalfDuplex.h"
  5. #if DEVICE_SERIAL
  6. #include "pinmap.h"
  7. #include "serial_api.h"
  8. namespace mbed {
  9. SerialHalfDuplex::SerialHalfDuplex(PinName tx, PinName rx)
  10. : Serial(tx, rx) {
  11. gpio_init(&gpio, tx, PIN_INPUT);
  12. gpio_mode(&gpio, PullNone); // no pull
  13. }
  14. // To transmit a byte in half duplex mode:
  15. // 1. Disable interrupts, so we don't trigger on loopback byte
  16. // 2. Set tx pin to UART out
  17. // 3. Transmit byte as normal
  18. // 4. Read back byte from looped back tx pin - this both confirms that the
  19. // transmit has occurred, and also clears the byte from the buffer.
  20. // 5. Return pin to input mode
  21. // 6. Re-enable interrupts
  22. int SerialHalfDuplex::_putc(int c) {
  23. int retc;
  24. // TODO: We should not disable all interrupts
  25. __disable_irq();
  26. serial_pinout_tx(gpio.pin);
  27. Serial::_putc(c);
  28. retc = Serial::getc(); // reading also clears any interrupt
  29. pin_function(gpio.pin, 0);
  30. __enable_irq();
  31. return retc;
  32. }
  33. int SerialHalfDuplex::_getc(void) {
  34. return Serial::_getc();
  35. }
  36. } // End namespace
  37. #endif