diff --git a/ports/nrf/common-hal/alarm/pin/PinAlarm.c b/ports/nrf/common-hal/alarm/pin/PinAlarm.c index 300369bb36..4bf412d85b 100644 --- a/ports/nrf/common-hal/alarm/pin/PinAlarm.c +++ b/ports/nrf/common-hal/alarm/pin/PinAlarm.c @@ -141,14 +141,6 @@ void alarm_pin_pinalarm_reset(void) { } static void configure_pins_for_sleep(void) { - nrfx_err_t err; - if (nrfx_gpiote_is_init()) { - nrfx_gpiote_uninit(); - } - err = nrfx_gpiote_init(NRFX_GPIOTE_CONFIG_IRQ_PRIORITY); - assert(err == NRFX_SUCCESS); - (void)err; // to suppress unused warning - _pinhandler_gpiote_count = 0; nrfx_gpiote_in_config_t cfg = { @@ -176,9 +168,10 @@ static void configure_pins_for_sleep(void) { cfg.sense = NRF_GPIOTE_POLARITY_TOGGLE; cfg.pull = NRF_GPIO_PIN_NOPULL; } - err = nrfx_gpiote_in_init((nrfx_gpiote_pin_t)i, &cfg, + nrfx_err_t err = nrfx_gpiote_in_init((nrfx_gpiote_pin_t)i, &cfg, pinalarm_gpiote_handler); assert(err == NRFX_SUCCESS); + (void)err; // In case the assert doesn't use err. nrfx_gpiote_in_event_enable((nrfx_gpiote_pin_t)i, true); if (((high_alarms & mask) != 0) && ((low_alarms & mask) == 0)) { nrf_gpio_cfg_sense_set((uint32_t)i, NRF_GPIO_PIN_SENSE_HIGH); diff --git a/ports/nrf/common-hal/countio/Counter.c b/ports/nrf/common-hal/countio/Counter.c index e863f5475a..452c9d9c83 100644 --- a/ports/nrf/common-hal/countio/Counter.c +++ b/ports/nrf/common-hal/countio/Counter.c @@ -1,5 +1,8 @@ #include "common-hal/countio/Counter.h" + +#include "py/runtime.h" + #include "nrfx_gpiote.h" // obj array to map pin number -> self since nrfx hide the mapping @@ -29,11 +32,14 @@ void common_hal_countio_counter_construct(countio_counter_obj_t *self, .skip_gpio_setup = false }; - nrfx_gpiote_in_init(self->pin_a, &cfg, _intr_handler); + + nrfx_err_t err = nrfx_gpiote_in_init(self->pin_a, &cfg, _intr_handler); + if (err != NRFX_SUCCESS) { + mp_raise_RuntimeError(translate("All channels in use")); + } nrfx_gpiote_in_event_enable(self->pin_a, true); claim_pin(pin_a); - } bool common_hal_countio_counter_deinited(countio_counter_obj_t *self) { diff --git a/ports/nrf/common-hal/microcontroller/Processor.c b/ports/nrf/common-hal/microcontroller/Processor.c index f59fc54972..14eaeee386 100644 --- a/ports/nrf/common-hal/microcontroller/Processor.c +++ b/ports/nrf/common-hal/microcontroller/Processor.c @@ -27,6 +27,8 @@ #include "py/runtime.h" #include "common-hal/microcontroller/Processor.h" + +#include "common-hal/alarm/__init__.h" #include "shared-bindings/microcontroller/ResetReason.h" #include "supervisor/shared/translate.h" @@ -136,6 +138,12 @@ mcu_reset_reason_t common_hal_mcu_processor_get_reset_reason(void) { r = RESET_REASON_WATCHDOG; } else if (reset_reason_saved & POWER_RESETREAS_SREQ_Msk) { r = RESET_REASON_SOFTWARE; + // Our "deep sleep" is still actually light sleep followed by a software + // reset. Adding this check here ensures we treat it as-if we're waking + // from deep sleep. + if (sleepmem_wakeup_event != SLEEPMEM_WAKEUP_BY_NONE) { + r = RESET_REASON_DEEP_SLEEP_ALARM; + } } else if ((reset_reason_saved & POWER_RESETREAS_OFF_Msk) || (reset_reason_saved & POWER_RESETREAS_LPCOMP_Msk) || (reset_reason_saved & POWER_RESETREAS_NFC_Msk) || diff --git a/ports/nrf/common-hal/pulseio/PulseIn.c b/ports/nrf/common-hal/pulseio/PulseIn.c index f8f877d965..84545f5293 100644 --- a/ports/nrf/common-hal/pulseio/PulseIn.c +++ b/ports/nrf/common-hal/pulseio/PulseIn.c @@ -112,11 +112,6 @@ static void _pulsein_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action } void pulsein_reset(void) { - if (nrfx_gpiote_is_init()) { - nrfx_gpiote_uninit(); - } - nrfx_gpiote_init(NRFX_GPIOTE_CONFIG_IRQ_PRIORITY); - if (timer != NULL) { nrf_peripherals_free_timer(timer); } @@ -178,7 +173,10 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self, const mcu .hi_accuracy = true, .skip_gpio_setup = false }; - nrfx_gpiote_in_init(self->pin, &cfg, _pulsein_handler); + nrfx_err_t err = nrfx_gpiote_in_init(self->pin, &cfg, _pulsein_handler); + if (err != NRFX_SUCCESS) { + mp_raise_RuntimeError(translate("All channels in use")); + } nrfx_gpiote_in_event_enable(self->pin, true); } diff --git a/ports/nrf/common-hal/rotaryio/IncrementalEncoder.c b/ports/nrf/common-hal/rotaryio/IncrementalEncoder.c index bc5851ccd4..425da9e6de 100644 --- a/ports/nrf/common-hal/rotaryio/IncrementalEncoder.c +++ b/ports/nrf/common-hal/rotaryio/IncrementalEncoder.c @@ -64,8 +64,15 @@ void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencode .hi_accuracy = true, .skip_gpio_setup = false }; - nrfx_gpiote_in_init(self->pin_a, &cfg, _intr_handler); - nrfx_gpiote_in_init(self->pin_b, &cfg, _intr_handler); + nrfx_err_t err = nrfx_gpiote_in_init(self->pin_a, &cfg, _intr_handler); + if (err != NRFX_SUCCESS) { + mp_raise_RuntimeError(translate("All channels in use")); + } + err = nrfx_gpiote_in_init(self->pin_b, &cfg, _intr_handler); + if (err != NRFX_SUCCESS) { + nrfx_gpiote_in_uninit(self->pin_a); + mp_raise_RuntimeError(translate("All channels in use")); + } nrfx_gpiote_in_event_enable(self->pin_a, true); nrfx_gpiote_in_event_enable(self->pin_b, true); diff --git a/ports/nrf/supervisor/port.c b/ports/nrf/supervisor/port.c index 6af6fff5b2..17e9817b5c 100644 --- a/ports/nrf/supervisor/port.c +++ b/ports/nrf/supervisor/port.c @@ -33,6 +33,7 @@ #include "nrfx/hal/nrf_clock.h" #include "nrfx/hal/nrf_power.h" +#include "nrfx/drivers/include/nrfx_gpiote.h" #include "nrfx/drivers/include/nrfx_power.h" #include "nrfx/drivers/include/nrfx_rtc.h" @@ -252,6 +253,12 @@ void reset_port(void) { watchdog_reset(); #endif + // Always reset GPIOTE because it is shared. + if (nrfx_gpiote_is_init()) { + nrfx_gpiote_uninit(); + } + nrfx_gpiote_init(NRFX_GPIOTE_CONFIG_IRQ_PRIORITY); + reset_all_pins(); } diff --git a/supervisor/shared/bluetooth/file_transfer.c b/supervisor/shared/bluetooth/file_transfer.c index d105892582..908cc958d6 100644 --- a/supervisor/shared/bluetooth/file_transfer.c +++ b/supervisor/shared/bluetooth/file_transfer.c @@ -404,7 +404,7 @@ STATIC uint8_t _process_delete(const uint8_t *raw_buf, size_t command_len) { FATFS *fs = &((fs_user_mount_t *)MP_STATE_VM(vfs_mount_table)->obj)->fatfs; char *path = (char *)((uint8_t *)command) + header_size; path[command->path_length] = '\0'; - FRESULT result; + FRESULT result = FR_OK; FILINFO file; if (f_stat(fs, path, &file) == FR_OK) { if ((file.fattrib & AM_DIR) != 0) {