Fork of SerialHalfDuplex library

Dependents:   PR_RobotArm lighthouse_2

Fork of SerialHalfDuplex by Aimen Al-Refai

Committer:
jah128
Date:
Thu Feb 16 23:58:49 2017 +0000
Revision:
3:ff073057f373
Parent:
2:5ecc72a47df0
Added comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jah128 3:ff073057f373 1 /* NB This is part of an older MBED library, included so the older code runs on the
jah128 3:ff073057f373 2 * newer MBED library. There is probably a better implementation.... - jah */
jah128 3:ff073057f373 3
aimen 2:5ecc72a47df0 4 /* mbed Microcontroller Library
GIPetrou 1:8fd1fa67565e 5 * Copyright (c) 2006-2012 ARM Limited
GIPetrou 1:8fd1fa67565e 6 *
GIPetrou 1:8fd1fa67565e 7 * Permission is hereby granted, free of charge, to any person obtaining a copy
GIPetrou 1:8fd1fa67565e 8 * of this software and associated documentation files (the "Software"), to deal
GIPetrou 1:8fd1fa67565e 9 * in the Software without restriction, including without limitation the rights
GIPetrou 1:8fd1fa67565e 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
GIPetrou 1:8fd1fa67565e 11 * copies of the Software, and to permit persons to whom the Software is
GIPetrou 1:8fd1fa67565e 12 * furnished to do so, subject to the following conditions:
GIPetrou 1:8fd1fa67565e 13 *
GIPetrou 1:8fd1fa67565e 14 * The above copyright notice and this permission notice shall be included in
GIPetrou 1:8fd1fa67565e 15 * all copies or substantial portions of the Software.
GIPetrou 1:8fd1fa67565e 16 *
GIPetrou 1:8fd1fa67565e 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
GIPetrou 1:8fd1fa67565e 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
GIPetrou 1:8fd1fa67565e 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
GIPetrou 1:8fd1fa67565e 20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
GIPetrou 1:8fd1fa67565e 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
GIPetrou 1:8fd1fa67565e 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
GIPetrou 1:8fd1fa67565e 23 * SOFTWARE.
GIPetrou 1:8fd1fa67565e 24 *
GIPetrou 1:8fd1fa67565e 25 * NOTE: This is an unsupported legacy untested library.
GIPetrou 1:8fd1fa67565e 26 */
GIPetrou 1:8fd1fa67565e 27 #include "SerialHalfDuplex.h"
aimen 2:5ecc72a47df0 28
GIPetrou 1:8fd1fa67565e 29 #if DEVICE_SERIAL
aimen 2:5ecc72a47df0 30
GIPetrou 1:8fd1fa67565e 31 #include "pinmap.h"
GIPetrou 1:8fd1fa67565e 32 #include "serial_api.h"
GIPetrou 1:8fd1fa67565e 33 #include "gpio_api.h"
aimen 2:5ecc72a47df0 34
GIPetrou 1:8fd1fa67565e 35 namespace mbed {
aimen 2:5ecc72a47df0 36
GIPetrou 1:8fd1fa67565e 37 SerialHalfDuplex::SerialHalfDuplex(PinName tx, PinName rx, const char *name)
GIPetrou 1:8fd1fa67565e 38 : Serial(tx, rx, name) {
GIPetrou 1:8fd1fa67565e 39 _txpin = tx;
GIPetrou 1:8fd1fa67565e 40
aimen 2:5ecc72a47df0 41 // set as input
GIPetrou 1:8fd1fa67565e 42 gpio_set(_txpin);
GIPetrou 1:8fd1fa67565e 43 pin_mode(_txpin, PullNone); // no pull
GIPetrou 1:8fd1fa67565e 44 pin_function(_txpin, 0); // set as gpio
GIPetrou 1:8fd1fa67565e 45 }
aimen 2:5ecc72a47df0 46
GIPetrou 1:8fd1fa67565e 47 // To transmit a byte in half duplex mode:
GIPetrou 1:8fd1fa67565e 48 // 1. Disable interrupts, so we don't trigger on loopback byte
GIPetrou 1:8fd1fa67565e 49 // 2. Set tx pin to UART out
GIPetrou 1:8fd1fa67565e 50 // 3. Transmit byte as normal
GIPetrou 1:8fd1fa67565e 51 // 4. Read back byte from looped back tx pin - this both confirms that the
GIPetrou 1:8fd1fa67565e 52 // transmit has occurred, and also clears the byte from the buffer.
GIPetrou 1:8fd1fa67565e 53 // 5. Return pin to input mode
GIPetrou 1:8fd1fa67565e 54 // 6. Re-enable interrupts
aimen 2:5ecc72a47df0 55
GIPetrou 1:8fd1fa67565e 56 int SerialHalfDuplex::_putc(int c) {
GIPetrou 1:8fd1fa67565e 57 int retc;
GIPetrou 1:8fd1fa67565e 58
GIPetrou 1:8fd1fa67565e 59 // TODO: We should not disable all interrupts
GIPetrou 1:8fd1fa67565e 60 __disable_irq();
GIPetrou 1:8fd1fa67565e 61
GIPetrou 1:8fd1fa67565e 62 serial_pinout_tx(_txpin);
GIPetrou 1:8fd1fa67565e 63
GIPetrou 1:8fd1fa67565e 64 Serial::_putc(c);
GIPetrou 1:8fd1fa67565e 65 retc = Serial::getc(); // reading also clears any interrupt
GIPetrou 1:8fd1fa67565e 66
GIPetrou 1:8fd1fa67565e 67 pin_function(_txpin, 0);
GIPetrou 1:8fd1fa67565e 68
GIPetrou 1:8fd1fa67565e 69 __enable_irq();
GIPetrou 1:8fd1fa67565e 70
GIPetrou 1:8fd1fa67565e 71 return retc;
GIPetrou 1:8fd1fa67565e 72 }
aimen 2:5ecc72a47df0 73
GIPetrou 1:8fd1fa67565e 74 int SerialHalfDuplex::_getc(void) {
GIPetrou 1:8fd1fa67565e 75 return Serial::_getc();
GIPetrou 1:8fd1fa67565e 76 }
aimen 2:5ecc72a47df0 77
GIPetrou 1:8fd1fa67565e 78 } // End namespace
aimen 2:5ecc72a47df0 79
aimen 2:5ecc72a47df0 80 #endif