SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
BQ25896Core.cpp
Go to the documentation of this file.
1
30#include "../../../SensorBuildOpt.h"
31#if !SENSORLIB_EXCLUDE_PMIC_BQ25896
32
33#include "BQ25896Core.hpp"
34#include "BQ25896Regs.hpp"
35
36using namespace BQ25896Regs;
37
39
41{
42 uint8_t rev = getDeviceRevision();
43 return rev == BQ25896_DEV_REV;
44}
45
47{
48 int val = readReg(REG_DEVICE_REV);
49 return (val >= 0) ? (val & MASK_DEV_REV) : 0xFF;
50}
51
53{
54 int val = readReg(REG_DEVICE_REV);
55 return (val >= 0) ? ((val >> SHIFT_PN) & 0x07) : 0xFF;
56}
57
59{
60 setRegBit(REG_DEVICE_REV, 7);
61}
62
64{
65 int val = readReg(REG_ADC_BUSV);
66 return (val >= 0) && (val & MASK_VBUS_GD);
67}
68
70{
71 int val = readReg(REG_STATUS);
72 return (val >= 0) ? ((val >> SHIFT_VBUS_STAT) & 0x07) : 0;
73}
74
76{
77 int val = readReg(REG_STATUS);
78 return (val >= 0) ? ((val >> SHIFT_CHRG_STAT) & 0x03) : 0xFF;
79}
80
82{
83 int val = readReg(REG_STATUS);
84 return (val >= 0) && (val & MASK_PG_STAT);
85}
86
88{
89 return getChargeStatus() != CHARGE_STATE_NO_CHARGE;
90}
91
93{
94 return getChargeStatus() == CHARGE_STATE_DONE;
95}
96
98{
99 return getBusStatus() == BUS_STATE_OTG;
100}
101
103{
104 int val = readReg(REG_STATUS);
105 return (val >= 0) && (val & MASK_VSYS_STAT);
106}
107
109{
110 int val = readReg(REG_IDPM_STATUS);
111 return (val >= 0) && (val & MASK_VDPM_STAT);
112}
113
115{
116 int val = readReg(REG_IDPM_STATUS);
117 return (val >= 0) && (val & MASK_IDPM_STAT);
118}
119
120bool BQ25896Core::getFaultStatus(uint8_t &fault)
121{
122 int val = readReg(REG_FAULT);
123 if (val < 0) return false;
124 fault = static_cast<uint8_t>(val);
125 return true;
126}
127
129{
130 int val = readReg(REG_DEVICE_REV);
131 return (val >= 0) && (val & MASK_ICO_OPTIMIZED);
132}
133
134bool BQ25896Core::initImpl(uint8_t param)
135{
136 (void)param;
137 uint8_t rev = getDeviceRevision();
138 if (rev != BQ25896_DEV_REV) {
139 SENSORLIB_LOG_E("Device revision mismatch: expected 0x%02x, got 0x%02x",
140 static_cast<unsigned int>(BQ25896_DEV_REV),
141 static_cast<unsigned int>(rev));
142 return false;
143 }
144
145 // Perform a reset to ensure device is in known state
146 reset();
147
148 while (getRegBit(REG_DEVICE_REV, 7)) {
149 // Wait for reset bit to clear, indicating reset complete
150 hal->delay(10);
151 }
152
153 // Disable watchdog timer by default for safety (can be enabled via API)
154 return updateBits(REG_CHG_TIMER, MASK_WATCHDOG, 0) >= 0;
155}
156
157#endif
@license MIT License
@license MIT License
#define SENSORLIB_LOG_E(...)
void reset()
Reset device to default state.
bool isInputCurrentOptimizerOptimized()
Check if Input Current Optimizer has optimized.
uint8_t getChargeStatus()
Get charging status.
bool isOtg()
Check if in OTG (boost) mode.
bool isPowerGood()
Check if power is good.
uint8_t getDeviceConfig()
Get device configuration/part number.
bool isVsysInRegulation()
Check if VSYS is in regulation.
uint8_t getDeviceRevision() const
Get device revision.
bool isVindpmActive()
Get fault status.
bool getFaultStatus(uint8_t &fault)
Get fault status.
uint8_t getBusStatus()
Get USB port type status.
bool isValid() const
Check if device is valid/present.
bool isVbusPresent()
Check if VBUS (USB power) is present.
bool isCharging()
Check if actively charging.
bool isIindpmActive()
Check if IINDPM is active (input current limiting)
void end()
Deinitialize and release resources.
bool isChargeDone()
Check if charging is complete.
std::unique_ptr< SensorHal > hal
bool getRegBit(uint8_t reg, uint8_t bit)
int readReg(uint8_t reg) const
int updateBits(uint8_t reg, uint8_t mask, uint8_t value_shifted)
bool setRegBit(uint8_t reg, uint8_t bit)