Fix GPIOTE crashes by checking everything is ok

Fixes #5240 and fixes #5211
This commit is contained in:
Scott Shawcroft 2021-08-27 14:24:31 -07:00
parent 31b9dd408d
commit a8dd881ee5
No known key found for this signature in database
GPG Key ID: 0DFD512649C052DA
7 changed files with 39 additions and 20 deletions

View File

@ -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);

View File

@ -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) {

View File

@ -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) ||

View File

@ -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);
}

View File

@ -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);

View File

@ -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();
}

View File

@ -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) {