mbed-os
Fork of mbed-os by
features/unsupported/tests/libs/SerialHalfDuplex/SerialHalfDuplex.cpp@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 | #include "SerialHalfDuplex.h" |
elessair | 0:f269e3021894 | 5 | #include "critical.h" |
elessair | 0:f269e3021894 | 6 | |
elessair | 0:f269e3021894 | 7 | #if DEVICE_SERIAL |
elessair | 0:f269e3021894 | 8 | |
elessair | 0:f269e3021894 | 9 | #include "pinmap.h" |
elessair | 0:f269e3021894 | 10 | #include "serial_api.h" |
elessair | 0:f269e3021894 | 11 | |
elessair | 0:f269e3021894 | 12 | namespace mbed { |
elessair | 0:f269e3021894 | 13 | |
elessair | 0:f269e3021894 | 14 | SerialHalfDuplex::SerialHalfDuplex(PinName tx, PinName rx) |
elessair | 0:f269e3021894 | 15 | : Serial(tx, rx) { |
elessair | 0:f269e3021894 | 16 | |
elessair | 0:f269e3021894 | 17 | gpio_init(&gpio, tx, PIN_INPUT); |
elessair | 0:f269e3021894 | 18 | gpio_mode(&gpio, PullNone); // no pull |
elessair | 0:f269e3021894 | 19 | } |
elessair | 0:f269e3021894 | 20 | |
elessair | 0:f269e3021894 | 21 | // To transmit a byte in half duplex mode: |
elessair | 0:f269e3021894 | 22 | // 1. Disable interrupts, so we don't trigger on loopback byte |
elessair | 0:f269e3021894 | 23 | // 2. Set tx pin to UART out |
elessair | 0:f269e3021894 | 24 | // 3. Transmit byte as normal |
elessair | 0:f269e3021894 | 25 | // 4. Read back byte from looped back tx pin - this both confirms that the |
elessair | 0:f269e3021894 | 26 | // transmit has occurred, and also clears the byte from the buffer. |
elessair | 0:f269e3021894 | 27 | // 5. Return pin to input mode |
elessair | 0:f269e3021894 | 28 | // 6. Re-enable interrupts |
elessair | 0:f269e3021894 | 29 | int SerialHalfDuplex::_putc(int c) { |
elessair | 0:f269e3021894 | 30 | int retc; |
elessair | 0:f269e3021894 | 31 | |
elessair | 0:f269e3021894 | 32 | // TODO: We should not disable all interrupts |
elessair | 0:f269e3021894 | 33 | core_util_critical_section_enter(); |
elessair | 0:f269e3021894 | 34 | |
elessair | 0:f269e3021894 | 35 | serial_pinout_tx(gpio.pin); |
elessair | 0:f269e3021894 | 36 | |
elessair | 0:f269e3021894 | 37 | Serial::_putc(c); |
elessair | 0:f269e3021894 | 38 | retc = Serial::getc(); // reading also clears any interrupt |
elessair | 0:f269e3021894 | 39 | |
elessair | 0:f269e3021894 | 40 | pin_function(gpio.pin, 0); |
elessair | 0:f269e3021894 | 41 | |
elessair | 0:f269e3021894 | 42 | core_util_critical_section_exit(); |
elessair | 0:f269e3021894 | 43 | |
elessair | 0:f269e3021894 | 44 | return retc; |
elessair | 0:f269e3021894 | 45 | } |
elessair | 0:f269e3021894 | 46 | |
elessair | 0:f269e3021894 | 47 | int SerialHalfDuplex::_getc(void) { |
elessair | 0:f269e3021894 | 48 | return Serial::_getc(); |
elessair | 0:f269e3021894 | 49 | } |
elessair | 0:f269e3021894 | 50 | |
elessair | 0:f269e3021894 | 51 | } // End namespace |
elessair | 0:f269e3021894 | 52 | |
elessair | 0:f269e3021894 | 53 | #endif |