summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Leech <andrew.leech@planetinnovation.com.au>2024-05-23 22:08:12 +1000
committerDamien George <damien@micropython.org>2024-10-07 11:06:57 +1100
commit5ae622ef7b31881cb38b56baa66d8aec3d132cf9 (patch)
tree3f1db2da07a00a10e7d1c54b4054bfdd5c25b2f7
parent641f60045f8014ad480e4297b982753edcb5b5c6 (diff)
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: https://github.com/espressif/arduino-esp32/issues/6762 Signed-off-by: Andrew Leech <andrew@alelec.net>
-rw-r--r--ports/esp32/boards/ESP32_GENERIC_S3/sdkconfig.board1
-rw-r--r--ports/esp32/boards/UM_FEATHERS2/sdkconfig.board1
-rw-r--r--ports/esp32/boards/UM_FEATHERS2NEO/sdkconfig.board1
-rw-r--r--ports/esp32/boards/UM_FEATHERS3/sdkconfig.board1
-rw-r--r--ports/esp32/boards/UM_NANOS3/sdkconfig.board1
-rw-r--r--ports/esp32/boards/UM_PROS3/sdkconfig.board1
-rw-r--r--ports/esp32/boards/UM_TINYS2/sdkconfig.board1
-rw-r--r--ports/esp32/boards/UM_TINYS3/sdkconfig.board1
-rw-r--r--ports/esp32/boards/UM_TINYWATCHS3/sdkconfig.board1
-rw-r--r--ports/esp32/modmachine.c12
-rw-r--r--ports/esp32/mpconfigport.h3
-rw-r--r--ports/esp32/usb.c12
-rw-r--r--ports/esp32/usb.h1
-rw-r--r--shared/tinyusb/mp_usbd_cdc.c23
-rw-r--r--shared/tinyusb/mp_usbd_cdc.h6
15 files changed, 54 insertions, 12 deletions
diff --git a/ports/esp32/boards/ESP32_GENERIC_S3/sdkconfig.board b/ports/esp32/boards/ESP32_GENERIC_S3/sdkconfig.board
index a36b97116..9839b0d30 100644
--- a/ports/esp32/boards/ESP32_GENERIC_S3/sdkconfig.board
+++ b/ports/esp32/boards/ESP32_GENERIC_S3/sdkconfig.board
@@ -1,6 +1,5 @@
CONFIG_ESPTOOLPY_FLASHMODE_QIO=y
CONFIG_ESPTOOLPY_FLASHFREQ_80M=y
-CONFIG_ESPTOOLPY_AFTER_NORESET=y
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=
CONFIG_ESPTOOLPY_FLASHSIZE_8MB=y
diff --git a/ports/esp32/boards/UM_FEATHERS2/sdkconfig.board b/ports/esp32/boards/UM_FEATHERS2/sdkconfig.board
index c2e1c2b3d..9ab58f215 100644
--- a/ports/esp32/boards/UM_FEATHERS2/sdkconfig.board
+++ b/ports/esp32/boards/UM_FEATHERS2/sdkconfig.board
@@ -1,6 +1,5 @@
CONFIG_ESPTOOLPY_FLASHMODE_QIO=y
CONFIG_ESPTOOLPY_FLASHFREQ_80M=y
-CONFIG_ESPTOOLPY_AFTER_NORESET=y
CONFIG_SPIRAM_MEMTEST=
diff --git a/ports/esp32/boards/UM_FEATHERS2NEO/sdkconfig.board b/ports/esp32/boards/UM_FEATHERS2NEO/sdkconfig.board
index a624f7fd9..6ef737212 100644
--- a/ports/esp32/boards/UM_FEATHERS2NEO/sdkconfig.board
+++ b/ports/esp32/boards/UM_FEATHERS2NEO/sdkconfig.board
@@ -1,6 +1,5 @@
CONFIG_ESPTOOLPY_FLASHMODE_QIO=y
CONFIG_ESPTOOLPY_FLASHFREQ_80M=y
-CONFIG_ESPTOOLPY_AFTER_NORESET=y
# LWIP
CONFIG_LWIP_LOCAL_HOSTNAME="UMFeatherS2Neo"
diff --git a/ports/esp32/boards/UM_FEATHERS3/sdkconfig.board b/ports/esp32/boards/UM_FEATHERS3/sdkconfig.board
index c557141eb..3ca0c4b24 100644
--- a/ports/esp32/boards/UM_FEATHERS3/sdkconfig.board
+++ b/ports/esp32/boards/UM_FEATHERS3/sdkconfig.board
@@ -1,6 +1,5 @@
CONFIG_ESPTOOLPY_FLASHMODE_QIO=y
CONFIG_ESPTOOLPY_FLASHFREQ_80M=y
-CONFIG_ESPTOOLPY_AFTER_NORESET=y
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=
CONFIG_ESPTOOLPY_FLASHSIZE_8MB=
diff --git a/ports/esp32/boards/UM_NANOS3/sdkconfig.board b/ports/esp32/boards/UM_NANOS3/sdkconfig.board
index 31d752831..2a39c6433 100644
--- a/ports/esp32/boards/UM_NANOS3/sdkconfig.board
+++ b/ports/esp32/boards/UM_NANOS3/sdkconfig.board
@@ -1,6 +1,5 @@
CONFIG_ESPTOOLPY_FLASHMODE_QIO=y
CONFIG_ESPTOOLPY_FLASHFREQ_80M=y
-CONFIG_ESPTOOLPY_AFTER_NORESET=y
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=
CONFIG_ESPTOOLPY_FLASHSIZE_8MB=y
diff --git a/ports/esp32/boards/UM_PROS3/sdkconfig.board b/ports/esp32/boards/UM_PROS3/sdkconfig.board
index 0de84d57d..5752e03e6 100644
--- a/ports/esp32/boards/UM_PROS3/sdkconfig.board
+++ b/ports/esp32/boards/UM_PROS3/sdkconfig.board
@@ -1,6 +1,5 @@
CONFIG_ESPTOOLPY_FLASHMODE_QIO=y
CONFIG_ESPTOOLPY_FLASHFREQ_80M=y
-CONFIG_ESPTOOLPY_AFTER_NORESET=y
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=
CONFIG_ESPTOOLPY_FLASHSIZE_8MB=
diff --git a/ports/esp32/boards/UM_TINYS2/sdkconfig.board b/ports/esp32/boards/UM_TINYS2/sdkconfig.board
index 0a2097e24..c09731431 100644
--- a/ports/esp32/boards/UM_TINYS2/sdkconfig.board
+++ b/ports/esp32/boards/UM_TINYS2/sdkconfig.board
@@ -1,6 +1,5 @@
CONFIG_ESPTOOLPY_FLASHMODE_QIO=y
CONFIG_ESPTOOLPY_FLASHFREQ_80M=y
-CONFIG_ESPTOOLPY_AFTER_NORESET=y
# LWIP
CONFIG_LWIP_LOCAL_HOSTNAME="UMTinyS2"
diff --git a/ports/esp32/boards/UM_TINYS3/sdkconfig.board b/ports/esp32/boards/UM_TINYS3/sdkconfig.board
index d1689aaf3..d1d19761a 100644
--- a/ports/esp32/boards/UM_TINYS3/sdkconfig.board
+++ b/ports/esp32/boards/UM_TINYS3/sdkconfig.board
@@ -1,6 +1,5 @@
CONFIG_ESPTOOLPY_FLASHMODE_QIO=y
CONFIG_ESPTOOLPY_FLASHFREQ_80M=y
-CONFIG_ESPTOOLPY_AFTER_NORESET=y
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=
CONFIG_ESPTOOLPY_FLASHSIZE_8MB=y
diff --git a/ports/esp32/boards/UM_TINYWATCHS3/sdkconfig.board b/ports/esp32/boards/UM_TINYWATCHS3/sdkconfig.board
index d73f4fe0b..1380e15ce 100644
--- a/ports/esp32/boards/UM_TINYWATCHS3/sdkconfig.board
+++ b/ports/esp32/boards/UM_TINYWATCHS3/sdkconfig.board
@@ -1,6 +1,5 @@
CONFIG_ESPTOOLPY_FLASHMODE_QIO=y
CONFIG_ESPTOOLPY_FLASHFREQ_80M=y
-CONFIG_ESPTOOLPY_AFTER_NORESET=y
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=
CONFIG_ESPTOOLPY_FLASHSIZE_8MB=y
diff --git a/ports/esp32/modmachine.c b/ports/esp32/modmachine.c
index 759455a59..5e05a261b 100644
--- a/ports/esp32/modmachine.c
+++ b/ports/esp32/modmachine.c
@@ -222,7 +222,19 @@ static mp_int_t mp_machine_reset_cause(void) {
#if MICROPY_ESP32_USE_BOOTLOADER_RTC
#include "soc/rtc_cntl_reg.h"
+#include "usb.h"
+#if CONFIG_IDF_TARGET_ESP32S3
+#include "esp32s3/rom/usb/usb_dc.h"
+#include "esp32s3/rom/usb/usb_persist.h"
+#include "esp32s3/rom/usb/chip_usb_dw_wrapper.h"
+#endif
+
NORETURN static void machine_bootloader_rtc(void) {
+ #if CONFIG_IDF_TARGET_ESP32S3
+ usb_usj_mode();
+ usb_dc_prepare_persist();
+ chip_usb_set_persist_flags(USBDC_BOOT_DFU);
+ #endif
REG_WRITE(RTC_CNTL_OPTION1_REG, RTC_CNTL_FORCE_DOWNLOAD_BOOT);
esp_restart();
}
diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h
index f1687cf16..4c94fd16e 100644
--- a/ports/esp32/mpconfigport.h
+++ b/ports/esp32/mpconfigport.h
@@ -199,7 +199,8 @@
#endif
#if MICROPY_HW_ENABLE_USBDEV
-#define MICROPY_SCHEDULER_STATIC_NODES (1)
+#define MICROPY_SCHEDULER_STATIC_NODES (1)
+#define MICROPY_HW_USB_CDC_DTR_RTS_BOOTLOADER (1)
#ifndef MICROPY_HW_USB_VID
#define USB_ESPRESSIF_VID 0x303A
diff --git a/ports/esp32/usb.c b/ports/esp32/usb.c
index d643a7bc7..3ce3d0458 100644
--- a/ports/esp32/usb.c
+++ b/ports/esp32/usb.c
@@ -57,6 +57,18 @@ void usb_init(void) {
}
+#if CONFIG_IDF_TARGET_ESP32S3
+void usb_usj_mode(void) {
+ // Switch the USB PHY back to Serial/Jtag mode, disabling OTG support
+ // This should be run before jumping to bootloader.
+ usb_del_phy(phy_hdl);
+ usb_phy_config_t phy_conf = {
+ .controller = USB_PHY_CTRL_SERIAL_JTAG,
+ };
+ usb_new_phy(&phy_conf, &phy_hdl);
+}
+#endif
+
void mp_usbd_port_get_serial_number(char *serial_buf) {
// use factory default MAC as serial ID
uint8_t mac[8];
diff --git a/ports/esp32/usb.h b/ports/esp32/usb.h
index 5e5eea34e..2bfa3d31a 100644
--- a/ports/esp32/usb.h
+++ b/ports/esp32/usb.h
@@ -29,5 +29,6 @@
#define MICROPY_HW_USB_CDC_TX_TIMEOUT_MS (500)
void usb_init(void);
+void usb_usj_mode(void);
#endif // MICROPY_INCLUDED_ESP32_USB_H
diff --git a/shared/tinyusb/mp_usbd_cdc.c b/shared/tinyusb/mp_usbd_cdc.c
index 0fbecb0a8..b4151f685 100644
--- a/shared/tinyusb/mp_usbd_cdc.c
+++ b/shared/tinyusb/mp_usbd_cdc.c
@@ -138,9 +138,12 @@ void tud_sof_cb(uint32_t frame_count) {
#endif
-#if MICROPY_HW_ENABLE_USBDEV && (MICROPY_HW_USB_CDC_1200BPS_TOUCH || MICROPY_HW_USB_CDC)
+#if MICROPY_HW_ENABLE_USBDEV && ( \
+ MICROPY_HW_USB_CDC_1200BPS_TOUCH || \
+ MICROPY_HW_USB_CDC || \
+ MICROPY_HW_USB_CDC_DTR_RTS_BOOTLOADER)
-#if MICROPY_HW_USB_CDC_1200BPS_TOUCH
+#if MICROPY_HW_USB_CDC_1200BPS_TOUCH || MICROPY_HW_USB_CDC_DTR_RTS_BOOTLOADER
static mp_sched_node_t mp_bootloader_sched_node;
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) {
}
#endif
+#if MICROPY_HW_USB_CDC_DTR_RTS_BOOTLOADER
+static struct {
+ bool dtr : 1;
+ bool rts : 1;
+} prev_line_state = {0};
+#endif
+
void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) {
#if MICROPY_HW_USB_CDC && !MICROPY_EXCLUDE_SHARED_TINYUSB_USBD_CDC
if (dtr) {
@@ -159,6 +169,15 @@ void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) {
tud_sof_cb_enable(true);
}
#endif
+ #if MICROPY_HW_USB_CDC_DTR_RTS_BOOTLOADER
+ if (dtr && !rts) {
+ if (prev_line_state.rts && !prev_line_state.dtr) {
+ mp_sched_schedule_node(&mp_bootloader_sched_node, usbd_cdc_run_bootloader_task);
+ }
+ }
+ prev_line_state.rts = rts;
+ prev_line_state.dtr = dtr;
+ #endif
#if MICROPY_HW_USB_CDC_1200BPS_TOUCH
if (dtr == false && rts == false) {
// Device is disconnected.
diff --git a/shared/tinyusb/mp_usbd_cdc.h b/shared/tinyusb/mp_usbd_cdc.h
index 648cf1288..8d37a7731 100644
--- a/shared/tinyusb/mp_usbd_cdc.h
+++ b/shared/tinyusb/mp_usbd_cdc.h
@@ -31,6 +31,12 @@
#define MICROPY_HW_USB_CDC_TX_TIMEOUT (500)
#endif
+// This is typically only enabled on esp32
+// parts which have an internal usb peripheral.
+#ifndef MICROPY_HW_USB_CDC_DTR_RTS_BOOTLOADER
+#define MICROPY_HW_USB_CDC_DTR_RTS_BOOTLOADER (0)
+#endif
+
uintptr_t mp_usbd_cdc_poll_interfaces(uintptr_t poll_flags);
void tud_cdc_rx_cb(uint8_t itf);
mp_uint_t mp_usbd_cdc_tx_strn(const char *str, mp_uint_t len);