31#ifndef PCA9685_I2C_INTERFACE_HPP
32#define PCA9685_I2C_INTERFACE_HPP
98template <
typename Derived>
124template <typename Port>
126 detail::ExactHook<Port, bool()
noexcept>::Take(&Port::EnsureInitialized);
127 detail::ExactHook<Port, bool(uint8_t,
const uint8_t*, std::size_t)
noexcept>::Take(&Port::Write);
128 detail::ExactHook<Port, bool(uint8_t,
const uint8_t*, std::size_t, uint8_t*,
129 std::size_t)
noexcept>::Take(&Port::WriteRead);
139template <
typename Port>
140concept DeclaresDelayUs =
141 requires { &Port::DelayUs; } ||
requires(Port& port) { port.DelayUs(1_u32); };
144template <
typename Port>
145concept HasDelayUs =
requires { ExactHook<Port, void(uint32_t)
noexcept>::Take(&Port::DelayUs); };
148template <
typename Port>
149concept DeclaresGpioSet =
requires { &Port::GpioSet; } ||
requires(Port& port) {
150 port.GpioSet(CtrlPin::OutputEnable, GpioSignal::INACTIVE);
154template <
typename Port>
156 requires { ExactHook<Port, bool(CtrlPin, GpioSignal)
noexcept>::Take(&Port::GpioSet); };
159template <
typename Port>
160concept DeclaresGetLastFailure =
requires {
161 &Port::GetLastFailure;
162} ||
requires(Port& port,
I2cFailure& failure) { port.GetLastFailure(failure); };
166template <
typename Port>
167concept HasGetLastFailure =
168 requires { ExactHook<Port, bool(I2cFailure&)
noexcept>::Take(&Port::GetLastFailure); };
171template <
typename Port>
173 requires { &Port::Log; } ||
requires(Port& port) { port.Log(LogLevel::Info,
""); };
176template <
typename Port>
178 requires { ExactHook<Port, void(LogLevel,
const char*)
noexcept>::Take(&Port::Log); };
182template <
typename Port>
183void DelayUs(Port& port, uint32_t us)
noexcept {
184 if constexpr (HasDelayUs<Port>) {
187 static_assert(kAlwaysFalse<Port>,
188 "pca9685: the port must implement `void DelayUs(uint32_t us) noexcept`");
193template <
typename Port>
194void Log(Port& port, [[maybe_unused]] LogLevel level,
195 [[maybe_unused]]
const char* message)
noexcept {
196 if constexpr (HasLog<Port>) {
197 port.Log(level, message);
205template <
typename Port>
206[[nodiscard]]
Error TransferError(Port& port)
noexcept {
207 if constexpr (HasGetLastFailure<Port>) {
209 if (port.GetLastFailure(failure) && (failure == I2cFailure::AddressNack)) {
210 return Error::NoDevice;
219inline constexpr std::size_t kMaxBurst =
220 static_cast<std::size_t
>(reg::kChannels) * reg::kBytesPerChannel;
223template <std::
size_t N>
224concept BurstSize = (N >= 1U) && (N <= kMaxBurst);
230template <I2cPort Port>
233 RegisterBus(Port& port, uint8_t address) noexcept : port_(port), address_(address) {}
235 Port& GetPort() &
noexcept {
239 [[nodiscard]] uint8_t GetAddress() const noexcept {
244 template <std::
size_t N>
245 requires BurstSize<N>
246 Error Read(uint8_t reg, std::span<uint8_t, N> out)
noexcept {
247 return Read(reg, std::span<uint8_t>{out});
252 Error Read(uint8_t reg, std::span<uint8_t> out)
noexcept {
253 if (out.empty() || (out.size() > kMaxBurst)) {
254 return Error::InvalidArgument;
256 const std::array<uint8_t, 1> pointer{reg};
257 return port_.WriteRead(address_, pointer.data(), pointer.size(), out.data(), out.size())
259 : TransferError(port_);
263 template <std::
size_t N>
264 requires BurstSize<N>
265 Error Write(uint8_t reg, std::span<const uint8_t, N> data)
noexcept {
266 return Write(reg, std::span<const uint8_t>{data});
271 Error Write(uint8_t reg, std::span<const uint8_t> data)
noexcept {
272 if (data.empty() || (data.size() > kMaxBurst)) {
273 return Error::InvalidArgument;
275 std::array<uint8_t, 1U + kMaxBurst> frame{};
277 const std::size_t count = data.size();
278 for (std::size_t i = 0U; i < count; ++i) {
279 frame[1U + i] = data[i];
281 return port_.Write(address_, frame.data(), count + 1U) ? Error::Ok : TransferError(port_);
CRTP base of the platform I2C port.
Definition pca9685_i2c_interface.hpp:99
I2cInterface(const I2cInterface &)=delete
Ports are not copied: the driver holds a reference.
I2cInterface() noexcept=default
Only the derived port is constructed.
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 bus hooks with ...
Definition pca9685_i2c_interface.hpp:125
constexpr uint8_t kMinI2cAddress
Lowest I2C address of a PCA9685: 1 A5 A4 A3 A2 A1 A0 with every pin low (figure 4).
Definition pca9685_types.hpp:273
Error
Why a call failed. Every call of the driver returns one of these in its Result.
Definition pca9685_types.hpp:39
The PCA9685 driver: everything it declares lives here; internals in detail.
Definition pca9685.hpp:30
I2cFailure
Why a Write() or WriteRead() hook returned false, as the optional GetLastFailure() hook reports it.
Definition pca9685_i2c_interface.hpp:82
@ 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.
constexpr bool kRepeatedStart
Definition pca9685_i2c_interface.hpp:57
constexpr uint8_t kI2cAddress
7-bit I2C address with A5 to A0 low, 1000000 (section 7.1.1, figure 4); the pins add 0 to 63.
Definition pca9685_i2c_interface.hpp:52
GpioSignal
Logical level of a control pin; the port maps it to the physical polarity.
Definition pca9685_i2c_interface.hpp:68
@ ACTIVE
The pin function is asserted: for OE, outputs enabled (pin low).
@ INACTIVE
The pin function is deasserted: for OE, outputs disabled (pin high).
CtrlPin
Control pins the port may drive through the optional GpioSet() hook.
Definition pca9685_i2c_interface.hpp:61
constexpr uint32_t kMaxClockHz
Highest SCL frequency: Fast-mode Plus, 1 MHz (table 15, f_SCL).
Definition pca9685_i2c_interface.hpp:54
Compile-time helpers of the PCA9685 driver: typed integer literals, exact hook matching,...
Public types of the PCA9685 driver: errors, Result<T>, channel values, configuration,...