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

CRTP-based UART implementation of TMC5160CommInterface. More...

#include <tmc51x0_comm_interface.hpp>

Inheritance diagram for tmc51x0::UartCommInterface< Derived >:
[legend]
Collaboration diagram for tmc51x0::UartCommInterface< Derived >:
[legend]

Public Member Functions

 UartCommInterface () noexcept
 Construct UART communication interface with pin active level configuration.
 
CommMode GetMode () const noexcept
 Get communication mode (always UART for this interface)
 
Result< void > SetNaiPin (bool active) noexcept
 Set NAI (Next Address Input) pin state for daisy chaining.
 
Result< bool > GetNaoPin () noexcept
 Read NAO (Next Address Output) pin state.
 
Result< void > UartSend (const uint8_t *data, size_t length) noexcept
 Send raw bytes via UART.
 
Result< void > UartReceive (uint8_t *data, size_t length) noexcept
 Receive raw bytes via UART.
 
Result< uint32_t > ReadRegister (uint8_t address, uint8_t node_address=0) noexcept
 Read a 32-bit register via UART.
 
Result< void > WriteRegister (uint8_t address, uint32_t value, uint8_t node_address=0) noexcept
 Write a 32-bit register via UART.
 
 UartCommInterface (const UartCommInterface &)=delete
 
UartCommInterfaceoperator= (const UartCommInterface &)=delete
 
- Public Member Functions inherited from tmc51x0::CommInterface< Derived >
 CommInterface () noexcept=default
 Construct communication interface.
 
CommMode GetMode () const noexcept
 Get the underlying communication mode used by this interface.
 
Result< uint32_t > ReadRegister (uint8_t address, uint8_t address_param=0) noexcept
 Read a 32-bit register from the TMC5160.
 
Result< void > WriteRegister (uint8_t address, uint32_t value, uint8_t address_param=0) noexcept
 Write a 32-bit register to the TMC5160.
 
Result< void > GpioSet (TMC51x0CtrlPin pin, GpioSignal signal) noexcept
 Set GPIO pin signal state (output control)
 
Result< GpioSignalGpioRead (TMC51x0CtrlPin pin) noexcept
 Read GPIO pin signal state (input state)
 
Result< void > GpioSetActive (TMC51x0CtrlPin pin) noexcept
 Set GPIO pin to active state (convenience method)
 
Result< void > GpioSetInactive (TMC51x0CtrlPin pin) noexcept
 Set GPIO pin to inactive state (convenience method)
 
void DelayMs (uint32_t ms) noexcept
 Delay execution for specified milliseconds.
 
void DelayUs (uint32_t us) noexcept
 Delay execution for specified microseconds.
 
Result< void > SetPowerEnabled (bool enabled) noexcept
 Enable/disable power to the TMC51x0 (optional)
 
Result< void > PowerCycle (uint32_t power_off_ms=20, uint32_t power_on_settle_ms=20) noexcept
 Power-cycle the TMC51x0 (optional)
 
Result< void > SetClkFreq (uint32_t frequency_hz) noexcept
 Set external clock frequency on CLK pin (optional)
 
 CommInterface (const CommInterface &)=delete
 
CommInterfaceoperator= (const CommInterface &)=delete
 
void LogDebug (int level, const char *tag, const char *format,...) noexcept
 Public debug logging wrapper for external classes.
 
void LogDebug (LogLevel level, const char *tag, const char *format,...) noexcept
 LogDebug overload that accepts the driver-native LogLevel enum.
 

Protected Member Functions

 ~UartCommInterface ()=default
 Protected destructor.
 
 UartCommInterface (UartCommInterface &&)=default
 
UartCommInterfaceoperator= (UartCommInterface &&)=default
 
- Protected Member Functions inherited from tmc51x0::CommInterface< Derived >
void DebugLog (int level, const char *tag, const char *format, va_list args) noexcept
 Debug logging function for detailed debugging information.
 
 ~CommInterface ()=default
 Protected destructor.
 
 CommInterface (CommInterface &&)=default
 
CommInterfaceoperator= (CommInterface &&)=default
 

Detailed Description

template<typename Derived>
class tmc51x0::UartCommInterface< Derived >

CRTP-based UART implementation of TMC5160CommInterface.

Uses UART single wire interface per datasheet section 5.1. Uses UART_TXD and UART_RXD signals; supports external transceivers via UART_TXEN. Each byte is transmitted LSB...MSB, highest byte transmitted first.

UART Mode Requirements

For UART operation, the TMC5160 must be configured with:

  • SD_MODE (pin 21): Must be LOW (0)
  • SPI_MODE (pin 22): Must be LOW (0)
  • When both are LOW, UART operation is enabled

Pin Functions in UART Mode

In UART mode (SD_MODE=0, SPI_MODE=0), certain pins take on special functions:

  • SDI_CFG1 (pin 15) → NAI (Next Address Input): Input for address selection
  • SDO_CFG0 (pin 16) → NAO (Next Address Output): Output that connects to next chip's NAI
  • DIAG0_SWN (pin 26) → SWION (Single Wire I/O Negative): UART single wire interface
  • DIAG1_SWP (pin 27) → SWIOP (Single Wire I/O Positive): UART single wire interface

UART Daisy Chaining

The TMC5160 supports daisy chaining up to 255 nodes in UART mode:

  • First chip: NAI tied to GND → responds to address 0
  • Each chip's NAO connects to the next chip's NAI
  • After programming each chip, its NAO must be LOW to enable the next chip
  • Program chips sequentially starting from address 0
  • See datasheet section 5.4 for detailed addressing procedure

UART Write Access Datagram (9 bytes per datasheet section 5.1.1):

  • Byte 0: Sync nibble (bits 0-3: 1,0,1,0) + Reserved (bits 4-7) + NODEADDR (8 bits)
    • Sync pattern enables baud rate synchronization
    • Reserved bits are don't cares but included in CRC
  • Byte 1: RW bit (bit 8 = 1 for write) + 7-bit register address
  • Bytes 2-5: 32-bit data (high byte to low byte, MSB-first)
  • Bytes 6-7: Reserved (don't cares but included in CRC)
  • Byte 8: CRC8 checksum (CRC8-ATM polynomial 0x07, LSB to MSB)

UART Read Access Request (5 bytes per datasheet section 5.1.2):

  • Byte 0: Sync nibble + Reserved + NODEADDR
  • Byte 1: RW bit (bit 8 = 0 for read) + 7-bit register address
  • Bytes 2-3: Reserved (don't cares but included in CRC)
  • Byte 4: CRC8 checksum

UART Read Access Reply (9 bytes per datasheet section 5.1.2):

  • Byte 0: Sync nibble + Reserved (0) + 0xFF (address code for master)
  • Byte 1: Register address (0)
  • Bytes 2-5: 32-bit data (high byte to low byte, MSB-first)
  • Bytes 6-7: Reserved (0)
  • Byte 8: CRC8 checksum

CRC8: CRC8-ATM polynomial (x^8 + x^2 + x^1 + x^0 = 0x07)

  • Applied LSB to MSB, including sync and addressing byte
  • Sync nibble is assumed to always be correct

Baud Rate (per datasheet section 5.1.1):

  • Minimum: 9000 baud (assuming 20 MHz clock)
  • Maximum: fCLK/16
  • Automatically detected from sync frame timing
  • Bit time calculated from start bit (1 to 0 transition) to end of sync frame

Communication Reset (per datasheet section 5.1.1):

  • Pause time > 63 bit times between start bits resets communication
  • Minimum 12 bit times bus idle time required after reset
  • Glitches (< 16 clock cycles) cause 12 bit time timeout

SENDDELAY (per datasheet section 5.1.2):

  • Programmable delay after read request before reply (multiples of 8 bit times)
  • Default: 8 bit times
  • Multi-node systems: Set SENDDELAY to minimum 2 for all nodes

Example usage:

class MyUART : public tmc5160::UartCommInterface<MyUART> {
public:
CommMode mode() const noexcept { return CommMode::UART; }
bool uartSend(...) { ... }
bool uartReceive(...) { ... }
bool gpioSet(...) { ... }
bool gpioRead(...) { ... }
void debugLog(...) { ... }
void delayMs(...) { ... }
void delayUs(...) { ... }
};
CommMode
Supported physical communication modes for TMC51x0.
Definition tmc51x0_comm_interface.hpp:221
Template Parameters
DerivedThe derived class type (CRTP pattern)

Constructor & Destructor Documentation

◆ UartCommInterface() [1/3]

template<typename Derived >
tmc51x0::UartCommInterface< Derived >::UartCommInterface ( )
inlinenoexcept

Construct UART communication interface with pin active level configuration.

Parameters
en_active_levelPhysical GPIO level for EN pin when ACTIVE (true=HIGH, false=LOW)
dir_active_levelPhysical GPIO level for DIR pin when ACTIVE (true=HIGH, false=LOW)
step_active_levelPhysical GPIO level for STEP pin when ACTIVE (true=HIGH, false=LOW)
Note
Multiple TMC5160 instances can share one UartCommInterface on the same UART bus. The node address is passed per transaction via ReadRegister() and WriteRegister() methods.

◆ ~UartCommInterface()

template<typename Derived >
tmc51x0::UartCommInterface< Derived >::~UartCommInterface ( )
protecteddefault

Protected destructor.

◆ UartCommInterface() [2/3]

template<typename Derived >
tmc51x0::UartCommInterface< Derived >::UartCommInterface ( UartCommInterface< Derived > && )
protecteddefault

◆ UartCommInterface() [3/3]

template<typename Derived >
tmc51x0::UartCommInterface< Derived >::UartCommInterface ( const UartCommInterface< Derived > & )
delete

Member Function Documentation

◆ GetMode()

template<typename Derived >
CommMode tmc51x0::UartCommInterface< Derived >::GetMode ( ) const
inlinenoexcept

Get communication mode (always UART for this interface)

Returns
CommMode::UART

◆ GetNaoPin()

template<typename Derived >
Result< bool > tmc51x0::UartCommInterface< Derived >::GetNaoPin ( )
inlinenoexcept

Read NAO (Next Address Output) pin state.

Parameters
activeReference to store NAO pin state
Returns
Result<bool> containing true if NAO is active, false otherwise

NAO is the output from one TMC5160 that connects to the next TMC5160's NAI input in a daisy chain.

  • NAO is the SDO_CFG0 pin (pin 16) in UART mode
  • After programming a chip to its target address, NAO must be LOW to enable the next chip in the chain
  • NAO HIGH: Next chip is not yet accessible
  • NAO LOW: Next chip is accessible for programming

Default implementation delegates to derived class. Override for hardware-specific NAO pin reading.

Note
In UART mode, SD_MODE=0 and SPI_MODE=0 must be set
Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator=() [1/2]

template<typename Derived >
UartCommInterface & tmc51x0::UartCommInterface< Derived >::operator= ( const UartCommInterface< Derived > & )
delete

◆ operator=() [2/2]

template<typename Derived >
UartCommInterface & tmc51x0::UartCommInterface< Derived >::operator= ( UartCommInterface< Derived > && )
protecteddefault

◆ ReadRegister()

template<typename Derived >
Result< uint32_t > tmc51x0::UartCommInterface< Derived >::ReadRegister ( uint8_t address,
uint8_t node_address = 0 )
inlinenoexcept

Read a 32-bit register via UART.

Parameters
addressRegister address (0x00-0x73)
valueReference to store the read value
node_addressUART node address (0-127) for multi-node addressing
Returns
Result<uint32_t> containing the value or error
Here is the call graph for this function:

◆ SetNaiPin()

template<typename Derived >
Result< void > tmc51x0::UartCommInterface< Derived >::SetNaiPin ( bool active)
inlinenoexcept

Set NAI (Next Address Input) pin state for daisy chaining.

Parameters
activetrue to set NAI active (high), false to set inactive (low)
Returns
Result<void> indicating success or error

For UART daisy chaining, NAI controls the addressing sequence.

  • NAI is the SDI_CFG1 pin (pin 15) in UART mode
  • When NAI is active (high), the node address increments by one
  • First chip in chain: NAI should be tied to GND (low) → responds to address 0
  • Subsequent chips: NAI connected to previous chip's NAO

Default implementation delegates to derived class. Override for hardware-specific NAI pin control.

Note
In UART mode, SD_MODE=0 and SPI_MODE=0 must be set
Here is the call graph for this function:
Here is the caller graph for this function:

◆ UartReceive()

template<typename Derived >
Result< void > tmc51x0::UartCommInterface< Derived >::UartReceive ( uint8_t * data,
size_t length )
inlinenoexcept

Receive raw bytes via UART.

Parameters
dataPointer to buffer to store received bytes
lengthNumber of bytes to receive
Returns
Result<void> indicating success or error
Here is the call graph for this function:
Here is the caller graph for this function:

◆ UartSend()

template<typename Derived >
Result< void > tmc51x0::UartCommInterface< Derived >::UartSend ( const uint8_t * data,
size_t length )
inlinenoexcept

Send raw bytes via UART.

Parameters
dataPointer to data bytes to send
lengthNumber of bytes to send
Returns
Result<void> indicating success or error
Here is the call graph for this function:
Here is the caller graph for this function:

◆ WriteRegister()

template<typename Derived >
Result< void > tmc51x0::UartCommInterface< Derived >::WriteRegister ( uint8_t address,
uint32_t value,
uint8_t node_address = 0 )
inlinenoexcept

Write a 32-bit register via UART.

Parameters
addressRegister address (0x00-0x73)
value32-bit value to write
node_addressUART node address (0-127) for multi-node addressing
Returns
Result<void> indicating success or error
Here is the call graph for this function:

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