Tool Libs
UMIT-TIROL Institute of Automation and Control Engineering library collection
 
Loading...
Searching...
No Matches
MAX31865.h
Go to the documentation of this file.
1
5#pragma once
6
7#include <cmath>
8#include <cstdint>
9
10#include "sys/spi.h"
11
28 enum Type {
29 PT2WIRE = 0,
30 PT3WIRE = 1,
31 PT4WIRE = 0,
32 };
33
34 // Register Addresses
35 enum {
36 REG_CONFIG = 0,
37 REG_RTD = 1,
38 REG_HIGH_FAULT_THRESHOLD = 3,
39 REG_LOW_FAULT_THRESHOLD = 5,
40 REG_STATUS = 7
41 };
42
43 // Operation on register
44 enum {
45 READ = 0,
46 WRITE = 0x80
47 };
49 struct Config {
51 double Rnom;
52 double Rref;
53 };
54
64 DIO cs,
65 Config c = {
66 .wires = PT2WIRE,
67 .Rnom = 100,
68 .Rref = 430,
69 })
70 : SPI::Device(bus, cs, {SPI::Mode::M3, SPI::FirstBit::MSB}), Rnom(c.Rnom), Rref(c.Rref) {
71 setConfig(1 << 7 | // bias
72 1 << 6 | // auto conversion
73 c.wires << 4 |
74 1 << 1 | // clear faults
75 1 << 0 // 50Hz filter
76 );
77 /* setLThr(0); */
78 /* setHThr(0xffff); */
79 getAllData();
80 }
81
86 double tempLin() {
87 if (data.rtd & 1) {
89 return NAN;
90 }
91 return lin();
92 }
93
99 double temp() {
100 if (data.rtd & 1) {
101 clearFaults();
102 return NAN;
103 }
104 return nonlin();
105 }
106
110 void sense() {
111 getTempData();
112 }
113
117 void readFault() {
118 getStatusData();
119 }
120
124 void readAllData() {
125 getAllData();
126 }
127
128 /*
129 * callback when async read is finished
130 */
131 void callback(const SPI::Request rq) override {
132 if (rq.dir == SPI::Request::MOSI) return;
133 const uint8_t *raw = rq.data.buf;
134 switch (rq.data.len) {
135 case 2: // STATUS
136 data.fault = raw[1];
137 break;
138 case 9: // All Data
139 data.rtd = raw[2] << 8 | raw[3];
140 data.threshold.high = raw[4]<<8 | raw[5];
141 data.threshold.low = raw[6]<<8 | raw[7];
142 data.fault = raw[8];
143 break;
144 case 3: // resistance data
145 data.rtd = raw[1] << 8 | raw[2];
146 break;
147 }
148 }
149
153 uint8_t fault() {
154 return data.fault;
155 }
156
160 void clearFaults() {
161 data.rtd = data.rtd >> 1 << 1;
162 getStatusData();
163 setConfig(data.config | 1 << 1);
164 }
165
166
171 void setLThr(uint16_t thr) {
172 bus.trypush({
173 .dev = this,
174 .data = {WRITE | REG_LOW_FAULT_THRESHOLD, (uint8_t)(thr>>8), (uint8_t)thr},
175 .dir = SPI::Request::MOSI,
176 });
177 }
178
183 void setHThr(uint16_t thr) {
184 bus.trypush({
185 .dev = this,
186 .data = {WRITE | REG_HIGH_FAULT_THRESHOLD, (uint8_t)(thr>>8), (uint8_t)thr},
187 .dir = SPI::Request::MOSI,
188 });
189 }
190
191private:
192 const double Rnom = 1000;
193 const double Rref = 0;
194 // from MAX31865 datasheet
195 const double alpha = 0.00385055;
196 const double a = 3.9083e-3;
197 const double b = -5.775e-7;
198 const double a2b = a/(2*b);
199 // from application note
200 /* const double Z1 = -3.9083e-3; */
201 /* const double Z2 = 17.58480889e-6; */
202 /* const double Z3 = 4 * b / Rnom; */
203 /* const double Z4 = 2*b; */
204
205 struct sensor {
206 uint8_t config;
207 uint8_t fault;
208 struct threshold {
209 uint16_t high, low;
210 } threshold;
211 int16_t rtd;
212 } data;
213
214 void setConfig(uint8_t val) {
215 data.config = val;
216 bus.trypush({
217 .dev = this,
218 .data = {WRITE|REG_CONFIG, data.config},
219 .dir = SPI::Request::MOSI,
220 });
221 }
222
223 void getTempData() {
224 bus.trypush({
225 .dev = this,
226 .data = {READ|REG_RTD, 0, 0},
227 .dir = SPI::Request::BOTH,
228 });
229 }
230
231 void getStatusData() {
232 bus.trypush({
233 .dev = this,
234 .data = {READ|REG_STATUS, 0},
235 .dir = SPI::Request::BOTH,
236 });
237 }
238
239 void getAllData() {
240 bus.trypush({
241 .dev = this,
242 .data = {READ|REG_CONFIG, 0,0,0,0,0,0,0,0},
243 .dir = SPI::Request::BOTH,
244 });
245 }
246 double lin() {
247 // resistance
248 double Rrtd = (double) (data.rtd >> 1) / (1 << 15) * Rref;
249 // linear: R(t) = R0 (1 + alpha*T)
250 return (Rrtd - Rnom)/(Rnom * alpha);
251 }
252 double nonlin() {
253 // resistance
254 double Rrtd = (double) (data.rtd >> 1) / (1 << 15) * Rref;
255 // quadratic:
256 // from
257 // R(t) = R0 (1 + aT + bT^2)
258 // !! ONLY for temperatures > 0°C !!
259 double dTemp = -a2b - sqrt(a2b*a2b - (Rnom - Rrtd)/(b*Rnom));
260 /* double dTemp = (Z1 + sqrt(Z2 + Z3 * Rrtd)) / Z4; */
261 return dTemp;
262 }
263};
Wrapper class for simple digital input/output pin.
Definition gpio.h:13
Copyright (c) 2023 IACE.
size_t len
number of items stored in buffer
Definition buffer.h:21
Implementation of MAX31865 based temperature sensor.
Definition MAX31865.h:26
void sense()
async read temperature data from sensor
Definition MAX31865.h:110
double temp()
quadratic temperature approximation
Definition MAX31865.h:99
void callback(const SPI::Request rq) override
is called as soon as requested data arrived over wire
Definition MAX31865.h:131
MAX31865(Sink< SPI::Request > &bus, DIO cs, Config c={ .wires=PT2WIRE,.Rnom=100,.Rref=430, })
configure temperature sensor
Definition MAX31865.h:63
void clearFaults()
clear faults on sensor
Definition MAX31865.h:160
Type wires
number of wires
Definition MAX31865.h:50
double tempLin()
linear temperature approximation
Definition MAX31865.h:86
void readFault()
async read fault data from sensor
Definition MAX31865.h:117
void readAllData()
async read all data from sensor
Definition MAX31865.h:124
double Rref
reference resistor
Definition MAX31865.h:52
Type
Sensor Type.
Definition MAX31865.h:28
@ PT2WIRE
2 wire
Definition MAX31865.h:29
@ PT4WIRE
4 wire
Definition MAX31865.h:31
@ PT3WIRE
3 wire
Definition MAX31865.h:30
void setHThr(uint16_t thr)
set high fault threshold value
Definition MAX31865.h:183
void setLThr(uint16_t thr)
set low fault threshold value
Definition MAX31865.h:171
double Rnom
nominal resistance
Definition MAX31865.h:51
uint8_t fault()
Definition MAX31865.h:153
Sensor configuration.
Definition MAX31865.h:49
generic SPI device wrapper.
Definition spi.h:22
DIO cs
GPIO chipselect line.
Definition spi.h:25
struct defining an SPI request.
Definition spi.h:54
Buffer< uint8_t > data
transfer data
Definition spi.h:56
enum SPI::Request::Direction dir
direction the data should travel
@ MOSI
Master out, Slave in.
Definition spi.h:58
@ BOTH
Both in, Both out.
Definition spi.h:60
generic object sink, i.e.
Definition streams.h:15
void trypush(T &&t)
use this if you don't care if it's going to be successful. data gets discarded if sink is full.
Definition streams.h:28