microchip_tc72r_rs/
lib.rs

1#![no_std]
2use embedded_hal as hal;
3
4use hal::blocking::spi;
5use hal::digital::v2::OutputPin;
6use embedded_hal::blocking::delay::DelayMs;
7
8pub enum Registers {
9    Control = 0x00,
10    Lsb = 0x01,
11    Msb = 0x02,
12    ManufacturerId = 0x03
13}
14
15pub struct Tc72<SPI, CS> {
16    spi: SPI,
17    cs: CS
18}
19
20const EXPECTED_ID: u8 = 0x54;
21
22#[derive(Debug, PartialEq, Clone)]
23pub enum Tc72Error<SpiError, PinError> {
24    Spi(SpiError),
25    Cs(PinError),
26    ManufacturerWrong
27}
28
29impl<SPI, CS, SpiError, PinError> Tc72<SPI, CS>
30    where
31        SPI: spi::Transfer<u8, Error=SpiError> + spi::Write<u8, Error=SpiError>,
32        CS: OutputPin<Error = PinError>
33{
34    /// Takes a config object to initialize the tc72 driver
35    pub fn new(spi: SPI, cs: CS) -> Result<Self, Tc72Error<SpiError, PinError>> {
36        let mut tc72 = Tc72 { spi, cs };
37        let id = tc72.manufacturer_id()?;
38        if id != EXPECTED_ID {
39            Err(Tc72Error::ManufacturerWrong)
40        } else {
41            Ok(tc72)
42        }
43    }
44
45    pub fn manufacturer_id(&mut self) -> Result<u8, Tc72Error<SpiError, PinError>> {
46        self.read_reg(Registers::ManufacturerId)
47    }
48
49    pub fn control(&mut self, shutdown: bool, one_shot: bool) -> Result<(), Tc72Error<SpiError, PinError>>{
50        let value = ((one_shot as u8) << 4) + (shutdown as u8);
51        self.write_reg(Registers::Control, value)
52    }
53
54    pub fn temp_raw(&mut self) -> Result<u16, Tc72Error<SpiError, PinError>> {
55        let msb = (self.read_reg(Registers::Msb)? as u16) << 2;
56        let lsb = (self.read_reg(Registers::Lsb)? as u16) >> 6;
57        Ok(msb | lsb)
58    }
59
60    pub fn temp(&mut self) -> Result<f32, Tc72Error<SpiError, PinError>> {
61        self.temp_raw().map(|v| {
62            let bit10: u16 = 1 << 9;
63            // if bit 10 is set it is negative. To cast 10bit unsigned to 16bit signed move sign
64            let v = if (v & bit10) > 0 { ( v & !bit10 ) | (1 << 15)} else { v };
65            let v = v as i16;
66            (v as f32) * 0.25f32
67        } as f32)
68    }
69
70    pub fn one_shot_with_150ms_delay(&mut self, delay: &mut dyn DelayMs<u8>) -> Result<f32, Tc72Error<SpiError, PinError>> {
71        self.control(false, true)?;
72        delay.delay_ms(150);
73        self.temp()
74    }
75
76    fn write_reg(&mut self, reg: Registers, value: u8) -> Result<(), Tc72Error<SpiError, PinError>>{
77        let bytes = [reg as u8 | 0b0000_1000, value];
78        self.cs.set_high().map_err(Tc72Error::Cs)?;
79        self.spi.write(&bytes).map_err(Tc72Error::Spi)?;
80        self.cs.set_low().map_err(Tc72Error::Cs)?;
81        Ok(())
82    }
83
84    fn read_reg(&mut self, reg: Registers) -> Result<u8, Tc72Error<SpiError, PinError>> {
85        let mut bytes = [reg as u8, 0];
86        self.cs.set_high().map_err(Tc72Error::Cs)?;
87        self.spi.transfer(&mut bytes)
88            .map_err(Tc72Error::Spi)?;
89        self.cs.set_low().map_err(Tc72Error::Cs)?;
90        Ok(bytes[1])
91    }
92}