HF-TMC51x0 Driver (TMC5130 & TMC5160) 0.1.0-dev
Hardware Agnostic C++ Driver for the TMC51x0 (TMC5130 & TMC5160)
Loading...
Searching...
No Matches
tmc51x0::CommInterface< Derived > Class Template Reference

CRTP-based communication interface for register read/write operations. More...

#include <tmc51x0_comm_interface.hpp>

Inheritance diagram for tmc51x0::CommInterface< Derived >:
[legend]

Public Member Functions

 CommInterface () noexcept=default
 Construct communication interface.
 
CommMode GetMode () const noexcept
 Get the underlying communication mode used by this interface.
 
Result< uint32_t > ReadRegister (uint8_t address, uint8_t address_param=0) noexcept
 Read a 32-bit register from the TMC5160.
 
Result< void > WriteRegister (uint8_t address, uint32_t value, uint8_t address_param=0) noexcept
 Write a 32-bit register to the TMC5160.
 
Result< void > GpioSet (TMC51x0CtrlPin pin, GpioSignal signal) noexcept
 Set GPIO pin signal state (output control)
 
Result< GpioSignalGpioRead (TMC51x0CtrlPin pin) noexcept
 Read GPIO pin signal state (input state)
 
Result< void > GpioSetActive (TMC51x0CtrlPin pin) noexcept
 Set GPIO pin to active state (convenience method)
 
Result< void > GpioSetInactive (TMC51x0CtrlPin pin) noexcept
 Set GPIO pin to inactive state (convenience method)
 
void DelayMs (uint32_t ms) noexcept
 Delay execution for specified milliseconds.
 
void DelayUs (uint32_t us) noexcept
 Delay execution for specified microseconds.
 
Result< void > SetPowerEnabled (bool enabled) noexcept
 Enable/disable power to the TMC51x0 (optional)
 
Result< void > PowerCycle (uint32_t power_off_ms=20, uint32_t power_on_settle_ms=20) noexcept
 Power-cycle the TMC51x0 (optional)
 
Result< void > SetClkFreq (uint32_t frequency_hz) noexcept
 Set external clock frequency on CLK pin (optional)
 
 CommInterface (const CommInterface &)=delete
 
CommInterfaceoperator= (const CommInterface &)=delete
 
void LogDebug (int level, const char *tag, const char *format,...) noexcept
 Public debug logging wrapper for external classes.
 
void LogDebug (LogLevel level, const char *tag, const char *format,...) noexcept
 LogDebug overload that accepts the driver-native LogLevel enum.
 

Protected Member Functions

void DebugLog (int level, const char *tag, const char *format, va_list args) noexcept
 Debug logging function for detailed debugging information.
 
 ~CommInterface ()=default
 Protected destructor.
 
 CommInterface (CommInterface &&)=default
 
CommInterfaceoperator= (CommInterface &&)=default
 

Detailed Description

template<typename Derived>
class tmc51x0::CommInterface< Derived >

CRTP-based communication interface for register read/write operations.

Defines the common API for higher-level code to read and write registers without knowledge of the underlying transport (SPI or UART). Also provides GPIO control interface for TMC5160 control pins.

This template class uses the CRTP (Curiously Recurring Template Pattern) for compile-time polymorphism, providing zero runtime overhead compared to virtual functions.

Example usage:

class MySPI : public tmc5160::SpiCommInterface<MySPI> {
public:
CommMode mode() const noexcept { return CommMode::SPI; }
bool spiTransfer(...) { ... }
bool gpioSet(...) { ... }
// ... implement other required methods
};
CommMode
Supported physical communication modes for TMC51x0.
Definition tmc51x0_comm_interface.hpp:221
Template Parameters
DerivedThe derived class type (CRTP pattern)

Constructor & Destructor Documentation

◆ CommInterface() [1/3]

template<typename Derived >
tmc51x0::CommInterface< Derived >::CommInterface ( )
defaultnoexcept

Construct communication interface.

Note: Pin active level configuration is handled by the derived class. The base class only deals with abstract signals (ACTIVE/INACTIVE).

◆ ~CommInterface()

template<typename Derived >
tmc51x0::CommInterface< Derived >::~CommInterface ( )
protecteddefault

Protected destructor.

◆ CommInterface() [2/3]

template<typename Derived >
tmc51x0::CommInterface< Derived >::CommInterface ( CommInterface< Derived > && )
protecteddefault

◆ CommInterface() [3/3]

template<typename Derived >
tmc51x0::CommInterface< Derived >::CommInterface ( const CommInterface< Derived > & )
delete

Member Function Documentation

◆ DebugLog()

template<typename Derived >
void tmc51x0::CommInterface< Derived >::DebugLog ( int level,
const char * tag,
const char * format,
va_list args )
inlineprotectednoexcept

Debug logging function for detailed debugging information.

Parameters
levelLog level (0=Error, 1=Warning, 2=Info, 3=Debug, 4=Verbose)
tagLog tag for categorization
formatprintf-style format string
argsVariable arguments list
Here is the call graph for this function:
Here is the caller graph for this function:

◆ DelayMs()

template<typename Derived >
void tmc51x0::CommInterface< Derived >::DelayMs ( uint32_t ms)
inlinenoexcept

Delay execution for specified milliseconds.

Parameters
msMilliseconds to delay
Here is the call graph for this function:
Here is the caller graph for this function:

◆ DelayUs()

template<typename Derived >
void tmc51x0::CommInterface< Derived >::DelayUs ( uint32_t us)
inlinenoexcept

Delay execution for specified microseconds.

Parameters
usMicroseconds to delay
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetMode()

template<typename Derived >
CommMode tmc51x0::CommInterface< Derived >::GetMode ( ) const
inlinenoexcept

Get the underlying communication mode used by this interface.

Returns
Communication mode (CommMode::SPI or CommMode::UART)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GpioRead()

template<typename Derived >
Result< GpioSignal > tmc51x0::CommInterface< Derived >::GpioRead ( TMC51x0CtrlPin pin)
inlinenoexcept

Read GPIO pin signal state (input state)

Parameters
pinThe TMC51x0 control pin to read
Returns
Result<GpioSignal> containing the signal state, or error code
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GpioSet()

template<typename Derived >
Result< void > tmc51x0::CommInterface< Derived >::GpioSet ( TMC51x0CtrlPin pin,
GpioSignal signal )
inlinenoexcept

Set GPIO pin signal state (output control)

Parameters
pinThe TMC51x0 control pin to control
signalThe desired signal state (ACTIVE or INACTIVE)
Returns
Result<void> indicating success or error code
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GpioSetActive()

template<typename Derived >
Result< void > tmc51x0::CommInterface< Derived >::GpioSetActive ( TMC51x0CtrlPin pin)
inlinenoexcept

Set GPIO pin to active state (convenience method)

Parameters
pinThe TMC51x0 control pin to set active
Returns
Result<void> indicating success or error code
Here is the call graph for this function:

◆ GpioSetInactive()

template<typename Derived >
Result< void > tmc51x0::CommInterface< Derived >::GpioSetInactive ( TMC51x0CtrlPin pin)
inlinenoexcept

Set GPIO pin to inactive state (convenience method)

Parameters
pinThe TMC51x0 control pin to set inactive
Returns
Result<void> indicating success or error code
Here is the call graph for this function:

◆ LogDebug() [1/2]

template<typename Derived >
void tmc51x0::CommInterface< Derived >::LogDebug ( int level,
const char * tag,
const char * format,
... )
inlinenoexcept

Public debug logging wrapper for external classes.

Parameters
levelLog level (0=Error, 1=Warning, 2=Info, 3=Debug, 4=Verbose)
tagLog tag for categorization
formatprintf-style format string
...Variable arguments for format string
Here is the call graph for this function:

◆ LogDebug() [2/2]

template<typename Derived >
void tmc51x0::CommInterface< Derived >::LogDebug ( LogLevel level,
const char * tag,
const char * format,
... )
inlinenoexcept

LogDebug overload that accepts the driver-native LogLevel enum.

Here is the call graph for this function:

◆ operator=() [1/2]

template<typename Derived >
CommInterface & tmc51x0::CommInterface< Derived >::operator= ( CommInterface< Derived > && )
protecteddefault

◆ operator=() [2/2]

template<typename Derived >
CommInterface & tmc51x0::CommInterface< Derived >::operator= ( const CommInterface< Derived > & )
delete

◆ PowerCycle()

template<typename Derived >
Result< void > tmc51x0::CommInterface< Derived >::PowerCycle ( uint32_t power_off_ms = 20,
uint32_t power_on_settle_ms = 20 )
inlinenoexcept

Power-cycle the TMC51x0 (optional)

Parameters
power_off_msTime to keep power disabled (milliseconds)
power_on_settle_msTime to wait after re-enabling power (milliseconds)
Returns
Result<void> indicating success or ErrorCode::UNSUPPORTED

Default implementation uses SetPowerEnabled(false/true) and DelayMs(). Derived classes may override this to implement board-specific sequencing.

Note
If SetPowerEnabled() is not implemented, this will return ErrorCode::UNSUPPORTED.
Here is the call graph for this function:

◆ ReadRegister()

template<typename Derived >
Result< uint32_t > tmc51x0::CommInterface< Derived >::ReadRegister ( uint8_t address,
uint8_t address_param = 0 )
inlinenoexcept

Read a 32-bit register from the TMC5160.

Parameters
addressRegister address (0x00-0x73)
address_paramTransport-specific addressing parameter:
  • SPI: daisy-chain position (0 = first chip/single chip)
  • UART: UART node address (0-254) for multi-node chains
Returns
Result<uint32_t> containing the register value, or error code

Each CommInterface instance can be shared by multiple TMC5160 drivers on the same bus. The meaning of address_param depends on the active communication mode (SPI vs UART).

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetClkFreq()

template<typename Derived >
Result< void > tmc51x0::CommInterface< Derived >::SetClkFreq ( uint32_t frequency_hz)
inlinenoexcept

Set external clock frequency on CLK pin (optional)

Parameters
frequency_hzDesired clock frequency in Hz (0 = use internal clock, >0 = external clock frequency)
Returns
Result<void> indicating success or error

This method is optional - derived classes can implement it if they support providing an external clock signal on the CLK pin. If not implemented, this method will return false, indicating that the internal oscillator should be used.

Clock Mode Selection:

  • Internal Clock:
    • Pass frequency_hz = 0 to explicitly use internal clock
    • CLK pin should be set to GND (low) to enable internal oscillator
    • Internal oscillator provides ~12MHz clock
    • Return true if CLK pin was successfully set to GND, false if not supported
    • f_clk in DriverConfig should still be set correctly (typically 12000000 Hz)
    • The driver uses f_clk for timing calculations regardless of clock source
  • External Clock:
    • Pass frequency_hz > 0 to use external clock at specified frequency
    • CLK pin should receive clock signal from external source
    • Return true if clock signal was successfully provided on CLK pin
    • f_clk in DriverConfig must match the actual external clock frequency
    • Typical frequencies: 12MHz (default) or 24MHz (for higher performance)

Important:

  • The f_clk value in DriverConfig is used for all timing calculations (IHOLDDELAY, TPOWERDOWN, TZEROWAIT, etc.)
  • f_clk must be set correctly regardless of whether using internal or external clock
  • For internal clock, f_clk is typically 12000000 Hz (12 MHz)
  • For external clock, f_clk must match the actual frequency provided
  • Passing frequency_hz = 0 allows users with external clock capability to switch back to internal clock

Implementation Guidelines:

  • If your system doesn't support clock control, return ErrorCode::UNSUPPORTED (driver will assume internal clock)
  • If your system supports clock control:
    • When frequency_hz = 0: Set CLK pin to GND (low) and return Result<void>()
    • When frequency_hz > 0: Provide clock signal at specified frequency and return Result<void>()
    • Return error only if the operation failed (e.g., invalid frequency, hardware error)
Note
This is called automatically during Initialize() with the f_clk value from DriverConfig.
If not implemented (returns UNSUPPORTED), the driver assumes internal clock (CLK pin tied to GND).
The internal clock has a fail-over circuit that protects against loss of external clock signal.
Per datasheet: "Tie to GND using short wire for internal clock or supply external clock."

◆ SetPowerEnabled()

template<typename Derived >
Result< void > tmc51x0::CommInterface< Derived >::SetPowerEnabled ( bool enabled)
inlinenoexcept

Enable/disable power to the TMC51x0 (optional)

Parameters
enabledtrue to enable power, false to disable power
Returns
Result<void> indicating success or ErrorCode::UNSUPPORTED

This hook allows a platform to control a load switch / regulator that supplies the TMC51x0 (typically VIO, or a dedicated enable pin for the device's logic rail). It is OPTIONAL.

If not implemented by the derived class, the default implementation returns ErrorCode::UNSUPPORTED.

Note
This is intended for a true hard reset (power-on reset). Most users will not provide this capability.
Here is the caller graph for this function:

◆ WriteRegister()

template<typename Derived >
Result< void > tmc51x0::CommInterface< Derived >::WriteRegister ( uint8_t address,
uint32_t value,
uint8_t address_param = 0 )
inlinenoexcept

Write a 32-bit register to the TMC5160.

Parameters
addressRegister address (0x00-0x73)
value32-bit value to write
address_paramTransport-specific addressing parameter:
  • SPI: daisy-chain position (0 = first chip/single chip)
  • UART: UART node address (0-254) for multi-node chains
Returns
Result<void> indicating success or error code

Each CommInterface instance can be shared by multiple TMC5160 drivers on the same bus. The meaning of address_param depends on the active communication mode (SPI vs UART).

Here is the call graph for this function:
Here is the caller graph for this function:

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