ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
sy6970.h
Go to the documentation of this file.
1#pragma once
2
5#include <vector>
6
7namespace esphome::sy6970 {
8
9// SY6970 Register addresses with descriptive names
10static const uint8_t SY6970_REG_INPUT_CURRENT_LIMIT = 0x00; // Input current limit control
11static const uint8_t SY6970_REG_VINDPM = 0x01; // Input voltage limit
12static const uint8_t SY6970_REG_ADC_CONTROL = 0x02; // ADC control and function disable
13static const uint8_t SY6970_REG_SYS_CONTROL = 0x03; // Charge enable and system config
14static const uint8_t SY6970_REG_CHARGE_CURRENT = 0x04; // Fast charge current limit
15static const uint8_t SY6970_REG_PRECHARGE_CURRENT = 0x05; // Pre-charge/termination current
16static const uint8_t SY6970_REG_CHARGE_VOLTAGE = 0x06; // Charge voltage limit
17static const uint8_t SY6970_REG_TIMER_CONTROL = 0x07; // Charge timer and status LED control
18static const uint8_t SY6970_REG_IR_COMP = 0x08; // IR compensation
19static const uint8_t SY6970_REG_FORCE_DPDM = 0x09; // Force DPDM detection
20static const uint8_t SY6970_REG_BOOST_CONTROL = 0x0A; // Boost mode voltage/current
21static const uint8_t SY6970_REG_STATUS = 0x0B; // System status (bus, charge status)
22static const uint8_t SY6970_REG_FAULT = 0x0C; // Fault status (NTC)
23static const uint8_t SY6970_REG_VINDPM_STATUS = 0x0D; // Input voltage limit status (also sys voltage)
24static const uint8_t SY6970_REG_BATV = 0x0E; // Battery voltage
25static const uint8_t SY6970_REG_VBUS_VOLTAGE = 0x11; // VBUS voltage
26static const uint8_t SY6970_REG_CHARGE_CURRENT_MONITOR = 0x12; // Charge current
27static const uint8_t SY6970_REG_INPUT_VOLTAGE_LIMIT = 0x13; // Input voltage limit
28static const uint8_t SY6970_REG_DEVICE_ID = 0x14; // Part information
29
30// Constants for voltage and current calculations
31static const uint16_t VBUS_BASE_MV = 2600; // mV
32static const uint16_t VBUS_STEP_MV = 100; // mV
33static const uint16_t VBAT_BASE_MV = 2304; // mV
34static const uint16_t VBAT_STEP_MV = 20; // mV
35static const uint16_t VSYS_BASE_MV = 2304; // mV
36static const uint16_t VSYS_STEP_MV = 20; // mV
37static const uint16_t CHG_CURRENT_STEP_MA = 50; // mA
38static const uint16_t PRE_CHG_BASE_MA = 64; // mA
39static const uint16_t PRE_CHG_STEP_MA = 64; // mA
40static const uint16_t CHG_VOLTAGE_BASE = 3840; // mV
41static const uint16_t CHG_VOLTAGE_STEP = 16; // mV
42static const uint16_t INPUT_CURRENT_MIN = 100; // mA
43static const uint16_t INPUT_CURRENT_STEP = 50; // mA
44
45// Bus Status values (REG_0B[7:5])
56
57// Charge Status values (REG_0B[4:3])
64
65// REG07[5:4]. When the watchdog elapses the chip reverts CHG_CONFIG and STAT_DIS to their defaults,
66// so an enabled watchdog is kicked on its own interval.
73
74// Structure to hold all register data read in one transaction
75struct SY6970Data {
76 uint8_t registers[21]; // Registers 0x00-0x14 (includes unused 0x0F, 0x10)
77};
78
79// Listener interface for components that want to receive SY6970 data updates
81 public:
82 virtual void on_data(const SY6970Data &data) = 0;
83};
84
85class SY6970Component final : public PollingComponent, public i2c::I2CDevice {
86 public:
87 SY6970Component(bool led_enabled, uint16_t input_current_limit, uint16_t charge_voltage, uint16_t charge_current,
88 uint16_t precharge_current, bool charge_enabled, bool enable_adc,
89 I2CWatchdogTimeout i2c_watchdog_timeout)
90 : led_enabled_(led_enabled),
91 input_current_limit_(input_current_limit),
92 charge_voltage_(charge_voltage),
93 charge_current_(charge_current),
94 precharge_current_(precharge_current),
95 charge_enabled_(charge_enabled),
96 enable_adc_(enable_adc),
97 i2c_watchdog_timeout_(i2c_watchdog_timeout) {}
98 void setup() override;
99 void dump_config() override;
100 void update() override;
101
102 // Listener registration
103 void add_listener(SY6970Listener *listener) { this->listeners_.push_back(listener); }
104
105 // Configuration methods to be called from lambdas
106 void set_input_current_limit(uint16_t milliamps);
107 void set_charge_target_voltage(uint16_t millivolts);
108 void set_precharge_current(uint16_t milliamps);
109 void set_charge_current(uint16_t milliamps);
110 void set_charge_enabled(bool enabled);
111 void set_led_enabled(bool enabled);
112 void set_enable_adc_measure(bool enabled = true);
114
116 bool reset_i2c_watchdog();
117
118 protected:
119 bool read_all_registers_();
120 bool write_register_(uint8_t reg, uint8_t value);
121 bool update_register_(uint8_t reg, uint8_t mask, uint8_t value);
122
124 std::vector<SY6970Listener *> listeners_;
125
126 // Configuration values to set during setup()
135};
136
137} // namespace esphome::sy6970
This class simplifies creating components that periodically check a state.
Definition component.h:514
This Class provides the methods to read/write bytes from/to an i2c device.
Definition i2c.h:132
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:152
void set_charge_target_voltage(uint16_t millivolts)
Definition sy6970.cpp:160
void set_enable_adc_measure(bool enabled=true)
Definition sy6970.cpp:219
void set_input_current_limit(uint16_t milliamps)
Definition sy6970.cpp:144
void set_charge_enabled(bool enabled)
Definition sy6970.cpp:204
SY6970Component(bool led_enabled, uint16_t input_current_limit, uint16_t charge_voltage, uint16_t charge_current, uint16_t precharge_current, bool charge_enabled, bool enable_adc, I2CWatchdogTimeout i2c_watchdog_timeout)
Definition sy6970.h:87
I2CWatchdogTimeout i2c_watchdog_timeout_
Definition sy6970.h:134
void set_precharge_current(uint16_t milliamps)
Definition sy6970.cpp:176
bool write_register_(uint8_t reg, uint8_t value)
Definition sy6970.cpp:39
void add_listener(SY6970Listener *listener)
Definition sy6970.h:103
void set_led_enabled(bool enabled)
Definition sy6970.cpp:211
bool reset_i2c_watchdog()
Kick the I2C watchdog (REG03 bit 6); runs on its own interval while enabled. Returns false when not w...
Definition sy6970.cpp:255
void set_charge_current(uint16_t milliamps)
Definition sy6970.cpp:192
void set_i2c_watchdog_timeout(I2CWatchdogTimeout timeout)
Definition sy6970.cpp:227
std::vector< SY6970Listener * > listeners_
Definition sy6970.h:124
bool update_register_(uint8_t reg, uint8_t mask, uint8_t value)
Definition sy6970.cpp:47
virtual void on_data(const SY6970Data &data)=0
@ I2C_WATCHDOG_160S
Definition sy6970.h:71
@ I2C_WATCHDOG_DISABLED
Definition sy6970.h:68
@ CHARGE_STATUS_CHARGE_DONE
Definition sy6970.h:62
@ CHARGE_STATUS_PRE_CHARGE
Definition sy6970.h:60
@ CHARGE_STATUS_NOT_CHARGING
Definition sy6970.h:59
@ CHARGE_STATUS_FAST_CHARGE
Definition sy6970.h:61
@ BUS_STATUS_USB_DCP
Definition sy6970.h:50
@ BUS_STATUS_USB_SDP
Definition sy6970.h:48
@ BUS_STATUS_ADAPTER
Definition sy6970.h:52
@ BUS_STATUS_NO_INPUT
Definition sy6970.h:47
@ BUS_STATUS_USB_CDP
Definition sy6970.h:49
@ BUS_STATUS_NO_STD_ADAPTER
Definition sy6970.h:53