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