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

High-level manager for multiple TMC51x0 drivers in a daisy-chain configuration. More...

#include <tmc51x0_daisy_chain.hpp>

Public Member Functions

 TMC51x0DaisyChain (CommType &comm, uint8_t num_onboard_devices, uint32_t f_clk=ClockFreq::DEFAULT_F_CLK) noexcept
 Construct a daisy-chain manager.
 
uint8_t GetNumOnboardDevices () const noexcept
 Get the number of onboard devices (fixed at construction)
 
uint8_t GetNumActiveDevices () const noexcept
 Get the total number of active devices (onboard + extra)
 
constexpr size_t GetMaxCapacity () const noexcept
 Get the maximum capacity (onboard + extra devices)
 
bool IsDeviceActive (uint8_t index) const noexcept
 Check if a device slot is active (has a device instance)
 
Result< void > AddDevice (uint8_t position) noexcept
 Add an extra device at the specified position.
 
Result< void > RemoveDevice (uint8_t position) noexcept
 Remove an extra device at the specified position.
 
TMC51x0< CommType > & operator[] (uint8_t index) noexcept
 Access individual TMC51x0 driver by index.
 
const TMC51x0< CommType > & operator[] (uint8_t index) const noexcept
 Const access to individual TMC51x0 driver by index.
 
Result< void > InitializeAll (const DriverConfig &config=DriverConfig()) noexcept
 Initialize all active devices with the same configuration.
 

Private Member Functions

void UpdateChainLength () noexcept
 Update the chain length on SpiCommInterface.
 

Private Attributes

CommType & comm_
 Shared SPI communication interface.
 
uint8_t num_onboard_devices_
 Number of onboard devices (fixed)
 
uint8_t num_active_devices_
 Total number of active devices (onboard + extra)
 
uint32_t f_clk_
 TMC51x0 clock frequency.
 
std::array< std::optional< TMC51x0< CommType > >, MaxDevices > drivers_
 TMC51x0 driver instances (optional for dynamic devices)
 

Detailed Description

template<typename CommType, size_t MaxDevices = 8>
class tmc51x0::TMC51x0DaisyChain< CommType, MaxDevices >

High-level manager for multiple TMC51x0 drivers in a daisy-chain configuration.

This class manages multiple TMC51x0 drivers on a single SPI bus using daisy-chaining. It supports a fixed number of onboard devices (known at construction) and allows dynamic addition/removal of extra devices up to a maximum capacity.

Key Features

  • Onboard Devices: Fixed number of devices created at construction time
  • Dynamic Devices: Support for adding/removing extra devices at runtime
  • Proper Chain Length: Automatically configures SpiCommInterface with total chain length
  • Individual Access: Access individual drivers via operator[]

Architecture

  • One SpiCommInterface: Shared by all TMC51x0 instances in the chain
  • Multiple TMC51x0 Instances: One per device, each with its own position (0, 1, 2, ...)
  • Total Chain Length: Automatically updated when devices are added/removed

Important: Sequential Positioning

In a daisy chain, devices are physically connected in sequence (MISO of one device connects to MOSI of the next). Therefore, positions MUST be sequential (0, 1, 2, 3...). The position corresponds to the physical order in the chain, not arbitrary numbering.

  • Onboard devices are always created at positions 0, 1, 2, ..., (num_onboard-1)
  • Extra devices must be added sequentially starting from position num_onboard
  • You cannot skip positions (e.g., cannot add device at position 5 if position 4 is empty)

Users can create their own aliases/names in their code for better readability:

// Create daisy-chain with 3 onboard devices
tmc51x0::TMC51x0DaisyChain<MySPI, 5> chain(spi_comm, 3, 12'000'000);
// Create user-friendly aliases for device indices
auto& x_axis = chain[0]; // Position 0 = X-axis motor
auto& y_axis = chain[1]; // Position 1 = Y-axis motor
auto& z_axis = chain[2]; // Position 2 = Z-axis motor
// Use aliases for clearer code
x_axis.rampControl.SetTargetPosition(1000);
y_axis.rampControl.SetMaxSpeed(500.0f);
z_axis.motorControl.Enable();
High-level manager for multiple TMC51x0 drivers in a daisy-chain configuration.
Definition tmc51x0_daisy_chain.hpp:115

Usage Example

// Create SPI communication interface (shared by all devices)
class MySPI : public tmc51x0::SpiCommInterface<MySPI> { ... };
MySPI spi_comm;
spi_comm.Initialize();
// Create daisy-chain manager with 3 onboard devices, capacity for 5 total
// Onboard devices are created at positions 0, 1, 2 (sequential)
tmc51x0::TMC51x0DaisyChain<MySPI, 5> chain(spi_comm, 3, 12'000'000);
// Initialize onboard devices
cfg.motor.irun = 20;
cfg.motor.ihold = 10;
chain.InitializeAll(cfg);
// Add an extra device at runtime (must be position 3, next sequential
position) if (chain.AddDevice(3)) { // Adds device at position 3
chain[3].Initialize(cfg);
}
// Create user-friendly aliases
auto& motor_a = chain[0];
auto& motor_b = chain[1];
auto& motor_c = chain[2];
auto& motor_d = chain[3];
// Access devices using aliases
motor_a.rampControl.SetTargetPosition(1000);
motor_b.rampControl.SetMaxSpeed(500.0f);
// Remove extra device when done
chain.RemoveDevice(3);
CRTP-based SPI implementation of TMC5160CommInterface.
Definition tmc51x0_comm_interface.hpp:1660
Driver initialization configuration structure.
Definition tmc51x0_types.hpp:2870
Template Parameters
CommTypeThe communication interface type (must be SpiCommInterface<CommType>)
MaxDevicesMaximum total capacity (onboard + extra devices, default: 8)

Constructor & Destructor Documentation

◆ TMC51x0DaisyChain()

template<typename CommType , size_t MaxDevices = 8>
tmc51x0::TMC51x0DaisyChain< CommType, MaxDevices >::TMC51x0DaisyChain ( CommType & comm,
uint8_t num_onboard_devices,
uint32_t f_clk = ClockFreq::DEFAULT_F_CLK )
inlineexplicitnoexcept

Construct a daisy-chain manager.

Parameters
commReference to SPI communication interface (shared by all devices)
num_onboard_devicesNumber of onboard devices (fixed, created at construction)
f_clkTMC51x0 clock frequency in Hz (default: 12 MHz)
Note
Onboard devices are created immediately and cannot be removed. Extra devices can be added/removed at runtime up to MaxDevices total.
Here is the call graph for this function:

Member Function Documentation

◆ AddDevice()

template<typename CommType , size_t MaxDevices = 8>
Result< void > tmc51x0::TMC51x0DaisyChain< CommType, MaxDevices >::AddDevice ( uint8_t position)
inlinenoexcept

Add an extra device at the specified position.

Parameters
positionPosition in daisy chain (must be >= num_onboard_devices)
Returns
Result<void> indicating success or error
Note
Position must be >= num_onboard_devices (cannot add before onboard devices)
Position must be < MaxDevices
Device slot must be empty (not already have a device)
Positions must be sequential - cannot skip positions in daisy chain (e.g., cannot add position 5 if position 4 is empty)
Chain length is automatically updated
Warning
In a daisy chain, positions correspond to physical order (MISO→MOSI). Positions MUST be sequential. Users can create aliases in their code for better readability, but the library uses numeric indices.
Here is the call graph for this function:

◆ GetMaxCapacity()

template<typename CommType , size_t MaxDevices = 8>
constexpr size_t tmc51x0::TMC51x0DaisyChain< CommType, MaxDevices >::GetMaxCapacity ( ) const
inlineconstexprnoexcept

Get the maximum capacity (onboard + extra devices)

Returns
Maximum number of devices (MaxDevices template parameter)

◆ GetNumActiveDevices()

template<typename CommType , size_t MaxDevices = 8>
uint8_t tmc51x0::TMC51x0DaisyChain< CommType, MaxDevices >::GetNumActiveDevices ( ) const
inlinenoexcept

Get the total number of active devices (onboard + extra)

Returns
Total number of active devices

◆ GetNumOnboardDevices()

template<typename CommType , size_t MaxDevices = 8>
uint8_t tmc51x0::TMC51x0DaisyChain< CommType, MaxDevices >::GetNumOnboardDevices ( ) const
inlinenoexcept

Get the number of onboard devices (fixed at construction)

Returns
Number of onboard devices

◆ InitializeAll()

template<typename CommType , size_t MaxDevices = 8>
Result< void > tmc51x0::TMC51x0DaisyChain< CommType, MaxDevices >::InitializeAll ( const DriverConfig & config = DriverConfig())
inlinenoexcept

Initialize all active devices with the same configuration.

Parameters
configDriver configuration (applied to all active devices)
Returns
Result<void> indicating success or first error encountered

◆ IsDeviceActive()

template<typename CommType , size_t MaxDevices = 8>
bool tmc51x0::TMC51x0DaisyChain< CommType, MaxDevices >::IsDeviceActive ( uint8_t index) const
inlinenoexcept

Check if a device slot is active (has a device instance)

Parameters
indexDevice index (0 to MaxDevices-1)
Returns
true if device exists at this index, false otherwise

◆ operator[]() [1/2]

template<typename CommType , size_t MaxDevices = 8>
const TMC51x0< CommType > & tmc51x0::TMC51x0DaisyChain< CommType, MaxDevices >::operator[] ( uint8_t index) const
inlinenoexcept

Const access to individual TMC51x0 driver by index.

Parameters
indexDevice index (0 = first device, 1 = second, etc.)
Returns
Const reference to TMC51x0 driver instance

◆ operator[]() [2/2]

template<typename CommType , size_t MaxDevices = 8>
TMC51x0< CommType > & tmc51x0::TMC51x0DaisyChain< CommType, MaxDevices >::operator[] ( uint8_t index)
inlinenoexcept

Access individual TMC51x0 driver by index.

Parameters
indexDevice index (0 = first device, 1 = second, etc.)
Returns
Reference to TMC51x0 driver instance
Note
Index must be in range [0, num_active_devices-1] and device must be active. No bounds checking is performed for performance reasons. Use IsDeviceActive() to verify device exists before access.

◆ RemoveDevice()

template<typename CommType , size_t MaxDevices = 8>
Result< void > tmc51x0::TMC51x0DaisyChain< CommType, MaxDevices >::RemoveDevice ( uint8_t position)
inlinenoexcept

Remove an extra device at the specified position.

Parameters
positionPosition in daisy chain
Returns
Result<void> indicating success or error
Note
Cannot remove onboard devices (position < num_onboard_devices)
Device slot must be active (have a device)
Can only remove from the end of the chain (highest position) to maintain sequential positioning
Chain length is automatically updated
Warning
In a daisy chain, positions must remain sequential. You can only remove devices from the end of the chain (highest position). Removing a device in the middle would create a gap, which is not physically possible in a daisy chain.
Here is the call graph for this function:

◆ UpdateChainLength()

template<typename CommType , size_t MaxDevices = 8>
void tmc51x0::TMC51x0DaisyChain< CommType, MaxDevices >::UpdateChainLength ( )
inlineprivatenoexcept

Update the chain length on SpiCommInterface.

This is called whenever devices are added or removed to ensure the SpiCommInterface knows the correct total chain length for proper response extraction using the datasheet formula 40·(n-k+1).

Here is the caller graph for this function:

Member Data Documentation

◆ comm_

template<typename CommType , size_t MaxDevices = 8>
CommType& tmc51x0::TMC51x0DaisyChain< CommType, MaxDevices >::comm_
private

Shared SPI communication interface.

◆ drivers_

template<typename CommType , size_t MaxDevices = 8>
std::array<std::optional<TMC51x0<CommType> >, MaxDevices> tmc51x0::TMC51x0DaisyChain< CommType, MaxDevices >::drivers_
private

TMC51x0 driver instances (optional for dynamic devices)

◆ f_clk_

template<typename CommType , size_t MaxDevices = 8>
uint32_t tmc51x0::TMC51x0DaisyChain< CommType, MaxDevices >::f_clk_
private

TMC51x0 clock frequency.

◆ num_active_devices_

template<typename CommType , size_t MaxDevices = 8>
uint8_t tmc51x0::TMC51x0DaisyChain< CommType, MaxDevices >::num_active_devices_
private

Total number of active devices (onboard + extra)

◆ num_onboard_devices_

template<typename CommType , size_t MaxDevices = 8>
uint8_t tmc51x0::TMC51x0DaisyChain< CommType, MaxDevices >::num_onboard_devices_
private

Number of onboard devices (fixed)


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