Skip to content

Commit b63be37

Browse files
committed
stmhal: In safe mode, still mount SD card and present as MSD over USB.
It's still "safe" because no scripts are run. Remove the SD card if you want to access the internal flash filesystem. Addresses issue adafruit#616. Also: remove obsolete pyb.source_dir setting, and reset pyb.main and pyb.usb_mode settings on soft-reset.
1 parent b0accc8 commit b63be37

3 files changed

Lines changed: 12 additions & 25 deletions

File tree

stmhal/main.c

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -127,26 +127,15 @@ void disable_irq(void) {
127127
__disable_irq();
128128
}
129129

130-
STATIC mp_obj_t pyb_config_source_dir = MP_OBJ_NULL;
131130
STATIC mp_obj_t pyb_config_main = MP_OBJ_NULL;
132131
STATIC mp_obj_t pyb_config_usb_mode = MP_OBJ_NULL;
133132

134-
STATIC mp_obj_t pyb_source_dir(mp_obj_t source_dir) {
135-
if (MP_OBJ_IS_STR(source_dir)) {
136-
pyb_config_source_dir = source_dir;
137-
}
138-
return mp_const_none;
139-
}
140-
141-
MP_DEFINE_CONST_FUN_OBJ_1(pyb_source_dir_obj, pyb_source_dir);
142-
143133
STATIC mp_obj_t pyb_main(mp_obj_t main) {
144134
if (MP_OBJ_IS_STR(main)) {
145135
pyb_config_main = main;
146136
}
147137
return mp_const_none;
148138
}
149-
150139
MP_DEFINE_CONST_FUN_OBJ_1(pyb_main_obj, pyb_main);
151140

152141
STATIC mp_obj_t pyb_usb_mode(mp_obj_t usb_mode) {
@@ -155,7 +144,6 @@ STATIC mp_obj_t pyb_usb_mode(mp_obj_t usb_mode) {
155144
}
156145
return mp_const_none;
157146
}
158-
159147
MP_DEFINE_CONST_FUN_OBJ_1(pyb_usb_mode_obj, pyb_usb_mode);
160148

161149
static const char fresh_boot_py[] =
@@ -444,7 +432,7 @@ int main(void) {
444432

445433
#if MICROPY_HW_HAS_SDCARD
446434
// if an SD card is present then mount it on /sd/
447-
if (reset_mode == 1 && sdcard_is_present()) {
435+
if (sdcard_is_present()) {
448436
FRESULT res = f_mount(&fatfs1, "/sd", 1);
449437
if (res != FR_OK) {
450438
printf("[SD] could not mount SD card\n");
@@ -466,7 +454,12 @@ int main(void) {
466454
}
467455
#endif
468456

457+
// reset config variables; they should be set by boot.py
458+
pyb_config_main = MP_OBJ_NULL;
459+
pyb_config_usb_mode = MP_OBJ_NULL;
460+
469461
// run boot.py, if it exists
462+
// TODO perhaps have pyb.reboot([bootpy]) function to soft-reboot and execute custom boot.py
470463
if (reset_mode == 1) {
471464
const char *boot_py = "boot.py";
472465
FRESULT res = f_stat(boot_py, NULL);
@@ -491,17 +484,14 @@ int main(void) {
491484
pyb_usb_host_init();
492485
#elif defined(USE_DEVICE_MODE)
493486
// USB device
494-
if (reset_mode == 1) {
495-
usb_device_mode_t usb_mode = USB_DEVICE_MODE_CDC_MSC;
496-
if (pyb_config_usb_mode != MP_OBJ_NULL) {
497-
if (strcmp(mp_obj_str_get_str(pyb_config_usb_mode), "CDC+HID") == 0) {
498-
usb_mode = USB_DEVICE_MODE_CDC_HID;
499-
}
487+
usb_device_mode_t usb_mode = USB_DEVICE_MODE_CDC_MSC;
488+
// if we are not in reset_mode==1, this config variable will always be NULL
489+
if (pyb_config_usb_mode != MP_OBJ_NULL) {
490+
if (strcmp(mp_obj_str_get_str(pyb_config_usb_mode), "CDC+HID") == 0) {
491+
usb_mode = USB_DEVICE_MODE_CDC_HID;
500492
}
501-
pyb_usb_dev_init(usb_mode, usb_medium);
502-
} else {
503-
pyb_usb_dev_init(USB_DEVICE_MODE_CDC_MSC, usb_medium);
504493
}
494+
pyb_usb_dev_init(usb_mode, usb_medium);
505495
#endif
506496

507497
#if MICROPY_HW_HAS_MMA7660

stmhal/modpyb.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,6 @@ STATIC mp_obj_t pyb_hid_send_report(mp_obj_t arg) {
341341
}
342342
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_hid_send_report_obj, pyb_hid_send_report);
343343

344-
MP_DECLARE_CONST_FUN_OBJ(pyb_source_dir_obj); // defined in main.c
345344
MP_DECLARE_CONST_FUN_OBJ(pyb_main_obj); // defined in main.c
346345
MP_DECLARE_CONST_FUN_OBJ(pyb_usb_mode_obj); // defined in main.c
347346

@@ -360,7 +359,6 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = {
360359

361360
{ MP_OBJ_NEW_QSTR(MP_QSTR_stop), (mp_obj_t)&pyb_stop_obj },
362361
{ MP_OBJ_NEW_QSTR(MP_QSTR_standby), (mp_obj_t)&pyb_standby_obj },
363-
{ MP_OBJ_NEW_QSTR(MP_QSTR_source_dir), (mp_obj_t)&pyb_source_dir_obj },
364362
{ MP_OBJ_NEW_QSTR(MP_QSTR_main), (mp_obj_t)&pyb_main_obj },
365363
{ MP_OBJ_NEW_QSTR(MP_QSTR_usb_mode), (mp_obj_t)&pyb_usb_mode_obj },
366364

stmhal/qstrdefsport.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ Q(disable_irq)
3939
Q(enable_irq)
4040
Q(stop)
4141
Q(standby)
42-
Q(source_dir)
4342
Q(main)
4443
Q(usb_mode)
4544
Q(sync)

0 commit comments

Comments
 (0)