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 update register 0x%02X",
reg);
55 ESP_LOGV(TAG,
"Setting up SY6970...");
59 if (!this->
read_byte(SY6970_REG_DEVICE_ID, ®_value)) {
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);
70 ESP_LOGV(TAG,
"Setting I2C watchdog timeout to %s",
75 ESP_LOGV(TAG,
"Setting LED enabled to %s", ONOFF(this->
led_enabled_));
81 ESP_LOGV(TAG,
"Setting charge voltage to %u mV", this->
charge_voltage_);
84 ESP_LOGV(TAG,
"Setting charge current to %u mA", this->
charge_current_);
90 ESP_LOGV(TAG,
"Setting charge enabled to %s", ONOFF(this->
charge_enabled_));
93 ESP_LOGV(TAG,
"Setting ADC measurements to %s", ONOFF(this->
enable_adc_));
96 ESP_LOGV(TAG,
"SY6970 initialized successfully");
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"
109 " I2C Watchdog Timeout: %s",
113 LOG_I2C_DEVICE(
this);
114 LOG_UPDATE_INTERVAL(
this);
124 ESP_LOGW(TAG,
"Failed to read registers during update");
130 listener->on_data(this->
data_);
138 if (milliamps < INPUT_CURRENT_MIN) {
139 milliamps = INPUT_CURRENT_MIN;
142 uint8_t
val = (milliamps - INPUT_CURRENT_MIN) / INPUT_CURRENT_STEP;
154 if (millivolts < CHG_VOLTAGE_BASE) {
155 millivolts = CHG_VOLTAGE_BASE;
158 uint8_t
val = (millivolts - CHG_VOLTAGE_BASE) / CHG_VOLTAGE_STEP;
170 if (milliamps < PRE_CHG_BASE_MA) {
171 milliamps = PRE_CHG_BASE_MA;
174 uint8_t
val = (milliamps - PRE_CHG_BASE_MA) / PRE_CHG_STEP_MA;
186 uint8_t
val = milliamps / 64;
198 this->
update_register_(SY6970_REG_SYS_CONTROL, 0x10, enabled ? 0x10 : 0x00);
206 this->
update_register_(SY6970_REG_TIMER_CONTROL, 0x40, enabled ? 0x00 : 0x40);
214 this->
update_register_(SY6970_REG_ADC_CONTROL, 0xC0, enabled ? 0xC0 : 0x00);
229 ESP_LOGW(TAG,
"Failed to set I2C watchdog timeout to %s", LOG_STR_ARG(i2c_watchdog_timeout_to_string(timeout)));
241 this->
set_interval(I2C_WATCHDOG_KICK_INTERVAL_ID, I2C_WATCHDOG_KICK_INTERVAL_MS,
251 ESP_LOGW(TAG,
"I2C watchdog reset failed, settings may revert");
void mark_failed()
Mark this component as failed.
bool cancel_interval(const char *name)
Cancel an interval function.
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.
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 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.
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")