I'm trying to upload the sketch on a Mega 2560 via the serial port 0 using another microcontroller.

To better undestand the process, I decided to first observe what avrdude is doing while it is uploading a hex file. To do this, I'm using another arduino (ESP32) with the following sketch:

void setup()

void loop()
    while (Serial2.available()) {
        Serial.print(Serial2.read(), HEX);

ESP32's Serial(serial0) is connected to PC via USB, and Serial2 is connected to Mega's Serial(serial0), the one connected to PC via USB, used by avrdude. enter image description here According to this answer the first things avrdude is sending over the serial interface are:

STK_GET_SYNC / CRC_EOP  (0x30/0x20)

But this is not what I receive through ESP32:


I can't find 30:20 anywhere. What am I missing?

The baud rate is 115200 everywhere which I believe is the right one. The link is good because if I send char 'A' from PC over the serial connected to Mega, I get 41: via ESP32 on the other side, which is correct.

  • Are both ESP32's serial ports implemented in hardware? Do the research because if 1 is implemented in software the ESP32 may not be able to echo back everything it sees on the hardware serial port. – st2000 Oct 13 '19 at 14:53
  • the bootloader responses on TX. and do you reset the board? the avrdude uses stk500 protocol, which is documented, no need to reverse engineer it – Juraj Oct 13 '19 at 15:06
  • @st2000 According to online docs they are both hardware. It's also labeled on the board and when I send a random char from PC to Mega, I receive the correct value via ESP32, but when I look at what I receive during sketch upload, makes no sense. @Juraj I'm interested in what avrdude is sending first, which should be 0x30,0x20, not 0x1B,0x01. I don't reset the board, the usb-to-serial chip already does it. The sketch upload from PC works. I just want to confirm the the first thing it sends is 0x30,0x20 to help me going. – Chris Oct 13 '19 at 15:12
  • Agreed, online it says the ESP32 has 3 hardware UARTs. But when the Arduino development environment was ported to the ESP processor, did it actually use the hardware UARTs? And if it did, how well implemented is the firmware? For that matter, how well is the ESP's hardware implemented? The type of problem I am concerned about occurs when many bytes are transferred rapidly. Of course the 1st byte, for such problems, should always make it through. Also, I agree with @Juraj. Use the existing documentation to develop you own programmer. Nothing says that what avrdude does is correct. – st2000 Oct 13 '19 at 15:37
  • 1
    The answer you link to is about the Uno. The Mega uses a different bootloader and, IIRC, a different protocol. – Edgar Bonet Oct 13 '19 at 17:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.