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

Open
#3,299 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 6 days

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
66/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
c
Domain
embedded-iot

Research direction

Read src/rp2_common/pico_low_power/low_power.c, starting with low_power_go_pstate() and the preparation and error paths described in the issue. Trace what state each failing call changes and how it can be restored before returning. Done means both error paths leave interrupts, USB, and powman settings in a safe state; the issue does not name a test to run.

Written by the indexing model from the issue text.

Description

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.

Dominant language
C
Stars
5k
Forks
1.3k
Avg merge
12d 19h
Merged PRs (30d)
5

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from raspberrypi/pico-sdk

All issues in raspberrypi/pico-sdk

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.