37#ifndef MAX22200_SPI_INTERFACE_HPP
38#define MAX22200_SPI_INTERFACE_HPP
89static_assert((
kMaxClockHz * 100ULL) == 1'000'000'000ULL,
"tCLK min 100 ns");
115template <
typename Derived>
134template <
typename Port>
136 requires { ExactHook<Port, bool(
CtrlPin,
GpioSignal)
noexcept>::Take(&Port::GpioSet); };
153template <
typename Port>
155 std::derived_from<Port, SpiInterface<Port>> && detail::HasGpioSet<Port> &&
requires {
156 detail::ExactHook<Port, bool()
noexcept>::Take(&Port::EnsureInitialized);
157 detail::ExactHook<Port, bool(
const uint8_t*, uint8_t*, std::size_t)
noexcept>::Take(
165constexpr std::array<uint8_t, 4> LsbFirst(uint32_t v)
noexcept {
166 return {
static_cast<uint8_t
>(v & 0xFFU),
static_cast<uint8_t
>((v >> 8U) & 0xFFU),
167 static_cast<uint8_t
>((v >> 16U) & 0xFFU),
static_cast<uint8_t
>(v >> 24U)};
169static_assert((LsbFirst(0x1122'3344_u32)[0] == 0x44U) && (LsbFirst(0x1122'3344_u32)[3] == 0x11U),
170 "Figure 10: the least significant byte goes first");
174 uint8_t status{0xFF};
185constexpr bool ResetByte(uint8_t status)
noexcept {
186 return (status != 0xFFU) && ((
static_cast<uint32_t
>(status) & reg::kActive) == 0U);
188static_assert(ResetByte(0x02U) && !ResetByte(0xFFU) && !ResetByte(0x01U),
189 "p36: STATUS[7:0] reads 0x02 (UVM, ACTIVE = 0) after a power-on reset");
194constexpr bool StopOnComer(uint8_t status)
noexcept {
195 return (status != 0xFFU) && ((
static_cast<uint32_t
>(status) & reg::kComer) != 0U);
209template <SpiPort Port>
212 explicit RegisterBus(Port& port) noexcept : port_(port) {}
214 Port& GetPort() &
noexcept {
222 Error Read32(uint8_t bank, uint32_t sdi, uint32_t& value, Reply& reply,
bool guard)
noexcept {
224 if (
const Error e = command(reg::Command(bank,
false,
false), reply, guard); e != Error::Ok) {
227 const std::array<uint8_t, 4> tx = LsbFirst(sdi);
228 std::array<uint8_t, 4> rx = {0xFF, 0xFF, 0xFF, 0xFF};
229 if (
const Error e = data<4>(tx, rx); e != Error::Ok) {
232 value = (
static_cast<uint32_t
>(rx[0]) << 24U) | (
static_cast<uint32_t
>(rx[1]) << 16U) |
233 (
static_cast<uint32_t
>(rx[2]) << 8U) |
static_cast<uint32_t
>(rx[3]);
240 Error Write32(uint8_t bank, uint32_t value, Reply& reply,
bool guard,
241 bool stop_on_comer)
noexcept {
242 if (
const Error e = command(reg::Command(bank,
true,
false), reply, guard); e != Error::Ok) {
245 if (stop_on_comer && StopOnComer(reply.status)) {
246 return Error::CommunicationError;
248 const std::array<uint8_t, 4> tx = LsbFirst(value);
249 std::array<uint8_t, 4> rx = {0xFF, 0xFF, 0xFF, 0xFF};
250 if (
const Error e = data<4>(tx, rx); e != Error::Ok) {
258 Error Read8(uint8_t bank, uint8_t& value, Reply& reply,
bool guard)
noexcept {
260 if (
const Error e = command(reg::Command(bank,
false,
true), reply, guard); e != Error::Ok) {
263 const std::array<uint8_t, 1> tx = {0x00};
264 std::array<uint8_t, 1> rx = {0xFF};
265 const Error e = data<1>(tx, rx);
272 Error Write8(uint8_t bank, uint8_t value, Reply& reply,
bool guard,
bool stop_on_comer)
noexcept {
273 if (
const Error e = command(reg::Command(bank,
true,
true), reply, guard); e != Error::Ok) {
276 if (stop_on_comer && StopOnComer(reply.status)) {
277 return Error::CommunicationError;
279 return Data8(value, reply);
286 Error Data8(uint8_t value, Reply& reply)
noexcept {
287 const std::array<uint8_t, 1> tx = {value};
288 std::array<uint8_t, 1> rx = {0xFF};
289 if (
const Error e = data<1>(tx, rx); e != Error::Ok) {
297 Error SetPin(CtrlPin pin, GpioSignal signal)
noexcept {
298 return port_.GpioSet(pin, signal) ? Error::Ok : Error::Pin;
306 Error command(uint8_t cmd, Reply& reply,
bool guard)
noexcept {
308 if (!port_.GpioSet(CtrlPin::Cmd, GpioSignal::ACTIVE)) {
311 const std::array<uint8_t, 1> tx = {cmd};
312 std::array<uint8_t, 1> rx = {0xFF};
313 if (!port_.Transfer(tx.data(), rx.data(), rx.size())) {
317 return (guard && ResetByte(rx[0])) ? Error::DeviceReset : Error::Ok;
321 static void take(uint8_t status, Reply& reply)
noexcept {
322 reply.status = status;
323 if (status != 0xFFU) {
325 static_cast<uint8_t
>(
static_cast<uint32_t
>(reply.flags) |
static_cast<uint32_t
>(status));
333 template <std::
size_t N>
335 Error data(std::span<const uint8_t, N> tx, std::span<uint8_t, N> rx)
noexcept {
336 if (!port_.GpioSet(CtrlPin::Cmd, GpioSignal::INACTIVE)) {
337 (void)port_.GpioSet(CtrlPin::Cmd, GpioSignal::ACTIVE);
340 const bool sent = port_.Transfer(tx.data(), rx.data(), rx.size());
341 const bool high = port_.GpioSet(CtrlPin::Cmd, GpioSignal::ACTIVE);
345 return high ? Error::Ok : Error::Pin;
CRTP base of the platform SPI port.
Definition max22200_spi_interface.hpp:116
SpiInterface(const SpiInterface &)=delete
Ports are not copied: the driver holds a reference.
SpiInterface & operator=(const SpiInterface &)=delete
Ports are not copied: the driver holds a reference.
SpiInterface() noexcept=default
Only the derived port is constructed.
Satisfied by a class that derives from SpiInterface<Port> and implements the required hooks with exac...
Definition max22200_spi_interface.hpp:154
Error
Why a call failed. Every call of the driver returns one of these in its Result.
Definition max22200_types.hpp:39
Compile-time helpers of the MAX22200 driver: exact hook matching, typed register fields and enumerati...
MAX22200 register map: the Command Register, the 32-bit register banks, their fields,...
Public types of the MAX22200 driver: errors, Result<T>, the channel and chip settings,...
The MAX22200 driver: everything it declares lives here; internals in detail.
Definition max22200.hpp:27
constexpr bool kContinuousWord
Definition max22200_spi_interface.hpp:77
constexpr uint8_t kSpiMode
SPI mode 0: CPOL = 0, CPHA = 0 ("SPI Detailed Description": the only mode the chip supports).
Definition max22200_spi_interface.hpp:60
constexpr bool kSdoOpenDrain
SDO is open drain (Pin Description): the board fits a pull-up.
Definition max22200_spi_interface.hpp:86
constexpr uint32_t kMaxClockHz
Definition max22200_spi_interface.hpp:68
constexpr uint32_t kCsSetupNs
CSB falling edge to the first SCK rising edge, tCSS.
Definition max22200_spi_interface.hpp:72
constexpr bool kMsbFirst
Definition max22200_spi_interface.hpp:64
constexpr uint32_t kMinCsHighNs
Shortest time CSB stays high between two transfers, tCSPW.
Definition max22200_spi_interface.hpp:70
constexpr uint32_t kCsHoldNs
Last SCK edge to CSB rising, tCSH.
Definition max22200_spi_interface.hpp:74
GpioSignal
Logical level of a control pin; the port maps it to the physical polarity.
Definition max22200_spi_interface.hpp:100
@ ACTIVE
The pin function is asserted.
@ INACTIVE
The pin function is deasserted.
constexpr uint32_t kCmdSetupNs
Definition max22200_spi_interface.hpp:81
constexpr std::array< std::size_t, 2 > kTransferBytes
Definition max22200_spi_interface.hpp:84
CtrlPin
The chip's control pins the port drives or reads.
Definition max22200_spi_interface.hpp:92
@ Enable
ENABLE, active high: ACTIVE wakes the chip, INACTIVE puts it to sleep (< 11 µA).
@ Cmd
CMD, active high: ACTIVE while the Command Register is written.