2023–02–17:
Pinephone keyboard firmware update (1.3)
I revisited the pinephone keyboard firwmare with a goal to further optimize
the power consumption of the keyboard MCU. Originally I managed to optimize it
down to 9 mW power consumption when no key was pressed, and 20 mW when the key
was pressed. Recently I noticed that when the phone is left in the keyboard,
the phone's battery discharges at faster rate than expected – a few weeks
and the battery is dead instead of expected 2 months.
Not nice. Ideally one should not worry about phone battery discharging while
leaving the phone in the keyboard.
The
datasheet for the keyboard MCU tells us on page 150 that achievable power
consumption in power down mode should be ~100 uA, but the MCU is consuming
1700–3000 uA with the current firmware.
There's no guide for optimizing the power use of the MCU, so I had to play
around and try various things. It turned out most of the power consumption was
due to leaving MCU inputs floating. Enabling pullups and/or switching unused
pins to output mode made it so that the MCU started consuming expected 100uA in
so called „power down“ mode. In this mode, the MCU oscillators are shut off
(not even timers run in this mode), and the MCU can only recover from this via
external signal.
While it's nice that the MCU can follow the supply current specification,
I noticed during the debugging session that at times I was able to get even
lower power consumption. I tried to isolate the source of the reduction and it
turned out to be a 3.3V regulator used for USB interface of the MCU. MCU USB is
not exposed to users of the keyboard, and the user firmware turns it off.
Nevertheless, the firmware left this regulator enabled. Turning
it off gained us extra 50 uA of power savings.
After all this optimization, with the latest firmware, keyboard was consuming
50% less power than minimum achievable level given in the datasheet. :D So about
250 uW. Great. Looks like I unlocked some new achievement, …
Except the problem with this is that I2CA bus doesn't seem to work when USB
3.3V regulator is disabled, so we can't control the keyboard from the phone (MCU
works though, and can communicate over other GPIO pins…). I2CA seems to be
marked as 3.3V only in the datasheet, so I guess I2CA not working with internal
3.3V regulator disabled meakes some sense. We'll have to live with 700 uW.
There was
also another bug in the firmware, that prevented entry to „power down“
mode after the user pressed the first key after keyboard powerup. So basically
the MCU never even got to that original 9 mW powerdown mode power consumption
level in practice and it was just constantly consuming 20 mW. This was the
cause of unexpectedly fast battery draining.
So in summary, firmware v1.3 brings in ~30× reduction in power use. That
translates to theoretical increase in how long the phone can stay powered off in
the keyboard case (on full battery) from 23 days to 654 days (1.8 years).
This means that you can leave the phone connected to the keyboard without any
worries and without it draining the phone battery in any meaningful way.
Firmware upgrade howto
To upgrade the firmware, you need to download the updated firmware from my website,
extract it and run:
./arm64/ppkb-i2c-flasher -e i2c -i fw-user.bin write reset
The tool will output this on success:
Opening keyboard I2C device
Flashing code ROM
Finishing flashing
Restarting the MCU
And that's all there's to it. :)
Enjoy!
Active mode
Video from testing/debugging:
Addendum (2023–03–07)
For some reason firmware 1.3 doesn't have very reliable I2C connection.
Probably because the MCU power down mode affects the I2C slave modeule/code
somehow, now that the chip actually goes into powerdown way more often.
A lot more testing will be needed. :(