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

High-level manager for multiple TMC51x0 drivers in a UART multi-node configuration. More...

#include <tmc51x0_multi_node.hpp>

Public Member Functions

 TMC51x0MultiNode (CommType &comm, uint8_t num_onboard_devices, uint32_t f_clk=ClockFreq::DEFAULT_F_CLK) noexcept
 Construct a multi-node 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 > ProgramSequentially (uint8_t send_delay=2) noexcept
 Program all active devices sequentially using NAI/NAO addressing.
 
Result< void > ProgramDevice (uint8_t index, uint8_t send_delay=2) noexcept
 Program a single device at the specified logical index.
 
Result< void > AddDevice (uint8_t index) noexcept
 Add an extra device at the specified logical index.
 
Result< void > RemoveDevice (uint8_t index) noexcept
 Remove an extra device at the specified logical index.
 
TMC51x0< CommType > & operator[] (uint8_t index) noexcept
 Access individual TMC5160 driver by logical index.
 
const TMC51x0< CommType > & operator[] (uint8_t index) const noexcept
 Const access to individual TMC5160 driver by logical index.
 
Result< void > InitializeAll (const DriverConfig &config=DriverConfig()) noexcept
 Initialize all active devices with the same configuration.
 

Private Attributes

CommType & comm_
 Shared UART 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_
 TMC5160 clock frequency.
 
std::array< std::optional< TMC51x0< CommType > >, MaxDevices > drivers_
 TMC5160 driver instances (optional for dynamic devices)
 

Detailed Description

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

High-level manager for multiple TMC51x0 drivers in a UART multi-node configuration.

This class manages multiple TMC51x0 drivers on a single UART bus using sequential addressing with NAI/NAO pins. 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
  • Sequential Programming: Handles NAI/NAO pin control for sequential addressing
  • Individual Access: Access individual drivers via operator[] using logical indices

Architecture

  • One UartCommInterface: Shared by all TMC51x0 instances on the UART bus
  • Multiple TMC51x0 Instances: One per device, each with its own programmed node address
  • Sequential Addressing: Uses NAI/NAO pins for sequential programming per datasheet

Important: Sequential Addressing (Per Datasheet)

In UART multi-node mode, devices are programmed sequentially using NAI/NAO pins:

Initial State at Power-Up:

  • First chip: NAI tied to GND (hardware) → responds to address 0
  • All other chips: NAI=HIGH (from previous chip's NAO) → respond to address 1

Programming Sequence (Per Datasheet Figure 5.1):

  • Program first chip (accessible at address 0) to address 254, NAO goes LOW
  • Next chip becomes accessible at address 0 (because previous chip's NAO is LOW)
  • Program second chip (accessible at address 0) to address 253, NAO goes LOW
  • Continue: program chip i to address (254 - i)

Logical vs Physical Addressing:

  • Devices are accessed via operator[] using logical indices (0, 1, 2, ...)
  • Physical addresses programmed into chips are (254, 253, 252, ...)
  • This follows the datasheet procedure for addressing up to 255 nodes

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

// Create multi-node manager with 3 onboard devices
tmc5160::TMC51x0MultiNode<MyUART, 5> nodes(uartComm, 3, 12'000'000);
// Program all devices sequentially (required at startup)
nodes.ProgramSequentially();
// Create user-friendly aliases for device addresses
auto& x_axis = nodes[0]; // Address 0 = X-axis motor
auto& y_axis = nodes[1]; // Address 1 = Y-axis motor
auto& z_axis = nodes[2]; // Address 2 = Z-axis motor
// Use aliases for clearer code
x_axis.rampControl.SetTargetPosition(1000);
y_axis.rampControl.SetMaxSpeed(500.0f);
z_axis.motorControl.Enable();

Usage Example

// Create UART communication interface (shared by all devices)
class MyUART : public tmc5160::UartCommInterface<MyUART> { ... };
MyUART uartComm;
uartComm.Initialize();
// Create multi-node manager with 3 onboard devices, capacity for 5 total
// Onboard devices are created with initial address 0 (will be programmed)
tmc5160::TMC51x0MultiNode<MyUART, 5> nodes(uartComm, 3, 12'000'000);
// Program all devices sequentially (required at startup)
// This programs devices to addresses 254, 253, 252 per datasheet procedure
if (!nodes.ProgramSequentially()) {
// Error handling
}
// Initialize onboard devices
cfg.motor.irun = 20;
cfg.motor.ihold = 10;
nodes.InitializeAll(cfg);
// Add an extra device at runtime (must be index 3, next sequential index)
if (nodes.AddDevice(3)) { // Adds device at logical index 3
// Program the new device (will be programmed to address 251 = 254 - 3)
// Note: ProgramDevice() requires the device to be accessible at address 0
// You may need to reprogram all devices or use ProgramSequentially() again
nodes[3].Initialize(cfg);
}
// Create user-friendly aliases
auto& motor_a = nodes[0];
auto& motor_b = nodes[1];
auto& motor_c = nodes[2];
auto& motor_d = nodes[3];
// Access devices using aliases
motor_a.rampControl.SetTargetPosition(1000);
motor_b.rampControl.SetMaxSpeed(500.0f);
// Remove extra device when done
nodes.RemoveDevice(3);
Driver initialization configuration structure.
Definition tmc51x0_types.hpp:2870
Template Parameters
CommTypeThe communication interface type (must be UartCommInterface<CommType>)
MaxDevicesMaximum total capacity (onboard + extra devices, default: 8)

Constructor & Destructor Documentation

◆ TMC51x0MultiNode()

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

Construct a multi-node manager.

Parameters
commReference to UART communication interface (shared by all devices)
num_onboard_devicesNumber of onboard devices (fixed, created at construction)
f_clkTMC5160 clock frequency in Hz (default: 12 MHz)
Note
Onboard devices are created immediately with initial address 0. Extra devices can be added/removed at runtime up to MaxDevices total.
After construction, call ProgramSequentially() to program all devices to addresses (254, 253, 252, ...) per datasheet procedure.

Member Function Documentation

◆ AddDevice()

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

Add an extra device at the specified logical index.

Parameters
indexLogical device index (must be >= num_onboard_devices)
Returns
Result<void> indicating success or error
Note
Index must be >= num_onboard_devices (cannot add before onboard devices)
Index must be < MaxDevices
Device slot must be empty (not already have a device)
Indices must be sequential - cannot skip indices (e.g., cannot add index 5 if index 4 is empty)
Warning
In UART multi-node mode, indices correspond to physical order (NAO→NAI). Indices MUST be sequential. The actual programmed addresses will be (254, 253, 252, ...) per datasheet procedure.

◆ GetMaxCapacity()

template<typename CommType , size_t MaxDevices = 8>
constexpr size_t tmc51x0::TMC51x0MultiNode< 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::TMC51x0MultiNode< 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::TMC51x0MultiNode< 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::TMC51x0MultiNode< 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 error

◆ IsDeviceActive()

template<typename CommType , size_t MaxDevices = 8>
bool tmc51x0::TMC51x0MultiNode< 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::TMC51x0MultiNode< CommType, MaxDevices >::operator[] ( uint8_t index) const
inlinenoexcept

Const access to individual TMC5160 driver by logical index.

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

◆ operator[]() [2/2]

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

Access individual TMC5160 driver by logical index.

Parameters
indexLogical device index (0 = first device, 1 = second, etc.)
Returns
Reference to TMC5160 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.
The device's actual programmed address is (254 - index) per datasheet.

◆ ProgramDevice()

template<typename CommType , size_t MaxDevices = 8>
Result< void > tmc51x0::TMC51x0MultiNode< CommType, MaxDevices >::ProgramDevice ( uint8_t index,
uint8_t send_delay = 2 )
inlinenoexcept

Program a single device at the specified logical index.

Parameters
indexLogical device index (0, 1, 2, ...)
send_delaySENDDELAY value for NODECONF (default: 2)
Returns
Result<void> indicating success or error
Note
This method programs the device at logical index to address (254 - index).
The device must be accessible at address 0 (previous chip's NAO must be LOW).
For programming all devices, use ProgramSequentially() instead.

◆ ProgramSequentially()

template<typename CommType , size_t MaxDevices = 8>
Result< void > tmc51x0::TMC51x0MultiNode< CommType, MaxDevices >::ProgramSequentially ( uint8_t send_delay = 2)
inlinenoexcept

Program all active devices sequentially using NAI/NAO addressing.

Parameters
send_delaySENDDELAY value for NODECONF (default: 2, minimum for multi-node)
Returns
Result<void> indicating success or error

This method programs all active devices sequentially using the datasheet procedure:

Initial state at power-up:

  • First chip: NAI=GND (hardware) → responds to address 0
  • All other chips: NAI=HIGH (from previous chip's NAO) → respond to address 1

Programming sequence (per datasheet Figure 5.1):

  1. Program first chip (accessible at address 0) to address (254 - 0) = 254 After programming, chip's NAO goes LOW → next chip becomes accessible at address 0
  2. Program second chip (accessible at address 0) to address (254 - 1) = 253 After programming, chip's NAO goes LOW → next chip becomes accessible at address 0
  3. Continue for all devices: program chip i to address (254 - i)

The devices are stored with their logical indices (0, 1, 2, ...) but programmed with addresses (254, 253, 252, ...) as per datasheet specification.

Note
This must be called after construction and before using the devices.
For multi-node systems, SENDDELAY should be set to minimum 2 for all nodes.
The programmed addresses (254, 253, 252...) are stored internally but the logical device indices (0, 1, 2...) are used for access via operator[].

◆ RemoveDevice()

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

Remove an extra device at the specified logical index.

Parameters
indexLogical device index
Returns
Result<void> indicating success or error
Note
Cannot remove onboard devices (index < num_onboard_devices)
Device slot must be active (have a device)
Can only remove from the end of the chain (highest index) to maintain sequential indexing
Warning
In UART multi-node mode, indices must remain sequential. You can only remove devices from the end of the chain (highest index). Removing a device in the middle would create a gap, which is not physically possible in a sequential addressing scheme.

Member Data Documentation

◆ comm_

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

Shared UART communication interface.

◆ drivers_

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

TMC5160 driver instances (optional for dynamic devices)

◆ f_clk_

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

TMC5160 clock frequency.

◆ num_active_devices_

template<typename CommType , size_t MaxDevices = 8>
uint8_t tmc51x0::TMC51x0MultiNode< 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::TMC51x0MultiNode< CommType, MaxDevices >::num_onboard_devices_
private

Number of onboard devices (fixed)


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