PF1550 Driver 0.1.0
API reference
Loading...
Searching...
No Matches
pf1550_i2c_interface.hpp
Go to the documentation of this file.
1// clang-format off: the table below must keep one row per line.
28// clang-format on
29#ifndef PF1550_I2C_INTERFACE_HPP
30#define PF1550_I2C_INTERFACE_HPP
31
32#include <array>
33#include <concepts>
34#include <cstddef>
35#include <cstdint>
36#include <span>
37
38#include "pf1550_detail.hpp"
39#include "pf1550_types.hpp"
40
41namespace pf1550 {
42
45enum class CtrlPin : uint8_t {
46 Standby = 0,
48 Intb = 1,
49};
50
52enum class GpioSignal : uint8_t {
53 INACTIVE = 0,
54 ACTIVE = 1,
55};
56
58enum class I2cFailure : uint8_t {
59 None = 0,
60 AddressNack = 1,
61 DataNack = 2,
62 Bus = 3,
63};
64
65namespace detail {
67template <>
68struct EnumLast<I2cFailure> {
69 static constexpr I2cFailure kValue = I2cFailure::Bus;
70};
71} // namespace detail
72
80inline constexpr uint8_t kI2cAddress = 0x08;
82inline constexpr uint8_t kMinI2cAddress = 0x08;
84inline constexpr uint8_t kMaxI2cAddress = 0x0F;
86inline constexpr uint32_t kMaxClockHz = 400'000;
88inline constexpr uint32_t kMinEdgeNs = 20;
90inline constexpr bool kRepeatedStart = true;
103template <typename Derived>
105public:
107 I2cInterface(const I2cInterface&) = delete;
110
111protected:
113 I2cInterface() noexcept = default;
115 ~I2cInterface() noexcept = default;
116};
117
128template <typename Port>
129concept I2cPort = std::derived_from<Port, I2cInterface<Port>> && requires {
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);
134};
135
136namespace detail {
137
140inline constexpr std::size_t kMaxBurst = 1;
141
143template <std::size_t N>
144concept BurstSize = (N >= 1U) && (N <= kMaxBurst);
145
148template <I2cPort Port>
149class RegisterBus {
150public:
151 RegisterBus(Port& port, uint8_t address) noexcept : port_(port), address_(address) {}
152
153 Port& GetPort() & noexcept {
154 return port_;
155 }
156
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});
162 }
163
166 Error Read(uint8_t reg, std::span<uint8_t> out) noexcept {
167 if (out.empty() || (out.size() > kMaxBurst)) {
168 return Error::InvalidArgument;
169 }
170 // The buffers the port sees are members of this object, never on the caller's stack, so a
171 // port whose bus reads them by DMA reaches them (hardware lessons 20).
172 pointer_[0] = reg;
173 if (!port_.WriteRead(address_, pointer_.data(), pointer_.size(), rx_.data(), out.size())) {
174 return Error::Bus;
175 }
176 for (std::size_t i = 0U; i < out.size(); ++i) {
177 out[i] = rx_[i];
178 }
179 return Error::Ok;
180 }
181
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});
187 }
188
191 Error Write(uint8_t reg, std::span<const uint8_t> data) noexcept {
192 if (data.empty() || (data.size() > kMaxBurst)) {
193 return Error::InvalidArgument;
194 }
195 frame_[0] = reg;
196 const std::size_t count = data.size();
197 for (std::size_t i = 0U; i < count; ++i) {
198 frame_[1U + i] = data[i];
199 }
200 return port_.Write(address_, frame_.data(), count + 1U) ? Error::Ok : Error::Bus;
201 }
202
203private:
204 Port& port_;
205 uint8_t address_;
206 std::array<uint8_t, 1> pointer_{};
207 std::array<uint8_t, kMaxBurst> rx_{};
208 std::array<uint8_t, 1U + kMaxBurst> frame_{};
209};
210
211} // namespace detail
212
213} // namespace pf1550
214
215#endif // PF1550_I2C_INTERFACE_HPP
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...