NabuCasa / esp-web-flasher

A web serial package for updating your ESP bootloader via the browser.
https://nabucasa.github.io/esp-web-flasher/
MIT License
43 stars 16 forks source link

Changes to be able to set higher Baud Rates #13

Closed conradopoole closed 3 years ago

conradopoole commented 3 years ago

Loved you presentation the other day on the livestream and wanted to contribute to try and fix the flashing speed.

The setBaudRate was not working because the SerialPort needs to be reconfigured after sending the CHANGE_BAUDRATE operation.

Since the WebSerial does not allow for the port to be reconfigured, it has first to be closed, and then reopened with the new BaudRate, this means we need to stop the ReadLoop in order to be able to close the Reader, reopen the port and restart the read loop.

With the setBaudrate method working, ESP-web-tools can now call it right after initialisation so that it does not flash at the default speed of 115200 which was causing the slowness.

For instance I was able to use it to change baud rate to 1100000 on an ESP32 board that has a CP210X chip. For some reason I can change baud rate to 2Mbps (2000000) on PlatformIO when flashing, but not with WebSerial.

See screen capture. On flash.ts in esp-web-tools await esploader.initialize(); await esploader.setBaudrate(1100000); In ESP-web-tools some changes would be needed to perhaps make the baud rate configurable, or at least set it always to something like 921600 which most if not all USB to Serial controllers can do.

PS: My coding may not be the cleanest but I think at least it depicts what would need to be done to be able to reopen the port.

TD-er commented 3 years ago

In ESP-web-tools some changes would be needed to perhaps make the baud rate configurable, or at least set it always to something like 921600 which most if not all USB to Serial controllers can do.

Nope, I have seen CH340 boards which do fail, or at least program unreliable at that rate.

conradopoole commented 3 years ago

I guess it'd be about finding a value that works on most then. Anything faster than 115200 is a win... unless it's made configurable from the web UI but that might complicate things for the end user I guess...

silbo commented 3 years ago

@conradopoole thanks a lot for this code. I was also trying to figure out how to use higher baud rates. As @TD-er said the 921600 does not work on my board that has the CH340C USB to Serial IC. But at least I can use 460800 which is much faster. Although I still notice quite a speed difference when I run the same (460800 baud) with esptool.py (is faster). I quess something to do with the WebSerial limitations ...

I had to make this change to make it work, esptool.py also sends the old baud rate: let buffer = this.pack("<II", baud, 115200);

conradopoole commented 3 years ago

@silbo Thank you that did the trick. I noticed that I could change the baud rate before running the STUB but esptool.py changes baud rate once the STUB is running. So trying to change it with my old code did not work on the STUB, I almost went crazy last night, and then as soon as I added the old baud rate it started working (there is some difference between the boot loader and the STUB on how they handle this command).

Now it also works at 2000000 baud rate on my CP2102 based chip.

I also recommend that the change baud reate is done on the STUB not on the boot loader, that is how esptool.py seems to be doing it. So in esp-web-tools instead of doing:

await esploader.initialize(); await esploader.setBaudrate(1100000);

Wait for the stub to run and then call the setBaudrate on the espstub object instead: const espStub = await esploader.runStub(); await espStub.setBaudrate(2000000);

TD-er commented 3 years ago

[...] Although I still notice quite a speed difference when I run the same (460800 baud) with esptool.py (is faster). I quess something to do with the WebSerial limitations ...

esptool.py does compress the files that need to be transferred. This typically reduces the amount of data to be sent with 30%.

balloob commented 3 years ago

Next release is already going to be faster. @conradopoole figured out we could increase the flash write size, added in #14. It's a lot faster now as we have to touch the event loop less.