ON Semiconductor / mbed-os

Dependents:   mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SerialHalfDuplex.cpp Source File

SerialHalfDuplex.cpp

00001 /* mbed Microcontroller Library - SerialHalfDuplex
00002  * Copyright (c) 2010-2011 ARM Limited. All rights reserved.
00003  */
00004 #include "SerialHalfDuplex.h"
00005 #include "critical.h"
00006 
00007 #if DEVICE_SERIAL
00008 
00009 #include "pinmap.h"
00010 #include "serial_api.h"
00011 
00012 namespace mbed {
00013 
00014 SerialHalfDuplex::SerialHalfDuplex(PinName tx, PinName rx)
00015     : Serial(tx, rx) {
00016 
00017     gpio_init(&gpio, tx, PIN_INPUT);
00018     gpio_mode(&gpio, PullNone); // no pull
00019 }
00020 
00021 // To transmit a byte in half duplex mode:
00022 // 1. Disable interrupts, so we don't trigger on loopback byte
00023 // 2. Set tx pin to UART out
00024 // 3. Transmit byte as normal
00025 // 4. Read back byte from looped back tx pin - this both confirms that the
00026 //    transmit has occurred, and also clears the byte from the buffer.
00027 // 5. Return pin to input mode
00028 // 6. Re-enable interrupts
00029 int SerialHalfDuplex::_putc(int c) {
00030     int retc;
00031 
00032     // TODO: We should not disable all interrupts
00033     core_util_critical_section_enter();
00034 
00035     serial_pinout_tx(gpio.pin);
00036 
00037     Serial::_putc(c);
00038     retc = Serial::getc();       // reading also clears any interrupt
00039 
00040     pin_function(gpio.pin, 0);
00041 
00042     core_util_critical_section_exit();
00043 
00044     return retc;
00045 }
00046 
00047 int SerialHalfDuplex::_getc(void) {
00048     return Serial::_getc();
00049 }
00050 
00051 } // End namespace
00052 
00053 #endif