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