PF1550 Driver
API reference
Loading...
Searching...
No Matches
pf1550.hpp
Go to the documentation of this file.
1
26#pragma once
27
28#include <array>
29#include <cstddef>
30#include <cstdint>
31#include <span>
32
35#include "pf1550_profiles.hpp"
36#include "pf1550_registers.hpp"
38#include "pf1550_version.h"
39
40namespace pf1550 {
41
61template <typename BusType>
62class PF1550 {
63public:
65 enum class Error : uint16_t {
66 None = 0,
67 I2cWrite = 1 << 0,
68 I2cRead = 1 << 1,
69 InvalidParam = 1 << 2,
70 DeviceNotFound = 1 << 3,
71 NotInitialized = 1 << 4,
72 WrongDeviceId = 1 << 5,
73 ProfileApply = 1 << 6,
74 DiagnosticRead = 1 << 7,
75 SelfTestFailed = 1 << 8,
76 };
77
78 // ---------------------------------------------------------------------------
79 // Construction / lifecycle
80 // ---------------------------------------------------------------------------
81
90 PF1550(BusType* bus, uint8_t address = kDefaultI2cAddress) noexcept;
91
100 bool EnsureInitialized() noexcept;
101
103 bool IsInitialized() const noexcept { return initialized_; }
104
106 uint8_t GetI2cAddress() const noexcept { return address_; }
107
108 // ---------------------------------------------------------------------------
109 // Identity
110 // ---------------------------------------------------------------------------
111
118 bool ReadDeviceId(uint8_t& id) noexcept;
119
125 bool VerifyDevice() noexcept;
126
127 // ---------------------------------------------------------------------------
128 // Raw register access
129 // ---------------------------------------------------------------------------
130
132 bool ReadRegister(Register reg, uint8_t& value) noexcept;
133
135 bool WriteRegister(Register reg, uint8_t value) noexcept;
136
137 // ---------------------------------------------------------------------------
138 // Profile application
139 // ---------------------------------------------------------------------------
140
152 bool ApplyProfile(std::span<const profiles::RegisterWrite> profile) noexcept;
153
155 bool ApplyPortentaH7DefaultProfile() noexcept;
156
165 bool ApplyPortentaH7CarrierProfile() noexcept;
166
167 // ---------------------------------------------------------------------------
168 // Power mode / strap GPIOs
169 // ---------------------------------------------------------------------------
170
177 bool SetPowerMode(PowerMode mode) noexcept;
178
180 bool SetUsbRails(bool vbus_en, bool otg_en) noexcept;
181
182 // ---------------------------------------------------------------------------
183 // Status / OTP
184 // ---------------------------------------------------------------------------
185
191 bool ReadPmicStatus(uint8_t& status) noexcept;
192
194 bool ReadPmicState(PmicState& state) noexcept;
195
197 bool ReadChargerState(ChargerState& state, uint8_t* raw_reg = nullptr) noexcept;
198
200 bool ReadOtpByte(uint8_t otp_addr, uint8_t& value) noexcept;
201
203 bool ReadOtpRegion(uint8_t* buffer, size_t len, uint8_t start = OtpRegion::kStart) noexcept;
204
205 // ---------------------------------------------------------------------------
206 // Voltage / current configuration
207 // ---------------------------------------------------------------------------
208
220 bool SetSwVoltage(uint8_t sw_index, SwVoltageCode run_code) noexcept;
228 bool SetSwVoltageMv(uint8_t sw_index, uint16_t mv) noexcept;
229
231 bool SetLdoVoltage(uint8_t ldo_index, LdoVoltageCode code) noexcept;
232
234 bool SetVbusCurrentLimitMa(uint16_t limit_ma) noexcept;
235
236 // ---------------------------------------------------------------------------
237 // Interrupt management
238 // ---------------------------------------------------------------------------
239
246 bool ReadInterruptCategory(uint8_t& category) noexcept;
247
253 bool ReadLatchedFaults(FaultFlags& faults) noexcept;
254
260 bool ClearLatchedFaults() noexcept;
261
268 bool SetInterruptMaskAll(bool masked) noexcept;
269
270 // ---------------------------------------------------------------------------
271 // Diagnostics / self-test
272 // ---------------------------------------------------------------------------
273
281 bool ReadDiagnosticSnapshot(DiagnosticSnapshot& out) noexcept;
282
297 bool RefreshStatusSnapshot(DiagnosticSnapshot& snap) noexcept;
298
306 bool RunPowerSelfTest(SelfTestResult& out) noexcept;
307
308 // ---------------------------------------------------------------------------
309 // Driver-internal error tracking
310 // ---------------------------------------------------------------------------
311
313 uint16_t GetErrorFlags() const noexcept { return error_flags_; }
314
316 void ClearErrorFlags(uint16_t mask = 0xFFFF) noexcept {
317 error_flags_ &= static_cast<uint16_t>(~mask);
318 }
319
332 void SetSwDvsEnabled(uint8_t sw_index, bool enabled) noexcept {
333 if (sw_index == 1U || sw_index == 2U) {
334 sw_dvs_enabled_[sw_index - 1U] = enabled;
335 }
336 }
337
339 static constexpr const char* GetDriverVersion() noexcept { return HF_PF1550_VERSION; }
340
341private:
342 bool writeReg8(uint8_t reg, uint8_t value) noexcept;
343 bool readReg8(uint8_t reg, uint8_t& value) noexcept;
344 void setError(Error e) noexcept { error_flags_ |= static_cast<uint16_t>(e); }
345
346 // Snapshot helpers (templated to keep the header-only interface tidy).
347 bool fillRailSnapshot(DiagnosticSnapshot& snap) noexcept;
348 bool fillFaultSnapshot(DiagnosticSnapshot& snap) noexcept;
349
350 BusType* bus_;
351 uint8_t address_;
352 bool initialized_;
353 uint16_t error_flags_;
354 bool sw_dvs_enabled_[2]{false, false};
355};
356
357} // namespace pf1550
358
359#include "../src/pf1550.ipp"
CRTP-based PF1550 PMIC driver.
Definition pf1550.hpp:62
bool SetPowerMode(PowerMode mode) noexcept
Drive the STANDBY strap pin to RUN or STANDBY.
Definition pf1550.ipp:120
bool ReadOtpByte(uint8_t otp_addr, uint8_t &value) noexcept
Read a single OTP byte via FMRADDR/FMRDATA indirection.
Definition pf1550.ipp:181
bool ReadDeviceId(uint8_t &id) noexcept
Read DEVICE_ID (Reg 0x00).
Definition pf1550.ipp:59
bool ReadRegister(Register reg, uint8_t &value) noexcept
Read any PF1550 register by enumerated address.
Definition pf1550.ipp:81
bool ReadPmicStatus(uint8_t &status) noexcept
Read STATE_INFO (Reg 0x67) and return the raw byte.
Definition pf1550.ipp:151
bool RefreshStatusSnapshot(DiagnosticSnapshot &snap) noexcept
Refresh only the status part of a snapshot (≈14 register reads).
Definition pf1550.ipp:465
bool ReadPmicState(PmicState &state) noexcept
Decoded variant of ReadPmicStatus.
Definition pf1550.ipp:156
bool ClearLatchedFaults() noexcept
Write 0xFF to every category STAT register to clear all RW1C bits.
Definition pf1550.ipp:317
bool ReadLatchedFaults(FaultFlags &faults) noexcept
Read every category STAT register and aggregate latched faults.
Definition pf1550.ipp:276
bool ApplyPortentaH7CarrierProfile() noexcept
Apply the Portenta-H7-on-carrier profile.
Definition pf1550.ipp:115
void SetSwDvsEnabled(uint8_t sw_index, bool enabled) noexcept
Declare the SW1 / SW2 voltage encoding the part's OTP selects.
Definition pf1550.hpp:332
bool ReadChargerState(ChargerState &state, uint8_t *raw_reg=nullptr) noexcept
Read CHG_SNS (Reg 0x87) — charger sub-state. Returns decoded enum.
Definition pf1550.ipp:167
bool ReadDiagnosticSnapshot(DiagnosticSnapshot &out) noexcept
Build a full diagnostic snapshot (≈30 register reads).
Definition pf1550.ipp:430
bool SetSwVoltageMv(uint8_t sw_index, uint16_t mv) noexcept
Set the RUN voltage of a DVS-enabled SW1 or SW2 in millivolts.
Definition pf1550.ipp:220
bool WriteRegister(Register reg, uint8_t value) noexcept
Write any PF1550 register by enumerated address.
Definition pf1550.ipp:86
bool ApplyPortentaH7DefaultProfile() noexcept
Apply the legacy VFR-heritage Portenta H7 default profile.
Definition pf1550.ipp:110
bool ReadInterruptCategory(uint8_t &category) noexcept
Read INT_CATEGORY (Reg 0x06).
Definition pf1550.ipp:271
bool RunPowerSelfTest(SelfTestResult &out) noexcept
Boot-time self-test.
Definition pf1550.ipp:487
void ClearErrorFlags(uint16_t mask=0xFFFF) noexcept
Clear sticky error bits matching mask.
Definition pf1550.hpp:316
uint8_t GetI2cAddress() const noexcept
Currently configured 7-bit I²C address.
Definition pf1550.hpp:106
bool SetSwVoltage(uint8_t sw_index, SwVoltageCode run_code) noexcept
Set the RUN voltage of SW1 or SW2 from a SwVoltageCode.
Definition pf1550.ipp:215
uint16_t GetErrorFlags() const noexcept
Read all sticky error bits (see Error).
Definition pf1550.hpp:313
bool IsInitialized() const noexcept
Idempotently initialise the bus and verify the device. — query without re-running the bus init.
Definition pf1550.hpp:103
bool VerifyDevice() noexcept
Read DEVICE_ID and assert it matches kExpectedDeviceId.
Definition pf1550.ipp:67
Error
Bitmask error codes accumulated in error_flags_.
Definition pf1550.hpp:65
@ DiagnosticRead
One or more reads while building a snapshot failed.
@ SelfTestFailed
RunPowerSelfTest() reported a non-Info severity.
@ ProfileApply
At least one write in the last applied profile failed.
@ I2cRead
Last I²C read transaction failed.
@ NotInitialized
Called before EnsureInitialized() succeeded.
@ WrongDeviceId
Returned DEVICE_ID ≠ kExpectedDeviceId (0x7C).
@ DeviceNotFound
DEVICE_ID read returned NACK / wrong address.
@ InvalidParam
Caller-side argument validation failed.
@ I2cWrite
Last I²C write transaction failed.
bool SetLdoVoltage(uint8_t ldo_index, LdoVoltageCode code) noexcept
Write LDOn_VOLT for n=1..3. Returns false if ldo_index is invalid.
Definition pf1550.ipp:237
bool EnsureInitialized() noexcept
Idempotently initialise the bus and verify the device.
Definition pf1550.ipp:42
bool SetVbusCurrentLimitMa(uint16_t limit_ma) noexcept
Convenience: write VBUS_IN_LIM_CNFG (0x94) from a mA value.
Definition pf1550.ipp:257
bool ApplyProfile(std::span< const profiles::RegisterWrite > profile) noexcept
Apply an arbitrary profile (sequence of register writes).
Definition pf1550.ipp:91
bool ReadOtpRegion(uint8_t *buffer, size_t len, uint8_t start=OtpRegion::kStart) noexcept
Read a contiguous region of OTP memory (default: full 0x1C..0x36 range).
Definition pf1550.ipp:196
bool SetInterruptMaskAll(bool masked) noexcept
Mask (1) or unmask (0) all interrupt categories at once.
Definition pf1550.ipp:332
static constexpr const char * GetDriverVersion() noexcept
Driver version (string).
Definition pf1550.hpp:339
bool SetUsbRails(bool vbus_en, bool otg_en) noexcept
Drive USB_VBUS_EN and USB_OTG_EN straps (HIGH = enabled on Portenta).
Definition pf1550.ipp:140
Definition pf1550.hpp:40
Register
Symbolic register addresses used by the PF1550 driver.
Definition pf1550_registers.hpp:51
LdoVoltageCode
Common LDO output voltage codes used on Portenta H7 (Table 41).
Definition pf1550_registers.hpp:274
constexpr uint8_t kDefaultI2cAddress
Default 7-bit I²C slave address (settable via OTP — see Table 147).
Definition pf1550_registers.hpp:32
PmicState
STATE_INFO[5:0] values (Reg 0x67) describing the PMIC state machine.
Definition pf1550_registers.hpp:226
ChargerState
CHG_SNS[3:0] values (Reg 0x87) describing the charger sub-state.
Definition pf1550_registers.hpp:236
SwVoltageCode
Common SW buck output voltage codes (RUN mode).
Definition pf1550_registers.hpp:265
PowerMode
Definition pf1550_i2c_interface.hpp:35
PF1550 diagnostic / fault-classification types and snapshot containers.
CRTP I2C + GPIO interface for PF1550 driver.
Named PF1550 register-write sequences (eval / reference hosts).
NXP PF1550 family I2C register map, bit-field constants, and OTP keys.
PF1550 SW / LDO voltage-code ↔ millivolt conversion helpers.
Full diagnostic snapshot returned by PF1550::ReadDiagnosticSnapshot().
Definition pf1550_diagnostics.hpp:140
Bitset of latched fault flags.
Definition pf1550_diagnostics.hpp:79
OTP memory indirect address range (via FMRADDR/FMRDATA).
Definition pf1550_registers.hpp:325
Result of a power self-test pass.
Definition pf1550_diagnostics.hpp:162