Skip to content

RP2040: ignore pin changes before deep sleep #7196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 12, 2022
Merged
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
3 changes: 3 additions & 0 deletions ports/raspberrypi/common-hal/alarm/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@ void NORETURN common_hal_alarm_enter_deep_sleep(void) {

// Reset uses the watchdog. Use scratch registers to store wake reason
watchdog_hw->scratch[RP_WKUP_SCRATCH_REG] = _get_wakeup_cause();

// Just before reset, enable the pinalarm interrupt.
alarm_pin_pinalarm_entering_deep_sleep();
reset_cpu();
}

Expand Down
27 changes: 16 additions & 11 deletions ports/raspberrypi/common-hal/alarm/pin/PinAlarm.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,29 @@
STATIC bool woke_up;
STATIC uint64_t alarm_triggered_pins; // 36 actual pins
STATIC uint64_t alarm_reserved_pins; // 36 actual pins
STATIC bool _pinalarm_set = false;
STATIC bool _not_yet_deep_sleeping = false;

#define GPIO_IRQ_ALL_EVENTS 0x15u

STATIC void gpio_callback(uint gpio, uint32_t events) {
alarm_triggered_pins |= (1 << gpio);
woke_up = true;

// does this need to be called, to prevent IRQ from constantly going off?
gpio_acknowledge_irq(gpio, events);
// gpio_acknowledge_irq(gpio, events) is called automatically, before this callback is called.

// Disable IRQ automatically
gpio_set_irq_enabled(gpio, events, false);
gpio_set_dormant_irq_enabled(gpio, events, false);
if (_not_yet_deep_sleeping) {
// Event went off prematurely, before we went to sleep, so set it again.
gpio_set_irq_enabled(gpio, events, false);
} else {
// Went off during sleep.
// Disable IRQ automatically.
gpio_set_irq_enabled(gpio, events, false);
gpio_set_dormant_irq_enabled(gpio, events, false);
}
}

void alarm_pin_pinalarm_entering_deep_sleep() {
_not_yet_deep_sleeping = false;
}

void common_hal_alarm_pin_pinalarm_construct(alarm_pin_pinalarm_obj_t *self, const mcu_pin_obj_t *pin, bool value, bool edge, bool pull) {
Expand Down Expand Up @@ -156,11 +165,7 @@ void alarm_pin_pinalarm_set_alarms(bool deep_sleep, size_t n_alarms, const mp_ob
gpio_set_dormant_irq_enabled((uint)alarm->pin->number, event, true);
}

_pinalarm_set = true;
_not_yet_deep_sleeping = true;
}
}
}

bool alarm_pin_pinalarm_is_set(void) {
return _pinalarm_set;
}
2 changes: 1 addition & 1 deletion ports/raspberrypi/common-hal/alarm/pin/PinAlarm.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ void alarm_pin_pinalarm_reset(void);
void alarm_pin_pinalarm_light_reset(void);
void alarm_pin_pinalarm_set_alarms(bool deep_sleep, size_t n_alarms, const mp_obj_t *alarms);
bool alarm_pin_pinalarm_woke_this_cycle(void);
bool alarm_pin_pinalarm_is_set(void);
void alarm_pin_pinalarm_entering_deep_sleep(void);