37#include "pf1550_version.h"
44inline constexpr uint8_t kByteZero = 0_u8;
51template <
typename Port>
52concept DeclaresGpioSet =
56template <
typename Port>
58 requires { ExactHook<Port, bool(
CtrlPin,
GpioSignal)
noexcept>::Take(&Port::GpioSet); };
61template <
typename Port>
62concept DeclaresGpioGet =
requires(Port& port,
bool& level) { port.GpioGet(
CtrlPin::Intb, level); };
65template <
typename Port>
67 requires { ExactHook<Port, bool(
CtrlPin,
bool&)
noexcept>::Take(&Port::GpioGet); };
70template <
typename Port>
71concept DeclaresGetLastFailure =
72 requires(Port& port,
I2cFailure& why) { port.GetLastFailure(why); };
75template <
typename Port>
76concept HasGetLastFailure =
77 requires { ExactHook<Port, bool(
I2cFailure&)
noexcept>::Take(&Port::GetLastFailure); };
80template <
typename Port>
81concept DeclaresDelayUs =
requires(Port& port) { port.DelayUs(1_u32); };
84template <
typename Port>
85concept HasDelayUs =
requires { ExactHook<Port, void(uint32_t)
noexcept>::Take(&Port::DelayUs); };
88template <
typename Port>
89concept DeclaresLog =
requires(Port& port) { port.Log(
LogLevel::Info,
""); };
92template <
typename Port>
94 requires { ExactHook<Port, void(
LogLevel,
const char*)
noexcept>::Take(&Port::Log); };
98template <
typename Port>
99void DelayUs(Port& port, uint32_t us)
noexcept {
100 if constexpr (HasDelayUs<Port>) {
103 static_assert(kAlwaysFalse<Port>,
104 "pf1550: the port must implement `void DelayUs(uint32_t us) noexcept`");
109template <
typename Port>
110void Log(Port& port, [[maybe_unused]]
LogLevel level,
111 [[maybe_unused]]
const char* message)
noexcept {
112 if constexpr (HasLog<Port>) {
113 port.Log(level, message);
120[[nodiscard]]
constexpr PowerState StateOf(uint8_t raw)
noexcept {
125[[nodiscard]]
constexpr ChargerState DecodeChargerState(uint32_t code)
noexcept {
146[[nodiscard]]
constexpr BatteryState DecodeBatteryState(uint32_t code)
noexcept {
163 bool battery_oc)
noexcept {
192[[nodiscard]]
constexpr VbusFlags DecodeVbus(uint8_t raw)
noexcept {
193 const auto bits =
static_cast<uint32_t
>(raw);
247template <I2cPort Port>
250 !detail::DeclaresGpioSet<Port> || detail::HasGpioSet<Port>,
251 "pf1550: the optional GpioSet hook must be `bool GpioSet(CtrlPin, GpioSignal) noexcept`");
253 !detail::DeclaresGpioGet<Port> || detail::HasGpioGet<Port>,
254 "pf1550: the optional GpioGet hook must be `bool GpioGet(CtrlPin, bool&) noexcept`");
255 static_assert(!detail::DeclaresGetLastFailure<Port> || detail::HasGetLastFailure<Port>,
256 "pf1550: the optional GetLastFailure hook must be `bool "
257 "GetLastFailure(I2cFailure&) noexcept`");
258 static_assert(!detail::DeclaresDelayUs<Port> || detail::HasDelayUs<Port>,
259 "pf1550: the DelayUs hook must be `void DelayUs(uint32_t us) noexcept`");
261 !detail::DeclaresLog<Port> || detail::HasLog<Port>,
262 "pf1550: the optional Log hook must be `void Log(LogLevel, const char* message) noexcept`");
274 : bus_(port, address), config_(config) {}
557 latch_.fill(detail::kByteZero);
568 Result<uint8_t> rd(uint8_t address) noexcept;
569 Result<uint8_t> rdChecked(uint8_t address) noexcept;
570 Result<
void> wr(uint8_t address, uint8_t value) noexcept;
571 Result<
void> rmw(uint8_t address, uint8_t current, uint8_t mask, uint8_t value,
572 uint8_t unchecked) noexcept;
573 Result<
void> update(uint8_t address, uint8_t mask, uint8_t value) noexcept;
574 Result<
void> applyPlan(std::span<const detail::RegWrite> plan) noexcept;
575 Error busError() noexcept;
576 Result<
void> fail(
Error error, const
char* message) noexcept;
577 Result<
void> requireInitialized() const noexcept;
578 Result<
void> requireIdentified() const noexcept;
579 Result<
void> requireLive() noexcept;
580 void noteStatus(std::
size_t group, uint8_t value) noexcept;
581 Result<
void> checkReset() noexcept;
582 void recordSentinel(uint8_t address, uint8_t changed, uint8_t expected) noexcept;
583 Result<
void> updateOff(uint8_t address, uint8_t mask, uint8_t value) noexcept;
584 Result<
void> checkDeclaredDvs() noexcept;
586 bool standby_written,
bool sleep_written) noexcept;
587 [[nodiscard]]
bool dvsOf(
Rail rail) const noexcept;
588 [[nodiscard]]
bool latchEmpty() const noexcept;
591 [[nodiscard]]
bool inputLimitOk(uint8_t raw) const noexcept;
595 Result<
void> checkInputLimit() noexcept;
597 Result<
void> identify() noexcept;
599 Result<
void> makeSafe() noexcept;
602 Result<
void> applyBlocks() noexcept;
606 Result<
void> applyCharger() noexcept;
607 [[nodiscard]] const
BuckConfig* buckConfig(
Rail rail) const noexcept;
608 [[nodiscard]] const
RailModes* modesOf(
Rail rail) const noexcept;
612 Result<
void> clearDvsDone(
Rail rail) noexcept;
613 Result<
bool> railsAsConfigured() noexcept;
614 Result<
bool> noLatchedFault() noexcept;
618 Result<
void> requireSystemOn() noexcept;
619 Result<
void> setPwrCtrl3(uint8_t bit) noexcept;
623 [[nodiscard]]
bool intbIdle() noexcept;
627 Result<
void> runPendingLimitCheck() noexcept;
630 static constexpr uint32_t kDvsPollUs = 16U;
632 static constexpr std::
size_t kSentinels = 4U;
634 detail::RegisterBus<Port> bus_;
636 std::array<detail::Sentinel, kSentinels> sentinels_{};
637 std::size_t newest_{0};
638 uint32_t restarts_{0};
639 std::array<uint8_t, kIrqGroups> latch_{};
640 std::array<bool, 2> dvs_{};
641 bool identified_{
false};
642 bool limit_pending_{
false};
643 bool intb_complete_{
false};
644 bool initialized_{
false};
652#include "../src/pf1550.ipp"
Driver for one PF1550 on a platform port.
Definition pf1550.hpp:248
Result< void > EnsureInitialized() noexcept
Start the driver: validate the configuration, bring the bus up, check DEVICE_ID = 0x7C and check the ...
PF1550 & operator=(const PF1550 &)=delete
One driver object per chip.
Result< uint8_t > ReadRegister(uint8_t address) noexcept
Read one documented register as it is, with no decoding: a read-only escape hatch for bench work....
Result< void > CancelShutdownRequest() noexcept
Clear GOTO_SHIP and GOTO_CORE_OFF (write 0 to PWRCTRL3) and read it back. Works after DEVICE_ID has p...
Result< void > EnableLoadSwitch(Rail rail, LoadSwitchConfirm confirm) noexcept
Turn LDO1 or LDO3 into a load switch. IRREVERSIBLE: the chip cannot go back until the next power cycl...
Result< void > SetIrqMask(const IrqMask &mask) noexcept
Write the interrupt masks (1 masks a source). Reserved bits are preserved.
Result< ChargerStatus > ReadChargerStatus() noexcept
One pass over VBUS_SNS, CHG_SNS (twice), BATT_SNS: states, flags and the classified fault....
PF1550(Port &port, const Config &config, uint8_t address=kI2cAddress) noexcept
Bind the driver to a port and a 7-bit I2C address, and store the configuration. No I/O,...
Definition pf1550.hpp:273
Result< SelfCheckReport > SelfCheck() noexcept
Result< void > SetChargerMode(ChargerMode mode) noexcept
Change the charger mode. Off and LinearOnly are always allowed once DEVICE_ID has passed (also when E...
const Config & GetConfig() const noexcept
Definition pf1550.hpp:316
Result< void > ServiceChargerWatchdog() noexcept
Clear the charger watchdog (WDTCLR = 1). With watchdog.enable call it every kWatchdogServiceMaxS seco...
Result< void > Configure() noexcept
Write every block of the configuration whose apply is true: rails in rail_order, the small blocks,...
Result< PowerDownSequence > ReadPowerDownSequence() noexcept
Read the power-down order.
Result< void > RestartCharger() noexcept
Leave a latched fault state: off (needs only an identified chip), verify, charge (needs a live driver...
void ClearLatchedEvents() noexcept
Empty the latch. No I/O.
Definition pf1550.hpp:556
Result< RailReadout > ReadRail(Rail rail) noexcept
Read a rail.
bool IsInitialized() const noexcept
Definition pf1550.hpp:311
Result< uint8_t > ReadSense(IrqGroup group) noexcept
Read the live SENSE register of a group.
Result< Categories > ReadCategories() noexcept
Read INT_CATEGORY.
Result< void > SetRailModes(Rail rail, const RailModes &modes) noexcept
Change which states a rail is on in.
Result< void > EnterCoreOff(CoreOffConfirm) noexcept
Arm CORE_OFF (GOTO_CORE_OFF). The bit stays set; it takes effect when the part is in REGS_DISABLE (VB...
Result< Snapshot > ReadSnapshot() noexcept
Result< PowerState > ReadPowerState() noexcept
InterruptEvents GetLatchedEvents() const noexcept
The status bits read by any call since the last ClearLatchedEvents() or service. No I/O.
Definition pf1550.hpp:549
PF1550(const PF1550 &)=delete
One driver object per chip.
~PF1550() noexcept=default
The driver holds a reference to the port.
Result< void > SetRailVoltage(Rail rail, Microvolts uv) noexcept
Set a rail's run-mode voltage. SW1 and SW2 only when declared Dvs::Enabled; waits for the DVS-done ev...
static constexpr uint32_t kMaxChargerRestarts
Most restarts of a latched charger fault in a row before RestartCharger() refuses.
Definition pf1550.hpp:565
Result< void > EnterShip(ShipConfirm) noexcept
Request SHIP mode (GOTO_SHIP): every supply, VSNVS included, turns off.
Result< void > RequestStandby(bool standby) noexcept
Drive the STANDBY pin (needs GpioSet()); Unsupported without it.
Result< InterruptEvents > ServiceInterrupts() noexcept
Read and clear the pending interrupts: INT_CATEGORY, then each flagged group's status byte (copied ou...
Result< VariantInfo > ReadVariant() noexcept
Result< ChargerAsFound > CheckChargerAgainstLimits() noexcept
Result< bool > IntbAsserted() noexcept
Read INTB.
Result< void > SetPowerDownSequence(const PowerDownSequence &sequence) noexcept
Write the power-down order. Needs Acknowledgements::power_down_order_reviewed: the order decides whic...
constexpr PowerState DecodePowerState(uint32_t code) noexcept
The 4-bit power-state code of STATE_INFO (Table 146) as a PowerState.
Definition pf1550_codec.hpp:259
constexpr bool HasFault(const std::array< uint8_t, kIrqGroups > &status) noexcept
Definition pf1550_irq.hpp:142
constexpr uint8_t kVbusIn2Sys
VBUS_SNS.VBUS_IN2SYS_SNS.
Definition pf1550_registers.hpp:244
constexpr uint8_t kVbusDpm
VBUS_SNS.VBUS_DPM_SNS.
Definition pf1550_registers.hpp:247
constexpr uint8_t kVbusUvlo
VBUS_SNS.VBUS_UVLO_SNS.
Definition pf1550_registers.hpp:243
constexpr uint8_t kVbusValid
VBUS_SNS.VBUS_VALID.
Definition pf1550_registers.hpp:246
constexpr uint8_t kVbusOvlo
VBUS_SNS.VBUS_OVLO_SNS.
Definition pf1550_registers.hpp:245
Error
Why a call failed. Every call of the driver returns one of these in its Result.
Definition pf1550_types.hpp:38
BatteryState
Battery state (BATT_SNS[2:0], Table 155).
Definition pf1550_types.hpp:458
constexpr std::size_t kRailCount
Number of rails.
Definition pf1550_types.hpp:259
ChargerMode
Charger operating mode (CHG_OPER, Table 156).
Definition pf1550_types.hpp:282
Fault
The fault the charger reports, classified from the state and flag registers.
Definition pf1550_types.hpp:469
Rail
The power rails the driver configures.
Definition pf1550_types.hpp:249
IrqGroup
Interrupt groups, one status/mask/sense register triple each.
Definition pf1550_types.hpp:504
ChargerState
Charger state (CHG_SNS[3:0], Table 154).
Definition pf1550_types.hpp:442
PowerState
STATE_INFO decoded (Table 146).
Definition pf1550_types.hpp:239
LogLevel
Severity passed to the optional Log() hook.
Definition pf1550_types.hpp:183
@ Unknown
A reserved code.
@ NotDetected
No battery with a valid input after wake-up.
@ Overvoltage
Battery overvoltage with a valid input.
@ AbovePrecharge
Above the precharge threshold.
@ NoValidVbus
No valid input.
@ BatteryMissing
BATT_SNS 6.
@ ThermalShutdown
CHG_SNS 10.
@ BatteryOvercurrent
BATTOC_SNS.
@ ThermistorSuspend
CHG_SNS 7.
@ BatteryOvervoltage
CHG_SNS 9 or BATT_SNS 5.
@ UnknownState
A reserved CHG_SNS or BATT_SNS code.
@ WatchdogExpired
WDT_SNS.
@ ThermalShutdown
Junction above TSHDN.
@ LinearOnly
Linear regulator only, not charging.
@ ThermistorSuspend
Battery temperature outside the window.
@ Unknown
A reserved code: reported as a fault, never as charging.
@ BatteryOvervoltage
Battery overvoltage.
@ Off
Input invalid or charger disabled.
@ TimerFault
Timer fault: only off then on exits it.
The PF1550 driver: everything it declares lives here; internals in detail.
Definition pf1550.hpp:39
CtrlPin
Control pins the port may reach through the optional GpioSet() and GpioGet() hooks.
Definition pf1550_i2c_interface.hpp:45
@ Intb
INTB (pin 9), open-drain output of the chip: GpioGet() reports asserted = low.
constexpr uint8_t kI2cAddress
Default 7-bit I2C address (section 9.7.1).
Definition pf1550_i2c_interface.hpp:80
GpioSignal
Logical level of a control pin; the port maps it to the physical polarity.
Definition pf1550_i2c_interface.hpp:52
@ ACTIVE
The pin function is asserted.
I2cFailure
Why the last transfer failed, reported by the optional GetLastFailure() hook.
Definition pf1550_i2c_interface.hpp:58
Register codecs of the PF1550: voltage and current grids of the data sheet tables,...
PF1550 configuration: the application's Limits, its explicit acknowledgements, the typed configuratio...
Compile-time helpers of the PF1550 driver: typed integer literals, exact hook matching,...
The I2C port of the PF1550 driver: the CRTP base, its pin enums, the I2cPort concept and the register...
Interrupt map of the PF1550: which register and bit holds each source, and helpers over the events Se...
The register writes of each configuration block as pure constexpr data.
PF1550 register map: addresses, bit fields, reset values and the access table (datasheet section 12).
The regulator power-up and power-down sequencer of the PF1550 as pure constexpr data: the pre-program...
Public types of the PF1550 driver: errors, Result<T>, enumerations, units, status and interrupt types...
A buck regulator. apply = false leaves the block as the chip holds it.
Definition pf1550_config.hpp:80
Which category-summary bits are set in INT_CATEGORY (Table 89).
Definition pf1550_types.hpp:570
Charger registers as found at start-up (the as-found check).
Definition pf1550_types.hpp:599
The charger status of one ReadChargerStatus() pass (separate reads, not atomic).
Definition pf1550_types.hpp:491
Everything the driver writes. The constructor stores it with no I/O.
Definition pf1550_config.hpp:245
Token: the caller confirms that EnterCoreOff() turns the PMIC core off.
Definition pf1550_types.hpp:635
Interrupt events returned by ServiceInterrupts(), one status byte per group.
Definition pf1550_types.hpp:562
std::array< uint8_t, kIrqGroups > status
The status bytes as read, before any clear.
Definition pf1550_types.hpp:563
Interrupt masks, one byte per group; a set bit masks the source. The default masks every source,...
Definition pf1550_types.hpp:556
An LDO.
Definition pf1550_config.hpp:95
Token: the caller confirms that a load switch has no current limit and cannot go back,...
Definition pf1550_types.hpp:641
A voltage in microvolts, so that grids such as 12.5 mV steps stay exact.
Definition pf1550_types.hpp:191
The registers of the power-down sequence (Tables 139 to 145).
Definition pf1550_types.hpp:582
Per-state rail enables (EN, STBY_EN, OMODE, LPWR).
Definition pf1550_config.hpp:72
One rail as the chip holds it.
Definition pf1550_types.hpp:589
The value of a call, or why there is none.
Definition pf1550_types.hpp:123
Result of SelfCheck(): what is wrong, if anything.
Definition pf1550_types.hpp:624
Token: the caller confirms that EnterShip() cuts all supplies, VSNVS included.
Definition pf1550_types.hpp:633
A read-only view of the chip for diagnostics (ReadSnapshot()).
Definition pf1550_types.hpp:613
What ReadVariant() reports.
Definition pf1550_types.hpp:230
VBUS flags of VBUS_SNS (Table 153).
Definition pf1550_types.hpp:482
bool uvlo
VBUS_UVLO_SNS.
Definition pf1550_types.hpp:485
bool valid
VBUS_VALID.
Definition pf1550_types.hpp:483