27#ifndef PCAL9555_I2C_INTERFACE_HPP
28#define PCAL9555_I2C_INTERFACE_HPP
81template <
typename Derived>
106template <typename Port>
108 detail::ExactHook<Port, bool()
noexcept>::Take(&Port::EnsureInitialized);
109 detail::ExactHook<Port, bool(uint8_t,
const uint8_t*, std::size_t)
noexcept>::Take(&Port::Write);
110 detail::ExactHook<Port, bool(uint8_t,
const uint8_t*, std::size_t, uint8_t*,
111 std::size_t)
noexcept>::Take(&Port::WriteRead);
122template <
typename Port>
123concept DeclaresGetLastFailure =
requires {
124 &Port::GetLastFailure;
125} ||
requires(Port& port, I2cFailure& failure) { port.GetLastFailure(failure); };
130template <
typename Port>
131concept HasGetLastFailure =
132 requires { ExactHook<Port, bool(I2cFailure&)
noexcept>::Take(&Port::GetLastFailure); };
135template <
typename Port>
137 requires { &Port::Log; } ||
requires(Port& port) { port.Log(LogLevel::Info,
""); };
140template <
typename Port>
142 requires { ExactHook<Port, void(LogLevel,
const char*)
noexcept>::Take(&Port::Log); };
145template <
typename Port>
146void Log(Port& port, [[maybe_unused]] LogLevel level,
147 [[maybe_unused]]
const char* message)
noexcept {
148 if constexpr (HasLog<Port>) {
149 port.Log(level, message);
157template <
typename Port>
158[[nodiscard]]
Error TransferError(Port& port)
noexcept {
159 if constexpr (HasGetLastFailure<Port>) {
161 if (port.GetLastFailure(failure) && (failure == I2cFailure::AddressNack)) {
162 return Error::NoDevice;
171inline constexpr std::size_t kMaxBurst = 2;
174template <std::
size_t N>
175concept BurstSize = (N >= 1U) && (N <= kMaxBurst);
181template <I2cPort Port>
184 RegisterBus(Port& port, uint8_t address) noexcept : port_(port), address_(address) {}
186 Port& GetPort() &
noexcept {
190 [[nodiscard]] uint8_t GetAddress() const noexcept {
195 template <std::
size_t N>
196 requires BurstSize<N>
197 Error Read(uint8_t reg, std::span<uint8_t, N> out)
noexcept {
198 return Read(reg, std::span<uint8_t>{out});
203 Error Read(uint8_t reg, std::span<uint8_t> out)
noexcept {
204 if (out.empty() || (out.size() > kMaxBurst)) {
205 return Error::InvalidArgument;
207 const std::array<uint8_t, 1> command{reg};
208 return port_.WriteRead(address_, command.data(), command.size(), out.data(), out.size())
210 : TransferError(port_);
214 template <std::
size_t N>
215 requires BurstSize<N>
216 Error Write(uint8_t reg, std::span<const uint8_t, N> data)
noexcept {
217 return Write(reg, std::span<const uint8_t>{data});
222 Error Write(uint8_t reg, std::span<const uint8_t> data)
noexcept {
223 if (data.empty() || (data.size() > kMaxBurst)) {
224 return Error::InvalidArgument;
226 std::array<uint8_t, 1U + kMaxBurst> frame{};
228 const std::size_t count = data.size();
229 for (std::size_t i = 0U; i < count; ++i) {
230 frame[1U + i] = data[i];
232 return port_.Write(address_, frame.data(), count + 1U) ? Error::Ok : TransferError(port_);
CRTP base of the platform I2C port.
Definition pcal9555_i2c_interface.hpp:82
I2cInterface & operator=(const I2cInterface &)=delete
Ports are not copied: the driver holds a reference.
I2cInterface() noexcept=default
Only the derived port is constructed.
I2cInterface(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 pcal9555_i2c_interface.hpp:107
Error
Why a call failed. Every call of the driver returns one of these in its Result.
Definition pcal9555_types.hpp:36
constexpr uint8_t kMinI2cAddress
Lowest 7-bit I2C address (A2, A1, A0 all low): 0100 A2 A1 A0 (data sheet section 6....
Definition pcal9555_types.hpp:249
The PCAL9555 driver: everything it declares lives here; internals in detail.
Definition pcal9555.hpp:26
constexpr uint32_t kMaxClockHz
Highest SCL frequency: fast mode, 400 kHz (section 13, table 33, f_SCL).
Definition pcal9555_i2c_interface.hpp:51
I2cFailure
Why a Write() or WriteRead() hook returned false, as the optional GetLastFailure() hook reports it.
Definition pcal9555_i2c_interface.hpp:65
@ DataNack
The device acknowledged its address, then did not acknowledge 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.
constexpr bool kRepeatedStart
Definition pcal9555_i2c_interface.hpp:54
constexpr uint8_t kI2cAddress
Definition pcal9555_i2c_interface.hpp:49
Compile-time helpers of the PCAL9555 driver: exact hook matching and enumeration checks.
Public types of the PCAL9555 driver: errors, Result<T>, pins and configuration.