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