Skip to content

low_power_go_pstate(): error returns leave interrupts disabled and USB deinitialised #3299

Open

Description

@gittest23

Summary

low_power_go_pstate(), behind low_power_pstate_until_aon_timer() and
low_power_pstate_until_gpio_pin_state(), first calls prepare_for_pstate_change() and then
has two error returns. Neither undoes the preparation. The caller gets an error code with
interrupts disabled, TinyUSB and USB stdio deinitialised, and the powman settings changed
(debug power request ignored, timer tick on LPOSC, VREG unlocked). Both functions are
documented as "0 on success, non-zero on error", so a caller would reasonably carry on after
an error.

Environment

pico-sdk 2.3.1, RP2350. The code is the same on master and develop as of today; #3192
changes this function but not these paths. Found by reading the code, not observed on
hardware.

Code (src/rp2_common/pico_low_power/low_power.c, 2.3.1)

  • L715: prepare_for_pstate_change(), which calls prepare_for_clock_switch() (L77-98):
    tud_deinit()/tuh_deinit() if they were initialised, stdio_usb_deinit(), then
    interrupt_flags = save_and_disable_interrupts();. After that come the powman changes
    (L119-127).
  • L722: return PICO_ERROR_INVALID_STATE; when powman_configure_wakeup_state() fails.
  • L738: return rc; when powman_set_power_state() fails.
  • post_pstate_change() (L129-130) is empty, so calling it on these paths would not undo
    anything either.

https://github.com/raspberrypi/pico-sdk/blob/2.3.1/src/rp2_common/pico_low_power/low_power.c#L708-L748
https://github.com/raspberrypi/pico-sdk/blob/2.3.1/src/rp2_common/pico_low_power/low_power.c#L77-L130

Suggested fix

If possible, do the checks that can fail before prepare_for_pstate_change(). Otherwise give
both error paths an undo: post_clock_switch() (it restores the interrupts and re-initialises
TinyUSB and USB stdio) plus reverting the powman changes. post_pstate_change() would be the
natural place for that.

Minor

interrupt_flags (L67) and irq_mask_disabled_during_sleep (L199) are single static slots
shared by both cores. The saved state is only right while one core at a time uses the API,
which may be worth a note in the documentation.

Related

#3298: the same kind of fault in flash_safe_execute(), where a nested call returns with
interrupts disabled and no error.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions