|
HF-TMC51x0 Driver (TMC5130 & TMC5160) 0.1.0-dev
Hardware Agnostic C++ Driver for the TMC51x0 (TMC5130 & TMC5160)
|
Main class representing a TMC51x0 stepper motor driver (TMC5130 & TMC5160) More...
#include <tmc51x0.hpp>
Classes | |
| struct | Communication |
| Communication subsystem. More... | |
| struct | Encoder |
| Encoder subsystem. More... | |
| struct | Events |
| Motion events / status outputs (X_COMPARE, RAMP_STAT clear) More... | |
| struct | HardResetOptions |
| Options for HardReset() More... | |
| struct | Homing |
| Homing subsystem with automatic settings caching. More... | |
| struct | Io |
| Chip IO / mode pins / IOIN helpers. More... | |
| struct | MotorControl |
| Motor control subsystem. More... | |
| struct | MotorCurrentDebugInfo |
| Snapshot of motor-current related calculated/cached values. More... | |
| struct | PowerStage |
| Power stage + protection subsystem (DRV_CONF, SHORT_CONF) More... | |
| struct | Printer |
| Register printer subsystem for debugging. More... | |
| struct | RampControl |
| Ramp control subsystem. More... | |
| struct | StallGuard |
| StallGuard2 subsystem (COOLCONF/DRV_STATUS + SW_MODE interactions) More... | |
| struct | Status |
| Status / monitoring subsystem (read-only) More... | |
| struct | Switches |
| Reference switches / endstops subsystem (SW_MODE / XLATCH) More... | |
| struct | Thresholds |
| Velocity thresholds / mode thresholds (TPWMTHRS, TCOOLTHRS, THIGH) More... | |
| struct | Tuning |
| Tuning subsystem for automatic parameter optimization. More... | |
| struct | UartConfig |
| UART configuration subsystem. More... | |
| struct | WriteOnlyRegisters |
Public Member Functions | |
| TMC51x0 (CommType &comm, uint8_t daisy_chain_position=0, uint8_t uart_node_address=0) noexcept | |
| Construct a TMC51x0 driver instance. | |
| ~TMC51x0 () noexcept | |
| Destructor for TMC51x0, cleans up resources. | |
| CommType & | GetComm () noexcept |
| Get the communication interface used by this TMC51x0 instance. | |
| Result< void > | Initialize (const DriverConfig &config=DriverConfig(), bool verbose=true) noexcept |
| Initialize the TMC51x0 driver with configuration. | |
| Result< void > | HardReset (const HardResetOptions &opts=HardResetOptions()) noexcept |
| Perform a hard reset (power-cycle when available, else software reset) | |
| DriverConfig | GetDriverConfig () const noexcept |
| Get current driver configuration. | |
| DriverConfig | GetDriverConfig (bool &initialized) const noexcept |
| Get current driver configuration with initialization status. | |
| std::string | GetDriverConfigString () const noexcept |
| Get driver configuration as formatted string (heap allocating) | |
| void | LogConfigSummary (const DriverConfig &cfg, const char *tag="TMC5160", LogLevel lvl=LogLevel::Info) noexcept |
| Log a comprehensive, human-readable summary of a DriverConfig. | |
| void | LogDerivedInitSummary (const char *tag="TMC5160", LogLevel lvl=LogLevel::Info) noexcept |
| Log a compact, table-style summary of derived/cached initialization values. | |
| void | LogLiveStatusReport (const char *tag="TMC5160", LogLevel lvl=LogLevel::Info) noexcept |
| Read silicon registers and log a comprehensive live status report. | |
| MotorCurrentDebugInfo | GetMotorCurrentDebugInfo () const noexcept |
| Get motor-current calculated/cached values for application logging. | |
Public Attributes | |
| struct tmc51x0::TMC51x0::RampControl * | this |
| struct tmc51x0::TMC51x0::Switches * | this |
| struct tmc51x0::TMC51x0::Events * | this |
| struct tmc51x0::TMC51x0::MotorControl * | this |
| struct tmc51x0::TMC51x0::Thresholds * | this |
| struct tmc51x0::TMC51x0::PowerStage * | this |
| struct tmc51x0::TMC51x0::Communication * | this |
| struct tmc51x0::TMC51x0::Io * | this |
| struct tmc51x0::TMC51x0::Encoder * | this |
| struct tmc51x0::TMC51x0::Status * | this |
| struct tmc51x0::TMC51x0::StallGuard * | this |
| struct tmc51x0::TMC51x0::Tuning * | this |
| struct tmc51x0::TMC51x0::Homing * | this |
| struct tmc51x0::TMC51x0::Printer * | this |
| struct tmc51x0::TMC51x0::UartConfig | this |
Protected Member Functions | |
| uint8_t | GetCommAddress () const noexcept |
| Get the appropriate address parameter for ReadRegister/WriteRegister. | |
| Result< void > | Reset () noexcept |
| Reset the TMC51x0 driver. | |
| bool | IsInitialized () const noexcept |
| Check if the driver is initialized. | |
Static Protected Member Functions | |
| static constexpr const char * | GetDriverVersion () noexcept |
| Get the compiled driver version string. | |
| static constexpr uint8_t | GetDriverVersionMajor () noexcept |
| Get the compiled driver major version number. | |
| static constexpr uint8_t | GetDriverVersionMinor () noexcept |
| Get the compiled driver minor version number. | |
| static constexpr uint8_t | GetDriverVersionPatch () noexcept |
| Get the compiled driver patch version number. | |
Private Member Functions | |
| Result< bool > | IsInternalRampMode () noexcept |
| Check whether the chip is currently in internal ramp-generator mode (SD_MODE=0). | |
| Result< void > | RequireInternalRampMode () noexcept |
| Require internal ramp-generator mode (SD_MODE=0) for motion-controller operations. | |
| float | convertSpeedToSteps (float value, Unit unit) const noexcept |
| Convert speed value to internal steps/s. | |
| float | convertAccelerationToSteps (float value, Unit unit) const noexcept |
| Convert acceleration value to internal steps/s² | |
| float | convertPositionToSteps (float value, Unit unit) const noexcept |
| Convert position value to steps. | |
| float | convertStepsToUnit (int32_t steps, Unit unit) const noexcept |
| Convert steps to specified unit (for position) | |
| float | convertSpeedToUnit (float steps_per_sec, Unit unit) const noexcept |
| Convert speed (steps/s) to specified unit. | |
| int32_t | speedToInternal (float speed_hz) const noexcept |
| Convert speed from Hz to internal TMC5160 units. | |
| float | speedFromInternal (int32_t speed_internal) const noexcept |
| Convert speed from internal TMC5160 units to Hz. | |
| int32_t | accelToInternal (float accel_hz) const noexcept |
| Convert acceleration from Hz/s to internal TMC5160 units. | |
| float | accelFromInternal (int32_t accel_internal) const noexcept |
| Convert acceleration from internal TMC5160 units to steps/s² | |
| int32_t | thresholdSpeedToTstep (float speed_hz) const noexcept |
| Convert threshold speed to TSTEP format. | |
Private Attributes | |
| CommType & | comm_ |
| Communication interface reference. | |
| uint32_t | f_clk_ {ClockFreq::DEFAULT_F_CLK} |
| TMC51x0 clock frequency in Hz. | |
| uint8_t | daisy_chain_position_ |
| uint8_t | uart_node_address_ |
| uint8_t | send_delay_ |
| UART send delay (0-15) stored locally from NODECONF register. | |
| bool | initialized_ {false} |
| Initialization status flag. | |
| uint8_t | chip_version_ {ChipVersion::TMC5160} |
| MotorSpec | motor_spec_ |
| MechanicalSystem | mechanical_system_ |
| DriverConfig | driver_config_ |
| uint16_t | calculated_global_scaler_ {0} |
| uint8_t | calculated_irun_ {0} |
| uint8_t | calculated_ihold_ {0} |
| uint16_t | current_microsteps_ {256} |
| struct tmc51x0::TMC51x0::WriteOnlyRegisters | write_only_regs_ |
Main class representing a TMC51x0 stepper motor driver (TMC5130 & TMC5160)
The TMC51x0 class provides a comprehensive high-level interface for configuring and controlling TMC5130 and TMC5160 stepper motor driver chips. This class abstracts the complex low-level register operations into intuitive, easy-to-use methods for stepper motor control applications.
The TMC51x0 class supports a wide range of stepper motor control features:
The class uses a CRTP-based communication interface for communication, making it completely agnostic to the physical communication layer. This allows the same code to work with SPI, UART, or other communication methods by simply providing the appropriate communication interface implementation.
The driver is a template class that takes the communication interface type as a template parameter, providing compile-time polymorphism with zero runtime overhead.
The TMC51x0 family supports two fundamentally different motion sources:
This driver supports both, but not every API makes sense in every mode. To prevent confusing behavior, motion-controller operations require SD_MODE=0 and will return ErrorCode::INVALID_STATE if the chip reports SD_MODE=1 (external Step/Dir) via IOIN.
Quick mode matrix
| Subsystem / Feature | SD_MODE=0 (Internal ramp) | SD_MODE=1 (External Step/Dir) |
|---|---|---|
rampControl motion/profile setters (XTARGET/VMAX/AMAX/...) | ✅ | ❌ (INVALID_STATE) |
homing (sensorless/switch homing routines) | ✅ | ❌ (INVALID_STATE) |
tuning (StallGuard tuning routines) | ✅ | ❌ (INVALID_STATE) |
motorControl (chopper, stealthChop, currents, LUT) | ✅ | ✅ (feature-dependent) |
thresholds (TPWMTHRS/TCOOLTHRS/THIGH/VDCMIN) | ✅ | ✅ (feature-dependent) |
powerStage (DRV_CONF/SHORT_CONF) | ✅ | ✅ |
encoder (ABN encoder config/read) | ✅ | ❌ (INVALID_STATE) |
status (GSTAT/DRV_STATUS/RAMP_STAT/IOIN/etc.) | ✅ | ✅ |
io (SPI_MODE/SD_MODE pin helpers, IOIN/OUTPUT helpers) | ✅ | ✅ |
Notes:
| CommType | The communication interface type (must inherit from SpiCommInterface<CommType> or UartCommInterface<CommType>) |
|
inlineexplicitnoexcept |
Construct a TMC51x0 driver instance.
| comm | Reference to a user-implemented communication interface (SPI, UART, etc) |
| daisy_chain_position | Position in daisy chain (0 = first chip/single chip, 1 = second, etc.) Only used for SPI daisy-chaining. Default: 0 (single chip) |
| uart_node_address | UART node address (0-254). Only used for UART multi-node addressing. Default: 0 (single node or first node). For sequential programming via TMC51x0MultiNode, this is set to 0 initially and updated automatically after ProgramSequentially(). For devices already programmed, specify the known address here. |
|
inlinenoexcept |
Destructor for TMC51x0, cleans up resources.
|
privatenoexcept |
Convert acceleration from internal TMC5160 units to steps/s²
| accel_internal | Acceleration in internal TMC5160 units |
|
privatenoexcept |
Convert acceleration from Hz/s to internal TMC5160 units.
| accel_hz | Acceleration in steps per second squared |
|
privatenoexcept |
Convert acceleration value to internal steps/s²
| value | Acceleration value in specified unit |
| unit | Unit of the value |
|
privatenoexcept |
Convert position value to steps.
| value | Position value in specified unit |
| unit | Unit of the value |
|
privatenoexcept |
Convert speed value to internal steps/s.
| value | Speed value in specified unit |
| unit | Unit of the value |
|
privatenoexcept |
Convert speed (steps/s) to specified unit.
| steps_per_sec | Speed in steps/s |
| unit | Target unit |
|
privatenoexcept |
Convert steps to specified unit (for position)
| steps | Position in steps |
| unit | Target unit |
|
inlinenoexcept |
Get the communication interface used by this TMC51x0 instance.
|
inlineprotectednoexcept |
Get the appropriate address parameter for ReadRegister/WriteRegister.
This method is protected so that subsystem classes can access it.
|
inlinenoexcept |
Get current driver configuration.
Retrieves the current driver configuration including all runtime changes. This includes the initial configuration plus any modifications made after initialization.
|
inlinenoexcept |
Get current driver configuration with initialization status.
| initialized | Reference to store initialization status (true if initialized, false otherwise) |
Retrieves the current driver configuration including all runtime changes. Also returns the initialization status through the initialized parameter.
|
noexcept |
Get driver configuration as formatted string (heap allocating)
Returns a human-readable string with all driver configuration, register values, and current status. Useful for debugging and diagnostics.
|
inlinestaticconstexprprotectednoexcept |
Get the compiled driver version string.
|
inlinestaticconstexprprotectednoexcept |
Get the compiled driver major version number.
|
inlinestaticconstexprprotectednoexcept |
Get the compiled driver minor version number.
|
inlinestaticconstexprprotectednoexcept |
Get the compiled driver patch version number.
|
noexcept |
Get motor-current calculated/cached values for application logging.
|
noexcept |
Perform a hard reset (power-cycle when available, else software reset)
| opts | Hard reset options |
Behavior:
UART multi-node note:
|
noexcept |
Initialize the TMC51x0 driver with configuration.
| config | Driver configuration structure |
| verbose | If true, print configuration summary table (default: true) |
This method performs the complete initialization sequence:
The configuration is stored internally as a snapshot of initialization state. This snapshot represents what was configured during Initialize(), not runtime state. Runtime changes (e.g., SetCurrent(), SetMaxSpeed()) do not update this snapshot.
|
inlineprotectednoexcept |
Check if the driver is initialized.
|
privatenoexcept |
Check whether the chip is currently in internal ramp-generator mode (SD_MODE=0).
This reads IOIN.SD_MODE from the chip. If SD_MODE=1 (external Step/Dir), the internal ramp generator is not the active motion source and many motion-controller registers will not behave as users expect.
|
noexcept |
Log a comprehensive, human-readable summary of a DriverConfig.
Uses the driver's communication interface for logging (TMC51X0_LOG_DEBUG).
This is useful both before Initialize() (to verify the config you are about to apply) and after Initialize() (to compare with derived/cached values).
|
noexcept |
Log a compact, table-style summary of derived/cached initialization values.
Includes f_clk, detected chip version, calculated currents (IRUN/IHOLD/GLOBAL_SCALER), cached write-only registers (IHOLD_IRUN, GLOBAL_SCALER, DRV_CONF), and a decoded IHOLDDELAY timing estimate.
|
noexcept |
Read silicon registers and log a comprehensive live status report.
Performs SPI/UART transactions to capture the current hardware state of the chip (GSTAT, DRV_STATUS, IOIN, CHOPCONF, PWMCONF, etc.) and prints it in a beautiful table format.
This is useful for "recapturing" the actual live state from the driver to verify it matches requested settings or to diagnose issues.
|
privatenoexcept |
Require internal ramp-generator mode (SD_MODE=0) for motion-controller operations.
|
protectednoexcept |
Reset the TMC51x0 driver.
Performs a software reset by writing to the GSTAT register.
|
privatenoexcept |
Convert speed from internal TMC5160 units to Hz.
| speed_internal | Speed in internal TMC5160 units |
|
privatenoexcept |
Convert speed from Hz to internal TMC5160 units.
| speed_hz | Speed in steps per second |
|
privatenoexcept |
Convert threshold speed to TSTEP format.
| speed_hz | Speed threshold in steps per second |
|
private |
|
private |
|
private |
|
private |
Detected chip version (0x11 = TMC5130, 0x30 = TMC5160)
|
private |
Communication interface reference.
|
private |
|
private |
Position in daisy chain (0 = first chip/single chip)
|
private |
|
private |
TMC51x0 clock frequency in Hz.
|
private |
Initialization status flag.
|
private |
|
private |
|
private |
UART send delay (0-15) stored locally from NODECONF register.
| struct tmc51x0::TMC51x0::RampControl* tmc51x0::TMC51x0< CommType >::this |
| struct tmc51x0::TMC51x0::Switches* tmc51x0::TMC51x0< CommType >::this |
| struct tmc51x0::TMC51x0::Events* tmc51x0::TMC51x0< CommType >::this |
| struct tmc51x0::TMC51x0::MotorControl* tmc51x0::TMC51x0< CommType >::this |
| struct tmc51x0::TMC51x0::Thresholds* tmc51x0::TMC51x0< CommType >::this |
| struct tmc51x0::TMC51x0::PowerStage* tmc51x0::TMC51x0< CommType >::this |
| struct tmc51x0::TMC51x0::Communication* tmc51x0::TMC51x0< CommType >::this |
| struct tmc51x0::TMC51x0::Io* tmc51x0::TMC51x0< CommType >::this |
| struct tmc51x0::TMC51x0::Encoder* tmc51x0::TMC51x0< CommType >::this |
| struct tmc51x0::TMC51x0::Status* tmc51x0::TMC51x0< CommType >::this |
| struct tmc51x0::TMC51x0::StallGuard* tmc51x0::TMC51x0< CommType >::this |
| struct tmc51x0::TMC51x0::Tuning* tmc51x0::TMC51x0< CommType >::this |
| struct tmc51x0::TMC51x0::Homing* tmc51x0::TMC51x0< CommType >::this |
| struct tmc51x0::TMC51x0::Printer* tmc51x0::TMC51x0< CommType >::this |
| struct tmc51x0::TMC51x0::UartConfig tmc51x0::TMC51x0< CommType >::this |
|
private |
UART node address (0-254) for multi-node addressing
|
private |