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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.

//! SyscallDriver for the STMicro HTS221 relative humidity and temperature
//! sensor using the I2C bus.
//!
//! <https://www.st.com/en/mems-and-sensors/hts221.html>
//!
//! > The HTS221 is an ultra-compact sensor for relative humidity and
//! > temperature. It includes a sensing element and a mixed signal ASIC
//! > to provide the measurement information through digital serial
//! > interfaces. The sensing element consists of a polymer dielectric
//! > planar capacitor structure capable of detecting relative humidity
//! > variations and is manufactured using a dedicated ST process.
//!
//! Driver Semantics
//! ----------------
//!
//! This driver exposes the HTS221's temperature and humidity functionality via
//! the [TemperatureDriver] and [HumidityDriver] HIL interfaces. The driver
//! _does not_ attempt to support multiple concurrent requests for temperature
//! or multiple concurrent requests for humidity, but _does_ support a
//! concurrent request each for temperature and humidity. It is not the role of
//! this driver to provide virtualization to multiple clients, but it does
//! provide virtualization to allow it to be used from both a temperature and
//! humidity driver.
//!
//! Specifically, the implementation _always_ reads both temperature and
//! humidity (the chip always provides both anyway). If the driver receives a
//! request for either temperature or humidity while a request for the other is
//! outstanding, both will be returned to their respective clients when the I2C
//! transaction is completed, rather than performing two separate transactions.
//!
//! Polling for data readiness
//! --------------------------
//!
//! The HTS221 has a data-ready line that can provide an interrupt when
//! temperature and humidity data is ready. However, the primary board (the Nano
//! 33 BLE Sense) this driver was developed for does not connect that line to
//! the MCU and, typically, data is ready within a couple read/write I2C
//! transactions. So, the driver **polls** the status register instead. This is
//! probably not optimal from an energy perspective, so should a use case for an
//! interrupt driven interface arise, some brave soul should modify the driver
//! to support both.
//!
//! Limitations
//! -----------
//!
//! The driver uses floating point math to adjust readings based on the
//! calibration registers. This is accurate and matches the chip's datasheet's
//! recommendation, but could increase code size significantly in platforms that
//! do not have hardware support for floating point operations.
//!
//! Usage
//! -----
//!
//! ```rust,ignore
//! # use kernel::static_init;
//!
//! let hts221_i2c = static_init!(
//!     capsules::virtual_i2c::I2CDevice,
//!     capsules::virtual_i2c::I2CDevice::new(i2c_bus, 0x5f));
//! let hts221 = static_init!(
//!     capsules::hts221::Hts221<'static>,
//!     capsules::hts221::Hts221::new(hts221_i2c,
//!         &mut capsules::hts221::BUFFER));
//! hts221_i2c.set_client(hts221);
//! ```

use core::cell::Cell;
use kernel::hil::i2c::{self, I2CClient, I2CDevice};
use kernel::hil::sensors::{HumidityClient, HumidityDriver, TemperatureClient, TemperatureDriver};
use kernel::utilities::cells::{OptionalCell, TakeCell};
use kernel::ErrorCode;

const REG_AUTO_INCREMENT: u8 = 1 << 7;
const CTRL_REG1: u8 = 0x20;
const STATUS_REG: u8 = 0x27;
const HUMID0_REG: u8 = 0x28;
const CALIB_REG_1ST: u8 = 0x30;

#[derive(Copy, Clone, Debug)]
struct CalibrationData {
    temp_slope: f32,
    temp_intercept: f32,
    humidity_slope: f32,
    humidity_intercept: f32,
}

pub struct Hts221<'a, I: I2CDevice> {
    buffer: TakeCell<'static, [u8]>,
    i2c: &'a I,
    temperature_client: OptionalCell<&'a dyn TemperatureClient>,
    humidity_client: OptionalCell<&'a dyn HumidityClient>,
    state: Cell<State>,
    pending_temperature: Cell<bool>,
    pending_humidity: Cell<bool>,
}

impl<'a, I: I2CDevice> Hts221<'a, I> {
    pub fn new(i2c: &'a I, buffer: &'static mut [u8]) -> Self {
        Hts221 {
            buffer: TakeCell::new(buffer),
            i2c,
            temperature_client: OptionalCell::empty(),
            humidity_client: OptionalCell::empty(),
            state: Cell::new(State::Reset),
            pending_temperature: Cell::new(false),
            pending_humidity: Cell::new(false),
        }
    }

    // Helper method to kick off a reading for both temperature and humidity.
    //
    // There are three cases:
    //   1. There is no calibration data available yet ([State::Reset])
    //   2. There is calibration data already ([State::Idle])
    //   3. There is a reading already taking place
    fn start_reading(&self) -> Result<(), ErrorCode> {
        self.buffer
            .take()
            .map(|buffer| {
                self.i2c.enable();
                match self.state.get() {
                    State::Reset => {
                        buffer[0] = REG_AUTO_INCREMENT | CALIB_REG_1ST;

                        if let Err((_error, buffer)) = self.i2c.write_read(buffer, 1, 16) {
                            self.buffer.replace(buffer);
                            self.i2c.disable();
                        } else {
                            self.state.set(State::Calibrating);
                        }
                    }
                    State::Idle(calibration_data, _, _) => {
                        buffer[0] = REG_AUTO_INCREMENT | CTRL_REG1;
                        buffer[1] = 1 << 2 | 1 << 7; // BDU + PD
                        buffer[2] = 1; // ONE SHOT

                        if let Err((_error, buffer)) = self.i2c.write(buffer, 3) {
                            self.buffer.replace(buffer);
                            self.i2c.disable();
                        } else {
                            self.state.set(State::InitiateReading(calibration_data));
                        }
                    }
                    _ => {} // Should really never happen since we only have `buffer` available in the above two states
                }
            })
            .ok_or(ErrorCode::BUSY)
    }
}

impl<'a, I: I2CDevice> TemperatureDriver<'a> for Hts221<'a, I> {
    fn set_client(&self, client: &'a dyn TemperatureClient) {
        self.temperature_client.set(client);
    }

    fn read_temperature(&self) -> Result<(), ErrorCode> {
        self.pending_temperature.set(true);
        if !self.pending_humidity.get() {
            self.start_reading()
        } else {
            Ok(())
        }
    }
}

impl<'a, I: I2CDevice> HumidityDriver<'a> for Hts221<'a, I> {
    fn set_client(&self, client: &'a dyn HumidityClient) {
        self.humidity_client.set(client);
    }

    fn read_humidity(&self) -> Result<(), ErrorCode> {
        self.pending_humidity.set(true);
        if !self.pending_temperature.get() {
            self.start_reading()
        } else {
            Ok(())
        }
    }
}

#[derive(Clone, Copy, Debug)]
enum State {
    Reset,
    Calibrating,
    InitiateReading(CalibrationData),
    CheckStatus(CalibrationData),
    Read(CalibrationData),
    Idle(CalibrationData, i32, usize),
}

impl<'a, I: I2CDevice> I2CClient for Hts221<'a, I> {
    fn command_complete(&self, buffer: &'static mut [u8], status: Result<(), i2c::Error>) {
        if let Err(i2c_err) = status {
            self.state.set(State::Idle(
                CalibrationData {
                    temp_slope: 0.0,
                    temp_intercept: 0.0,
                    humidity_slope: 0.0,
                    humidity_intercept: 0.0,
                },
                0,
                0,
            ));
            self.buffer.replace(buffer);
            self.temperature_client
                .map(|client| client.callback(Err(i2c_err.into())));
            self.humidity_client.map(|client| client.callback(0));
            return;
        }

        match self.state.get() {
            State::Calibrating => {
                let h0rh = buffer[0] as f32;
                let h1rh = buffer[1] as f32;
                let h0t0out = ((buffer[6] as i16) | ((buffer[7] as i16) << 8)) as f32;
                let h1t0out = ((buffer[10] as i16) | ((buffer[11] as i16) << 8)) as f32;

                let humidity_slope = (h1rh - h0rh) / (2.0 * (h1t0out - h0t0out));
                let humidity_intercept = (h0rh / 2.0) - humidity_slope * h0t0out;

                let t0deg_c = ((buffer[2] as i16) | (((buffer[5] & 0b11) as i16) << 8)) as f32;
                let t1deg_c = ((buffer[3] as i16) | (((buffer[5] & 0b1100) as i16) << 6)) as f32;

                let t0out = ((buffer[12] as i16) | ((buffer[13] as i16) << 8)) as f32;
                let t1out = ((buffer[14] as i16) | ((buffer[15] as i16) << 8)) as f32;

                let temp_slope = (t1deg_c - t0deg_c) / (8.0 * (t1out - t0out));
                let temp_intercept = (t0deg_c / 8.0) - temp_slope * t0out;

                buffer[0] = REG_AUTO_INCREMENT | CTRL_REG1;
                buffer[1] = 1 << 2 | 1 << 7; // BDU + PD
                buffer[2] = 1; // ONE SHOT

                if let Err((error, buffer)) = self.i2c.write(buffer, 3) {
                    self.state.set(State::Idle(
                        CalibrationData {
                            temp_slope: 0.0,
                            temp_intercept: 0.0,
                            humidity_slope: 0.0,
                            humidity_intercept: 0.0,
                        },
                        0,
                        0,
                    ));
                    self.buffer.replace(buffer);
                    self.temperature_client
                        .map(|client| client.callback(Err(error.into())));
                    self.humidity_client.map(|client| client.callback(0));
                } else {
                    self.state.set(State::InitiateReading(CalibrationData {
                        temp_slope,
                        temp_intercept,
                        humidity_slope,
                        humidity_intercept,
                    }));
                }
            }
            State::InitiateReading(calibration_data) => {
                buffer[0] = STATUS_REG;

                if let Err((error, buffer)) = self.i2c.write_read(buffer, 1, 1) {
                    self.state.set(State::Idle(
                        CalibrationData {
                            temp_slope: 0.0,
                            temp_intercept: 0.0,
                            humidity_slope: 0.0,
                            humidity_intercept: 0.0,
                        },
                        0,
                        0,
                    ));
                    self.buffer.replace(buffer);
                    self.temperature_client
                        .map(|client| client.callback(Err(error.into())));
                    self.humidity_client.map(|client| client.callback(0));
                } else {
                    self.state.set(State::CheckStatus(calibration_data));
                }
            }
            State::CheckStatus(calibration_data) => {
                if buffer[0] & 0b11 == 0b11 {
                    buffer[0] = REG_AUTO_INCREMENT | HUMID0_REG;

                    if let Err((error, buffer)) = self.i2c.write_read(buffer, 1, 4) {
                        self.state.set(State::Idle(
                            CalibrationData {
                                temp_slope: 0.0,
                                temp_intercept: 0.0,
                                humidity_slope: 0.0,
                                humidity_intercept: 0.0,
                            },
                            0,
                            0,
                        ));
                        self.buffer.replace(buffer);
                        self.temperature_client
                            .map(|client| client.callback(Err(error.into())));
                        self.humidity_client.map(|client| client.callback(0));
                    } else {
                        self.state.set(State::Read(calibration_data));
                    }
                } else {
                    buffer[0] = STATUS_REG;

                    if let Err((error, buffer)) = self.i2c.write_read(buffer, 1, 1) {
                        self.state.set(State::Idle(
                            CalibrationData {
                                temp_slope: 0.0,
                                temp_intercept: 0.0,
                                humidity_slope: 0.0,
                                humidity_intercept: 0.0,
                            },
                            0,
                            0,
                        ));
                        self.buffer.replace(buffer);
                        self.temperature_client
                            .map(|client| client.callback(Err(error.into())));
                        self.humidity_client.map(|client| client.callback(0));
                    }
                }
            }
            State::Read(calibration_data) => {
                let humidity_raw = ((buffer[0] as i16) | ((buffer[1] as i16) << 8)) as f32;
                let humidity = ((humidity_raw * calibration_data.humidity_slope
                    + calibration_data.humidity_intercept)
                    * 100.0) as usize;

                let temperature_raw = ((buffer[2] as i16) | ((buffer[3] as i16) << 8)) as f32;
                let temperature = ((temperature_raw * calibration_data.temp_slope
                    + calibration_data.temp_intercept)
                    * 100.0) as i32;
                buffer[0] = CTRL_REG1;
                // TODO(alevy): this is a workaround for a bug. We should be able to turn
                // off the the sensor between transactions, and turn it back on (as is done
                // in [start_reading]), but doing so seems not to work and the sensor's
                // Status register never updates to read after the first transaction. For
                // now, leave it on and waste 2uA.
                buffer[1] = 1 << 7; // Leave PD bit on

                if let Err((error, buffer)) = self.i2c.write(buffer, 2) {
                    self.state.set(State::Idle(
                        CalibrationData {
                            temp_slope: 0.0,
                            temp_intercept: 0.0,
                            humidity_slope: 0.0,
                            humidity_intercept: 0.0,
                        },
                        0,
                        0,
                    ));
                    self.buffer.replace(buffer);
                    self.temperature_client
                        .map(|client| client.callback(Err(error.into())));
                    self.humidity_client.map(|client| client.callback(0));
                } else {
                    self.state
                        .set(State::Idle(calibration_data, temperature, humidity));
                }
            }
            State::Idle(_, temperature, humidity) => {
                self.buffer.replace(buffer);
                self.i2c.disable();
                if self.pending_temperature.get() {
                    self.pending_temperature.set(false);
                    self.temperature_client
                        .map(|client| client.callback(Ok(temperature)));
                }
                if self.pending_humidity.get() {
                    self.pending_humidity.set(false);
                    self.humidity_client.map(|client| client.callback(humidity));
                }
            }
            State::Reset => {} // should never happen
        }
    }
}