ESPHome 2026.12.0-dev
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"
5
6namespace esphome::sy6970 {
7
8ESPHOME_LOG_TAG(TAG, "sy6970");
9
10// Indexed by I2CWatchdogTimeout
11PROGMEM_STRING_TABLE(I2CWatchdogTimeoutStrings, "Disabled", "40s", "80s", "160s", "Unknown");
12
13// REG14 PN[5:3]: 001 on the SY6970, 000 on the register-compatible TI BQ25896
14static constexpr uint8_t PART_NUMBER_MASK = 0x38;
15static constexpr uint8_t PART_NUMBER_SY6970 = 0x08;
16static constexpr uint8_t PART_NUMBER_BQ25896 = 0x00;
17
18static constexpr uint32_t I2C_WATCHDOG_KICK_INTERVAL_ID = 0;
19// Well under the 40 s default, so a failed kick or a timeout the chip did not accept still leaves margin
20static constexpr uint32_t I2C_WATCHDOG_KICK_INTERVAL_MS = 10000;
21
23 // Read all registers from 0x00 to 0x14 in one transaction (21 bytes)
24 if (!this->read_bytes(SY6970_REG_INPUT_CURRENT_LIMIT, this->data_.registers, 21)) {
25 ESP_LOGW(TAG, "Failed to read registers 0x00-0x14");
26 return false;
27 }
28
29 return true;
30}
31
32bool SY6970Component::write_register_(uint8_t reg, uint8_t value) {
33 if (!this->write_byte(reg, value)) {
34 ESP_LOGW(TAG, "Failed to write register 0x%02X", reg);
35 return false;
36 }
37 return true;
38}
39
40bool SY6970Component::update_register_(uint8_t reg, uint8_t mask, uint8_t value) {
41 if (this->update_register(reg, mask, value))
42 return true;
43 ESP_LOGW(TAG, "Failed to update register 0x%02X", reg);
44 return false;
45}
46
48 ESP_LOGV(TAG, "Setting up SY6970...");
49
50 // Try to read chip ID
51 uint8_t reg_value;
52 if (!this->setup_check(this->read_byte(SY6970_REG_DEVICE_ID, &reg_value)))
53 return;
54
55 const uint8_t part = reg_value & PART_NUMBER_MASK;
56 if (part != PART_NUMBER_SY6970 && part != PART_NUMBER_BQ25896) {
57 ESP_LOGW(TAG, "Unexpected part information: 0x%02X", reg_value);
58 }
59
60 // Service the watchdog first, so the settings below are not reverted while they are written
61 ESP_LOGV(TAG, "Setting I2C watchdog timeout to %s",
62 LOG_STR_ARG(I2CWatchdogTimeoutStrings::get_log_str(this->i2c_watchdog_timeout_)));
64
65 // Apply configuration options (all have defaults now)
66 ESP_LOGV(TAG, "Setting LED enabled to %s", ONOFF(this->led_enabled_));
67 this->set_led_enabled(this->led_enabled_);
68
69 ESP_LOGV(TAG, "Setting input current limit to %u mA", this->input_current_limit_);
70 this->set_input_current_limit(this->input_current_limit_);
71
72 ESP_LOGV(TAG, "Setting charge voltage to %u mV", this->charge_voltage_);
74
75 ESP_LOGV(TAG, "Setting charge current to %u mA", this->charge_current_);
77
78 ESP_LOGV(TAG, "Setting precharge current to %u mA", this->precharge_current_);
80
81 ESP_LOGV(TAG, "Setting charge enabled to %s", ONOFF(this->charge_enabled_));
83
84 ESP_LOGV(TAG, "Setting ADC measurements to %s", ONOFF(this->enable_adc_));
86
87 ESP_LOGV(TAG, "SY6970 initialized successfully");
88}
89
91 ESP_LOGCONFIG(TAG,
92 "SY6970:\n"
93 " LED Enabled: %s\n"
94 " Input Current Limit: %u mA\n"
95 " Charge Voltage: %u mV\n"
96 " Charge Current: %u mA\n"
97 " Precharge Current: %u mA\n"
98 " Charge Enabled: %s\n"
99 " ADC Enabled: %s\n"
100 " I2C Watchdog Timeout: %s",
101 ONOFF(this->led_enabled_), this->input_current_limit_, this->charge_voltage_, this->charge_current_,
102 this->precharge_current_, ONOFF(this->charge_enabled_), ONOFF(this->enable_adc_),
103 LOG_STR_ARG(I2CWatchdogTimeoutStrings::get_log_str(this->i2c_watchdog_timeout_)));
104 LOG_I2C_DEVICE(this);
105 LOG_UPDATE_INTERVAL(this);
106}
107
109 if (this->is_failed()) {
110 return;
111 }
112
113 // Read all registers in one transaction
114 if (!this->status_comm_result(this->read_all_registers_())) {
115 ESP_LOGW(TAG, "Failed to read registers during update");
116 return;
117 }
118
119#ifdef USE_SY6970_DATA_PUBLISHER
120 if (this->data_publisher_ != nullptr)
121 this->data_publisher_(this->data_);
122#endif
123}
124
125void SY6970Component::set_input_current_limit(uint16_t milliamps) {
126 if (this->is_failed())
127 return;
128
129 if (milliamps < INPUT_CURRENT_MIN) {
130 milliamps = INPUT_CURRENT_MIN;
131 }
132
133 uint8_t val = (milliamps - INPUT_CURRENT_MIN) / INPUT_CURRENT_STEP;
134 if (val > 0x3F) {
135 val = 0x3F;
136 }
137
138 this->update_register_(SY6970_REG_INPUT_CURRENT_LIMIT, 0x3F, val);
139}
140
142 if (this->is_failed())
143 return;
144
145 if (millivolts < CHG_VOLTAGE_BASE) {
146 millivolts = CHG_VOLTAGE_BASE;
147 }
148
149 uint8_t val = (millivolts - CHG_VOLTAGE_BASE) / CHG_VOLTAGE_STEP;
150 if (val > 0x3F) {
151 val = 0x3F;
152 }
153
154 this->update_register_(SY6970_REG_CHARGE_VOLTAGE, 0xFC, val << 2);
155}
156
158 if (this->is_failed())
159 return;
160
161 if (milliamps < PRE_CHG_BASE_MA) {
162 milliamps = PRE_CHG_BASE_MA;
163 }
164
165 uint8_t val = (milliamps - PRE_CHG_BASE_MA) / PRE_CHG_STEP_MA;
166 if (val > 0x0F) {
167 val = 0x0F;
168 }
169
170 this->update_register_(SY6970_REG_PRECHARGE_CURRENT, 0xF0, val << 4);
171}
172
173void SY6970Component::set_charge_current(uint16_t milliamps) {
174 if (this->is_failed())
175 return;
176
177 uint8_t val = milliamps / 64;
178 if (val > 0x7F) {
179 val = 0x7F;
180 }
181
182 this->update_register_(SY6970_REG_CHARGE_CURRENT, 0x7F, val);
183}
184
186 if (this->is_failed())
187 return;
188
189 this->update_register_(SY6970_REG_SYS_CONTROL, 0x10, enabled ? 0x10 : 0x00);
190}
191
193 if (this->is_failed())
194 return;
195
196 // Bit 6: 0 = LED enabled, 1 = LED disabled
197 this->update_register_(SY6970_REG_TIMER_CONTROL, 0x40, enabled ? 0x00 : 0x40);
198}
199
201 if (this->is_failed())
202 return;
203
204 // Set bits to enable ADC conversion
205 this->update_register_(SY6970_REG_ADC_CONTROL, 0xC0, enabled ? 0xC0 : 0x00);
206}
207
209 if (this->is_failed())
210 return;
211
212 if (timeout != I2C_WATCHDOG_DISABLED) {
213 // Changing the timeout does not restart the chip's timer, which may be close to expiring
214 this->reset_i2c_watchdog();
215 }
216
217 // REG07 bits 5:4 (WATCHDOG[1:0])
218 bool written = this->update_register_(SY6970_REG_TIMER_CONTROL, 0x30, static_cast<uint8_t>(timeout) << 4);
219 if (!written) {
220 ESP_LOGW(TAG, "Failed to set I2C watchdog timeout to %s",
221 LOG_STR_ARG(I2CWatchdogTimeoutStrings::get_log_str(timeout)));
222 }
223
224 if (timeout == I2C_WATCHDOG_DISABLED && written) {
225 this->i2c_watchdog_timeout_ = timeout;
226 this->cancel_interval(I2C_WATCHDOG_KICK_INTERVAL_ID);
227 return;
228 }
229 if (timeout != I2C_WATCHDOG_DISABLED) {
230 this->i2c_watchdog_timeout_ = timeout;
231 }
232 // Keeps kicking after a failed write too: the chip may still be on its 40 s default
233 this->set_interval(I2C_WATCHDOG_KICK_INTERVAL_ID, I2C_WATCHDOG_KICK_INTERVAL_MS,
234 [this]() { this->reset_i2c_watchdog(); });
235}
236
238 if (this->is_failed())
239 return false;
240
241 // WD_RST, self-clearing
242 if (!this->update_register_(SY6970_REG_SYS_CONTROL, 0x40, 0x40)) {
243 ESP_LOGW(TAG, "I2C watchdog reset failed, settings may revert");
244 return false;
245 }
246 return true;
247}
248
249} // namespace esphome::sy6970
bool is_failed() const
Definition component.h:288
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.
bool ESPHOME_ALWAYS_INLINE setup_check(bool ok)
Marks the component failed with message (default "Communication failed") unless ok; returns ok.
Definition component.h:236
void set_interval(const char *name, uint32_t interval, SchedulerCallback &&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:111
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:141
void set_enable_adc_measure(bool enabled=true)
Definition sy6970.cpp:200
void set_charge_enabled(bool enabled)
Definition sy6970.cpp:185
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
bool update_register_(uint8_t reg, uint8_t mask, uint8_t value)
Definition sy6970.cpp:40
mopeka_std_values val[3]
@ I2C_WATCHDOG_DISABLED
Definition sy6970.h:71
ESPHOME_LOG_TAG(TAG, "sy6970")
PROGMEM_STRING_TABLE(I2CWatchdogTimeoutStrings, "Disabled", "40s", "80s", "160s", "Unknown")
uint32_t value
Definition hal.cpp:59
int written
Definition helpers.h:1537
static void uint32_t