MAX22200 Driver 0.1.0
API reference
Loading...
Searching...
No Matches
max22200_spi_interface.hpp
Go to the documentation of this file.
1// clang-format off: the hook table below must keep one row per line.
36// clang-format on
37#ifndef MAX22200_SPI_INTERFACE_HPP
38#define MAX22200_SPI_INTERFACE_HPP
39
40#include <array>
41#include <concepts>
42#include <cstddef>
43#include <cstdint>
44#include <span>
45
46#include "max22200_detail.hpp"
48#include "max22200_types.hpp"
49
50namespace max22200 {
51
60inline constexpr uint8_t kSpiMode = 0;
64inline constexpr bool kMsbFirst = true;
68inline constexpr uint32_t kMaxClockHz = 10'000'000;
70inline constexpr uint32_t kMinCsHighNs = 30;
72inline constexpr uint32_t kCsSetupNs = 30;
74inline constexpr uint32_t kCsHoldNs = 30;
77inline constexpr bool kContinuousWord = false;
81inline constexpr uint32_t kCmdSetupNs = 20;
84inline constexpr std::array<std::size_t, 2> kTransferBytes = {1, 4};
86inline constexpr bool kSdoOpenDrain = true;
89static_assert((kMaxClockHz * 100ULL) == 1'000'000'000ULL, "tCLK min 100 ns");
90
92enum class CtrlPin : uint8_t {
93 Enable = 0,
94 Cmd = 1,
95 Fault = 2,
97};
98
100enum class GpioSignal : uint8_t {
101 INACTIVE = 0,
102 ACTIVE = 1,
103};
104
115template <typename Derived>
117public:
119 SpiInterface(const SpiInterface&) = delete;
122
123protected:
125 SpiInterface() noexcept = default;
127 ~SpiInterface() noexcept = default;
128};
129
130namespace detail {
131
134template <typename Port>
135concept HasGpioSet =
136 requires { ExactHook<Port, bool(CtrlPin, GpioSignal) noexcept>::Take(&Port::GpioSet); };
137
138} // namespace detail
139
153template <typename Port>
154concept SpiPort =
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(
158 &Port::Transfer);
159 };
160
161namespace detail {
162
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)};
168}
169static_assert((LsbFirst(0x1122'3344_u32)[0] == 0x44U) && (LsbFirst(0x1122'3344_u32)[3] == 0x11U),
170 "Figure 10: the least significant byte goes first");
171
173struct Reply {
174 uint8_t status{0xFF};
176 uint8_t echo{0xFF};
177 uint8_t flags{0};
179};
180
185constexpr bool ResetByte(uint8_t status) noexcept {
186 return (status != 0xFFU) && ((static_cast<uint32_t>(status) & reg::kActive) == 0U);
187}
188static_assert(ResetByte(0x02U) && !ResetByte(0xFFU) && !ResetByte(0x01U),
189 "p36: STATUS[7:0] reads 0x02 (UVM, ACTIVE = 0) after a power-on reset");
190
194constexpr bool StopOnComer(uint8_t status) noexcept {
195 return (status != 0xFFU) && ((static_cast<uint32_t>(status) & reg::kComer) != 0U);
196}
197
209template <SpiPort Port>
210class RegisterBus {
211public:
212 explicit RegisterBus(Port& port) noexcept : port_(port) {}
213
214 Port& GetPort() & noexcept {
215 return port_;
216 }
217
222 Error Read32(uint8_t bank, uint32_t sdi, uint32_t& value, Reply& reply, bool guard) noexcept {
223 value = 0xFFFFFFFFU;
224 if (const Error e = command(reg::Command(bank, false, false), reply, guard); e != Error::Ok) {
225 return e;
226 }
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) {
230 return e;
231 }
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]);
234 return Error::Ok;
235 }
236
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) {
243 return e;
244 }
245 if (stop_on_comer && StopOnComer(reply.status)) {
246 return Error::CommunicationError;
247 }
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) {
251 return e;
252 }
253 reply.echo = rx[1];
254 return Error::Ok;
255 }
256
258 Error Read8(uint8_t bank, uint8_t& value, Reply& reply, bool guard) noexcept {
259 value = 0xFF;
260 if (const Error e = command(reg::Command(bank, false, true), reply, guard); e != Error::Ok) {
261 return e;
262 }
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);
266 value = rx[0];
267 return e;
268 }
269
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) {
274 return e;
275 }
276 if (stop_on_comer && StopOnComer(reply.status)) {
277 return Error::CommunicationError;
278 }
279 return Data8(value, reply);
280 }
281
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) {
290 return e;
291 }
292 take(rx[0], reply);
293 return Error::Ok;
294 }
295
297 Error SetPin(CtrlPin pin, GpioSignal signal) noexcept {
298 return port_.GpioSet(pin, signal) ? Error::Ok : Error::Pin;
299 }
300
301private:
306 Error command(uint8_t cmd, Reply& reply, bool guard) noexcept {
307 reply = Reply{};
308 if (!port_.GpioSet(CtrlPin::Cmd, GpioSignal::ACTIVE)) {
309 return Error::Pin;
310 }
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())) {
314 return Error::Bus;
315 }
316 take(rx[0], reply);
317 return (guard && ResetByte(rx[0])) ? Error::DeviceReset : Error::Ok;
318 }
319
321 static void take(uint8_t status, Reply& reply) noexcept {
322 reply.status = status;
323 if (status != 0xFFU) {
324 reply.flags =
325 static_cast<uint8_t>(static_cast<uint32_t>(reply.flags) | static_cast<uint32_t>(status));
326 }
327 }
328
333 template <std::size_t N>
334 requires((N == kTransferBytes[0]) || (N == kTransferBytes[1]))
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);
338 return Error::Pin;
339 }
340 const bool sent = port_.Transfer(tx.data(), rx.data(), rx.size());
341 const bool high = port_.GpioSet(CtrlPin::Cmd, GpioSignal::ACTIVE);
342 if (!sent) {
343 return Error::Bus;
344 }
345 return high ? Error::Ok : Error::Pin;
346 }
347
348 Port& port_;
349};
350
351} // namespace detail
352
353} // namespace max22200
354
355#endif // MAX22200_SPI_INTERFACE_HPP
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.