29#ifndef PF1550_I2C_INTERFACE_HPP
30#define PF1550_I2C_INTERFACE_HPP
103template <
typename Derived>
128template <typename Port>
130 detail::ExactHook<Port, bool()
noexcept>::Take(&Port::EnsureInitialized);
131 detail::ExactHook<Port, bool(uint8_t,
const uint8_t*, std::size_t)
noexcept>::Take(&Port::Write);
132 detail::ExactHook<Port, bool(uint8_t,
const uint8_t*, std::size_t, uint8_t*,
133 std::size_t)
noexcept>::Take(&Port::WriteRead);
140inline constexpr std::size_t kMaxBurst = 1;
143template <std::
size_t N>
144concept BurstSize = (N >= 1U) && (N <= kMaxBurst);
148template <I2cPort Port>
151 RegisterBus(Port& port, uint8_t address) noexcept : port_(port), address_(address) {}
153 Port& GetPort() &
noexcept {
158 template <std::
size_t N>
159 requires BurstSize<N>
160 Error Read(uint8_t reg, std::span<uint8_t, N> out)
noexcept {
161 return Read(reg, std::span<uint8_t>{out});
166 Error Read(uint8_t reg, std::span<uint8_t> out)
noexcept {
167 if (out.empty() || (out.size() > kMaxBurst)) {
168 return Error::InvalidArgument;
173 if (!port_.WriteRead(address_, pointer_.data(), pointer_.size(), rx_.data(), out.size())) {
176 for (std::size_t i = 0U; i < out.size(); ++i) {
183 template <std::
size_t N>
184 requires BurstSize<N>
185 Error Write(uint8_t reg, std::span<const uint8_t, N> data)
noexcept {
186 return Write(reg, std::span<const uint8_t>{data});
191 Error Write(uint8_t reg, std::span<const uint8_t> data)
noexcept {
192 if (data.empty() || (data.size() > kMaxBurst)) {
193 return Error::InvalidArgument;
196 const std::size_t count = data.size();
197 for (std::size_t i = 0U; i < count; ++i) {
198 frame_[1U + i] = data[i];
200 return port_.Write(address_, frame_.data(), count + 1U) ? Error::Ok : Error::Bus;
206 std::array<uint8_t, 1> pointer_{};
207 std::array<uint8_t, kMaxBurst> rx_{};
208 std::array<uint8_t, 1U + kMaxBurst> frame_{};
CRTP base of the platform I2C port.
Definition pf1550_i2c_interface.hpp:104
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 pf1550_i2c_interface.hpp:129
Error
Why a call failed. Every call of the driver returns one of these in its Result.
Definition pf1550_types.hpp:38
The PF1550 driver: everything it declares lives here; internals in detail.
Definition pf1550.hpp:39
constexpr uint32_t kMaxClockHz
Highest SCL frequency: fast mode (section 9.7.2).
Definition pf1550_i2c_interface.hpp:86
constexpr bool kRepeatedStart
true: a register read is a combined write-read with a repeated START (Figure 25).
Definition pf1550_i2c_interface.hpp:90
constexpr uint32_t kMinEdgeNs
Shortest SCL and SDA rise and fall time the chip accepts, in nanoseconds (section 9....
Definition pf1550_i2c_interface.hpp:88
constexpr uint8_t kMinI2cAddress
Lowest address OTP can select (I2C_ADDR[2:0] = 000).
Definition pf1550_i2c_interface.hpp:82
constexpr uint8_t kMaxI2cAddress
Highest address OTP can select (I2C_ADDR[2:0] = 111).
Definition pf1550_i2c_interface.hpp:84
CtrlPin
Control pins the port may reach through the optional GpioSet() and GpioGet() hooks.
Definition pf1550_i2c_interface.hpp:45
@ Intb
INTB (pin 9), open-drain output of the chip: GpioGet() reports asserted = low.
constexpr uint8_t kI2cAddress
Default 7-bit I2C address (section 9.7.1).
Definition pf1550_i2c_interface.hpp:80
GpioSignal
Logical level of a control pin; the port maps it to the physical polarity.
Definition pf1550_i2c_interface.hpp:52
@ ACTIVE
The pin function is asserted.
@ INACTIVE
The pin function is deasserted.
I2cFailure
Why the last transfer failed, reported by the optional GetLastFailure() hook.
Definition pf1550_i2c_interface.hpp:58
@ DataNack
The address was acknowledged, a data byte was not.
@ AddressNack
Nothing acknowledged the address: no chip, wrong address or unpowered.
@ None
The last transfer succeeded.
@ Bus
Arbitration lost, a stuck line or a timeout.
Compile-time helpers of the PF1550 driver: typed integer literals, exact hook matching,...
Public types of the PF1550 driver: errors, Result<T>, enumerations, units, status and interrupt types...