ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
tmc2209.h
Go to the documentation of this file.
1#pragma once
2
4
5namespace esphome::tmc2209 {
6
7using tmc22xx::RegisterField;
8
9// StallGuard and CoolStep registers, which the TMC2208 does not have
10static constexpr uint8_t REG_TCOOLTHRS = 0x14;
11static constexpr uint8_t REG_SGTHRS = 0x40;
12static constexpr uint8_t REG_SG_RESULT = 0x41;
13static constexpr uint8_t REG_COOLCONF = 0x42;
14
15static constexpr RegisterField TCOOLTHRS{REG_TCOOLTHRS, 0, 20, false};
16static constexpr RegisterField SGTHRS{REG_SGTHRS, 0, 8, false};
17static constexpr RegisterField SG_RESULT{REG_SG_RESULT, 0, 10, false};
18static constexpr RegisterField SEMIN{REG_COOLCONF, 0, 4, false};
19static constexpr RegisterField SEUP{REG_COOLCONF, 5, 2, false};
20static constexpr RegisterField SEMAX{REG_COOLCONF, 8, 4, false};
21static constexpr RegisterField SEDN{REG_COOLCONF, 13, 2, false};
22static constexpr RegisterField SEIMIN{REG_COOLCONF, 15, 1, false};
23
25 public:
26 void dump_config() override;
27
29 template<typename F> void add_on_stall_callback(F &&callback) {
30 this->stall_callback_.add(std::forward<F>(callback));
31 }
32
34 void set_stallguard_threshold(uint8_t threshold) { this->write_field(SGTHRS, threshold); }
35 uint8_t get_stallguard_threshold() const { return extract_field(this->sgthrs_, SGTHRS); }
37 void set_tcool_threshold(uint32_t threshold) { this->write_field(TCOOLTHRS, threshold); }
38 void set_semin(uint8_t semin) { this->write_field(SEMIN, semin); }
39 void set_semax(uint8_t semax) { this->write_field(SEMAX, semax); }
40 void set_seup(uint8_t seup) { this->write_field(SEUP, seup); }
41 void set_sedn(uint8_t sedn) { this->write_field(SEDN, sedn); }
42 void set_seimin(bool seimin) { this->write_field(SEIMIN, seimin); }
43
44 protected:
45 uint8_t expected_version() const override { return 0x21; }
46 uint32_t *shadow_register(uint8_t reg) override;
47 void restore_registers() override;
48 void on_diag_without_error() override { this->stall_callback_.call(); }
49
51 // Write-only registers with their power-on values
55};
56
57} // namespace esphome::tmc2209
uint8_t get_stallguard_threshold() const
Definition tmc2209.h:35
LazyCallbackManager< void()> stall_callback_
Definition tmc2209.h:50
void set_seup(uint8_t seup)
Definition tmc2209.h:40
uint32_t * shadow_register(uint8_t reg) override
Definition tmc2209.cpp:13
void set_stallguard_threshold(uint8_t threshold)
A stall is reported when the StallGuard result drops below twice this value.
Definition tmc2209.h:34
uint8_t expected_version() const override
Definition tmc2209.h:45
void set_tcool_threshold(uint32_t threshold)
Lower velocity limit, as TSTEP, for CoolStep and the StallGuard output on DIAG.
Definition tmc2209.h:37
void set_seimin(bool seimin)
Definition tmc2209.h:42
void set_semin(uint8_t semin)
Definition tmc2209.h:38
void add_on_stall_callback(F &&callback)
Called when DIAG reports a stall. StallGuard only works in StealthChop, between TCOOLTHRS and TPWMTHR...
Definition tmc2209.h:29
void set_semax(uint8_t semax)
Definition tmc2209.h:39
void on_diag_without_error() override
Definition tmc2209.h:48
void set_sedn(uint8_t sedn)
Definition tmc2209.h:41
void restore_registers() override
Definition tmc2209.cpp:26
Base class for Trinamic stepper drivers that are configured over the single wire UART interface.
Definition tmc22xx.h:123
static uint32_t extract_field(uint32_t data, const RegisterField &field)
Definition tmc22xx.cpp:526
bool write_field(const RegisterField &field, uint32_t value)
Definition tmc22xx.cpp:500
static void uint32_t