Skip to content

Commit c607efe

Browse files
committed
main.c: Fix safe mode
Back in adafruit#5536 I modified how boot_out.txt got written. However, I broke USB enumeration in the safe-mode case. This fixes it so that a safe-mode board still connects on USB with all defaults. (tested on a macropad)
1 parent c27b3a0 commit c607efe

File tree

1 file changed

+41
-43
lines changed

1 file changed

+41
-43
lines changed

main.c

Lines changed: 41 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -645,10 +645,6 @@ STATIC void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
645645
&& safe_mode == NO_SAFE_MODE
646646
&& MP_STATE_VM(vfs_mount_table) != NULL;
647647

648-
if (!ok_to_run) {
649-
return;
650-
}
651-
652648
static const char * const boot_py_filenames[] = STRING_LIST("boot.py", "boot.txt");
653649

654650
// Do USB setup even if boot.py is not run.
@@ -661,54 +657,56 @@ STATIC void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
661657
usb_set_defaults();
662658
#endif
663659

664-
#ifdef CIRCUITPY_BOOT_OUTPUT_FILE
665-
vstr_t boot_text;
666-
vstr_init(&boot_text, 512);
667-
boot_output = &boot_text;
668-
#endif
660+
pyexec_result_t result = {0, MP_OBJ_NULL, 0};
669661

670-
// Write version info
671-
mp_printf(&mp_plat_print, "%s\nBoard ID:%s\n", MICROPY_FULL_VERSION_INFO, CIRCUITPY_BOARD_ID);
662+
if (ok_to_run) {
663+
#ifdef CIRCUITPY_BOOT_OUTPUT_FILE
664+
vstr_t boot_text;
665+
vstr_init(&boot_text, 512);
666+
boot_output = &boot_text;
667+
#endif
672668

673-
pyexec_result_t result = {0, MP_OBJ_NULL, 0};
669+
// Write version info
670+
mp_printf(&mp_plat_print, "%s\nBoard ID:%s\n", MICROPY_FULL_VERSION_INFO, CIRCUITPY_BOARD_ID);
674671

675-
bool found_boot = maybe_run_list(boot_py_filenames, &result);
676-
(void) found_boot;
672+
bool found_boot = maybe_run_list(boot_py_filenames, &result);
673+
(void) found_boot;
677674

678675

679-
#ifdef CIRCUITPY_BOOT_OUTPUT_FILE
680-
// Get the base filesystem.
681-
fs_user_mount_t *vfs = (fs_user_mount_t *) MP_STATE_VM(vfs_mount_table)->obj;
682-
FATFS *fs = &vfs->fatfs;
676+
#ifdef CIRCUITPY_BOOT_OUTPUT_FILE
677+
// Get the base filesystem.
678+
fs_user_mount_t *vfs = (fs_user_mount_t *) MP_STATE_VM(vfs_mount_table)->obj;
679+
FATFS *fs = &vfs->fatfs;
683680

684-
boot_output = NULL;
685-
bool write_boot_output = (common_hal_mcu_processor_get_reset_reason() == RESET_REASON_POWER_ON);
686-
FIL boot_output_file;
687-
if (f_open(fs, &boot_output_file, CIRCUITPY_BOOT_OUTPUT_FILE, FA_READ) == FR_OK) {
688-
char *file_contents = m_new(char, boot_text.alloc);
689-
UINT chars_read;
690-
if (f_read(&boot_output_file, file_contents, 1+boot_text.len, &chars_read) == FR_OK) {
691-
write_boot_output =
692-
(chars_read != boot_text.len) || (memcmp(boot_text.buf, file_contents, chars_read) != 0);
681+
boot_output = NULL;
682+
bool write_boot_output = (common_hal_mcu_processor_get_reset_reason() == RESET_REASON_POWER_ON);
683+
FIL boot_output_file;
684+
if (f_open(fs, &boot_output_file, CIRCUITPY_BOOT_OUTPUT_FILE, FA_READ) == FR_OK) {
685+
char *file_contents = m_new(char, boot_text.alloc);
686+
UINT chars_read;
687+
if (f_read(&boot_output_file, file_contents, 1+boot_text.len, &chars_read) == FR_OK) {
688+
write_boot_output =
689+
(chars_read != boot_text.len) || (memcmp(boot_text.buf, file_contents, chars_read) != 0);
690+
}
691+
// no need to f_close the file
693692
}
694-
// no need to f_close the file
695-
}
696693

697-
if (write_boot_output) {
698-
// Wait 1 second before opening CIRCUITPY_BOOT_OUTPUT_FILE for write,
699-
// in case power is momentary or will fail shortly due to, say a low, battery.
700-
mp_hal_delay_ms(1000);
701-
702-
// USB isn't up, so we can write the file.
703-
// operating at the oofatfs (f_open) layer means the usb concurrent write permission
704-
// is not even checked!
705-
f_open(fs, &boot_output_file, CIRCUITPY_BOOT_OUTPUT_FILE, FA_WRITE | FA_CREATE_ALWAYS);
706-
UINT chars_written;
707-
f_write(&boot_output_file, boot_text.buf, boot_text.len, &chars_written);
708-
f_close(&boot_output_file);
709-
filesystem_flush();
694+
if (write_boot_output) {
695+
// Wait 1 second before opening CIRCUITPY_BOOT_OUTPUT_FILE for write,
696+
// in case power is momentary or will fail shortly due to, say a low, battery.
697+
mp_hal_delay_ms(1000);
698+
699+
// USB isn't up, so we can write the file.
700+
// operating at the oofatfs (f_open) layer means the usb concurrent write permission
701+
// is not even checked!
702+
f_open(fs, &boot_output_file, CIRCUITPY_BOOT_OUTPUT_FILE, FA_WRITE | FA_CREATE_ALWAYS);
703+
UINT chars_written;
704+
f_write(&boot_output_file, boot_text.buf, boot_text.len, &chars_written);
705+
f_close(&boot_output_file);
706+
filesystem_flush();
707+
}
708+
#endif
710709
}
711-
#endif
712710

713711
#if CIRCUITPY_USB
714712
// Some data needs to be carried over from the USB settings in boot.py

0 commit comments

Comments
 (0)