SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
SY6970Core.cpp
Go to the documentation of this file.
1
30#include "../../../SensorBuildOpt.h"
31#if !SENSORLIB_EXCLUDE_PMIC_SY6970
32
33#include "SY6970Core.hpp"
34#include "SY6970Regs.hpp"
35
36using namespace SY6970Regs;
37
39
41{
42 uint8_t rev = getDeviceRevision();
43 return rev == SY6970_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 setRegBit(REG_DEVICE_REV, 7);
55}
56
58{
59 int val = readReg(REG_ADC_BUSV);
60 return (val >= 0) && (val & MASK_BUS_GD);
61}
62
64{
65 int val = readReg(REG_STATUS);
66 return (val >= 0) ? ((val >> SHIFT_BUS_STAT) & 0x07) : 0;
67}
68
70{
71 int val = readReg(REG_STATUS);
72 return (val >= 0) ? ((val >> SHIFT_CHRG_STAT) & 0x03) : 0xFF;
73}
74
76{
77 int val = readReg(REG_STATUS);
78 return (val >= 0) && (val & MASK_PG_STAT);
79}
80
82{
83 return getChargeStatus() != CHARGE_STATE_NO_CHARGE;
84}
85
87{
88 return getChargeStatus() == CHARGE_STATE_DONE;
89}
90
92{
93 return getBusStatus() == BUS_STATE_OTG;
94}
95
96bool SY6970Core::getFaultStatus(uint8_t &fault)
97{
98 int val = readReg(REG_FAULT);
99 if (val < 0) return false;
100 fault = static_cast<uint8_t>(val);
101 return true;
102}
103
104bool SY6970Core::initImpl(uint8_t param)
105{
106 (void)param;
107 uint8_t rev = getDeviceRevision();
108 if (rev != SY6970_DEV_REV) {
109 SENSORLIB_LOG_E("Device revision mismatch: expected 0x%02x, got 0x%02x",
110 static_cast<unsigned int>(SY6970_DEV_REV),
111 static_cast<unsigned int>(rev));
112 return false;
113 }
114 // Perform a reset to ensure device is in known state
115 reset();
116
117 while (getRegBit(REG_DEVICE_REV, 7)) {
118 // Wait for reset bit to clear, indicating reset complete
119 hal->delay(10);
120 }
121
122 // Disable watchdog timer by default for safety (can be enabled via API)
123 return updateBits(REG_CHG_TIMER, MASK_WATCHDOG, 0x00) >= 0;
124}
125
126#endif
@license MIT License
@license MIT License
#define SENSORLIB_LOG_E(...)
std::unique_ptr< SensorHal > hal
uint8_t getBusStatus()
Get USB port type status.
bool isOtg()
Check if in OTG (boost) mode.
void reset()
Reset device to default state.
void end()
Deinitialize and release resources.
bool isChargeDone()
Check if charging is complete.
bool isCharging()
Check if actively charging.
uint8_t getChargeStatus()
Get charging status.
uint8_t getDeviceRevision() const
Get device revision.
bool isVbusPresent()
Check if VBUS (USB power) is present.
bool isPowerGood()
Check if power is good.
bool isValid() const
Check if device is valid/present.
bool getFaultStatus(uint8_t &fault)
Get fault status.
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)