edrv_spl06/lib.rs
1//! Driver for the Goertek SPL06-001 and the register-compatible SPL06-007
2//! digital barometric pressure sensor.
3//!
4//! The SPL06-001 / SPL06-007 is a calibrated digital barometer with 24-bit
5//! pressure and temperature conversion results. This crate provides both an
6//! asynchronous driver ([`SPL06`]) and a blocking one ([`blocking::SPL06`])
7//! with identical APIs, built on `embedded-hal` v1.
8//!
9//! # Compensation
10//!
11//! The datasheet defines the compensation as a real-number polynomial
12//! (SPL06-001 V1.0 sections 5.7.1/5.7.2, SPL06-007 V1.0 sections 5.6.1/5.6.2):
13//!
14//! ```text
15//! Traw_sc = Traw / kT
16//! Praw_sc = Praw / kP
17//!
18//! Tcomp(degC) = c0 * 0.5 + c1 * Traw_sc
19//! Pcomp(Pa) = c00 + Praw_sc * (c10 + Praw_sc * (c20 + Praw_sc * c30))
20//! + Traw_sc * c01
21//! + Traw_sc * Praw_sc * (c11 + Praw_sc * c21)
22//! ```
23//!
24//! `kP` and `kT` are **not** constants. Each depends on the configured
25//! oversampling rate (Table 7 of the SPL06-001 datasheet), so the raw 24-bit
26//! values must be divided by the factor that belongs to the oversampling the
27//! driver is actually using. Using the wrong factor silently produces a
28//! plausible-looking but wrong reading.
29//!
30//! The driver evaluates the polynomial in integer Q24 fixed-point arithmetic,
31//! so it does no floating point itself; the `f32` accessors on [`Measurements`]
32//! are the only conversion and stay out of the generated code for a target
33//! without an FPU unless they are actually called.
34//!
35//! # Init and reset
36//!
37//! [`SPL06::init`] reads the product/revision ID register (`0x0D`, expected
38//! value [`PRODUCT_ID`] = `0x10`) and the 18-byte calibration block at `0x10`,
39//! then programs the default oversampling. Like `edrv-bme280` and
40//! `edrv-bme680`, `init` deliberately does **not** soft reset;
41//! [`SPL06::reset`] is a separate, explicit step.
42//!
43//! # Licence
44//!
45//! This crate is distributed under `MIT OR Apache-2.0`.
46
47#![cfg_attr(not(test), no_std)]
48
49use embedded_hal_async::delay::DelayNs;
50
51pub mod blocking;
52
53/// I2C address with `SDO` high - the sensor's **default** address.
54///
55/// SPL06-001/-007 datasheet, section 5.3.1: "The sensor's address is `0x77`
56/// (default) or `0x76` (if the `SDO` pin is pulled-down to GND)."
57///
58/// Note this is the **opposite** convention to Bosch's barometers, where the
59/// primary address is `0x76`. Do not carry that habit across: an SPL06 at
60/// `0x76` and one at `0x77` are the same part with a different `SDO` strap.
61pub const ADDRESS: u8 = 0x77;
62
63/// I2C address with `SDO` pulled down to GND.
64///
65/// The address is a property of your board's `SDO` strap, not of the part, so
66/// confirm which one you have by reading register `0x0D` rather than assuming.
67pub const ADDRESS_ALT: u8 = 0x76;
68
69/// Value expected in the product/revision ID register ([`regs::ID`]).
70///
71/// The register holds `PROD_ID[7:4]` and `REV_ID[3:0]`, and both the SPL06-001
72/// V1.0 and the SPL06-007 V1.0 datasheets give `0x10` as its reset value
73/// (`PROD_ID = 0x1`, `REV_ID = 0x0`). The two parts therefore cannot be told
74/// apart by this register.
75pub const PRODUCT_ID: u8 = 0x10;
76
77/// Value written to [`regs::RESET`] to trigger a soft reset.
78///
79/// The datasheet specifies `SOFT_RST[3:0] = 0b1001`; the part then runs the
80/// same sequence as a power-on reset, so the calibration data has to be read
81/// again before the next measurement.
82pub const SOFT_RESET_COMMAND: u8 = 0x09;
83
84/// Worst-case time from power-on (or soft reset) until the calibration
85/// coefficients can be read, `TCoef_rdy` in the datasheet.
86///
87/// Both [`SPL06::init`] and [`SPL06::reset`] wait this long.
88pub(crate) const STARTUP_TIME_MS: u32 = 40;
89
90/// Register addresses, register field masks and block lengths.
91///
92/// Addresses and bit positions are from the SPL06-001 V1.0 datasheet,
93/// section 7 ("Register Map") and section 8 ("Register Description"); the
94/// SPL06-007 V1.0 map is identical.
95pub mod regs {
96 /// `PRS_B2`: pressure result, bits 23:16.
97 pub const PRS_B2: u8 = 0x00;
98 /// `PRS_B1`: pressure result, bits 15:8.
99 pub const PRS_B1: u8 = 0x01;
100 /// `PRS_B0`: pressure result, bits 7:0.
101 pub const PRS_B0: u8 = 0x02;
102
103 /// `TMP_B2`: temperature result, bits 23:16.
104 pub const TMP_B2: u8 = 0x03;
105 /// `TMP_B1`: temperature result, bits 15:8.
106 pub const TMP_B1: u8 = 0x04;
107 /// `TMP_B0`: temperature result, bits 7:0.
108 pub const TMP_B0: u8 = 0x05;
109
110 /// `PRS_CFG`: pressure measurement rate and oversampling.
111 pub const PRS_CFG: u8 = 0x06;
112 /// `TMP_CFG`: temperature measurement rate, source and oversampling.
113 pub const TMP_CFG: u8 = 0x07;
114 /// `MEAS_CFG`: operating mode, plus the sensor/coefficient/data-ready flags.
115 pub const MEAS_CFG: u8 = 0x08;
116 /// `CFG_REG`: interrupt, result-shift and FIFO configuration.
117 pub const CFG_REG: u8 = 0x09;
118 /// `INT_STS`: interrupt status, cleared on read.
119 pub const INT_STS: u8 = 0x0A;
120 /// `FIFO_STS`: FIFO full/empty status.
121 pub const FIFO_STS: u8 = 0x0B;
122 /// `RESET`: soft reset and FIFO flush.
123 pub const RESET: u8 = 0x0C;
124 /// `ID`: product and revision ID.
125 pub const ID: u8 = 0x0D;
126
127 /// Start of the 18-byte calibration coefficient block (`0x10..=0x21`).
128 pub const COEF: u8 = 0x10;
129
130 /// `PRS_CFG[6:4]`: pressure measurement rate (background mode only).
131 pub const PM_RATE_MASK: u8 = 0x70;
132 /// `PRS_CFG[3:0]`: pressure oversampling rate.
133 pub const PM_PRC_MASK: u8 = 0x0F;
134
135 /// `TMP_CFG` bit 7: temperature source, `1` = external MEMS sensor.
136 pub const TMP_CFG_EXT: u8 = 0x80;
137 /// `TMP_CFG[6:4]`: temperature measurement rate (background mode only).
138 pub const TMP_RATE_MASK: u8 = 0x70;
139 /// `TMP_CFG[2:0]`: temperature oversampling rate.
140 pub const TMP_PRC_MASK: u8 = 0x07;
141
142 /// `MEAS_CFG` bit 7: calibration coefficients are available.
143 pub const COEF_RDY: u8 = 0x80;
144 /// `MEAS_CFG` bit 6: the pressure sensor finished self-initialisation.
145 pub const SENSOR_RDY: u8 = 0x40;
146 /// `MEAS_CFG` bit 5: a new temperature result is available.
147 pub const TMP_RDY: u8 = 0x20;
148 /// `MEAS_CFG` bit 4: a new pressure result is available.
149 pub const PRS_RDY: u8 = 0x10;
150 /// `MEAS_CFG[2:0]`: operating mode and measurement type.
151 pub const MEAS_CTRL_MASK: u8 = 0x07;
152
153 /// `MEAS_CTRL = 0b001`: one pressure measurement, then standby.
154 pub const MEAS_PRESSURE: u8 = 0b001;
155 /// `MEAS_CTRL = 0b010`: one temperature measurement, then standby.
156 pub const MEAS_TEMPERATURE: u8 = 0b010;
157
158 /// `CFG_REG` bit 3: shift the temperature result.
159 ///
160 /// Mandatory when the temperature oversampling is greater than 8 times.
161 pub const TMP_SHIFT_EN: u8 = 0x08;
162 /// `CFG_REG` bit 2: shift the pressure result.
163 ///
164 /// Mandatory when the pressure oversampling is greater than 8 times.
165 pub const PRS_SHIFT_EN: u8 = 0x04;
166 /// `CFG_REG` bit 1: enable the FIFO. This driver keeps the FIFO disabled.
167 pub const FIFO_EN: u8 = 0x02;
168 /// `CFG_REG` bit 0: `0` = 4-wire SPI, `1` = 3-wire SPI. Unused over I2C.
169 pub const SPI_MODE: u8 = 0x01;
170
171 /// `INT_STS` bit 2: FIFO-full interrupt is active.
172 pub const INT_FIFO_FULL: u8 = 0x04;
173 /// `INT_STS` bit 1: temperature-ready interrupt is active.
174 pub const INT_TMP: u8 = 0x02;
175 /// `INT_STS` bit 0: pressure-ready interrupt is active.
176 pub const INT_PRS: u8 = 0x01;
177
178 /// `FIFO_STS` bit 1: the FIFO is full.
179 pub const FIFO_FULL: u8 = 0x02;
180 /// `FIFO_STS` bit 0: the FIFO is empty.
181 pub const FIFO_EMPTY: u8 = 0x01;
182
183 /// `RESET` bit 7: flush the FIFO.
184 pub const FIFO_FLUSH: u8 = 0x80;
185 /// `RESET[3:0]`: the soft reset command field.
186 pub const SOFT_RST_MASK: u8 = 0x0F;
187
188 /// `ID[7:4]`: product ID.
189 pub const PROD_ID_MASK: u8 = 0xF0;
190 /// `ID[3:0]`: revision ID.
191 pub const REV_ID_MASK: u8 = 0x0F;
192
193 /// Length of one 24-bit conversion result.
194 pub const DATA_LEN: usize = 3;
195 /// Length of the calibration coefficient block (`0x10..=0x21`).
196 pub const CALIBRATION_LEN: usize = 18;
197}
198
199/// Errors returned by the SPL06 driver.
200#[derive(Debug)]
201pub enum Error<E> {
202 /// Error from the underlying I2C bus.
203 Bus(E),
204 /// The ID register ([`regs::ID`]) did not read [`PRODUCT_ID`]. The payload is
205 /// the value that was read.
206 InvalidDevice(u8),
207 /// A measurement was requested before the calibration data had been read.
208 /// Call `init` first.
209 NotCalibrated,
210 /// The sensor did not report a new measurement within the driver's polling
211 /// limit.
212 Timeout,
213}
214
215impl<E> From<E> for Error<E> {
216 fn from(error: E) -> Self {
217 Error::Bus(error)
218 }
219}
220
221/// Oversampling (precision) setting for one measurement channel.
222///
223/// The variants map directly onto `PM_PRC[3:0]` and `TMP_PRC[2:0]` in
224/// [`regs::PRS_CFG`] / [`regs::TMP_CFG`]. 16 times and above also require the
225/// corresponding result bit-shift in [`regs::CFG_REG`]; [`Config`] takes care
226/// of that.
227#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
228#[repr(u8)]
229pub enum Oversampling {
230 /// Single conversion, 3.6 ms.
231 Single = 0b000,
232 /// 2 times (datasheet "Low Power"), 5.2 ms.
233 X2 = 0b001,
234 /// 4 times, 8.4 ms.
235 X4 = 0b010,
236 /// 8 times, 14.8 ms.
237 #[default]
238 X8 = 0b011,
239 /// 16 times (datasheet "Standard"), 27.6 ms. Needs a result shift.
240 X16 = 0b100,
241 /// 32 times, 53.2 ms. Needs a result shift.
242 X32 = 0b101,
243 /// 64 times (datasheet "High Precision"), 104.4 ms. Needs a result shift.
244 X64 = 0b110,
245 /// 128 times, 206.8 ms. Needs a result shift.
246 X128 = 0b111,
247}
248
249impl Oversampling {
250 /// The `PM_PRC` / `TMP_PRC` register value for this setting.
251 pub const fn bits(self) -> u8 {
252 self as u8
253 }
254
255 /// The datasheet's `kP` / `kT` scale factor for this setting (Table 7).
256 ///
257 /// The raw 24-bit result must be divided by this before compensation:
258 /// `Traw_sc = Traw / kT`, `Praw_sc = Praw / kP`.
259 pub const fn scale_factor(self) -> i32 {
260 match self {
261 Oversampling::Single => 524_288,
262 Oversampling::X2 => 1_572_864,
263 Oversampling::X4 => 3_670_016,
264 Oversampling::X8 => 7_864_320,
265 Oversampling::X16 => 253_952,
266 Oversampling::X32 => 516_096,
267 Oversampling::X64 => 1_040_384,
268 Oversampling::X128 => 2_088_960,
269 }
270 }
271
272 /// Whether the result bit-shift in [`regs::CFG_REG`] is mandatory.
273 ///
274 /// True for every setting above 8 times.
275 pub const fn requires_shift(self) -> bool {
276 (self as u8) > (Oversampling::X8 as u8)
277 }
278
279 /// Worst-case measurement time in milliseconds, rounded up.
280 ///
281 /// From the "Pressure measurement time (ms)" row of the SPL06-001
282 /// datasheet Table 11.
283 pub const fn measurement_time_ms(self) -> u32 {
284 match self {
285 Oversampling::Single => 4,
286 Oversampling::X2 => 6,
287 Oversampling::X4 => 9,
288 Oversampling::X8 => 15,
289 Oversampling::X16 => 28,
290 Oversampling::X32 => 54,
291 Oversampling::X64 => 105,
292 Oversampling::X128 => 207,
293 }
294 }
295}
296
297/// Oversampling configuration, with the register writes it implies.
298///
299/// `kP` / `kT` are derived from this configuration, so changing the
300/// oversampling after a measurement means the next measurement has to be
301/// compensated with the new factors. [`SPL06::set_oversampling`] writes the
302/// configuration registers and updates the driver's configuration together.
303#[derive(Debug, Clone, Copy, PartialEq, Eq)]
304pub struct Config {
305 /// Pressure oversampling.
306 pub pressure: Oversampling,
307 /// Temperature oversampling. Pressure compensation depends on a fresh
308 /// temperature result, so this should normally be greater than or equal to
309 /// [`Config::pressure`].
310 pub temperature: Oversampling,
311}
312
313impl Default for Config {
314 /// 8 times oversampling on both channels.
315 ///
316 /// 8 times is the highest setting that needs no result bit-shift, which
317 /// keeps the default configuration self-contained at a moderate conversion
318 /// time (14.8 ms per channel).
319 fn default() -> Self {
320 Config {
321 pressure: Oversampling::X8,
322 temperature: Oversampling::X8,
323 }
324 }
325}
326
327impl Config {
328 /// The register writes implied by this configuration, **in this exact
329 /// order**.
330 ///
331 /// [`regs::PRS_CFG`] and [`regs::TMP_CFG`] select the oversampling, and
332 /// [`regs::CFG_REG`] carries the result bit-shifts that the datasheet
333 /// requires for any oversampling above 8 times. Writing `CFG_REG` after the
334 /// two oversampling registers keeps every shift enable next to the precision
335 /// it belongs to, and a single source of truth
336 /// ([`Config::control_writes`]) drives `init`, `set_oversampling` and the
337 /// regression test that guards the sequence, so the shift and the
338 /// oversampling can never be programmed from two places that drift apart.
339 ///
340 /// `CFG_REG`'s interrupt and FIFO enable bits are written as zero because
341 /// this driver uses neither; the measurement rate fields are left at their
342 /// default 1 measurement per second because they only apply in background
343 /// mode, which this single-shot driver does not use.
344 pub const fn control_writes(&self) -> [(u8, u8); 3] {
345 [
346 // PM_RATE = 0, PM_PRC = oversampling.
347 (regs::PRS_CFG, self.pressure.bits() & regs::PM_PRC_MASK),
348 // TMP_EXT = 1 (the datasheet asks for the external MEMS sensor),
349 // TMP_RATE = 0, TMP_PRC = oversampling.
350 (
351 regs::TMP_CFG,
352 regs::TMP_CFG_EXT | (self.temperature.bits() & regs::TMP_PRC_MASK),
353 ),
354 // Result bit-shifts, mandatory above 8 times.
355 (regs::CFG_REG, self.shift_enables()),
356 ]
357 }
358
359 /// The [`regs::CFG_REG`] shift bits this configuration requires.
360 pub const fn shift_enables(&self) -> u8 {
361 let mut value = 0;
362 if self.pressure.requires_shift() {
363 value |= regs::PRS_SHIFT_EN;
364 }
365 if self.temperature.requires_shift() {
366 value |= regs::TMP_SHIFT_EN;
367 }
368 value
369 }
370}
371
372/// Calibration coefficients, named after the datasheet's `COEF` registers.
373///
374/// All fields are public so that a calibration image read elsewhere can be
375/// inspected, but normal use only requires [`SPL06::init`] to read them from
376/// the device.
377///
378/// The coefficients are two's complement values of mixed width: `c0` and `c1`
379/// are 12-bit, `c00` and `c10` are 20-bit, and `c01`, `c11`, `c20`, `c21` and
380/// `c30` are 16-bit (SPL06-001 V1.0 Table 13).
381#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
382pub struct CalibrationData {
383 /// Temperature coefficient 0 (12-bit).
384 pub c0: i16,
385 /// Temperature coefficient 1 (12-bit).
386 pub c1: i16,
387 /// Pressure coefficient 00 (20-bit), the pressure offset in pascal.
388 pub c00: i32,
389 /// Pressure coefficient 10 (20-bit).
390 pub c10: i32,
391 /// Pressure coefficient 01 (16-bit).
392 pub c01: i16,
393 /// Pressure coefficient 11 (16-bit).
394 pub c11: i16,
395 /// Pressure coefficient 20 (16-bit).
396 pub c20: i16,
397 /// Pressure coefficient 21 (16-bit).
398 pub c21: i16,
399 /// Pressure coefficient 30 (16-bit).
400 pub c30: i16,
401}
402
403impl CalibrationData {
404 /// Placeholder used until `init` reads the real values from the device.
405 pub const fn new() -> Self {
406 Self {
407 c0: 0,
408 c1: 0,
409 c00: 0,
410 c10: 0,
411 c01: 0,
412 c11: 0,
413 c20: 0,
414 c21: 0,
415 c30: 0,
416 }
417 }
418
419 /// Decode the 18-byte calibration block starting at [`regs::COEF`].
420 ///
421 /// Every coefficient is a two's complement value spanning a nibble
422 /// boundary, so each one has to be sign-extended from its own bit width;
423 /// missing sign extension is a classic way to get plausible but wrong
424 /// readings.
425 pub fn from_raw(raw: &[u8; regs::CALIBRATION_LEN]) -> Self {
426 // c0 = raw[0] and the high nibble of raw[1].
427 // c1 = the low nibble of raw[1] and raw[2].
428 // c00 = raw[3], raw[4] and the high nibble of raw[5].
429 // c10 = the low nibble of raw[5], raw[6] and raw[7].
430 // The rest are plain big-endian 16-bit values.
431 Self {
432 c0: sign_extend(u32::from(raw[0]) << 4 | u32::from(raw[1]) >> 4, 12) as i16,
433 c1: sign_extend(u32::from(raw[1] & 0x0F) << 8 | u32::from(raw[2]), 12) as i16,
434 c00: sign_extend(
435 u32::from(raw[3]) << 12 | u32::from(raw[4]) << 4 | u32::from(raw[5]) >> 4,
436 20,
437 ),
438 c10: sign_extend(
439 u32::from(raw[5] & 0x0F) << 16 | u32::from(raw[6]) << 8 | u32::from(raw[7]),
440 20,
441 ),
442 c01: i16::from_be_bytes([raw[8], raw[9]]),
443 c11: i16::from_be_bytes([raw[10], raw[11]]),
444 c20: i16::from_be_bytes([raw[12], raw[13]]),
445 c21: i16::from_be_bytes([raw[14], raw[15]]),
446 c30: i16::from_be_bytes([raw[16], raw[17]]),
447 }
448 }
449}
450
451/// One compensated measurement.
452///
453/// The `raw_*` accessors return the 24-bit two's complement ADC values exactly
454/// as they appeared in the sensor's data registers. The public fields hold the
455/// integer compensation results, so an integer-only user never has to touch
456/// floating point; the `f32` accessors are a convenience on top.
457#[derive(Debug, Clone, Copy, PartialEq, Eq)]
458pub struct Measurements {
459 raw_temperature: i32,
460 raw_pressure: i32,
461 /// Compensated temperature in hundredths of a degree Celsius.
462 pub temperature: i32,
463 /// Compensated pressure in pascal.
464 pub pressure: i32,
465}
466
467impl Measurements {
468 /// Compensated temperature in degrees Celsius.
469 pub fn temperature_celsius(&self) -> f32 {
470 self.temperature as f32 / 100.0
471 }
472
473 /// Compensated pressure in pascal.
474 pub fn pressure_pa(&self) -> f32 {
475 self.pressure as f32
476 }
477
478 /// Compensated pressure in hectopascal (millibar).
479 pub fn pressure_hpa(&self) -> f32 {
480 self.pressure as f32 / 100.0
481 }
482
483 /// Raw 24-bit two's complement temperature ADC value.
484 pub fn raw_temperature(&self) -> i32 {
485 self.raw_temperature
486 }
487
488 /// Raw 24-bit two's complement pressure ADC value.
489 pub fn raw_pressure(&self) -> i32 {
490 self.raw_pressure
491 }
492}
493
494// - MARK: Fixed-point compensation
495
496/// Fractional bits used for the scaled raw values and the polynomial.
497///
498/// 24 bits leave a wide safety margin against i64 overflow even for a
499/// saturated 24-bit raw value at single oversampling, where the scaled value
500/// can reach 16 (`|raw| <= 2^23`, `kP = 2^19`). See [`compensate_pressure`] for
501/// the worst-case products.
502const FRACTIONAL_BITS: u32 = 24;
503/// `1 << FRACTIONAL_BITS`.
504const FRACTIONAL_SCALE: i64 = 1 << FRACTIONAL_BITS;
505
506/// Sign-extend the low `bits` of `value` to a full `i32`.
507#[inline]
508const fn sign_extend(value: u32, bits: u32) -> i32 {
509 let shift = 32 - bits;
510 ((value << shift) as i32) >> shift
511}
512
513/// Divide, rounding half away from zero. `denominator` must be positive.
514#[inline]
515fn div_round(numerator: i64, denominator: i64) -> i64 {
516 if numerator >= 0 {
517 (numerator + denominator / 2) / denominator
518 } else {
519 -((-numerator + denominator / 2) / denominator)
520 }
521}
522
523/// Divide toward zero. `denominator` must be positive.
524#[inline]
525fn div_trunc(numerator: i64, denominator: i64) -> i64 {
526 numerator / denominator
527}
528
529/// Scale a raw 24-bit result by its oversampling-dependent `kP` / `kT` factor.
530///
531/// The returned value is Q24: it represents `raw / k`.
532#[inline]
533fn scale_raw(raw: i32, oversampling: Oversampling) -> i64 {
534 div_round(
535 i64::from(raw) * FRACTIONAL_SCALE,
536 i64::from(oversampling.scale_factor()),
537 )
538}
539
540/// Decode a 24-bit big-endian two's complement value.
541#[inline]
542fn decode_i24(raw: &[u8; regs::DATA_LEN]) -> i32 {
543 sign_extend(u32::from(raw[0]) << 16 | u32::from(raw[1]) << 8 | u32::from(raw[2]), 24)
544}
545
546/// Datasheet 5.7.2: `Tcomp = c0 * 0.5 + c1 * Traw_sc`, in hundredths of a
547/// degree Celsius.
548#[inline]
549fn compensate_temperature(
550 raw_temperature: i32,
551 temperature_oversampling: Oversampling,
552 calib: &CalibrationData,
553) -> i32 {
554 let traw_sc = scale_raw(raw_temperature, temperature_oversampling);
555
556 // Q24 degrees Celsius.
557 let tcomp = i64::from(calib.c0) * (FRACTIONAL_SCALE / 2) + i64::from(calib.c1) * traw_sc;
558
559 div_round(tcomp * 100, FRACTIONAL_SCALE) as i32
560}
561
562/// Datasheet 5.7.1: the pressure polynomial, in pascal.
563///
564/// The polynomial is expanded term by term rather than evaluated in its nested
565/// form. With `Praw_sc` in Q24 the nested form squares and cubes `Praw_sc`
566/// before dividing, and `Praw_sc` can reach 16 for a saturated 24-bit raw value
567/// at single oversampling, which overflows `i64`. Expanding it lets every
568/// division happen before the next multiplication, so the largest intermediate
569/// is `Praw_sc^3 * c30` at about `2^51` - six orders of magnitude below
570/// `i64::MAX`. Q24 also keeps the rounding error of the scaled values well
571/// below the sensor's own noise: over the physical range, one Q24 step of error
572/// in `Praw_sc` moves the result by well under 0.1 Pa.
573#[inline]
574fn compensate_pressure(
575 raw_pressure: i32,
576 raw_temperature: i32,
577 pressure_oversampling: Oversampling,
578 temperature_oversampling: Oversampling,
579 calib: &CalibrationData,
580) -> i32 {
581 let praw_sc = scale_raw(raw_pressure, pressure_oversampling);
582 let traw_sc = scale_raw(raw_temperature, temperature_oversampling);
583
584 // c00 + Praw_sc*(c10 + Praw_sc*(c20 + Praw_sc*c30))
585 // + Traw_sc*c01 + Traw_sc*Praw_sc*(c11 + Praw_sc*c21)
586 let praw_sq = div_trunc(praw_sc * praw_sc, FRACTIONAL_SCALE);
587 let praw_cu = div_trunc(praw_sq * praw_sc, FRACTIONAL_SCALE);
588 let traw_praw = div_trunc(traw_sc * praw_sc, FRACTIONAL_SCALE);
589 let traw_praw_sq = div_trunc(traw_praw * praw_sc, FRACTIONAL_SCALE);
590
591 let mut pressure = i64::from(calib.c00) * FRACTIONAL_SCALE;
592 pressure += praw_sc * i64::from(calib.c10);
593 pressure += praw_sq * i64::from(calib.c20);
594 pressure += praw_cu * i64::from(calib.c30);
595 pressure += traw_sc * i64::from(calib.c01);
596 pressure += traw_praw * i64::from(calib.c11);
597 pressure += traw_praw_sq * i64::from(calib.c21);
598
599 div_round(pressure, FRACTIONAL_SCALE) as i32
600}
601
602/// Compensate one pressure / temperature pair.
603#[inline]
604fn compensate(raw_pressure: i32, raw_temperature: i32, config: &Config, calib: &CalibrationData) -> Measurements {
605 Measurements {
606 raw_temperature,
607 raw_pressure,
608 temperature: compensate_temperature(raw_temperature, config.temperature, calib),
609 pressure: compensate_pressure(
610 raw_pressure,
611 raw_temperature,
612 config.pressure,
613 config.temperature,
614 calib,
615 ),
616 }
617}
618
619// - MARK: Async driver
620
621/// Async SPL06-001 / SPL06-007 driver.
622pub struct SPL06<I2C: embedded_hal_async::i2c::I2c> {
623 addr: u8,
624 i2c: I2C,
625 calibrated: bool,
626 /// Oversampling configuration used by [`SPL06::configure`] and
627 /// [`SPL06::measure`]. Starts at [`Config::default`].
628 pub config: Config,
629 /// Calibration data read by [`SPL06::init`].
630 pub calib: CalibrationData,
631}
632
633impl<I2C: embedded_hal_async::i2c::I2c> SPL06<I2C> {
634 /// Create a driver for a sensor at `addr`.
635 ///
636 /// This does not touch the bus. Call [`SPL06::init`] before measuring.
637 pub fn new(i2c: I2C, addr: u8) -> Self {
638 Self {
639 addr,
640 i2c,
641 calibrated: false,
642 config: Config::default(),
643 calib: CalibrationData::new(),
644 }
645 }
646
647 /// Create a driver for a sensor at the default address ([`ADDRESS`]).
648 pub fn new_primary(i2c: I2C) -> Self {
649 Self::new(i2c, ADDRESS)
650 }
651
652 /// Validate the device, wait for its start-up sequence, read the
653 /// calibration data and program the default oversampling.
654 ///
655 /// Reads [`regs::ID`] and rejects anything other than [`PRODUCT_ID`], then
656 /// polls `MEAS_CFG` until `COEF_RDY` is set, reads the 18-byte calibration
657 /// block at [`regs::COEF`] and applies [`Config::default`].
658 ///
659 /// Unlike the vendor's own start-up sequence this does **not** soft reset:
660 /// [`SPL06::reset`] is an explicit, separate step, matching `edrv-bme280`
661 /// and `edrv-bme680`. `init` is safe without a preceding reset because it
662 /// programs every writable configuration register explicitly.
663 ///
664 /// The wait is not optional. The coefficients are unavailable for
665 /// `TCoef_rdy` (40 ms) after power-on, and the part answers a read in that
666 /// window with an all-zero block rather than an error. Every term of the
667 /// compensation polynomial is multiplied by a coefficient, so that block
668 /// decodes to exactly 0 Pa and 0.00 degC and no measurement ever complains
669 /// about it. `init` therefore returns [`Error::Timeout`] if the flag does not
670 /// appear within twice `TCoef_rdy`, rather than accepting a blank block.
671 pub async fn init(&mut self, mut delay: impl DelayNs) -> Result<(), Error<I2C::Error>> {
672 let id = self.read_reg(regs::ID).await?;
673 if id != PRODUCT_ID {
674 return Err(Error::InvalidDevice(id));
675 }
676
677 self.wait_for_startup(&mut delay).await?;
678
679 self.read_calibration().await?;
680 self.configure().await?;
681
682 Ok(())
683 }
684
685 /// Soft reset the sensor and wait for it to come back.
686 ///
687 /// Writes [`SOFT_RESET_COMMAND`] to [`regs::RESET`] and waits 40 ms, the
688 /// datasheet's worst-case time for the calibration coefficients to become
689 /// available again (`TCoef_rdy`). The whole configuration returns to its
690 /// reset defaults, so `init` has to run again before the next measurement;
691 /// the driver is marked uncalibrated so a premature [`SPL06::measure`]
692 /// fails with [`Error::NotCalibrated`] instead of using stale data.
693 pub async fn reset(&mut self, mut delay: impl DelayNs) -> Result<(), Error<I2C::Error>> {
694 self.write_reg(regs::RESET, SOFT_RESET_COMMAND).await?;
695
696 delay.delay_ms(STARTUP_TIME_MS).await;
697
698 self.calibrated = false;
699
700 Ok(())
701 }
702
703 /// Write the current [`SPL06::config`] to the sensor.
704 ///
705 /// The writes and their order come from [`Config::control_writes`]; see
706 /// that method for why the order matters.
707 pub async fn configure(&mut self) -> Result<(), Error<I2C::Error>> {
708 for (reg, value) in self.config.control_writes() {
709 self.write_reg(reg, value).await?;
710 }
711
712 Ok(())
713 }
714
715 /// Change the oversampling and write the new configuration immediately.
716 pub async fn set_oversampling(
717 &mut self,
718 pressure: Oversampling,
719 temperature: Oversampling,
720 ) -> Result<(), Error<I2C::Error>> {
721 self.config = Config { pressure, temperature };
722
723 self.configure().await
724 }
725
726 /// Read and store the calibration block. Also called by [`SPL06::init`].
727 pub async fn read_calibration(&mut self) -> Result<(), Error<I2C::Error>> {
728 let mut raw = [0u8; regs::CALIBRATION_LEN];
729 self.read_regs(regs::COEF, &mut raw).await?;
730
731 self.calib = CalibrationData::from_raw(&raw);
732 self.calibrated = true;
733
734 Ok(())
735 }
736
737 /// Trigger a single temperature conversion and return the raw 24-bit value.
738 pub async fn read_raw_temperature(&mut self, mut delay: impl DelayNs) -> Result<i32, Error<I2C::Error>> {
739 self.trigger(regs::MEAS_TEMPERATURE).await?;
740 self.wait_for_flag(regs::TMP_RDY, self.config.temperature.measurement_time_ms(), &mut delay)
741 .await?;
742
743 let mut raw = [0u8; regs::DATA_LEN];
744 self.read_regs(regs::TMP_B2, &mut raw).await?;
745
746 Ok(decode_i24(&raw))
747 }
748
749 /// Trigger a single pressure conversion and return the raw 24-bit value.
750 pub async fn read_raw_pressure(&mut self, mut delay: impl DelayNs) -> Result<i32, Error<I2C::Error>> {
751 self.trigger(regs::MEAS_PRESSURE).await?;
752 self.wait_for_flag(regs::PRS_RDY, self.config.pressure.measurement_time_ms(), &mut delay)
753 .await?;
754
755 let mut raw = [0u8; regs::DATA_LEN];
756 self.read_regs(regs::PRS_B2, &mut raw).await?;
757
758 Ok(decode_i24(&raw))
759 }
760
761 /// Perform one single-shot temperature and pressure measurement.
762 ///
763 /// Temperature is measured first because the pressure compensation depends
764 /// on it. Each conversion is started through [`regs::MEAS_CFG`] and then
765 /// polled for its ready flag, sleeping 1 ms between polls; the poll limit is
766 /// twice the datasheet's worst-case conversion time plus 10 ms, after which
767 /// [`Error::Timeout`] is returned.
768 ///
769 /// [`Error::NotCalibrated`] is returned if [`SPL06::init`] has not run (or
770 /// [`SPL06::reset`] has run since).
771 pub async fn measure(&mut self, mut delay: impl DelayNs) -> Result<Measurements, Error<I2C::Error>> {
772 if !self.calibrated {
773 return Err(Error::NotCalibrated);
774 }
775
776 let raw_temperature = self.read_raw_temperature(&mut delay).await?;
777 let raw_pressure = self.read_raw_pressure(&mut delay).await?;
778
779 Ok(compensate(raw_pressure, raw_temperature, &self.config, &self.calib))
780 }
781
782 /// Start one command-mode conversion.
783 async fn trigger(&mut self, command: u8) -> Result<(), Error<I2C::Error>> {
784 self.write_reg(regs::MEAS_CFG, command).await
785 }
786
787 /// Poll [`regs::MEAS_CFG`] until `flag` is set.
788 ///
789 /// Gives up after twice `worst_case_ms` plus 10 ms, sleeping 1 ms between
790 /// polls.
791 async fn wait_for_flag<D: DelayNs>(
792 &mut self,
793 flag: u8,
794 worst_case_ms: u32,
795 delay: &mut D,
796 ) -> Result<(), Error<I2C::Error>> {
797 let limit = worst_case_ms * 2 + 10;
798 let mut polls = 0;
799
800 while self.read_reg(regs::MEAS_CFG).await? & flag == 0 {
801 polls += 1;
802 if polls >= limit {
803 return Err(Error::Timeout);
804 }
805 delay.delay_ms(1).await;
806 }
807
808 Ok(())
809 }
810
811 /// Poll [`regs::MEAS_CFG`] until the part reports that the calibration
812 /// coefficients can be read.
813 ///
814 /// [`regs::COEF_RDY`] (bit 7) means "calibration coefficients valid", which
815 /// the datasheet places about 40 ms after power-up. That is exactly the
816 /// precondition for the read that follows, so waiting on it alone is both
817 /// sufficient and minimal.
818 ///
819 /// [`regs::SENSOR_RDY`] (bit 6) is deliberately *not* required: it reports
820 /// that the sensor finished its own initialisation, which is not a
821 /// precondition for reading the coefficient block. Requiring it would make
822 /// `init` fail on a part that publishes its coefficients before, or without,
823 /// that flag. Paparazzi's `spa06.c` waits for both bits, iNav's
824 /// `barometer_spl06.c` reads the block on `COEFFS_RDY` alone.
825 async fn wait_for_startup(&mut self, delay: &mut impl DelayNs) -> Result<(), Error<I2C::Error>> {
826 self.wait_for_flag(regs::COEF_RDY, STARTUP_TIME_MS, delay).await
827 }
828
829 /// Read one register.
830 pub async fn read_reg(&mut self, reg: u8) -> Result<u8, Error<I2C::Error>> {
831 let mut buf = [0u8; 1];
832 self.i2c.write_read(self.addr, &[reg], &mut buf).await?;
833 Ok(buf[0])
834 }
835
836 /// Read consecutive registers into `buf`.
837 pub async fn read_regs(&mut self, reg: u8, buf: &mut [u8]) -> Result<(), Error<I2C::Error>> {
838 self.i2c.write_read(self.addr, &[reg], buf).await?;
839 Ok(())
840 }
841
842 /// Write one register.
843 pub async fn write_reg(&mut self, reg: u8, val: u8) -> Result<(), Error<I2C::Error>> {
844 self.i2c.write(self.addr, &[reg, val]).await?;
845 Ok(())
846 }
847}
848
849#[cfg(test)]
850mod tests {
851 use super::*;
852
853 /// A calibration image read from a real SPL06-001, with the coefficient
854 /// values it decodes to asserted alongside it. Using bytes from a real part
855 /// rather than a synthetic pattern is what makes the signedness and scaling
856 /// mistakes visible.
857 const CALIBRATION_REAL: [u8; regs::CALIBRATION_LEN] = [
858 0x0c, 0xbe, 0xfc, 0x13, 0xd9, 0xaf, 0x2b, 0x34, 0xf3, 0xf7, 0x04, 0xff, 0xda, 0x5a, 0x00, 0x0a, 0xfb, 0x1b,
859 ];
860
861 /// Every coefficient at its most negative value: `c0`/`c1` = -2048,
862 /// `c00`/`c10` = -524288, the rest = -32768.
863 const CALIBRATION_SIGN_BITS: [u8; regs::CALIBRATION_LEN] = [
864 0x80, 0x08, 0x00, 0x80, 0x00, 0x08, 0x00, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00,
865 ];
866
867 /// Hand-built, physically plausible calibration used to check the integer
868 /// path against the datasheet's real-number formula.
869 const CALIBRATION_PHYSICAL: CalibrationData = CalibrationData {
870 c0: 50,
871 c1: 100,
872 c00: 100_000,
873 c10: -2_000,
874 c01: 120,
875 c11: 250,
876 c20: 1_500,
877 c21: -30,
878 c30: -200,
879 };
880
881 #[test]
882 fn calibration_decoding_matches_mit_reference() {
883 let calib = CalibrationData::from_raw(&CALIBRATION_REAL);
884
885 assert_eq!(calib.c0, 203);
886 assert_eq!(calib.c1, -260);
887 assert_eq!(calib.c00, 81306);
888 assert_eq!(calib.c10, -54476);
889 assert_eq!(calib.c01, -3081);
890 assert_eq!(calib.c11, 1279);
891 assert_eq!(calib.c20, -9638);
892 assert_eq!(calib.c21, 10);
893 assert_eq!(calib.c30, -1253);
894 }
895
896 #[test]
897 fn calibration_sign_extends_every_coefficient() {
898 let calib = CalibrationData::from_raw(&CALIBRATION_SIGN_BITS);
899
900 assert_eq!(calib.c0, -2048);
901 assert_eq!(calib.c1, -2048);
902 assert_eq!(calib.c00, -524_288);
903 assert_eq!(calib.c10, -524_288);
904 assert_eq!(calib.c01, -32_768);
905 assert_eq!(calib.c11, -32_768);
906 assert_eq!(calib.c20, -32_768);
907 assert_eq!(calib.c21, -32_768);
908 assert_eq!(calib.c30, -32_768);
909
910 // ... and a positive coefficient stays positive, i.e. the sign bits are
911 // not simply copied into every value.
912 let mut raw = CALIBRATION_SIGN_BITS;
913 raw[0] = 0x7F;
914 raw[1] = 0xF0;
915 raw[3] = 0x7F;
916 raw[4] = 0xFF;
917 raw[5] = 0xF0;
918 raw[8] = 0x7F;
919 raw[9] = 0xFF;
920 let calib = CalibrationData::from_raw(&raw);
921 assert_eq!(calib.c0, 2047);
922 assert_eq!(calib.c1, 0);
923 assert_eq!(calib.c00, 524_287);
924 assert_eq!(calib.c10, 0);
925 assert_eq!(calib.c01, 32_767);
926 }
927
928 #[test]
929 fn raw_24_bit_values_are_sign_extended() {
930 assert_eq!(decode_i24(&[0x00, 0x00, 0x01]), 1);
931 assert_eq!(decode_i24(&[0x7F, 0xFF, 0xFF]), 8_388_607);
932 assert_eq!(decode_i24(&[0x80, 0x00, 0x00]), -8_388_608);
933 assert_eq!(decode_i24(&[0xFF, 0xFF, 0xFF]), -1);
934 }
935
936 #[test]
937 fn temperature_matches_the_datasheet_formula() {
938 let calib = CalibrationData::from_raw(&CALIBRATION_REAL);
939
940 // float reference: 66.8333 degC at 8 times oversampling.
941 assert_eq!(compensate_temperature(0x100000, Oversampling::X8, &calib), 6683);
942 assert_eq!(compensate_temperature(-0x100000, Oversampling::X8, &calib), 13617);
943 // The kT factor belongs to the configured oversampling: at single
944 // oversampling the same raw value is 1000 times smaller.
945 assert_eq!(compensate_temperature(0x000800, Oversampling::Single, &calib), 10048);
946
947 assert_eq!(
948 compensate_temperature(0x040000, Oversampling::X8, &CALIBRATION_PHYSICAL),
949 2833
950 );
951 assert_eq!(
952 compensate_temperature(0x000400, Oversampling::Single, &CALIBRATION_PHYSICAL),
953 2520
954 );
955 }
956
957 #[test]
958 fn pressure_matches_the_datasheet_formula() {
959 let calib = CalibrationData::from_raw(&CALIBRATION_REAL);
960
961 let pressure = |raw_pressure: i32, raw_temperature: i32, pressure_os, temperature_os| {
962 compensate_pressure(raw_pressure, raw_temperature, pressure_os, temperature_os, &calib)
963 };
964
965 // Float references (see the crate's test-vector cross-check): the
966 // integer path agrees with the C implementations to well under 1 Pa.
967 assert_eq!(pressure(0x300000, 0x100000, Oversampling::X8, Oversampling::X8), 57551);
968 assert_eq!(
969 pressure(-0x300000, -0x100000, Oversampling::X8, Oversampling::X8),
970 102113
971 );
972 assert_eq!(
973 pressure(0x001000, 0x000800, Oversampling::Single, Oversampling::Single),
974 80868
975 );
976 assert_eq!(
977 pressure(0x400000, 0x100000, Oversampling::X64, Oversampling::X8),
978 -376763
979 );
980
981 let physical = |raw_pressure: i32, raw_temperature: i32, os| {
982 compensate_pressure(raw_pressure, raw_temperature, os, os, &CALIBRATION_PHYSICAL)
983 };
984 assert_eq!(physical(0x100000, 0x040000, Oversampling::X8), 99765);
985 assert_eq!(physical(0x000800, 0x000400, Oversampling::Single), 99992);
986 }
987
988 #[test]
989 fn pressure_uses_each_channels_own_scale_factor() {
990 let calib = CalibrationData::from_raw(&CALIBRATION_REAL);
991
992 // Temperature and pressure oversampling are independent, so the two
993 // scale factors must be looked up separately.
994 assert_eq!(
995 compensate_pressure(0x300000, 0x100000, Oversampling::X8, Oversampling::X8, &calib),
996 57551
997 );
998 assert_eq!(
999 compensate_pressure(0x300000, 0x100000, Oversampling::X64, Oversampling::X8, &calib),
1000 -206042
1001 );
1002
1003 // The trap: feeding 8-times raw data through the single-oversampling kP
1004 // is a factor of 15 out and gives a completely different pressure.
1005 let wrong = compensate_pressure(0x300000, 0x100000, Oversampling::Single, Oversampling::X8, &calib);
1006 assert_eq!(wrong, -862_506);
1007 assert!((wrong - 57551).abs() > 100_000);
1008 }
1009
1010 #[test]
1011 fn compensate_uses_the_configured_oversampling() {
1012 let calib = CalibrationData::from_raw(&CALIBRATION_REAL);
1013
1014 // An asymmetric configuration makes sure the two scale factors are not
1015 // swapped somewhere in the plumbing.
1016 let config = Config {
1017 pressure: Oversampling::X64,
1018 temperature: Oversampling::X8,
1019 };
1020 let measurements = compensate(0x300000, 0x100000, &config, &calib);
1021
1022 assert_eq!(measurements.pressure, -206042);
1023 assert_eq!(measurements.temperature, 6683);
1024 }
1025
1026 #[test]
1027 fn scale_factors_match_the_datasheet_table() {
1028 // (setting, kP/kT, needs a result shift, worst-case time in ms)
1029 let expected = [
1030 (Oversampling::Single, 524_288, false, 4),
1031 (Oversampling::X2, 1_572_864, false, 6),
1032 (Oversampling::X4, 3_670_016, false, 9),
1033 (Oversampling::X8, 7_864_320, false, 15),
1034 (Oversampling::X16, 253_952, true, 28),
1035 (Oversampling::X32, 516_096, true, 54),
1036 (Oversampling::X64, 1_040_384, true, 105),
1037 (Oversampling::X128, 2_088_960, true, 207),
1038 ];
1039
1040 for (setting, factor, shift, time_ms) in expected {
1041 assert_eq!(setting.scale_factor(), factor);
1042 assert_eq!(setting.requires_shift(), shift);
1043 assert_eq!(setting.measurement_time_ms(), time_ms);
1044 }
1045 }
1046
1047 #[test]
1048 fn control_writes_encode_the_oversampling() {
1049 assert_eq!(
1050 Config::default().control_writes(),
1051 [
1052 (regs::PRS_CFG, 0b0011),
1053 (regs::TMP_CFG, regs::TMP_CFG_EXT | 0b0011),
1054 (regs::CFG_REG, 0),
1055 ]
1056 );
1057
1058 let high = Config {
1059 pressure: Oversampling::X128,
1060 temperature: Oversampling::X16,
1061 };
1062 assert_eq!(
1063 high.control_writes(),
1064 [
1065 (regs::PRS_CFG, 0b0111),
1066 (regs::TMP_CFG, regs::TMP_CFG_EXT | 0b0100),
1067 (regs::CFG_REG, regs::PRS_SHIFT_EN | regs::TMP_SHIFT_EN),
1068 ]
1069 );
1070 }
1071
1072 #[test]
1073 fn measurements_expose_both_integer_and_float_results() {
1074 let calib = CalibrationData::from_raw(&CALIBRATION_REAL);
1075 let measurements = compensate(0x300000, 0x100000, &Config::default(), &calib);
1076
1077 assert_eq!(measurements.raw_pressure(), 0x300000);
1078 assert_eq!(measurements.raw_temperature(), 0x100000);
1079 assert_eq!(measurements.pressure, 57551);
1080 assert_eq!(measurements.temperature, 6683);
1081
1082 assert_eq!(measurements.pressure_pa(), 57551.0);
1083 assert_eq!(measurements.pressure_hpa(), 575.51);
1084 assert_eq!(measurements.temperature_celsius(), 66.83);
1085 }
1086
1087 #[test]
1088 fn constants_are_as_documented() {
1089 assert_eq!(ADDRESS, 0x77);
1090 assert_eq!(ADDRESS_ALT, 0x76);
1091 assert_eq!(PRODUCT_ID, 0x10);
1092 assert_eq!(SOFT_RESET_COMMAND, 0b1001);
1093
1094 // Register addresses (datasheet section 7).
1095 assert_eq!(regs::PRS_B2, 0x00);
1096 assert_eq!(regs::PRS_B1, 0x01);
1097 assert_eq!(regs::PRS_B0, 0x02);
1098 assert_eq!(regs::TMP_B2, 0x03);
1099 assert_eq!(regs::TMP_B1, 0x04);
1100 assert_eq!(regs::TMP_B0, 0x05);
1101 assert_eq!(regs::PRS_CFG, 0x06);
1102 assert_eq!(regs::TMP_CFG, 0x07);
1103 assert_eq!(regs::MEAS_CFG, 0x08);
1104 assert_eq!(regs::CFG_REG, 0x09);
1105 assert_eq!(regs::INT_STS, 0x0A);
1106 assert_eq!(regs::FIFO_STS, 0x0B);
1107 assert_eq!(regs::RESET, 0x0C);
1108 assert_eq!(regs::ID, 0x0D);
1109 assert_eq!(regs::COEF, 0x10);
1110 assert_eq!(regs::CALIBRATION_LEN, 18);
1111 assert_eq!(regs::DATA_LEN, 3);
1112
1113 // Register fields (datasheet section 8).
1114 assert_eq!(regs::PM_RATE_MASK, 0x70);
1115 assert_eq!(regs::PM_PRC_MASK, 0x0F);
1116 assert_eq!(regs::TMP_CFG_EXT, 0x80);
1117 assert_eq!(regs::TMP_RATE_MASK, 0x70);
1118 assert_eq!(regs::TMP_PRC_MASK, 0x07);
1119 assert_eq!(regs::COEF_RDY, 0x80);
1120 assert_eq!(regs::SENSOR_RDY, 0x40);
1121 assert_eq!(regs::TMP_RDY, 0x20);
1122 assert_eq!(regs::PRS_RDY, 0x10);
1123 assert_eq!(regs::MEAS_CTRL_MASK, 0x07);
1124 assert_eq!(regs::MEAS_PRESSURE, 0b001);
1125 assert_eq!(regs::MEAS_TEMPERATURE, 0b010);
1126 assert_eq!(regs::TMP_SHIFT_EN, 0x08);
1127 assert_eq!(regs::PRS_SHIFT_EN, 0x04);
1128 assert_eq!(regs::FIFO_EN, 0x02);
1129 assert_eq!(regs::SPI_MODE, 0x01);
1130 assert_eq!(regs::FIFO_FLUSH, 0x80);
1131 assert_eq!(regs::SOFT_RST_MASK, 0x0F);
1132 assert_eq!(regs::PROD_ID_MASK, 0xF0);
1133 assert_eq!(regs::REV_ID_MASK, 0x0F);
1134
1135 assert_eq!(FRACTIONAL_BITS, 24);
1136 }
1137}