HF-Core Platform 0.1.0-dev
Hardware-Agnostic Handler Layer & RTOS Utilities for HardFOC
Loading...
Searching...
No Matches
Tmc9660Handler.h File Reference

Unified handler for TMC9660 motor controller with GPIO, ADC, and temperature integration. More...

#include <cstdint>
#include <memory>
#include <array>
#include <atomic>
#include <type_traits>
#include <variant>
#include "core/hf-core-drivers/external/hf-tmc9660-driver/inc/tmc9660.hpp"
#include "core/hf-core-drivers/external/hf-tmc9660-driver/inc/tmc9660_comm_interface.hpp"
#include "base/BaseGpio.h"
#include "base/BaseAdc.h"
#include "base/BaseTemperature.h"
#include "base/BaseSpi.h"
#include "base/BaseUart.h"
#include "RtosMutex.h"
#include <utility>
Include dependency graph for Tmc9660Handler.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  Tmc9660CtrlPins
 Shared helper holding the four TMC9660 host-side control pin references. More...
 
class  HalSpiTmc9660Comm
 Concrete SPI communication adapter for TMC9660 using BaseSpi and BaseGpio. More...
 
class  HalUartTmc9660Comm
 Concrete UART communication adapter for TMC9660 using BaseUart and BaseGpio. More...
 
class  Tmc9660Handler
 Unified, non-templated handler for a single TMC9660 motor controller device. More...
 
class  Tmc9660Handler::Gpio
 BaseGpio adapter for a single TMC9660 internal GPIO channel. More...
 
class  Tmc9660Handler::Adc
 BaseAdc adapter for all TMC9660 ADC channels. More...
 
class  Tmc9660Handler::Temperature
 BaseTemperature adapter for the TMC9660 internal chip temperature sensor. More...
 

Detailed Description

Unified handler for TMC9660 motor controller with GPIO, ADC, and temperature integration.

This file provides the complete HAL-level integration for a single TMC9660 motor driver device. It bridges the HardFOC base interfaces (BaseSpi, BaseUart, BaseGpio, BaseAdc, BaseTemperature) with the templated tmc9660::TMC9660<CommType> driver from the hf-tmc9660-driver library.

Architecture Overview

The file contains three layers:

  1. CRTP Communication Adapters (HalSpiTmc9660Comm, HalUartTmc9660Comm): Bridge HardFOC BaseSpi/BaseUart/BaseGpio to the TMC9660 driver's CRTP-based communication interfaces (tmc9660::SpiCommInterface, tmc9660::UartCommInterface). These also manage the four host-side GPIO control pins (RST, DRV_EN, FAULTN, WAKE) required by the TMC9660 bootloader initialization sequence.
  2. Tmc9660Handler (main class): Non-templated facade that owns one typed driver instance (SpiDriver or UartDriver). Uses the visitDriver() template to route calls to the active driver, keeping the public API free of template parameters. Provides:
    • Bootloader initialization with configurable reset and verification options
    • Typed driver pointers (driverViaSpi/driverViaUart) and GetDriver() variant
    • visitDriver() for generic comm-agnostic code
    • Peripheral wrappers (GPIO, ADC, Temperature) via inner classes
  3. Peripheral Wrappers (Gpio, Adc, Temperature inner classes): Implement BaseGpio, BaseAdc, and BaseTemperature respectively, delegating to the TMC9660 driver's subsystems (gpio, telemetry, etc.) via the handler's visitDriver().

Ownership Model

The handler owns all its internal resources:

  • One HalSpiTmc9660Comm or HalUartTmc9660Comm (communication adapter)
  • One SpiDriver or UartDriver (typed TMC9660 driver)
  • Two Gpio wrappers (for GPIO17, GPIO18)
  • One Adc wrapper (multi-channel TMC9660 ADC)
  • One Temperature wrapper (chip temperature sensor)

External managers (AdcManager, TemperatureManager) that need to own a BaseAdc* or BaseTemperature* should use the thin delegation wrappers Tmc9660AdcWrapper and Tmc9660TemperatureWrapper (defined in their own files), which delegate to the handler's inner class instances.

Initialization Sequence

// 1. Obtain SPI/UART interface and four GPIO control pin instances
// 2. Construct the handler
Tmc9660Handler handler(spi, rst_gpio, drv_en_gpio, faultn_gpio, wake_gpio, 0x01);
// 3. Initialize (runs bootloader config, enters parameter mode)
if (!handler.Initialize()) { return; }
// 4. Access motor control via typed driver pointer
auto* drv = handler.driverViaSpi();
drv->velocityControl.setTargetVelocity(1000);
float voltage = drv->telemetry.getSupplyVoltage();
// 5. Or via visitDriver for generic code
handler.visitDriver([](auto& d) {
d.motorControl.enable();
});
Unified, non-templated handler for a single TMC9660 motor controller device.
Definition Tmc9660Handler.h:452

Thread Safety

The handler uses RtosMutex (recursive) for thread-safe access to all public methods. visitDriver() acquires the lock automatically. Raw pointer methods (driverViaSpi(), driverViaUart()) are NOT mutex-protected — the caller is responsible for synchronization when using them from multiple tasks. The Adc and Temperature inner classes use additional RtosMutex instances for their internal statistics tracking.

See also
Tmc9660AdcWrapper Thin adapter for AdcManager ownership
Tmc9660TemperatureWrapper Thin adapter for TemperatureManager ownership
MotorController Higher-level manager that owns Tmc9660Handler instances
Author
HardFOC Team
Date
2025