HF-MAX22200 Driver 0.1.0-dev
HF-MAX22200 C++ Driver
Loading...
Searching...
No Matches
max22200_spi_interface.hpp
Go to the documentation of this file.
1
47#pragma once
48#include <cstddef>
49#include <cstdint>
50
51namespace max22200 {
52
53// ============================================================================
54// GPIO Enums -- Standardised Control Pin Model
55// ============================================================================
56
71enum class CtrlPin : uint8_t {
72 ENABLE = 0,
73 FAULT,
74 CMD
75};
76
85enum class GpioSignal : uint8_t {
86 INACTIVE = 0,
87 ACTIVE = 1
88};
89
114template <typename Derived> class SpiInterface {
115public:
125 bool Initialize() { return static_cast<Derived *>(this)->Initialize(); }
126
141 bool Transfer(const uint8_t *tx_data, uint8_t *rx_data, size_t length) {
142 return static_cast<Derived *>(this)->Transfer(tx_data, rx_data, length);
143 }
144
155 void SetChipSelect(bool state) {
156 return static_cast<Derived *>(this)->SetChipSelect(state);
157 }
158
179 bool Configure(uint32_t speed_hz, uint8_t mode, bool msb_first = true) {
180 return static_cast<Derived *>(this)->Configure(speed_hz, mode, msb_first);
181 }
182
191 bool IsReady() const { return static_cast<const Derived *>(this)->IsReady(); }
192
202 void DelayUs(uint32_t us) { static_cast<Derived *>(this)->DelayUs(us); }
203
204 // --------------------------------------------------------------------------
211
232 static_cast<Derived *>(this)->GpioSet(pin, signal);
233 }
234
254 return static_cast<Derived *>(this)->GpioRead(pin, signal);
255 }
256
263
270
272
273protected:
277 SpiInterface() = default;
278
279 // Prevent copying
280 SpiInterface(const SpiInterface &) = delete;
282
283 // Allow moving
286
291 ~SpiInterface() = default;
292};
293
294} // namespace max22200
Definition max22200.hpp:133
CRTP-based template interface for SPI communication.
Definition max22200_spi_interface.hpp:114
bool GpioRead(CtrlPin pin, GpioSignal &signal)
Read the current state of a control pin.
Definition max22200_spi_interface.hpp:253
SpiInterface & operator=(SpiInterface &&)=default
void GpioSetInactive(CtrlPin pin)
Deassert a control pin (set to INACTIVE). Convenience wrapper for GpioSet(pin, GpioSignal::INACTIVE).
Definition max22200_spi_interface.hpp:269
SpiInterface(SpiInterface &&)=default
bool Configure(uint32_t speed_hz, uint8_t mode, bool msb_first=true)
Configure SPI parameters.
Definition max22200_spi_interface.hpp:179
bool Initialize()
Initialize the SPI interface.
Definition max22200_spi_interface.hpp:125
SpiInterface()=default
Protected constructor to prevent direct instantiation.
SpiInterface(const SpiInterface &)=delete
SpiInterface & operator=(const SpiInterface &)=delete
bool Transfer(const uint8_t *tx_data, uint8_t *rx_data, size_t length)
Transfer data over SPI interface.
Definition max22200_spi_interface.hpp:141
void SetChipSelect(bool state)
Set chip select state.
Definition max22200_spi_interface.hpp:155
void GpioSet(CtrlPin pin, GpioSignal signal)
Set a control pin to the specified signal state.
Definition max22200_spi_interface.hpp:231
void GpioSetActive(CtrlPin pin)
Assert a control pin (set to ACTIVE). Convenience wrapper for GpioSet(pin, GpioSignal::ACTIVE).
Definition max22200_spi_interface.hpp:262
bool IsReady() const
Check if SPI interface is ready.
Definition max22200_spi_interface.hpp:191
~SpiInterface()=default
Protected destructor.
void DelayUs(uint32_t us)
Blocking delay in microseconds (from comm/CRTP implementation).
Definition max22200_spi_interface.hpp:202
Definition max22200.ipp:15
GpioSignal
Abstract signal level for control pins.
Definition max22200_spi_interface.hpp:85
@ ACTIVE
Pin function is asserted.
@ INACTIVE
Pin function is deasserted.
CtrlPin
Identifies the hardware control pins of the MAX22200.
Definition max22200_spi_interface.hpp:71
@ FAULT
Fault status output (active-low, open-drain)
@ CMD
Command mode select (HIGH = SPI register, LOW = direct drive)
@ ENABLE
Output enable (active-high on the physical pin)