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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
#![no_std]
#![allow(unused)]
use embedded_hal::blocking::{i2c, delay::DelayMs};
#[derive(Debug)]
pub enum Error<E> {
CommunicationError(E),
SensorDetectFailed,
RegisterReadFailed,
RegisterWriteFailed,
CrcCheckFailed,
InvalidResolution,
}
impl<E> From<E> for Error<E> {
fn from(e: E) -> Self {
Error::CommunicationError(e)
}
}
pub struct MS8607<I2C>
{
i2c: I2C,
press_sens: u16,
press_offset: u16,
press_sens_temp_coeff: u16,
press_offset_temp_coeff: u16,
ref_temp: u16,
temp_temp_coeff: u16,
hum_sensor_i2c_read_mode: Ms8607HumidityClockStretch,
psensor_resolution_osr: Ms8607PressureResolution,
_pressure: f64,
_temperature: f64,
_humidity: f64,
}
impl<I2C, E> MS8607<I2C>
where
I2C: i2c::Write<Error = E> + i2c::WriteRead<Error = E>,
{
pub fn new(i2c:I2C) -> Self {
MS8607 {
i2c,
press_sens: 0,
press_offset: 0,
press_sens_temp_coeff: 0,
press_offset_temp_coeff: 0,
ref_temp: 0,
temp_temp_coeff: 0,
hum_sensor_i2c_read_mode: Ms8607HumidityClockStretch::Ms8607I2cHold,
psensor_resolution_osr: Ms8607PressureResolution::Osr4096,
_pressure: 0.0,
_temperature: 0.0,
_humidity: 0.0,
}
}
pub fn begin<Delay>(&mut self, delay:&mut Delay) -> Result<(), Error<E>> where
Delay: DelayMs<u16> {
self.reset(delay)?;
self.init()?;
Ok(())
}
pub fn init(&mut self) -> Result<(), Error<E>>{
self._fetch_temp_calibration_values()?;
self.enable_humidity_clock_stretching(false);
self.set_humidity_resolution( Ms8607HumidityResolution::Osr12b)?;
self.set_pressure_resolution(Ms8607PressureResolution::Osr4096);
Ok(())
}
pub fn enable_humidity_clock_stretching(&mut self, enable_stretching: bool) {
if enable_stretching {
self.hum_sensor_i2c_read_mode = Ms8607HumidityClockStretch::Ms8607I2cHold;
} else {
self.hum_sensor_i2c_read_mode = Ms8607HumidityClockStretch::Ms8607I2cNoHold;
}
}
pub fn set_humidity_resolution(
&mut self,
resolution: Ms8607HumidityResolution,
) -> Result<(), Error<E>> {
let mut reg_value = self._read_humidity_user_register()?;
reg_value &= _MS8607::HSENSOR_USER_REG_RESOLUTION_MASK;
reg_value |= resolution as u8 & _MS8607::HSENSOR_USER_REG_RESOLUTION_MASK;
self._write_humidity_user_register( reg_value)
}
pub fn set_pressure_resolution(&mut self, resolution: Ms8607PressureResolution) {
self.psensor_resolution_osr = resolution;
}
pub fn get_humidity_resolution(&mut self) -> Result<Ms8607HumidityResolution, Error<E>>{
let reg_value = self._read_humidity_user_register()?;
let resolution = reg_value & _MS8607::HSENSOR_USER_REG_RESOLUTION_MASK;
match resolution {
0 => Ok(Ms8607HumidityResolution::Osr12b),
1 => Ok(Ms8607HumidityResolution::Osr8b),
2 => Ok(Ms8607HumidityResolution::Osr10b),
3 => Ok(Ms8607HumidityResolution::Osr11b),
_ => Err(Error::InvalidResolution),
}
}
pub fn get_pressure_resolution(&self) -> Ms8607PressureResolution {
self.psensor_resolution_osr
}
pub fn get_measurements<Delay>(&mut self,delay:&mut Delay) -> Result<(f64, f64, f64), Error<E>> where
Delay: DelayMs<u16> {
self._read( delay)?;
self._read_humidity()?;
Ok((self._pressure, self._temperature, self._humidity))
}
fn _read<Delay>(&mut self, delay:&mut Delay) -> Result<(), Error<E>> where
Delay: DelayMs<u16> {
let mut cmd = [0u8; 1];
let mut buffer = [0u8; 3];
cmd[0] = self.psensor_resolution_osr as u8 * 2;
cmd[0] |= _MS8607::PSENSOR_START_TEMPERATURE_ADC_CONVERSION;
self.i2c.write(_MS8607::PT_ADDRESS, &cmd).map_err(Error::from)?;
delay.delay_ms(20);
cmd = [_MS8607::PSENSOR_READ_ADC];
self.i2c.write_read(_MS8607::PT_ADDRESS, &cmd, &mut buffer).map_err(Error::from)?;
let raw_temp = ((buffer[0] as u32) << 16) | ((buffer[1] as u32) << 8) | (buffer[2] as u32);
delay.delay_ms(20);
cmd[0] = self.psensor_resolution_osr as u8 * 2;
cmd[0] |= _MS8607::PSENSOR_START_PRESSURE_ADC_CONVERSION;
self.i2c.write(_MS8607::PT_ADDRESS, &cmd).map_err(Error::from)?;
delay.delay_ms(20);
buffer = [0u8; 3];
cmd = [_MS8607::PSENSOR_READ_ADC];
self.i2c.write_read(_MS8607::PT_ADDRESS, &cmd, &mut buffer).map_err(Error::from)?;
let raw_pressure =
((buffer[0] as u32) << 16) | ((buffer[1] as u32) << 8) | (buffer[2] as u32);
self._apply_ptcorrections(raw_temp as i32, raw_pressure as i32);
Ok(())
}
fn _read_humidity(&mut self) -> Result<(), Error<E>> {
let mut buffer = [0u8; 3];
self.get_humidity_resolution()?;
let cmd = [Ms8607HumidityClockStretch::Ms8607I2cHold as u8];
self.i2c.write_read(_MS8607::HUM_ADDRESS, &cmd, &mut buffer).map_err(Error::from)?;
let raw_hum: u16 = (buffer[0] as u16) << 8 | (buffer[1] as u16);
let crc = buffer[2];
self._hsensor_crc_check(raw_hum, crc)?;
self._humidity = ((raw_hum as f32) * _MS8607::MS8607_RH_LSB) as f64;
self._humidity -= 6.0;
Ok(())
}
fn _apply_ptcorrections(&mut self, raw_temp: i32, raw_pressure: i32) {
let mut off: i64;
let mut sens: i64;
let t2: i64;
let mut off2: i64;
let mut sens2: i64;
let d_t = raw_temp - ((self.ref_temp as i32) << 8);
let temp = (2000 + (((d_t as i64) * (self.temp_temp_coeff as i64)) >> 23)) as i32;
if temp < 2000 {
t2 = (3 * ((d_t as i64) * (d_t as i64))) >> 33;
off2 = 61 * ((temp as i64) - 2000) * ((temp as i64) - 2000) / 16;
sens2 = 29 * ((temp as i64) - 2000) * ((temp as i64) - 2000) / 16;
if temp < -1500 {
off2 += 17 * ((temp as i64) + 1500) * ((temp as i64) + 1500);
sens2 += 9 * ((temp as i64) + 1500) * ((temp as i64) + 1500);
}
} else {
t2 = (5 * ((d_t as i64) * (d_t as i64))) >> 38;
off2 = 0;
sens2 = 0;
}
off = ((self.press_offset as i64) << 17)
+ (((self.press_offset_temp_coeff as i64) * (d_t as i64)) >> 6);
off -= off2;
sens = ((self.press_sens as i64) << 16)
+ (((self.press_sens_temp_coeff as i64) * (d_t as i64)) >> 7);
sens -= sens2;
let p = ((((raw_pressure as i64) * sens) >> 21) - off) >> 15;
self._temperature = ((temp as f64) - t2 as f64) / 100.0;
self._pressure = p as f64 / 100.0;
}
fn _write_humidity_user_register(&mut self, new_reg_value: u8) -> Result<(), Error<E>>{
let buffer = [_MS8607::HSENSOR_WRITE_USER_REG_COMMAND, new_reg_value];
self.i2c.write(_MS8607::HUM_ADDRESS, &buffer).map_err(Error::from)?;
Ok(())
}
fn _read_humidity_user_register(&mut self) -> Result<u8, Error<E>> {
let mut cmd = [_MS8607::HSENSOR_READ_USER_REG_COMMAND];
let mut buffer = [0u8; 1];
self.i2c.write_read(_MS8607::HUM_ADDRESS, &cmd, &mut buffer).map_err(Error::from)?;
Ok(buffer[0])
}
fn _fetch_temp_calibration_values(&mut self) -> Result<(), Error<E>> {
let mut offset: u8 = 0;
let mut buffer = [0u16; 8];
let mut tmp_buffer = [0u8; 2];
buffer
.iter_mut()
.enumerate()
.take(7usize)
.for_each(|(i, v)| {
offset = 2 * (i as u8);
let cmd = [_MS8607::PROM_ADDRESS_READ_ADDRESS_0 + offset];
let req = self.i2c.write_read(_MS8607::PT_ADDRESS, &cmd,&mut tmp_buffer);
if req.is_err() {
return;
}
*v = (tmp_buffer[0] as u16) << 8;
*v |= tmp_buffer[1] as u16;
});
if offset != 12 {
return Err(Error::RegisterReadFailed);
}
self._psensor_crc_check(&mut buffer)?;
self.press_sens = buffer[1];
self.press_offset = buffer[2];
self.press_sens_temp_coeff = buffer[3];
self.press_offset_temp_coeff = buffer[4];
self.ref_temp = buffer[5];
self.temp_temp_coeff = buffer[6];
Ok(())
}
fn _psensor_crc_check(&mut self, n_prom: &mut [u16; 8]) -> Result<(), Error<E>> {
let mut n_rem = 0;
let crc = ((n_prom[0] & 0xF000) >> 12) as u8;
n_prom[0] &= 0x0FFF; n_prom[7] = 0; for cnt in 0..16 {
if cnt % 2 == 1 {
n_rem ^= (n_prom[cnt >> 1] & 0x00FF);
} else {
n_rem ^= n_prom[cnt >> 1] >> 8;
}
for n_bit in 0..8 {
if n_rem & 0x8000 != 0 {
n_rem = (n_rem << 1) ^ 0x3000;
} else {
n_rem <<= 1;
}
}
}
n_rem = (n_rem >> 12) & 0x000F; if n_rem != crc as u16 {
return Err(Error::CrcCheckFailed);
}
Ok(())
}
fn _hsensor_crc_check(&mut self, value: u16, crc: u8) -> Result<(), Error<E>> {
let mut polynom: u32 = 0x988000; let mut msb: u32 = 0x800000;
let mut mask: u32 = 0xFF8000;
let mut result: u32 = (value as u32) << 8; while msb != 0x80 {
if result & msb != 0 {
result = ((result ^ polynom) & mask) | (result & !mask);
}
msb >>= 1;
mask >>= 1;
polynom >>= 1;
}
if (result) as u8 != crc {
return Err(Error::CrcCheckFailed);
}
Ok(())
}
pub fn reset<Delay>(&mut self, delay: &mut Delay) -> Result<(), Error<E>> where Delay: DelayMs<u16> {
let cmd = [_MS8607::P_T_RESET];
self.i2c.write(_MS8607::PT_ADDRESS, &cmd).map_err(Error::from)?;
let cmd = [_MS8607::HSENSOR_RESET_COMMAND];
self.i2c.write(_MS8607::HUM_ADDRESS, &cmd).map_err(Error::from)?;
delay.delay_ms(15);
Ok(())
}
}
struct _MS8607 {}
impl _MS8607 {
pub const PT_ADDRESS: u8 = 0x76;
pub const HUM_ADDRESS: u8 = 0x40;
pub const HSENSOR_RESET_COMMAND: u8 = 0xFE;
pub const HSENSOR_READ_HUMIDITY_W_HOLD_COMMAND: u8 = 0xE5;
pub const HSENSOR_READ_HUMIDITY_WO_HOLD_COMMAND: u8 = 0xF5;
pub const HSENSOR_READ_SERIAL_FIRST_8BYTES_COMMAND: u16 = 0xFA0F;
pub const HSENSOR_READ_SERIAL_LAST_6BYTES_COMMAND: u16 = 0xFCC9;
pub const HSENSOR_WRITE_USER_REG_COMMAND: u8 = 0xE6;
pub const HSENSOR_READ_USER_REG_COMMAND: u8 = 0xE7;
pub const HSENSOR_TEMPERATURE_COEFFICIENT: f32 = -0.15;
pub const HSENSOR_CONSTANT_A: f32 = 8.1332;
pub const HSENSOR_CONSTANT_B: f32 = 1762.39;
pub const HSENSOR_CONSTANT_C: f32 = 235.66;
pub const TEMPERATURE_COEFF_MUL: f32 = 175.72;
pub const TEMPERATURE_COEFF_ADD: f32 = -46.85;
pub const HUMIDITY_COEFF_MUL: f32 = 125.0;
pub const HUMIDITY_COEFF_ADD: f32 = -6.0;
pub const HSENSOR_CONVERSION_TIME_12B: u8 = 16;
pub const HSENSOR_CONVERSION_TIME_10B: u8 = 5;
pub const HSENSOR_CONVERSION_TIME_8B: u8 = 3;
pub const HSENSOR_CONVERSION_TIME_11B: u8 = 9;
pub const HSENSOR_USER_REG_RESOLUTION_MASK: u8 = 0x81;
pub const HSENSOR_USER_REG_END_OF_BATTERY_MASK: u8 = 0x40;
pub const HSENSOR_USER_REG_ENABLE_ONCHIP_HEATER_MASK: u8 = 0x4;
pub const HSENSOR_USER_REG_DISABLE_OTP_RELOAD_MASK: u8 = 0x2;
pub const RH_ADDRESS: u8 = 0x40;
pub const MS8607_RH_LSB: f32 = 0.001_907_348_6;
pub const PROM_ADDRESS_READ_ADDRESS_0: u8 = 0xA0;
pub const P_T_RESET: u8 = 0x1E;
pub const CONVERT_D1_OSR_256: u8 = 0x40;
pub const CONVERT_D1_OSR_512: u8 = 0x42;
pub const CONVERT_D1_OSR_1024: u8 = 0x44;
pub const CONVERT_D1_OSR_2048: u8 = 0x46;
pub const CONVERT_D1_OSR_4096: u8 = 0x48;
pub const CONVERT_D1_OSR_8192: u8 = 0x4A;
pub const CONVERT_D2_OSR_256: u8 = 0x50;
pub const CONVERT_D2_OSR_512: u8 = 0x52;
pub const CONVERT_D2_OSR_1024: u8 = 0x54;
pub const CONVERT_D2_OSR_2048: u8 = 0x56;
pub const CONVERT_D2_OSR_4096: u8 = 0x58;
pub const CONVERT_D2_OSR_8192: u8 = 0x5A;
pub const ADC_READ: u8 = 0x00;
pub const HUMIDITY_RESET: u8 = 0xFE;
pub const HUMIDITY_WRITE_REGISTER: u8 = 0xE6;
pub const HUMIDITY_READ_REGISTER: u8 = 0xE7;
pub const HUM_MEASURE_RH_HOLD: u8 = 0xE5;
pub const HUM_MEASURE_RH_NO_HOLD: u8 = 0xF5;
pub const PSENSOR_RESET_COMMAND: u8 = 0x1E;
pub const PSENSOR_START_PRESSURE_ADC_CONVERSION: u8 = 0x40;
pub const PSENSOR_START_TEMPERATURE_ADC_CONVERSION: u8 = 0x50;
pub const PSENSOR_READ_ADC: u8 = 0x00;
}
#[derive(Clone, Copy)]
pub enum Ms8607PressureResolution {
Osr256,
Osr512,
Osr1024,
Osr2048,
Osr4096,
Osr8192,
}
pub enum Ms8607HumidityResolution {
Osr12b = 0x00,
Osr11b = 0x81,
Osr10b = 0x80,
Osr8b = 0x01,
}
pub enum Ms8607HumidityClockStretch {
Ms8607I2cHold = 0xE5,
Ms8607I2cNoHold = 0xF5,
}