capsules_extra/isl29035.rs
1// Licensed under the Apache License, Version 2.0 or the MIT License.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright Tock Contributors 2022.
4
5//! SyscallDriver for the ISL29035 digital light sensor.
6//!
7//! <http://bit.ly/2rA00cH>
8//!
9//! > The ISL29035 is an integrated ambient and infrared light-to-digital
10//! > converter with I2C (SMBus compatible) Interface. Its advanced self-
11//! > calibrated photodiode array emulates human eye response with excellent IR
12//! > rejection. The on-chip ADC is capable of rejecting 50Hz and 60Hz flicker
13//! > caused by artificial light sources. The Lux range select feature allows
14//! > users to program the Lux range for optimized counts/Lux.
15//!
16//! Usage
17//! -----
18//!
19//! ```rust,ignore
20//! # use kernel::static_init;
21//! # use capsules::virtual_alarm::VirtualMuxAlarm;
22//!
23//! let isl29035_i2c = static_init!(I2CDevice, I2CDevice::new(i2c_bus, 0x44));
24//! let isl29035_virtual_alarm = static_init!(
25//! VirtualMuxAlarm<'static, sam4l::ast::Ast>,
26//! VirtualMuxAlarm::new(mux_alarm));
27//! isl29035_virtual_alarm.setup();
28//!
29//! let isl29035 = static_init!(
30//! capsules::isl29035::Isl29035<'static, VirtualMuxAlarm<'static, sam4l::ast::Ast>>,
31//! capsules::isl29035::Isl29035::new(isl29035_i2c, isl29035_virtual_alarm,
32//! &mut capsules::isl29035::BUF));
33//! isl29035_i2c.set_client(isl29035);
34//! isl29035_virtual_alarm.set_client(isl29035);
35//! ```
36
37use core::cell::Cell;
38use kernel::ErrorCode;
39use kernel::hil::i2c::{Error, I2CClient, I2CDevice};
40use kernel::hil::sensors::{AmbientLight, AmbientLightClient};
41use kernel::hil::time::{self, ConvertTicks};
42use kernel::utilities::cells::{OptionalCell, TakeCell};
43
44/// Recommended buffer length.
45pub const BUF_LEN: usize = 3;
46
47#[derive(Copy, Clone, PartialEq)]
48enum State {
49 Disabled,
50 Enabling,
51 Integrating,
52 ReadingLI,
53 Disabling(usize),
54}
55
56pub struct Isl29035<'a, A: time::Alarm<'a>> {
57 i2c: &'a dyn I2CDevice<'a>,
58 alarm: &'a A,
59 state: Cell<State>,
60 buffer: TakeCell<'static, [u8]>,
61 client: OptionalCell<&'a dyn AmbientLightClient>,
62}
63
64impl<'a, A: time::Alarm<'a>> Isl29035<'a, A> {
65 pub fn new(
66 i2c: &'a dyn I2CDevice<'a>,
67 alarm: &'a A,
68 buffer: &'static mut [u8],
69 ) -> Isl29035<'a, A> {
70 Isl29035 {
71 i2c,
72 alarm,
73 state: Cell::new(State::Disabled),
74 buffer: TakeCell::new(buffer),
75 client: OptionalCell::empty(),
76 }
77 }
78
79 pub fn start_read_lux(&self) -> Result<(), ErrorCode> {
80 if self.state.get() == State::Disabled {
81 self.buffer.take().map_or(Err(ErrorCode::NOMEM), |buf| {
82 self.i2c.enable();
83 buf[0] = 0;
84 // CMD 1 Register:
85 // Interrupt persist for 1 integration cycle (bits 0 & 1)
86 // Measure ALS continuously (buts 5,6 & 7)
87 // Bit 2 is the interrupt bit
88 // Bits 3 & 4 are reserved
89 buf[1] = 0b10100000;
90
91 // CMD 2 Register:
92 // Range 4000 (bits 0, 1)
93 // ADC resolution 8-bit (bits 2,3)
94 // Other bits are reserved
95 buf[2] = 0b00001001;
96
97 if let Err((error, buf)) = self.i2c.write(buf, 3) {
98 self.buffer.replace(buf);
99 self.i2c.disable();
100 Err(error.into())
101 } else {
102 self.state.set(State::Enabling);
103 Ok(())
104 }
105 })
106 } else {
107 Err(ErrorCode::BUSY)
108 }
109 }
110}
111
112impl<'a, A: time::Alarm<'a>> AmbientLight<'a> for Isl29035<'a, A> {
113 fn set_client(&self, client: &'a dyn AmbientLightClient) {
114 self.client.set(client)
115 }
116
117 fn read_light_intensity(&self) -> Result<(), ErrorCode> {
118 self.start_read_lux()
119 }
120}
121
122impl<'a, A: time::Alarm<'a>> time::AlarmClient for Isl29035<'a, A> {
123 fn alarm(&self) {
124 self.buffer.take().map(|buffer| {
125 // Turn on i2c to send commands.
126 self.i2c.enable();
127
128 buffer[0] = 0x02_u8;
129 if let Err((_error, buf)) = self.i2c.write_read(buffer, 1, 2) {
130 self.buffer.replace(buf);
131 self.i2c.disable();
132 self.state.set(State::Disabled);
133 self.client.map(|client| client.callback(0));
134 } else {
135 self.state.set(State::ReadingLI);
136 }
137 });
138 }
139}
140
141impl<'a, A: time::Alarm<'a>> I2CClient for Isl29035<'a, A> {
142 fn command_complete(&self, buffer: &'static mut [u8], status: Result<(), Error>) {
143 if status.is_err() {
144 self.state.set(State::Disabled);
145 self.buffer.replace(buffer);
146 self.client.map(|client| client.callback(0));
147 return;
148 }
149 match self.state.get() {
150 State::Enabling => {
151 // Set a timer to wait for the conversion to be done.
152 // For 8 bits, thats 410 us (per Table 11 in the datasheet).
153 let interval = self.alarm.ticks_from_us(410);
154 self.alarm.set_alarm(self.alarm.now(), interval);
155
156 // Now wait for timer to expire
157 self.buffer.replace(buffer);
158 self.i2c.disable();
159 self.state.set(State::Integrating);
160 }
161 State::ReadingLI => {
162 // During configuration we set the ADC resolution to 8 bits and
163 // the range to 4000.
164 //
165 // Since it's only 8 bits, we ignore the second byte of output.
166 //
167 // For a given Range and n (-bits of ADC resolution):
168 // Lux = Data * (Range / 2^n)
169 let data = buffer[0] as usize; //((buffer[1] as usize) << 8) | buffer[0] as usize;
170 let lux = (data * 4000) >> 8;
171
172 buffer[0] = 0;
173
174 if let Err((_error, buffer)) = self.i2c.write(buffer, 2) {
175 self.state.set(State::Disabled);
176 self.buffer.replace(buffer);
177 self.client.map(|client| client.callback(0));
178 } else {
179 self.state.set(State::Disabling(lux));
180 }
181 }
182 State::Disabling(lux) => {
183 self.i2c.disable();
184 self.state.set(State::Disabled);
185 self.buffer.replace(buffer);
186 self.client.map(|client| client.callback(lux));
187 }
188 _ => {}
189 }
190 }
191}