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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
//! Driver for the TI TMP006 infrared thermopile contactless temperature sensor.
//!
//! <http://www.ti.com/product/TMP006>
//!
//! > The TMP006 and TMP006B are fully integrated MEMs thermopile sensors that
//! > measure the temperature of an object without having to be in direct
//! > contact. The thermopile absorbs passive infrared energy from an object at
//! > wavelengths between 4 um to 16 um within the end-user defined field of
//! > view.

use core::cell::Cell;
use kernel::{AppId, Callback, Driver, ReturnCode};
use kernel::common::math::{get_errno, sqrtf32};
use kernel::common::take_cell::TakeCell;
use kernel::hil::gpio::{Client, InterruptMode, Pin};
use kernel::hil::i2c;

pub static mut BUFFER: [u8; 3] = [0; 3];

const MAX_SAMPLING_RATE: u8 = 0x0;
const DEFAULT_SAMPLING_RATE: u8 = 0x02;

// temperature calculation constants
//  From TMP006 User's Guide section 5.1
//  S_0 should be determined from calibration and ranges from 5E-14 to 7E-14
//  We have selected 5E-14 experimentally
const S_0: f32 = 5E-14;
const A_1: f32 = 1.75E-3;
const A_2: f32 = -1.678E-5;
const T_REF: f32 = 298.15;
const B_0: f32 = -2.94E-5;
const B_1: f32 = -5.7E-7;
const B_2: f32 = 4.63E-9;
const C_2: f32 = 13.4;
const K_TO_C: f32 = -273.15;
const C_TO_K: f32 = 273.15;
const NV_TO_V: f32 = 1E9;
const T_DIE_CONVERT: f32 = 0.03125;
const V_OBJ_CONVERT: f32 = 156.25;

#[allow(dead_code)]
enum Registers {
    SensorVoltage = 0x00,
    DieTemperature = 0x01,
    Configuration = 0x02,
    ManufacturerID = 0xFE,
    DeviceID = 0xFF,
}

type SensorVoltage = i16;

/// States of the I2C protocol with the TMP006. There are three sequences:
///
/// ### Enable sensor
///
/// Configure -> ()
///
/// ### Disable sensor
///
/// Disconfigure -> ()
///
/// ### Read temperature
///
/// SetRegSensorVoltage -->
///     ReadingSensorVoltage --(voltage)->
///         SetRegDieTemperature(voltage) --(voltage)->
///             ReadingDieTemperature --(unless repeated_mode)->
///                 Disconfigure
#[derive(Clone, Copy, PartialEq)]
enum ProtocolState {
    Idle,

    /// Enable sensor by setting the configuration register.
    Configure,

    /// Disable sensor by setting the configuration register. Optionally contains the most recent
    /// temperature to give back to callbacks.
    Deconfigure(Option<f32>),

    /// Set the active register to sensor voltage.
    SetRegSensorVoltage,

    /// Read the sensor voltage register.
    ReadingSensorVoltage,

    /// Set the active register to die temperature, carrying over the sensor
    /// voltage reading.
    SetRegDieTemperature(SensorVoltage),

    /// Read the die temperature register, carrying over the sensor voltage
    /// reading.
    ReadingDieTemperature(SensorVoltage),
}

pub struct TMP006<'a> {
    i2c: &'a i2c::I2CDevice,
    interrupt_pin: &'a Pin,
    sampling_period: Cell<u8>,
    repeated_mode: Cell<bool>,
    callback: Cell<Option<Callback>>,
    protocol_state: Cell<ProtocolState>,
    buffer: TakeCell<'static, [u8]>,
}

impl<'a> TMP006<'a> {
    /// The `interrupt_pin` must be pulled-up since the TMP006 is open-drain.
    pub fn new(
        i2c: &'a i2c::I2CDevice,
        interrupt_pin: &'a Pin,
        buffer: &'static mut [u8],
    ) -> TMP006<'a> {
        // setup and return struct
        TMP006 {
            i2c: i2c,
            interrupt_pin: interrupt_pin,
            sampling_period: Cell::new(DEFAULT_SAMPLING_RATE),
            repeated_mode: Cell::new(false),
            callback: Cell::new(None),
            protocol_state: Cell::new(ProtocolState::Idle),
            buffer: TakeCell::new(buffer),
        }
    }

    fn enable_sensor(&self, sampling_period: u8) {
        // enable and configure TMP006
        self.buffer.take().map(|buf| {
            // turn on i2c to send commands
            self.i2c.enable();

            let config = 0x7100 | (((sampling_period & 0x7) as u16) << 9);
            buf[0] = Registers::Configuration as u8;
            buf[1] = ((config & 0xFF00) >> 8) as u8;
            buf[2] = (config & 0x00FF) as u8;
            self.i2c.write(buf, 3);
            self.protocol_state.set(ProtocolState::Configure);
        });
    }

    fn disable_sensor(&self, temperature: Option<f32>) {
        // disable the TMP006
        self.buffer.take().map(|buf| {
            // turn on i2c to send commands
            self.i2c.enable();

            let config = 0x0000;
            buf[0] = Registers::Configuration as u8;
            buf[1] = ((config & 0xFF00) >> 8) as u8;
            buf[2] = (config & 0x00FF) as u8;
            self.i2c.write(buf, 3);
            self.protocol_state
                .set(ProtocolState::Deconfigure(temperature));
        });
    }

    fn enable_interrupts(&self) {
        // setup interrupts from the sensor
        self.interrupt_pin.make_input();
        self.interrupt_pin
            .enable_interrupt(0, InterruptMode::FallingEdge);
    }

    fn disable_interrupts(&self) {
        // disable interrupts from the sensor
        self.interrupt_pin.disable_interrupt();
        self.interrupt_pin.disable();
    }
}

fn calculate_temperature(sensor_voltage: i16, die_temperature: i16) -> f32 {
    // do calculation of actual temperature
    //  Calculations based on TMP006 User's Guide section 5.1
    let t_die = ((die_temperature >> 2) as f32) * T_DIE_CONVERT + C_TO_K;
    let t_adj = t_die - T_REF;
    let s = S_0 * (1.0 + A_1 * t_adj + A_2 * t_adj * t_adj);

    let v_obj = (sensor_voltage as f32) * V_OBJ_CONVERT / NV_TO_V;
    let v_os = B_0 + B_1 * t_adj + B_2 * t_adj * t_adj;

    let v_adj = v_obj - v_os;
    let f_v_obj = v_adj + C_2 * v_adj * v_adj;

    let t_kelvin = sqrtf32(sqrtf32(t_die * t_die * t_die * t_die + (f_v_obj / s)));
    let t_celsius = t_kelvin + K_TO_C;

    // return data value
    t_celsius
}

impl<'a> i2c::I2CClient for TMP006<'a> {
    fn command_complete(&self, buffer: &'static mut [u8], _error: i2c::Error) {
        // TODO(alevy): handle protocol errors
        match self.protocol_state.get() {
            ProtocolState::Configure => {
                self.buffer.replace(buffer);
                self.enable_interrupts();
                self.i2c.disable();
                self.protocol_state.set(ProtocolState::Idle);
            }
            ProtocolState::Deconfigure(temperature) => {
                self.buffer.replace(buffer);
                self.disable_interrupts();
                self.i2c.disable();
                self.protocol_state.set(ProtocolState::Idle);
                temperature.map(|temp_val| {
                    self.callback
                        .get()
                        .map(|mut cb| cb.schedule(temp_val as usize, get_errno() as usize, 0));
                    self.callback.set(None);
                });
            }
            ProtocolState::SetRegSensorVoltage => {
                // Read sensor voltage register
                self.i2c.read(buffer, 2);
                self.protocol_state.set(ProtocolState::ReadingSensorVoltage);
            }
            ProtocolState::ReadingSensorVoltage => {
                let sensor_voltage = (((buffer[0] as u16) << 8) | buffer[1] as u16) as i16;

                // Select die temperature register
                buffer[0] = Registers::DieTemperature as u8;
                self.i2c.write(buffer, 1);

                self.protocol_state
                    .set(ProtocolState::SetRegDieTemperature(sensor_voltage));
            }
            ProtocolState::SetRegDieTemperature(sensor_voltage) => {
                // Read die temperature register
                self.i2c.read(buffer, 2);
                self.protocol_state
                    .set(ProtocolState::ReadingDieTemperature(sensor_voltage));
            }
            ProtocolState::ReadingDieTemperature(sensor_voltage) => {
                let die_temperature = (((buffer[0] as u16) << 8) | buffer[1] as u16) as i16;
                self.buffer.replace(buffer);

                let temp_val = calculate_temperature(sensor_voltage, die_temperature);

                // disable callback and sensing if in single-shot mode
                if self.repeated_mode.get() == false {
                    // disable temperature sensor. When disabling is finished, we will give the
                    // temperature to the callback.
                    self.disable_sensor(Some(temp_val));
                } else {
                    // send value to callback
                    self.callback
                        .get()
                        .map(|mut cb| cb.schedule(temp_val as usize, get_errno() as usize, 0));

                    self.i2c.disable();
                }
            }
            _ => {}
        }
    }
}

impl<'a> Client for TMP006<'a> {
    fn fired(&self, _: usize) {
        self.buffer.take().map(|buf| {
            // turn on i2c to send commands
            self.i2c.enable();

            // select sensor voltage register and read it
            buf[0] = Registers::SensorVoltage as u8;
            self.i2c.write(buf, 1);
            self.protocol_state.set(ProtocolState::SetRegSensorVoltage);
        });
    }
}

impl<'a> Driver for TMP006<'a> {
    fn subscribe(&self, subscribe_num: usize, callback: Callback) -> ReturnCode {
        match subscribe_num {
            // single temperature reading with callback
            0 => {
                // single sample mode
                self.repeated_mode.set(false);

                // set callback function
                self.callback.set(Some(callback));

                // enable sensor
                //  turn up the sampling rate so we get the sample faster
                self.enable_sensor(MAX_SAMPLING_RATE);

                ReturnCode::SUCCESS
            }

            // periodic temperature reading subscription
            1 => {
                // periodic sampling mode
                self.repeated_mode.set(true);

                // set callback function
                self.callback.set(Some(callback));

                // enable temperature sensor
                self.enable_sensor(self.sampling_period.get());

                ReturnCode::SUCCESS
            }

            // default
            _ => ReturnCode::ENOSUPPORT,
        }
    }

    fn command(&self, command_num: usize, data: usize, _: usize, _: AppId) -> ReturnCode {
        match command_num {
            0 /* check if present */ => ReturnCode::SUCCESS,
            // set period for sensing
            1 => {
                // bounds check on the period
                if (data & 0xFFFFFFF8) != 0 {
                    return ReturnCode::EINVAL;
                }

                // set period value
                self.sampling_period.set((data & 0x7) as u8);

                ReturnCode::SUCCESS
            }

            // unsubscribe callback
            2 => {
                // clear callback function
                self.callback.set(None);

                // disable temperature sensor
                self.disable_sensor(None);

                ReturnCode::SUCCESS
            }

            // default
            _ => ReturnCode::ENOSUPPORT,
        }
    }
}