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
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;
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;
#[derive(Clone, Copy, PartialEq)]
enum ProtocolState {
Idle,
Configure,
Deconfigure(Option<f32>),
SetRegSensorVoltage,
ReadingSensorVoltage,
SetRegDieTemperature(SensorVoltage),
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> {
pub fn new(
i2c: &'a i2c::I2CDevice,
interrupt_pin: &'a Pin,
buffer: &'static mut [u8],
) -> TMP006<'a> {
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) {
self.buffer.take().map(|buf| {
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>) {
self.buffer.take().map(|buf| {
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) {
self.interrupt_pin.make_input();
self.interrupt_pin
.enable_interrupt(0, InterruptMode::FallingEdge);
}
fn disable_interrupts(&self) {
self.interrupt_pin.disable_interrupt();
self.interrupt_pin.disable();
}
}
fn calculate_temperature(sensor_voltage: i16, die_temperature: i16) -> f32 {
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;
t_celsius
}
impl<'a> i2c::I2CClient for TMP006<'a> {
fn command_complete(&self, buffer: &'static mut [u8], _error: i2c::Error) {
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 => {
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;
buffer[0] = Registers::DieTemperature as u8;
self.i2c.write(buffer, 1);
self.protocol_state
.set(ProtocolState::SetRegDieTemperature(sensor_voltage));
}
ProtocolState::SetRegDieTemperature(sensor_voltage) => {
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);
if self.repeated_mode.get() == false {
self.disable_sensor(Some(temp_val));
} else {
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| {
self.i2c.enable();
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 {
0 => {
self.repeated_mode.set(false);
self.callback.set(Some(callback));
self.enable_sensor(MAX_SAMPLING_RATE);
ReturnCode::SUCCESS
}
1 => {
self.repeated_mode.set(true);
self.callback.set(Some(callback));
self.enable_sensor(self.sampling_period.get());
ReturnCode::SUCCESS
}
_ => ReturnCode::ENOSUPPORT,
}
}
fn command(&self, command_num: usize, data: usize, _: usize, _: AppId) -> ReturnCode {
match command_num {
0 => ReturnCode::SUCCESS,
1 => {
if (data & 0xFFFFFFF8) != 0 {
return ReturnCode::EINVAL;
}
self.sampling_period.set((data & 0x7) as u8);
ReturnCode::SUCCESS
}
2 => {
self.callback.set(None);
self.disable_sensor(None);
ReturnCode::SUCCESS
}
_ => ReturnCode::ENOSUPPORT,
}
}
}