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

Unified handler for TMC5160/TMC5130 stepper motor driver with SPI/UART integration. More...

#include <cstdint>
#include <memory>
#include <type_traits>
#include <cstdarg>
#include <variant>
#include "core/hf-core-drivers/external/hf-tmc5160-driver/inc/tmc51x0.hpp"
#include "core/hf-core-drivers/external/hf-tmc5160-driver/inc/tmc51x0_comm_interface.hpp"
#include "core/hf-core-drivers/external/hf-tmc5160-driver/inc/tmc51x0_types.hpp"
#include "core/hf-core-drivers/external/hf-tmc5160-driver/inc/tmc51x0_result.hpp"
#include "base/BaseSpi.h"
#include "base/BaseUart.h"
#include "base/BaseGpio.h"
#include "RtosMutex.h"
Include dependency graph for Tmc5160Handler.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  Tmc5160CtrlPins
 Shared helper holding TMC5160 host-side control pin references. More...
 
class  HalSpiTmc5160Comm
 Concrete SPI communication adapter for TMC5160 using BaseSpi and BaseGpio. More...
 
class  HalUartTmc5160Comm
 Concrete UART communication adapter for TMC5160 using BaseUart and BaseGpio. More...
 
class  Tmc5160Handler
 Unified handler for TMC5160/TMC5130 stepper motor driver. More...
 

Detailed Description

Unified handler for TMC5160/TMC5130 stepper motor driver with SPI/UART integration.

This file provides the complete HAL-level integration for a single TMC5160 stepper motor driver device. It bridges the HardFOC base interfaces (BaseSpi, BaseUart, BaseGpio) with the templated tmc51x0::TMC51x0<CommType> driver from the hf-tmc5160-driver library.

Architecture Overview

The file contains three layers:

  1. CRTP Communication Adapters (HalSpiTmc5160Comm, HalUartTmc5160Comm): Bridge HardFOC BaseSpi/BaseUart/BaseGpio to the TMC5160 driver's CRTP-based communication interfaces (tmc51x0::SpiCommInterface, tmc51x0::UartCommInterface).
  2. Tmc5160Handler (main class): Non-templated facade that owns one typed driver instance (SpiDriver or UartDriver). Uses driverViaSpi()/driverViaUart() typed pointers or the visitDriver() template to route calls to the active driver, keeping the public API free of template parameters. Provides:
    • Full driver initialization with DriverConfig or ConfigBuilder
    • Direct access to all 15 driver subsystems (rampControl, motorControl, etc.)
    • Convenience methods for common operations
    • Thread-safe operations with RtosMutex protection
    • Lazy initialization pattern
  3. Subsystem Access: All TMC5160 subsystems are accessible through typed driver pointers (driverViaSpi() / driverViaUart()) or the visitDriver() template.

Supported Subsystems

Subsystem Access via driver. Description
rampControl driver.rampControl Motion profile control
motorControl driver.motorControl Current/chopper/stealthChop
thresholds driver.thresholds Velocity threshold config
switches driver.switches Reference switch control
encoder driver.encoder ABN encoder interface
stallGuard driver.stallGuard StallGuard2 detection
tuning driver.tuning Auto-tuning routines
homing driver.homing Sensorless/switch/encoder homing
status driver.status Status/diagnostics
powerStage driver.powerStage Short/overcurrent protection
communication driver.communication Register read/write
io driver.io Pin/mode helpers
events driver.events XCompare / ramp events
printer driver.printer Debug printing
uartConfig driver.uartConfig UART node addressing

Usage Example

// 1. Obtain SPI interface and EN GPIO
Tmc5160Handler handler(spi, enable_gpio);
// 2. Build configuration
auto config = tmc51x0::Tmc5160ConfigBuilder()
.WithMotorMa(1500)
.WithRunCurrentMa(1200)
.WithHoldCurrentMa(400)
.WithStealthChop(true)
.Build();
// 3. Initialize
if (handler.Initialize(config)) {
// 4. Use motor control
if (auto* drv = handler.driverViaSpi()) {
drv->rampControl.SetRampMode(tmc51x0::RampMode::POSITIONING);
drv->rampControl.SetMaxSpeed(tmc51x0::Speed::FromRPM(100));
drv->rampControl.SetAcceleration(tmc51x0::Acceleration::FromRPMS2(50));
drv->rampControl.SetTargetPosition(51200); // 1 revolution at 256 usteps
}
}
Unified handler for TMC5160/TMC5130 stepper motor driver.
Definition Tmc5160Handler.h:264
Author
HardFOC Team
Date
2025