Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 15 additions & 5 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1031,9 +1031,12 @@ int __attribute__((used)) main(void) {
// A power brownout here could make it appear as if there's
// no SPI flash filesystem, and we might erase the existing one.

// Re-aquire safe mode status.
safe_mode_t safe_mode = get_safe_mode();

// Check whether CIRCUITPY is available. No need to reset to get safe mode
// since we haven't run user code yet.
if (!filesystem_init(get_safe_mode() == SAFE_MODE_NONE, false)) {
if (!filesystem_init(safe_mode == SAFE_MODE_NONE, false)) {
set_safe_mode(SAFE_MODE_NO_CIRCUITPY);
}

Expand All @@ -1059,14 +1062,21 @@ int __attribute__((used)) main(void) {
supervisor_set_run_reason(RUN_REASON_STARTUP);

// If not in safe mode turn on autoreload by default but before boot.py in case it wants to change it.
if (get_safe_mode() == SAFE_MODE_NONE) {
if (safe_mode == SAFE_MODE_NONE) {
autoreload_enable();
}

// By default our internal flash is readonly to local python code and
// writable over USB. Set it here so that safemode.py or boot.py can change it.
// By default, on boards that have USB_MSC, our internal flash is read-only to local python code
// and writable by USB. However if USB_MSC is toggled off or the board only has other workflows,
// the board starts with the internal flash in read-write mode.
static mp_int_t default_usb_state = CIRCUITPY_USB == 1;
#if CIRCUITPY_OS_GETENV && CIRCUITPY_USB && CIRCUITPY_TOML_USB_DEFAULTS
if (safe_mode == SAFE_MODE_NONE) {
(void)common_hal_os_getenv_int("CIRCUITPY_USB_MSC_DEFAULT", &default_usb_state);
}
#endif
filesystem_set_internal_concurrent_write_protection(true);
filesystem_set_internal_writable_by_usb(CIRCUITPY_USB == 1);
filesystem_set_internal_writable_by_usb(default_usb_state);

#if CIRCUITPY_SAFEMODE_PY
// Run safemode.py if we ARE in safe mode.
Expand Down
1 change: 1 addition & 0 deletions ports/stm/mpconfigport.mk
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,5 @@ ifeq ($(MCU_SERIES),L4)
endif

CIRCUITPY_PARALLELDISPLAYBUS := 0
CIRCUITPY_TOML_USB_DEFAULTS = 0
CIRCUITPY_BUILD_EXTENSIONS ?= bin
3 changes: 3 additions & 0 deletions py/circuitpy_mpconfig.mk
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ CFLAGS += -DCIRCUITPY_ENABLE_MPY_NATIVE=$(CIRCUITPY_ENABLE_MPY_NATIVE)
CIRCUITPY_OS_GETENV ?= $(CIRCUITPY_FULL_BUILD)
CFLAGS += -DCIRCUITPY_OS_GETENV=$(CIRCUITPY_OS_GETENV)

CIRCUITPY_TOML_USB_DEFAULTS ?= $(CIRCUITPY_OS_GETENV)
CFLAGS += -DCIRCUITPY_TOML_USB_DEFAULTS=$(CIRCUITPY_TOML_USB_DEFAULTS)

CIRCUITPY_ERRNO ?= $(CIRCUITPY_FULL_BUILD)
CFLAGS += -DCIRCUITPY_ERRNO=$(CIRCUITPY_ERRNO)

Expand Down
13 changes: 12 additions & 1 deletion shared-module/storage/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
#if CIRCUITPY_USB_MSC
#include "tusb.h"

#if CIRCUITPY_OS_GETENV && CIRCUITPY_TOML_USB_DEFAULTS
#include "shared-module/os/__init__.h"
#include "supervisor/shared/safe_mode.h"
#endif

static const uint8_t usb_msc_descriptor_template[] = {
// MSC Interface Descriptor
0x09, // 0 bLength
Expand Down Expand Up @@ -88,7 +93,13 @@ static const uint8_t usb_msc_descriptor_template[] = {
bool storage_usb_is_enabled;

void storage_usb_set_defaults(void) {
storage_usb_is_enabled = CIRCUITPY_USB_MSC_ENABLED_DEFAULT;
mp_int_t getenv_d = (mp_int_t)CIRCUITPY_USB_MSC_ENABLED_DEFAULT;
#if CIRCUITPY_OS_GETENV && CIRCUITPY_TOML_USB_DEFAULTS
if (get_safe_mode() == SAFE_MODE_NONE) {
(void)common_hal_os_getenv_int("CIRCUITPY_USB_MSC_DEFAULT", &getenv_d);
}
#endif
storage_usb_is_enabled = (bool)getenv_d;
}

bool storage_usb_enabled(void) {
Expand Down
16 changes: 14 additions & 2 deletions shared-module/usb_cdc/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
#error CFG_TUD_CDC must be exactly 2
#endif

#if CIRCUITPY_OS_GETENV && CIRCUITPY_TOML_USB_DEFAULTS
#include "shared-module/os/__init__.h"
#include "supervisor/shared/safe_mode.h"
#endif

static const uint8_t usb_cdc_descriptor_template[] = {
// CDC IAD Descriptor
0x08, // 0 bLength
Expand Down Expand Up @@ -169,8 +174,15 @@ static bool usb_cdc_console_is_enabled;
static bool usb_cdc_data_is_enabled;

void usb_cdc_set_defaults(void) {
common_hal_usb_cdc_enable(CIRCUITPY_USB_CDC_CONSOLE_ENABLED_DEFAULT,
CIRCUITPY_USB_CDC_DATA_ENABLED_DEFAULT);
mp_int_t getenv_console = (mp_int_t)CIRCUITPY_USB_CDC_CONSOLE_ENABLED_DEFAULT;
mp_int_t getenv_data = (mp_int_t)CIRCUITPY_USB_CDC_DATA_ENABLED_DEFAULT;
#if CIRCUITPY_OS_GETENV && CIRCUITPY_TOML_USB_DEFAULTS
if (get_safe_mode() == SAFE_MODE_NONE) {
(void)common_hal_os_getenv_int("CIRCUITPY_USB_CDC_CONSOLE_DEFAULT", &getenv_console);
(void)common_hal_os_getenv_int("CIRCUITPY_USB_CDC_DATA_DEFAULT", &getenv_data);
}
#endif
common_hal_usb_cdc_enable((bool)getenv_console, (bool)getenv_data);
}

bool usb_cdc_console_enabled(void) {
Expand Down
18 changes: 16 additions & 2 deletions shared-module/usb_hid/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
#include "supervisor/port.h"
#include "supervisor/usb.h"

#if CIRCUITPY_OS_GETENV && CIRCUITPY_TOML_USB_DEFAULTS
#include "shared-module/os/__init__.h"
#include "supervisor/shared/safe_mode.h"
#endif

static const uint8_t usb_hid_descriptor_template[] = {
0x09, // 0 bLength
0x04, // 1 bDescriptorType (Interface)
Expand Down Expand Up @@ -154,8 +159,17 @@ uint8_t common_hal_usb_hid_get_boot_device(void) {
void usb_hid_set_defaults(void) {
hid_boot_device = 0;
hid_boot_device_requested = false;
common_hal_usb_hid_enable(
CIRCUITPY_USB_HID_ENABLED_DEFAULT ? &default_hid_devices_tuple : mp_const_empty_tuple, 0);
mp_int_t getenv_d = 1;
#if CIRCUITPY_OS_GETENV && CIRCUITPY_TOML_USB_DEFAULTS
if (get_safe_mode() == SAFE_MODE_NONE) {
(void)common_hal_os_getenv_int("CIRCUITPY_USB_HID_DEFAULT", &getenv_d);
}
#endif
if (getenv_d) {
common_hal_usb_hid_enable(
CIRCUITPY_USB_HID_ENABLED_DEFAULT ? &default_hid_devices_tuple : mp_const_empty_tuple, 0
);
}
}

// This is the interface descriptor, not the report descriptor.
Expand Down
21 changes: 16 additions & 5 deletions shared-module/usb_midi/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
#include "supervisor/usb.h"
#include "tusb.h"

#if CIRCUITPY_OS_GETENV && CIRCUITPY_TOML_USB_DEFAULTS
#include "shared-module/os/__init__.h"
#include "supervisor/shared/safe_mode.h"
#endif

static const uint8_t usb_midi_descriptor_template[] = {
// Audio Interface Descriptor
0x09, // 0 bLength
Expand Down Expand Up @@ -164,15 +169,10 @@ static const uint8_t usb_midi_descriptor_template[] = {
// Is the USB MIDI device enabled?
static bool usb_midi_is_enabled;

void usb_midi_set_defaults(void) {
usb_midi_is_enabled = CIRCUITPY_USB_MIDI_ENABLED_DEFAULT;
}

bool usb_midi_enabled(void) {
return usb_midi_is_enabled;
}


size_t usb_midi_descriptor_length(void) {
return sizeof(usb_midi_descriptor_template);
}
Expand Down Expand Up @@ -267,3 +267,14 @@ bool common_hal_usb_midi_disable(void) {
bool common_hal_usb_midi_enable(void) {
return usb_midi_set_enabled(true);
}

void usb_midi_set_defaults(void) {
mp_int_t getenv_d = (mp_int_t)CIRCUITPY_USB_MIDI_ENABLED_DEFAULT;
#if CIRCUITPY_OS_GETENV && CIRCUITPY_TOML_USB_DEFAULTS
if (get_safe_mode() == SAFE_MODE_NONE) {
(void)common_hal_os_getenv_int("CIRCUITPY_USB_MIDI_DEFAULT", &getenv_d);
}
#endif
usb_midi_is_enabled = (bool)getenv_d;
usb_midi_set_enabled(usb_midi_is_enabled);
}