8 static const char *
const TAG =
"nau7802";
12 static const uint8_t READ_BIT = 0x01;
14 static const uint8_t PU_CTRL_REG = 0x00;
15 static const uint8_t PU_CTRL_REGISTER_RESET = 0x01;
16 static const uint8_t PU_CTRL_POWERUP_DIGITAL = 0x02;
17 static const uint8_t PU_CTRL_POWERUP_ANALOG = 0x04;
18 static const uint8_t PU_CTRL_POWERUP_READY = 0x08;
19 static const uint8_t PU_CTRL_CYCLE_START = 0x10;
20 static const uint8_t PU_CTRL_CYCLE_READY = 0x20;
21 static const uint8_t PU_CTRL_AVDD_EXTERNAL = 0x80;
23 static const uint8_t CTRL1_REG = 0x01;
24 static const uint8_t CTRL1_LDO_SHIFT = 3;
25 static const uint8_t CTRL1_LDO_MASK = (0x7 << CTRL1_LDO_SHIFT);
26 static const uint8_t CTRL1_GAIN_MASK = 0x7;
28 static const uint8_t CTRL2_REG = 0x02;
29 static const uint8_t CTRL2_CRS_SHIFT = 4;
30 static const uint8_t CTRL2_CRS_MASK = (0x7 << CTRL2_CRS_SHIFT);
31 static const uint8_t CTRL2_CALS = 0x04;
32 static const uint8_t CTRL2_CAL_ERR = 0x08;
33 static const uint8_t CTRL2_GAIN_CALIBRATION = 0x03;
34 static const uint8_t CTRL2_CONFIG_MASK = 0xF0;
36 static const uint8_t OCAL1_B2_REG = 0x03;
37 static const uint8_t GCAL1_B3_REG = 0x06;
38 static const uint8_t GCAL1_FRACTIONAL = 23;
41 static const uint8_t ADCO_B2_REG = 0x12;
43 static const uint8_t ADC_REG = 0x15;
44 static const uint8_t ADC_CHPS_DISABLE = 0x30;
46 static const uint8_t PGA_REG = 0x1B;
47 static const uint8_t PGA_LDOMODE_ESR = 0x40;
49 static const uint8_t POWER_REG = 0x1C;
50 static const uint8_t POWER_PGA_CAP_EN = 0x80;
52 static const uint8_t DEVICE_REV = 0x1F;
56 ESP_LOGCONFIG(TAG,
"Setting up NAU7802 '%s'...", this->
name_.
c_str());
60 ESP_LOGE(TAG,
"Failed I2C read during setup()");
64 ESP_LOGI(TAG,
"Setting up NAU7802 Rev %d", rev);
67 pu_ctrl |= PU_CTRL_REGISTER_RESET;
69 pu_ctrl &= ~PU_CTRL_REGISTER_RESET;
72 pu_ctrl |= PU_CTRL_POWERUP_DIGITAL;
75 if (!(pu_ctrl.
get() & PU_CTRL_POWERUP_READY)) {
76 ESP_LOGE(TAG,
"Failed to reset sensor during setup()");
81 uint32_t gcal = (uint32_t) (round(this->
gain_calibration_ * (1 << GCAL1_FRACTIONAL)));
86 pu_ctrl |= PU_CTRL_POWERUP_ANALOG;
95 pu_ctrl |= PU_CTRL_CYCLE_START;
98 ctrl1 &= ~CTRL1_GAIN_MASK;
103 pu_ctrl |= PU_CTRL_AVDD_EXTERNAL;
104 ctrl1 &= ~CTRL1_LDO_MASK;
105 ctrl1 |= this->
ldo_ << CTRL1_LDO_SHIFT;
109 ctrl2 &= ~CTRL2_CRS_MASK;
110 ctrl2 |= this->
sps_ << CTRL2_CRS_SHIFT;
114 adc_reg |= ADC_CHPS_DISABLE;
118 pga_reg &= ~PGA_LDOMODE_ESR;
122 pwr_reg |= POWER_PGA_CAP_EN;
128 LOG_SENSOR(
"",
"NAU7802",
this);
129 LOG_I2C_DEVICE(
this);
132 ESP_LOGE(TAG,
"Communication with NAU7802 failed earlier, during setup");
139 std::string voltage =
"unknown";
140 switch (this->
ldo_) {
166 voltage =
"External";
169 ESP_LOGCONFIG(TAG,
" LDO Voltage: %s", voltage.c_str());
171 switch (this->
gain_) {
197 ESP_LOGCONFIG(TAG,
" Gain: %dx", gain);
199 switch (this->
sps_) {
216 ESP_LOGCONFIG(TAG,
" Samples Per Second: %d", sps);
217 LOG_UPDATE_INTERVAL(
this);
222 for (
int i = 0; i < size; i++) {
223 data[i] = 0xFF & (value >> (size - 1 - i) * 8);
232 for (
int i = 0; i < size; i++) {
233 result |= data[i] << (size - 1 - i) * 8;
236 if (result & 0x800000 && size == 3) {
237 result |= 0xFF000000;
245 ESP_LOGW(TAG,
"Calibration already in progress");
253 ctrl2 &= CTRL2_CONFIG_MASK;
261 switch (this->state_) {
278 if (ctrl2.
get() & CTRL2_CAL_ERR) {
281 ESP_LOGE(TAG,
"Failed to calibrate sensor");
293 ESP_LOGI(TAG,
"New Offset: %s",
to_string(ocal).c_str());
294 uint32_t gcal = this->
read_value_(GCAL1_B3_REG, 4);
295 float gcal_f = ((float) gcal / (
float) (1 << GCAL1_FRACTIONAL));
296 ESP_LOGI(TAG,
"New Gain: %f", gcal_f);
304 ESP_LOGW(TAG,
"No measurements ready!");
312 int32_t result = this->
read_value_(ADCO_B2_REG, 3);
314 ESP_LOGD(TAG,
"'%s': Got value %" PRId32, this->
name_.
c_str(), result);
const float DATA
For components that import data from directly connected sensors like DHT.
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len, bool stop=true)
reads an array of bytes from a specific register in the I²C device
void dump_config() override
void status_set_warning(const char *message="unspecified")
int32_t offset_calibration_
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
uint8_t get() const
returns the register value
void set_calibration_failure_(bool failed)
bool offset_calibration_failed_
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
bool gain_calibration_failed_
float get_setup_priority() const override
void status_set_error(const char *message="unspecified")
void status_clear_warning()
BedjetMode mode
BedJet operating mode.
bool calibrate_(enum NAU7802CalibrationModes mode)
void publish_state(float state)
Publish a new state to the front-end.
void write_value_(uint8_t start_reg, size_t size, int32_t value)
constexpr const char * c_str() const
void status_clear_error()
std::string to_string(int value)
This class is used to create I2CRegister objects that act as proxies to read/write internal registers...
virtual void mark_failed()
Mark this component as failed.
Implementation of SPI Controller mode.
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a specific register in the I²C device
int32_t read_value_(uint8_t start_reg, size_t size)
bool can_proceed() override
void IRAM_ATTR HOT delay(uint32_t ms)