Skip to content

Commit eea01c3

Browse files
authored
Merge pull request #2868 from xobs/fix-usb-sleep-nrf
Fix usb sleep for NRF
2 parents edc5d89 + 67cf005 commit eea01c3

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

ports/nrf/peripherals/nrf/nvm.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040

4141
STATIC bool sd_is_enabled(void) {
4242
uint8_t sd_en = 0;
43+
if (__get_PRIMASK())
44+
return false;
4345
(void) sd_softdevice_is_enabled(&sd_en);
4446
return sd_en;
4547
}

ports/nrf/supervisor/port.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,11 @@
5151
#include "common-hal/rtc/RTC.h"
5252
#include "common-hal/neopixel_write/__init__.h"
5353

54+
#include "shared-bindings/microcontroller/__init__.h"
5455
#include "shared-bindings/rtc/__init__.h"
5556

57+
#include "lib/tinyusb/src/device/usbd.h"
58+
5659
#ifdef CIRCUITPY_AUDIOBUSIO
5760
#include "common-hal/audiobusio/I2SOut.h"
5861
#endif
@@ -264,7 +267,17 @@ void port_sleep_until_interrupt(void) {
264267
sd_app_evt_wait();
265268
} else {
266269
// Call wait for interrupt ourselves if the SD isn't enabled.
267-
__WFI();
270+
// Note that `wfi` should be called with interrupts disabled,
271+
// to ensure that the queue is properly drained. The `wfi`
272+
// instruction will returned as long as an interrupt is
273+
// available, even though the actual handler won't fire until
274+
// we re-enable interrupts.
275+
common_hal_mcu_disable_interrupts();
276+
if (!tud_task_event_ready()) {
277+
__DSB();
278+
__WFI();
279+
}
280+
common_hal_mcu_enable_interrupts();
268281
}
269282
}
270283

supervisor/shared/usb/tusb_config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@
4949
//--------------------------------------------------------------------+
5050
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE
5151

52+
#ifndef CFG_TUSB_DEBUG
5253
#define CFG_TUSB_DEBUG 0
54+
#endif
5355

5456
/*------------- RTOS -------------*/
5557
#ifndef CFG_TUSB_OS

0 commit comments

Comments
 (0)