Add attach

Fork of SerialHalfDuplex by Aimen Al-Refai

Committer:
yusuke_kyo
Date:
Sun May 03 16:17:06 2015 +0000
Revision:
3:a5021aaf31d9
Parent:
2:5ecc72a47df0
add attach

Who changed what in which revision?

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