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
//! This is a platform agnostic Rust driver for the LM75 temperature //! sensor and thermal watchdog, based on the [`embedded-hal`] traits. //! //! [`embedded-hal`]: https://github.com/rust-embedded/embedded-hal //! //! This driver allows you to: //! - Enable/disable the device. //! - Read the temperature. //! //! ## The device //! The LM75 temperature sensor includes a delta-sigma analog-to-digital //! converter, and a digital overtemperature detector. The host can //! query the LM75 through its I2C interface to read temperature at any //! time. The open-drain overtemperature output (OS) sinks current when //! the programmable temperature limit is exceeded. //! The OS output operates in either of two modes, comparator or //! interrupt. The host controls the temperature at which the alarm is //! asserted (TOS) and the hysteresis temperature below which the alarm //! condition is not valid (THYST). Also, the LM75's TOS and THYST //! registers can be read by the host. The address of the LM75 is set //! with three pins to allow multiple devices to work on the same bus. //! Power-up is in comparator mode, with defaults of TOS= +80ºC and //! THYST= +75ºC. The 3.0V to 5.5V supply voltage range, low supply //! current, and I2C interface make the LM75 ideal for many applications //! in thermal management and protection. //! //! Datasheet: //! - [LM75](https://datasheets.maximintegrated.com/en/ds/LM75.pdf) //! //! This driver is also compatible with LM75A, LM75B and LM75C: [LM75B/C Datasheet] //! //! [LM75B/C Datasheet]: http://www.ti.com/lit/ds/symlink/lm75b.pdf //! //! And also at least with the devices MAX7500, MAX6625, MAX6626, DS75LV, //! and DS7505. //! //! ## Usage examples (see also examples folder) //! //! ### Read temperature //! //! Import this crate and an `embedded_hal` implementation, then instantiate //! the device: //! //! ```no_run //! extern crate linux_embedded_hal as hal; //! extern crate lm75; //! //! use hal::I2cdev; //! use lm75::{ Lm75, SlaveAddr }; //! //! # fn main() { //! let dev = I2cdev::new("/dev/i2c-1").unwrap(); //! let address = SlaveAddr::default(); //! let mut sensor = Lm75::new(dev, address); //! let temperature = sensor.read_temperature().unwrap(); //! println!("Temperature: {}", temperature); //! # } //! ``` //! //! ### Provide an alternative address //! //! ```no_run //! extern crate linux_embedded_hal as hal; //! extern crate lm75; //! //! use hal::I2cdev; //! use lm75::{ Lm75, SlaveAddr }; //! //! # fn main() { //! let dev = I2cdev::new("/dev/i2c-1").unwrap(); //! let (a2, a1, a0) = (false, false, true); //! let address = SlaveAddr::Alternative(a2, a1, a0); //! let mut sensor = Lm75::new(dev, address); //! # } //! ``` //! //! ### Enable / disable the sensor //! //! ```no_run //! extern crate linux_embedded_hal as hal; //! extern crate lm75; //! //! use hal::I2cdev; //! use lm75::{ Lm75, SlaveAddr }; //! //! # fn main() { //! let dev = I2cdev::new("/dev/i2c-1").unwrap(); //! let mut sensor = Lm75::new(dev, SlaveAddr::default()); //! sensor.disable().unwrap(); // shutdown //! sensor.enable().unwrap(); //! # } //! ``` #![deny(unsafe_code)] #![deny(missing_docs)] #![no_std] extern crate embedded_hal as hal; use hal::blocking::i2c; /// All possible errors in this crate #[derive(Debug)] pub enum Error<E> { /// I²C bus error I2C(E), } /// Possible slave addresses #[derive(Debug, Clone)] pub enum SlaveAddr { /// Default slave address Default, /// Alternative slave address providing bit values for A2, A1 and A0 Alternative(bool, bool, bool) } impl Default for SlaveAddr { /// Default slave address fn default() -> Self { SlaveAddr::Default } } impl SlaveAddr { fn addr(self, default: u8) -> u8 { match self { SlaveAddr::Default => default, SlaveAddr::Alternative(a2, a1, a0) => default | ((a2 as u8) << 2) | ((a1 as u8) << 1) | a0 as u8 } } } const DEVICE_BASE_ADDRESS: u8 = 0b100_1000; struct Register; impl Register { const TEMPERATURE : u8 = 0x00; const CONFIGURATION : u8 = 0x01; } struct BitFlags; impl BitFlags { const SHUTDOWN : u8 = 0b0000_0001; } /// LM75 device driver. #[derive(Debug, Default)] pub struct Lm75<I2C> { /// The concrete I²C device implementation. i2c: I2C, /// The I²C device address. address: u8, /// Configuration register status. config: u8, } mod conversion; impl<I2C, E> Lm75<I2C> where I2C: i2c::Write<Error = E> { /// Create new instance of the LM75 device. pub fn new(i2c: I2C, address: SlaveAddr) -> Self { Lm75 { i2c, address: address.addr(DEVICE_BASE_ADDRESS), config: 0 } } /// Destroy driver instance, return I²C bus instance. pub fn destroy(self) -> I2C { self.i2c } /// Enable the sensor. pub fn enable(&mut self) -> Result<(), Error<E>> { let config = self.config; self.write_config(config & !BitFlags::SHUTDOWN) } /// Disable the sensor (shutdown). pub fn disable(&mut self) -> Result<(), Error<E>> { let config = self.config; self.write_config(config | BitFlags::SHUTDOWN) } fn write_config(&mut self, config: u8) -> Result<(), Error<E>> { self.i2c .write(self.address, &[Register::CONFIGURATION, config]) .map_err(Error::I2C)?; self.config = config; Ok(()) } } impl<I2C, E> Lm75<I2C> where I2C: i2c::WriteRead<Error = E> { /// Read the temperature from the sensor. pub fn read_temperature(&mut self) -> Result<f32, Error<E>> { let mut data = [0; 2]; self.i2c .write_read(self.address, &[Register::TEMPERATURE], &mut data) .map_err(Error::I2C)?; Ok(conversion::convert_temp_from_register(data[0], data[1])) } } #[cfg(test)] mod tests { use super::*; #[test] fn can_get_default_address() { let addr = SlaveAddr::default(); assert_eq!(DEVICE_BASE_ADDRESS, addr.addr(DEVICE_BASE_ADDRESS)); } #[test] fn can_generate_alternative_addresses() { assert_eq!(0b100_1000, SlaveAddr::Alternative(false, false, false).addr(DEVICE_BASE_ADDRESS)); assert_eq!(0b100_1001, SlaveAddr::Alternative(false, false, true).addr(DEVICE_BASE_ADDRESS)); assert_eq!(0b100_1010, SlaveAddr::Alternative(false, true, false).addr(DEVICE_BASE_ADDRESS)); assert_eq!(0b100_1100, SlaveAddr::Alternative( true, false, false).addr(DEVICE_BASE_ADDRESS)); assert_eq!(0b100_1111, SlaveAddr::Alternative( true, true, true).addr(DEVICE_BASE_ADDRESS)); } }