Flashing 8266 using Serial Relay and esptool

I am trying to flash 8266 using Mega 32U4, which is programmed to act as a serial relay.
This fail, and I have no idea why.
Here is the scenario:
flashing the 8266 with FTDI works like a breeze.
I then hook up the Mega32U4 => ESP like this:
3.3V => VCC
GND => GND
TX => RX
RX => TX
and upload to the Mega32U4 a serial relay program:

void setup() {
Serial.begin(115200);
Serial1.begin(115200);
}

void loop() {
while(Serial1.available())
Serial.write((uint8_t)Serial1.read());

while(Serial.available())
Serial1.write((uint8_t)Serial.read());
}

I then attempt to use the esptool again, this time, instead of the FTDI, I have the Mega32U4 connected, but I keep getting this error:

`Connecting...

A fatal error occurred: Failed to connect to ESP8266

Any idea what I am doing wrong?

Hi,

why type casting, just send what you get.

void loop() {
while(Serial1.available())
    Serial.write(Serial1.read());

while(Serial.available()) 
     Serial1.write(Serial.read());
}

Um, is the mega32u4 running at 3.3v? The ESP8266 is not 5v tolerant - so if you have the serial lines connected directly, and the mega32u4 is running at 5v, that's not a good thing.

Also, did you do the reset correctly? How are you doing the reset and holding the right IO line in the right state to make it boot into flash mode, instead of running the previously uploaded code?