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