Serial Half Duplex implementation
Dependents: 4dofRoboticArmAX12 2014-Mx64 2014-ax12-test 2014-mx64-test
Fork of SerialHalfDuplex by
SerialHalfDuplex.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2012 ARM Limited 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a copy 00005 * of this software and associated documentation files (the "Software"), to deal 00006 * in the Software without restriction, including without limitation the rights 00007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00008 * copies of the Software, and to permit persons to whom the Software is 00009 * furnished to do so, subject to the following conditions: 00010 * 00011 * The above copyright notice and this permission notice shall be included in 00012 * all copies or substantial portions of the Software. 00013 * 00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 00020 * SOFTWARE. 00021 * 00022 * NOTE: This is an unsupported legacy untested library. 00023 */ 00024 #include "SerialHalfDuplex.h" 00025 00026 #if DEVICE_SERIAL 00027 00028 #include "pinmap.h" 00029 #include "serial_api.h" 00030 #include "gpio_api.h" 00031 00032 namespace mbed { 00033 00034 SerialHalfDuplex::SerialHalfDuplex(PinName tx, PinName rx, const char *name) 00035 : Serial(tx, rx, name) { 00036 _txpin = tx; 00037 00038 // set as input 00039 gpio_set(_txpin); 00040 pin_mode(_txpin, PullNone); // no pull 00041 pin_function(_txpin, 0); // set as gpio 00042 } 00043 00044 // To transmit a byte in half duplex mode: 00045 // 1. Disable interrupts, so we don't trigger on loopback byte 00046 // 2. Set tx pin to UART out 00047 // 3. Transmit byte as normal 00048 // 4. Read back byte from looped back tx pin - this both confirms that the 00049 // transmit has occurred, and also clears the byte from the buffer. 00050 // 5. Return pin to input mode 00051 // 6. Re-enable interrupts 00052 00053 int SerialHalfDuplex::_putc(int c) { 00054 int retc; 00055 00056 // TODO: We should not disable all interrupts 00057 __disable_irq(); 00058 00059 serial_pinout_tx(_txpin); 00060 00061 Serial::_putc(c); 00062 retc = Serial::getc(); // reading also clears any interrupt 00063 00064 pin_function(_txpin, 0); 00065 00066 __enable_irq(); 00067 00068 return retc; 00069 } 00070 00071 int SerialHalfDuplex::_getc(void) { 00072 return Serial::_getc(); 00073 } 00074 00075 } // End namespace 00076 00077 #endif
Generated on Wed Jul 13 2022 09:09:24 by 1.7.2