雄介 太田 / SerialHalf

Fork of SerialHalfDuplex by Aimen Al-Refai

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SerialHalfDuplex.cpp Source File

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 #include "mbed.h"
00033 
00034 DigitalOut led4(LED4);
00035  
00036 namespace mbed {
00037  
00038 SerialHalfDuplex::SerialHalfDuplex(PinName tx, PinName rx, const char *name)
00039     : Serial(tx, rx, name) {
00040     _txpin = tx;
00041     
00042     // set as input 
00043     gpio_set(_txpin); 
00044     pin_mode(_txpin, PullNone); // no pull
00045     pin_function(_txpin, 0);    // set as gpio
00046 }
00047  
00048 // To transmit a byte in half duplex mode:
00049 // 1. Disable interrupts, so we don't trigger on loopback byte
00050 // 2. Set tx pin to UART out
00051 // 3. Transmit byte as normal
00052 // 4. Read back byte from looped back tx pin - this both confirms that the
00053 //    transmit has occurred, and also clears the byte from the buffer.
00054 // 5. Return pin to input mode
00055 // 6. Re-enable interrupts
00056 
00057 Timeout en;
00058 
00059 void SerialHalfDuplex::enable(){
00060 //    printf("timeout enable/n");
00061     __enable_irq();
00062 //    led4 = 1;
00063 //    wait(0.5);
00064 //    led4 = 0;
00065 }
00066 
00067 int SerialHalfDuplex::_putc(int c) {
00068 
00069     int retc;
00070     
00071     en.attach(this, &SerialHalfDuplex::enable, 1.0);
00072     
00073     // TODO: We should not disable all interrupts
00074 //    printf("disable_irq/n");
00075     __disable_irq();
00076     
00077     serial_pinout_tx(_txpin);
00078     
00079     Serial::_putc(c);
00080     retc = Serial::getc();       // reading also clears any interrupt
00081     
00082 
00083     pin_function(_txpin, 0);
00084     
00085     __enable_irq();
00086 //    printf("enable/n");
00087         
00088     return retc;
00089 }
00090  
00091 int SerialHalfDuplex::_getc(void) {
00092     return Serial::_getc();
00093 }
00094  
00095 } // End namespace
00096  
00097 #endif