BMP581 Driver 0.1.0
API reference
Loading...
Searching...
No Matches
bmp581_i2c_interface.hpp
Go to the documentation of this file.
1// clang-format off: the table below must keep one row per line.
27// clang-format on
28#ifndef BMP581_I2C_INTERFACE_HPP
29#define BMP581_I2C_INTERFACE_HPP
30
31#include <array>
32#include <concepts>
33#include <cstddef>
34#include <cstdint>
35#include <span>
36
37#include "bmp581_detail.hpp"
38#include "bmp581_registers.hpp"
39#include "bmp581_types.hpp"
40
45namespace bmp581::i2c {
47inline constexpr uint8_t kI2cAddress = 0x46;
49inline constexpr uint8_t kI2cAddressSdoHigh = 0x47;
51inline constexpr uint32_t kMaxClockHz = 1'000'000;
53inline constexpr uint32_t kMinClockHz = 100'000;
56inline constexpr bool kRepeatedStart = false;
57} // namespace bmp581::i2c
58
59namespace bmp581 {
60
62enum class I2cFailure : uint8_t {
63 Other = 0,
64 AddressNack = 1,
65 DataNack = 2,
66};
67
78template <typename Derived>
80public:
82 I2cInterface(const I2cInterface&) = delete;
85
86protected:
88 I2cInterface() noexcept = default;
90 ~I2cInterface() noexcept = default;
91};
92
103template <typename Port>
104concept I2cPort = std::derived_from<Port, I2cInterface<Port>> && requires {
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);
109};
110
111namespace detail {
112
114template <typename Port>
115concept DeclaresGetLastFailure = requires {
116 &Port::GetLastFailure;
117} || requires(Port& port, I2cFailure& failure) { port.GetLastFailure(failure); };
118
120template <typename Port>
121concept HasGetLastFailure =
122 requires { ExactHook<Port, bool(I2cFailure&) noexcept>::Take(&Port::GetLastFailure); };
123
125inline constexpr std::size_t kMaxBurst = kFifoMaxBytes;
126
133template <I2cPort Port>
134class I2cRegisterBus {
135public:
137 explicit I2cRegisterBus(Port& port, uint8_t address = i2c::kI2cAddress) noexcept
138 : port_(port), address_(address) {}
139
141 Port& GetPort() & noexcept {
142 return port_;
143 }
144
146 [[nodiscard]] uint8_t GetAddress() const noexcept {
147 return address_;
148 }
149
151 [[nodiscard]] bool AddressValid() const noexcept {
152 return (address_ == i2c::kI2cAddress) || (address_ == i2c::kI2cAddressSdoHigh);
153 }
154
156 Error SelectInterface() noexcept {
157 return Error::Ok;
158 }
159
162 Error Read(uint8_t reg, std::span<uint8_t> out) noexcept {
163 if (out.empty() || (out.size() > kMaxBurst)) {
164 return Error::InvalidArgument;
165 }
166 const std::array<uint8_t, 1> pointer{reg};
167 if (port_.WriteRead(address_, pointer.data(), pointer.size(), out.data(), out.size())) {
168 return Error::Ok;
169 }
170 return failure();
171 }
172
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();
177 }
178
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())) {
185 return Error::Ok;
186 }
187 return (failure() == Error::NoDevice) ? Error::NoDevice : Error::Ok;
188 }
189
190private:
192 Error failure() noexcept {
193 if constexpr (HasGetLastFailure<Port>) {
194 I2cFailure reason = I2cFailure::Other;
195 if (port_.GetLastFailure(reason) && (reason == I2cFailure::AddressNack)) {
196 return Error::NoDevice;
197 }
198 }
199 return Error::Bus;
200 }
201
202 Port& port_;
203 uint8_t address_;
204};
205
206} // namespace detail
207
208} // namespace bmp581
209
210#endif // BMP581_I2C_INTERFACE_HPP
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.