ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
sy6970.cpp
Go to the documentation of this file.
1#include "sy6970.h"
2#include "esphome/core/hal.h"
3#include "esphome/core/log.h"
4
5namespace esphome::sy6970 {
6
7ESPHOME_LOG_TAG(TAG, "sy6970");
8
9static const LogString *i2c_watchdog_timeout_to_string(I2CWatchdogTimeout timeout) {
10 switch (timeout) {
12 return LOG_STR("Disabled");
14 return LOG_STR("40s");
16 return LOG_STR("80s");
18 return LOG_STR("160s");
19 default:
20 return LOG_STR("Unknown");
21 }
22}
23
24static constexpr uint32_t I2C_WATCHDOG_KICK_INTERVAL_ID = 0;
25// Well under the 40 s default, so a failed kick or a timeout the chip did not accept still leaves margin
26static constexpr uint32_t I2C_WATCHDOG_KICK_INTERVAL_MS = 10000;
27
29 // Read all registers from 0x00 to 0x14 in one transaction (21 bytes)
30 // This includes unused registers 0x0F, 0x10 for performance
31 if (!this->read_bytes(SY6970_REG_INPUT_CURRENT_LIMIT, this->data_.registers, 21)) {
32 ESP_LOGW(TAG, "Failed to read registers 0x00-0x14");
33 return false;
34 }
35
36 return true;
37}
38
39bool SY6970Component::write_register_(uint8_t reg, uint8_t value) {
40 if (!this->write_byte(reg, value)) {
41 ESP_LOGW(TAG, "Failed to write register 0x%02X", reg);
42 return false;
43 }
44 return true;
45}
46
47bool SY6970Component::update_register_(uint8_t reg, uint8_t mask, uint8_t value) {
48 if (this->update_register(reg, mask, value))
49 return true;
50 ESP_LOGW(TAG, "Failed to update register 0x%02X", reg);
51 return false;
52}
53
55 ESP_LOGV(TAG, "Setting up SY6970...");
56
57 // Try to read chip ID
58 uint8_t reg_value;
59 if (!this->read_byte(SY6970_REG_DEVICE_ID, &reg_value)) {
60 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
61 return;
62 }
63
64 uint8_t chip_id = reg_value & 0x03;
65 if (chip_id != 0x00) {
66 ESP_LOGW(TAG, "Unexpected chip ID: 0x%02X (expected 0x00)", chip_id);
67 }
68
69 // Service the watchdog first, so the settings below are not reverted while they are written
70 ESP_LOGV(TAG, "Setting I2C watchdog timeout to %s",
71 LOG_STR_ARG(i2c_watchdog_timeout_to_string(this->i2c_watchdog_timeout_)));
73
74 // Apply configuration options (all have defaults now)
75 ESP_LOGV(TAG, "Setting LED enabled to %s", ONOFF(this->led_enabled_));
76 this->set_led_enabled(this->led_enabled_);
77
78 ESP_LOGV(TAG, "Setting input current limit to %u mA", this->input_current_limit_);
80
81 ESP_LOGV(TAG, "Setting charge voltage to %u mV", this->charge_voltage_);
83
84 ESP_LOGV(TAG, "Setting charge current to %u mA", this->charge_current_);
86
87 ESP_LOGV(TAG, "Setting precharge current to %u mA", this->precharge_current_);
89
90 ESP_LOGV(TAG, "Setting charge enabled to %s", ONOFF(this->charge_enabled_));
92
93 ESP_LOGV(TAG, "Setting ADC measurements to %s", ONOFF(this->enable_adc_));
95
96 ESP_LOGV(TAG, "SY6970 initialized successfully");
97}
98
100 ESP_LOGCONFIG(TAG,
101 "SY6970:\n"
102 " LED Enabled: %s\n"
103 " Input Current Limit: %u mA\n"
104 " Charge Voltage: %u mV\n"
105 " Charge Current: %u mA\n"
106 " Precharge Current: %u mA\n"
107 " Charge Enabled: %s\n"
108 " ADC Enabled: %s\n"
109 " I2C Watchdog Timeout: %s",
110 ONOFF(this->led_enabled_), this->input_current_limit_, this->charge_voltage_, this->charge_current_,
111 this->precharge_current_, ONOFF(this->charge_enabled_), ONOFF(this->enable_adc_),
112 LOG_STR_ARG(i2c_watchdog_timeout_to_string(this->i2c_watchdog_timeout_)));
113 LOG_I2C_DEVICE(this);
114 LOG_UPDATE_INTERVAL(this);
115}
116
118 if (this->is_failed()) {
119 return;
120 }
121
122 // Read all registers in one transaction
123 if (!this->status_comm_result(this->read_all_registers_())) {
124 ESP_LOGW(TAG, "Failed to read registers during update");
125 return;
126 }
127
128 // Notify all listeners with the new data
129 for (auto *listener : this->listeners_) {
130 listener->on_data(this->data_);
131 }
132}
133
135 if (this->is_failed())
136 return;
137
138 if (milliamps < INPUT_CURRENT_MIN) {
139 milliamps = INPUT_CURRENT_MIN;
140 }
141
142 uint8_t val = (milliamps - INPUT_CURRENT_MIN) / INPUT_CURRENT_STEP;
143 if (val > 0x3F) {
144 val = 0x3F;
145 }
146
147 this->update_register_(SY6970_REG_INPUT_CURRENT_LIMIT, 0x3F, val);
148}
149
151 if (this->is_failed())
152 return;
153
154 if (millivolts < CHG_VOLTAGE_BASE) {
155 millivolts = CHG_VOLTAGE_BASE;
156 }
157
158 uint8_t val = (millivolts - CHG_VOLTAGE_BASE) / CHG_VOLTAGE_STEP;
159 if (val > 0x3F) {
160 val = 0x3F;
161 }
162
163 this->update_register_(SY6970_REG_CHARGE_VOLTAGE, 0xFC, val << 2);
164}
165
167 if (this->is_failed())
168 return;
169
170 if (milliamps < PRE_CHG_BASE_MA) {
171 milliamps = PRE_CHG_BASE_MA;
172 }
173
174 uint8_t val = (milliamps - PRE_CHG_BASE_MA) / PRE_CHG_STEP_MA;
175 if (val > 0x0F) {
176 val = 0x0F;
177 }
178
179 this->update_register_(SY6970_REG_PRECHARGE_CURRENT, 0xF0, val << 4);
180}
181
182void SY6970Component::set_charge_current(uint16_t milliamps) {
183 if (this->is_failed())
184 return;
185
186 uint8_t val = milliamps / 64;
187 if (val > 0x7F) {
188 val = 0x7F;
189 }
190
191 this->update_register_(SY6970_REG_CHARGE_CURRENT, 0x7F, val);
192}
193
195 if (this->is_failed())
196 return;
197
198 this->update_register_(SY6970_REG_SYS_CONTROL, 0x10, enabled ? 0x10 : 0x00);
199}
200
202 if (this->is_failed())
203 return;
204
205 // Bit 6: 0 = LED enabled, 1 = LED disabled
206 this->update_register_(SY6970_REG_TIMER_CONTROL, 0x40, enabled ? 0x00 : 0x40);
207}
208
210 if (this->is_failed())
211 return;
212
213 // Set bits to enable ADC conversion
214 this->update_register_(SY6970_REG_ADC_CONTROL, 0xC0, enabled ? 0xC0 : 0x00);
215}
216
218 if (this->is_failed())
219 return;
220
221 if (timeout != I2C_WATCHDOG_DISABLED) {
222 // Changing the timeout does not restart the chip's timer, which may be close to expiring
223 this->reset_i2c_watchdog();
224 }
225
226 // REG07 bits 5:4 (WATCHDOG[1:0])
227 bool written = this->update_register_(SY6970_REG_TIMER_CONTROL, 0x30, static_cast<uint8_t>(timeout) << 4);
228 if (!written) {
229 ESP_LOGW(TAG, "Failed to set I2C watchdog timeout to %s", LOG_STR_ARG(i2c_watchdog_timeout_to_string(timeout)));
230 }
231
232 if (timeout == I2C_WATCHDOG_DISABLED && written) {
233 this->i2c_watchdog_timeout_ = timeout;
234 this->cancel_interval(I2C_WATCHDOG_KICK_INTERVAL_ID);
235 return;
236 }
237 if (timeout != I2C_WATCHDOG_DISABLED) {
238 this->i2c_watchdog_timeout_ = timeout;
239 }
240 // Keeps kicking after a failed write too: the chip may still be on its 40 s default
241 this->set_interval(I2C_WATCHDOG_KICK_INTERVAL_ID, I2C_WATCHDOG_KICK_INTERVAL_MS,
242 [this]() { this->reset_i2c_watchdog(); });
243}
244
246 if (this->is_failed())
247 return false;
248
249 // WD_RST, self-clearing
250 if (!this->update_register_(SY6970_REG_SYS_CONTROL, 0x40, 0x40)) {
251 ESP_LOGW(TAG, "I2C watchdog reset failed, settings may revert");
252 return false;
253 }
254 return true;
255}
256
257} // namespace esphome::sy6970
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:287
bool cancel_interval(const char *name)
Cancel an interval function.
Definition component.cpp:92
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
void set_interval(const char *name, uint32_t interval, std::function< void()> &&f)
Set an interval function with a const char* name.
Definition component.cpp:88
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:312
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:272
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:164
bool update_register(uint8_t a_register, uint8_t mask, uint8_t value)
Read a register, replace the bits in mask with those of value and write it back.
Definition i2c.cpp:95
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
Definition i2c.h:249
void set_charge_target_voltage(uint16_t millivolts)
Definition sy6970.cpp:150
void set_enable_adc_measure(bool enabled=true)
Definition sy6970.cpp:209
void set_input_current_limit(uint16_t milliamps)
Definition sy6970.cpp:134
void set_charge_enabled(bool enabled)
Definition sy6970.cpp:194
I2CWatchdogTimeout i2c_watchdog_timeout_
Definition sy6970.h:134
void set_precharge_current(uint16_t milliamps)
Definition sy6970.cpp:166
bool write_register_(uint8_t reg, uint8_t value)
Definition sy6970.cpp:39
void set_led_enabled(bool enabled)
Definition sy6970.cpp:201
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:245
void set_charge_current(uint16_t milliamps)
Definition sy6970.cpp:182
void set_i2c_watchdog_timeout(I2CWatchdogTimeout timeout)
Definition sy6970.cpp:217
std::vector< SY6970Listener * > listeners_
Definition sy6970.h:124
bool update_register_(uint8_t reg, uint8_t mask, uint8_t value)
Definition sy6970.cpp:47
mopeka_std_values val[3]
@ I2C_WATCHDOG_160S
Definition sy6970.h:71
@ I2C_WATCHDOG_DISABLED
Definition sy6970.h:68
ESPHOME_LOG_TAG(TAG, "sy6970")
uint32_t value
Definition hal.cpp:41
int written
Definition helpers.h:1340
static void uint32_t