10000 ports/esp32: Add automatic bootloader handling for S2 and S3. · micropython/micropython@77e1ec4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 77e1ec4

Browse files
committed
ports/esp32: Add automatic bootloader handling for S2 and S3.
Enables support for the ESP standard DTR/RTS based reboot to bootloader. Switches from OTG to Serial/Jtag mode to workaround issue discussed in: espressif/arduino-esp32#6762 Signed-off-by: Andrew Leech <andrew@alelec.net>
1 parent 4929cd0 commit 77e1ec4

File tree

15 files changed

+54
-12
lines changed

15 files changed

+54
-12
lines changed

ports/esp32/boards/ESP32_GENERIC_S3/sdkconfig.board

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
CONFIG_ESPTOOLPY_FLASHMODE_QIO=y
22
CONFIG_ESPTOOLPY_FLASHFREQ_80M=y
3-
CONFIG_ESPTOOLPY_AFTER_NORESET=y
43

54
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=
65
CONFIG_ESPTOOLPY_FLASHSIZE_8MB=y

ports/esp32/boards/UM_FEATHERS2/sdkconfig.board

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
CONFIG_ESPTOOLPY_FLASHMODE_QIO=y
22
CONFIG_ESPTOOLPY_FLASHFREQ_80M=y
3-
CONFIG_ESPTOOLPY_AFTER_NORESET=y
43

54
CONFIG_SPIRAM_MEMTEST=
65

ports/esp32/boards/UM_FEATHERS2NEO/sdkconfig.board

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
CONFIG_ESPTOOLPY_FLASHMODE_QIO=y
22
CONFIG_ESPTOOLPY_FLASHFREQ_80M=y
3-
CONFIG_ESPTOOLPY_AFTER_NORESET=y
43

54
# LWIP
65
CONFIG_LWIP_LOCAL_HOSTNAME="UMFeatherS2Neo"

ports/esp32/boards/UM_FEATHERS3/sdkconfig.board

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
CONFIG_ESPTOOLPY_FLASHMODE_QIO=y
22
CONFIG_ESPTOOLPY_FLASHFREQ_80M=y
3-
CONFIG_ESPTOOLPY_AFTER_NORESET=y
43

54
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=
65
CONFIG_ESPTOOLPY_FLASHSIZE_8MB=

ports/esp32/boards/UM_NANOS3/sdkconfig.board

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
CONFIG_ESPTOOLPY_FLASHMODE_QIO=y
22
CONFIG_ESPTOOLPY_FLASHFREQ_80M=y
3-
CONFIG_ESPTOOLPY_AFTER_NORESET=y
43

54
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=
65
CONFIG_ESPTOOLPY_FLASHSIZE_8MB=y

ports/esp32/boards/UM_PROS3/sdkconfig.board

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
CONFIG_ESPTOOLPY_FLASHMODE_QIO=y
22
CONFIG_ESPTOOLPY_FLASHFREQ_80M=y
3-
CONFIG_ESPTOOLPY_AFTER_NORESET=y
43

54
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=
65
CONFIG_ESPTOOLPY_FLASHSIZE_8MB=

ports/esp32/boards/UM_TINYS2/sdkconfig.board

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
CONFIG_ESPTOOLPY_FLASHMODE_QIO=y
22
CONFIG_ESPTOOLPY_FLASHFREQ_80M=y
3-
CONFIG_ESPTOOLPY_AFTER_NORESET=y
43

54
# LWIP
65
CONFIG_LWIP_LOCAL_HOSTNAME="UMTinyS2"

ports/esp32/boards/UM_TINYS3/sdkconfig.board

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
CONFIG_ESPTOOLPY_FLASHMODE_QIO=y
22
CONFIG_ESPTOOLPY_FLASHFREQ_80M=y
3-
CONFIG_ESPTOOLPY_AFTER_NORESET=y
43

54
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=
65
CONFIG_ESPTOOLPY_FLASHSIZE_8MB=y

ports/esp32/boards/UM_TINYWATCHS3/sdkconfig.board

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
CONFIG_ESPTOOLPY_FLASHMODE_QIO=y
22
CONFIG_ESPTOOLPY_FLASHFREQ_80M=y
3-
CONFIG_ESPTOOLPY_AFTER_NORESET=y
43

54
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=
65
CONFIG_ESPTOOLPY_FLASHSIZE_8MB=y

ports/esp32/modmachine.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,19 @@ static mp_int_t mp_machine_reset_cause(void) {
222222

223223
#if MICROPY_ESP32_USE_BOOTLOADER_RTC
224224
#include "soc/rtc_cntl_reg.h"
225+
#include "usb.h"
226+
#if CONFIG_IDF_TARGET_ESP32S3
227+
#include "esp32s3/rom/usb/usb_dc.h"
228+
#include "esp32s3/rom/usb/usb_persist.h"
229+
#include "esp32s3/rom/usb/chip_usb_dw_wrapper.h"
230+
#endif
231+
225232
NORETURN static void machine_bootloader_rtc(void) {
233+
#if CONFIG_IDF_TARGET_ESP32S3
234+
usb_usj_mode();
235+
usb_dc_prepare_persist();
236+
chip_usb_set_persist_flags(USBDC_BOOT_DFU);
237+
#endif
226238
REG_WRITE(RTC_CNTL_OPTION1_REG, RTC_CNTL_FORCE_DOWNLOAD_BOOT);
227239
esp_restart();
228240
}

ports/esp32/mpconfigport.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,8 @@
202202
#endif
203203

204204
#if MICROPY_HW_ENABLE_USBDEV
205-
#define MICROPY_SCHEDULER_STATIC_NODES (1)
205+
#define MICROPY_SCHEDULER_STATIC_NODES (1)
206+
#define MICROPY_HW_USB_CDC_DTR_RTS_BOOTLOADER (1)
206207

207208
// Enable USB-CDC serial port
208209
#ifndef MICROPY_HW_USB_CDC

ports/esp32/usb.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@ void usb_init(void) {
8181

8282
}
8383

84+
#if CONFIG_IDF_TARGET_ESP32S3
85+
void usb_usj_mode(void) {
86+
// Switch the USB PHY back to Serial/Jtag mode, disabling OTG support
87+
// This should be run before jumping to bootloader.
88+
usb_del_phy(phy_hdl);
89+
usb_phy_config_t phy_conf = {
90+
.controller = USB_PHY_CTRL_SERIAL_JTAG,
91+
};
92+
usb_new_phy(&phy_conf, &phy_hdl);
93+
}
94+
#endif
95+
8496
void mp_usbd_port_get_serial_number(char *serial_buf) {
8597
// use factory default MAC as serial ID
8698
uint8_t mac[8];

ports/esp32/usb.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@
2929
#define MICROPY_HW_USB_CDC_TX_TIMEOUT_MS (500)
3030

3131
void usb_init(void);
32+
void usb_usj_mode(void);
3233

3334
#endif // MICROPY_INCLUDED_ESP32_USB_H

shared/tinyusb/mp_usbd_cdc.c

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,12 @@ void tud_sof_cb(uint32_t frame_count) {
138138

139139
#endif
140140

141-
#if MICROPY_HW_ENABLE_USBDEV && (MICROPY_HW_USB_CDC_1200BPS_TOUCH || MICROPY_HW_USB_CDC)
141+
#if MICROPY_HW_ENABLE_USBDEV && ( \
142+
MICROPY_HW_USB_CDC_1200BPS_TOUCH || \
143+
MICROPY_HW_USB_CDC || \
144+
MICROPY_HW_USB_CDC_DTR_RTS_BOOTLOADER)
142145

143-
#if MICROPY_HW_USB_CDC_1200BPS_TOUCH
146+
#if MICROPY_HW_USB_CDC_1200BPS_TOUCH || MICROPY_HW_USB_CDC_DTR_RTS_BOOTLOADER
144147
static mp_sched_node_t mp_bootloader_sched_node;
145148

146149
static void usbd_cdc_run_bootloader_task(mp_sched_node_t *node) {
@@ -149,6 +152,13 @@ static void usbd_cdc_run_bootloader_task(mp_sched_node_t *node) {
149152
}
150153
#endif
151154

155+
#if MICROPY_HW_USB_CDC_DTR_RTS_BOOTLOADER
156+
static struct {
157+
bool dtr : 1;
158+
bool rts : 1;
159+
} prev_line_state = {0};
160+
#endif
161+
152162
void
153163
#if MICROPY_HW_USB_EXTERNAL_TINYUSB
154164
mp_usbd_line_state_cb
@@ -165,6 +175,15 @@ tud_cdc_line_state_cb
165175
tud_sof_cb_enable(true);
166176
}
167177
#endif
178+
#if MICROPY_HW_USB_CDC_DTR_RTS_BOOTLOADER
179+
if (dtr && !rts) {
180+
if (prev_line_state.rts && !prev_line_state.dtr) {
181+
mp_sched_schedule_node(&mp_bootloader_sched_node, usbd_cdc_run_bootloader_task);
182+
}
183+
}
184+
prev_line_state.rts = rts;
185+
prev_line_state.dtr = dtr;
186+
#endif
168187
#if MICROPY_HW_USB_CDC_1200BPS_TOUCH
169188
if (dtr == false && rts == false) {
170189
// Device is disconnected.

shared/tinyusb/mp_usbd_cdc.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@
3131
#define MICROPY_HW_USB_CDC_TX_TIMEOUT (500)
3232
#endif
3333

34+
// This is typically only enabled on esp32
35+
// parts which have an internal usb peripheral.
36+
#ifndef MICROPY_HW_USB_CDC_DTR_RTS_BOOTLOADER
37+
#define MICROPY_HW_USB_CDC_DTR_RTS_BOOTLOADER (0)
38+
#endif
39+
3440
uintptr_t mp_usbd_cdc_poll_interfaces(uintptr_t poll_flags);
3541
void tud_cdc_rx_cb(uint8_t itf);
3642
mp_uint_t mp_usbd_cdc_tx_strn(const char *str, mp_uint_t len);

0 commit comments

Comments
 (0)
0