diff --git a/ports/nrf/common-hal/microcontroller/__init__.c b/ports/nrf/common-hal/microcontroller/__init__.c index f5caf68ef8..06aac9409d 100644 --- a/ports/nrf/common-hal/microcontroller/__init__.c +++ b/ports/nrf/common-hal/microcontroller/__init__.c @@ -50,36 +50,34 @@ void common_hal_mcu_delay_us(uint32_t delay) { static volatile uint32_t nesting_count = 0; static uint8_t is_nested_critical_region; -static uint8_t sd_is_enabled = false; void common_hal_mcu_disable_interrupts() { - sd_softdevice_is_enabled(&sd_is_enabled); - if (sd_is_enabled) { + if (nesting_count == 0) { + // Unlike __disable_irq(), this should only be called the first time + // "is_nested_critical_region" is sd's equivalent of our nesting count + // so a nested call would store 0 in the global and make the later + // exit call not actually reenable interrupts + // + // This only disables interrupts of priority 2 through 7; levels 0, 1, + // and 4, are exclusive to softdevice and should never be used, so + // this limitation is not important. sd_nvic_critical_region_enter(&is_nested_critical_region); - } else { - __disable_irq(); - __DMB(); - nesting_count++; } + __DMB(); + nesting_count++; } void common_hal_mcu_enable_interrupts() { - // Don't check here if SD is enabled, because we'll crash if interrupts - // were turned off and sd_softdevice_is_enabled is called. - if (sd_is_enabled) { - sd_nvic_critical_region_exit(is_nested_critical_region); - } else { - if (nesting_count == 0) { - // This is very very bad because it means there was mismatched disable/enables so we - // crash. - reset_into_safe_mode(HARD_CRASH); - } - nesting_count--; - if (nesting_count > 0) { - return; - } - __DMB(); - __enable_irq(); + if (nesting_count == 0) { + // This is very very bad because it means there was mismatched disable/enables so we + // crash. + reset_into_safe_mode(HARD_CRASH); } + nesting_count--; + if (nesting_count > 0) { + return; + } + __DMB(); + sd_nvic_critical_region_exit(is_nested_critical_region); } void common_hal_mcu_on_next_reset(mcu_runmode_t runmode) { diff --git a/ports/nrf/supervisor/port.c b/ports/nrf/supervisor/port.c index aee2d63e1d..e681e6825f 100644 --- a/ports/nrf/supervisor/port.c +++ b/ports/nrf/supervisor/port.c @@ -313,12 +313,21 @@ void port_sleep_until_interrupt(void) { // instruction will returned as long as an interrupt is // available, even though the actual handler won't fire until // we re-enable interrupts. - common_hal_mcu_disable_interrupts(); + // + // We do not use common_hal_mcu_disable_interrupts here because + // we truly require that interrupts be disabled, while + // common_hal_mcu_disable_interrupts actually just masks the + // interrupts that are not required to allow the softdevice to + // function (whether or not SD is enabled) + int nested = __get_PRIMASK(); + __disable_irq(); if (!tud_task_event_ready()) { __DSB(); __WFI(); } - common_hal_mcu_enable_interrupts(); + if (!nested) { + __enable_irq(); + } } }