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
#![no_std]
use embedded_hal as hal;
use hal::blocking::spi;
use hal::digital::v2::OutputPin;
use embedded_hal::blocking::delay::DelayMs;
pub enum Registers {
Control = 0x00,
Lsb = 0x01,
Msb = 0x02,
ManufacturerId = 0x03
}
pub struct Tc72<SPI, CS> {
spi: SPI,
cs: CS
}
const EXPECTED_ID: u8 = 0x54;
#[derive(Debug, PartialEq, Clone)]
pub enum Tc72Error<SpiError, PinError> {
Spi(SpiError),
Cs(PinError),
ManufacturerWrong
}
impl<SPI, CS, SpiError, PinError> Tc72<SPI, CS>
where
SPI: spi::Transfer<u8, Error=SpiError> + spi::Write<u8, Error=SpiError>,
CS: OutputPin<Error = PinError>
{
pub fn new(spi: SPI, cs: CS) -> Result<Self, Tc72Error<SpiError, PinError>> {
let mut tc72 = Tc72 { spi, cs };
let id = tc72.manufacturer_id()?;
if id != EXPECTED_ID {
Err(Tc72Error::ManufacturerWrong)
} else {
Ok(tc72)
}
}
pub fn manufacturer_id(&mut self) -> Result<u8, Tc72Error<SpiError, PinError>> {
self.read_reg(Registers::ManufacturerId)
}
pub fn control(&mut self, shutdown: bool, one_shot: bool) -> Result<(), Tc72Error<SpiError, PinError>>{
let value = ((one_shot as u8) << 4) + (shutdown as u8);
self.write_reg(Registers::Control, value)
}
pub fn temp_raw(&mut self) -> Result<u16, Tc72Error<SpiError, PinError>> {
let msb = (self.read_reg(Registers::Msb)? as u16) << 2;
let lsb = (self.read_reg(Registers::Lsb)? as u16) >> 6;
Ok(msb | lsb)
}
pub fn temp(&mut self) -> Result<f32, Tc72Error<SpiError, PinError>> {
self.temp_raw().map(|v| {
let bit10: u16 = 1 << 9;
let v = if (v & bit10) > 0 { ( v & !bit10 ) | (1 << 15)} else { v };
let v = v as i16;
(v as f32) * 0.25f32
} as f32)
}
pub fn one_shot_with_150ms_delay(&mut self, delay: &mut dyn DelayMs<u8>) -> Result<f32, Tc72Error<SpiError, PinError>> {
self.control(false, true)?;
delay.delay_ms(150);
self.temp()
}
fn write_reg(&mut self, reg: Registers, value: u8) -> Result<(), Tc72Error<SpiError, PinError>>{
let bytes = [((reg as u8) << 1) | 0b0000_1000, value];
self.cs.set_high().map_err(Tc72Error::Cs)?;
self.spi.write(&bytes).map_err(Tc72Error::Spi)?;
self.cs.set_low().map_err(Tc72Error::Cs)?;
Ok(())
}
fn read_reg(&mut self, reg: Registers) -> Result<u8, Tc72Error<SpiError, PinError>> {
let mut bytes = [((reg as u8) << 1), 0];
self.cs.set_high().map_err(Tc72Error::Cs)?;
self.spi.transfer(&mut bytes)
.map_err(Tc72Error::Spi)?;
self.cs.set_low().map_err(Tc72Error::Cs)?;
Ok(bytes[1])
}
}