Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions components/system/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ over HTTPS, or HTTP on localhost; a `file://` copy loses the grant on reload.

- [System Component](#system-component)
- [Protocol (module 7, `espp.system` v1)](#protocol-module-7-esppsystem-v1)
- [Host side: release the device before it resets](#host-side-release-the-device-before-it-resets)
- [Example](#example)

<!-- markdown-toc end -->
Expand Down Expand Up @@ -96,6 +97,33 @@ u32), 22 capabilities (u32: bit0 reboot allowed, bit1 bootloader reboot allowed
and supported). The delay is clamped to at least `Config::min_restart_delay`
so the OK reply leaves the transport before the restart.

## Host side: release the device before it resets

A host that sends **REBOOT_TO_BOOTLOADER** over USB must have let go of the
device before the reset fires. A device that is still open and claimed at that
moment (a WebUSB interface with an IN transfer pending, a pyusb handle, an open
serial port) can leave the host holding a stale record of it; macOS in
particular. Then the ROM's USB device, which has other ids, does not enumerate
until the board is unplugged and plugged back in. To the user it looks as if the
device hung on its way into the bootloader.

So, in a host (a web page, a script, a tool):

1. Ask for a delay long enough to close the connection first: at least
**1000 ms**. The System Console sends `max(1000, the delay box)` for a
bootloader reboot.
2. As soon as the OK reply arrives, stop anything that keeps transfers in
flight (e.g. a monitor stream), release the interface and close the device
(WebUSB: `releaseInterface()` then `close()`; Web Serial: cancel the reader
and `close()` the port). If the link drops before the OK, the device is
already on its way; close your side the same way.
3. Only then wait for the ROM's device: it enumerates with different ids
(USB-Serial-JTAG `303a:1001` or the ROM's USB CDC `303a:0009` on the
ESP32-S3), so look for it rather than reconnecting to the old one.

A plain **REBOOT** comes back with the same ids and is reconnected the usual
way, but the same order (close, then let it reset) never hurts.

## Example

The [example](./example) exposes `SystemService` and `MonitorService` on the
Expand Down
5 changes: 5 additions & 0 deletions components/system/include/detail/system_protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
// 0x01 GET_INFO (no payload)
// 0x02 REBOOT [delay_ms u16]
// 0x03 REBOOT_TO_BOOTLOADER [delay_ms u16]
// A host should ask for a delay long enough to
// close its connection first (>= 1000 ms) and
// close it as soon as the OK arrives: see the
// component README, "Host side: release the
// device before it resets".
// Replies (device -> host, high bit set = frame reply flag):
// 0x81 INFO a list of tagged records [tag u8][len u8][value...]; a host
// skips tags it does not know, so fields can be added without a
Expand Down
15 changes: 14 additions & 1 deletion components/system/web/system_console.html
Original file line number Diff line number Diff line change
Expand Up @@ -1659,8 +1659,17 @@ <h2>Log</h2>
const v = Number.parseInt(els.rebootDelay.value, 10);
return Number.isInteger(v) && v >= 0 && v <= 65535 ? v : 500;
}
// Release the device before it restarts into the ROM bootloader: a WebUSB
// device that is still claimed (an interface and a pending IN transfer)
// while the chip resets can leave the host -- macOS in particular --
// holding a stale record of it, so the ROM's USB device (other ids) does
// not enumerate until the board is replugged. The reset is asked for at
// least this far ahead, and the page closes the device as soon as the
// request is acknowledged, well before it fires. (A plain reboot comes back
// with the same ids and is reconnected as usual.)
const BOOTLOADER_REBOOT_MIN_DELAY_MS = 1000;
async function requestReboot(bootloader) {
const delay = rebootDelayMs();
const delay = bootloader ? Math.max(BOOTLOADER_REBOOT_MIN_DELAY_MS, rebootDelayMs()) : rebootDelayMs();
const type = bootloader ? SYS.REBOOT_TO_BOOTLOADER : SYS.REBOOT;
logLine("tx", "> " + (bootloader ? "reboot into bootloader" : "reboot") + " in " + delay + " ms");
// A plain reboot keeps the device's USB ids: auto-reconnect picks it up
Expand All @@ -1669,6 +1678,10 @@ <h2>Log</h2>
if (bootloader) reconnect.suppress(); else reconnect.expectReboot();
try {
await transact("sys", type, u16(delay), [SYS.OK]);
if (bootloader && transport) {
await disconnect(); // let go of it before it resets (see above)
logLine("sys", "Released the device so it can restart into its ROM download mode.");
}
logLine("sys", "Acknowledged: the device " + (bootloader ?
"restarts into the ROM download mode (it re-enumerates as the ROM's USB CDC / DFU or USB-Serial-JTAG port; use esptool / idf.py flash, then reset it)." :
(els.autoReconnect.checked ? "restarts shortly; reconnecting automatically once it is back." : "restarts shortly. Reconnect once it is back.")));
Expand Down
13 changes: 13 additions & 0 deletions doc/en/system/system.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ it doubles as a serial monitor): a device-info panel, the two restart buttons
:doc:`monitor <../core/monitor>` component's ``MonitorService`` — heap-region
gauges and a live, sortable task table with a stream toggle.

.. note::

**A host must release the device before it resets into the bootloader.** A
device that is still open and claimed when the chip resets (a WebUSB
interface with an IN transfer pending, a pyusb handle, an open serial port)
can leave the host -- macOS in particular -- holding a stale record of it,
so the ROM's USB device (other ids) does not enumerate until the board is
replugged. Ask for a delay of at least 1000 ms with ``REBOOT_TO_BOOTLOADER``,
and as soon as the OK reply arrives stop any stream, release the interface
and close the device; then wait for the ROM's device. The System Console
does exactly this. See the component README's *Host side: release the device
before it resets* for the details.

.. image:: https://github.com/user-attachments/assets/bc5fa520-f3a0-40de-9b53-889a887c0322
:alt: espp System Console: device info, reboot / bootloader controls and heap gauges
:width: 100%
Expand Down
Loading