30#include "bmp581_version.h"
39template <
typename Port>
49template <
typename Port>
50concept DeclaresDelayUs =
requires(Port& port) { port.DelayUs(1_u32); };
54template <
typename Port>
55concept HasDelayUs =
requires { ExactHook<Port, void(uint32_t)
noexcept>::Take(&Port::DelayUs); };
58template <
typename Port>
59concept DeclaresLog =
requires(Port& port) { port.Log(
LogLevel::Info,
""); };
62template <
typename Port>
64 requires { ExactHook<Port, void(
LogLevel,
const char*)
noexcept>::Take(&Port::Log); };
67template <BusPort Port>
70template <BusPort Port>
73 using Type = I2cRegisterBus<Port>;
75 static constexpr bool kSelectionFrame =
false;
78template <BusPort Port>
81 using Type = SpiRegisterBus<Port>;
84 static constexpr bool kSelectionFrame =
true;
89template <
typename Port>
90void DelayUs(Port& port, uint32_t us)
noexcept {
91 if constexpr (HasDelayUs<Port>) {
94 static_assert(kAlwaysFalse<Port>,
95 "bmp581: the port must implement `void DelayUs(uint32_t us) noexcept`");
100template <
typename Port>
101void Log(Port& port, [[maybe_unused]]
LogLevel level,
102 [[maybe_unused]]
const char* message)
noexcept {
103 if constexpr (HasLog<Port>) {
104 port.Log(level, message);
114[[nodiscard]]
constexpr uint8_t SetIf(uint8_t value, uint8_t bit,
bool on)
noexcept {
115 return on ?
static_cast<uint8_t
>(
static_cast<uint32_t
>(value) |
static_cast<uint32_t
>(bit))
120[[nodiscard]]
constexpr bool Has(uint8_t value, uint8_t bits)
noexcept {
121 return (
static_cast<uint32_t
>(value) &
static_cast<uint32_t
>(bits)) != 0U;
125[[nodiscard]]
constexpr uint8_t IntConfigValue(
const Config& c)
noexcept {
126 auto v = reg::IntConfigMode::Insert(0_u8, ToUnderlying(c.int_mode));
127 v = reg::IntConfigPol::Insert(v, ToUnderlying(c.int_polarity));
128 v = reg::IntConfigOd::Insert(v, ToUnderlying(c.int_drive));
129 v = reg::IntConfigEn::Insert(v, c.int_pin_enable ? 1_u32 : 0_u32);
130 return reg::IntConfigPadDrv::Insert(v, c.int_pad_drive);
134[[nodiscard]]
constexpr uint8_t IntSourceValue(
const Config& c)
noexcept {
142[[nodiscard]]
constexpr uint8_t FifoConfigValue(
const Config& c)
noexcept {
143 return reg::FifoModeBit::Insert(reg::FifoThreshold::Insert(0_u8, c.fifo_threshold),
144 ToUnderlying(c.fifo_mode));
148[[nodiscard]]
constexpr uint8_t FifoSelValue(
const Config& c)
noexcept {
149 return reg::FifoDecSel::Insert(reg::FifoFrameSel::Insert(0_u8, ToUnderlying(c.fifo_frame)),
155[[nodiscard]]
constexpr uint8_t DspConfigValue(
const Config& c)
noexcept {
156 auto v = reg::DspCompPtEn::Insert(0_u8, 3_u32);
164[[nodiscard]]
constexpr uint8_t DspIirValue(
const Config& c)
noexcept {
165 return reg::DspIirP::Insert(reg::DspIirT::Insert(0_u8, ToUnderlying(c.temperature_iir)),
166 ToUnderlying(c.pressure_iir));
170[[nodiscard]]
constexpr uint8_t OorConfigValue(
const Config& c)
noexcept {
171 return reg::OorCntLim::Insert(reg::OorThrP16::Insert(0_u8, c.oor_reference_pa >> 16U),
172 ToUnderlying(c.oor_count));
178[[nodiscard]]
constexpr uint8_t OsrConfigValue(
const Config& c)
noexcept {
180 reg::OsrP::Insert(reg::OsrT::Insert(0_u8, ToUnderlying(c.temperature_oversampling)),
181 ToUnderlying(c.pressure_oversampling));
186[[nodiscard]]
constexpr uint8_t OdrConfigValue(
const Config& c,
PowerMode mode)
noexcept {
188 reg::OdrPwrMode::Insert(reg::OdrOdr::Insert(0_u8, ToUnderlying(c.odr)), ToUnderlying(mode));
193struct RegisterWrite {
202[[nodiscard]]
constexpr std::array<RegisterWrite, 12> ConfigWrites(
const Config& c)
noexcept {
209 {.reg =
reg::kOorThrPLsb, .value =
static_cast<uint8_t
>(c.oor_reference_pa & 0xFFU)},
210 {.reg =
reg::kOorThrPMsb, .value =
static_cast<uint8_t
>((c.oor_reference_pa >> 8U) & 0xFFU)},
220[[nodiscard]]
constexpr std::array<uint8_t, 5> ExpectedBlockA(
const Config& c)
noexcept {
221 return {IntConfigValue(c), IntSourceValue(c), FifoConfigValue(c), 0_u8, FifoSelValue(c)};
225[[nodiscard]]
constexpr std::array<uint8_t, 8> ExpectedBlockB(
const Config& c)
noexcept {
226 return {DspConfigValue(c),
228 static_cast<uint8_t
>(c.oor_reference_pa & 0xFFU),
229 static_cast<uint8_t
>((c.oor_reference_pa >> 8U) & 0xFFU),
238[[nodiscard]]
constexpr uint8_t OdrAwakeStandby(
const Config& c)
noexcept {
248inline constexpr std::array<uint8_t, 2> kMarker{0x3CU, 0xC3U};
251[[nodiscard]]
constexpr bool OsrShowsReset(uint8_t osr)
noexcept {
256[[nodiscard]]
constexpr InterruptStatus DecodeIntStatus(uint8_t raw)
noexcept {
265[[nodiscard]]
constexpr uint32_t Le24(std::span<const uint8_t, 3> b)
noexcept {
266 return static_cast<uint32_t
>(b[0]) | (
static_cast<uint32_t
>(b[1]) << 8U) |
267 (
static_cast<uint32_t
>(b[2]) << 16U);
271inline constexpr std::size_t kDataFrameBytes = 12U;
273inline constexpr std::size_t kDataIntStatus = 10U;
275inline constexpr std::size_t kDataStatus = 11U;
278[[nodiscard]]
constexpr bool AllBytes(std::span<const uint8_t> bytes, uint8_t value)
noexcept {
279 return std::ranges::all_of(bytes, [value](uint8_t b)
noexcept ->
bool {
return b == value; });
286[[nodiscard]]
constexpr bool DataFrameValid(std::span<const uint8_t, kDataFrameBytes> f)
noexcept {
287 const auto reserved = f.subspan<6, 4>();
288 return AllBytes(reserved, 0_u8) && !Has(f[kDataIntStatus], 0xE0_u8) &&
294[[nodiscard]]
constexpr Result<Measurement> DecodeMeasurement(uint32_t temperature,
295 uint32_t pressure)
noexcept {
299 const Measurement m{.pressure_raw =
SignExtend24(pressure),
309[[nodiscard]]
constexpr uint32_t FifoFrameBytes(
FifoFrame frame)
noexcept {
324[[nodiscard]]
constexpr uint32_t FifoCapacity(
FifoFrame frame)
noexcept {
330[[nodiscard]]
constexpr FifoSample DecodeFifoValue(FifoSample s, uint32_t raw,
331 bool pressure)
noexcept {
343 s.has_pressure =
true;
344 s.pressure_raw = value;
346 s.has_temperature =
true;
347 s.temperature_raw = value;
353[[nodiscard]]
constexpr FifoSample DecodeFifoFrame(
FifoFrame frame,
354 std::span<const uint8_t, 6> b)
noexcept {
355 const uint32_t first = Le24(b.first<3>());
357 const FifoSample t = DecodeFifoValue(FifoSample{}, first,
false);
358 return DecodeFifoValue(t, Le24(b.last<3>()),
true);
368static_assert((IntSourceValue(Config{}) == 0U) && (FifoConfigValue(Config{}) == 0U) &&
369 (FifoSelValue(Config{}) == 0U) && (DspIirValue(Config{}) == 0U) &&
370 (OorConfigValue(Config{}) == 0U));
375static_assert(OorConfigValue(Config{.oor_reference_pa = 97'150U}) == 0x01U);
376static_assert(ConfigWrites(Config{.oor_reference_pa = 97'150U})[6].value == 0x7BU);
377static_assert(ConfigWrites(Config{.oor_reference_pa = 97'150U})[5].value == 0x7EU);
379static_assert(Le24(std::array<uint8_t, 3>{0x00U, 0xA6U, 0x0EU}) == 0x0E'A600U);
381 std::array<uint8_t, 6>{0x00U, 0xA6U, 0x5EU, 0U, 0U, 0U})
382 .pressure_raw == 6'202'880);
384 std::array<uint8_t, 6>{0x7FU, 0x7FU, 0x7FU, 0U, 0U, 0U})
386static_assert(DecodeMeasurement(0x7F'7F7FU, 0x7F'7F7FU).error ==
Error::NoData);
387static_assert(DecodeMeasurement(0x19'0000U, 0x61'A800U).value.pressure_raw == 6'400'000);
446template <BusPort Port>
448 static_assert(!detail::DeclaresDelayUs<Port> || detail::HasDelayUs<Port>,
449 "bmp581: the DelayUs hook must be `void DelayUs(uint32_t us) noexcept`, so that no "
450 "wait is truncated");
451 static_assert(!detail::DeclaresLog<Port> || detail::HasLog<Port>,
452 "bmp581: the optional Log hook must be `void Log(LogLevel, const char* message) "
454 static_assert(!detail::DeclaresGetLastFailure<Port> || detail::HasGetLastFailure<Port>,
455 "bmp581: the optional GetLastFailure hook must be `bool "
456 "GetLastFailure(I2cFailure& failure) noexcept`");
465 explicit BMP581(Port& port,
const Config& config = {})
noexcept : bus_(port), config_(config) {}
474 BMP581(Port& port, uint8_t address,
const Config& config = {})
noexcept
476 : bus_(port, address), config_(config) {}
565 return config_known_;
699 return detail::DecodeIntStatus(latched_int_);
709 return last_int_status_;
719 return last_osr_eff_;
745 using Bus =
typename detail::BusFor<Port>::Type;
748 Result<
void> resetChip() noexcept;
749 Result<
void> identify() noexcept;
750 Result<
void> waitNvmReady() noexcept;
751 Result<
void> checkResetFlags() noexcept;
753 void leaveDeepStandby() noexcept;
754 Result<
void> configure(const
Config& config, uint8_t odr) noexcept;
755 void waitFifoFlush() noexcept;
756 Result<
bool> markerCleared() noexcept;
757 Result<
void> ensureMarker() noexcept;
758 Result<
void> waitForcedShown() noexcept;
759 Result<
void> checkSelectionFrame() noexcept;
760 Result<
void> writeConfig(const
Config& config) noexcept;
761 Result<
void> readBackConfig(const
Config& config) noexcept;
762 Result<
void> enterStandby(const
Config& config) noexcept;
765 Result<
void> afterFailedSequence(
Error own) noexcept;
766 Result<
void> standbyAfterStart() noexcept;
769 Result<uint8_t> readFifoCountRaw() noexcept;
770 Result<std::
size_t> readFifoFrames(std::span<
FifoSample> out, uint32_t count) noexcept;
771 Result<uint8_t> readIntStatus() noexcept;
772 void noteIntStatus(uint8_t raw) noexcept;
773 Result<uint8_t> checkSentinel() noexcept;
774 [[nodiscard]]
bool sentinelEvidence(const
Result<std::array<uint8_t, 2>>& pair) const noexcept;
775 Result<std::array<uint8_t, 2>> readSentinelPair() noexcept;
778 Result<
bool> configHeld() noexcept;
779 Result<uint8_t> checkBeforeWrite() noexcept;
780 Result<
void> gate() noexcept;
781 Result<uint8_t> readByte(uint8_t reg) noexcept;
782 Error read(uint8_t reg, std::
size_t count) noexcept;
783 Error write(uint8_t reg, uint8_t value) noexcept;
784 Error markReset() noexcept;
785 Result<
void> fail(
Error error, const
char* message) noexcept;
789 std::array<uint8_t, kFifoMaxBytes> buffer_{};
790 uint8_t revision_{0};
791 uint8_t last_int_status_{0};
792 uint8_t last_status_{0};
793 uint8_t last_osr_eff_{0};
794 uint8_t latched_int_{0};
795 bool initialized_{
false};
796 bool started_once_{
false};
797 bool config_known_{
false};
798 bool check_pending_{
false};
799 bool por_unconfirmed_{
false};
800 bool marker_fresh_{
false};
802 bool sentinel_doubt_{
false};
811#include "../src/bmp581.ipp"
Compile-time helpers of the BMP581 driver: typed integer literals, exact hook matching,...
The I2C port of the BMP581 driver: the CRTP base, the I2cPort concept, the bus requirements and the r...
BMP581 register map: addresses, bit fields and reset values (datasheet section 7).
The SPI port of the BMP581 driver: the CRTP base, the SpiPort concept, the bus requirements and the r...
Public types of the BMP581 driver: errors, Result<T>, configuration, measurements and the datasheet's...
Driver for one BMP581 on a platform port.
Definition bmp581.hpp:447
Result< Measurement > MeasureOnce(Verify verify=Verify::None) noexcept
One forced measurement (section 4.3.3): the reset check, ODR_CONFIG with pwr_mode = forced,...
BMP581(Port &port, const Config &config={}) noexcept
Bind the driver to a port (on I2C at i2c::kI2cAddress) and store the configuration....
Definition bmp581.hpp:465
uint8_t GetLastStatus() const noexcept
Definition bmp581.hpp:713
Result< std::size_t > ReadFifo(std::span< FifoSample > out, Verify verify=Verify::None) noexcept
Read up to out.size() frames from the FIFO: FIFO_COUNT, then one burst of whole frames from FIFO_DATA...
Result< void > CheckForReset() noexcept
Find out whether the chip reset since the start: one 3-byte read of OSR_CONFIG to OSR_EFF,...
void ClearLatchedInterrupts() noexcept
Empty the latch of GetLatchedInterrupts(). No I/O.
Definition bmp581.hpp:703
uint8_t GetLastIntStatus() const noexcept
Definition bmp581.hpp:708
BMP581 & operator=(const BMP581 &)=delete
One driver object per chip.
BMP581(Port &port, uint8_t address, const Config &config={}) noexcept
Bind the driver to an I2C port at a 7-bit address and store the configuration. No I/O.
Definition bmp581.hpp:474
Result< Measurement > ReadMeasurement(Verify verify=Verify::None) noexcept
Read the latest measurement from the data registers: one 12-byte read of TEMP_DATA to STATUS (section...
Result< void > SetPowerMode(PowerMode mode) noexcept
Change the power mode (section 4.3.7).
Result< void > WaitDataReady() noexcept
Wait for new data in normal or continuous mode: INT_STATUS polled every kPollUs until drdy_data_reg,...
InterruptStatus GetLatchedInterrupts() const noexcept
The INT_STATUS flags seen by any read since ClearLatchedInterrupts(), the driver's own reads included...
Definition bmp581.hpp:698
Result< InterruptStatus > ReadInterruptStatus() noexcept
Read INT_STATUS once (section 7.21): clears its flags on the chip; the driver latches them....
bool IsInitialized() const noexcept
Definition bmp581.hpp:518
Result< void > Deinitialize() noexcept
Put the chip in its lowest-power state with the soft reset, CMD = 0xB6: it leaves the chip in deep st...
Result< void > EnsureInitialized() noexcept
Start the chip. Once started, a later call runs the reset check (one 3-byte read) and returns Ok,...
void GetConfig() const &&=delete
Not on a temporary driver: the reference would dangle (KB-CPP20-044).
uint8_t GetLastOsrEff() const noexcept
Definition bmp581.hpp:718
BMP581(const BMP581 &)=delete
One driver object per chip.
const Config & GetConfig() const &noexcept
Definition bmp581.hpp:556
Result< uint8_t > ReadRegister(uint8_t address) noexcept
Read one register once: one transfer, no retry, no decoding (handbook KB-CPP-129).
bool IsConfigKnown() const noexcept
Definition bmp581.hpp:564
Result< void > ApplyConfig(const Config &config) noexcept
Replace the configuration.
Result< uint8_t > ReadFifoCount() noexcept
Read FIFO_COUNT (section 4.6.2). With the FIFO on, a count of 0 runs the reset check: a reset empties...
uint8_t GetRevision() const noexcept
Definition bmp581.hpp:723
A port the driver accepts: an I2cPort or an SpiPort, never both, so the bus framing is never ambiguou...
Definition bmp581.hpp:40
Satisfied by a class that derives from I2cInterface<Port> and implements the required hooks with exac...
Definition bmp581_i2c_interface.hpp:104
Satisfied by a class that derives from SpiInterface<Port> and implements the required hooks with exac...
Definition bmp581_spi_interface.hpp:100
constexpr uint8_t kFifoConfig
FIFO_CONFIG (R/W): threshold, mode (7.7).
Definition bmp581_registers.hpp:42
constexpr uint8_t kOsrConfigReset
OSR_CONFIG: x1, x1, pressure off.
Definition bmp581_registers.hpp:76
constexpr uint8_t kIntPor
INT_STATUS only: POR or soft reset complete.
Definition bmp581_registers.hpp:99
constexpr uint8_t kDspIirFlushForced
iir_flush_forced_en.
Definition bmp581_registers.hpp:121
constexpr uint8_t kOdrConfig
ODR_CONFIG (R/W): power mode, ODR (7.34).
Definition bmp581_registers.hpp:67
constexpr uint8_t kOorConfig
OOR_CONFIG (R/W): reference bit 16, count (7.32).
Definition bmp581_registers.hpp:65
constexpr uint8_t kIntFifoThs
FIFO threshold reached.
Definition bmp581_registers.hpp:97
constexpr uint8_t kFifoSel
FIFO_SEL (R/W): frame type, decimation (7.9).
Definition bmp581_registers.hpp:44
constexpr uint8_t kOsrConfig
OSR_CONFIG (R/W): oversampling, press_en (7.33).
Definition bmp581_registers.hpp:66
constexpr uint8_t kOorThrPMsb
OOR_THR_P_MSB (R/W): reference bits 15..8.
Definition bmp581_registers.hpp:63
constexpr uint8_t kOorThrPLsb
OOR_THR_P_LSB (R/W): reference bits 7..0.
Definition bmp581_registers.hpp:62
constexpr uint8_t kIntConfig
INT_CONFIG (R/W): the INT pin (section 7.5).
Definition bmp581_registers.hpp:40
constexpr uint8_t kDspIir
DSP_IIR (R/W): IIR coefficients (7.28).
Definition bmp581_registers.hpp:61
constexpr uint8_t kIntConfigReset
Definition bmp581_registers.hpp:73
constexpr uint8_t kStatusReserved
reserved_6_5: read 0.
Definition bmp581_registers.hpp:117
constexpr uint8_t kDspOorSelIirP
Out-of-range check after the IIR.
Definition bmp581_registers.hpp:126
constexpr uint32_t kDataReset
Definition bmp581_registers.hpp:79
constexpr uint8_t kOorRange
OOR_RANGE (R/W): window, Pa (7.31).
Definition bmp581_registers.hpp:64
constexpr uint8_t kIntDrdy
Data ready (drdy_data_reg).
Definition bmp581_registers.hpp:95
constexpr uint8_t kDspConfigReset
DSP_CONFIG: both compensated, IIR off.
Definition bmp581_registers.hpp:75
constexpr uint8_t kOdrConfigReset
ODR_CONFIG: standby, 1 Hz, deep standby on.
Definition bmp581_registers.hpp:77
constexpr uint8_t kDspShdwSelIir
Both data registers after the IIR.
Definition bmp581_registers.hpp:127
constexpr uint8_t kDspFifoSelIir
Both FIFO values after the IIR.
Definition bmp581_registers.hpp:128
constexpr uint8_t kOdrDeepDis
deep_dis: deep standby disabled.
Definition bmp581_registers.hpp:156
constexpr uint8_t kDspConfig
DSP_CONFIG (R/W): compensation, IIR routing.
Definition bmp581_registers.hpp:60
constexpr uint8_t kIntStatusAfterReset
Definition bmp581_registers.hpp:82
constexpr uint8_t kIntFifoFull
FIFO full.
Definition bmp581_registers.hpp:96
constexpr uint8_t kIntSource
INT_SOURCE (R/W): interrupt sources (7.6).
Definition bmp581_registers.hpp:41
constexpr uint8_t kOsrPressEn
press_en: pressure measured as well.
Definition bmp581_registers.hpp:150
constexpr uint8_t kIntOorP
Pressure out of range.
Definition bmp581_registers.hpp:98
constexpr bool TemperatureInRange(int32_t raw) noexcept
Whether a raw temperature lies in the measurement range, -40 to 85 °C (table 2).
Definition bmp581_types.hpp:635
constexpr Config HighResolutionPreset() noexcept
High resolution at 25 Hz: pressure x16 (table 9), the IIR on the data registers, data ready reported.
Definition bmp581_types.hpp:722
LogLevel
Severity passed to the optional Log() hook.
Definition bmp581_types.hpp:205
FifoFrame
What the FIFO stores, FIFO_SEL.fifo_frame_sel (section 4.6.1).
Definition bmp581_types.hpp:289
constexpr bool PressureInRange(int32_t raw) noexcept
Whether a raw pressure lies in the measurement range, 30 to 125 kPa (table 1).
Definition bmp581_types.hpp:625
constexpr uint32_t kFifoFramesSingle
Frames the FIFO holds with 3-byte frames (section 4.6).
Definition bmp581_types.hpp:377
PowerMode
The power mode, ODR_CONFIG.pwr_mode (sections 4.3, 7.34).
Definition bmp581_types.hpp:220
Verify
Extra traffic a call may spend to check what it did (handbook KB-ARCH-041).
Definition bmp581_types.hpp:213
constexpr uint32_t kFifoFramesBoth
Frames the FIFO holds with 6-byte frames (section 4.6).
Definition bmp581_types.hpp:379
constexpr int32_t SignExtend24(uint32_t raw) noexcept
A 24-bit two's complement register value as a signed integer, the one place the driver reinterprets w...
Definition bmp581_types.hpp:597
Error
Why a call failed. Every call of the driver returns one of these in its Result.
Definition bmp581_types.hpp:40
@ Info
Lifecycle events: initialised, reset.
@ Pressure
3-byte pressure frames, up to 32.
@ Disabled
FIFO off. Reset value.
@ PressureAndTemperature
6-byte frames, temperature first, up to 16.
@ Temperature
3-byte temperature frames, up to 32.
@ None
Only the traffic the datasheet needs.
The BMP581 driver: everything it declares lives here; internals in detail.
Definition bmp581.hpp:32
Everything EnsureInitialized() and ApplyConfig() write to the chip.
Definition bmp581_types.hpp:411
One FIFO frame, with its own validity: a multi-item read reports each item.
Definition bmp581_types.hpp:352
Error error
Ok, NoData (0x7F7F7F) or OutOfRange; the values are 0 then.
Definition bmp581_types.hpp:353
The interrupt status flags, INT_STATUS (section 7.21), decoded. Reading INT_STATUS clears them on the...
Definition bmp581_types.hpp:332
bool data_ready
drdy_data_reg: new data in the data registers and the FIFO.
Definition bmp581_types.hpp:333
bool power_on_reset
por: a power-on or soft reset completed.
Definition bmp581_types.hpp:337
One measurement in the chip's own units, as the data registers hold it (section 4....
Definition bmp581_types.hpp:346
The value of a call, or why there is none.
Definition bmp581_types.hpp:145
static constexpr Result Failure(Error e) noexcept
A failed result.
Definition bmp581_types.hpp:171
static constexpr Result Success(T v) noexcept
A successful result.
Definition bmp581_types.hpp:163