1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#![cfg_attr(not(test), no_std)]
extern crate embedded_hal as hal;
use hal::blocking::spi;
use hal::digital::v2::{InputPin, OutputPin};
use hal::spi::{Mode, Phase, Polarity};
#[cfg(feature = "doc")]
pub mod examples;
pub const MODE: Mode = Mode {
phase: Phase::CaptureOnSecondTransition,
polarity: Polarity::IdleHigh,
};
pub mod temp_conversion;
pub enum FilterMode {
Filter60Hz = 0,
Filter50Hz = 1,
}
pub enum SensorType {
TwoOrFourWire = 0,
ThreeWire = 1,
}
pub struct Max31865<SPI, NCS, RDY> {
spi: SPI,
ncs: NCS,
rdy: RDY,
calibration: u32,
}
#[derive(Debug)]
pub enum Error<E> {
SPIError(E),
PinError,
}
impl<E, SPI, NCS, RDY> Max31865<SPI, NCS, RDY>
where
SPI: spi::Write<u8, Error = E> + spi::Transfer<u8, Error = E>,
NCS: OutputPin,
RDY: InputPin,
{
pub fn new(spi: SPI, mut ncs: NCS, rdy: RDY) -> Result<Max31865<SPI, NCS, RDY>, Error<E>> {
let default_calib = 40000;
ncs.set_high().map_err(|_| Error::PinError)?;
let max31865 = Max31865 {
spi,
ncs,
rdy,
calibration: default_calib,
};
Ok(max31865)
}
pub fn configure(
&mut self,
vbias: bool,
conversion_mode: bool,
one_shot: bool,
sensor_type: SensorType,
filter_mode: FilterMode,
) -> Result<(), Error<E>> {
let conf: u8 = ((vbias as u8) << 7)
| ((conversion_mode as u8) << 6)
| ((one_shot as u8) << 5)
| ((sensor_type as u8) << 4)
| (filter_mode as u8);
self.write(Register::CONFIG, conf)?;
Ok(())
}
pub fn set_calibration(&mut self, calib: u32) -> () {
self.calibration = calib;
}
pub fn read_ohms(&mut self) -> Result<u32, Error<E>> {
let raw = self.read_raw()?;
let ohms = ((raw >> 1) as u32 * self.calibration) >> 15;
Ok(ohms)
}
pub fn read_default_conversion(&mut self) -> Result<i32, Error<E>> {
let ohms = self.read_ohms()?;
let temp = temp_conversion::LOOKUP_VEC_PT100.lookup_temperature(ohms as i32);
Ok(temp)
}
pub fn read_raw(&mut self) -> Result<u16, Error<E>> {
let msb: u16 = self.read(Register::RTD_MSB)? as u16;
let lsb: u16 = self.read(Register::RTD_LSB)? as u16;
Ok((msb << 8) | lsb)
}
pub fn is_ready(&self) -> Result<bool, RDY::Error> {
self.rdy.is_low()
}
fn read(&mut self, reg: Register) -> Result<u8, Error<E>> {
let buffer: [u8; 2] = self.read_two(reg)?;
Ok(buffer[1])
}
fn read_two(&mut self, reg: Register) -> Result<[u8; 2], Error<E>> {
let mut buffer = [0u8; 2];
let slice: &mut [u8] = &mut buffer;
slice[0] = reg.read_address();
self.ncs.set_low().map_err(|_| Error::PinError)?;
self.spi.transfer(slice).map_err(|e| Error::SPIError(e))?;
self.ncs.set_high().map_err(|_| Error::PinError)?;
Ok(buffer)
}
fn write(&mut self, reg: Register, val: u8) -> Result<(), Error<E>> {
self.ncs.set_low().map_err(|_| Error::PinError)?;
self.spi
.write(&[reg.write_address(), val])
.map_err(|e| Error::SPIError(e))?;
self.ncs.set_high().map_err(|_| Error::PinError)?;
Ok(())
}
}
#[allow(non_camel_case_types)]
#[allow(dead_code)]
#[derive(Clone, Copy)]
enum Register {
CONFIG = 0x00,
RTD_MSB = 0x01,
RTD_LSB = 0x02,
HIGH_FAULT_THRESHOLD_MSB = 0x03,
HIGH_FAULT_THRESHOLD_LSB = 0x04,
LOW_FAULT_THRESHOLD_MSB = 0x05,
LOW_FAULT_THRESHOLD_LSB = 0x06,
FAULT_STATUS = 0x07,
}
const R: u8 = 0 << 7;
const W: u8 = 1 << 7;
impl Register {
fn read_address(&self) -> u8 {
*self as u8 | R
}
fn write_address(&self) -> u8 {
*self as u8 | W
}
}