ESPHome  2023.5.5
max31865.cpp
Go to the documentation of this file.
1 #include "max31865.h"
2 
3 #include "esphome/core/log.h"
4 #include <cmath>
5 
6 namespace esphome {
7 namespace max31865 {
8 
9 static const char *const TAG = "max31865";
10 
12  // Check new faults since last measurement
13  if (!has_fault_) {
14  const uint8_t faults = this->read_register_(FAULT_STATUS_REG);
15  if (faults & 0b11111100) {
16  if (faults & (1 << 2)) {
17  ESP_LOGW(TAG, "Overvoltage/undervoltage fault between measurements");
18  }
19  if (faults & (1 << 3)) {
20  ESP_LOGW(TAG, "RTDIN- < 0.85 x V_BIAS (FORCE- open) between measurements");
21  }
22  if (faults & (1 << 4)) {
23  ESP_LOGW(TAG, "REFIN- < 0.85 x V_BIAS (FORCE- open) between measurements");
24  }
25  if (faults & (1 << 5)) {
26  ESP_LOGW(TAG, "REFIN- > 0.85 x V_BIAS between measurements");
27  }
28  if (!has_warn_) {
29  if (faults & (1 << 6)) {
30  ESP_LOGW(TAG, "RTD Low Threshold between measurements");
31  }
32  if (faults & (1 << 7)) {
33  ESP_LOGW(TAG, "RTD High Threshold between measurements");
34  }
35  }
36  }
37  }
38 
39  // Run fault detection
40  this->write_config_(0b11101110, 0b10000110);
41  const uint32_t start_time = micros();
42  uint8_t config;
43  uint32_t fault_detect_time;
44  do {
45  config = this->read_register_(CONFIGURATION_REG);
46  fault_detect_time = micros() - start_time;
47  if ((fault_detect_time >= 6000) && (config & 0b00001100)) {
48  ESP_LOGE(TAG, "Fault detection incomplete (0x%02X) after %uμs (datasheet spec is 600μs max)! Aborting read.",
49  config, fault_detect_time);
50  this->publish_state(NAN);
51  this->status_set_error();
52  return;
53  }
54  } while (config & 0b00001100);
55  ESP_LOGV(TAG, "Fault detection completed in %uμs.", fault_detect_time);
56 
57  // Start 1-shot conversion
58  this->write_config_(0b11100000, 0b10100000);
59 
60  // Datasheet max conversion time is 55ms for 60Hz / 66ms for 50Hz
61  auto f = std::bind(&MAX31865Sensor::read_data_, this);
62  this->set_timeout("value", filter_ == FILTER_60HZ ? 55 : 66, f);
63 }
64 
66  ESP_LOGCONFIG(TAG, "Setting up MAX31865Sensor '%s'...", this->name_.c_str());
67  this->spi_setup();
68 
69  // Build base configuration
70  base_config_ = 0b00000000;
71  base_config_ |= (filter_ & 1) << 0;
72  if (rtd_wires_ == 3) {
73  base_config_ |= 1 << 4;
74  }
75 
76  // Clear any existing faults & set base config
77  this->write_config_(0b00000010, 0b00000010);
78 }
79 
81  LOG_SENSOR("", "MAX31865", this);
82  LOG_PIN(" CS Pin: ", this->cs_);
83  LOG_UPDATE_INTERVAL(this);
84  ESP_LOGCONFIG(TAG, " Reference Resistance: %.2fΩ", reference_resistance_);
85  ESP_LOGCONFIG(TAG, " RTD: %u-wire %.2fΩ", rtd_wires_, rtd_nominal_resistance_);
86  ESP_LOGCONFIG(TAG, " Mains Filter: %s",
87  (filter_ == FILTER_60HZ ? "60 Hz" : (filter_ == FILTER_50HZ ? "50 Hz" : "Unknown!")));
88 }
89 
91 
93  // Read temperature, disable V_BIAS (save power)
94  const uint16_t rtd_resistance_register = this->read_register_16_(RTD_RESISTANCE_MSB_REG);
95  this->write_config_(0b11000000, 0b00000000);
96 
97  // Check for bad connection
98  if (rtd_resistance_register == 0b0000000000000000 || rtd_resistance_register == 0b1111111111111111) {
99  ESP_LOGE(TAG, "SPI bus read all 0 or all 1 (0x%04X), check MAX31865 wiring & power.", rtd_resistance_register);
100  this->publish_state(NAN);
101  this->status_set_error();
102  return;
103  }
104 
105  // Check faults
106  const uint8_t faults = this->read_register_(FAULT_STATUS_REG);
107  if ((has_fault_ = faults & 0b00111100)) {
108  if (faults & (1 << 2)) {
109  ESP_LOGE(TAG, "Overvoltage/undervoltage fault");
110  }
111  if (faults & (1 << 3)) {
112  ESP_LOGE(TAG, "RTDIN- < 0.85 x V_BIAS (FORCE- open)");
113  }
114  if (faults & (1 << 4)) {
115  ESP_LOGE(TAG, "REFIN- < 0.85 x V_BIAS (FORCE- open)");
116  }
117  if (faults & (1 << 5)) {
118  ESP_LOGE(TAG, "REFIN- > 0.85 x V_BIAS");
119  }
120  this->publish_state(NAN);
121  this->status_set_error();
122  return;
123  } else {
124  this->status_clear_error();
125  }
126  if ((has_warn_ = faults & 0b11000000)) {
127  if (faults & (1 << 6)) {
128  ESP_LOGW(TAG, "RTD Low Threshold");
129  }
130  if (faults & (1 << 7)) {
131  ESP_LOGW(TAG, "RTD High Threshold");
132  }
133  this->status_set_warning();
134  } else {
135  this->status_clear_warning();
136  }
137 
138  // Process temperature
139  if (rtd_resistance_register & 0x0001) {
140  ESP_LOGW(TAG, "RTD Resistance Registers fault bit set! (0x%04X)", rtd_resistance_register);
141  this->status_set_warning();
142  }
143  const float rtd_ratio = static_cast<float>(rtd_resistance_register >> 1) / static_cast<float>((1 << 15) - 1);
144  const float temperature = this->calc_temperature_(rtd_ratio);
145  ESP_LOGV(TAG, "RTD read complete. %.5f (ratio) * %.1fΩ (reference) = %.2fΩ --> %.2f°C", rtd_ratio,
146  reference_resistance_, reference_resistance_ * rtd_ratio, temperature);
147  this->publish_state(temperature);
148 }
149 
150 void MAX31865Sensor::write_config_(uint8_t mask, uint8_t bits, uint8_t start_position) {
151  uint8_t value = base_config_;
152 
153  value &= (~mask);
154  value |= (bits << start_position);
155 
156  this->write_register_(CONFIGURATION_REG, value);
157 }
158 
159 void MAX31865Sensor::write_register_(uint8_t reg, uint8_t value) {
160  this->enable();
161  this->write_byte(reg |= SPI_WRITE_M);
162  this->write_byte(value);
163  this->disable();
164  ESP_LOGVV(TAG, "write_register_ 0x%02X: 0x%02X", reg, value);
165 }
166 
167 uint8_t MAX31865Sensor::read_register_(uint8_t reg) {
168  this->enable();
169  this->write_byte(reg);
170  const uint8_t value(this->read_byte());
171  this->disable();
172  ESP_LOGVV(TAG, "read_register_ 0x%02X: 0x%02X", reg, value);
173  return value;
174 }
175 
176 uint16_t MAX31865Sensor::read_register_16_(uint8_t reg) {
177  this->enable();
178  this->write_byte(reg);
179  const uint8_t msb(this->read_byte());
180  const uint8_t lsb(this->read_byte());
181  this->disable();
182  const uint16_t value((msb << 8) | lsb);
183  ESP_LOGVV(TAG, "read_register_16_ 0x%02X: 0x%04X", reg, value);
184  return value;
185 }
186 
187 float MAX31865Sensor::calc_temperature_(float rtd_ratio) {
188  // Based loosely on Adafruit's library: https://github.com/adafruit/Adafruit_MAX31865
189  // Mainly based on formulas provided by Analog:
190  // http://www.analog.com/media/en/technical-documentation/application-notes/AN709_0.pdf
191 
192  const float a = 3.9083e-3;
193  const float b = -5.775e-7;
194  const float z1 = -a;
195  const float z2 = a * a - 4 * b;
196  const float z3 = 4 * b / rtd_nominal_resistance_;
197  const float z4 = 2 * b;
198 
199  float rtd_resistance = rtd_ratio * reference_resistance_;
200 
201  // ≥ 0°C Formula
202  const float pos_temp = (z1 + std::sqrt(z2 + (z3 * rtd_resistance))) / z4;
203  if (pos_temp >= 0) {
204  return pos_temp;
205  }
206 
207  // < 0°C Formula
208  if (rtd_nominal_resistance_ != 100) {
209  // Normalize RTD to 100Ω
210  rtd_resistance /= rtd_nominal_resistance_;
211  rtd_resistance *= 100;
212  }
213  float rpoly = rtd_resistance;
214  float neg_temp = -242.02f;
215  neg_temp += 2.2228f * rpoly;
216  rpoly *= rtd_resistance; // square
217  neg_temp += 2.5859e-3f * rpoly;
218  rpoly *= rtd_resistance; // ^3
219  neg_temp -= 4.8260e-6f * rpoly;
220  rpoly *= rtd_resistance; // ^4
221  neg_temp -= 2.8183e-8f * rpoly;
222  rpoly *= rtd_resistance; // ^5
223  neg_temp += 1.5243e-10f * rpoly;
224  return neg_temp;
225 }
226 
227 } // namespace max31865
228 } // namespace esphome
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:18
uint16_t read_register_16_(uint8_t reg)
Definition: max31865.cpp:176
float get_setup_priority() const override
Definition: max31865.cpp:90
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition: component.cpp:68
float temperature
Definition: qmp6988.h:71
uint32_t IRAM_ATTR HOT micros()
Definition: core.cpp:29
void write_register_(uint8_t reg, uint8_t value)
Definition: max31865.cpp:159
void status_clear_warning()
Definition: component.cpp:153
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
void status_set_warning()
Definition: component.cpp:145
float calc_temperature_(float rtd_ratio)
Definition: max31865.cpp:187
void write_config_(uint8_t mask, uint8_t bits, uint8_t start_position=0)
Definition: max31865.cpp:150
constexpr const char * c_str() const
Definition: string_ref.h:68
MAX31865ConfigFilter filter_
Definition: max31865.h:44
void status_clear_error()
Definition: component.cpp:154
uint8_t read_register_(uint8_t reg)
Definition: max31865.cpp:167
Definition: a4988.cpp:4