12 return LOG_STR(
"Disabled");
14 return LOG_STR(
"40s");
16 return LOG_STR(
"80s");
18 return LOG_STR(
"160s");
20 return LOG_STR(
"Unknown");
24static constexpr uint32_t I2C_WATCHDOG_KICK_INTERVAL_ID = 0;
26static constexpr uint32_t I2C_WATCHDOG_KICK_INTERVAL_MS = 10000;
32 ESP_LOGW(TAG,
"Failed to read registers 0x00-0x14");
41 ESP_LOGW(TAG,
"Failed to write register 0x%02X",
reg);
50 ESP_LOGW(TAG,
"Failed to read register 0x%02X for update",
reg);
53 reg_value = (reg_value & ~mask) | (value & mask);
58 ESP_LOGV(TAG,
"Setting up SY6970...");
62 if (!this->
read_byte(SY6970_REG_DEVICE_ID, ®_value)) {
63 ESP_LOGE(TAG,
"Failed to communicate with SY6970");
68 uint8_t chip_id = reg_value & 0x03;
69 if (chip_id != 0x00) {
70 ESP_LOGW(TAG,
"Unexpected chip ID: 0x%02X (expected 0x00)", chip_id);
74 ESP_LOGV(TAG,
"Setting I2C watchdog timeout to %s",
79 ESP_LOGV(TAG,
"Setting LED enabled to %s", ONOFF(this->
led_enabled_));
85 ESP_LOGV(TAG,
"Setting charge voltage to %u mV", this->
charge_voltage_);
88 ESP_LOGV(TAG,
"Setting charge current to %u mA", this->
charge_current_);
94 ESP_LOGV(TAG,
"Setting charge enabled to %s", ONOFF(this->
charge_enabled_));
97 ESP_LOGV(TAG,
"Setting ADC measurements to %s", ONOFF(this->
enable_adc_));
100 ESP_LOGV(TAG,
"SY6970 initialized successfully");
107 " Input Current Limit: %u mA\n"
108 " Charge Voltage: %u mV\n"
109 " Charge Current: %u mA\n"
110 " Precharge Current: %u mA\n"
111 " Charge Enabled: %s\n"
113 " I2C Watchdog Timeout: %s",
117 LOG_I2C_DEVICE(
this);
118 LOG_UPDATE_INTERVAL(
this);
120 ESP_LOGE(TAG,
"Communication with SY6970 failed!");
131 ESP_LOGW(TAG,
"Failed to read registers during update");
140 listener->on_data(this->
data_);
148 if (milliamps < INPUT_CURRENT_MIN) {
149 milliamps = INPUT_CURRENT_MIN;
152 uint8_t
val = (milliamps - INPUT_CURRENT_MIN) / INPUT_CURRENT_STEP;
164 if (millivolts < CHG_VOLTAGE_BASE) {
165 millivolts = CHG_VOLTAGE_BASE;
168 uint8_t
val = (millivolts - CHG_VOLTAGE_BASE) / CHG_VOLTAGE_STEP;
180 if (milliamps < PRE_CHG_BASE_MA) {
181 milliamps = PRE_CHG_BASE_MA;
184 uint8_t
val = (milliamps - PRE_CHG_BASE_MA) / PRE_CHG_STEP_MA;
196 uint8_t
val = milliamps / 64;
208 this->
update_register_(SY6970_REG_SYS_CONTROL, 0x10, enabled ? 0x10 : 0x00);
216 this->
update_register_(SY6970_REG_TIMER_CONTROL, 0x40, enabled ? 0x00 : 0x40);
224 this->
update_register_(SY6970_REG_ADC_CONTROL, 0xC0, enabled ? 0xC0 : 0x00);
239 ESP_LOGW(TAG,
"Failed to set I2C watchdog timeout to %s", LOG_STR_ARG(i2c_watchdog_timeout_to_string(timeout)));
251 this->
set_interval(I2C_WATCHDOG_KICK_INTERVAL_ID, I2C_WATCHDOG_KICK_INTERVAL_MS,
261 ESP_LOGW(TAG,
"I2C watchdog reset failed, settings may revert");
void mark_failed()
Mark this component as failed.
void status_set_warning()
bool cancel_interval(const char *name)
Cancel an interval function.
void set_interval(const char *name, uint32_t interval, std::function< void()> &&f)
Set an interval function with a const char* name.
void status_clear_warning()
bool write_byte(uint8_t a_register, uint8_t data) const
bool read_byte(uint8_t a_register, uint8_t *data)
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
void set_charge_target_voltage(uint16_t millivolts)
void set_enable_adc_measure(bool enabled=true)
void set_input_current_limit(uint16_t milliamps)
void set_charge_enabled(bool enabled)
uint16_t input_current_limit_
I2CWatchdogTimeout i2c_watchdog_timeout_
void set_precharge_current(uint16_t milliamps)
bool write_register_(uint8_t reg, uint8_t value)
uint16_t precharge_current_
void set_led_enabled(bool enabled)
bool reset_i2c_watchdog()
Kick the I2C watchdog (REG03 bit 6); runs on its own interval while enabled. Returns false when not w...
void set_charge_current(uint16_t milliamps)
void set_i2c_watchdog_timeout(I2CWatchdogTimeout timeout)
bool read_all_registers_()
std::vector< SY6970Listener * > listeners_
bool update_register_(uint8_t reg, uint8_t mask, uint8_t value)
void dump_config() override
ESPHOME_LOG_TAG(TAG, "sy6970")