HardFOC Core
API reference
Loading...
Searching...
No Matches
Max22200Handler.h
Go to the documentation of this file.
1
27#ifndef COMPONENT_HANDLER_MAX22200_HANDLER_H_
28#define COMPONENT_HANDLER_MAX22200_HANDLER_H_
29
30#include <cstdint>
31#include <memory>
32#include <type_traits>
33#include <utility>
34#include "core/hf-core-drivers/external/hf-max22200-driver/inc/max22200.hpp"
35#include "core/hf-core-drivers/external/hf-max22200-driver/inc/max22200_spi_interface.hpp"
36#include "core/hf-core-drivers/external/hf-max22200-driver/inc/max22200_types.hpp"
37#include "base/BaseSpi.h"
38#include "base/BaseGpio.h"
39#include "RtosMutex.h"
40
45
55class HalSpiMax22200Comm : public max22200::SpiInterface<HalSpiMax22200Comm> {
56public:
64 HalSpiMax22200Comm(BaseSpi& spi, BaseGpio& enable, BaseGpio& cmd,
65 BaseGpio* fault = nullptr) noexcept;
66
69
71 bool Initialize() noexcept;
72
80 bool Transfer(const uint8_t* tx_data, uint8_t* rx_data, size_t length) noexcept;
81
86 bool SetChipSelect(bool state) noexcept;
87
92 bool Configure(uint32_t speed_hz, uint8_t mode, bool msb_first = true) noexcept;
93
95 [[nodiscard]] bool IsReady() const noexcept;
96
98 void DelayUs(uint32_t us) noexcept;
99
104 void GpioSet(max22200::CtrlPin pin, max22200::GpioSignal signal) noexcept;
105
111 bool GpioRead(max22200::CtrlPin pin, max22200::GpioSignal& signal) noexcept;
112
114
115private:
116 BaseSpi& spi_;
117 BaseGpio& enable_;
118 BaseGpio& cmd_;
119 BaseGpio* fault_;
120 bool initialized_{false};
121};
122
124
129
151public:
153 using DriverType = max22200::MAX22200<HalSpiMax22200Comm>;
154
156 static constexpr uint8_t kNumChannels = 8;
157
158 //=========================================================================
159 // Construction
160 //=========================================================================
161
169 Max22200Handler(BaseSpi& spi, BaseGpio& enable, BaseGpio& cmd,
170 BaseGpio* fault = nullptr) noexcept;
171
172 ~Max22200Handler() noexcept;
173
174 // Non-copyable
176 Max22200Handler& operator=(const Max22200Handler&) = delete;
177
178 // Non-movable
180 Max22200Handler& operator=(Max22200Handler&&) = delete;
181
182 //=========================================================================
183 // Initialization
184 //=========================================================================
185
194 max22200::DriverStatus Initialize() noexcept;
195
202 [[nodiscard]] bool EnsureInitialized() noexcept;
203
212 max22200::DriverStatus Initialize(const max22200::BoardConfig& board_config) noexcept;
213
218 max22200::DriverStatus Deinitialize() noexcept;
219
221 [[nodiscard]] bool IsInitialized() const noexcept { return initialized_; }
222
231 [[nodiscard]] uint8_t GetLastFaultByte() const noexcept;
232
233 //=========================================================================
234 // Channel Configuration
235 //=========================================================================
236
243 max22200::DriverStatus ConfigureChannel(uint8_t channel, const max22200::ChannelConfig& config) noexcept;
244
259 max22200::DriverStatus SetupCdrChannel(uint8_t channel, uint16_t hit_ma,
260 uint16_t hold_ma, float hit_time_ms) noexcept;
261
273 max22200::DriverStatus SetupVdrChannel(uint8_t channel, float hit_duty_pct,
274 float hold_duty_pct, float hit_time_ms) noexcept;
275
276 //=========================================================================
277 // Channel Control
278 //=========================================================================
279
285 max22200::DriverStatus EnableChannel(uint8_t channel) noexcept;
286
292 max22200::DriverStatus DisableChannel(uint8_t channel) noexcept;
293
295 max22200::DriverStatus EnableAllChannels() noexcept;
296
298 max22200::DriverStatus DisableAllChannels() noexcept;
299
305 [[nodiscard]] bool IsChannelEnabled(uint8_t channel) noexcept;
306
312 max22200::DriverStatus SetChannelsMask(uint8_t mask) noexcept;
313
314 //=========================================================================
315 // Status & Faults
316 //=========================================================================
317
323 max22200::DriverStatus GetStatus(max22200::StatusConfig& status) noexcept;
324
334 max22200::DriverStatus GetChannelFaults(uint8_t channel, max22200::FaultStatus& faults) noexcept;
335
340 [[nodiscard]] bool HasFault() noexcept;
341
343 max22200::DriverStatus ClearFaults() noexcept;
344
345 //=========================================================================
346 // Device Control
347 //=========================================================================
348
356 max22200::DriverStatus ReadFaultRegister(max22200::FaultStatus& faults) noexcept;
357
358 //=========================================================================
359 // Direct Driver Access
360 //=========================================================================
361
376 template <typename Fn>
377 auto WithDriver(Fn&& fn) noexcept {
378 using R = std::invoke_result_t<Fn, DriverType&>;
379 if constexpr (std::is_void_v<R>) {
380 MutexLockGuard lock(mutex_);
381 if (!EnsureInitializedLocked() || !driver_) return false;
382 fn(*driver_);
383 return true;
384 } else {
385 return withDriver(std::forward<Fn>(fn));
386 }
387 }
388
398 [[nodiscard]] DriverType* GetDriver() noexcept;
399
406 [[nodiscard]] const DriverType* GetDriver() const noexcept;
407
413 void DumpDiagnostics() noexcept;
414
415private:
417 max22200::DriverStatus InitializeLocked() noexcept;
418 bool EnsureInitializedLocked() noexcept;
419
437 bool WaitForActiveAndDrainFaults() noexcept;
438
445 template <typename Fn>
446 auto withDriver(Fn&& fn) noexcept {
447 using R = std::invoke_result_t<Fn, DriverType&>;
448 static_assert(!std::is_void_v<R>, "withDriver requires non-void return");
449 MutexLockGuard lock(mutex_);
450 if (!EnsureInitializedLocked() || !driver_) {
451 if constexpr (std::is_same_v<R, max22200::DriverStatus>)
452 return max22200::DriverStatus::INITIALIZATION_ERROR;
453 else
454 return R{};
455 }
456 return fn(*driver_);
457 }
458
459 bool initialized_{false};
460 mutable RtosMutex mutex_;
461 std::unique_ptr<HalSpiMax22200Comm> comm_;
462 std::unique_ptr<DriverType> driver_;
464 uint8_t last_fault_byte_{0xFF};
465};
466
468
469#endif // COMPONENT_HANDLER_MAX22200_HANDLER_H_
CRTP SPI communication adapter for MAX22200 using BaseSpi and BaseGpio.
Definition Max22200Handler.h:55
bool initialized_
Definition Max22200Handler.h:120
BaseGpio * fault_
Definition Max22200Handler.h:119
void DelayUs(uint32_t us) noexcept
Blocking microsecond delay (handler_utils::DelayUs).
Definition Max22200Handler.cpp:151
BaseGpio & enable_
Definition Max22200Handler.h:117
BaseGpio & cmd_
Definition Max22200Handler.h:118
bool SetChipSelect(bool state) noexcept
Soft-CS no-op — chip select is owned by StmSpiDevice.
Definition Max22200Handler.cpp:132
bool IsReady() const noexcept
True when comm adapter and all bound GPIO/SPI objects are ready.
Definition Max22200Handler.cpp:143
bool GpioRead(max22200::CtrlPin pin, max22200::GpioSignal &signal) noexcept
Read ENABLE, CMD, or nFAULT level.
Definition Max22200Handler.cpp:191
bool Configure(uint32_t speed_hz, uint8_t mode, bool msb_first=true) noexcept
SPI mode/speed no-op — BaseSpi is pre-configured (Mode 0).
Definition Max22200Handler.cpp:138
bool Transfer(const uint8_t *tx_data, uint8_t *rx_data, size_t length) noexcept
SPI exchange via aligned internal-SRAM staging buffers.
Definition Max22200Handler.cpp:96
BaseSpi & spi_
Definition Max22200Handler.h:116
bool Initialize() noexcept
Park ENABLE/CMD, optional nFAULT input, and 5 ms VM settle.
Definition Max22200Handler.cpp:39
void GpioSet(max22200::CtrlPin pin, max22200::GpioSignal signal) noexcept
Drive ENABLE, CMD, or nFAULT through BaseGpio.
Definition Max22200Handler.cpp:155
Unified handler for MAX22200 octal solenoid/motor driver.
Definition Max22200Handler.h:150
DriverType * GetDriver() noexcept
Get the underlying driver for advanced operations.
Definition Max22200Handler.cpp:556
bool IsChannelEnabled(uint8_t channel) noexcept
Check if a channel is enabled (reads STATUS channels_on_mask).
Definition Max22200Handler.cpp:506
RtosMutex mutex_
Definition Max22200Handler.h:460
auto WithDriver(Fn &&fn) noexcept
Run fn against the raw driver with mutex_ held.
Definition Max22200Handler.h:377
max22200::DriverStatus Initialize() noexcept
Initialize driver (ENABLE HIGH, read/clear STATUS, set ACTIVE).
Definition Max22200Handler.cpp:231
std::unique_ptr< HalSpiMax22200Comm > comm_
Definition Max22200Handler.h:461
max22200::DriverStatus DisableChannel(uint8_t channel) noexcept
Disable a channel.
Definition Max22200Handler.cpp:487
bool WaitForActiveAndDrainFaults() noexcept
Post-init wake poll: poke ACTIVE=1, wait for latch or SPI-live fallback.
Definition Max22200Handler.cpp:328
max22200::DriverStatus SetChannelsMask(uint8_t mask) noexcept
Set channels enabled by bitmask.
Definition Max22200Handler.cpp:515
max22200::DriverStatus GetStatus(max22200::StatusConfig &status) noexcept
Read the STATUS register.
Definition Max22200Handler.cpp:521
max22200::DriverStatus SetupVdrChannel(uint8_t channel, float hit_duty_pct, float hold_duty_pct, float hit_time_ms) noexcept
Quick VDR setup with duty cycle percentages.
Definition Max22200Handler.cpp:465
std::unique_ptr< DriverType > driver_
Definition Max22200Handler.h:462
bool initialized_
Definition Max22200Handler.h:459
max22200::DriverStatus ConfigureChannel(uint8_t channel, const max22200::ChannelConfig &config) noexcept
Configure a channel completely.
Definition Max22200Handler.cpp:419
max22200::DriverStatus EnableChannel(uint8_t channel) noexcept
Enable a channel (sets bit in STATUS channels_on_mask).
Definition Max22200Handler.cpp:480
bool EnsureInitialized() noexcept
Ensure driver is initialized (lazy init entrypoint).
Definition Max22200Handler.cpp:269
bool IsInitialized() const noexcept
Check if Initialize completed successfully.
Definition Max22200Handler.h:221
static constexpr uint8_t kNumChannels
Number of MAX22200 output channels (0..7).
Definition Max22200Handler.h:156
max22200::DriverStatus Deinitialize() noexcept
Deinitialize — disable all channels, ACTIVE=0, ENABLE LOW.
Definition Max22200Handler.cpp:406
max22200::DriverStatus ClearFaults() noexcept
Clear all latched fault flags (read-clear FAULT register).
Definition Max22200Handler.cpp:544
max22200::DriverStatus ReadFaultRegister(max22200::FaultStatus &faults) noexcept
Read the FAULT register (OCP/OLF/HHF/DPM channel bitmasks).
Definition Max22200Handler.cpp:550
max22200::MAX22200< HalSpiMax22200Comm > DriverType
Underlying max22200::MAX22200 driver type.
Definition Max22200Handler.h:153
max22200::DriverStatus DisableAllChannels() noexcept
Disable all channels.
Definition Max22200Handler.cpp:500
bool EnsureInitializedLocked() noexcept
Definition Max22200Handler.cpp:313
max22200::DriverStatus InitializeLocked() noexcept
Initialize while mutex_ is held (no board IFS).
Definition Max22200Handler.cpp:236
auto withDriver(Fn &&fn) noexcept
Execute a lambda with a locked, initialized driver.
Definition Max22200Handler.h:446
max22200::DriverStatus GetChannelFaults(uint8_t channel, max22200::FaultStatus &faults) noexcept
Read the device-wide FAULT register (channel param is range-checked only).
Definition Max22200Handler.cpp:527
bool HasFault() noexcept
Check if any fault flag is set (reads STATUS fault summary).
Definition Max22200Handler.cpp:536
uint8_t GetLastFaultByte() const noexcept
STATUS[7:0] captured on the last command-phase SPI byte.
Definition Max22200Handler.cpp:324
max22200::DriverStatus SetupCdrChannel(uint8_t channel, uint16_t hit_ma, uint16_t hold_ma, float hit_time_ms) noexcept
Quick CDR setup with milliamp values.
Definition Max22200Handler.cpp:451
uint8_t last_fault_byte_
Definition Max22200Handler.h:464
max22200::DriverStatus EnableAllChannels() noexcept
Enable all eight channels (writes full channels_on_mask).
Definition Max22200Handler.cpp:494
void DumpDiagnostics() noexcept
Log STATUS, FAULT register, and SPI transfer statistics.
Definition Max22200Handler.cpp:569