ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
npm1300.h
Go to the documentation of this file.
1#pragma once
2
3// Product specification: https://docs.nordicsemi.com/bundle/ps_npm1300/page/keyfeatures_html5.html
4
9#ifdef USE_SENSOR
11#endif
12#ifdef USE_TEXT_SENSOR
14#endif
15#ifdef USE_SWITCH
17#endif
18
19namespace esphome::npm1300 {
20
22enum class NPM1300Output : uint8_t {
27};
28static constexpr uint8_t NPM1300_OUTPUT_COUNT = 4;
29
31static constexpr uint8_t NPM1300_UNSET = 0xFF;
32
35 uint8_t voltage; // VOUT: (millivolts - 1000) / 100
36 uint8_t ldo_mode; // LDSWxLDOSEL, LDO1 and LDO2 only: 1 LDO, 0 load switch
37 uint8_t enabled; // turned on (1) or off (0) at boot
38};
39
42 uint16_t current; // BCHGISET: milliamperes / 2
43 uint16_t discharge_limit; // BCHGISETDISCHARGE: 84 for 200 mA, 415 for 1000 mA
44 uint8_t termination_voltage; // BCHGVTERM
45 uint8_t warm_termination_voltage; // BCHGVTERMR
46 uint8_t trickle_voltage; // BCHGVTRICKLESEL: 0 for 2.9 V, 1 for 2.5 V
47 uint8_t termination_current; // BCHGITERMSEL: 0 for 10 %, 1 for 20 %
49 bool enabled;
50};
51
54 NPM1300OutputConfig outputs[NPM1300_OUTPUT_COUNT];
55 NPM1300ChargerConfig charger; // only applied when charger_configured
57 uint8_t thermistor; // ADCNTCRSEL code: 0 for none, 1 for 10 kOhm, 2 for 47 kOhm, 3 for 100 kOhm, or NPM1300_UNSET
59};
60
63 uint8_t vbus; // VBUSINSTATUS
64 uint8_t charger_enable; // BCHGENABLESET
65 uint8_t ntc; // NTCSTATUS
66 uint8_t charge; // BCHGCHARGESTATUS
67 uint8_t error_reason; // BCHGERRREASON
68};
69
70#ifdef USE_NPM1300_DATA_PUBLISHER
72inline uint16_t adc_code(const uint8_t *adc, uint8_t msb, uint8_t lsbs, uint8_t shift) {
73 return (adc[msb] << 2) | ((adc[lsbs] >> shift) & 0x03);
74}
76float ntc_temperature(uint16_t code, float inv_beta);
77#endif
78#ifdef USE_TEXT_SENSOR
80void publish_charger_status(text_sensor::TextSensor *sensor, uint8_t &last, const NPM1300Status &status);
81#endif
82
84#ifdef USE_SENSOR
85 SUB_SENSOR(vbus_current_limit)
86#endif
87#ifdef USE_SWITCH
88 SUB_SWITCH(charger)
89 SUB_SWITCH(buck1)
90 SUB_SWITCH(buck2)
91 SUB_SWITCH(ldo1)
92 SUB_SWITCH(ldo2)
93#endif
94
95 public:
97 explicit NPM1300Component(const NPM1300Config *config) : config_(config) {}
98
99 void setup() override;
100 void update() override;
101 void dump_config() override;
102 // Right after the I2C bus: the regulators may power other peripherals
103 float get_setup_priority() const override { return setup_priority::BUS - 1.0f; }
104
105#ifdef USE_NPM1300_STATUS_PUBLISHER
106 HUB_PUBLISHER(status, const NPM1300Status &)
107#endif
108#ifdef USE_NPM1300_DATA_PUBLISHER
109 HUB_PUBLISHER(data, const uint8_t *, float)
110#endif
111#if defined(USE_NPM1300_STATUS_PUBLISHER) || defined(USE_NPM1300_DATA_PUBLISHER)
112 HUB_PUBLISHER(dump, const char *)
113#endif
114
116 void set_vbus_current_limit(uint8_t code) { this->vbus_current_limit_ = code; }
117
119 bool set_charger_enabled(bool enabled);
121 bool set_output_enabled(NPM1300Output output, bool enabled);
123 void enter_ship_mode();
126 void hibernate(uint32_t duration_ms);
127
128 protected:
131 bool configure_charger_(const NPM1300Config &config);
132 bool configure_output_(uint8_t index, const NPM1300OutputConfig &config);
133#ifdef USE_NPM1300_DATA_PUBLISHER
135 void read_measurements_();
136#endif
137#ifdef USE_SWITCH
140#endif
141
143#ifdef USE_NPM1300_DATA_PUBLISHER
144 // BCHGISET code and discharge limit, for the full scale of the battery current measurement
145 uint16_t charge_code_{0};
147#endif
148 uint8_t vbus_current_limit_{NPM1300_UNSET};
150 bool vbus_present_{false};
151};
152
153} // namespace esphome::npm1300
uint8_t status
Definition bl0942.h:8
This class simplifies creating components that periodically check a state.
Definition component.h:542
This Class provides the methods to read/write bytes from/to an i2c device.
Definition i2c.h:144
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
uint16_t adc_code(const uint8_t *adc, uint8_t msb, uint8_t lsbs, uint8_t shift)
The 10-bit ADC result whose high 8 bits are adc[msb] and low 2 bits are at shift in adc[lsbs].
Definition npm1300.h:72
NPM1300Output
The regulators, in the order of their enable task registers.
Definition npm1300.h:22
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
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
constexpr float BUS
For communication buses like i2c/spi.
Definition component.h:40
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