86#ifndef COMPONENT_HANDLER_TMC9660_HANDLER_H_
87#define COMPONENT_HANDLER_TMC9660_HANDLER_H_
95#include "core/hf-core-drivers/external/hf-tmc9660-driver/inc/tmc9660.hpp"
96#include "core/hf-core-drivers/external/hf-tmc9660-driver/inc/tmc9660_comm_interface.hpp"
97#include "base/BaseGpio.h"
98#include "base/BaseAdc.h"
99#include "base/BaseTemperature.h"
100#include "base/BaseSpi.h"
101#include "base/BaseUart.h"
102#include "RtosMutex.h"
130 BaseGpio&
get(tmc9660::TMC9660CtrlPin pin)
noexcept {
132 case tmc9660::TMC9660CtrlPin::RST:
return rst;
133 case tmc9660::TMC9660CtrlPin::DRV_EN:
return drv_en;
134 case tmc9660::TMC9660CtrlPin::FAULTN:
return faultn;
135 case tmc9660::TMC9660CtrlPin::WAKE:
return wake;
141 const BaseGpio&
get(tmc9660::TMC9660CtrlPin pin)
const noexcept {
143 case tmc9660::TMC9660CtrlPin::RST:
return rst;
144 case tmc9660::TMC9660CtrlPin::DRV_EN:
return drv_en;
145 case tmc9660::TMC9660CtrlPin::FAULTN:
return faultn;
146 case tmc9660::TMC9660CtrlPin::WAKE:
return wake;
206 BaseGpio& faultn, BaseGpio& wake,
207 bool rst_active_high =
true,
bool drv_en_active_high =
true,
208 bool faultn_active_low =
false,
bool wake_active_low =
false)
noexcept;
220 bool spiTransferTMCL(std::array<uint8_t, 8>& tx, std::array<uint8_t, 8>& rx)
noexcept;
240 bool gpioSet(tmc9660::TMC9660CtrlPin pin, tmc9660::GpioSignal signal)
noexcept;
252 bool gpioRead(tmc9660::TMC9660CtrlPin pin, tmc9660::GpioSignal& signal)
noexcept;
261 void debugLog(
int level,
const char* tag,
const char* format, va_list args)
noexcept;
267 void delayMs(uint32_t ms)
noexcept;
277 void delayUs(uint32_t us)
noexcept;
321 BaseGpio& faultn, BaseGpio& wake,
322 bool rst_active_high =
true,
bool drv_en_active_high =
true,
323 bool faultn_active_low =
false,
bool wake_active_low =
false)
noexcept;
333 bool uartSendTMCL(
const std::array<uint8_t, 9>& data)
noexcept;
349 std::array<uint8_t, 8>& rx)
noexcept;
352 bool gpioSet(tmc9660::TMC9660CtrlPin pin, tmc9660::GpioSignal signal)
noexcept;
355 bool gpioRead(tmc9660::TMC9660CtrlPin pin, tmc9660::GpioSignal& signal)
noexcept;
358 void debugLog(
int level,
const char* tag,
const char* format, va_list args)
noexcept;
361 void delayMs(uint32_t ms)
noexcept;
364 void delayUs(uint32_t us)
noexcept;
466 using DriverVariant = std::variant<std::monostate, SpiDriver*, UartDriver*>;
522 BaseGpio& faultn, BaseGpio& wake,
540 BaseGpio& faultn, BaseGpio& wake,
1143 return std::monostate{};
1154 return std::monostate{};
1179 template <
typename Func>
1184 if constexpr (std::is_void_v<ReturnType>) {
1195 if constexpr (std::is_void_v<ReturnType>) {
1203 template <
typename Func>
1208 if (!
self->EnsureInitializedLocked()) {
1209 if constexpr (std::is_void_v<ReturnType>) {
1220 if constexpr (std::is_void_v<ReturnType>) {
1246 if constexpr (std::is_void_v<ReturnType>) {
Concrete SPI communication adapter for TMC9660 using BaseSpi and BaseGpio.
Definition Tmc9660Handler.h:183
void delayMs(uint32_t ms) noexcept
Delay execution for the specified number of milliseconds.
Definition Tmc9660Handler.cpp:198
Tmc9660CtrlPins ctrl_pins_
Host-side control pin references.
Definition Tmc9660Handler.h:283
bool spiTransferBootloader(std::array< uint8_t, 5 > &tx, std::array< uint8_t, 5 > &rx) noexcept
Perform a 5-byte full-duplex SPI transfer for bootloader mode.
Definition Tmc9660Handler.cpp:176
BaseSpi & spi_
SPI bus interface (not owned).
Definition Tmc9660Handler.h:282
bool spiTransferTMCL(std::array< uint8_t, 8 > &tx, std::array< uint8_t, 8 > &rx) noexcept
Perform an 8-byte full-duplex SPI transfer for TMCL parameter mode.
Definition Tmc9660Handler.cpp:168
bool gpioRead(tmc9660::TMC9660CtrlPin pin, tmc9660::GpioSignal &signal) noexcept
Read a TMC9660 control pin's current logical signal state.
Definition Tmc9660Handler.cpp:189
void debugLog(int level, const char *tag, const char *format, va_list args) noexcept
Route TMC9660 driver debug output to the HardFOC Logger.
Definition Tmc9660Handler.cpp:194
bool gpioSet(tmc9660::TMC9660CtrlPin pin, tmc9660::GpioSignal signal) noexcept
Set a TMC9660 control pin to a given logical signal state.
Definition Tmc9660Handler.cpp:184
void delayUs(uint32_t us) noexcept
Delay execution for the specified number of microseconds.
Definition Tmc9660Handler.cpp:199
HalSpiTmc9660Comm(BaseSpi &spi, BaseGpio &rst, BaseGpio &drv_en, BaseGpio &faultn, BaseGpio &wake, bool rst_active_high=true, bool drv_en_active_high=true, bool faultn_active_low=false, bool wake_active_low=false) noexcept
Construct the SPI communication adapter.
Definition Tmc9660Handler.cpp:159
Concrete UART communication adapter for TMC9660 using BaseUart and BaseGpio.
Definition Tmc9660Handler.h:305
void delayUs(uint32_t us) noexcept
Delay execution for the specified number of microseconds.
Definition Tmc9660Handler.cpp:258
bool uartTransferBootloader(const std::array< uint8_t, 8 > &tx, std::array< uint8_t, 8 > &rx) noexcept
Perform an 8-byte UART bootloader transfer (send + receive).
Definition Tmc9660Handler.cpp:230
bool uartReceiveTMCL(std::array< uint8_t, 9 > &data) noexcept
Receive a 9-byte TMCL reply datagram over UART.
Definition Tmc9660Handler.cpp:222
HalUartTmc9660Comm(BaseUart &uart, BaseGpio &rst, BaseGpio &drv_en, BaseGpio &faultn, BaseGpio &wake, bool rst_active_high=true, bool drv_en_active_high=true, bool faultn_active_low=false, bool wake_active_low=false) noexcept
Construct the UART communication adapter.
Definition Tmc9660Handler.cpp:205
void debugLog(int level, const char *tag, const char *format, va_list args) noexcept
Route TMC9660 driver debug output to the HardFOC Logger.
Definition Tmc9660Handler.cpp:253
bool uartSendTMCL(const std::array< uint8_t, 9 > &data) noexcept
Send a 9-byte TMCL datagram over UART.
Definition Tmc9660Handler.cpp:214
Tmc9660CtrlPins ctrl_pins_
Host-side control pin references.
Definition Tmc9660Handler.h:370
bool gpioSet(tmc9660::TMC9660CtrlPin pin, tmc9660::GpioSignal signal) noexcept
Set a TMC9660 control pin to a given logical signal state.
Definition Tmc9660Handler.cpp:243
void delayMs(uint32_t ms) noexcept
Delay execution for the specified number of milliseconds.
Definition Tmc9660Handler.cpp:257
BaseUart & uart_
UART bus interface (not owned).
Definition Tmc9660Handler.h:369
bool gpioRead(tmc9660::TMC9660CtrlPin pin, tmc9660::GpioSignal &signal) noexcept
Read a TMC9660 control pin's current logical signal state.
Definition Tmc9660Handler.cpp:248
BaseAdc adapter for all TMC9660 ADC channels.
Definition Tmc9660Handler.h:729
~Adc() noexcept override=default
Default destructor.
BaseGpio adapter for a single TMC9660 internal GPIO channel.
Definition Tmc9660Handler.h:634
~Gpio() noexcept override=default
Default destructor.
BaseTemperature adapter for the TMC9660 internal chip temperature sensor.
Definition Tmc9660Handler.h:938
~Temperature() noexcept override=default
Default destructor.
Unified, non-templated handler for a single TMC9660 motor controller device.
Definition Tmc9660Handler.h:452
bool use_spi_
true = SPI communication active, false = UART.
Definition Tmc9660Handler.h:1257
std::variant< std::monostate, SpiDriver *, UartDriver * > DriverVariant
Non-owning variant holding either driver type or empty (monostate).
Definition Tmc9660Handler.h:466
auto visitDriverInternal(Func &&func) noexcept
Internal no-lock visitDriver for use from already-locked contexts.
Definition Tmc9660Handler.h:1239
std::unique_ptr< UartDriver > uart_driver_
TMC9660 driver for UART mode (or nullptr).
Definition Tmc9660Handler.h:1270
std::variant< std::monostate, const SpiDriver *, const UartDriver * > ConstDriverVariant
Const version of DriverVariant.
Definition Tmc9660Handler.h:469
bool IsDriverReady() const noexcept
Check if the TMC9660 driver has been initialized and is ready.
Definition Tmc9660Handler.cpp:374
uint8_t device_address_
7-bit TMCL device address.
Definition Tmc9660Handler.h:1284
const tmc9660::BootloaderConfig * bootCfg_
Bootloader config (not owned; must outlive handler).
Definition Tmc9660Handler.h:1283
const tmc9660::BootloaderConfig & bootConfig() const noexcept
Get the bootloader configuration in use.
Definition Tmc9660Handler.h:1048
std::unique_ptr< SpiDriver > spi_driver_
TMC9660 driver for SPI mode (or nullptr).
Definition Tmc9660Handler.h:1269
Tmc9660Handler(BaseSpi &spi, BaseGpio &rst, BaseGpio &drv_en, BaseGpio &faultn, BaseGpio &wake, uint8_t address=0, const tmc9660::BootloaderConfig *bootCfg=&kDefaultBootConfig)
Construct a handler for a TMC9660 connected via SPI.
Definition Tmc9660Handler.cpp:264
Temperature & temperature()
Get a reference to the Temperature wrapper.
Definition Tmc9660Handler.cpp:395
std::unique_ptr< HalUartTmc9660Comm > uart_comm_
UART communication adapter (or nullptr).
Definition Tmc9660Handler.h:1263
static const tmc9660::BootloaderConfig kDefaultBootConfig
Default bootloader configuration based on TMC9660-3PH-EVAL board settings.
Definition Tmc9660Handler.h:27
auto visitDriver(Func &&func) noexcept
Execute a function on the underlying typed TMC9660 driver.
Definition Tmc9660Handler.h:1180
bool EnsureInitialized() noexcept
Ensure driver is initialized (lazy init entrypoint).
Definition Tmc9660Handler.cpp:362
tmc9660::TMC9660< HalSpiTmc9660Comm > SpiDriver
TMC9660 driver instantiated with SPI communication.
Definition Tmc9660Handler.h:460
tmc9660::TMC9660< HalUartTmc9660Comm > UartDriver
TMC9660 driver instantiated with UART communication.
Definition Tmc9660Handler.h:463
std::unique_ptr< Temperature > temperatureWrapper_
Chip temperature wrapper.
Definition Tmc9660Handler.h:1278
bool EnsureInitializedLocked() noexcept
Ensure initialized while handler_mutex_ is already held.
Definition Tmc9660Handler.cpp:367
Adc & adc()
Get a reference to the ADC wrapper.
Definition Tmc9660Handler.cpp:390
SpiDriver * driverViaSpi() noexcept
Get the TMC9660 driver instance connected via SPI (nullptr if using UART).
Definition Tmc9660Handler.cpp:408
std::array< std::unique_ptr< Gpio >, 2 > gpioWrappers_
GPIO17 [0] and GPIO18 [1].
Definition Tmc9660Handler.h:1276
auto visitDriver(Func &&func) const noexcept
Const overload of visitDriver().
Definition Tmc9660Handler.h:1204
RtosMutex handler_mutex_
Recursive mutex for thread-safe access.
Definition Tmc9660Handler.h:1289
std::unique_ptr< Adc > adcWrapper_
Multi-channel ADC wrapper.
Definition Tmc9660Handler.h:1277
const char * GetDescription() const noexcept
Get a human-readable description of the handler.
Definition Tmc9660Handler.cpp:983
std::unique_ptr< HalSpiTmc9660Comm > spi_comm_
SPI communication adapter (or nullptr).
Definition Tmc9660Handler.h:1262
DriverVariant GetDriver() noexcept
Get the active driver pointer without requiring visitor usage.
Definition Tmc9660Handler.h:1136
UartDriver * driverViaUart() noexcept
Get the TMC9660 driver instance connected via UART (nullptr if using SPI).
Definition Tmc9660Handler.cpp:420
tmc9660::CommMode GetCommMode() const noexcept
Get the communication mode in use (SPI or UART).
Definition Tmc9660Handler.cpp:404
~Tmc9660Handler() noexcept
Destructor. Releases the driver, communication adapter, and all wrappers.
char description_[64]
Human-readable handler description.
Definition Tmc9660Handler.h:1292
Gpio & gpio(uint8_t gpioNumber)
Get a reference to the GPIO wrapper for a TMC9660 internal GPIO pin.
Definition Tmc9660Handler.cpp:383
bool Initialize(bool performReset=true, bool retrieveBootloaderInfo=true, bool failOnVerifyError=false)
Initialize the TMC9660 device.
Definition Tmc9660Handler.cpp:305
void DumpDiagnostics() noexcept
Dump comprehensive diagnostics to the system log.
Definition Tmc9660Handler.cpp:936
ConstDriverVariant GetDriver() const noexcept
Const overload of GetDriver().
Definition Tmc9660Handler.h:1147
Shared helper holding the four TMC9660 host-side control pin references.
Definition Tmc9660Handler.h:123
BaseGpio & wake
WAKE control pin GPIO (not owned).
Definition Tmc9660Handler.h:127
const BaseGpio & get(tmc9660::TMC9660CtrlPin pin) const noexcept
Resolve a TMC9660CtrlPin enum (const).
Definition Tmc9660Handler.h:141
BaseGpio & faultn
FAULTN status pin GPIO (not owned).
Definition Tmc9660Handler.h:126
BaseGpio & get(tmc9660::TMC9660CtrlPin pin) noexcept
Resolve a TMC9660CtrlPin enum to the corresponding BaseGpio reference.
Definition Tmc9660Handler.h:130
BaseGpio & rst
RST control pin GPIO (not owned).
Definition Tmc9660Handler.h:124
BaseGpio & drv_en
DRV_EN control pin GPIO (not owned).
Definition Tmc9660Handler.h:125