// This method translates 2 wires (a tx and rx line) to 1 wire, by letting the
// RX line control when data should be read or written from the single line
void usb1WirePassthrough(int8_t escIndex)
{
  // Reset all GPIO
  deinit_gpio(escIndex);
  // Take control of the LEDs
  ledInitDebug();
  //delay(1000);
  disable_hardware_uart();
  init_all_gpio(escIndex);
  // reset all the pins, 1wire goes into input mode, pullup on
  reset_all_gpio(escIndex);

  // set the programmer high
  txSet(Bit_SET);

  // Wait for programmer to go from 1 -> 0 indicating incoming data
  while(rxHi());
  while(1) {
    // A new iteration on this loop starts when we have data from the programmer (read_programmer goes low)
    // Setup escIndex pin to send data, pullup is the default
    gpio_set_mode_escs(escIndex, Mode_Out_PP);
    // Write the first bit
    escSet(escIndex, Bit_RESET);
    // Echo on the programmer tx line
    txSet(Bit_RESET);

    // Wait for programmer to go 0 -> 1
    while(!rxHi());

    // Programmer is high, end of bit
    // Echo to the esc
    escSet(escIndex, Bit_SET);
    // Listen to the escIndex, input mode, pullup resistor is on
    gpio_set_mode_escs(escIndex, Mode_IPU);

    // Listen to the escIndex while there is no data from the programmer
    while (rxHi()) {
      if (escHi(escIndex)) {
        txSet(Bit_SET);
      }
      else {
        txSet(Bit_RESET);
      }
    }
  }
}
// This method translates 2 wires (a tx and rx line) to 1 wire, by letting the
// RX line control when data should be read or written from the single line
void usb1WirePassthrough(uint8_t escIndex)
{
#ifdef STM32F3DISCOVERY
  ledInitDebug();
#endif

  //Disable all interrupts
  __disable_irq();

  // reset all the pins
  GPIO_ResetBits(S1W_RX_GPIO, S1W_RX_PIN);
  GPIO_ResetBits(S1W_TX_GPIO, S1W_TX_PIN);
  // configure gpio
  gpio_set_mode(S1W_RX_GPIO, S1W_RX_PIN, Mode_IPU);
  gpio_set_mode(S1W_TX_GPIO, S1W_TX_PIN, Mode_Out_PP);
  // hey user, turn on your ESC now

#ifdef STM32F10X
  // reset our gpio register pointers and bitmask values
  gpio_prep_vars(escIndex);
#endif

  ESC_OUTPUT(escIndex);
  ESC_SET_HI(escIndex);
  TX_SET_HIGH;
  // Wait for programmer to go from 1 -> 0 indicating incoming data
  while(RX_HI);

  while(1) {
    // A new iteration on this loop starts when we have data from the programmer (read_programmer goes low)
    // Setup escIndex pin to send data, pullup is the default
    ESC_OUTPUT(escIndex);
    // Write the first bit
    ESC_SET_LO(escIndex);
    // Echo on the programmer tx line
    TX_SET_LO;
    //set LEDs
    RX_LED_OFF;
    TX_LED_ON;
    // Wait for programmer to go 0 -> 1
    uint32_t ct=3333;
    while(!RX_HI) {
      if (ct > 0) ct--; // count down until 0;
      // check for low time ->ct=3333 ~600uS //byte LO time for 0 @ 19200 baud -> 9*52 uS => 468.75uS
      // App must send a 0 at 9600 baud (or lower) which has a LO time of at 104uS (or more) > 0 =  937.5uS LO
      // BLHeliSuite will use 4800 baud
    }
    // Programmer is high, end of bit
    // At first Echo to the esc, which helps to charge input capacities at ESC
    ESC_SET_HI(escIndex);
    // Listen to the escIndex, input mode, pullup resistor is on
    gpio_set_mode(escHardware[escIndex].gpio, (1U << escHardware[escIndex].pinpos), Mode_IPU);
    TX_LED_OFF;
    if (ct==0) break; //we reached zero
    // Listen to the escIndex while there is no data from the programmer
    while (RX_HI) {
      if (ESC_HI(escIndex)) {
        TX_SET_HIGH;
        RX_LED_OFF;
      }
      else {
        TX_SET_LO;
        RX_LED_ON;
      }
    }
  }
  // we get here in case ct reached zero
  TX_SET_HIGH;
  RX_LED_OFF;
  // Enable all irq (for Hardware UART)
  __enable_irq();
  return;
}