Skip to content

Fix endpoint checking; Clean up safe mode printing #4754

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 11 additions & 16 deletions locale/circuitpython.pot
Original file line number Diff line number Diff line change
Expand Up @@ -710,13 +710,7 @@ msgid "CircuitPython core code crashed hard. Whoops!\n"
msgstr ""

#: supervisor/shared/safe_mode.c
msgid ""
"CircuitPython is in safe mode because you pressed the reset button during "
"boot. Press again to exit safe mode.\n"
msgstr ""

#: supervisor/shared/safe_mode.c
msgid "CircuitPython was unable to allocate the heap.\n"
msgid "CircuitPython was unable to allocate the heap."
msgstr ""

#: shared-module/bitbangio/SPI.c
Expand Down Expand Up @@ -1634,10 +1628,6 @@ msgstr ""
msgid "Not playing"
msgstr ""

#: main.c
msgid "Not running saved code.\n"
msgstr ""

#: shared-bindings/_bleio/__init__.c
msgid "Not settable"
msgstr ""
Expand Down Expand Up @@ -1950,7 +1940,7 @@ msgid "Row entry must be digitalio.DigitalInOut"
msgstr ""

#: main.c
msgid "Running in safe mode! "
msgid "Running in safe mode! Not running saved code.\n"
msgstr ""

#: shared-module/sdcardio/SDCard.c
Expand Down Expand Up @@ -2071,13 +2061,13 @@ msgstr ""
#: supervisor/shared/safe_mode.c
msgid ""
"The CircuitPython heap was corrupted because the stack was too small.\n"
"Please increase the stack size if you know how, or if not:"
"Increase the stack size if you know how. If not:"
msgstr ""

#: supervisor/shared/safe_mode.c
msgid ""
"The `microcontroller` module was used to boot into safe mode. Press reset to "
"exit safe mode.\n"
"exit safe mode."
msgstr ""

#: shared-bindings/rgbmatrix/RGBMatrix.c
Expand All @@ -2088,7 +2078,7 @@ msgstr ""
msgid ""
"The microcontroller's power dipped. Make sure your power supply provides\n"
"enough power for the whole circuit and press reset (after ejecting "
"CIRCUITPY).\n"
"CIRCUITPY)."
msgstr ""

#: shared-module/audiomixer/MixerVoice.c
Expand Down Expand Up @@ -2382,7 +2372,12 @@ msgid "Writes not supported on Characteristic"
msgstr ""

#: supervisor/shared/safe_mode.c
msgid "You are in safe mode: something unanticipated happened.\n"
msgid "You are in safe mode because:\n"
msgstr ""

#: supervisor/shared/safe_mode.c
msgid ""
"You pressed the reset button during boot. Press again to exit safe mode."
msgstr ""

#: supervisor/shared/safe_mode.c
Expand Down
22 changes: 13 additions & 9 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,18 +261,20 @@ STATIC void print_code_py_status_message(safe_mode_t safe_mode) {
serial_write_compressed(translate("Auto-reload is off.\n"));
}
if (safe_mode != NO_SAFE_MODE) {
serial_write_compressed(translate("Running in safe mode! "));
serial_write_compressed(translate("Not running saved code.\n"));
serial_write_compressed(translate("Running in safe mode! Not running saved code.\n"));
}
}

STATIC bool run_code_py(safe_mode_t safe_mode) {
bool serial_connected_at_start = serial_connected();
bool printed_safe_mode_message = false;
#if CIRCUITPY_AUTORELOAD_DELAY_MS > 0
serial_write("\n");
print_code_py_status_message(safe_mode);
print_safe_mode_message(safe_mode);
serial_write("\n");
if (serial_connected_at_start) {
serial_write("\r\n");
print_code_py_status_message(safe_mode);
print_safe_mode_message(safe_mode);
printed_safe_mode_message = true;
}
#endif

pyexec_result_t result;
Expand Down Expand Up @@ -383,8 +385,11 @@ STATIC bool run_code_py(safe_mode_t safe_mode) {
print_code_py_status_message(safe_mode);
}

print_safe_mode_message(safe_mode);
serial_write("\n");
if (!printed_safe_mode_message) {
print_safe_mode_message(safe_mode);
printed_safe_mode_message = true;
}
serial_write("\r\n");
serial_write_compressed(translate("Press any key to enter the REPL. Use CTRL-D to reload.\n"));
printed_press_any_key = true;
}
Expand Down Expand Up @@ -521,7 +526,6 @@ STATIC void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
usb_set_defaults();
#endif

// TODO(tannewt): Re-add support for flashing boot error output.
if (ok_to_run) {
bool found_boot = maybe_run_list(boot_py_filenames, NULL);
(void) found_boot;
Expand Down
6 changes: 3 additions & 3 deletions py/circuitpy_mpconfig.mk
Original file line number Diff line number Diff line change
Expand Up @@ -350,18 +350,18 @@ CFLAGS += -DCIRCUITPY_USB_CDC_DATA_ENABLED_DEFAULT=$(CIRCUITPY_USB_CDC_DATA_ENAB

CIRCUITPY_USB_HID ?= 1
CFLAGS += -DCIRCUITPY_USB_HID=$(CIRCUITPY_USB_HID)
CIRCUITPY_USB_HID_ENABLED_DEFAULT = $(CIRCUITPY_USB_HID)
CIRCUITPY_USB_HID_ENABLED_DEFAULT ?= $(CIRCUITPY_USB_HID)
CFLAGS += -DCIRCUITPY_USB_HID_ENABLED_DEFAULT=$(CIRCUITPY_USB_HID_ENABLED_DEFAULT)

# MIDI is usually available if there are at least 8 endpoints.
CIRCUITPY_USB_MIDI ?= $(USB_NUM_EP_8_OR_GREATER)
CFLAGS += -DCIRCUITPY_USB_MIDI=$(CIRCUITPY_USB_MIDI)
CIRCUITPY_USB_MIDI_ENABLED_DEFAULT = $(CIRCUITPY_USB_MIDI)
CIRCUITPY_USB_MIDI_ENABLED_DEFAULT ?= $(CIRCUITPY_USB_MIDI)
CFLAGS += -DCIRCUITPY_USB_MIDI_ENABLED_DEFAULT=$(CIRCUITPY_USB_MIDI_ENABLED_DEFAULT)

CIRCUITPY_USB_MSC ?= 1
CFLAGS += -DCIRCUITPY_USB_MSC=$(CIRCUITPY_USB_MSC)
CIRCUITPY_USB_MSC_ENABLED_DEFAULT = $(CIRCUITPY_USB_MSC)
CIRCUITPY_USB_MSC_ENABLED_DEFAULT ?= $(CIRCUITPY_USB_MSC)
CFLAGS += -DCIRCUITPY_USB_MSC_ENABLED_DEFAULT=$(CIRCUITPY_USB_MSC_ENABLED_DEFAULT)

# Defaulting this to OFF initially because it has only been tested on a
Expand Down
73 changes: 37 additions & 36 deletions supervisor/shared/safe_mode.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,17 @@ void __attribute__((noinline,)) reset_into_safe_mode(safe_mode_t reason) {



#define FILE_AN_ISSUE translate("\nPlease file an issue with the contents of your CIRCUITPY drive at \nhttps://github.com/adafruit/circuitpython/issues\n")

void print_safe_mode_message(safe_mode_t reason) {
if (reason == NO_SAFE_MODE) {
return;
}
serial_write("\n");

serial_write("\r\n");
serial_write_compressed(translate("You are in safe mode because:\n"));

const compressed_string_t *message = NULL;

// First check for safe mode reasons that do not necessarily reflect bugs.

switch (reason) {
case USER_SAFE_MODE:
Expand All @@ -133,40 +137,40 @@ void print_safe_mode_message(safe_mode_t reason) {
serial_write_compressed(BOARD_USER_SAFE_MODE_ACTION);
serial_write_compressed(translate("To exit, please reset the board without "));
serial_write_compressed(BOARD_USER_SAFE_MODE_ACTION);
#else
break;
#endif
return;
break;
case MANUAL_SAFE_MODE:
serial_write_compressed(translate("CircuitPython is in safe mode because you pressed the reset button during boot. Press again to exit safe mode.\n"));
return;
message = translate("You pressed the reset button during boot. Press again to exit safe mode.");
break;
case PROGRAMMATIC_SAFE_MODE:
serial_write_compressed(translate("The `microcontroller` module was used to boot into safe mode. Press reset to exit safe mode.\n"));
return;
default:
message = translate("The `microcontroller` module was used to boot into safe mode. Press reset to exit safe mode.");
break;
}

serial_write_compressed(translate("You are in safe mode: something unanticipated happened.\n"));
switch (reason) {
case BROWNOUT:
serial_write_compressed(translate("The microcontroller's power dipped. Make sure your power supply provides\nenough power for the whole circuit and press reset (after ejecting CIRCUITPY).\n"));
return;
case HEAP_OVERWRITTEN:
serial_write_compressed(translate("The CircuitPython heap was corrupted because the stack was too small.\nPlease increase the stack size if you know how, or if not:"));
serial_write_compressed(FILE_AN_ISSUE);
return;
case NO_HEAP:
serial_write_compressed(translate("CircuitPython was unable to allocate the heap.\n"));
serial_write_compressed(FILE_AN_ISSUE);
return;
message = translate("The microcontroller's power dipped. Make sure your power supply provides\nenough power for the whole circuit and press reset (after ejecting CIRCUITPY).");
break;
case USB_TOO_MANY_ENDPOINTS:
message = translate("USB devices need more endpoints than are available.");
break;
case USB_TOO_MANY_INTERFACE_NAMES:
message = translate("USB devices specify too many interface names.");
break;
case WATCHDOG_RESET:
message = translate("Watchdog timer expired.");
break;
default:
break;
}

if (message) {
serial_write_compressed(message);
serial_write("\r\n");
return;
}

// Something worse happened.

serial_write_compressed(translate("CircuitPython core code crashed hard. Whoops!\n"));

const compressed_string_t *message = NULL;
switch (reason) {
case HARD_CRASH:
message = translate("Crash into the HardFault_Handler.");
Expand All @@ -177,6 +181,12 @@ void print_safe_mode_message(safe_mode_t reason) {
case MICROPY_FATAL_ERROR:
message = translate("Fatal error.");
break;
case NO_HEAP:
message = translate("CircuitPython was unable to allocate the heap.");
break;
case HEAP_OVERWRITTEN:
message = translate("The CircuitPython heap was corrupted because the stack was too small.\nIncrease the stack size if you know how. If not:");
break;
case GC_ALLOC_OUTSIDE_VM:
message = translate("Attempted heap allocation when VM not running.");
break;
Expand All @@ -193,19 +203,10 @@ void print_safe_mode_message(safe_mode_t reason) {
case MEM_MANAGE:
message = translate("Invalid memory access.");
break;
case WATCHDOG_RESET:
message = translate("Watchdog timer expired.");
break;
case USB_TOO_MANY_ENDPOINTS:
message = translate("USB devices need more endpoints than are available.");
break;
case USB_TOO_MANY_INTERFACE_NAMES:
message = translate("USB devices specify too many interface names.");
break;
default:
message = translate("Unknown reason.");
break;
}
serial_write_compressed(message);
serial_write_compressed(FILE_AN_ISSUE);
serial_write_compressed(translate("\nPlease file an issue with the contents of your CIRCUITPY drive at \nhttps://github.com/adafruit/circuitpython/issues\n"));
}
2 changes: 1 addition & 1 deletion supervisor/shared/usb/usb_desc.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ static void usb_build_configuration_descriptor(void) {
configuration_descriptor[CONFIG_NUM_INTERFACES_INDEX] = current_interface;

// Did we run out of endpoints?
if (current_endpoint - 1 > USB_NUM_EP) {
if (current_endpoint > USB_NUM_EP) {
reset_into_safe_mode(USB_TOO_MANY_ENDPOINTS);
}

Expand Down