ESPHome 2025.6.0
Loading...
Searching...
No Matches
max31856.cpp
Go to the documentation of this file.
1#include "max31856.h"
2
3#include "esphome/core/log.h"
4#include <cmath>
5
6namespace esphome {
7namespace max31856 {
8
9static const char *const TAG = "max31856";
10
11// Based on Adafruit's library: https://github.com/adafruit/Adafruit_MAX31856
12
14 ESP_LOGCONFIG(TAG, "Running setup for '%s'", this->name_.c_str());
15 this->spi_setup();
16
17 // assert on any fault
18 ESP_LOGCONFIG(TAG, "Setting up assertion on all faults");
20
21 ESP_LOGCONFIG(TAG, "Setting up open circuit fault detection");
23
25 this->set_noise_filter_();
26}
27
29 LOG_SENSOR("", "MAX31856", this);
30 LOG_PIN(" CS Pin: ", this->cs_);
31 ESP_LOGCONFIG(TAG, " Mains Filter: %s",
32 (filter_ == FILTER_60HZ ? "60 Hz" : (filter_ == FILTER_50HZ ? "50 Hz" : "Unknown!")));
34 ESP_LOGCONFIG(TAG, " Thermocouple Type: Unknown");
35 } else {
36 ESP_LOGCONFIG(TAG, " Thermocouple Type: %c", "BEJKNRST"[this->thermocouple_type_]);
37 }
38
39 LOG_UPDATE_INTERVAL(this);
40}
41
43 ESP_LOGVV(TAG, "update");
44
46
47 // Datasheet max conversion time for 1 shot is 155ms for 60Hz / 185ms for 50Hz
48 auto f = std::bind(&MAX31856Sensor::read_thermocouple_temperature_, this);
49 this->set_timeout("MAX31856Sensor::read_thermocouple_temperature_", filter_ == FILTER_60HZ ? 155 : 185, f);
50}
51
53 if (this->has_fault_()) {
54 // Faults have been logged, clear it for next loop
55 this->clear_fault_();
56 } else {
57 int32_t temp24 = this->read_register24_(MAX31856_LTCBH_REG);
58 if (temp24 & 0x800000) {
59 temp24 |= 0xFF000000; // fix sign
60 }
61
62 temp24 >>= 5; // bottom 5 bits are unused
63
64 float temp_c = temp24;
65 temp_c *= 0.0078125;
66
67 ESP_LOGD(TAG, "Got thermocouple temperature: %.2f°C", temp_c);
68 this->publish_state(temp_c);
69 }
70}
71
73 ESP_LOGVV(TAG, "one_shot_temperature_");
75
76 uint8_t t = this->read_register_(MAX31856_CR0_REG);
77
78 t &= ~MAX31856_CR0_AUTOCONVERT; // turn off autoconversion mode
79 t |= MAX31856_CR0_1SHOT; // turn on one shot mode
80
82}
83
85 ESP_LOGVV(TAG, "read_fault_");
86 uint8_t faults = this->read_register_(MAX31856_SR_REG);
87
88 if (faults == 0) {
89 ESP_LOGV(TAG, "status_set_warning");
91 return false;
92 }
93
94 ESP_LOGV(TAG, "status_set_warning");
95 this->status_set_warning();
96
98 ESP_LOGW(TAG, "Cold Junction Out-of-Range: '%s'", this->name_.c_str());
99 }
101 ESP_LOGW(TAG, "Thermocouple Out-of-Range: '%s'", this->name_.c_str());
102 }
104 ESP_LOGW(TAG, "Cold-Junction High Fault: '%s'", this->name_.c_str());
105 }
106 if ((faults & MAX31856_FAULT_CJLOW) == MAX31856_FAULT_CJLOW) {
107 ESP_LOGW(TAG, "Cold-Junction Low Fault: '%s'", this->name_.c_str());
108 }
110 ESP_LOGW(TAG, "Thermocouple Temperature High Fault: '%s'", this->name_.c_str());
111 }
112 if ((faults & MAX31856_FAULT_TCLOW) == MAX31856_FAULT_TCLOW) {
113 ESP_LOGW(TAG, "Thermocouple Temperature Low Fault: '%s'", this->name_.c_str());
114 }
115 if ((faults & MAX31856_FAULT_OVUV) == MAX31856_FAULT_OVUV) {
116 ESP_LOGW(TAG, "Overvoltage or Undervoltage Input Fault: '%s'", this->name_.c_str());
117 }
118 if ((faults & MAX31856_FAULT_OPEN) == MAX31856_FAULT_OPEN) {
119 ESP_LOGW(TAG, "Thermocouple Open-Circuit Fault (possibly not connected): '%s'", this->name_.c_str());
120 }
121
122 return true;
123}
124
126 ESP_LOGV(TAG, "clear_fault_");
127 uint8_t t = this->read_register_(MAX31856_CR0_REG);
128
129 t |= MAX31856_CR0_FAULT; // turn on fault interrupt mode
130 t |= MAX31856_CR0_FAULTCLR; // enable the fault status clear bit
131
133}
134
138 type = MAX31856_TCTYPE_K;
139 } else {
140 type = this->thermocouple_type_;
141 }
142 ESP_LOGCONFIG(TAG, "set_thermocouple_type_: 0x%02X", type);
143 uint8_t t = this->read_register_(MAX31856_CR1_REG);
144 t &= 0xF0; // mask off bottom 4 bits
145 t |= (uint8_t) type & 0x0F;
147}
148
150 ESP_LOGCONFIG(TAG, "set_noise_filter_: 0x%02X", filter_);
151 uint8_t t = this->read_register_(MAX31856_CR0_REG);
152 if (filter_ == FILTER_50HZ) {
153 t |= 0x01;
154 ESP_LOGCONFIG(TAG, "set_noise_filter_: 50 Hz, t==0x%02X", t);
155 } else {
156 t &= 0xfe;
157 ESP_LOGCONFIG(TAG, "set_noise_filter_: 60 Hz, t==0x%02X", t);
158 }
160}
161
162void MAX31856Sensor::write_register_(uint8_t reg, uint8_t value) {
163 ESP_LOGVV(TAG, "write_register_ raw reg=0x%02X value=0x%02X", reg, value);
164 reg |= SPI_WRITE_M;
165 ESP_LOGVV(TAG, "write_register_ masked reg=0x%02X value=0x%02X", reg, value);
166 this->enable();
167 ESP_LOGVV(TAG, "write_byte reg=0x%02X", reg);
168 this->write_byte(reg);
169 ESP_LOGVV(TAG, "write_byte value=0x%02X", value);
170 this->write_byte(value);
171 this->disable();
172 ESP_LOGV(TAG, "write_register_ 0x%02X: 0x%02X", reg, value);
173}
174
175uint8_t MAX31856Sensor::read_register_(uint8_t reg) {
176 ESP_LOGVV(TAG, "read_register_ 0x%02X", reg);
177 this->enable();
178 ESP_LOGVV(TAG, "write_byte reg=0x%02X", reg);
179 this->write_byte(reg);
180 const uint8_t value(this->read_byte());
181 ESP_LOGVV(TAG, "read_byte value=0x%02X", value);
182 this->disable();
183 ESP_LOGV(TAG, "read_register_ reg=0x%02X: value=0x%02X", reg, value);
184 return value;
185}
186
187uint32_t MAX31856Sensor::read_register24_(uint8_t reg) {
188 ESP_LOGVV(TAG, "read_register_24_ 0x%02X", reg);
189 this->enable();
190 ESP_LOGVV(TAG, "write_byte reg=0x%02X", reg);
191 this->write_byte(reg);
192 const uint8_t msb(this->read_byte());
193 ESP_LOGVV(TAG, "read_byte msb=0x%02X", msb);
194 const uint8_t mid(this->read_byte());
195 ESP_LOGVV(TAG, "read_byte mid=0x%02X", mid);
196 const uint8_t lsb(this->read_byte());
197 ESP_LOGVV(TAG, "read_byte lsb=0x%02X", lsb);
198 this->disable();
199 const uint32_t value((msb << 16) | (mid << 8) | lsb);
200 ESP_LOGV(TAG, "read_register_24_ reg=0x%02X: value=0x%06" PRIX32, reg, value);
201 return value;
202}
203
205
206} // namespace max31856
207} // namespace esphome
void status_set_warning(const char *message="unspecified")
void status_clear_warning()
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:75
constexpr const char * c_str() const
Definition string_ref.h:69
void write_register_(uint8_t reg, uint8_t value)
Definition max31856.cpp:162
float get_setup_priority() const override
Definition max31856.cpp:204
MAX31856ConfigFilter filter_
Definition max31856.h:87
MAX31856ThermocoupleType thermocouple_type_
Definition max31856.h:88
uint32_t read_register24_(uint8_t reg)
Definition max31856.cpp:187
uint8_t read_register_(uint8_t reg)
Definition max31856.cpp:175
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:39
uint8_t type
MAX31856ThermocoupleType
Multiple types of thermocouples supported by the chip.
Definition max31856.h:54
@ MAX31856_CR0_1SHOT
Config 0 one shot convert flag.
Definition max31856.h:17
@ MAX31856_CR1_REG
Config 1 register.
Definition max31856.h:25
@ MAX31856_FAULT_TCLOW
Fault status Thermocouple Temperature Low Fault flag.
Definition max31856.h:46
@ MAX31856_CR0_FAULT
Config 0 fault mode flag.
Definition max31856.h:22
@ MAX31856_LTCBH_REG
Linearized TC Temperature, Byte 2.
Definition max31856.h:36
@ MAX31856_FAULT_TCRANGE
Fault status Thermocouple Out-of-Range flag.
Definition max31856.h:42
@ MAX31856_CR0_REG
Config 0 register.
Definition max31856.h:15
@ MAX31856_CJTO_REG
Cold-Junction Temperature Offset Register.
Definition max31856.h:33
@ MAX31856_FAULT_OPEN
Fault status Thermocouple Open-Circuit Fault flag.
Definition max31856.h:48
@ MAX31856_FAULT_CJHIGH
Fault status Cold-Junction High Fault flag.
Definition max31856.h:43
@ MAX31856_SR_REG
Fault Status Register.
Definition max31856.h:39
@ MAX31856_MASK_REG
Fault Mask register.
Definition max31856.h:26
@ MAX31856_FAULT_CJLOW
Fault status Cold-Junction Low Fault flag.
Definition max31856.h:44
@ MAX31856_FAULT_OVUV
Fault status Overvoltage or Undervoltage Input Fault flag.
Definition max31856.h:47
@ MAX31856_CR0_OCFAULT01
Config 0 open circuit fault 01 flag.
Definition max31856.h:19
@ MAX31856_FAULT_TCHIGH
Fault status Thermocouple Temperature High Fault flag.
Definition max31856.h:45
@ MAX31856_FAULT_CJRANGE
Fault status Cold Junction Out-of-Range flag.
Definition max31856.h:41
@ MAX31856_CR0_FAULTCLR
Config 0 fault clear flag.
Definition max31856.h:23
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:20
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7