Phillip Pearson - web + electronics notes

tech notes and web hackery from a new zealander who was vaguely useful on the web back in 2002 (see: python community server, the blogging ecosystem, the new zealand coffee review, the internet topic exchange).

2016-2-13

Using a Teensy as a USB-Serial bridge to program an ESP8266

Hopefully in future I'll be able to program my ESP8266 modules using JTAG, but I have a project from last year which used the ESP8266, didn't bring out the JTAG pins, and tied GPIO15 (TDO) to ground, which means it would require desoldering the ESP-03 module to fix. I programmed it using the ESP8266 Arduino extension and a Sparkfun FTDI Basic Breakout board, which unfortunately doesn't bring out RTS, which meant for a lot of power cycling. To make matters worse, the FT232 seems to crash sometimes when subjected to this sort of thing, losing control of its DTR output and needing power cycling *itself*.

I happen to have a couple of Teensy 3.2 boards here, however, and they're super easy to program up to act as a more flexible serial adapter. Here's the sketch I ended up with, which happily programs my ESP-03 at the max upload speed selectable in the ESP8266 Arduino IDE, 921600 baud, and with any luck will work with the ESP GDB stub:

// Simple USB-Serial bridge for programming ESP8266 modules

// new connector:
// GND (black) RXD (grey)  GPIO0 (brown)
// 5V (n/c)    TXD (white) RTS (purple)

// RX2 = pin 9, white wire, connects to ESP8266 TX [saleae ch0]
// TX2 = pin 10, grey wire, connects to ESP8266 RX [saleae ch1]
#define HWSerial Serial2
// DTR = pin 11, brown wire, connects to GPIO0 on the ESP8266 [saleae ch2]
#define DTR_PIN 11
// RTS = pin 12, purple wire, connects to CH_PD on the ESP8266 [saleae ch3]
#define RTS_PIN 12

uint32_t current_baud = 9600;
uint8_t current_dtr = -1, current_rts = -1;

void setup() {
  Serial.begin(9600); // fake value -- USB always 12Mbit
  HWSerial.begin(9600);
  pinMode(RTS_PIN, OUTPUT);
  digitalWrite(RTS_PIN, LOW);
  pinMode(DTR_PIN, OUTPUT);
  digitalWrite(DTR_PIN, HIGH);
}

void loop() {
  if (Serial.available() > 0) {
    uint8_t c = Serial.read();
    HWSerial.write(c);
  }
  while (HWSerial.available() > 0) {
    Serial.write(HWSerial.read());
  }
  // update params
  uint32_t baud = Serial.baud();
  if (baud != current_baud) {
    current_baud = baud;
    HWSerial.begin(current_baud);
  }
  uint8_t rts = Serial.rts();
  if (rts != current_rts) {
    current_rts = rts;
    digitalWrite(RTS_PIN, current_rts ? LOW : HIGH);
  }
  uint8_t dtr = Serial.dtr();
  if (dtr != current_dtr) {
    current_dtr = dtr;
    digitalWrite(DTR_PIN, current_dtr ? LOW : HIGH);
  }
}
... more like this: [, ]