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

Skip to content

Commit c5120d6

Browse files
< 10000 div class="prc-PageHeader-Description-kFg8r">
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 7bd0870 commit c5120d6

File tree

17 files changed

+53
-14
lines changed

17 files changed

+53
-14
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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,25 @@ 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_ESP32S2
227+
#include "esp32s2/rom/usb/usb_dc.h"
228+
#include "esp32s2/rom/usb/usb_persist.h"
229+
#include "esp32s2/rom/usb/chip_usb_dw_wrapper.h"
230+
#elif CONFIG_IDF_TARGET_ESP32S3
231+
#include "esp32s3/rom/usb/usb_dc.h"
232+
#include "esp32s3/rom/usb/usb_persist.h"
233+
#include "esp32s3/rom/usb/chip_usb_dw_wrapper.h"
234+
#endif
235+
225236
NORETURN static void machine_bootloader_rtc(void) {
237+
#if CONFIG_IDF_TARGET_ESP32S3
238+
usb_usj_mode();
239+
#endif
240+
usb_dc_prepare_persist();
241+
chip_usb_set_persist_flags(USBDC_BOOT_DFU);
226242
REG_WRITE(RTC_CNTL_OPTION1_REG, RTC_CNTL_FORCE_DOWNLOAD_BOOT);
243+
mp_hal_delay_ms(100);
227244
esp_restart();
228245
}
229246
#endif

ports/esp32/mpconfigport.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@
205205

206206
#if MICROPY_HW_ENABLE_USBDEV
207207
#define MICROPY_SCHEDULER_STATIC_NODES (1)
208+
#define MICROPY_HW_ESP_AUTOMATIC_BOOTLOADER (1)
208209

209210
// Enable USB-CDC serial port
210211
#ifndef MICROPY_HW_USB_CDC
@@ -291,7 +292,7 @@ void *esp_native_code_commit(void *, size_t, void *);
291292
extern void mp_handle_pending(bool); \
292293
mp_handle_pending(true); \
293294
MICROPY_PY_SOCKET_EVENTS_HANDLER \
294-
MICROPY_HW_USBDEV_TASK_HOOK \
295+
MICROPY_HW_USBDEV_TASK_HOOK \
295296
asm ("waiti 0"); \
296297
} while (0);
297298
#endif

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

ports/renesas-ra/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ when invoking `make`.
4343
* Protocol buffer compiler
4444
Building for the `ARDUINO_PORTENTA_C33` board will build the `esp_hosted` driver, that depends
4545
on the Protocol Buffer Compiler being installed. On Debian/Ubuntu, this can be installed with
46-
`sudo apt-get install protobuf-compiler`.
46+
`sudo apt-get install protobuf-c-compiler`.
4747

4848
* Obtain submodules
4949
First the submodules must be obtained using:

ports/renesas-ra/boards/EK_RA4W1/mpconfigboard.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#define MICROPY_HW_ENABLE_ADC (1)
2020
#define MICROPY_HW_HAS_FLASH (1)
2121
#define MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE (1)
22-
22+
#define MICROPY_HW_ENABLE_USBDEV (0)
2323
// board config
2424

2525
// UART

ports/renesas-ra/main.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
#include "shared/readline/readline.h"
3737
#include "shared/runtime/pyexec.h"
3838
#include "shared/runtime/softtimer.h"
39-
#include "shared/tinyusb/mp_usbd.h"
4039
#include "lib/oofatfs/ff.h"
4140
#include "lib/littlefs/lfs1.h"
4241
#include "lib/littlefs/lfs1_util.h"

shared/tinyusb/mp_usbd_cdc.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ void tud_sof_cb(uint32_t frame_count) {
127127
}
128128
}
129129

130-
#if MICROPY_HW_USB_CDC_1200BPS_TOUCH
130+
#if MICROPY_HW_USB_CDC_1200BPS_TOUCH || MICROPY_HW_ESP_AUTOMATIC_BOOTLOADER
131131
static mp_sched_node_t mp_bootloader_sched_node;
132132

133133
static void usbd_cdc_run_bootloader_task(mp_sched_node_t *node) {
@@ -136,6 +136,13 @@ static void usbd_cdc_run_bootloader_task(mp_sched_node_t *node) {
136136
}
137137
#endif
138138

139+
#if MICROPY_HW_ESP_AUTOMATIC_BOOTLOADER
140+
static struct {
141+
bool dtr : 1;
142+
bool rts : 1;
143+
} prev_line_state = {0};
144+
#endif
145+
139146
void
140147
#if MICROPY_HW_USB_EXTERNAL_TINYUSB
141148
mp_usbd_line_state_cb
@@ -151,6 +158,17 @@ tud_cdc_line_state_cb
151158
cdc_connected_flush_delay = (tud_speed_get() == TUSB_SPEED_HIGH) ? 128 : 16;
152159
tud_sof_cb_enable(true);
153160
}
161+
162+
#if MICROPY_HW_ESP_AUTOMATIC_BOOTLOADER
163+
if (dtr && !rts) {
164+
if (prev_line_state.rts && !prev_line_state.dtr) {
165+
mp_sched_schedule_node(&mp_bootloader_sched_node, usbd_cdc_run_bootloader_task);
166+
}
167+
}
168+
prev_line_state.rts = rts;
169+
prev_line_state.dtr = dtr;
170+
#endif
171+
154172
#if MICROPY_HW_USB_CDC_1200BPS_TOUCH
155173
if (dtr == false && rts == false) {
156174
// Device is disconnected.

0 commit comments

Comments
 (0)
0