Skip to content

Commit 0ba9db7

Browse files
pi-anlPepperoniPingu
authored andcommitted
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 <[email protected]>
1 parent b871452 commit 0ba9db7

File tree

15 files changed

+54
-12
lines changed

15 files changed

+54
-12
lines changed

Diff for: ports/esp32/boards/ESP32_GENERIC_S3/sdkconfig.board

-1
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

Diff for: ports/esp32/boards/UM_FEATHERS2/sdkconfig.board

-1
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

Diff for: ports/esp32/boards/UM_FEATHERS2NEO/sdkconfig.board

-1
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"

Diff for: ports/esp32/boards/UM_FEATHERS3/sdkconfig.board

-1
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=

Diff for: ports/esp32/boards/UM_NANOS3/sdkconfig.board

-1
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

Diff for: ports/esp32/boards/UM_PROS3/sdkconfig.board

-1
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=

Diff for: ports/esp32/boards/UM_TINYS2/sdkconfig.board

-1
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"

Diff for: ports/esp32/boards/UM_TINYS3/sdkconfig.board

-1
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

Diff for: ports/esp32/boards/UM_TINYWATCHS3/sdkconfig.board

-1
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

Diff for: ports/esp32/modmachine.c

+12
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
}

Diff for: ports/esp32/mpconfigport.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@
199199
#endif
200200

201201
#if MICROPY_HW_ENABLE_USBDEV
202-
#define MICROPY_SCHEDULER_STATIC_NODES (1)
202+
#define MICROPY_SCHEDULER_STATIC_NODES (1)
203+
#define MICROPY_HW_USB_CDC_DTR_RTS_BOOTLOADER (1)
203204

204205
#ifndef MICROPY_HW_USB_VID
205206
#define USB_ESPRESSIF_VID 0x303A

Diff for: ports/esp32/usb.c

+12
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ void usb_init(void) {
5757

5858
}
5959

60+
#if CONFIG_IDF_TARGET_ESP32S3
61+
void usb_usj_mode(void) {
62+
// Switch the USB PHY back to Serial/Jtag mode, disabling OTG support
63+
// This should be run before jumping to bootloader.
64+
usb_del_phy(phy_hdl);
65+
usb_phy_config_t phy_conf = {
66+
.controller = USB_PHY_CTRL_SERIAL_JTAG,
67+
};
68+
usb_new_phy(&phy_conf, &phy_hdl);
69+
}
70+
#endif
71+
6072
void mp_usbd_port_get_serial_number(char *serial_buf) {
6173
// use factory default MAC as serial ID
6274
uint8_t mac[8];

Diff for: ports/esp32/usb.h

+1
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

Diff for: shared/tinyusb/mp_usbd_cdc.c

+21-2
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 tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) {
153163
#if MICROPY_HW_USB_CDC && !MICROPY_EXCLUDE_SHARED_TINYUSB_USBD_CDC
154164
if (dtr) {
@@ -159,6 +169,15 @@ void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) {
159169
tud_sof_cb_enable(true);
160170
}
161171
#endif
172+
#if MICROPY_HW_USB_CDC_DTR_RTS_BOOTLOADER
173+
if (dtr && !rts) {
174+
if (prev_line_state.rts && !prev_line_state.dtr) {
175+
mp_sched_schedule_node(&mp_bootloader_sched_node, usbd_cdc_run_bootloader_task);
176+
}
177+
}
178+
prev_line_state.rts = rts;
179+
prev_line_state.dtr = dtr;
180+
#endif
162181
#if MICROPY_HW_USB_CDC_1200BPS_TOUCH
163182
if (dtr == false && rts == false) {
164183
// Device is disconnected.

Diff for: shared/tinyusb/mp_usbd_cdc.h

+6
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)