Skip to content

Commit d2f2afe

Browse files
authored
Merge pull request #4925 from microDev1/update-run-mode
Add RunMode.UF2 and update esp32s2 run-modes
2 parents b228891 + 9ac607d commit d2f2afe

File tree

5 files changed

+62
-17
lines changed

5 files changed

+62
-17
lines changed

ports/esp32s2/common-hal/microcontroller/__init__.c

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@
4242

4343
#include "freertos/FreeRTOS.h"
4444

45+
#include "soc/rtc_cntl_reg.h"
46+
#include "esp_private/system_internal.h"
47+
48+
#include "esp32s2/rom/rtc.h"
49+
#include "esp32s2/rom/usb/usb_persist.h"
50+
#include "esp32s2/rom/usb/chip_usb_dw_wrapper.h"
51+
4552
void common_hal_mcu_delay_us(uint32_t delay) {
4653
mp_hal_delay_us(delay);
4754
}
@@ -68,12 +75,32 @@ void common_hal_mcu_enable_interrupts(void) {
6875
}
6976

7077
void common_hal_mcu_on_next_reset(mcu_runmode_t runmode) {
71-
if (runmode == RUNMODE_SAFE_MODE) {
72-
safe_mode_on_next_reset(PROGRAMMATIC_SAFE_MODE);
78+
switch (runmode) {
79+
case RUNMODE_UF2:
80+
// 0x11F2 is APP_REQUEST_UF2_RESET_HINT & is defined by TinyUF2
81+
esp_reset_reason_set_hint(0x11F2);
82+
break;
83+
case RUNMODE_NORMAL:
84+
// revert back to normal boot
85+
REG_WRITE(RTC_RESET_CAUSE_REG, 0); // reset uf2
86+
REG_WRITE(RTC_CNTL_STORE0_REG, 0); // reset safe mode
87+
REG_WRITE(RTC_CNTL_OPTION1_REG, 0); // reset bootloader
88+
break;
89+
case RUNMODE_SAFE_MODE:
90+
// enter safe mode on next boot
91+
safe_mode_on_next_reset(PROGRAMMATIC_SAFE_MODE);
92+
break;
93+
case RUNMODE_BOOTLOADER:
94+
// DFU download
95+
chip_usb_set_persist_flags(USBDC_BOOT_DFU);
96+
REG_WRITE(RTC_CNTL_OPTION1_REG, RTC_CNTL_FORCE_DOWNLOAD_BOOT);
97+
break;
98+
default:
99+
break;
73100
}
74101
}
75102

76-
void common_hal_mcu_reset(void) {
103+
void NORETURN common_hal_mcu_reset(void) {
77104
filesystem_flush(); // TODO: implement as part of flash improvements
78105
esp_restart();
79106
}

ports/raspberrypi/common-hal/microcontroller/__init__.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,16 @@ void common_hal_mcu_enable_interrupts(void) {
7373
static bool next_reset_to_bootloader = false;
7474

7575
void common_hal_mcu_on_next_reset(mcu_runmode_t runmode) {
76-
if (runmode == RUNMODE_BOOTLOADER) {
77-
next_reset_to_bootloader = true;
78-
} else {
79-
}
80-
if (runmode == RUNMODE_SAFE_MODE) {
81-
safe_mode_on_next_reset(PROGRAMMATIC_SAFE_MODE);
76+
switch (runmode) {
77+
case RUNMODE_UF2:
78+
case RUNMODE_BOOTLOADER:
79+
next_reset_to_bootloader = true;
80+
break;
81+
case RUNMODE_SAFE_MODE:
82+
safe_mode_on_next_reset(PROGRAMMATIC_SAFE_MODE);
83+
break;
84+
default:
85+
break;
8286
}
8387
}
8488

shared-bindings/microcontroller/RunMode.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,27 @@
3939
//| :type microcontroller.RunMode:"""
4040
//|
4141
//| SAFE_MODE: RunMode
42-
//| """Run CircuitPython in safe mode. User code will not be run and the
42+
//| """Run CircuitPython in safe mode. User code will not run and the
4343
//| file system will be writeable over USB.
4444
//|
4545
//| :type microcontroller.RunMode:"""
4646
//|
47+
//| UF2: RunMode
48+
//| """Run the uf2 bootloader.
49+
//|
50+
//| :type microcontroller.RunMode:"""
51+
//|
4752
//| BOOTLOADER: RunMode
48-
//| """Run the bootloader.
53+
//| """Run the default bootloader.
4954
//|
5055
//| :type microcontroller.RunMode:"""
5156
//|
5257
const mp_obj_type_t mcu_runmode_type;
5358

59+
const mcu_runmode_obj_t mcu_runmode_uf2_obj = {
60+
{ &mcu_runmode_type },
61+
};
62+
5463
const mcu_runmode_obj_t mcu_runmode_normal_obj = {
5564
{ &mcu_runmode_type },
5665
};
@@ -64,6 +73,7 @@ const mcu_runmode_obj_t mcu_runmode_bootloader_obj = {
6473
};
6574

6675
STATIC const mp_rom_map_elem_t mcu_runmode_locals_dict_table[] = {
76+
{MP_ROM_QSTR(MP_QSTR_UF2), MP_ROM_PTR(&mcu_runmode_uf2_obj)},
6777
{MP_ROM_QSTR(MP_QSTR_NORMAL), MP_ROM_PTR(&mcu_runmode_normal_obj)},
6878
{MP_ROM_QSTR(MP_QSTR_SAFE_MODE), MP_ROM_PTR(&mcu_runmode_safe_mode_obj)},
6979
{MP_ROM_QSTR(MP_QSTR_BOOTLOADER), MP_ROM_PTR(&mcu_runmode_bootloader_obj)},
@@ -72,10 +82,11 @@ STATIC MP_DEFINE_CONST_DICT(mcu_runmode_locals_dict, mcu_runmode_locals_dict_tab
7282

7383
STATIC void mcu_runmode_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
7484
qstr runmode = MP_QSTR_NORMAL;
75-
if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&mcu_runmode_safe_mode_obj)) {
85+
if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&mcu_runmode_uf2_obj)) {
86+
runmode = MP_QSTR_UF2;
87+
} else if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&mcu_runmode_safe_mode_obj)) {
7688
runmode = MP_QSTR_SAFE_MODE;
77-
} else if (MP_OBJ_TO_PTR(self_in) ==
78-
MP_ROM_PTR(&mcu_runmode_bootloader_obj)) {
89+
} else if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&mcu_runmode_bootloader_obj)) {
7990
runmode = MP_QSTR_BOOTLOADER;
8091
}
8192
mp_printf(print, "%q.%q.%q", MP_QSTR_microcontroller, MP_QSTR_RunMode,

shared-bindings/microcontroller/RunMode.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "py/obj.h"
3131

3232
typedef enum {
33+
RUNMODE_UF2,
3334
RUNMODE_NORMAL,
3435
RUNMODE_SAFE_MODE,
3536
RUNMODE_BOOTLOADER
@@ -40,6 +41,8 @@ extern const mp_obj_type_t mcu_runmode_type;
4041
typedef struct {
4142
mp_obj_base_t base;
4243
} mcu_runmode_obj_t;
44+
45+
extern const mcu_runmode_obj_t mcu_runmode_uf2_obj;
4346
extern const mcu_runmode_obj_t mcu_runmode_normal_obj;
4447
extern const mcu_runmode_obj_t mcu_runmode_safe_mode_obj;
4548
extern const mcu_runmode_obj_t mcu_runmode_bootloader_obj;

shared-bindings/microcontroller/__init__.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(mcu_enable_interrupts_obj, mcu_enable_interrupt
109109
//|
110110
STATIC mp_obj_t mcu_on_next_reset(mp_obj_t run_mode_obj) {
111111
mcu_runmode_t run_mode;
112-
if (run_mode_obj == &mcu_runmode_normal_obj) {
112+
if (run_mode_obj == &mcu_runmode_uf2_obj) {
113+
run_mode = RUNMODE_UF2;
114+
} else if (run_mode_obj == &mcu_runmode_normal_obj) {
113115
run_mode = RUNMODE_NORMAL;
114116
} else if (run_mode_obj == &mcu_runmode_safe_mode_obj) {
115117
run_mode = RUNMODE_SAFE_MODE;
@@ -118,9 +120,7 @@ STATIC mp_obj_t mcu_on_next_reset(mp_obj_t run_mode_obj) {
118120
} else {
119121
mp_raise_ValueError(translate("Invalid run mode."));
120122
}
121-
122123
common_hal_mcu_on_next_reset(run_mode);
123-
124124
return mp_const_none;
125125
}
126126
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mcu_on_next_reset_obj, mcu_on_next_reset);

0 commit comments

Comments
 (0)