ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
sy6970.h
Go to the documentation of this file.
1#pragma once
2
8
9namespace esphome::sy6970 {
10
11// SY6970 Register addresses with descriptive names
12static const uint8_t SY6970_REG_INPUT_CURRENT_LIMIT = 0x00; // Input current limit control
13static const uint8_t SY6970_REG_VINDPM = 0x01; // Input voltage limit
14static const uint8_t SY6970_REG_ADC_CONTROL = 0x02; // ADC control and function disable
15static const uint8_t SY6970_REG_SYS_CONTROL = 0x03; // Charge enable and system config
16static const uint8_t SY6970_REG_CHARGE_CURRENT = 0x04; // Fast charge current limit
17static const uint8_t SY6970_REG_PRECHARGE_CURRENT = 0x05; // Pre-charge/termination current
18static const uint8_t SY6970_REG_CHARGE_VOLTAGE = 0x06; // Charge voltage limit
19static const uint8_t SY6970_REG_TIMER_CONTROL = 0x07; // Charge timer and status LED control
20static const uint8_t SY6970_REG_IR_COMP = 0x08; // IR compensation
21static const uint8_t SY6970_REG_FORCE_DPDM = 0x09; // Force DPDM detection
22static const uint8_t SY6970_REG_BOOST_CONTROL = 0x0A; // Boost mode voltage/current
23static const uint8_t SY6970_REG_STATUS = 0x0B; // System status (bus, charge status)
24static const uint8_t SY6970_REG_FAULT = 0x0C; // Fault status (NTC)
25static const uint8_t SY6970_REG_VINDPM_STATUS = 0x0D; // Absolute input voltage limit
26static const uint8_t SY6970_REG_BATV = 0x0E; // Battery voltage
27static const uint8_t SY6970_REG_SYSV = 0x0F; // System voltage
28static const uint8_t SY6970_REG_VBUS_VOLTAGE = 0x11; // VBUS voltage
29static const uint8_t SY6970_REG_CHARGE_CURRENT_MONITOR = 0x12; // Charge current
30static const uint8_t SY6970_REG_INPUT_VOLTAGE_LIMIT = 0x13; // Input voltage limit
31static const uint8_t SY6970_REG_DEVICE_ID = 0x14; // Part information
32
33// Constants for voltage and current calculations
34static const uint16_t VBUS_BASE_MV = 2600; // mV
35static const uint16_t VBUS_STEP_MV = 100; // mV
36static const uint16_t VBAT_BASE_MV = 2304; // mV
37static const uint16_t VBAT_STEP_MV = 20; // mV
38static const uint16_t VSYS_BASE_MV = 2304; // mV
39static const uint16_t VSYS_STEP_MV = 20; // mV
40static const uint16_t CHG_CURRENT_STEP_MA = 50; // mA
41static const uint16_t PRE_CHG_BASE_MA = 64; // mA
42static const uint16_t PRE_CHG_STEP_MA = 64; // mA
43static const uint16_t CHG_VOLTAGE_BASE = 3840; // mV
44static const uint16_t CHG_VOLTAGE_STEP = 16; // mV
45static const uint16_t INPUT_CURRENT_MIN = 100; // mA
46static const uint16_t INPUT_CURRENT_STEP = 50; // mA
47
48// Bus Status values (REG_0B[7:5])
59
60// Charge Status values (REG_0B[4:3])
67
68// REG07[5:4]. When the watchdog elapses the chip reverts CHG_CONFIG and STAT_DIS to their defaults,
69// so an enabled watchdog is kicked on its own interval.
76
77// Structure to hold all register data read in one transaction
78struct SY6970Data {
79 uint8_t registers[21]; // Registers 0x00-0x14
80};
81
83inline uint16_t vbus_voltage_mv(uint8_t reg) {
84 return (reg & 0x80) != 0 ? VBUS_BASE_MV + (reg & 0x7F) * VBUS_STEP_MV : 0;
85}
86
87inline bool is_charging(uint8_t charge_status) {
88 return charge_status == CHARGE_STATUS_PRE_CHARGE || charge_status == CHARGE_STATUS_FAST_CHARGE;
89}
90
91// Indexed by status value; every value of the masked field has a string
92PROGMEM_STRING_TABLE(BusStatusStrings, "No Input", "USB SDP", "USB CDP", "USB DCP", "HVDCP", "Adapter",
93 "Non-Standard Adapter", "OTG");
94PROGMEM_STRING_TABLE(ChargeStatusStrings, "Not Charging", "Pre-charge", "Fast Charge", "Charge Done");
95PROGMEM_STRING_TABLE(NtcStatusStrings, "Normal", "Unknown", "Warm", "Cool", "Unknown", "Cold", "Hot", "Unknown");
96
97class SY6970Component final : public PollingComponent, public i2c::I2CDevice {
98 public:
99 SY6970Component(bool led_enabled, uint16_t input_current_limit, uint16_t charge_voltage, uint16_t charge_current,
100 uint16_t precharge_current, bool charge_enabled, bool enable_adc,
101 I2CWatchdogTimeout i2c_watchdog_timeout)
102 : led_enabled_(led_enabled),
103 input_current_limit_(input_current_limit),
104 charge_voltage_(charge_voltage),
105 charge_current_(charge_current),
106 precharge_current_(precharge_current),
107 charge_enabled_(charge_enabled),
108 enable_adc_(enable_adc),
109 i2c_watchdog_timeout_(i2c_watchdog_timeout) {}
110 void setup() override;
111 void dump_config() override;
112 void update() override;
113
114#ifdef USE_SY6970_DATA_PUBLISHER
116#endif
117
118 // Configuration methods to be called from lambdas
119 void set_input_current_limit(uint16_t milliamps);
120 void set_charge_target_voltage(uint16_t millivolts);
121 void set_precharge_current(uint16_t milliamps);
122 void set_charge_current(uint16_t milliamps);
123 void set_charge_enabled(bool enabled);
124 void set_led_enabled(bool enabled);
125 void set_enable_adc_measure(bool enabled = true);
127
129 bool reset_i2c_watchdog();
130
131 protected:
132 bool read_all_registers_();
133 bool write_register_(uint8_t reg, uint8_t value);
134 bool update_register_(uint8_t reg, uint8_t mask, uint8_t value);
135
137
138 // Configuration values to set during setup()
147};
148
149} // namespace esphome::sy6970
This class simplifies creating components that periodically check a state.
Definition component.h:542
This Class provides the methods to read/write bytes from/to an i2c device.
Definition i2c.h:144
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:164
void set_charge_target_voltage(uint16_t millivolts)
Definition sy6970.cpp:141
void set_enable_adc_measure(bool enabled=true)
Definition sy6970.cpp:200
void set_charge_enabled(bool enabled)
Definition sy6970.cpp:185
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:99
I2CWatchdogTimeout i2c_watchdog_timeout_
Definition sy6970.h:146
void set_precharge_current(uint16_t milliamps)
Definition sy6970.cpp:157
bool write_register_(uint8_t reg, uint8_t value)
Definition sy6970.cpp:32
void set_led_enabled(bool enabled)
Definition sy6970.cpp:192
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:237
void set_charge_current(uint16_t milliamps)
Definition sy6970.cpp:173
void set_i2c_watchdog_timeout(I2CWatchdogTimeout timeout)
Definition sy6970.cpp:208
HUB_PUBLISHER(data, const SY6970Data &) void set_input_current_limit(uint16_t milliamps)
bool update_register_(uint8_t reg, uint8_t mask, uint8_t value)
Definition sy6970.cpp:40
@ I2C_WATCHDOG_160S
Definition sy6970.h:74
@ I2C_WATCHDOG_DISABLED
Definition sy6970.h:71
uint16_t vbus_voltage_mv(uint8_t reg)
VBUS voltage in mV from REG11; 0 while VBUS_GD (bit 7) shows no input, as the ADC then reads its 2....
Definition sy6970.h:83
@ CHARGE_STATUS_CHARGE_DONE
Definition sy6970.h:65
@ CHARGE_STATUS_PRE_CHARGE
Definition sy6970.h:63
@ CHARGE_STATUS_NOT_CHARGING
Definition sy6970.h:62
@ CHARGE_STATUS_FAST_CHARGE
Definition sy6970.h:64
@ BUS_STATUS_USB_DCP
Definition sy6970.h:53
@ BUS_STATUS_USB_SDP
Definition sy6970.h:51
@ BUS_STATUS_ADAPTER
Definition sy6970.h:55
@ BUS_STATUS_NO_INPUT
Definition sy6970.h:50
@ BUS_STATUS_USB_CDP
Definition sy6970.h:52
@ BUS_STATUS_NO_STD_ADAPTER
Definition sy6970.h:56
PROGMEM_STRING_TABLE(I2CWatchdogTimeoutStrings, "Disabled", "40s", "80s", "160s", "Unknown")
bool is_charging(uint8_t charge_status)
Definition sy6970.h:87
uint32_t value
Definition hal.cpp:59