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
// 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 Silicon Labs SI7021 temperature/humidity sensor.
//!
//! <https://www.silabs.com/products/sensors/humidity-sensors/Pages/si7013-20-21.aspx>
//!
//! > The Si7006/13/20/21/34 devices are Silicon Labs’ latest generation I2C
//! > relative humidity and temperature sensors. All members of this device
//! > family combine fully factory-calibrated humidity and temperature sensor
//! > elements with an analog to digital converter, signal processing and an I2C
//! > host interface. Patented use of industry-standard low-K polymer
//! > dielectrics provides excellent accuracy and long term stability, along
//! > with low drift and low hysteresis. The innovative CMOS design also offers
//! > the lowest power consumption in the industry for a relative humidity and
//! > temperature sensor. The Si7013/20/21/34 devices are designed for high-
//! > accuracy applications, while the Si7006 is targeted toward lower-accuracy
//! > applications that traditionally have used discrete RH/T sensors.
//!
//! Usage
//! -----
//!
//! ```rust,ignore
//! # use kernel::static_init;
//! # use capsules::virtual_alarm::VirtualMuxAlarm;
//!
//! let si7021_i2c = static_init!(
//!     capsules::virtual_i2c::I2CDevice,
//!     capsules::virtual_i2c::I2CDevice::new(i2c_bus, 0x40));
//! let si7021_virtual_alarm = static_init!(
//!     VirtualMuxAlarm<'static, sam4l::ast::Ast>,
//!     VirtualMuxAlarm::new(mux_alarm));
//! si7021_virtual_alarm.setup();
//!
//! let si7021 = static_init!(
//!     capsules::si7021::SI7021<'static, VirtualMuxAlarm<'static, sam4l::ast::Ast>>,
//!     capsules::si7021::SI7021::new(si7021_i2c,
//!         si7021_virtual_alarm,
//!         &mut capsules::si7021::BUFFER));
//! si7021_i2c.set_client(si7021);
//! si7021_virtual_alarm.set_client(si7021);
//! ```

use core::cell::Cell;
use kernel::hil::i2c;
use kernel::hil::time::{self, ConvertTicks};
use kernel::utilities::cells::{OptionalCell, TakeCell};
use kernel::ErrorCode;

#[allow(dead_code)]
enum Registers {
    MeasRelativeHumidityHoldMode = 0xe5,
    MeasRelativeHumidityNoHoldMode = 0xf5,
    MeasTemperatureHoldMode = 0xe3,
    MeasTemperatureNoHoldMode = 0xf3,
    ReadTemperaturePreviousRHMeasurement = 0xe0,
    Reset = 0xfe,
    WriteRHTUserRegister1 = 0xe6,
    ReadRHTUserRegister1 = 0xe7,
    WriteHeaterControlRegister = 0x51,
    ReadHeaterControlRegister = 0x11,
    ReadElectronicIdByteOneA = 0xfa,
    ReadElectronicIdByteOneB = 0x0f,
    ReadElectronicIdByteTwoA = 0xfc,
    ReadElectronicIdByteTwoB = 0xc9,
    ReadFirmwareVersionA = 0x84,
    ReadFirmwareVersionB = 0xb8,
}

/// States of the I2C protocol with the LPS331AP.
#[derive(Clone, Copy, PartialEq)]
enum State {
    Idle,
    WaitTemp,
    WaitRh,

    /// States to read the internal ID
    SelectElectronicId1,
    ReadElectronicId1,
    SelectElectronicId2,
    ReadElectronicId2,

    /// States to take the current measurement
    TakeTempMeasurementInit,
    TakeRhMeasurementInit,
    ReadRhMeasurement,
    ReadTempMeasurement,
    GotTempMeasurement,
    GotRhMeasurement,
}

#[derive(PartialEq, Eq, Copy, Clone)]
enum OnDeck {
    Nothing,
    Temperature,
    Humidity,
}

pub struct SI7021<'a, A: time::Alarm<'a>, I: i2c::I2CDevice> {
    i2c: &'a I,
    alarm: &'a A,
    temp_callback: OptionalCell<&'a dyn kernel::hil::sensors::TemperatureClient>,
    humidity_callback: OptionalCell<&'a dyn kernel::hil::sensors::HumidityClient>,
    state: Cell<State>,
    on_deck: Cell<OnDeck>,
    buffer: TakeCell<'static, [u8]>,
}

impl<'a, A: time::Alarm<'a>, I: i2c::I2CDevice> SI7021<'a, A, I> {
    pub fn new(i2c: &'a I, alarm: &'a A, buffer: &'static mut [u8]) -> SI7021<'a, A, I> {
        // setup and return struct
        SI7021 {
            i2c,
            alarm,
            temp_callback: OptionalCell::empty(),
            humidity_callback: OptionalCell::empty(),
            state: Cell::new(State::Idle),
            on_deck: Cell::new(OnDeck::Nothing),
            buffer: TakeCell::new(buffer),
        }
    }

    pub fn read_id(&self) {
        self.buffer.take().map(|buffer| {
            // turn on i2c to send commands
            self.i2c.enable();

            buffer[0] = Registers::ReadElectronicIdByteOneA as u8;
            buffer[1] = Registers::ReadElectronicIdByteOneB as u8;
            // TODO verify errors
            let _ = self.i2c.write(buffer, 2);
            self.state.set(State::SelectElectronicId1);
        });
    }

    fn init_measurement(&self, buffer: &'static mut [u8]) {
        let delay = self.alarm.ticks_from_ms(20);
        self.alarm.set_alarm(self.alarm.now(), delay);

        // Now wait for timer to expire
        self.buffer.replace(buffer);
        self.i2c.disable();
    }

    fn set_idle(&self, buffer: &'static mut [u8]) {
        self.buffer.replace(buffer);
        self.i2c.disable();
        self.state.set(State::Idle);
    }
}

impl<'a, A: time::Alarm<'a>, I: i2c::I2CDevice> i2c::I2CClient for SI7021<'a, A, I> {
    fn command_complete(&self, buffer: &'static mut [u8], _status: Result<(), i2c::Error>) {
        match self.state.get() {
            State::SelectElectronicId1 => {
                // TODO verify errors
                let _ = self.i2c.read(buffer, 8);
                self.state.set(State::ReadElectronicId1);
            }
            State::ReadElectronicId1 => {
                buffer[6] = buffer[0];
                buffer[7] = buffer[1];
                buffer[8] = buffer[2];
                buffer[9] = buffer[3];
                buffer[10] = buffer[4];
                buffer[11] = buffer[5];
                buffer[12] = buffer[6];
                buffer[13] = buffer[7];
                buffer[0] = Registers::ReadElectronicIdByteTwoA as u8;
                buffer[1] = Registers::ReadElectronicIdByteTwoB as u8;
                // TODO verify errors
                let _ = self.i2c.write(buffer, 2);
                self.state.set(State::SelectElectronicId2);
            }
            State::SelectElectronicId2 => {
                // TODO verify errors
                let _ = self.i2c.read(buffer, 6);
                self.state.set(State::ReadElectronicId2);
            }
            State::ReadElectronicId2 => {
                self.set_idle(buffer);
            }
            State::TakeTempMeasurementInit => {
                self.init_measurement(buffer);
                self.state.set(State::WaitTemp);
            }
            State::TakeRhMeasurementInit => {
                self.init_measurement(buffer);
                self.state.set(State::WaitRh);
            }
            State::ReadRhMeasurement => {
                // TODO verify errors
                let _ = self.i2c.read(buffer, 2);
                self.state.set(State::GotRhMeasurement);
            }
            State::ReadTempMeasurement => {
                // TODO verify errors
                let _ = self.i2c.read(buffer, 2);
                self.state.set(State::GotTempMeasurement);
            }
            State::GotTempMeasurement => {
                // Temperature in hundredths of degrees centigrade
                let temp_raw = ((buffer[0] as u32) << 8) | (buffer[1] as u32);
                let temp = ((temp_raw * 17572) / 65536) as i32 - 4685;

                self.temp_callback.map(|cb| cb.callback(Ok(temp)));

                match self.on_deck.get() {
                    OnDeck::Humidity => {
                        self.on_deck.set(OnDeck::Nothing);
                        buffer[0] = Registers::MeasRelativeHumidityNoHoldMode as u8;
                        // TODO verify errors
                        let _ = self.i2c.write(buffer, 1);
                        self.state.set(State::TakeRhMeasurementInit);
                    }
                    _ => {
                        self.set_idle(buffer);
                    }
                }
            }
            State::GotRhMeasurement => {
                // Humidity in hundredths of percent
                let humidity_raw = ((buffer[0] as u32) << 8) | (buffer[1] as u32);
                let humidity = (((humidity_raw * 125 * 100) / 65536) - 600) as u16;

                self.humidity_callback
                    .map(|cb| cb.callback(humidity as usize));
                match self.on_deck.get() {
                    OnDeck::Temperature => {
                        self.on_deck.set(OnDeck::Nothing);
                        buffer[0] = Registers::MeasTemperatureNoHoldMode as u8;
                        // TODO verify errors
                        let _ = self.i2c.write(buffer, 1);
                        self.state.set(State::TakeTempMeasurementInit);
                    }
                    _ => {
                        self.set_idle(buffer);
                    }
                }
            }
            _ => {}
        }
    }
}

impl<'a, A: time::Alarm<'a>, I: i2c::I2CDevice> kernel::hil::sensors::TemperatureDriver<'a>
    for SI7021<'a, A, I>
{
    fn read_temperature(&self) -> Result<(), ErrorCode> {
        // This chip handles both humidity and temperature measurements. We can
        // only start a new measurement if the chip is idle. If it isn't then we
        // can put this request "on deck" and it will happen after the
        // temperature measurement has finished.
        if self.state.get() == State::Idle {
            self.buffer.take().map_or(Err(ErrorCode::BUSY), |buffer| {
                // turn on i2c to send commands
                self.i2c.enable();

                buffer[0] = Registers::MeasTemperatureNoHoldMode as u8;
                // TODO verify errors
                let _ = self.i2c.write(buffer, 1);
                self.state.set(State::TakeTempMeasurementInit);
                Ok(())
            })
        } else {
            // Queue this request if nothing else queued.
            if self.on_deck.get() == OnDeck::Nothing {
                self.on_deck.set(OnDeck::Temperature);
                Ok(())
            } else {
                Err(ErrorCode::BUSY)
            }
        }
    }

    fn set_client(&self, client: &'a dyn kernel::hil::sensors::TemperatureClient) {
        self.temp_callback.set(client);
    }
}

impl<'a, A: time::Alarm<'a>, I: i2c::I2CDevice> kernel::hil::sensors::HumidityDriver<'a>
    for SI7021<'a, A, I>
{
    fn read_humidity(&self) -> Result<(), ErrorCode> {
        // This chip handles both humidity and temperature measurements. We can
        // only start a new measurement if the chip is idle. If it isn't then we
        // can put this request "on deck" and it will happen after the
        // temperature measurement has finished.
        if self.state.get() == State::Idle {
            self.buffer.take().map_or(Err(ErrorCode::BUSY), |buffer| {
                // turn on i2c to send commands
                self.i2c.enable();

                buffer[0] = Registers::MeasRelativeHumidityNoHoldMode as u8;
                // TODO verify errors
                let _ = self.i2c.write(buffer, 1);
                self.state.set(State::TakeRhMeasurementInit);
                Ok(())
            })
        } else {
            // Not idle, so queue this request if nothing else is queued. If we have already
            // queued a request return an error.
            if self.on_deck.get() == OnDeck::Nothing {
                self.on_deck.set(OnDeck::Humidity);
                Ok(())
            } else {
                Err(ErrorCode::BUSY)
            }
        }
    }

    fn set_client(&self, client: &'a dyn kernel::hil::sensors::HumidityClient) {
        self.humidity_callback.set(client);
    }
}

impl<'a, A: time::Alarm<'a>, I: i2c::I2CDevice> time::AlarmClient for SI7021<'a, A, I> {
    fn alarm(&self) {
        self.buffer.take().map(|buffer| {
            // turn on i2c to send commands
            self.i2c.enable();

            // TODO verify errors
            let _ = self.i2c.read(buffer, 2);
            match self.state.get() {
                State::WaitRh => self.state.set(State::ReadRhMeasurement),
                State::WaitTemp => self.state.set(State::ReadTempMeasurement),
                _ => (),
            }
        });
    }
}