ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
npm1300.cpp
Go to the documentation of this file.
1#include "npm1300.h"
2#include <algorithm>
3#include <cinttypes>
4#include <cmath>
5#include "esphome/core/hal.h"
6#include "esphome/core/log.h"
8
9namespace esphome::npm1300 {
10
11ESPHOME_LOG_TAG(TAG, "npm1300");
12
13// Registers: the base address of the peripheral in the high byte, the offset in the low byte
14static constexpr uint16_t REG_VBUSIN_TASK_UPDATE_ILIM = 0x0200;
15static constexpr uint16_t REG_VBUSIN_ILIM0 = 0x0201;
16static constexpr uint16_t REG_VBUSIN_ILIM_STARTUP = 0x0202;
17static constexpr uint16_t REG_VBUSIN_STATUS = 0x0207;
18static constexpr uint16_t REG_BCHG_TASK_RELEASE_ERR = 0x0300;
19static constexpr uint16_t REG_BCHG_TASK_CLEAR_CHG_ERR = 0x0301;
20static constexpr uint16_t REG_BCHG_ENABLE_SET = 0x0304;
21static constexpr uint16_t REG_BCHG_ENABLE_CLR = 0x0305;
22static constexpr uint16_t REG_BCHG_DISABLE_SET = 0x0306;
23static constexpr uint16_t REG_BCHG_DISABLE_CLR = 0x0307;
24static constexpr uint16_t REG_BCHG_ISET_MSB = 0x0308;
25static constexpr uint16_t REG_BCHG_ISET_DISCHARGE_MSB = 0x030A;
26static constexpr uint16_t REG_BCHG_VTERM = 0x030C;
27static constexpr uint16_t REG_BCHG_VTERM_R = 0x030D;
28static constexpr uint16_t REG_BCHG_VTRICKLE_SEL = 0x030E;
29static constexpr uint16_t REG_BCHG_ITERM_SEL = 0x030F;
30static constexpr uint16_t REG_BCHG_NTC_STATUS = 0x0332;
31static constexpr uint16_t REG_BCHG_CHARGE_STATUS = 0x0334;
32static constexpr uint16_t REG_BCHG_ERR_REASON = 0x0336;
33static constexpr uint16_t REG_BUCK1_ENA_SET = 0x0400;
34static constexpr uint16_t REG_BUCK1_NORM_VOUT = 0x0408;
35static constexpr uint16_t REG_BUCK_SW_CTRL_SEL = 0x040F;
36static constexpr uint16_t REG_BUCK_STATUS = 0x0434;
37static constexpr uint16_t REG_ADC_TASK_VBAT = 0x0500;
38static constexpr uint16_t REG_ADC_TASK_VBUS = 0x0507;
39static constexpr uint16_t REG_ADC_NTC_R_SEL = 0x050A;
40static constexpr uint16_t REG_ADC_TASK_AUTO_TIM_UPDATE = 0x050C;
41static constexpr uint16_t REG_ADC_RESULTS = 0x0510;
42static constexpr uint16_t REG_ADC_IBAT_MEAS_EN = 0x0524;
43static constexpr uint16_t REG_TIMER_TARGET_STROBE = 0x0703;
44static constexpr uint16_t REG_TIMER_CONFIG = 0x0705;
45static constexpr uint16_t REG_TIMER_HI_BYTE = 0x0708;
46static constexpr uint16_t REG_LDSW1_TASK_SET = 0x0800;
47static constexpr uint16_t REG_LDSW_STATUS = 0x0804;
48static constexpr uint16_t REG_LDSW1_LDO_SEL = 0x0808;
49static constexpr uint16_t REG_LDSW1_VOUT_SEL = 0x080C;
50static constexpr uint16_t REG_SHIP_TASK_HIBERNATE = 0x0B00;
51static constexpr uint16_t REG_SHIP_TASK_SHIP_MODE = 0x0B02;
52
53// VBUSINSTATUS
54static constexpr uint8_t VBUS_PRESENT = 0x01;
55// NTCSTATUS
56static constexpr uint8_t NTC_STATUS_COLD = 0x01;
57static constexpr uint8_t NTC_STATUS_HOT = 0x08;
58// BCHGCHARGESTATUS; bit 0 (battery detected) is reserved since C00 silicon
59static constexpr uint8_t CHARGE_STATUS_COMPLETED = 0x02;
60static constexpr uint8_t CHARGE_STATUS_TRICKLE = 0x04;
61static constexpr uint8_t CHARGE_STATUS_CONSTANT_CURRENT = 0x08;
62static constexpr uint8_t CHARGE_STATUS_CONSTANT_VOLTAGE = 0x10;
63static constexpr uint8_t CHARGE_STATUS_DIE_TEMP_HIGH = 0x40;
64// BCHGDISABLESET and BCHGDISABLECLR
65static constexpr uint8_t CHARGER_DISABLE_RECHARGE = 0x01;
66static constexpr uint8_t CHARGER_DISABLE_NTC = 0x02;
67// BUCKSTATUS power good, LDSWSTATUS powered up as load switch or as LDO
68static constexpr uint8_t BUCK_POWER_GOOD[2] = {0x04, 0x40};
69static constexpr uint8_t LDSW_POWERED[2] = {0x03, 0x0C};
70// TIMERCONFIG: wake-up timer with the 16 ms prescaler
71static constexpr uint8_t TIMER_CONFIG_WAKEUP = 0x04;
72static constexpr uint32_t TIMER_TICK_MS = 16;
73static constexpr uint32_t TIMER_MAX_TICKS = 0xFFFFFF;
74
75// BCHGISETDISCHARGE codes up to this one select the 200 mA discharge limit, the others 1000 mA
76static constexpr uint16_t DISCHARGE_LIMIT_200MA_CODE = 84;
77
78static uint16_t discharge_limit_ma(uint16_t code) { return code <= DISCHARGE_LIMIT_200MA_CODE ? 200 : 1000; }
79
80// BCHGISET and BCHGISETDISCHARGE hold a 9-bit code: the high 8 bits, then bit 0 in the next register
81static void split_9bit(uint16_t code, uint8_t *out) {
82 out[0] = code >> 1;
83 out[1] = code & 1;
84}
85
86#ifdef USE_NPM1300_DATA_PUBLISHER
87static uint16_t join_9bit(const uint8_t *in) { return (in[0] << 1) | (in[1] & 1); }
88
89// ADCIBATMEASSTATUS
90static constexpr uint8_t IBAT_MEAS_INVALID = 0x10;
91static constexpr uint8_t IBAT_MODE_SHIFT = 2;
92static constexpr uint8_t IBAT_MODE_DISCHARGE = 0x01;
93static constexpr uint8_t IBAT_MODE_CHARGE = 0x03;
94// Full scale of the battery current measurement per milliampere of the charge and discharge limits
95static constexpr float CHARGE_FULL_SCALE_FACTOR = 1.25f;
96static constexpr float DISCHARGE_FULL_SCALE_FACTOR = 1.12f;
97static constexpr float ADC_MAX_CODE = 1023.0f;
98// Five conversions of 250 us each; the loop runs the timeout later than this anyway
99static constexpr uint32_t CONVERSION_TIME_MS = 2;
100
101static constexpr uint32_t READ_TIMEOUT_ID = 0;
102
103float ntc_temperature(uint16_t code, float inv_beta) {
104 if (code == 0)
105 return NAN;
106 // The NTC divider gives code = 1024 * R / (R + R_nominal)
107 return 1.0f / (1.0f / 298.15f - logf(1024.0f / code - 1.0f) * inv_beta) - 273.15f;
108}
109#endif
110
111#ifdef USE_TEXT_SENSOR
112PROGMEM_STRING_TABLE(ChargerStatusStrings, "Discharging", "Error", "Disabled", "Paused, PMIC Too Hot",
113 "Charge Complete", "Trickle Charge", "Constant Current", "Constant Voltage",
114 "Paused, Battery Too Cold", "Paused, Battery Too Hot", "Not Charging");
115static constexpr uint8_t CHARGER_STATUS_FIRST_BIT = 3;
116// BCHGCHARGESTATUS bits in order of priority, reported from string CHARGER_STATUS_FIRST_BIT on
117static const uint8_t CHARGER_STATUS_BITS[] PROGMEM = {CHARGE_STATUS_DIE_TEMP_HIGH, CHARGE_STATUS_COMPLETED,
118 CHARGE_STATUS_TRICKLE, CHARGE_STATUS_CONSTANT_CURRENT,
119 CHARGE_STATUS_CONSTANT_VOLTAGE};
120
121static uint8_t charger_status_index(const NPM1300Status &status) {
122 if ((status.vbus & VBUS_PRESENT) == 0)
123 return 0;
124 if (status.error_reason != 0)
125 return 1;
126 if ((status.charger_enable & 1) == 0)
127 return 2;
128 for (size_t i = 0; i < sizeof(CHARGER_STATUS_BITS); i++) {
129 if ((status.charge & progmem_read_byte(&CHARGER_STATUS_BITS[i])) != 0)
130 return CHARGER_STATUS_FIRST_BIT + i;
131 }
132 // The charger stops without an error outside the cold and hot limits of the battery temperature
133 if ((status.ntc & NTC_STATUS_COLD) != 0)
134 return 8;
135 if ((status.ntc & NTC_STATUS_HOT) != 0)
136 return 9;
137 return 10;
138}
139
140void publish_charger_status(text_sensor::TextSensor *sensor, uint8_t &last, const NPM1300Status &status) {
141 const uint8_t index = charger_status_index(status);
142 if (index == last)
143 return;
144 last = index;
145 sensor->publish_state(ChargerStatusStrings::get_progmem_str(index));
146}
147#endif
148
150 NPM1300Config config;
151 progmem_memcpy(&config, this->config_, sizeof(config));
152 return config;
153}
154
155void NPM1300Component::setup() {
156 uint8_t status;
157 if (!this->setup_check(this->read_byte_reg16(REG_BCHG_CHARGE_STATUS, &status)))
158 return;
159
160 // Raise the VBUS current limit before anything turns on a regulator: at the power-on limit of 100 mA the
161 // inrush current of a regulator can trip the over-current protection and brown out the board
162 if (this->vbus_current_limit_ != NPM1300_UNSET &&
163 !this->setup_check(this->write_byte_reg16(REG_VBUSIN_ILIM_STARTUP, this->vbus_current_limit_) &&
165 return;
166
167 const NPM1300Config config = this->load_config_();
168 if (config.thermistor != NPM1300_UNSET &&
169 !this->setup_check(this->write_byte_reg16(REG_ADC_NTC_R_SEL, config.thermistor)))
170 return;
171
172 if (config.charger_configured && !this->setup_check(this->configure_charger_(config)))
173 return;
174
175 for (uint8_t i = 0; i < NPM1300_OUTPUT_COUNT; i++) {
176 if (!this->setup_check(this->configure_output_(i, config.outputs[i])))
177 return;
178 }
179
180#ifdef USE_NPM1300_DATA_PUBLISHER
181 if (this->data_publisher_ != nullptr) {
182 // The battery current is measured after every battery voltage measurement
183 if (!this->setup_check(this->write_byte_reg16(REG_ADC_IBAT_MEAS_EN, 1)))
184 return;
185 if (config.charger_configured) {
186 this->charge_code_ = config.charger.current;
187 this->discharge_limit_high_ = discharge_limit_ma(config.charger.discharge_limit) == 1000;
188 } else if (!this->setup_check(this->read_charge_currents_())) {
189 return;
190 }
191 }
192#endif
193
194 this->update();
195}
196
198 return this->write_byte_reg16(REG_VBUSIN_ILIM0, this->vbus_current_limit_) &&
199 this->write_byte_reg16(REG_VBUSIN_TASK_UPDATE_ILIM, 1);
200}
201
203 const NPM1300ChargerConfig &charger = config.charger;
204 // The charge settings are only taken while the charger is off
205 if (!this->write_byte_reg16(REG_BCHG_ENABLE_CLR, 1))
206 return false;
207 uint8_t current[2];
208 uint8_t discharge[2];
209 split_9bit(charger.current, current);
210 split_9bit(charger.discharge_limit, discharge);
211 uint8_t disable = charger.recharge ? 0 : CHARGER_DISABLE_RECHARGE;
212 // Without a thermistor the charger reports an NTC error and does not charge
213 if (config.thermistor == 0 || config.thermistor == NPM1300_UNSET)
214 disable |= CHARGER_DISABLE_NTC;
215 const uint8_t enable = (CHARGER_DISABLE_RECHARGE | CHARGER_DISABLE_NTC) & ~disable;
216 if (this->write_register16(REG_BCHG_ISET_MSB, current, sizeof(current)) != i2c::ERROR_OK ||
217 this->write_register16(REG_BCHG_ISET_DISCHARGE_MSB, discharge, sizeof(discharge)) != i2c::ERROR_OK ||
218 !this->write_byte_reg16(REG_BCHG_VTERM, charger.termination_voltage) ||
219 !this->write_byte_reg16(REG_BCHG_VTERM_R, charger.warm_termination_voltage) ||
220 !this->write_byte_reg16(REG_BCHG_VTRICKLE_SEL, charger.trickle_voltage) ||
221 !this->write_byte_reg16(REG_BCHG_ITERM_SEL, charger.termination_current) ||
222 !this->write_byte_reg16(REG_BCHG_DISABLE_SET, disable) || !this->write_byte_reg16(REG_BCHG_DISABLE_CLR, enable) ||
223 // Measure the battery temperature automatically while charging
224 !this->write_byte_reg16(REG_ADC_TASK_AUTO_TIM_UPDATE, 1))
225 return false;
226 return !charger.enabled || this->set_charger_enabled(true);
227}
228
230 const uint8_t channel = index & 1;
231 if (index < 2) {
232 // The software voltage replaces the one set by the VSET pin only once its SWCTRLSEL bit is set
233 if (config.voltage != NPM1300_UNSET &&
234 (!this->write_byte_reg16(REG_BUCK1_NORM_VOUT + channel * 2, config.voltage) ||
235 !this->update_register16(REG_BUCK_SW_CTRL_SEL, 1 << channel, 1 << channel)))
236 return false;
237 } else if ((config.voltage != NPM1300_UNSET &&
238 !this->write_byte_reg16(REG_LDSW1_VOUT_SEL + channel, config.voltage)) ||
239 (config.ldo_mode != NPM1300_UNSET &&
240 !this->write_byte_reg16(REG_LDSW1_LDO_SEL + channel, config.ldo_mode))) {
241 return false;
242 }
243 return config.enabled == NPM1300_UNSET ||
244 this->set_output_enabled(static_cast<NPM1300Output>(index), config.enabled != 0);
245}
246
247#ifdef USE_NPM1300_DATA_PUBLISHER
249 uint8_t charge[2];
250 uint8_t discharge[2];
251 if (this->read_register16(REG_BCHG_ISET_MSB, charge, sizeof(charge)) != i2c::ERROR_OK ||
252 this->read_register16(REG_BCHG_ISET_DISCHARGE_MSB, discharge, sizeof(discharge)) != i2c::ERROR_OK)
253 return false;
254 this->charge_code_ = join_9bit(charge);
255 this->discharge_limit_high_ = discharge_limit_ma(join_9bit(discharge)) == 1000;
256 return true;
257}
258#endif
259
260bool NPM1300Component::set_charger_enabled(bool enabled) {
261 if (this->is_failed())
262 return false;
263 if (!enabled)
264 return this->write_byte_reg16(REG_BCHG_ENABLE_CLR, 1);
265 // A charger error stops the charger until the error is cleared and the charger released
266 return this->write_byte_reg16(REG_BCHG_TASK_CLEAR_CHG_ERR, 1) &&
267 this->write_byte_reg16(REG_BCHG_TASK_RELEASE_ERR, 1) && this->write_byte_reg16(REG_BCHG_ENABLE_SET, 1);
268}
269
270bool NPM1300Component::set_output_enabled(NPM1300Output output, bool enabled) {
271 if (this->is_failed())
272 return false;
273 const uint8_t index = static_cast<uint8_t>(output);
274 // A pair of SET and CLR task registers per output
275 const uint16_t base = index < 2 ? REG_BUCK1_ENA_SET : REG_LDSW1_TASK_SET;
276 const uint16_t reg = base + (index & 1) * 2 + (enabled ? 0 : 1);
277 if (!this->write_byte_reg16(reg, 1)) {
278 ESP_LOGW(TAG, "Turning output %u %s failed", index, enabled ? LOG_STR_LITERAL("on") : LOG_STR_LITERAL("off"));
279 return false;
280 }
281 return true;
282}
283
284void NPM1300Component::enter_ship_mode() {
285 ESP_LOGI(TAG, "Entering ship mode");
286 this->write_byte_reg16(REG_SHIP_TASK_SHIP_MODE, 1);
287}
288
289void NPM1300Component::hibernate(uint32_t duration_ms) {
290 // A zero period would never wake the chip
291 const uint32_t ticks = std::clamp<uint32_t>(duration_ms / TIMER_TICK_MS, 1, TIMER_MAX_TICKS);
292 uint8_t timer[3];
293 put_be24(timer, ticks);
294 ESP_LOGI(TAG, "Hibernating for %" PRIu32 " ms", ticks * TIMER_TICK_MS);
295 if (!this->write_byte_reg16(REG_TIMER_CONFIG, TIMER_CONFIG_WAKEUP) ||
296 this->write_register16(REG_TIMER_HI_BYTE, timer, sizeof(timer)) != i2c::ERROR_OK ||
297 !this->write_byte_reg16(REG_TIMER_TARGET_STROBE, 1)) {
298 ESP_LOGW(TAG, "Setting the wake-up timer failed");
299 return;
300 }
301 // The timer needs a moment to load the new period
302 delay(1);
303 this->write_byte_reg16(REG_SHIP_TASK_HIBERNATE, 1);
304}
305
306void NPM1300Component::update() {
307 NPM1300Status status;
308 // NTCSTATUS, DIETEMPSTATUS and BCHGCHARGESTATUS
309 uint8_t charger_status[3];
310 if (!this->status_comm_check(this->read_byte_reg16(REG_VBUSIN_STATUS, &status.vbus) &&
311 this->read_byte_reg16(REG_BCHG_ENABLE_SET, &status.charger_enable) &&
312 this->read_register16(REG_BCHG_NTC_STATUS, charger_status, sizeof(charger_status)) ==
314 this->read_byte_reg16(REG_BCHG_ERR_REASON, &status.error_reason)))
315 return;
316 status.ntc = charger_status[0];
317 status.charge = charger_status[2];
318 // Without a thermistor the NTC pin is grounded and can read as a hot battery
319 if (progmem_read_byte(&this->config_->thermistor) == 0)
320 status.ntc = 0;
321
322 // The chip goes back to its default VBUS current limit whenever VBUS is removed
323 const bool vbus_present = (status.vbus & VBUS_PRESENT) != 0;
324 if (vbus_present && !this->vbus_present_ && this->vbus_current_limit_ != NPM1300_UNSET &&
326 return;
327 this->vbus_present_ = vbus_present;
328
329 if (status.error_reason != this->last_error_reason_) {
330 this->last_error_reason_ = status.error_reason;
331 if (status.error_reason != 0) {
332 ESP_LOGW(TAG, "Charger error, BCHGERRREASON 0x%02X", status.error_reason);
333 }
334 }
335#ifdef USE_NPM1300_STATUS_PUBLISHER
336 if (this->status_publisher_ != nullptr)
337 this->status_publisher_(status);
338#endif
339#ifdef USE_SWITCH
340 if (!this->status_comm_check(this->publish_switches_(status)))
341 return;
342#endif
343
344#ifdef USE_SENSOR
345 if (this->vbus_current_limit_sensor_ != nullptr) {
346 uint8_t code;
347 if (!this->status_comm_check(this->read_byte_reg16(REG_VBUSIN_ILIM0, &code)))
348 return;
349 // 100 mA per step of the code, but code 0 is 500 mA
350 const uint16_t milliamps = code == 0 ? 500 : code * 100;
351 this->vbus_current_limit_sensor_->publish_state(milliamps / 1000.0f);
352 }
353#endif
354#ifdef USE_NPM1300_DATA_PUBLISHER
355 if (this->data_publisher_ != nullptr) {
356 // Start the battery voltage (followed by the battery current), NTC, die temperature and system voltage
357 // conversions, then the VBUS one
358 static constexpr uint8_t START[4] = {1, 1, 1, 1};
359 if (!this->status_comm_check(this->write_register16(REG_ADC_TASK_VBAT, START, sizeof(START)) == i2c::ERROR_OK &&
360 this->write_byte_reg16(REG_ADC_TASK_VBUS, 1)))
361 return;
362 this->set_timeout(READ_TIMEOUT_ID, CONVERSION_TIME_MS, [this]() { this->read_measurements_(); });
363 return;
364 }
365#endif
366 this->status_comm_result(true);
367}
368
369#ifdef USE_SWITCH
371 PUBLISH_STATE(this->charger_switch_, (status.charger_enable & 1) != 0);
372 uint8_t value;
373 bool ok = true;
374 switch_::Switch *const bucks[2] = {this->buck1_switch_, this->buck2_switch_};
375 if (bucks[0] != nullptr || bucks[1] != nullptr) {
376 if (this->read_byte_reg16(REG_BUCK_STATUS, &value)) {
377 for (uint8_t i = 0; i < 2; i++)
378 PUBLISH_STATE(bucks[i], (value & BUCK_POWER_GOOD[i]) != 0);
379 } else {
380 ok = false;
381 }
382 }
383 switch_::Switch *const ldos[2] = {this->ldo1_switch_, this->ldo2_switch_};
384 if (ldos[0] != nullptr || ldos[1] != nullptr) {
385 if (this->read_byte_reg16(REG_LDSW_STATUS, &value)) {
386 for (uint8_t i = 0; i < 2; i++)
387 PUBLISH_STATE(ldos[i], (value & LDSW_POWERED[i]) != 0);
388 } else {
389 ok = false;
390 }
391 }
392 return ok;
393}
394#endif
395
396#ifdef USE_NPM1300_DATA_PUBLISHER
398 // ADCIBATMEASSTATUS, then the VBAT, NTC, TEMP and VSYS results, their low bits, VBAT0 to VBAT3 (VBAT2 holds the
399 // battery current and VBAT3 the VBUS voltage) and their low bits
400 uint8_t adc[11];
401 if (!this->status_comm_result(this->read_register16(REG_ADC_RESULTS, adc, sizeof(adc)) == i2c::ERROR_OK))
402 return;
403 // Amperes per code of the battery current, positive while charging and negative while discharging
404 float ibat_scale = NAN;
405 // Any other mode means the system runs from VBUS and the battery current is not measured
406 const uint8_t mode = (adc[0] >> IBAT_MODE_SHIFT) & 0x03;
407 if ((adc[0] & IBAT_MEAS_INVALID) == 0) {
408 if (mode == IBAT_MODE_CHARGE) {
409 ibat_scale = this->charge_code_ * (2 * CHARGE_FULL_SCALE_FACTOR) / (ADC_MAX_CODE * 1000.0f);
410 } else if (mode == IBAT_MODE_DISCHARGE) {
411 ibat_scale = -(this->discharge_limit_high_ ? 1000 : 200) * DISCHARGE_FULL_SCALE_FACTOR / (ADC_MAX_CODE * 1000.0f);
412 }
413 }
414 if (this->data_publisher_ != nullptr)
415 this->data_publisher_(adc, ibat_scale);
416}
417#endif
418
419// BCHGVTERM: 3.50 V to 3.65 V, then 4.00 V to 4.45 V, both in 50 mV steps
420static uint16_t termination_voltage_mv(uint8_t code) { return code < 4 ? 3500 + 50 * code : 4000 + 50 * (code - 4); }
421
422// Indexed by NPM1300Output
423PROGMEM_STRING_TABLE(OutputNameStrings, "BUCK1", "BUCK2", "LDO1", "LDO2");
424
425void NPM1300Component::dump_config() {
426 ESP_LOGCONFIG(TAG, "nPM1300:");
427 LOG_I2C_DEVICE(this);
428 LOG_UPDATE_INTERVAL(this);
429 if (this->vbus_current_limit_ != NPM1300_UNSET) {
430 // 100 mA per step of the code
431 ESP_LOGCONFIG(TAG, " VBUS current limit: %u mA", this->vbus_current_limit_ * 100u);
432 }
433 const NPM1300Config config = this->load_config_();
434 if (config.charger_configured) {
435 const NPM1300ChargerConfig &charger = config.charger;
436 ESP_LOGCONFIG(TAG,
437 " Charger:\n"
438 " Current: %u mA\n"
439 " Discharge limit: %u mA\n"
440 " Termination voltage: %u mV\n"
441 " Warm termination voltage: %u mV\n"
442 " Termination current: %u%%\n"
443 " Trickle voltage: %u mV\n"
444 " Enabled: %s\n"
445 " Recharge: %s",
446 charger.current * 2u, discharge_limit_ma(charger.discharge_limit),
447 termination_voltage_mv(charger.termination_voltage),
448 termination_voltage_mv(charger.warm_termination_voltage), charger.termination_current ? 20u : 10u,
449 charger.trickle_voltage ? 2500u : 2900u, YESNO(charger.enabled), YESNO(charger.recharge));
450 }
451 for (uint8_t i = 0; i < NPM1300_OUTPUT_COUNT; i++) {
452 const NPM1300OutputConfig &output = config.outputs[i];
453 if (output.voltage == NPM1300_UNSET && output.ldo_mode == NPM1300_UNSET && output.enabled == NPM1300_UNSET)
454 continue;
455 ESP_LOGCONFIG(TAG, " %s:", LOG_STR_ARG(OutputNameStrings::get_log_str(i)));
456 if (output.voltage != NPM1300_UNSET) {
457 ESP_LOGCONFIG(TAG, " Voltage: %u mV", 1000u + output.voltage * 100u);
458 }
459 if (output.ldo_mode != NPM1300_UNSET) {
460 ESP_LOGCONFIG(TAG, " Mode: %s",
461 output.ldo_mode != 0 ? LOG_STR_LITERAL("LDO") : LOG_STR_LITERAL("load switch"));
462 }
463 if (output.enabled != NPM1300_UNSET) {
464 ESP_LOGCONFIG(TAG, " Enabled at boot: %s", YESNO(output.enabled != 0));
465 }
466 }
467 if (config.thermistor == 0) {
468 ESP_LOGCONFIG(TAG, " Thermistor: none");
469 } else if (config.thermistor != NPM1300_UNSET) {
470 ESP_LOGCONFIG(TAG, " Thermistor B constant: %u", config.thermistor_beta);
471 }
472#ifdef USE_SENSOR
473 LOG_SENSOR(" ", "VBUS Current Limit", this->vbus_current_limit_sensor_);
474#endif
475#ifdef USE_SWITCH
476 LOG_SWITCH(" ", "Charger", this->charger_switch_);
477 LOG_SWITCH(" ", "BUCK1", this->buck1_switch_);
478 LOG_SWITCH(" ", "BUCK2", this->buck2_switch_);
479 LOG_SWITCH(" ", "LDO1", this->ldo1_switch_);
480 LOG_SWITCH(" ", "LDO2", this->ldo2_switch_);
481#endif
482#if defined(USE_NPM1300_STATUS_PUBLISHER) || defined(USE_NPM1300_DATA_PUBLISHER)
483 if (this->dump_publisher_ != nullptr)
484 this->dump_publisher_(TAG);
485#endif
486}
487
488} // namespace esphome::npm1300
BedjetMode mode
BedJet operating mode.
uint8_t status
Definition bl0942.h:8
bool is_failed() const
Definition component.h:288
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
bool status_comm_check(bool ok)
Sets the warning ("Communication failed") when not ok but never clears it; returns ok.
Definition component.h:309
void set_timeout(const char *name, uint32_t timeout, SchedulerCallback &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
ErrorCode write_register16(uint16_t a_register, const uint8_t *data, size_t len) const
write an array of bytes to a specific register in the I²C device
Definition i2c.cpp:83
bool write_byte_reg16(uint16_t a_register, uint8_t data) const
Write one byte to a register with a 16-bit register address.
Definition i2c.h:330
bool read_byte_reg16(uint16_t a_register, uint8_t *data)
Read one byte from a register with a 16-bit register address.
Definition i2c.h:325
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:164
ErrorCode read_register16(uint16_t a_register, uint8_t *data, size_t len)
reads an array of bytes from a specific register in the I²C device
Definition i2c.cpp:62
bool publish_switches_(const NPM1300Status &status)
Returns false when a status read failed.
Definition npm1300.cpp:370
bool configure_output_(uint8_t index, const NPM1300OutputConfig &config)
Definition npm1300.cpp:229
const NPM1300Config * config_
Definition npm1300.h:142
bool configure_charger_(const NPM1300Config &config)
Definition npm1300.cpp:202
NPM1300Config load_config_() const
Definition npm1300.cpp:149
Base class for all switches.
Definition switch.h:43
void publish_state(const std::string &state)
Definition text_sensor.h:42
std::vector< uint8_t > & out
Definition gif.cpp:49
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:13
NPM1300Output
The regulators, in the order of their enable task registers.
Definition npm1300.h:22
ESPHOME_LOG_TAG(TAG, "npm1300")
void publish_charger_status(text_sensor::TextSensor *sensor, uint8_t &last, const NPM1300Status &status)
Publish the charger status text, only when it differs from last
Definition npm1300.cpp:140
PROGMEM_STRING_TABLE(ChargerStatusStrings, "Discharging", "Error", "Disabled", "Paused, PMIC Too Hot", "Charge Complete", "Trickle Charge", "Constant Current", "Constant Voltage", "Paused, Battery Too Cold", "Paused, Battery Too Hot", "Not Charging")
float ntc_temperature(uint16_t code, float inv_beta)
Battery temperature in degrees Celsius from the NTC code and 1 / B constant of the thermistor.
Definition npm1300.cpp:103
void progmem_memcpy(void *dst, const void *src, size_t len)
Definition hal.h:56
uint32_t value
Definition hal.cpp:59
constexpr uint8_t * put_be24(uint8_t *dst, uint32_t value)
Definition helpers.h:1301
void HOT delay(uint32_t ms)
Definition hal.cpp:78
uint8_t progmem_read_byte(const uint8_t *addr)
Definition hal.h:51
static void uint32_t
Register codes for the battery charger.
Definition npm1300.h:41
The settings applied in setup(), built by code generation into a PROGMEM table.
Definition npm1300.h:53
NPM1300ChargerConfig charger
Definition npm1300.h:55
NPM1300OutputConfig outputs[NPM1300_OUTPUT_COUNT]
Definition npm1300.h:54
Register codes for one regulator, NPM1300_UNSET where the YAML leaves the field out.
Definition npm1300.h:34
The status registers read on every update.
Definition npm1300.h:62