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
use rppal::i2c::I2c;
use rppal::gpio::{OutputPin};
use std::thread::sleep;
use std::cmp::min;
use super::constants::{*};
use std::result::Result::Err;
fn float_to_bytes(value: f32) -> [u8; 2] {
let base = value.floor();
let fraction = min(((value - base) * 512.0 - 1.0) as u16, 511);
let hi = ((base as u8 & 0b1111111) << 1) | ((&fraction & 0b100000000) >> 8) as u8;
let lo = (&fraction & 0xFF) as u8;
[hi, lo]
}
pub struct Ccs811Data {
pub t_voc: u16,
pub e_co2: u16,
pub raw: Vec<u8>
}
pub struct CCS811 {
pub i2c: I2c,
pub wake: Option<OutputPin>
}
impl CCS811 {
fn reset(&mut self) -> Result<(), String> {
self.i2c.block_write(CCS811_SW_RESET, &[0x11,0xE5,0x72,0x8A])
.map_err(|error| format!("Couldn't write to I2C: {}", error))?;
sleep(CCS811_WAIT_AFTER_RESET_US);
Ok(())
}
fn app_start(&mut self) -> Result<(), String> {
self.i2c.write(&[CCS811_APP_START])
.map_err(|error| format!("Could not set App start: {}", error))?;
sleep(CCS811_WAIT_AFTER_APPSTART_US);
Ok(())
}
fn erase_app(&mut self) -> Result<(), String> {
self.i2c.block_write(CCS811_APP_ERASE, &[0xE7, 0xA7, 0xE6, 0x09])
.map_err(|error| format!("Could not erase app: {}", error))?;
sleep(CCS811_WAIT_AFTER_APPERASE_MS);
Ok(())
}
fn check_hw_id(&mut self) -> Result<(), String> {
let hw_id = self.i2c.smbus_read_byte(CCS811_HW_ID)
.map_err(|error| format!("Couldn't read HWID: {}", error))?;
if hw_id != 0x81 {
return Err(format!("HWID of chip is not 0x81 but {:x?}", hw_id));
}
Ok(())
}
fn check_status(&mut self, expected: u8) -> Result<(), String> {
let status = self.i2c.smbus_read_byte(CCS811_STATUS)
.map_err(|error| format!("Could not read chip status: {}", error))?;
if (status & expected) == 0 {
return Err(format!("Chip status is not {:#010b} but {:#010b}", expected, status));
}
Ok(())
}
fn awake(&mut self) {
if let Some(pin) = &mut self.wake {
pin.set_low();
sleep(CCS811_WAIT_AFTER_WAKE_US);
}
}
fn sleep(&mut self) {
if let Some(pin) = &mut self.wake {
pin.set_high();
}
}
pub fn begin(&mut self) -> Result<(), String> {
self.i2c.set_slave_address(CCS811_SLAVEADDR_0)
.map_err(|error| format!("Could not set slave addr: {}", error))?;
self.awake();
self.reset()
.and(self.check_hw_id())
.and(self.app_start())
.and(self.check_status(CCS811_STATUS_APP_MODE & CCS811_STATUS_APP_VERIFY))?;
self.sleep();
Ok(())
}
pub fn start(&mut self, mode: Ccs811Mode) -> Result<(), String> {
self.awake();
self.i2c.block_write(CCS811_MEAS_MODE, &[(mode as u8) << 4])
.map_err(|error| format!("Could not set mode: {}", error))?;
self.sleep();
Ok(())
}
pub fn hardware_version(&mut self) -> Result<u8, String> {
self.i2c.smbus_read_byte(CCS811_HW_VERSION)
.map_err(|error| format!("Could not read hardware version: {}", error))
}
pub fn bootloader_version(&mut self) -> Result<[u8; 2], String> {
let mut buffer = [0; 2];
self.i2c.block_read(CCS811_FW_BOOT_VERSION, &mut buffer)
.map_err(|error| format!("Could not read boot loader version: {}", error))?;
Ok(buffer)
}
pub fn application_version(&mut self) -> Result<[u8; 2], String> {
let mut buffer = [0; 2];
self.i2c.block_read(CCS811_FW_APP_VERSION, &mut buffer)
.map_err(|error| format!("Could not read application version: {}", error))?;
Ok(buffer)
}
pub fn get_baseline(&mut self) -> Result<u16, String> {
self.i2c.smbus_read_word(CCS811_BASELINE)
.map_err(|error| format!("Could not read baseline: {}", error))
}
pub fn set_baseline(&mut self, baseline: u16) -> Result<(), String> {
self.i2c.smbus_write_word(CCS811_BASELINE, baseline)
.map_err(|error| format!("Could not set baseline: {}", error))
}
pub fn set_env_data(&mut self, humidity: f32, temperature: f32) -> Result<(), String> {
let data = [
float_to_bytes(humidity),
float_to_bytes(temperature)
].concat();
self.i2c.block_write(CCS811_ENV_DATA, &data)
.map_err(|error| format!("Could npt write env data: {}", error))?;
Ok(())
}
pub fn read(&mut self) -> Result<Ccs811Data, String> {
let mut buffer = [0; 8];
self.awake();
self.i2c.block_read(CCS811_ALG_RESULT_DATA, &mut buffer)
.map_err(|error| format!("Could not read chip data: {}", error))?;
self.sleep();
if buffer[5] != 0 {
return Err(format!("Some error while reading data {:x?}", buffer[5]));
}
let data = Ccs811Data {
e_co2: buffer[0] as u16 * 256 + buffer[1] as u16,
t_voc: buffer[2] as u16 * 256 + buffer[3] as u16,
raw: buffer.to_vec()
};
if data.t_voc > 1187 || data.e_co2 > 8192 {
return Err(format!("The data is above max {}ppb, {}ppm", data.t_voc, data.e_co2));
}
Ok(data)
}
pub fn flash(&mut self, data: Vec<u8>) -> Result<(), String> {
self.i2c.set_slave_address(CCS811_SLAVEADDR_0)
.map_err(|error| format!("Could not set slave addr: {}", error))?;
self.reset()?;
self.check_status(CCS811_STATUS_APP_VALID)
.map_err(|error| format!("Not valid: {}", error))?;
self.erase_app()?;
self.check_status(CCS811_STATUS_APP_ERASE)
.map_err(|error| format!("Not erased: {}", error))?;
let mut i = 0;
loop {
println!("Flashing {} of {}\r", i, data.len());
if i >= data.len() {
break;
}
let end = match i + 8 {
v if v > data.len() => data.len(),
v => v
};
self.i2c.block_write(CCS811_APP_DATA, &data[i..end])
.map_err(|error| format!("Could not write firmware: {}", error))?;
i += 8;
}
sleep(CCS811_WAIT_AFTER_APPDATA_MS);
self.i2c.write(&[CCS811_APP_VERIFY])
.map_err(|error| format!("Could not reset verify bit: {}", error))?;
sleep(CCS811_WAIT_AFTER_APPVERIFY_MS);
self.check_status(CCS811_STATUS_APP_ERASE | CCS811_STATUS_APP_VERIFY | CCS811_STATUS_APP_VALID)
.map_err(|error| format!("Not verified: {}", error))?;
self.reset()?;
self.check_status(CCS811_STATUS_APP_VALID)
.map_err(|error| format!("Unexpected status after flashing: {}", error))
}
}