HF-TMC9660 Driver 0.1.0-dev
Hardware Agnostic C++ Driver for the TMC9660
Loading...
Searching...
No Matches
tmc9660::CommInterface< Derived > Class Template Reference

SPI status codes as per TMC9660 Parameter Mode. More...

#include <tmc9660_comm_interface.hpp>

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

Public Member Functions

 CommInterface (bool rst_active_level, bool drv_en_active_level, bool wake_active_level, bool faultn_active_level) noexcept
 Construct communication interface with pin active level configuration.
 
CommMode mode () const noexcept
 Get the underlying communication mode used by this interface.
 
bool transferTMCL (const TMCLFrame &tx, TMCLReply &reply, uint8_t address, TMCLReply *first_reply=nullptr, const TMCLFrame *second_command=nullptr) noexcept
 Perform a full duplex TMCL transfer for parameter mode communication.
 
bool gpioSet (TMC9660CtrlPin pin, GpioSignal signal) noexcept
 Set GPIO pin signal state (output control).
 
bool gpioRead (TMC9660CtrlPin pin, GpioSignal &signal) noexcept
 Read GPIO pin signal state (input state).
 
bool signalToGpioLevel (TMC9660CtrlPin pin, GpioSignal signal) const noexcept
 Convert signal state to physical GPIO level.
 
GpioSignal gpioLevelToSignal (TMC9660CtrlPin pin, bool gpio_level) const noexcept
 Convert physical GPIO level to signal state.
 
bool gpioSetActive (TMC9660CtrlPin pin) noexcept
 Set GPIO pin to active state (convenience method).
 
bool gpioSetInactive (TMC9660CtrlPin pin) noexcept
 Set GPIO pin to inactive state (convenience method).
 
bool set_pin_active_level (TMC9660CtrlPin pin, bool active_level) noexcept
 Configure the active level for a specific pin.
 
void setSpiRetryMaxCount (uint8_t max_retries) noexcept
 Set maximum number of retries for SPI_STATUS_NOT_READY responses.
 
uint8_t getSpiRetryMaxCount () const noexcept
 Get current maximum retry count for SPI_STATUS_NOT_READY.
 
void setSpiRetryInterval (uint32_t interval_us) noexcept
 Set delay interval between retry attempts for SPI_STATUS_NOT_READY.
 
uint32_t getSpiRetryInterval () const noexcept
 Get current retry interval for SPI_STATUS_NOT_READY.
 
void delayMs (uint32_t ms) noexcept
 Delay execution for specified milliseconds.
 
void delayUs (uint32_t us) noexcept
 Delay execution for specified microseconds.
 
void logDebug (int level, const char *tag, const char *format,...) noexcept
 Public debug logging wrapper for external classes.
 

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 (const CommInterface &)=delete
 
CommInterfaceoperator= (const CommInterface &)=delete
 
 CommInterface (CommInterface &&)=default
 
CommInterfaceoperator= (CommInterface &&)=default
 

Protected Attributes

bool pinActiveLevels_ [4]
 Pin active level configuration storage.
 
uint8_t spiRetryMaxCount_ = 3
 Maximum number of retries for SPI_STATUS_NOT_READY responses.
 
uint32_t spiRetryIntervalUs_ = 100
 Delay interval in microseconds between retry attempts.
 

Detailed Description

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

SPI status codes as per TMC9660 Parameter Mode.

Table 4: SPI status codes:

  • 0xFF: OK (operation successful)
  • 0x00: CHECKSUM_ERROR
  • 0x0C: FIRST_CMD (initial response after initialization)
  • 0xF0: NOT_READY (system busy, resend datagram)

CRTP-based communication interface for sending/receiving TMCL datagrams.

Defines the common API for higher-level code to send and receive 64-bit TMCL frames (via SPI) or 72-bit (via UART) without knowledge of the underlying transport. Also provides GPIO control interface for TMC9660 control pins.

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

Benefits of CRTP:

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

Example usage:

class MySPI : public tmc9660::SpiCommInterface<MySPI> {
public:
bool spiTransferTMCL(...) { ... }
bool gpioSet(...) { ... }
// ... implement other required methods
};
SPI status codes as per TMC9660 Parameter Mode.
Definition tmc9660_comm_interface.hpp:514
CommMode mode() const noexcept
Get the underlying communication mode used by this interface.
Definition tmc9660_comm_interface.hpp:536
bool gpioSet(TMC9660CtrlPin pin, GpioSignal signal) noexcept
Set GPIO pin signal state (output control).
Definition tmc9660_comm_interface.hpp:573
CRTP-based SPI implementation of TMC9660CommInterface.
Definition tmc9660_comm_interface.hpp:855
CommMode
Supported physical communication modes for TMC9660.
Definition tmc9660_comm_interface.hpp:113
@ SPI
SPI (Serial Peripheral Interface) mode - 4-wire synchronous communication.
Template Parameters
DerivedThe derived class type (CRTP pattern)

Constructor & Destructor Documentation

◆ CommInterface() [1/3]

template<typename Derived >
tmc9660::CommInterface< Derived >::CommInterface ( bool rst_active_level,
bool drv_en_active_level,
bool wake_active_level,
bool faultn_active_level )
inlinenoexcept

Construct communication interface with pin active level configuration.

Parameters
rst_active_levelPhysical GPIO level for RST pin when ACTIVE (true=HIGH, false=LOW)
drv_en_active_levelPhysical GPIO level for DRV_EN pin when ACTIVE (true=HIGH, false=LOW)
wake_active_levelPhysical GPIO level for WAKE pin when ACTIVE (true=HIGH, false=LOW)
faultn_active_levelPhysical GPIO level for FAULTN pin when ACTIVE (true=HIGH, false=LOW)
Here is the caller graph for this function:

◆ ~CommInterface()

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

Protected destructor.

Note
Derived classes can have public destructors

◆ CommInterface() [2/3]

template<typename Derived >
tmc9660::CommInterface< Derived >::CommInterface ( const CommInterface< Derived > & )
protecteddelete

◆ CommInterface() [3/3]

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

Member Function Documentation

◆ debugLog()

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

Debug logging function for detailed debugging information.

This function allows the TMC9660 driver to output debug information through the communication interface, which can be routed to platform-specific logging systems (e.g., ESP-IDF logging for ESP32).

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 tmc9660::CommInterface< Derived >::delayMs ( uint32_t ms)
inlinenoexcept

Delay execution for specified milliseconds.

Parameters
msMilliseconds to delay
Note
This function should be implemented by the user to provide platform-specific delay
Used by bootloader operations that require timing (e.g., VDRV voltage drop wait)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ delayUs()

template<typename Derived >
void tmc9660::CommInterface< Derived >::delayUs ( uint32_t us)
inlinenoexcept

Delay execution for specified microseconds.

Parameters
usMicroseconds to delay
Note
This function should be implemented by the user to provide platform-specific delay
Used for fine-grained retry timing in SPI communication
Default implementation converts to milliseconds (may lose precision for < 1ms)
Platform implementations should provide accurate microsecond delays when possible
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getSpiRetryInterval()

template<typename Derived >
uint32_t tmc9660::CommInterface< Derived >::getSpiRetryInterval ( ) const
inlinenoexcept

Get current retry interval for SPI_STATUS_NOT_READY.

Returns
Current retry interval in microseconds

◆ getSpiRetryMaxCount()

template<typename Derived >
uint8_t tmc9660::CommInterface< Derived >::getSpiRetryMaxCount ( ) const
inlinenoexcept

Get current maximum retry count for SPI_STATUS_NOT_READY.

Returns
Current maximum retry count

◆ gpioLevelToSignal()

template<typename Derived >
GpioSignal tmc9660::CommInterface< Derived >::gpioLevelToSignal ( TMC9660CtrlPin pin,
bool gpio_level ) const
inlinenoexcept

Convert physical GPIO level to signal state.

Parameters
pinThe TMC9660 control pin
gpio_levelPhysical GPIO level (true=HIGH, false=LOW)
Returns
Signal state (ACTIVE or INACTIVE)
Here is the caller graph for this function:

◆ gpioRead()

template<typename Derived >
bool tmc9660::CommInterface< Derived >::gpioRead ( TMC9660CtrlPin pin,
GpioSignal & signal )
inlinenoexcept

Read GPIO pin signal state (input state).

Used for reading TMC9660 status pins like FAULTN. The implementation must handle the specific hardware GPIO configuration and map physical levels to ACTIVE/INACTIVE based on board design.

Parameters
pinThe TMC9660 control pin to read
signalReference to store the current signal state
Returns
true if the GPIO was read successfully, false otherwise
Here is the call graph for this function:
Here is the caller graph for this function:

◆ gpioSet()

template<typename Derived >
bool tmc9660::CommInterface< Derived >::gpioSet ( TMC9660CtrlPin pin,
GpioSignal signal )
inlinenoexcept

Set GPIO pin signal state (output control).

Used for controlling TMC9660 control pins like RST, DRV_EN, and WAKE. The implementation must handle the specific hardware GPIO configuration and map ACTIVE/INACTIVE to the appropriate physical levels based on board design.

Parameters
pinThe TMC9660 control pin to control
signalThe desired signal state (ACTIVE or INACTIVE)
Returns
true if the GPIO was set successfully, false otherwise
Here is the call graph for this function:
Here is the caller graph for this function:

◆ gpioSetActive()

template<typename Derived >
bool tmc9660::CommInterface< Derived >::gpioSetActive ( TMC9660CtrlPin pin)
inlinenoexcept

Set GPIO pin to active state (convenience method).

Parameters
pinThe TMC9660 control pin to set active
Returns
true if the GPIO was set successfully, false otherwise
Here is the call graph for this function:

◆ gpioSetInactive()

template<typename Derived >
bool tmc9660::CommInterface< Derived >::gpioSetInactive ( TMC9660CtrlPin pin)
inlinenoexcept

Set GPIO pin to inactive state (convenience method).

Parameters
pinThe TMC9660 control pin to set inactive
Returns
true if the GPIO was set successfully, false otherwise
Here is the call graph for this function:

◆ logDebug()

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

Public debug logging wrapper for external classes.

This function allows external classes like TMC9660Bootloader to output debug information through the communication interface. Automatically ensures newlines are present.

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
Note
When TMC9660_DISABLE_DEBUG_LOGGING is defined, this function becomes a no-op and all logging code is optimized out at compile time.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mode()

template<typename Derived >
CommMode tmc9660::CommInterface< Derived >::mode ( ) const
inlinenoexcept

Get the underlying communication mode used by this interface.

Returns whether this interface uses SPI or UART for communication. This information is useful for protocol-specific optimizations or debugging purposes.

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

◆ operator=() [1/2]

◆ operator=() [2/2]

◆ set_pin_active_level()

template<typename Derived >
bool tmc9660::CommInterface< Derived >::set_pin_active_level ( TMC9660CtrlPin pin,
bool active_level )
inlinenoexcept

Configure the active level for a specific pin.

This method allows the user to configure which physical GPIO level (HIGH or LOW) corresponds to the ACTIVE state for each pin. This accommodates different board implementations (direct connection, inverters, etc.).

Parameters
pinThe TMC9660 control pin to configure
active_levelThe physical GPIO level that represents ACTIVE state
Returns
true if the configuration was successful, false otherwise

◆ setSpiRetryInterval()

template<typename Derived >
void tmc9660::CommInterface< Derived >::setSpiRetryInterval ( uint32_t interval_us)
inlinenoexcept

Set delay interval between retry attempts for SPI_STATUS_NOT_READY.

When retrying a command due to SPI_STATUS_NOT_READY, wait this many microseconds before retrying.

Parameters
interval_usDelay in microseconds between retry attempts
Note
Typical values: 10-1000 microseconds for SPI communication

◆ setSpiRetryMaxCount()

template<typename Derived >
void tmc9660::CommInterface< Derived >::setSpiRetryMaxCount ( uint8_t max_retries)
inlinenoexcept

Set maximum number of retries for SPI_STATUS_NOT_READY responses.

When a command receives SPI_STATUS_NOT_READY (0xF0), the system will automatically retry the command up to the specified number of times.

Parameters
max_retriesMaximum number of retry attempts (0 = no retries, only original attempt)
Note
Setting to 0 disables retry logic entirely

◆ signalToGpioLevel()

template<typename Derived >
bool tmc9660::CommInterface< Derived >::signalToGpioLevel ( TMC9660CtrlPin pin,
GpioSignal signal ) const
inlinenoexcept

Convert signal state to physical GPIO level.

Parameters
pinThe TMC9660 control pin
signalThe signal state (ACTIVE or INACTIVE)
Returns
Physical GPIO level (true=HIGH, false=LOW)
Here is the caller graph for this function:

◆ transferTMCL()

template<typename Derived >
bool tmc9660::CommInterface< Derived >::transferTMCL ( const TMCLFrame & tx,
TMCLReply & reply,
uint8_t address,
TMCLReply * first_reply = nullptr,
const TMCLFrame * second_command = nullptr )
inlinenoexcept

Perform a full duplex TMCL transfer for parameter mode communication.

For SPI: Performs two transactions - sends command, receives first reply, then sends second command (or NO_OP if not provided), receives second reply.

For UART: Single transaction - sends command, receives reply.

Parameters
txTMCL command frame to transmit
replyReference to store the final reply
addressTMC9660 module address (UART only, ignored for SPI)
first_replyOptional pointer to capture first reply (SPI only)
second_commandOptional second command to send (SPI only, defaults to NO_OP)
Returns
true if transfer succeeded
Note
This is for TMCL parameter mode communication (8-byte SPI, 9-byte UART)
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ pinActiveLevels_

template<typename Derived >
bool tmc9660::CommInterface< Derived >::pinActiveLevels_[4]
protected

Pin active level configuration storage.

Stores the physical GPIO level (HIGH or LOW) that corresponds to the ACTIVE state for each TMC9660 control pin. This configuration enables board-agnostic pin control by abstracting physical voltage levels.

Array indices: [RST, DRV_EN, WAKE, FAULTN]

See also
set_pin_active_level() for configuration
signalToGpioLevel() for usage

◆ spiRetryIntervalUs_

template<typename Derived >
uint32_t tmc9660::CommInterface< Derived >::spiRetryIntervalUs_ = 100
protected

Delay interval in microseconds between retry attempts.

When retrying a command due to SPI_STATUS_NOT_READY, wait this many microseconds before retrying. Default: 100us (0.1ms) - fine-grained for fast SPI communication

◆ spiRetryMaxCount_

template<typename Derived >
uint8_t tmc9660::CommInterface< Derived >::spiRetryMaxCount_ = 3
protected

Maximum number of retries for SPI_STATUS_NOT_READY responses.

When a command receives SPI_STATUS_NOT_READY (0xF0), the system will automatically retry the command up to this many times. Default: 3 retries


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