Arduino autoreset (DTR/RTS) with the internal serial bootloader of any STM32 MCU

If you made your own board, post here, unless you built a Maple or Maple mini clone etc
User avatar
Squonk42
Posts: 75
Joined: Wed Feb 27, 2019 5:17 pm
Location: Boredaux, France
OS: Linux
IDE: Arduino, Sloeber, Emacs
Core: Roger's, STM official, bare metal
Board: All
Contact:

Re: Arduino autoreset (DTR/RTS) with the internal serial bootloader of any STM32 MCU

Post by Squonk42 » Sat Nov 09, 2019 7:08 pm

Just4Fun wrote:
Sat Nov 09, 2019 4:16 pm
Squonk42 wrote:
Fri Nov 08, 2019 5:45 am
Why not use something simpler like the the ESP32 Weemos?
Hi Squonk42,
I started from the circuit suggested by Espriff for the ESP8266, that should be the same used for the ESP32 you mentioned. But seeing the behavior of RTS/DTR when using the ST core I ended up that it needed modifications. More, the RTS/DTR behavior is different when a linux host is used with the ST core. I think that the ESP8266/ESP32 cores use a proper script to drive RTS/DTR signals.
This circuit was first used on the NodeMCU board for the ESP8266, here is the truth table right from their schematic:
PleY8PQ.png
PleY8PQ.png (120.22 KiB) Viewed 200 times
And here is the interesting part of the esptool.py script used by the ESP8266/ESP32 Arduino core that I used successfully both under Windows and Linux (please note the specific Windows hack for RTS with the usbser.sys driver:

Code: Select all

    def _setDTR(self, state):
        self._port.setDTR(state)

    def _setRTS(self, state):
        self._port.setRTS(state)
        # Work-around for adapters on Windows using the usbser.sys driver:
        # generate a dummy change to DTR so that the set-control-line-state
        # request is sent with the updated RTS state and the same DTR state
        self._port.setDTR(self._port.dtr)
...
        if mode != 'no_reset':
            self._setDTR(False)  # IO0=HIGH
            self._setRTS(True)   # EN=LOW, chip in reset
            time.sleep(0.1)
            if esp32r0_delay:
                # Some chips are more likely to trigger the esp32r0
                # watchdog reset silicon bug if they're held with EN=LOW
                # for a longer period
                time.sleep(1.2)
            self._setDTR(True)   # IO0=LOW
            self._setRTS(False)  # EN=HIGH, chip out of reset
            if esp32r0_delay:
                # Sleep longer after reset.
                # This workaround only works on revision 0 ESP32 chips,
                # it exploits a silicon bug spurious watchdog reset.
                time.sleep(0.4)  # allow watchdog reset to occur
            time.sleep(0.05)
            self._setDTR(False)  # IO0=HIGH, done
I don't see a reason why this should not work with the STM32.

User avatar
Just4Fun
Posts: 34
Joined: Tue Mar 05, 2019 8:09 am
Location: Dark side of the Moon
OS: Linux, Win7, Win10
IDE: Arduino 1.8.5, 1.8.9
Core: Roger's, STM official
Board: Custom
Contact:

Re: Arduino autoreset (DTR/RTS) with the internal serial bootloader of any STM32 MCU

Post by Just4Fun » Sat Nov 09, 2019 7:20 pm

I've found my notes about DTR/RTS... (unfortunately I didn't saved the screenshot from the scope...)

Here the ESP8266 DTR/RTS behavior using the ESP8266 oore:
20191109_200828.jpg
20191109_200828.jpg (24.33 KiB) Viewed 200 times
here using the ST core under Win 10:
20191109_200713B.jpg
20191109_200713B.jpg (26.24 KiB) Viewed 200 times
they are quite different... and under linux is different from the above too...

User avatar
Just4Fun
Posts: 34
Joined: Tue Mar 05, 2019 8:09 am
Location: Dark side of the Moon
OS: Linux, Win7, Win10
IDE: Arduino 1.8.5, 1.8.9
Core: Roger's, STM official
Board: Custom
Contact:

Re: Arduino autoreset (DTR/RTS) with the internal serial bootloader of any STM32 MCU

Post by Just4Fun » Sat Nov 09, 2019 7:27 pm

Squonk42 wrote:
Sat Nov 09, 2019 7:08 pm
I don't see a reason why this should not work with the STM32.
But you need to "change" the core to add that script, as I think that there isn't any esptool.py script in the ST core.

The assumption was not to change anything in the ST core...

Anyway this is very interesting, as I never messed with these "Arduino internals"...

User avatar
Squonk42
Posts: 75
Joined: Wed Feb 27, 2019 5:17 pm
Location: Boredaux, France
OS: Linux
IDE: Arduino, Sloeber, Emacs
Core: Roger's, STM official, bare metal
Board: All
Contact:

Re: Arduino autoreset (DTR/RTS) with the internal serial bootloader of any STM32 MCU

Post by Squonk42 » Sat Nov 09, 2019 8:57 pm

Yes, of course, the waveforms are different between different MCUs (and even between ESP8266 and ESP32), but the principle is still the same.

In the STM32 oficial core, the upload tools are in the platform.txt file. And the explanation is in the Wiki. It is not difficult to change it to run a new method if required.

Post Reply