14static constexpr uint8_t PART_NUMBER_MASK = 0x38;
15static constexpr uint8_t PART_NUMBER_SY6970 = 0x08;
16static constexpr uint8_t PART_NUMBER_BQ25896 = 0x00;
18static constexpr uint32_t I2C_WATCHDOG_KICK_INTERVAL_ID = 0;
20static constexpr uint32_t I2C_WATCHDOG_KICK_INTERVAL_MS = 10000;
25 ESP_LOGW(TAG,
"Failed to read registers 0x00-0x14");
34 ESP_LOGW(TAG,
"Failed to write register 0x%02X",
reg);
43 ESP_LOGW(TAG,
"Failed to update register 0x%02X",
reg);
48 ESP_LOGV(TAG,
"Setting up SY6970...");
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);
61 ESP_LOGV(TAG,
"Setting I2C watchdog timeout to %s",
66 ESP_LOGV(TAG,
"Setting LED enabled to %s", ONOFF(this->
led_enabled_));
72 ESP_LOGV(TAG,
"Setting charge voltage to %u mV", this->
charge_voltage_);
75 ESP_LOGV(TAG,
"Setting charge current to %u mA", this->
charge_current_);
81 ESP_LOGV(TAG,
"Setting charge enabled to %s", ONOFF(this->
charge_enabled_));
84 ESP_LOGV(TAG,
"Setting ADC measurements to %s", ONOFF(this->
enable_adc_));
87 ESP_LOGV(TAG,
"SY6970 initialized successfully");
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"
100 " I2C Watchdog Timeout: %s",
104 LOG_I2C_DEVICE(
this);
105 LOG_UPDATE_INTERVAL(
this);
115 ESP_LOGW(TAG,
"Failed to read registers during update");
119#ifdef USE_SY6970_DATA_PUBLISHER
120 if (this->data_publisher_ !=
nullptr)
121 this->data_publisher_(this->
data_);
125void SY6970Component::set_input_current_limit(uint16_t milliamps) {
129 if (milliamps < INPUT_CURRENT_MIN) {
130 milliamps = INPUT_CURRENT_MIN;
133 uint8_t
val = (milliamps - INPUT_CURRENT_MIN) / INPUT_CURRENT_STEP;
145 if (millivolts < CHG_VOLTAGE_BASE) {
146 millivolts = CHG_VOLTAGE_BASE;
149 uint8_t
val = (millivolts - CHG_VOLTAGE_BASE) / CHG_VOLTAGE_STEP;
161 if (milliamps < PRE_CHG_BASE_MA) {
162 milliamps = PRE_CHG_BASE_MA;
165 uint8_t
val = (milliamps - PRE_CHG_BASE_MA) / PRE_CHG_STEP_MA;
177 uint8_t
val = milliamps / 64;
189 this->
update_register_(SY6970_REG_SYS_CONTROL, 0x10, enabled ? 0x10 : 0x00);
197 this->
update_register_(SY6970_REG_TIMER_CONTROL, 0x40, enabled ? 0x00 : 0x40);
205 this->
update_register_(SY6970_REG_ADC_CONTROL, 0xC0, enabled ? 0xC0 : 0x00);
220 ESP_LOGW(TAG,
"Failed to set I2C watchdog timeout to %s",
221 LOG_STR_ARG(I2CWatchdogTimeoutStrings::get_log_str(timeout)));
233 this->
set_interval(I2C_WATCHDOG_KICK_INTERVAL_ID, I2C_WATCHDOG_KICK_INTERVAL_MS,
243 ESP_LOGW(TAG,
"I2C watchdog reset failed, settings may revert");
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.
bool ESPHOME_ALWAYS_INLINE setup_check(bool ok)
Marks the component failed with message (default "Communication failed") unless ok; returns ok.
void set_interval(const char *name, uint32_t interval, SchedulerCallback &&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_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_()
bool update_register_(uint8_t reg, uint8_t mask, uint8_t value)
void dump_config() override
ESPHOME_LOG_TAG(TAG, "sy6970")
PROGMEM_STRING_TABLE(I2CWatchdogTimeoutStrings, "Disabled", "40s", "80s", "160s", "Unknown")