ESPHome 2026.3.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// Structure to hold all register data read in one transaction
66struct SY6970Data {
67 uint8_t registers[21]; // Registers 0x00-0x14 (includes unused 0x0F, 0x10)
68};
69
70// Listener interface for components that want to receive SY6970 data updates
72 public:
73 virtual void on_data(const SY6970Data &data) = 0;
74};
75
77 public:
78 SY6970Component(bool led_enabled, uint16_t input_current_limit, uint16_t charge_voltage, uint16_t charge_current,
79 uint16_t precharge_current, bool charge_enabled, bool enable_adc)
80 : led_enabled_(led_enabled),
81 input_current_limit_(input_current_limit),
82 charge_voltage_(charge_voltage),
83 charge_current_(charge_current),
84 precharge_current_(precharge_current),
85 charge_enabled_(charge_enabled),
86 enable_adc_(enable_adc) {}
87 void setup() override;
88 void dump_config() override;
89 void update() override;
90
91 // Listener registration
92 void add_listener(SY6970Listener *listener) { this->listeners_.push_back(listener); }
93
94 // Configuration methods to be called from lambdas
95 void set_input_current_limit(uint16_t milliamps);
96 void set_charge_target_voltage(uint16_t millivolts);
97 void set_precharge_current(uint16_t milliamps);
98 void set_charge_current(uint16_t milliamps);
99 void set_charge_enabled(bool enabled);
100 void set_led_enabled(bool enabled);
101 void set_enable_adc_measure(bool enabled = true);
102
103 protected:
104 bool read_all_registers_();
105 bool write_register_(uint8_t reg, uint8_t value);
106 bool update_register_(uint8_t reg, uint8_t mask, uint8_t value);
107
109 std::vector<SY6970Listener *> listeners_;
110
111 // Configuration values to set during setup()
119};
120
121} // namespace esphome::sy6970
This class simplifies creating components that periodically check a state.
Definition component.h:512
This Class provides the methods to read/write bytes from/to an i2c device.
Definition i2c.h:133
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:153
void set_charge_target_voltage(uint16_t millivolts)
Definition sy6970.cpp:134
void set_enable_adc_measure(bool enabled=true)
Definition sy6970.cpp:193
void set_input_current_limit(uint16_t milliamps)
Definition sy6970.cpp:118
void set_charge_enabled(bool enabled)
Definition sy6970.cpp:178
void set_precharge_current(uint16_t milliamps)
Definition sy6970.cpp:150
bool write_register_(uint8_t reg, uint8_t value)
Definition sy6970.cpp:20
void add_listener(SY6970Listener *listener)
Definition sy6970.h:92
void set_led_enabled(bool enabled)
Definition sy6970.cpp:185
void set_charge_current(uint16_t milliamps)
Definition sy6970.cpp:166
std::vector< SY6970Listener * > listeners_
Definition sy6970.h:109
bool update_register_(uint8_t reg, uint8_t mask, uint8_t value)
Definition sy6970.cpp:28
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)
Definition sy6970.h:78
virtual void on_data(const SY6970Data &data)=0
@ 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