|
HF-TMC51x0 Driver (TMC5130 & TMC5160) 0.1.0-dev
Hardware Agnostic C++ Driver for the TMC51x0 (TMC5130 & TMC5160)
|
ESP32 SPI implementation of TMC51x0 communication interface. More...
#include <esp32_tmc51x0_bus.hpp>
Public Member Functions | |
| Esp32SPI (spi_host_device_t host, const Esp32SpiPinConfig &pin_config, uint32_t clock_speed_hz=4000000, const tmc51x0::PinActiveLevels &active_levels=tmc51x0::PinActiveLevels{}) noexcept | |
| Construct ESP32 SPI communication interface with complete pin configuration. | |
| void | ConfigureActiveLevels (const tmc51x0::PinActiveLevels &active_levels) noexcept |
| Configure pin active levels from PinActiveLevels struct. | |
| const tmc51x0::PinActiveLevels & | GetActiveLevels () const noexcept |
| Get current active level configuration. | |
| Esp32SPI (spi_host_device_t host, gpio_num_t mosi_pin, gpio_num_t miso_pin, gpio_num_t sclk_pin, gpio_num_t cs_pin, const tmc51x0::TMC51x0PinConfig &pin_config, uint32_t clock_speed_hz=4000000, const tmc51x0::PinActiveLevels &active_levels=tmc51x0::PinActiveLevels{}) noexcept | |
| Construct ESP32 SPI communication interface with separate SPI pins and TMC51x0 pin config. | |
| Esp32SPI (spi_host_device_t host, gpio_num_t mosi_pin, gpio_num_t miso_pin, gpio_num_t sclk_pin, gpio_num_t cs_pin, gpio_num_t en_pin, gpio_num_t dir_pin=static_cast< gpio_num_t >(-1), gpio_num_t step_pin=static_cast< gpio_num_t >(-1), uint32_t clock_speed_hz=4000000, const tmc51x0::PinActiveLevels &active_levels=tmc51x0::PinActiveLevels{}) noexcept | |
| Construct ESP32 SPI communication interface (legacy constructor for backward compatibility) | |
| ~Esp32SPI () noexcept | |
| Destructor - cleans up SPI resources. | |
| bool | ApplyPinConfig (const tmc51x0::TMC51x0PinConfig &pin_config) noexcept |
| Apply pin configuration structure (handles compound pins automatically) | |
| bool | SetPinMapping (tmc51x0::TMC51x0CtrlPin pin, gpio_num_t gpio_pin) noexcept |
| Set pin mapping for a TMC51x0 control pin. | |
| gpio_num_t | GetPinMapping (tmc51x0::TMC51x0CtrlPin pin) const noexcept |
| Get pin mapping for a TMC51x0 control pin. | |
| tmc51x0::Result< void > | Initialize () noexcept |
| Initialize the SPI interface. | |
| tmc51x0::CommMode | GetMode () const noexcept |
| Get communication mode (always SPI for this interface) | |
| bool | EnsureInitialized () noexcept |
| Ensure SPI interface is initialized, initializing if necessary. | |
| tmc51x0::Result< void > | Deinitialize () noexcept |
| Deinitialize the SPI interface. | |
| tmc51x0::Result< void > | SpiTransfer (const uint8_t *tx, uint8_t *rx, size_t length) noexcept |
| Perform SPI transfer. | |
| tmc51x0::Result< void > | GpioSet (tmc51x0::TMC51x0CtrlPin pin, tmc51x0::GpioSignal signal) noexcept |
| Set GPIO pin state. | |
| tmc51x0::Result< tmc51x0::GpioSignal > | GpioRead (tmc51x0::TMC51x0CtrlPin pin) noexcept |
| Read GPIO pin state. | |
| void | DebugLog (int level, const char *tag, const char *format, va_list args) noexcept |
| Debug logging. | |
| void | DelayMs (uint32_t ms) noexcept |
| Delay milliseconds. | |
| void | DelayUs (uint32_t us) noexcept |
| Delay microseconds. | |
| tmc51x0::Result< void > | SetClkFreq (uint32_t frequency_hz) noexcept |
| Set external clock frequency on CLK pin. | |
Public Member Functions inherited from tmc51x0::SpiCommInterface< Esp32SPI > | |
| SpiCommInterface () noexcept | |
| Construct SPI communication interface. | |
| SpiCommInterface (const SpiCommInterface &)=delete | |
| void | SetDaisyChainLength (uint8_t total_length) noexcept |
| Set the total number of devices in the daisy chain. | |
| uint8_t | GetDaisyChainLength () const noexcept |
| Get the total number of devices in the daisy chain. | |
| uint8_t | AutoDetectChainLength (uint8_t max_devices=8) noexcept |
| Auto-detect the daisy chain length by sending a unique command that loops back. | |
| CommMode | GetMode () const noexcept |
| Get communication mode (always SPI for this interface) | |
| Result< void > | SpiTransfer (const uint8_t *tx, uint8_t *rx, size_t length) noexcept |
| Low-level SPI transfer for register read/write. | |
| Result< uint32_t > | ReadRegister (uint8_t address, uint8_t daisy_chain_position=0) noexcept |
| Read a 32-bit register via SPI. | |
| Result< void > | WriteRegister (uint8_t address, uint32_t value, uint8_t daisy_chain_position=0) noexcept |
| Write a 32-bit register via SPI. | |
| SpiCommInterface & | operator= (const SpiCommInterface &)=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< GpioSignal > | GpioRead (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 | |
| CommInterface & | operator= (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. | |
Private Member Functions | |
| bool | SignalToGpioLevel (tmc51x0::TMC51x0CtrlPin pin, tmc51x0::GpioSignal signal) const noexcept |
| Convert signal state to physical GPIO level. | |
| tmc51x0::GpioSignal | GpioLevelToSignal (tmc51x0::TMC51x0CtrlPin pin, bool gpio_level) const noexcept |
| Convert physical GPIO level to signal state. | |
| bool | configureGpioPins () noexcept |
Private Attributes | |
| tmc51x0::PinActiveLevels | active_levels_ |
| Pin active level configuration storage. | |
| spi_host_device_t | host_ |
| gpio_num_t | mosi_pin_ |
| gpio_num_t | miso_pin_ |
| gpio_num_t | sclk_pin_ |
| gpio_num_t | cs_pin_ |
| gpio_num_t | en_pin_ |
| gpio_num_t | dir_pin_ |
| gpio_num_t | step_pin_ |
| uint32_t | clock_speed_hz_ |
| spi_device_handle_t | device_handle_ |
| SemaphoreHandle_t | spi_mutex_ {nullptr} |
| bool | initialized_ |
| gpio_num_t | pin_mapping_ [16] {} |
| Pin mapping array: maps TMC51x0CtrlPin enum to ESP32 GPIO numbers. | |
Additional Inherited Members | |
Static Public Attributes inherited from tmc51x0::SpiCommInterface< Esp32SPI > | |
| static constexpr size_t | kSpiScratchBytes |
Protected Member Functions inherited from tmc51x0::SpiCommInterface< Esp32SPI > | |
| SpiCommInterface (SpiCommInterface &&)=default | |
| ~SpiCommInterface ()=default | |
| Protected destructor. | |
| SpiCommInterface & | operator= (SpiCommInterface &&)=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 | |
| CommInterface & | operator= (CommInterface &&)=default |
Protected Attributes inherited from tmc51x0::SpiCommInterface< Esp32SPI > | |
| uint8_t | total_chain_length_ |
| Total number of devices in the daisy chain. | |
| uint8_t | user_specified_chain_length_ |
| bool | chain_length_verified_ |
ESP32 SPI implementation of TMC51x0 communication interface.
This class provides SPI communication for the TMC51x0 using ESP-IDF SPI driver with full GPIO pin support.
Pin Configuration Example (ESP32-C6):
|
inlinenoexcept |
Construct ESP32 SPI communication interface with complete pin configuration.
| host | SPI host device (e.g., SPI2_HOST) |
| pin_config | Complete pin configuration including SPI pins and TMC51x0 control pins |
| clock_speed_hz | SPI clock speed in Hz (max 4 MHz recommended) |
| active_levels | Pin active level configuration (optional, uses datasheet defaults if not provided) |
This is the recommended constructor as it allows all GPIO pins (SPI + TMC51x0 control) to be configured in a single structure, making it easier to manage pin assignments.
Example:
|
inlinenoexcept |
Construct ESP32 SPI communication interface with separate SPI pins and TMC51x0 pin config.
| host | SPI host device (e.g., SPI2_HOST) |
| mosi_pin | MOSI GPIO pin |
| miso_pin | MISO GPIO pin |
| sclk_pin | SCLK GPIO pin |
| cs_pin | CS GPIO pin |
| pin_config | TMC51x0 pin configuration structure (handles compound pins automatically) |
| clock_speed_hz | SPI clock speed in Hz (max 4 MHz recommended) |
| active_levels | Pin active level configuration (optional, uses datasheet defaults if not provided) |
|
inlinenoexcept |
Construct ESP32 SPI communication interface (legacy constructor for backward compatibility)
| host | SPI host device (e.g., SPI2_HOST) |
| mosi_pin | MOSI GPIO pin |
| miso_pin | MISO GPIO pin |
| sclk_pin | SCLK GPIO pin |
| cs_pin | CS GPIO pin |
| en_pin | EN control pin (DRV_ENN, active LOW enables power stage) |
| dir_pin | DIR control pin (REFR_DIR, optional - used in external step/dir mode, use -1 if not connected) |
| step_pin | STEP control pin (REFL_STEP, optional - used in external step/dir mode, use -1 if not connected) |
| clock_speed_hz | SPI clock speed in Hz (max 4 MHz recommended) |
| active_levels | Pin active level configuration (optional, uses datasheet defaults if not provided) |
|
inlinenoexcept |
Destructor - cleans up SPI resources.
|
inlinenoexcept |
Apply pin configuration structure (handles compound pins automatically)
| pin_config | Pin configuration structure |
This method automatically handles compound pins (pins that share the same physical GPIO):
You can override individual mappings using SetPinMapping() if needed.
|
inlinenoexcept |
Configure pin active levels from PinActiveLevels struct.
| active_levels | Pin active level configuration structure |
Allows users to update active levels after construction, useful for runtime configuration or if board setup changes.
Example:
|
inlineprivatenoexcept |
|
inlinenoexcept |
Debug logging.
|
inlinenoexcept |
Deinitialize the SPI interface.
|
inlinenoexcept |
Delay milliseconds.
|
inlinenoexcept |
Delay microseconds.
|
inlinenoexcept |
Ensure SPI interface is initialized, initializing if necessary.
This is a convenience method that checks initialization status and calls Initialize() if needed. Useful for lazy initialization patterns.
|
inlinenoexcept |
Get current active level configuration.
|
inlinenoexcept |
Get communication mode (always SPI for this interface)
|
inlinenoexcept |
Get pin mapping for a TMC51x0 control pin.
| pin | TMC51x0 control pin identifier |
|
inlineprivatenoexcept |
Convert physical GPIO level to signal state.
| pin | The TMC51x0 control pin |
| gpio_level | Physical GPIO level (true=HIGH, false=LOW) |
|
inlinenoexcept |
Read GPIO pin state.
| pin | The TMC51x0 control pin to read |
Supports reading diagnostic pins (DIAG0, DIAG1), reference switch pins (REFL_STEP, REFR_DIR), encoder pins (ENCA, ENCB, ENCN), and CLK pin.
|
inlinenoexcept |
Set GPIO pin state.
| pin | The TMC51x0 control pin to control |
| signal | The desired signal state (ACTIVE or INACTIVE) |
Supports all TMC51x0 control pins: EN, DIR, STEP, REFL_STEP, REFR_DIR, ENCA, ENCB, ENCN, DCEN, DCIN, DCO, CLK.
|
inlinenoexcept |
Initialize the SPI interface.
|
inlinenoexcept |
Set external clock frequency on CLK pin.
| frequency_hz | Desired clock frequency in Hz (0 = use internal clock, set CLK pin to GND) |
Internal Clock Mode (frequency_hz = 0):
External Clock Mode (frequency_hz > 0):
|
inlinenoexcept |
Set pin mapping for a TMC51x0 control pin.
| pin | TMC51x0 control pin identifier |
| gpio_pin | ESP32 GPIO pin number (or -1 to disable/unmap) |
Use this method to configure additional pins like DIAG0, DIAG1, CLK, encoder pins, etc.
Example:
|
inlineprivatenoexcept |
Convert signal state to physical GPIO level.
| pin | The TMC51x0 control pin |
| signal | The signal state (ACTIVE or INACTIVE) |
|
inlinenoexcept |
Perform SPI transfer.
| tx | Transmit buffer |
| rx | Receive buffer |
| length | Number of bytes to transfer |
spi_device_transmit() (or provide external serialization).spi_device_transmit() may block; do not call this from an ISR.
|
private |
Pin active level configuration storage.
Stores the PinActiveLevels struct which defines the physical GPIO level (HIGH or LOW) that corresponds to the ACTIVE state for each TMC51x0 control pin. This struct is initialized from the constructor parameter (or defaults).
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
Pin mapping array: maps TMC51x0CtrlPin enum to ESP32 GPIO numbers.
Array indices correspond to TMC51x0CtrlPin enum values. -1 indicates the pin is not mapped.
|
private |
|
private |
|
private |