Corentin Potron / SerialHalfDuplex

Fork of SerialHalfDuplex by mbed unsupported

Committer:
cpotron
Date:
Sun Mar 25 20:38:37 2018 +0000
Revision:
1:7ce196329ced
Parent:
0:7802a25daf3b
Child:
2:242a54f3629b
Initial Commit

Who changed what in which revision?

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