Fix PM1 button and wake IRQ handling and correct the power API docs - #329
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A register-level audit of
M5PM1_Classagainst the PM1 datasheet foundseveral defects:
getPekPress()reported a double click with the same value as a singleclick, and cleared the whole IRQ Status 3 register on any click — erasing a
pending WAKEUP flag as a side effect. It also cleared flags that arrived
between its read and its write.
clearWakeSource()used a read-modify-write: a wake bit that rose betweenthe read and the write was written back as zero and lost. Worse, the read
path folds an I2C failure into
0, so a single failed read caused thewrite to wipe every wake flag regardless of the mask.
gpio_function_texposedwake = 0b10, which the datasheet defines asReserved (the real wake configuration lives in separate registers this
class does not touch), and
setGPIOFunction()accepted any cast value,shifting out-of-range encodings into neighboring pins' mux fields.
getBatteryCharge(bool*)andgetBatteryVoltage(uint16_t*)dereferencedtheir output pointers without a null check.
(
PWR_CFGbit1 =3.3V_DCDC_EN), and the charge current/voltage/statusmethods documented parameter ranges for registers the PM1 does not have —
they are permanent stubs.
Fix
getPekPress()keeps the AXP-shaped contract (0/2; the PM1 does notreport long presses) and consumes the SINGLE and DOUBLE click flags with a
single selective write-zero-to-clear access that preserves WAKEUP. A double
click still reports
2soPower.getKeyState()consumers see a click, andthe new
wasPekDoubleClicked()reports the distinction without extra I2Ctraffic. If the clear write fails the event stays pending and
0isreturned instead of consuming it.
clearWakeSource()performs one selective write with no read, masked tothe documented bits
[6:0]. The docs now state the write-zero-to-clearassumption explicitly, since the datasheet does not specify the polarity.
gpio_function_t::wakeis removed andsetGPIOFunction()rejectsanything but
gpio,irq, andspecial.neighboring APIs.
dead commented-out implementations referencing nonexistent registers are
removed.
Breaking change
gpio_function_t::wakeno longer exists; code naming it fails to compile.It never worked — it wrote a reserved encoding.
Post-review amendment
A second local adversarial review round (two independent passes over the
final diff) converged on one real defect: the double-click latch was
set-only, so a stale double could be attributed to a much later single
click. The latch is now reassigned on every successfully reported click.
The
wasPekDoubleClicked()doc also states the single-task pollingexpectation, and the charge getter docs no longer show value ranges for
registers that do not exist.