HF-MAX22200 Driver 0.1.0-dev
HF-MAX22200 C++ Driver
Loading...
Searching...
No Matches
max22200::SpiInterface< Derived > Class Template Reference

CRTP-based template interface for SPI communication. More...

#include <max22200_spi_interface.hpp>

Public Member Functions

bool Initialize ()
 Initialize the SPI interface.
 
bool Transfer (const uint8_t *tx_data, uint8_t *rx_data, size_t length)
 Transfer data over SPI interface.
 
void SetChipSelect (bool state)
 Set chip select state.
 
bool Configure (uint32_t speed_hz, uint8_t mode, bool msb_first=true)
 Configure SPI parameters.
 
bool IsReady () const
 Check if SPI interface is ready.
 
void DelayUs (uint32_t us)
 Blocking delay in microseconds (from comm/CRTP implementation).
 
GPIO Pin Control

Unified interface for controlling MAX22200 hardware control pins. The platform bus implementation maps GpioSignal::ACTIVE/INACTIVE to the correct physical level for each pin based on its polarity.

void GpioSet (CtrlPin pin, GpioSignal signal)
 Set a control pin to the specified signal state.
 
bool GpioRead (CtrlPin pin, GpioSignal &signal)
 Read the current state of a control pin.
 
void GpioSetActive (CtrlPin pin)
 Assert a control pin (set to ACTIVE). Convenience wrapper for GpioSet(pin, GpioSignal::ACTIVE).
 
void GpioSetInactive (CtrlPin pin)
 Deassert a control pin (set to INACTIVE). Convenience wrapper for GpioSet(pin, GpioSignal::INACTIVE).
 

Protected Member Functions

 SpiInterface ()=default
 Protected constructor to prevent direct instantiation.
 
 SpiInterface (const SpiInterface &)=delete
 
SpiInterfaceoperator= (const SpiInterface &)=delete
 
 SpiInterface (SpiInterface &&)=default
 
SpiInterfaceoperator= (SpiInterface &&)=default
 
 ~SpiInterface ()=default
 Protected destructor.
 

Detailed Description

template<typename Derived>
class max22200::SpiInterface< Derived >

CRTP-based template interface for SPI communication.

This template class provides a hardware-agnostic interface for SPI communication using the CRTP pattern. Platform-specific implementations should inherit from this template with themselves as the template parameter.

Benefits of CRTP:

  • Compile-time polymorphism (no virtual function overhead)
  • Static dispatch instead of dynamic dispatch
  • Better optimization opportunities for the compiler

Example usage:

class MySPI : public max22200::SpiInterface<MySPI> {
public:
bool Initialize() { ... }
bool Transfer(...) { ... }
// ... implement other methods
};
Definition max22200.hpp:133
CRTP-based template interface for SPI communication.
Definition max22200_spi_interface.hpp:114
Template Parameters
DerivedThe derived class type (CRTP pattern)

Constructor & Destructor Documentation

◆ SpiInterface() [1/3]

template<typename Derived >
max22200::SpiInterface< Derived >::SpiInterface ( )
protecteddefault

Protected constructor to prevent direct instantiation.

◆ SpiInterface() [2/3]

template<typename Derived >
max22200::SpiInterface< Derived >::SpiInterface ( const SpiInterface< Derived > & )
protecteddelete

◆ SpiInterface() [3/3]

template<typename Derived >
max22200::SpiInterface< Derived >::SpiInterface ( SpiInterface< Derived > && )
protecteddefault

◆ ~SpiInterface()

template<typename Derived >
max22200::SpiInterface< Derived >::~SpiInterface ( )
protecteddefault

Protected destructor.

Note
Derived classes can have public destructors

Member Function Documentation

◆ Configure()

template<typename Derived >
bool max22200::SpiInterface< Derived >::Configure ( uint32_t speed_hz,
uint8_t mode,
bool msb_first = true )
inline

Configure SPI parameters.

Sets up SPI communication parameters such as clock speed, data mode, and bit order.

Parameters
speed_hzSPI clock speed in Hz
  • Standalone: Up to 10 MHz
  • Daisy-chain: Up to 5 MHz
  • Minimum period: 100ns (10 MHz max)
modeSPI mode (0-3) defining clock polarity and phase
  • Must be Mode 0 (CPOL=0, CPHA=0) for MAX22200
  • Clock idle LOW, data sampled on rising edge
msb_firsttrue for MSB first, false for LSB first
Returns
true if configuration was successful, false otherwise
Note
MAX22200 requires Mode 0 and MSB-first bit order.
Clock speed should not exceed 10 MHz (standalone) or 5 MHz (daisy-chain).

◆ DelayUs()

template<typename Derived >
void max22200::SpiInterface< Derived >::DelayUs ( uint32_t us)
inline

Blocking delay in microseconds (from comm/CRTP implementation).

Used for device timing requirements, e.g. MAX22200 power-up delay (0.5 ms after ENABLE) before first SPI access. Platform implements the actual wait (e.g. esp_rom_delay_us, or RTOS-aware delay).

Parameters
usDelay duration in microseconds.

◆ GpioRead()

template<typename Derived >
bool max22200::SpiInterface< Derived >::GpioRead ( CtrlPin pin,
GpioSignal & signal )
inline

Read the current state of a control pin.

Primarily used for reading the FAULT pin status to detect fault conditions.

Parameters
[in]pinWhich control pin to read:
  • FAULT: Active-low (ACTIVE = LOW = fault present)
  • ENABLE: Can be read to verify state
  • CMD: Can be read to verify state
[out]signalReceives the current signal state:
Returns
true if the read was successful, false otherwise.
Note
FAULT pin is open-drain and requires external pullup resistor.
FAULT pin is LOW when any unmasked fault is active.
Reading FAULT pin does not clear fault flags (use ReadFaultRegister()).

◆ GpioSet()

template<typename Derived >
void max22200::SpiInterface< Derived >::GpioSet ( CtrlPin pin,
GpioSignal signal )
inline

Set a control pin to the specified signal state.

Controls the MAX22200 hardware control pins (ENABLE, CMD, FAULT). The platform implementation maps GpioSignal::ACTIVE/INACTIVE to the correct physical level based on each pin's polarity.

Parameters
[in]pinWhich control pin to drive:
  • ENABLE: Active-high (ACTIVE = HIGH, enables device)
  • CMD: Active-high (ACTIVE = HIGH, Command Register mode)
  • FAULT: Read-only (use GpioRead instead)
[in]signalACTIVE to assert the pin function, INACTIVE to deassert.
Note
For CMD pin: Must be held HIGH during Command Register writes.
For ENABLE pin: 0.5ms delay required after setting HIGH before SPI access.
FAULT pin is read-only (open-drain output from device).
See also
GpioSetActive(), GpioSetInactive() for convenience wrappers

◆ GpioSetActive()

template<typename Derived >
void max22200::SpiInterface< Derived >::GpioSetActive ( CtrlPin pin)
inline

Assert a control pin (set to ACTIVE). Convenience wrapper for GpioSet(pin, GpioSignal::ACTIVE).

Parameters
[in]pinWhich control pin to assert.

◆ GpioSetInactive()

template<typename Derived >
void max22200::SpiInterface< Derived >::GpioSetInactive ( CtrlPin pin)
inline

Deassert a control pin (set to INACTIVE). Convenience wrapper for GpioSet(pin, GpioSignal::INACTIVE).

Parameters
[in]pinWhich control pin to deassert.

◆ Initialize()

template<typename Derived >
bool max22200::SpiInterface< Derived >::Initialize ( )
inline

Initialize the SPI interface.

This method should perform any necessary initialization of the SPI hardware, including setting up clock rates, modes, and other configuration parameters.

Returns
true if initialization was successful, false otherwise

◆ IsReady()

template<typename Derived >
bool max22200::SpiInterface< Derived >::IsReady ( ) const
inline

Check if SPI interface is ready.

Verifies that the SPI interface is properly initialized and ready for communication.

Returns
true if interface is ready, false otherwise

◆ operator=() [1/2]

template<typename Derived >
SpiInterface & max22200::SpiInterface< Derived >::operator= ( const SpiInterface< Derived > & )
protecteddelete

◆ operator=() [2/2]

template<typename Derived >
SpiInterface & max22200::SpiInterface< Derived >::operator= ( SpiInterface< Derived > && )
protecteddefault

◆ SetChipSelect()

template<typename Derived >
void max22200::SpiInterface< Derived >::SetChipSelect ( bool state)
inline

Set chip select state.

Controls the chip select (CS) line for the SPI device.

Parameters
statetrue to assert CS (active low), false to deassert
Note
The actual polarity of CS depends on hardware implementation. This method should handle the correct CS polarity internally.

◆ Transfer()

template<typename Derived >
bool max22200::SpiInterface< Derived >::Transfer ( const uint8_t * tx_data,
uint8_t * rx_data,
size_t length )
inline

Transfer data over SPI interface.

Performs a full-duplex SPI transfer. The method should handle both transmission and reception of data simultaneously.

Parameters
tx_dataPointer to data buffer to transmit
rx_dataPointer to buffer for received data (can be same as tx_data)
lengthNumber of bytes to transfer
Returns
true if transfer was successful, false otherwise
Note
Both tx_data and rx_data must be valid pointers and length must be greater than 0 for a successful transfer.

The documentation for this class was generated from the following file: