HF-Core Platform 0.1.0-dev
Hardware-Agnostic Handler Layer & RTOS Utilities for HardFOC
Loading...
Searching...
No Matches
Tmc5160Handler Class Reference

Unified handler for TMC5160/TMC5130 stepper motor driver. More...

#include <Tmc5160Handler.h>

Collaboration diagram for Tmc5160Handler:
[legend]

Public Types

using SpiDriver = tmc51x0::TMC51x0<HalSpiTmc5160Comm>
 SPI driver type alias.
 
using UartDriver = tmc51x0::TMC51x0<HalUartTmc5160Comm>
 UART driver type alias.
 
using DriverVariant = std::variant<std::monostate, SpiDriver*, UartDriver*>
 Non-owning variant holding either driver type or empty (monostate).
 
using ConstDriverVariant = std::variant<std::monostate, const SpiDriver*, const UartDriver*>
 Const version of DriverVariant.
 

Public Member Functions

 Tmc5160Handler (BaseSpi &spi, BaseGpio &enable, BaseGpio *diag0=nullptr, BaseGpio *diag1=nullptr, uint8_t daisy_chain_position=0, const tmc51x0::PinActiveLevels &active_levels={}) noexcept
 Construct a TMC5160 handler with SPI communication.
 
 Tmc5160Handler (BaseUart &uart, BaseGpio &enable, BaseGpio *diag0=nullptr, BaseGpio *diag1=nullptr, uint8_t uart_node_address=0, const tmc51x0::PinActiveLevels &active_levels={}) noexcept
 Construct a TMC5160 handler with UART communication.
 
 ~Tmc5160Handler () noexcept
 Destructor.
 
 Tmc5160Handler (const Tmc5160Handler &)=delete
 
Tmc5160Handleroperator= (const Tmc5160Handler &)=delete
 
 Tmc5160Handler (Tmc5160Handler &&)=delete
 
Tmc5160Handleroperator= (Tmc5160Handler &&)=delete
 
tmc51x0::ErrorCode Initialize (const tmc51x0::DriverConfig &config, bool verbose=true) noexcept
 Initialize the TMC5160 driver with full configuration.
 
bool EnsureInitialized () noexcept
 Ensure driver is initialized (lazy init entrypoint).
 
bool Deinitialize () noexcept
 Deinitialize — disable motor and release resources.
 
bool IsInitialized () const noexcept
 Check if the handler is initialized.
 
bool IsSpi () const noexcept
 Check if SPI is being used.
 
template<typename Fn >
auto visitDriver (Fn &&fn) noexcept -> decltype(fn(std::declval< SpiDriver & >()))
 Visit the typed driver with a callable.
 
SpiDriverdriverViaSpi () noexcept
 Get typed SPI driver pointer (null if UART mode or not initialized).
 
const SpiDriverdriverViaSpi () const noexcept
 
UartDriverdriverViaUart () noexcept
 Get typed UART driver pointer (null if SPI mode or not initialized).
 
const UartDriverdriverViaUart () const noexcept
 
DriverVariant GetDriver () noexcept
 Get the active driver pointer without requiring visitor usage.
 
ConstDriverVariant GetDriver () const noexcept
 Const overload of GetDriver().
 
void DumpDiagnostics () noexcept
 Dump diagnostic information to logger.
 
const char * GetDescription () const noexcept
 Get a human-readable description of the handler.
 
const tmc51x0::DriverConfig & GetDriverConfig () const noexcept
 Get the driver configuration used during initialization.
 

Static Public Member Functions

static tmc51x0::DriverConfig GetDefaultConfig () noexcept
 Get a sensible default DriverConfig.
 

Private Member Functions

bool EnsureInitializedLocked () noexcept
 
template<typename Fn >
auto visitDriverInternal (Fn &&fn) noexcept -> decltype(fn(std::declval< SpiDriver & >()))
 Helper: execute a visitor on the active driver (no lock, internal use)
 

Private Attributes

bool is_spi_ {true}
 Communication mode flag.
 
bool initialized_ {false}
 Initialization state.
 
RtosMutex mutex_
 Thread safety mutex.
 
std::unique_ptr< HalSpiTmc5160Commspi_comm_
 SPI communication adapter (owned, null if UART)
 
std::unique_ptr< HalUartTmc5160Commuart_comm_
 UART communication adapter (owned, null if SPI)
 
std::unique_ptr< SpiDriverspi_driver_
 SPI driver instance (owned, null if UART)
 
std::unique_ptr< UartDriveruart_driver_
 UART driver instance (owned, null if SPI)
 
uint8_t address_ {0}
 Daisy chain position (SPI) or node address (UART)
 
tmc51x0::DriverConfig config_ {}
 Configuration snapshot.
 
char description_ [64] {}
 Human-readable handler description.
 

Detailed Description

Unified handler for TMC5160/TMC5130 stepper motor driver.

Non-templated facade that owns one typed driver instance (SPI or UART). Provides convenience methods for common operations and direct access to all driver subsystems through visitDriver().

Member Typedef Documentation

◆ ConstDriverVariant

using Tmc5160Handler::ConstDriverVariant = std::variant<std::monostate, const SpiDriver*, const UartDriver*>

Const version of DriverVariant.

◆ DriverVariant

using Tmc5160Handler::DriverVariant = std::variant<std::monostate, SpiDriver*, UartDriver*>

Non-owning variant holding either driver type or empty (monostate).

◆ SpiDriver

using Tmc5160Handler::SpiDriver = tmc51x0::TMC51x0<HalSpiTmc5160Comm>

SPI driver type alias.

◆ UartDriver

UART driver type alias.

Constructor & Destructor Documentation

◆ Tmc5160Handler() [1/4]

Tmc5160Handler::Tmc5160Handler ( BaseSpi & spi,
BaseGpio & enable,
BaseGpio * diag0 = nullptr,
BaseGpio * diag1 = nullptr,
uint8_t daisy_chain_position = 0,
const tmc51x0::PinActiveLevels & active_levels = {} )
noexcept

Construct a TMC5160 handler with SPI communication.

Parameters
spiReference to pre-configured BaseSpi (Mode 3, MSB first).
enableBaseGpio connected to DRV_ENN (active LOW).
diag0Optional DIAG0 pin.
diag1Optional DIAG1 pin.
daisy_chain_positionPosition in SPI daisy chain (0 = single/first).
active_levelsPin polarity configuration.
Here is the call graph for this function:

◆ Tmc5160Handler() [2/4]

Tmc5160Handler::Tmc5160Handler ( BaseUart & uart,
BaseGpio & enable,
BaseGpio * diag0 = nullptr,
BaseGpio * diag1 = nullptr,
uint8_t uart_node_address = 0,
const tmc51x0::PinActiveLevels & active_levels = {} )
noexcept

Construct a TMC5160 handler with UART communication.

Parameters
uartReference to pre-configured BaseUart.
enableBaseGpio connected to DRV_ENN (active LOW).
diag0Optional DIAG0 pin.
diag1Optional DIAG1 pin.
uart_node_addressUART node address (0-254).
active_levelsPin polarity configuration.
Here is the call graph for this function:

◆ ~Tmc5160Handler()

Tmc5160Handler::~Tmc5160Handler ( )
noexcept

Destructor.

Here is the call graph for this function:

◆ Tmc5160Handler() [3/4]

Tmc5160Handler::Tmc5160Handler ( const Tmc5160Handler & )
delete

◆ Tmc5160Handler() [4/4]

Tmc5160Handler::Tmc5160Handler ( Tmc5160Handler && )
delete

Member Function Documentation

◆ Deinitialize()

bool Tmc5160Handler::Deinitialize ( )
noexcept

Deinitialize — disable motor and release resources.

Returns
true if deinitialization succeeded.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ driverViaSpi() [1/2]

const Tmc5160Handler::SpiDriver * Tmc5160Handler::driverViaSpi ( ) const
noexcept
Here is the call graph for this function:

◆ driverViaSpi() [2/2]

Tmc5160Handler::SpiDriver * Tmc5160Handler::driverViaSpi ( )
noexcept

Get typed SPI driver pointer (null if UART mode or not initialized).

Warning
Raw pointer — NOT mutex-protected. Caller is responsible for external synchronization in multi-task environments. Prefer visitDriver() for thread-safe access.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ driverViaUart() [1/2]

const Tmc5160Handler::UartDriver * Tmc5160Handler::driverViaUart ( ) const
noexcept
Here is the call graph for this function:

◆ driverViaUart() [2/2]

Tmc5160Handler::UartDriver * Tmc5160Handler::driverViaUart ( )
noexcept

Get typed UART driver pointer (null if SPI mode or not initialized).

Warning
Raw pointer — NOT mutex-protected. Prefer visitDriver().
Here is the call graph for this function:
Here is the caller graph for this function:

◆ DumpDiagnostics()

void Tmc5160Handler::DumpDiagnostics ( )
noexcept

Dump diagnostic information to logger.

Here is the call graph for this function:

◆ EnsureInitialized()

bool Tmc5160Handler::EnsureInitialized ( )
noexcept

Ensure driver is initialized (lazy init entrypoint).

Returns
true if initialized and ready.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ EnsureInitializedLocked()

bool Tmc5160Handler::EnsureInitializedLocked ( )
privatenoexcept
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetDefaultConfig()

static tmc51x0::DriverConfig Tmc5160Handler::GetDefaultConfig ( )
inlinestaticnoexcept

Get a sensible default DriverConfig.

Returns
Default tmc51x0::DriverConfig structure.

◆ GetDescription()

const char * Tmc5160Handler::GetDescription ( ) const
noexcept

Get a human-readable description of the handler.

Returns
String e.g. "TMC5160 Stepper Driver (SPI @0)"

◆ GetDriver() [1/2]

ConstDriverVariant Tmc5160Handler::GetDriver ( ) const
inlinenoexcept

Const overload of GetDriver().

Here is the call graph for this function:

◆ GetDriver() [2/2]

DriverVariant Tmc5160Handler::GetDriver ( )
inlinenoexcept

Get the active driver pointer without requiring visitor usage.

Returns
SpiDriver* or UartDriver* wrapped in std::variant, or monostate if unavailable.
Here is the call graph for this function:

◆ GetDriverConfig()

const tmc51x0::DriverConfig & Tmc5160Handler::GetDriverConfig ( ) const
inlinenoexcept

Get the driver configuration used during initialization.

◆ Initialize()

tmc51x0::ErrorCode Tmc5160Handler::Initialize ( const tmc51x0::DriverConfig & config,
bool verbose = true )
noexcept

Initialize the TMC5160 driver with full configuration.

Parameters
configDriver configuration (motor, chopper, ramp, etc.).
verbosePrint config summary if true.
Returns
tmc51x0::ErrorCode::OK on success, or a specific error code.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsInitialized()

bool Tmc5160Handler::IsInitialized ( ) const
inlinenoexcept

Check if the handler is initialized.

◆ IsSpi()

bool Tmc5160Handler::IsSpi ( ) const
inlinenoexcept

Check if SPI is being used.

◆ operator=() [1/2]

Tmc5160Handler & Tmc5160Handler::operator= ( const Tmc5160Handler & )
delete

◆ operator=() [2/2]

Tmc5160Handler & Tmc5160Handler::operator= ( Tmc5160Handler && )
delete

◆ visitDriver()

template<typename Fn >
auto Tmc5160Handler::visitDriver ( Fn && fn) -> decltype(fn(std::declval<SpiDriver&>()))
inlinenoexcept

Visit the typed driver with a callable.

This is the primary way to access all 15 subsystems of the TMC5160 driver. The callable receives a reference to either SpiDriver or UartDriver.

Template Parameters
FnCallable type accepting auto& (SpiDriver& or UartDriver&)
Parameters
fnCallable to execute with the driver reference
Returns
The return value of fn, or a default-constructed value if not initialized
handler.visitDriver([](auto& drv) {
drv.motorControl.Enable();
drv.rampControl.SetTargetPosition(51200);
});
auto pos = handler.visitDriver([](auto& drv) -> int32_t {
auto result = drv.rampControl.GetCurrentPosition();
return result ? result.Value() : 0;
});
Here is the call graph for this function:

◆ visitDriverInternal()

template<typename Fn >
auto Tmc5160Handler::visitDriverInternal ( Fn && fn) -> decltype(fn(std::declval<SpiDriver&>()))
inlineprivatenoexcept

Helper: execute a visitor on the active driver (no lock, internal use)

Here is the caller graph for this function:

Member Data Documentation

◆ address_

uint8_t Tmc5160Handler::address_ {0}
private

Daisy chain position (SPI) or node address (UART)

◆ config_

tmc51x0::DriverConfig Tmc5160Handler::config_ {}
private

Configuration snapshot.

◆ description_

char Tmc5160Handler::description_[64] {}
private

Human-readable handler description.

◆ initialized_

bool Tmc5160Handler::initialized_ {false}
private

Initialization state.

◆ is_spi_

bool Tmc5160Handler::is_spi_ {true}
private

Communication mode flag.

◆ mutex_

RtosMutex Tmc5160Handler::mutex_
mutableprivate

Thread safety mutex.

◆ spi_comm_

std::unique_ptr<HalSpiTmc5160Comm> Tmc5160Handler::spi_comm_
private

SPI communication adapter (owned, null if UART)

◆ spi_driver_

std::unique_ptr<SpiDriver> Tmc5160Handler::spi_driver_
private

SPI driver instance (owned, null if UART)

◆ uart_comm_

std::unique_ptr<HalUartTmc5160Comm> Tmc5160Handler::uart_comm_
private

UART communication adapter (owned, null if SPI)

◆ uart_driver_

std::unique_ptr<UartDriver> Tmc5160Handler::uart_driver_
private

UART driver instance (owned, null if SPI)


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