28#ifndef BMP581_I2C_INTERFACE_HPP
29#define BMP581_I2C_INTERFACE_HPP
78template <
typename Derived>
103template <typename Port>
105 detail::ExactHook<Port, bool()
noexcept>::Take(&Port::EnsureInitialized);
106 detail::ExactHook<Port, bool(uint8_t,
const uint8_t*, std::size_t)
noexcept>::Take(&Port::Write);
107 detail::ExactHook<Port, bool(uint8_t,
const uint8_t*, std::size_t, uint8_t*,
108 std::size_t)
noexcept>::Take(&Port::WriteRead);
114template <
typename Port>
115concept DeclaresGetLastFailure =
requires {
116 &Port::GetLastFailure;
117} ||
requires(Port& port, I2cFailure& failure) { port.GetLastFailure(failure); };
120template <
typename Port>
121concept HasGetLastFailure =
122 requires { ExactHook<Port, bool(I2cFailure&)
noexcept>::Take(&Port::GetLastFailure); };
133template <I2cPort Port>
134class I2cRegisterBus {
137 explicit I2cRegisterBus(Port& port, uint8_t address = i2c::kI2cAddress) noexcept
138 : port_(port), address_(address) {}
141 Port& GetPort() &
noexcept {
146 [[nodiscard]] uint8_t GetAddress() const noexcept {
151 [[nodiscard]]
bool AddressValid() const noexcept {
152 return (address_ == i2c::kI2cAddress) || (address_ == i2c::kI2cAddressSdoHigh);
156 Error SelectInterface() noexcept {
162 Error Read(uint8_t reg, std::span<uint8_t> out)
noexcept {
163 if (out.empty() || (out.size() > kMaxBurst)) {
164 return Error::InvalidArgument;
166 const std::array<uint8_t, 1> pointer{reg};
167 if (port_.WriteRead(address_, pointer.data(), pointer.size(), out.data(), out.size())) {
174 Error WriteByte(uint8_t reg, uint8_t value)
noexcept {
175 const std::array<uint8_t, 2> frame{reg, value};
176 return port_.Write(address_, frame.data(), frame.size()) ? Error::Ok : failure();
182 Error WriteResetCommand() noexcept {
183 const std::array<uint8_t, 2> frame{reg::kCmd, reg::kCmdSoftReset};
184 if (port_.Write(address_, frame.data(), frame.size())) {
187 return (failure() == Error::NoDevice) ? Error::NoDevice : Error::Ok;
192 Error failure() noexcept {
193 if constexpr (HasGetLastFailure<Port>) {
195 if (port_.GetLastFailure(reason) && (reason == I2cFailure::AddressNack)) {
196 return Error::NoDevice;
Compile-time helpers of the BMP581 driver: typed integer literals, exact hook matching,...
BMP581 register map: addresses, bit fields and reset values (datasheet section 7).
Public types of the BMP581 driver: errors, Result<T>, configuration, measurements and the datasheet's...
CRTP base of the platform I2C port.
Definition bmp581_i2c_interface.hpp:79
I2cInterface() noexcept=default
Only the derived port is constructed.
I2cInterface(const I2cInterface &)=delete
Ports are not copied: the driver holds a reference.
I2cInterface & operator=(const I2cInterface &)=delete
Ports are not copied: the driver holds a reference.
Satisfied by a class that derives from I2cInterface<Port> and implements the required hooks with exac...
Definition bmp581_i2c_interface.hpp:104
constexpr uint32_t kFifoMaxBytes
Bytes of a full FIFO, either frame size.
Definition bmp581_types.hpp:381
Error
Why a call failed. Every call of the driver returns one of these in its Result.
Definition bmp581_types.hpp:40
What the BMP581 needs from the I2C bus (sections 5.6, 6.2.3, table 4). A port and a board configurati...
Definition bmp581_i2c_interface.hpp:45
constexpr uint32_t kMinClockHz
Lowest SCL frequency (table 4, f_i2c).
Definition bmp581_i2c_interface.hpp:53
constexpr uint32_t kMaxClockHz
Highest SCL frequency: fast mode plus, with C_bus below 100 pF (table 4, f_i2c).
Definition bmp581_i2c_interface.hpp:51
constexpr bool kRepeatedStart
Definition bmp581_i2c_interface.hpp:56
constexpr uint8_t kI2cAddressSdoHigh
7-bit address with SDO high (section 5.6).
Definition bmp581_i2c_interface.hpp:49
constexpr uint8_t kI2cAddress
7-bit address with SDO low (section 5.6). SDO must not float on I2C.
Definition bmp581_i2c_interface.hpp:47
The BMP581 driver: everything it declares lives here; internals in detail.
Definition bmp581.hpp:32
I2cFailure
Why an I2C transfer failed, as the optional GetLastFailure() hook reports it.
Definition bmp581_i2c_interface.hpp:62
@ DataNack
The device acknowledged its address, then not a data byte.
@ AddressNack
No device acknowledged the address byte.
@ Other
Arbitration lost, a stuck line, a timeout, or a NACK the port cannot place.