Trying to flash ESP32 via USB CDC Black Pill

Working libraries, libraries being ported and related hardware
Post Reply
MomePP
Posts: 5
Joined: Mon Sep 24, 2018 12:31 pm

Trying to flash ESP32 via USB CDC Black Pill

Post by MomePP » Tue Sep 25, 2018 12:44 pm

Greeting for all .! I'm a student from thailand. Please excuse my bad English.

I'm attempting to use Black Pill (STM32F103C8) as an USB2TTL for ESP32 board.
Communication will look like : PC <-> STM32 <-> ESP32

For now, I can send string between PC and ESP via USB virtual serial on STM without any problem.
But I cannot flash ESP through STM. It always say "timed out waiting for packet header".

What I have done:
I find out that when start flashing ESP (using esptool). Esptool(PC) will send 'sync' packet to ESP and ESP will send response packet back to esptool(PC). I already checked my wiring and baudrate(i use 115200 lowest possible to flash ESP and its default baudrate for USB CDC)
In my case, ESP received the 'sync' packet and response it but when response packet reach STM. Its not passthrough the packet via USB to esptool(PC).
I used 'logic analyzer' to detect packet out from ESP and 'Serial Monitor' program for monitor serial packet on my PC.

Image
ESP32 received and responded packet

Image
PC serial monitor - only outgoing packet (flash via STM using USB CDC and 1 HW serial)

Image
The packet that it should be - success flashing (flash via STM but using 2 HW serial for passthrough flash packet)

What I guess:
My best guess for now is USBComposite Library(v0.90) that i used for USB virtual serial can't handle the packet.
Buffer is not enough so the packet has been dropped ??
But I also try copy correct response packet add to static string in STM32 and send out from USB CDC to PC. (test_byte[] in my arduino code)
It still works. My PC received the static response without any character drop.
So I'm not sure about buffer is not enough cause it still send the response packet. I'm so confuse and newbie for USB stuff.

So please guide me. Is that possible to flash ESP32 via USB CDC on STM32 ? or need to customize USB library ??

Image
My arduino code that i used for passthrough packet (USBSerial connect to PC / Serial2 connect to ESP Serial)

Image
My arduino code that can flash succeeded by using 2 HW Serial for passthrough packet

My computer development environment:
Windows 10 Pro Version 1803
Arduino IDE 1.8.7
VSCode with Arduino Extension v0.2.20
Arduino_STM32 Core - https://github.com/rogerclarkmelbourne/Arduino_STM32
USBComposite v0.90 - built-in with Arduino_STM32
STM32 Board - Black Pill STM32F103C8T6
ESP32 Board - ESP32 Devkit V1
Last edited by MomePP on Wed Sep 26, 2018 4:40 am, edited 1 time in total.

stevestrong
Posts: 3053
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany
Contact:

Re: Trying to flash ESP32 via USB CDC Black Pill

Post by stevestrong » Tue Sep 25, 2018 1:52 pm

Original post:
MomePP wrote:
Tue Sep 25, 2018 9:57 am
Hi,
for my project, I need to use both Serial and HID for communicate with PC.(which Native is not support for customize ??)
from the post, that is only USB CDC part which i try to test with the problem i found.
Another thing, I flash STM32 via Serial which doesn't has -DSERIAL_USB flag to triggered native USB stuff.
Maybe I missed something ??

stevestrong
Posts: 3053
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany
Contact:

Re: Trying to flash ESP32 via USB CDC Black Pill

Post by stevestrong » Tue Sep 25, 2018 2:31 pm

You can enable the native USB serial for upload over serial by adding -DSERIAL_USB to this line: https://github.com/rogerclarkmelbourne/ ... s.txt#L375.
Close and reopen Arduino IDE, rebuild the project and upload.

MomePP
Posts: 5
Joined: Mon Sep 24, 2018 12:31 pm

Re: Trying to flash ESP32 via USB CDC Black Pill

Post by MomePP » Wed Sep 26, 2018 4:05 am

stevestrong wrote:
Tue Sep 25, 2018 2:31 pm
You can enable the native USB serial for upload over serial by adding -DSERIAL_USB to this line: https://github.com/rogerclarkmelbourne/ ... s.txt#L375.
Close and reopen Arduino IDE, rebuild the project and upload.
Thanks for your suggestion.

I've try adding -DSERIAL_USB to enable native USB but still cannot flash ESP through with this native one.

Here is what i edit and the result.

Image
adding flag to enable native USB serial for serial upload method.

Image
edit arduino code for the native USB. I use USART3 connected to ESP32 boards. (UART0 of ESP)

Image
Test connection before try to upload through STM. I've programmed ESP to echo what it received.

Image
Command and flags for flashing ESP32.

Image
Result.. Still cannot flash due to sync packet response from ESP can't be reached PC.

Image
'Logic analyzer' which i connect probes to ESP UART0 pins.

Image
The response packet from ESP.

stevestrong
Posts: 3053
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany
Contact:

Re: Trying to flash ESP32 via USB CDC Black Pill

Post by stevestrong » Wed Sep 26, 2018 7:35 am

1. For a quick test, shorten UART3 Rx with Tx and check what data is receiving the PC.

2. I think you can have a buffer overflow. To handle this, use an intermediate buffer where you store the data.

Code: Select all

uint8_t buf1[1024];
uint8_t buf2[1024];
void setup()
{
  Serial.begin();
  Serial3.begin(115200);
}
void loop()
{
  int16_t rx1 = Serial.available();
  if ( rx1>0 ) {
    Serial.read(buf1, rx1);
  }
  int16_t rx2 = Serial3.available();
  if ( rx2>0 ) {
    Serial3.read(buf2, rx2);
  }
  if ( rx1>0 ) {
    Serial3.write(buf1, rx1);
  }
  if ( rx2>0 ) {
    Serial.write(buf2, rx2);
  }
}

MomePP
Posts: 5
Joined: Mon Sep 24, 2018 12:31 pm

Re: Trying to flash ESP32 via USB CDC Black Pill

Post by MomePP » Thu Sep 27, 2018 9:53 am

Sorry for late reply.

After testing again. It still not working.
The result is still the same. ESP response but cannot pass the data through USB.

Code: Select all

uint8_t buf1[1024];
uint8_t buf2[1024];
uint16 rx1, rx2 = 0;
void setup()
{
    Serial.begin();
    Serial3.begin(115200);
}
void loop()
{
    rx1 = Serial.available();
    if (rx1 > 0)
    {
        Serial.read(buf1, rx1);
    }
    rx2 = Serial3.available();
    if (rx2 > 0)
    {
        Serial3.readBytes(buf2, rx2);
    }
    if (rx1 > 0)
    {
        Serial3.write(buf1, rx1);
    }
    if (rx2 > 0)
    {
        Serial.write(buf2, rx2);
    }
}
I think receiving data to buffer maybe not a point because the problem is found when sending data through USB.
The buffer of USB may not enough to handle data to be send out ?

I have tried to log the response packet from ESP to another HW Serial. I can receive the packet as i said with two HW Serial passthrough.

stevestrong
Posts: 3053
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany
Contact:

Re: Trying to flash ESP32 via USB CDC Black Pill

Post by stevestrong » Thu Sep 27, 2018 11:23 am

You still don't have any test case where you check the correctness of data output from ESP sent to STM32.
The data shown in UART0 of ESP does not reflect the data sent out over another serial port of ESP.

This is why I recommended the loopback test.
Just try to send data from PC to STM32 and loop it back to PC:
PC -> USB serial Rx -> UASRT3 output -> UASRT3 input -> USB serial Tx -> PC
without sending it to ESP. Then verify the correctness of the data.
If this pass, then you have a problem of data output from ESP to SMT32.

I have a project where I send huge amount of data over USB serial to PC and have no reception problem at all with that.

MomePP
Posts: 5
Joined: Mon Sep 24, 2018 12:31 pm

Re: Trying to flash ESP32 via USB CDC Black Pill

Post by MomePP » Thu Sep 27, 2018 1:20 pm

stevestrong wrote:
Thu Sep 27, 2018 11:23 am
You still don't have any test case where you check the correctness of data output from ESP sent to STM32.
Ahh, okay that my fault. I didn't tell you all the things i have done.

Actually I already check so many times before post this topic. Also compare to SLIP protocol* to check each byte of packet what it should be.
*Reference from here : https://github.com/espressif/esptool/wi ... l-Protocol

Both are running the same arduino program except passthrough USBSerial or HWSerial.
It's hard to show all the capture packet. So I will add the link for capture files on new reply after I back to my office tmr.
Image
stevestrong wrote:
Thu Sep 27, 2018 11:23 am
This is why I recommended the loopback test.
Just try to send data from PC to STM32 and loop it back to PC:
PC -> USB serial Rx -> UASRT3 output -> UASRT3 input -> USB serial Tx -> PC
without sending it to ESP. Then verify the correctness of the data.
If this pass, then you have a problem of data output from ESP to SMT32.
For loopback test, I already test by use USB2TTL receive passthrough packet directly to PC
PC -> USB serial Rx -> USART3 Tx -> USB2TTL Rx -> PC
This is how I confirm correctness of 'sync' request packet.
Compare the packet between the 'monitor packet'* outgoing from PC and the packet received from USB2TTL.
But I will try on your suggest. Maybe I do something wrong on my testing. I want to confirm all what i done. :?
*I used 'Serial Port Monitor' program to background monitor packet over serial port.

After i tested that I think it must be the problem from another half of communication.
So I think ESP not in this point because it's in bootloader mode and i also try on another ESP boards. It still the same.
Then it end on I post this topic about USBComposite library. This one make me so confused and I don't know where to start.

stevestrong
Posts: 3053
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany
Contact:

Re: Trying to flash ESP32 via USB CDC Black Pill

Post by stevestrong » Thu Sep 27, 2018 1:44 pm

MomePP wrote:
Thu Sep 27, 2018 1:20 pm
For loopback test, I already test by use USB2TTL receive passthrough packet directly to PC
PC -> USB serial Rx -> USART3 Tx -> USB2TTL Rx -> PC
Try also the other way around:
PC -> USB2TTL Tx -> USART3 Rx -> USB serial Tx -> PC

Post Reply