TMC5160 Driver 0.1.0
API reference
Loading...
Searching...
No Matches
tmc5160_types.hpp
Go to the documentation of this file.
1
14#ifndef TMC5160_TYPES_HPP
15#define TMC5160_TYPES_HPP
16
17#include <array>
18#include <cstdint>
19
20#include "tmc5160_detail.hpp"
21#include "tmc5160_registers.hpp"
22#include "tmc5160_version.h"
23
25namespace tmc5160 {
26
39enum class [[nodiscard]] Error : uint8_t {
40 // Base set: the same names and values in every driver of the org (standards/driver.md).
41 Ok = 0,
42 NotInitialized = 1,
43 InvalidArgument = 2,
44 Bus = 3,
45 Timeout = 4,
47 Crc = 6,
48 Busy = 7,
49 DeviceFault = 8,
50 Unsupported = 9,
51 // Chip-specific values start at 16; each one is returned by some call.
53 16,
54 VerifyFailed = 17,
55 Refused = 18,
56 ModeMismatch = 19,
57 LimitExceeded = 20,
58};
59
65constexpr const char* ToString(Error error) noexcept {
66 switch (error) {
67 case Error::Ok:
68 return "Ok";
70 return "NotInitialized";
72 return "InvalidArgument";
73 case Error::Bus:
74 return "Bus";
75 case Error::Timeout:
76 return "Timeout";
78 return "DeviceIdMismatch";
79 case Error::Crc:
80 return "Crc";
81 case Error::Busy:
82 return "Busy";
84 return "DeviceFault";
86 return "Unsupported";
88 return "UnexpectedReset";
90 return "VerifyFailed";
91 case Error::Refused:
92 return "Refused";
94 return "ModeMismatch";
96 return "LimitExceeded";
97 default:
98 break;
99 }
100 return "Unknown";
101}
102
110template <typename T>
111struct [[nodiscard]] Result {
112 T value{};
113 Error error{Error::Ok};
114
116 [[nodiscard]] constexpr bool ok() const noexcept {
117 return error == Error::Ok;
118 }
120 constexpr explicit operator bool() const noexcept {
121 return ok();
122 }
123
129 static constexpr Result Success(T v) noexcept {
130 return {v, Error::Ok};
131 }
137 static constexpr Result Failure(Error e) noexcept {
138 return {T{}, e};
139 }
140};
141
143template <>
144struct [[nodiscard]] Result<void> {
145 Error error{Error::Ok};
146
148 [[nodiscard]] constexpr bool ok() const noexcept {
149 return error == Error::Ok;
150 }
152 constexpr explicit operator bool() const noexcept {
153 return ok();
154 }
155
157 static constexpr Result Success() noexcept {
158 return {Error::Ok};
159 }
165 static constexpr Result Failure(Error e) noexcept {
166 return {e};
167 }
168};
169
171enum class LogLevel : uint8_t {
172 Error = 0,
173 Warning,
174 Info,
175 Debug,
176};
177
179enum class CtrlPin : uint8_t {
180 DrvEnn = 0,
181 DcEnable = 1,
182 DcIn = 2,
183};
184
186enum class InputPin : uint8_t {
187 Diag0 = 0,
188 Diag1 = 1,
189 DcOut = 2,
190};
191
193enum class GpioSignal : uint8_t {
194 INACTIVE = 0,
195 ACTIVE = 1,
196};
197
199enum class BusStatus : uint8_t {
200 Ok = 0,
201 Fault = 1,
202 Timeout = 2,
203};
204
206enum class Mode : uint8_t {
207 MotionController = 0,
208 StepDir = 1,
209};
210
212enum class Verify : uint8_t {
213 None = 0,
214 Echo = 1,
215 ReadBack = 2,
216 Counter = 3,
217};
218
220enum class ChopperMode : uint8_t {
221 SpreadCycle = 0,
222 ConstOffTime = 1,
223};
224
226enum class Microsteps : uint8_t {
227 M256 = 0,
228 M128 = 1,
229 M64 = 2,
230 M32 = 3,
231 M16 = 4,
232 M8 = 5,
233 M4 = 6,
234 M2 = 7,
235 Full = 8,
236};
237
239enum class OtLevel : uint8_t {
240 C150 = 0,
241 C143 = 1,
242 C136 = 2,
243 C120 = 3,
244};
245
247enum class Freewheel : uint8_t {
248 Normal = 0,
249 Freewheel = 1,
250 ShortLowSide = 2,
251 ShortHighSide = 3,
252};
253
255enum class RampMode : uint8_t {
256 Position = 0,
257 VelocityPositive = 1,
258 VelocityNegative = 2,
259 Hold = 3,
260};
261
263enum class StopKind : uint8_t {
264 Ramp = 0,
265 Immediate = 1,
266 HoldTarget = 2,
268 Emergency = 3,
269};
270
277enum class Ack : uint8_t {
278 HoldAboveRun = 1U << 0,
279 ShortProtectionOff = 1U << 1,
280 LowOtLevel = 1U << 2,
281 HardStopWithStealthChop = 1U << 3,
282 VstartAboveVstop = 1U << 4,
283 DirectMode = 1U << 5,
284 VoltageModeNoCurrentLimit = 1U << 6,
285};
286
288struct AckSet {
289 uint32_t bits{0};
290
296 [[nodiscard]] constexpr bool Has(Ack a) const noexcept {
297 return (bits & static_cast<uint32_t>(a)) != 0U;
298 }
304 [[nodiscard]] constexpr AckSet With(Ack a) const noexcept {
305 return AckSet{bits | static_cast<uint32_t>(a)};
306 }
307};
308
310struct Motor {
311 uint32_t rated_rms_ma{0};
312 uint32_t max_rms_ma{0};
314 0};
315 uint32_t fullsteps_per_rev{200};
316};
317
319struct Board {
320 uint32_t rsense_mohm{0};
321 uint32_t supply_mv{0};
322 uint32_t supply_max_mv{0};
323};
324
326struct Clock {
327 bool external{false};
328 uint32_t hz{12'000'000};
329};
330
332struct Current {
333 uint32_t run_ma{0};
334 uint32_t hold_ma{0};
335};
336
339 uint8_t hold_delay{1};
340 uint8_t power_down_delay{10};
341 bool fast_standstill{false};
342};
343
345struct Chopper {
347 uint8_t toff{3};
348 uint8_t tbl{2};
349 uint8_t tpfd{4};
350 uint8_t hstrt{5};
351 uint8_t hend{2};
352 uint8_t tfd{0};
353 uint8_t offset{0};
354 bool no_comparator{false};
356 bool interpolate{true};
357 bool double_edge{false};
358 bool high_fullstep{false};
359 bool high_const_off{false};
360};
361
364 bool enable{false};
365 bool autoscale{true};
366 bool autograd{true};
367 uint8_t freq{0};
368 uint8_t reg{4};
369 uint8_t lim{12};
370 uint8_t ofs{30};
371 uint8_t grad{0};
373};
374
377 uint32_t pwm{0};
378 uint32_t cool{0};
379 uint32_t high{0};
380 bool small_hysteresis{false};
381};
382
384struct CoolStep {
385 uint8_t semin{0};
386 uint8_t semax{0};
387 uint8_t seup{0};
388 uint8_t sedn{0};
389 bool min_quarter{false};
390};
391
394 int8_t threshold{0};
395 bool filter{false};
396};
397
399struct DcStep {
400 uint32_t vdcmin{0};
401 uint16_t dc_time{0};
402 uint8_t dc_sg{0};
403};
404
406struct Bridge {
407 bool declared{false};
408 uint8_t bbm_time{0};
409 uint8_t bbm_clks{4};
410 uint8_t drive_strength{0};
411 uint8_t sense_filter{0};
414};
415
418 bool declared{false};
419 uint8_t s2vs_level{6};
420 uint8_t s2g_level{6};
421 uint8_t filter{1};
422 bool long_delay{false};
423 bool disable_s2g{false};
424 bool disable_s2vs{false};
425};
426
428struct Flags {
429 bool invert_direction{false};
430 bool recalibrate{false};
431 bool multistep_filt{false};
432 bool stop_input{false};
433};
434
436struct Diag {
437 bool diag0_error{false};
438 bool diag0_otpw{false};
439 bool diag0_stall{false};
440 bool diag0_step_output{false};
441 bool diag1_stall{false};
442 bool diag1_dir_output{false};
443 bool diag1_index{false};
444 bool diag1_on_state{false};
446 bool diag0_push_pull{false};
447 bool diag1_push_pull{false};
448};
449
453 uint32_t vstart{0};
454 uint32_t a1{1000};
455 uint32_t v1{50'000};
456 uint32_t amax{500};
457 uint32_t vmax{200'000};
458 uint32_t dmax{700};
459 uint32_t d1{1400};
460 uint32_t vstop{10};
461 uint32_t zero_wait{0};
462};
463
465struct Switches {
466 bool stop_left{false};
467 bool stop_right{false};
468 bool left_active_low{false};
469 bool right_active_low{false};
470 bool swap{false};
471 bool latch_left_active{false};
473 bool latch_right_active{false};
475 bool latch_encoder{false};
476 bool stop_on_stall{false};
477 bool soft_stop{false};
478};
479
481struct NEvent {
482 bool pol_a{false};
483 bool pol_b{false};
484 bool pol_n{false};
485 bool ignore_ab{false};
486 bool repeat{false};
487 bool once{false};
488 bool pos_edge{false};
489 bool neg_edge{false};
490 bool clear_counter{false};
491 bool latch_xactual{false};
492};
493
496 bool decimal{false};
497 int16_t integer{1};
498 uint16_t fraction{0};
499 bool negative{false};
500};
501
503struct Encoder {
504 bool declared{false};
507 uint32_t deviation{0};
508};
509
512 std::array<uint32_t, 8> words{reg::kDefaultMslut};
513 std::array<uint8_t, 4> w{2, 1, 1, 1};
514 uint8_t x1{128};
515 uint8_t x2{255};
516 uint8_t x3{255};
517 uint8_t start_sin{0};
518 uint8_t start_sin90{247};
519};
520
523 uint8_t node{0};
524 uint8_t send_delay{8};
525 bool multi_node{false};
526 uint32_t baud{115'200};
527};
528
531 uint8_t chain_length{1};
532 uint8_t chain_index{0};
533};
534
567
570inline constexpr uint32_t kInternalClockHz = 12'000'000;
571inline constexpr uint32_t kExternalClockMinHz = 4'000'000;
572inline constexpr uint32_t kExternalClockMaxHz = 18'000'000;
573inline constexpr uint32_t kVfsMv = 325;
574inline constexpr uint32_t kCurrentTolerancePercent = 5;
575inline constexpr uint32_t kVsMinMv = 10'000;
576inline constexpr uint32_t kVsMaxMv = 55'000;
577inline constexpr uint32_t kUartMinBaud = 9000;
578inline constexpr uint32_t kUartRecoveryBits = 12;
579inline constexpr uint32_t kUartReadRetries = 2;
580inline constexpr uint32_t kMaxChain = 8;
581inline constexpr uint32_t kVmaxLimit = (1U << 23) - 512U;
582inline constexpr uint32_t kMaxSckInternalHz =
583 4'000'000;
585
586namespace detail {
588template <>
589struct EnumLast<Mode> {
590 static constexpr Mode kValue = Mode::StepDir;
591};
592template <>
593struct EnumLast<Verify> {
594 static constexpr Verify kValue = Verify::Counter;
595};
596template <>
597struct EnumLast<ChopperMode> {
598 static constexpr ChopperMode kValue = ChopperMode::ConstOffTime;
599};
600template <>
601struct EnumLast<Microsteps> {
602 static constexpr Microsteps kValue = Microsteps::Full;
603};
604template <>
605struct EnumLast<OtLevel> {
606 static constexpr OtLevel kValue = OtLevel::C120;
607};
608template <>
609struct EnumLast<Freewheel> {
610 static constexpr Freewheel kValue = Freewheel::ShortHighSide;
611};
612template <>
613struct EnumLast<RampMode> {
614 static constexpr RampMode kValue = RampMode::Hold;
615};
616template <>
617struct EnumLast<StopKind> {
618 static constexpr StopKind kValue = StopKind::Emergency;
619};
620
622[[nodiscard]] constexpr int32_t TableStep(uint32_t width_code, uint32_t bit) noexcept {
623 return static_cast<int32_t>(width_code & 3U) - 1 + static_cast<int32_t>(bit & 1U);
624}
625
627[[nodiscard]] constexpr uint32_t SegmentWidth(const MicrostepTable& t, uint32_t n) noexcept {
628 if (n < t.x1) {
629 return t.w[0];
630 }
631 if (n < t.x2) {
632 return t.w[1];
633 }
634 return (n < t.x3) ? t.w[2] : t.w[3];
635}
636
638[[nodiscard]] constexpr int32_t TableEntry(const MicrostepTable& t, uint32_t n) noexcept {
639 auto value = static_cast<int32_t>(t.start_sin);
640 for (uint32_t i = 1U; i <= (n & 255U); ++i) {
641 const uint32_t bit = (t.words[(i >> 5U) & 7U] >> (i & 31U)) & 1U;
642 value += TableStep(SegmentWidth(t, i), bit);
643 }
644 return value;
645}
646} // namespace detail
647
654[[nodiscard]] constexpr Error CheckTable(const MicrostepTable& table) noexcept {
655 if ((table.x1 == 0U) || (table.x1 >= table.x2) || (table.x2 > table.x3)) {
657 }
658 auto value = static_cast<int32_t>(table.start_sin);
659 for (uint32_t i = 1U; i <= 255U; ++i) {
660 const uint32_t bit = (table.words[(i >> 5U) & 7U] >> (i & 31U)) & 1U;
661 value += detail::TableStep(detail::SegmentWidth(table, i), bit);
662 if ((value < -248) || (value > 248)) {
664 }
665 }
666 const auto last = static_cast<int32_t>(table.start_sin90);
667 return (value == last) ? Error::Ok : Error::InvalidArgument;
668}
669
671enum class Advice : uint16_t {
672 IrunBelow16 = 1U << 0,
673 GlobalScalerBelow129 = 1U << 1,
674 VstopBelow10 = 1U << 2,
675 PwmLimBelow5 = 1U << 3,
676 PwmFrequency = 1U << 4,
677 ToffBelow2 = 1U << 5,
678 ExternalClock = 1U << 6,
679 TblOutside = 1U << 7,
680 HoldAbove70 = 1U << 8,
681};
682
683namespace detail {
685[[nodiscard]] constexpr Error Need(bool ok) noexcept {
686 return ok ? Error::Ok : Error::InvalidArgument;
687}
689[[nodiscard]] constexpr Error Both(Error a, Error b) noexcept {
690 return (a != Error::Ok) ? a : b;
691}
693[[nodiscard]] constexpr Error Range(uint32_t v, uint32_t lo, uint32_t hi) noexcept {
694 return Need((v >= lo) && (v <= hi));
695}
697[[nodiscard]] constexpr Error Order(uint32_t a, uint32_t b) noexcept {
698 return Need(a <= b);
699}
701[[nodiscard]] constexpr Error Unless(bool when, Error e) noexcept {
702 return when ? e : Error::Ok;
703}
704
705[[nodiscard]] constexpr Error ValidateClock(const Config& c) noexcept {
706 Error e = Need(IsEnumerator(c.mode) && IsEnumerator(c.verify));
707 e = Both(e, c.clock.external ? Range(c.clock.hz, kExternalClockMinHz, kExternalClockMaxHz)
708 : Need(c.clock.hz == kInternalClockHz));
709 e = Both(e, Range(c.uart.node, 0U,
710 c.uart.multi_node ? 254U : 253U)); // 254: the addressing procedure (A31)
711 e = Both(e, Range(c.uart.send_delay, c.uart.multi_node ? 2U : 0U, 15U));
712 e = Both(e, Range(c.uart.baud, kUartMinBaud, c.clock.hz / 16U));
713 e = Both(e, Range(c.spi.chain_length, 1U, kMaxChain));
714 return Both(e, Order(static_cast<uint32_t>(c.spi.chain_index) + 1U, c.spi.chain_length));
715}
716
717[[nodiscard]] constexpr Error ValidateDeclared(const Config& c) noexcept {
718 const uint32_t supply_max =
719 (c.board.supply_max_mv == 0U) ? c.board.supply_mv : c.board.supply_max_mv;
720 Error e = Range(c.motor.rated_rms_ma, 1U, 100'000U);
721 e = Both(e, Range(c.motor.max_rms_ma, 0U, 100'000U));
722 e = Both(e, Range(c.motor.fullsteps_per_rev, 1U, 1'000'000U));
723 e = Both(e, Range(c.board.rsense_mohm, 1U, 10'000U));
724 e = Both(e, Range(c.board.supply_mv, kVsMinMv, kVsMaxMv));
725 e = Both(e, Range(supply_max, c.board.supply_mv, kVsMaxMv));
726 e = Both(e, Range(c.current.run_ma, 1U, 100'000U));
727 return Both(e, Range(c.standstill.hold_delay, 0U, 15U));
728}
729
730[[nodiscard]] constexpr Error ValidateChopper(const Config& c) noexcept {
731 const Chopper& k = c.chopper;
732 Error e = Need(IsEnumerator(k.mode) && IsEnumerator(k.mres));
733 e = Both(e, Range(k.toff, 1U, 15U));
734 e = Both(e, Range(k.tbl, (k.toff == 1U) ? 2U : 0U, 3U));
735 e = Both(e, Range(k.tpfd, 0U, 15U));
736 e = Both(e, Range(k.hstrt, 0U, 7U));
737 e = Both(e, Range(k.hend, 0U, 15U));
738 e = Both(e, Range(k.tfd, 0U, 15U));
739 e = Both(e, Range(k.offset, 0U, 15U));
740 const uint32_t effective = static_cast<uint32_t>(k.hstrt) + static_cast<uint32_t>(k.hend);
741 return Both(e, Unless(k.mode == ChopperMode::SpreadCycle, Order(effective, 18U)));
742}
743
744[[nodiscard]] constexpr Error ValidateStealth(const Config& c) noexcept {
745 const StealthChop& s = c.stealth;
746 Error e = Need(IsEnumerator(s.freewheel));
747 e = Both(e, Range(s.freq, 0U, 3U));
748 e = Both(e, Range(s.reg, s.autoscale ? 1U : 0U, 15U));
749 e = Both(e, Range(s.lim, 0U, 15U));
750 return Both(e, Range(c.standstill.power_down_delay, (s.enable && s.autoscale) ? 2U : 0U, 255U));
751}
752
753[[nodiscard]] constexpr Error ValidateThresholds(const Config& c) noexcept {
754 const Thresholds& t = c.thresholds;
755 Error e = Range(t.pwm, 0U, 0xFFFFFU);
756 e = Both(e, Range(t.cool, 0U, 0xFFFFFU));
757 e = Both(e, Range(t.high, 0U, 0xFFFFFU));
758 e = Both(e, Unless((t.cool != 0U) && (t.pwm != 0U), Order(t.cool, t.pwm)));
759 e = Both(e, Unless((t.high != 0U) && (t.cool != 0U), Order(t.high, t.cool)));
760 const bool needs_cool = (c.cool.semin != 0U) || c.switches.stop_on_stall;
761 e = Both(e, Unless(needs_cool, Range(t.cool, 1U, 0xFFFFFU)));
762 e = Both(e, Range(c.cool.semin, 0U, 15U));
763 e = Both(e, Range(c.cool.semax, 0U, 15U));
764 e = Both(e, Range(c.cool.seup, 0U, 3U));
765 e = Both(e, Range(c.cool.sedn, 0U, 3U));
766 e = Both(e, Need((c.stall.threshold >= -64) && (c.stall.threshold <= 63)));
767 e = Both(e, Unless(c.dcstep.vdcmin != 0U, Range(c.dcstep.vdcmin, 256U, 0x7FFFFFU)));
768 return Both(e, Range(c.dcstep.dc_time, 0U, 1023U));
769}
770
771[[nodiscard]] constexpr Error ValidateDcStep(const Config& c) noexcept {
772 Error e = Need(!c.custom_table);
773 e = Both(e, Need(c.chopper.high_fullstep && c.chopper.high_const_off));
774 e = Both(e, Range(c.chopper.toff, 3U, 15U));
775 // DC_TIME has a lower limit: tBLANK in clock cycles plus 1 to 100 (page 101, A44); at or below
776 // tBLANK the load measurement never sees a reference pulse.
777 constexpr std::array<uint32_t, 4> kBlankClocks{16U, 24U, 36U, 54U};
778 const uint32_t lowest = kBlankClocks[static_cast<std::size_t>(c.chopper.tbl & 3U)] + 1U;
779 return Unless(c.dcstep.vdcmin != 0U, Both(e, Range(c.dcstep.dc_time, lowest, 1023U)));
780}
781
782[[nodiscard]] constexpr uint32_t BbmNs(const Bridge& b) noexcept {
783 const uint32_t from_time = (static_cast<uint32_t>(b.bbm_time) * 83U) / 10U;
784 const uint32_t from_clks = static_cast<uint32_t>(b.bbm_clks) * 83U;
785 return (from_time > from_clks) ? from_time : from_clks;
786}
787
788[[nodiscard]] constexpr Error ValidateBridge(const Config& c) noexcept {
789 const Bridge& b = c.bridge;
790 Error e = Need(IsEnumerator(b.ot_level));
791 e = Both(e, Range(b.bbm_time, 0U, 24U));
792 e = Both(e, Range(b.bbm_clks, 0U, 15U));
793 e = Both(e, Range(b.drive_strength, 0U, 3U));
794 e = Both(e, Range(b.sense_filter, 0U, 3U));
795 e = Both(e, Unless(b.mosfet_switching_ns != 0U,
796 Order(static_cast<uint32_t>(b.mosfet_switching_ns) * 130U, BbmNs(b) * 100U)));
797 const Protection& p = c.protection;
798 const uint32_t supply_max =
799 (c.board.supply_max_mv == 0U) ? c.board.supply_mv : c.board.supply_max_mv;
800 e = Both(e, Range(p.s2vs_level, 4U, 15U));
801 e = Both(e, Range(p.s2g_level, (supply_max > 52'000U) ? 12U : 2U, 15U));
802 return Both(e, Range(p.filter, 0U, 3U));
803}
804
805[[nodiscard]] constexpr Error ValidateRamp(const Config& c) noexcept {
806 const RampProfile& r = c.ramp;
807 Error e = Range(r.vstart, 0U, 0x3FFFFU);
808 e = Both(e, Range(r.a1, 0U, 0xFFFFU));
809 e = Both(e, Range(r.v1, 0U, 0xFFFFFU));
810 e = Both(e, Range(r.amax, 0U, 0xFFFFU));
811 e = Both(e, Range(r.dmax, 0U, 0xFFFFU));
812 e = Both(e, Range(r.d1, 1U, 0xFFFFU));
813 e = Both(e, Range(r.vstop, 1U, 0x3FFFFU));
814 e = Both(e, Range(r.zero_wait, 0U, 0xFFFFU));
815 e = Both(e,
816 Range(r.vmax, r.vstart, (c.max_velocity_reg == 0U) ? kVmaxLimit : c.max_velocity_reg));
817 e = Both(e, Order(r.vmax, kVmaxLimit));
818 return e;
819}
820
821[[nodiscard]] constexpr Error ValidateDiag(const Config& c) noexcept {
822 const Diag& d = c.diag;
823 const bool sd = c.mode == Mode::StepDir;
824 const bool sd_only = d.diag0_error || d.diag0_otpw || d.diag0_stall || d.diag1_stall ||
825 d.diag1_index || d.diag1_on_state;
826 Error e = Unless(!sd, Need(!sd_only));
827 e = Both(e, Unless(sd, Need(!(d.diag0_step_output || d.diag1_dir_output))));
828 const bool other1 = d.diag1_stall || d.diag1_index || d.diag1_on_state || d.diag1_dir_output;
829 e = Both(e, Unless(d.diag1_steps_skipped, Need(!other1)));
830 e = Both(e, Need(!(c.switches.soft_stop && c.switches.stop_on_stall)));
831 e = Both(e, Range(c.encoder.deviation, 0U, 0xFFFFFU));
832 return Both(e, Order(c.encoder.scale.fraction, c.encoder.scale.decimal ? 9999U : 65'535U));
833}
834} // namespace detail
835
836namespace detail {
838[[nodiscard]] constexpr Error NeedAck(bool needs, const Config& c, Ack ack) noexcept {
839 return (needs && !c.ack.Has(ack)) ? Error::Refused : Error::Ok;
840}
841} // namespace detail
842
849[[nodiscard]] constexpr Error CheckAcks(const Config& c) noexcept {
850 using detail::Both;
851 using detail::NeedAck;
852 const bool short_off = c.protection.disable_s2g || c.protection.disable_s2vs;
853 const bool low_ot = (c.bridge.ot_level == OtLevel::C120) ||
854 ((c.bridge.ot_level == OtLevel::C136) && (c.board.supply_mv > 24'000U));
855 const bool hard_stop =
856 (c.switches.stop_left || c.switches.stop_right || c.switches.stop_on_stall) &&
857 !c.switches.soft_stop;
858 Error e = NeedAck(short_off, c, Ack::ShortProtectionOff);
859 e = Both(e, NeedAck(c.bridge.declared && low_ot, c, Ack::LowOtLevel));
860 e = Both(e, NeedAck(hard_stop && c.stealth.enable, c, Ack::HardStopWithStealthChop));
861 return Both(e, NeedAck(c.ramp.vstart > c.ramp.vstop, c, Ack::VstartAboveVstop));
862}
863
875[[nodiscard]] constexpr Error Validate(const Config& config) noexcept {
876 Error e = detail::ValidateClock(config);
877 e = detail::Both(e, detail::ValidateDeclared(config));
878 e = detail::Both(e, detail::ValidateChopper(config));
879 e = detail::Both(e, detail::ValidateStealth(config));
880 e = detail::Both(e, detail::ValidateThresholds(config));
881 e = detail::Both(e, detail::ValidateDcStep(config));
882 e = detail::Both(e, detail::ValidateBridge(config));
883 e = detail::Both(e, detail::ValidateRamp(config));
884 e = detail::Both(e, detail::ValidateDiag(config));
885 return detail::Both(e, config.custom_table ? CheckTable(config.table) : Error::Ok);
886}
887
893[[nodiscard]] constexpr uint32_t Advise(const Config& config) noexcept {
894 uint32_t bits = 0U;
895 const auto flag = [&bits](bool on, Advice a) noexcept {
896 bits |= static_cast<uint32_t>(on) * static_cast<uint32_t>(a);
897 };
898 flag(config.chopper.toff < 2U, Advice::ToffBelow2);
899 flag((config.chopper.tbl < 1U) || (config.chopper.tbl > 2U), Advice::TblOutside);
900 flag(config.ramp.vstop < 10U, Advice::VstopBelow10);
901 flag(config.stealth.lim < 5U, Advice::PwmLimBelow5);
902 flag(config.stealth.freq != 0U, Advice::PwmFrequency);
903 flag(
904 config.clock.external && ((config.clock.hz < 10'000'000U) || (config.clock.hz > 16'000'000U)),
906 flag((config.current.hold_ma * 10U) > (config.current.run_ma * 7U), Advice::HoldAbove70);
907 return bits;
908}
909
914void ConfigIsInvalid() noexcept;
915
923public:
928 consteval explicit CheckedConfig(const Config& config) noexcept : config_(config) {
929 if (Validate(config) != Error::Ok) {
931 }
932 }
934 [[nodiscard]] constexpr const Config& Get() const& noexcept {
935 return config_;
936 }
938 void Get() const&& = delete;
939
940private:
941 Config config_;
942};
943
952[[nodiscard]] constexpr Config MakeConfig(const Motor& motor, const Board& board) noexcept {
953 Config c{};
954 c.motor = motor;
955 c.board = board;
956 const uint32_t ceiling = (motor.max_rms_ma == 0U) ? motor.rated_rms_ma : motor.max_rms_ma;
957 c.current.run_ma = (ceiling * 85U) / 100U;
958 c.current.hold_ma = (c.current.run_ma * 3U) / 10U;
959 return c;
960}
961
962// A declared motor and power stage give a valid configuration; the default table decodes (A6).
963static_assert(Validate(Config{}) == Error::InvalidArgument);
964static_assert(Validate(MakeConfig(Motor{.rated_rms_ma = 1000},
965 Board{.rsense_mohm = 100, .supply_mv = 24'000})) == Error::Ok);
966static_assert(CheckTable(MicrostepTable{}) == Error::Ok);
967
969constexpr const char* GetDriverVersion() noexcept {
970 return TMC5160_VERSION_STRING;
971}
973constexpr uint32_t GetDriverVersionMajor() noexcept {
974 return TMC5160_VERSION_MAJOR;
975}
977constexpr uint32_t GetDriverVersionMinor() noexcept {
978 return TMC5160_VERSION_MINOR;
979}
981constexpr uint32_t GetDriverVersionPatch() noexcept {
982 return TMC5160_VERSION_PATCH;
983}
984
987} // namespace tmc5160
988
989#endif // TMC5160_TYPES_HPP
A configuration proven valid at build time.
Definition tmc5160_types.hpp:922
constexpr const Config & Get() const &noexcept
Definition tmc5160_types.hpp:934
consteval CheckedConfig(const Config &config) noexcept
Check config at build time.
Definition tmc5160_types.hpp:928
void Get() const &&=delete
Not on a temporary: the reference would dangle (KB-CPP20-044).
constexpr std::array< uint32_t, 8 > kDefaultMslut
Default sine table words, MSLUT[0] to MSLUT[7] (page 106).
Definition tmc5160_registers.hpp:113
constexpr uint32_t kCurrentTolerancePercent
Full-scale current tolerance.
Definition tmc5160_types.hpp:574
constexpr Error CheckTable(const MicrostepTable &table) noexcept
Whether a table is acceptable: segment starts ordered, every entry within -248 to 247 (the peak may r...
Definition tmc5160_types.hpp:654
constexpr uint32_t kVsMinMv
Lowest VS (page 120).
Definition tmc5160_types.hpp:575
BusStatus
Result of one UART exchange as the port reports it.
Definition tmc5160_types.hpp:199
constexpr uint32_t kMaxChain
Longest SPI daisy chain the driver frames.
Definition tmc5160_types.hpp:580
Microsteps
Microstep resolution (CHOPCONF.MRES): the value is the shift from 256 microsteps.
Definition tmc5160_types.hpp:226
constexpr uint32_t kVfsMv
Sense threshold, typical (page 123).
Definition tmc5160_types.hpp:573
Mode
Where the chip's step pulses come from (SD_MODE strap, section 2.2 page 13).
Definition tmc5160_types.hpp:206
constexpr uint32_t kMaxSckInternalHz
SPI clock with the internal clock (page 25).
Definition tmc5160_types.hpp:582
constexpr const char * GetDriverVersion() noexcept
Definition tmc5160_types.hpp:969
constexpr const char * ToString(Error error) noexcept
The name of an error, for logs.
Definition tmc5160_types.hpp:65
Advice
Soft guidance bits from Advise(); never an error.
Definition tmc5160_types.hpp:671
constexpr uint32_t kUartMinBaud
Lowest UART baud rate (page 26).
Definition tmc5160_types.hpp:577
LogLevel
Severity passed to the optional Log() hook.
Definition tmc5160_types.hpp:171
InputPin
Input pins the port may read through the optional GpioGet() hook.
Definition tmc5160_types.hpp:186
Verify
Extra traffic a setter spends to prove a write (handbook KB-ARCH-041).
Definition tmc5160_types.hpp:212
Freewheel
Standstill mode with IHOLD = 0 (PWMCONF.freewheel, page 38).
Definition tmc5160_types.hpp:247
constexpr Error CheckAcks(const Config &c) noexcept
The guards that an acknowledgement can lift (section 2.5 of the design): each returns Refused when th...
Definition tmc5160_types.hpp:849
StopKind
How Stop() brings the motor to rest (section 6.3.2, page 43).
Definition tmc5160_types.hpp:263
OtLevel
Overtemperature shutdown level (DRV_CONF.OTSELECT, page 36).
Definition tmc5160_types.hpp:239
constexpr Config MakeConfig(const Motor &motor, const Board &board) noexcept
A valid configuration for a declared motor and power stage: run current 90 percent of the rated curre...
Definition tmc5160_types.hpp:952
constexpr Error Validate(const Config &config) noexcept
Check a configuration without touching the chip.
Definition tmc5160_types.hpp:875
Ack
Acknowledgements: each lets one call do what the datasheet warns against.
Definition tmc5160_types.hpp:277
constexpr uint32_t kVmaxLimit
Highest VMAX (page 41).
Definition tmc5160_types.hpp:581
constexpr uint32_t kExternalClockMinHz
Lowest external clock (page 122).
Definition tmc5160_types.hpp:571
constexpr uint32_t kInternalClockHz
Internal oscillator (page 119).
Definition tmc5160_types.hpp:570
void ConfigIsInvalid() noexcept
Reached only when a CheckedConfig is built from an invalid configuration: it is never defined,...
CtrlPin
Output pins the port may drive through the optional GpioSet() hook.
Definition tmc5160_types.hpp:179
constexpr uint32_t kUartRecoveryBits
Idle bit times after a UART error.
Definition tmc5160_types.hpp:578
constexpr uint32_t kExternalClockMaxHz
Highest external clock (page 122).
Definition tmc5160_types.hpp:572
constexpr uint32_t GetDriverVersionMajor() noexcept
Definition tmc5160_types.hpp:973
constexpr uint32_t GetDriverVersionPatch() noexcept
Definition tmc5160_types.hpp:981
RampMode
Ramp generator mode (RAMPMODE, page 40).
Definition tmc5160_types.hpp:255
ChopperMode
Chopper mode (CHOPCONF.chm).
Definition tmc5160_types.hpp:220
constexpr uint32_t GetDriverVersionMinor() noexcept
Definition tmc5160_types.hpp:977
constexpr uint32_t kVsMaxMv
Highest VS (page 120).
Definition tmc5160_types.hpp:576
GpioSignal
Logical level of a control pin; the port maps it to the physical polarity.
Definition tmc5160_types.hpp:193
Error
Why a call failed. Every call of the driver returns one of these in its Result.
Definition tmc5160_types.hpp:39
constexpr uint32_t kUartReadRetries
Extra attempts of a failed UART read.
Definition tmc5160_types.hpp:579
constexpr uint32_t Advise(const Config &config) noexcept
Soft guidance for a configuration: bits of Advice, never an error.
Definition tmc5160_types.hpp:893
@ Fault
The line or the peripheral failed.
@ M256
256 microsteps per full step.
@ Full
Full steps.
@ MotionController
SD_MODE low: the internal ramp generator moves the motor. Default.
@ StepDir
SD_MODE high: STEP and DIR pins; serial configuration, no ramp.
@ ExternalClock
External clock outside 10 to 16 MHz (page 122).
@ HoldAbove70
Hold current above 70 percent of run (page 38).
@ PwmFrequency
PWM frequency outside 20 to 50 kHz (page 59).
@ IrunBelow16
IRUN below 16: coarse microsteps (page 38).
@ GlobalScalerBelow129
GLOBALSCALER below 129 (page 36).
@ ToffBelow2
TOFF below 2 (page 51).
@ TblOutside
TBL outside 1 to 2 (page 51).
@ PwmLimBelow5
PWM_LIM below 5 (page 54).
@ VstopBelow10
VSTOP below 10 (page 41).
@ Warning
Something unexpected that the driver recovered from.
@ Info
Lifecycle events: initialised, reset.
@ Debug
Detail for bring-up.
@ DcOut
ENCN_DCO_CFG6: DcStep ready output.
@ Diag1
DIAG1_SWP: position compare or stall output.
@ Diag0
DIAG0_SWN: interrupt or error output.
@ Counter
UART: one IFCNT read; it must have counted this write (page 33).
@ None
No extra traffic.
@ Echo
SPI: one more transfer; its reply must mirror the data just written (page 24).
@ ReadBack
Read the register back where the chip allows it (one or two transfers).
@ ShortHighSide
Coils shorted through the high-side MOSFETs.
@ ShortLowSide
Coils shorted through the low-side MOSFETs.
@ Normal
Normal operation.
@ Immediate
VSTART 0 and VMAX 0 in velocity mode: still the AMAX ramp (no VSTART floor).
@ Ramp
Velocity mode with VMAX 0: decelerate with AMAX (page 83).
@ Emergency
Driver off (DRV_ENN, TOFF 0): the motor is not held.
@ C143
143 degrees Celsius.
@ C120
120 degrees Celsius.
@ C150
150 degrees Celsius.
@ C136
136 degrees Celsius.
@ DirectMode
Direct mode: coil currents from XTARGET (page 110).
@ VoltageModeNoCurrentLimit
Direct mode with a voltage-mode chopper (page 110).
@ ShortProtectionOff
diss2g or diss2vs set (page 22).
@ HoldAboveRun
IHOLD above IRUN (page 38).
@ LowOtLevel
120 degrees shutdown, or 136 degrees above 24 V (page 36).
@ HardStopWithStealthChop
Hard stop in StealthChop: overcurrent risk (page 43).
@ VstartAboveVstop
VSTOP below VSTART (page 41).
@ DcIn
ENCA_DCIN_CFG5: DcStep input or emergency stop input.
@ DcEnable
ENCB_DCEN_CFG4: DcStep enable input (STEP/DIR mode).
@ DrvEnn
DRV_ENN, active low on the package pin: ACTIVE means the driver is enabled.
@ VelocityNegative
Run at VMAX in the negative direction.
@ Position
Move to XTARGET with the six-point ramp.
@ VelocityPositive
Run at VMAX in the positive direction.
@ Hold
Velocity stays constant; used as the safe state.
@ SpreadCycle
Hysteresis decay, page 51.
@ ConstOffTime
Classic constant off time.
@ ACTIVE
The pin function is asserted.
@ INACTIVE
The pin function is deasserted.
@ UnexpectedReset
The chip reset (GSTAT.reset, SPI status bit 0): every write-only register is lost.
@ LimitExceeded
The request exceeds a declared motor, board or ramp limit.
@ InvalidArgument
An argument or a Config field is out of range; nothing was sent.
@ DeviceIdMismatch
IOIN.VERSION is not 0x30: another chip, no chip or a wiring fault.
@ Crc
The CRC-8 of a UART reply did not match (never produced on SPI).
@ Bus
A port hook returned false.
@ VerifyFailed
A write-back, echo or interface-counter check did not match.
@ DeviceFault
The driver reports an error (GSTAT.drv_err): short or overtemperature.
@ NotInitialized
EnsureInitialized() has not succeeded, or Deinitialize() ran since.
@ ModeMismatch
The SD_MODE strap or the bus does not match Config::mode.
@ Ok
The call succeeded.
@ Unsupported
The hook, bus or mode this call needs is absent.
@ Timeout
The chip did not answer in the allowed time or a bounded wait ran out.
@ Busy
The motor is moving or a ramp is running: the call needs standstill.
@ Refused
A guard refused the call: a missing acknowledgement or an unsafe order.
The TMC5160 driver: everything it declares lives here; internals in detail.
Definition tmc5160.hpp:34
A set of acknowledgements.
Definition tmc5160_types.hpp:288
constexpr bool Has(Ack a) const noexcept
Whether a is in the set.
Definition tmc5160_types.hpp:296
uint32_t bits
The Ack values or-ed together.
Definition tmc5160_types.hpp:289
constexpr AckSet With(Ack a) const noexcept
A copy with a added.
Definition tmc5160_types.hpp:304
The power stage as built: the sense resistor and the supply.
Definition tmc5160_types.hpp:319
uint32_t rsense_mohm
Sense resistor per coil in mOhm. Required, 1 or more.
Definition tmc5160_types.hpp:320
uint32_t supply_mv
Motor supply VS in mV, 10000 to 55000 (section 28.1, page 120).
Definition tmc5160_types.hpp:321
uint32_t supply_max_mv
Highest supply that can occur in mV; 0 means supply_mv.
Definition tmc5160_types.hpp:322
Gate driver settings (DRV_CONF, section 6.1 page 35); written only when declared.
Definition tmc5160_types.hpp:406
uint8_t sense_filter
FILT_ISENSE 0 to 3: 100, 200, 300, 400 ns.
Definition tmc5160_types.hpp:411
bool declared
true: the application declares these values; false: not written.
Definition tmc5160_types.hpp:407
uint8_t bbm_time
BBMTIME 0 to 24 (about 8.3 ns each).
Definition tmc5160_types.hpp:408
uint16_t mosfet_switching_ns
Slowest MOSFET switching time in ns, 0 = not declared.
Definition tmc5160_types.hpp:413
uint8_t drive_strength
DRVSTRENGTH 0 to 3.
Definition tmc5160_types.hpp:410
uint8_t bbm_clks
BBMCLKS 0 to 15 clocks.
Definition tmc5160_types.hpp:409
OtLevel ot_level
Overtemperature shutdown level.
Definition tmc5160_types.hpp:412
Chopper configuration (CHOPCONF, section 6.5.2 page 51).
Definition tmc5160_types.hpp:345
uint8_t tfd
Constant off time: fast decay time 0 to 15.
Definition tmc5160_types.hpp:352
uint8_t hstrt
SpreadCycle hysteresis start 0 to 7 (reset 5).
Definition tmc5160_types.hpp:350
uint8_t offset
Constant off time: sine wave offset 0 to 15.
Definition tmc5160_types.hpp:353
uint8_t tbl
Blank time 0 to 3: 16, 24, 36, 54 clocks; reset value 2.
Definition tmc5160_types.hpp:348
bool high_fullstep
Full steps above THIGH.
Definition tmc5160_types.hpp:358
bool high_const_off
Constant off time above THIGH.
Definition tmc5160_types.hpp:359
uint8_t tpfd
Passive fast decay 0 to 15, 128 clocks per step; reset value 4.
Definition tmc5160_types.hpp:349
uint8_t hend
SpreadCycle hysteresis end 0 to 15 (reset 2).
Definition tmc5160_types.hpp:351
uint8_t toff
Off time 1 to 15 (0 is Disable()); reset value 0 means off.
Definition tmc5160_types.hpp:347
ChopperMode mode
SpreadCycle or constant off time.
Definition tmc5160_types.hpp:346
bool no_comparator
Constant off time: no current comparator on fast decay.
Definition tmc5160_types.hpp:354
bool interpolate
Interpolate to 256 microsteps (reset 1).
Definition tmc5160_types.hpp:356
bool double_edge
Step on both STEP edges.
Definition tmc5160_types.hpp:357
Microsteps mres
Microstep resolution.
Definition tmc5160_types.hpp:355
The clock: internal 12 MHz or an external clock on CLK (page 119).
Definition tmc5160_types.hpp:326
bool external
true when an external clock drives CLK.
Definition tmc5160_types.hpp:327
uint32_t hz
The clock frequency: 12 MHz internal, 4 to 18 MHz external.
Definition tmc5160_types.hpp:328
Everything EnsureInitialized() writes to the chip. See docs/configuration.md.
Definition tmc5160_types.hpp:536
Protection protection
Short protection.
Definition tmc5160_types.hpp:553
bool begin_enabled
true: EnsureInitialized() leaves the driver enabled.
Definition tmc5160_types.hpp:565
uint32_t max_velocity_reg
Declared VMAX ceiling; 0 = only the register limit.
Definition tmc5160_types.hpp:541
Encoder encoder
Encoder.
Definition tmc5160_types.hpp:558
Diag diag
DIAG outputs.
Definition tmc5160_types.hpp:555
UartOptions uart
UART options.
Definition tmc5160_types.hpp:561
RampProfile ramp
Ramp registers.
Definition tmc5160_types.hpp:556
Board board
Declared power stage; required.
Definition tmc5160_types.hpp:540
Motor motor
Declared motor; required.
Definition tmc5160_types.hpp:539
MicrostepTable table
The microstep table.
Definition tmc5160_types.hpp:560
StealthChop stealth
StealthChop.
Definition tmc5160_types.hpp:547
AckSet ack
Acknowledgements.
Definition tmc5160_types.hpp:564
Clock clock
The clock source.
Definition tmc5160_types.hpp:538
Thresholds thresholds
Velocity thresholds.
Definition tmc5160_types.hpp:548
Flags flags
General flags.
Definition tmc5160_types.hpp:554
bool include_tolerance
Reduce the current ceiling by the 5 percent full-scale tolerance.
Definition tmc5160_types.hpp:542
Chopper chopper
Chopper.
Definition tmc5160_types.hpp:546
DcStep dcstep
DcStep.
Definition tmc5160_types.hpp:551
SpiOptions spi
SPI options.
Definition tmc5160_types.hpp:562
Switches switches
Reference switches.
Definition tmc5160_types.hpp:557
Mode mode
SD_MODE strap the board uses.
Definition tmc5160_types.hpp:537
Bridge bridge
Gate drivers.
Definition tmc5160_types.hpp:552
StallGuard stall
StallGuard2.
Definition tmc5160_types.hpp:550
Current current
Run and hold current.
Definition tmc5160_types.hpp:544
CoolStep cool
CoolStep.
Definition tmc5160_types.hpp:549
Standstill standstill
Standstill behaviour.
Definition tmc5160_types.hpp:545
Verify verify
Verification the start-up uses on its writes.
Definition tmc5160_types.hpp:563
bool custom_table
true: write table (needs MSCNT = 0).
Definition tmc5160_types.hpp:559
CoolStep (COOLCONF, section 13, page 87).
Definition tmc5160_types.hpp:384
uint8_t semax
Upper threshold offset 0 to 15.
Definition tmc5160_types.hpp:386
uint8_t sedn
Decrement rate 0 to 3: 32, 8, 2, 1 readings.
Definition tmc5160_types.hpp:388
uint8_t seup
Increment 0 to 3: 1, 2, 4, 8.
Definition tmc5160_types.hpp:387
bool min_quarter
Minimum current a quarter instead of a half of IRUN.
Definition tmc5160_types.hpp:389
uint8_t semin
Lower threshold 0 to 15; 0 turns CoolStep off.
Definition tmc5160_types.hpp:385
Run and hold current requests, RMS in mA; PlanCurrent() turns them into registers.
Definition tmc5160_types.hpp:332
uint32_t hold_ma
Standstill current in mA; at most run_ma unless Ack::HoldAboveRun.
Definition tmc5160_types.hpp:334
uint32_t run_ma
Run current in mA, 1 or more.
Definition tmc5160_types.hpp:333
DcStep (VDCMIN, DCCTRL, section 17, page 100).
Definition tmc5160_types.hpp:399
uint32_t vdcmin
VDCMIN, 0 (off) or 256 to 2^23 - 1; bits 7..0 are ignored.
Definition tmc5160_types.hpp:400
uint8_t dc_sg
DC_SG 0 to 255, 16 clocks per count; 0 is off.
Definition tmc5160_types.hpp:402
uint16_t dc_time
DC_TIME 0 to 1023 clocks.
Definition tmc5160_types.hpp:401
DIAG0 and DIAG1 outputs (GCONF bits 5 to 13, section 6.1 page 32).
Definition tmc5160_types.hpp:436
bool diag1_index
DIAG1 on microstep index 0 (StepDir mode).
Definition tmc5160_types.hpp:443
bool diag0_stall
DIAG0 on stall (StepDir mode).
Definition tmc5160_types.hpp:439
bool diag0_otpw
DIAG0 on pre-warning (StepDir mode).
Definition tmc5160_types.hpp:438
bool diag1_push_pull
DIAG1 push-pull instead of open drain.
Definition tmc5160_types.hpp:447
bool diag0_push_pull
DIAG0 push-pull instead of open drain.
Definition tmc5160_types.hpp:446
bool diag1_stall
DIAG1 on stall (StepDir mode).
Definition tmc5160_types.hpp:441
bool diag1_dir_output
DIAG1 outputs the internal DIR (MotionController mode).
Definition tmc5160_types.hpp:442
bool diag0_error
DIAG0 on driver error (StepDir mode).
Definition tmc5160_types.hpp:437
bool diag1_steps_skipped
DIAG1 toggles on skipped DcStep steps; exclusive.
Definition tmc5160_types.hpp:445
bool diag0_step_output
DIAG0 outputs the internal STEP (MotionController mode).
Definition tmc5160_types.hpp:440
bool diag1_on_state
DIAG1 on chopper on-state (StepDir mode).
Definition tmc5160_types.hpp:444
The ENC_CONST accumulation factor (page 45, 107).
Definition tmc5160_types.hpp:495
bool negative
Decimal only: the constant is negative (page 107 rule).
Definition tmc5160_types.hpp:499
bool decimal
ENCMODE.enc_sel_decimal: fraction in 1/10000 instead of 1/65536.
Definition tmc5160_types.hpp:496
uint16_t fraction
Fractional part: 1/65536 (binary) or 1/10000 (decimal, below 10000).
Definition tmc5160_types.hpp:498
int16_t integer
Integer part, signed.
Definition tmc5160_types.hpp:497
Incremental encoder (ABN), written only when declared.
Definition tmc5160_types.hpp:503
EncoderScale scale
Accumulation factor.
Definition tmc5160_types.hpp:506
uint32_t deviation
ENC_DEVIATION in steps, 0 = off, up to 2^20 - 1.
Definition tmc5160_types.hpp:507
bool declared
true: ENCMODE, ENC_CONST and ENC_DEVIATION are written.
Definition tmc5160_types.hpp:504
NEvent n_event
N-channel conditions.
Definition tmc5160_types.hpp:505
General flags (GCONF).
Definition tmc5160_types.hpp:428
bool multistep_filt
GCONF.multistep_filt (page 33; reset text says 1, A12).
Definition tmc5160_types.hpp:431
bool stop_input
GCONF.stop_enable: ENCA_DCIN stops the sequencer.
Definition tmc5160_types.hpp:432
bool recalibrate
GCONF.recalibrate.
Definition tmc5160_types.hpp:430
bool invert_direction
GCONF.shaft.
Definition tmc5160_types.hpp:429
A microstep table (MSLUT, MSLUTSEL, MSLUTSTART, section 6.5 page 47).
Definition tmc5160_types.hpp:511
uint8_t x2
Segment 2 start.
Definition tmc5160_types.hpp:515
std::array< uint32_t, 8 > words
MSLUT[0] to MSLUT[7].
Definition tmc5160_types.hpp:512
uint8_t x1
Segment 1 start.
Definition tmc5160_types.hpp:514
uint8_t start_sin90
Table value at entry 256.
Definition tmc5160_types.hpp:518
uint8_t start_sin
Table value at entry 0.
Definition tmc5160_types.hpp:517
std::array< uint8_t, 4 > w
Segment width codes W0 to W3, 0 to 3.
Definition tmc5160_types.hpp:513
uint8_t x3
Segment 3 start.
Definition tmc5160_types.hpp:516
The motor as its data sheet declares it; the current ceiling comes from here.
Definition tmc5160_types.hpp:310
uint32_t rated_rms_ma
Rated coil current, RMS, in mA. Required, 1 or more.
Definition tmc5160_types.hpp:311
uint32_t fullsteps_per_rev
Full steps per revolution, 1 or more.
Definition tmc5160_types.hpp:315
uint32_t coil_resistance_mohm
Coil resistance in mOhm; 0 = unknown (StealthChop lower limit).
Definition tmc5160_types.hpp:313
uint32_t max_rms_ma
Hard ceiling in mA; 0 means rated_rms_ma.
Definition tmc5160_types.hpp:312
Encoder N-channel event conditions (ENCMODE, page 46).
Definition tmc5160_types.hpp:481
bool pos_edge
Sensitive to the active-going edge.
Definition tmc5160_types.hpp:488
bool once
Latch or clear on the next N event only (clr_once).
Definition tmc5160_types.hpp:487
bool repeat
Latch or clear on every N event (clr_cont).
Definition tmc5160_types.hpp:486
bool pol_n
Active polarity of N.
Definition tmc5160_types.hpp:484
bool latch_xactual
Latch XACTUAL at the event.
Definition tmc5160_types.hpp:491
bool neg_edge
Sensitive to the inactive-going edge.
Definition tmc5160_types.hpp:489
bool pol_a
Required A polarity.
Definition tmc5160_types.hpp:482
bool clear_counter
Clear X_ENC at the event.
Definition tmc5160_types.hpp:490
bool ignore_ab
Ignore A and B.
Definition tmc5160_types.hpp:485
bool pol_b
Required B polarity.
Definition tmc5160_types.hpp:483
Short protection (SHORT_CONF, section 11.2 page 78); written only when declared.
Definition tmc5160_types.hpp:417
bool declared
true: the application declares these values; false: not written.
Definition tmc5160_types.hpp:418
bool disable_s2g
diss2g; needs Ack::ShortProtectionOff.
Definition tmc5160_types.hpp:423
uint8_t s2vs_level
S2VS_LEVEL 4 to 15.
Definition tmc5160_types.hpp:419
bool disable_s2vs
diss2vs; needs Ack::ShortProtectionOff.
Definition tmc5160_types.hpp:424
bool long_delay
shortdelay: 1500 ns instead of 750 ns.
Definition tmc5160_types.hpp:422
uint8_t filter
SHORTFILTER 0 to 3.
Definition tmc5160_types.hpp:421
uint8_t s2g_level
S2G_LEVEL 2 to 15 (12 to 15 above 52 V).
Definition tmc5160_types.hpp:420
The six-point ramp (page 40). Defaults are the datasheet's quick-start values (page 116).
Definition tmc5160_types.hpp:452
uint32_t amax
AMAX 0 to 65535.
Definition tmc5160_types.hpp:456
uint32_t v1
V1 0 to 2^20 - 1; 0 disables A1 and D1.
Definition tmc5160_types.hpp:455
uint32_t d1
D1 1 to 65535.
Definition tmc5160_types.hpp:459
uint32_t zero_wait
TZEROWAIT 0 to 65535, 512 clocks per count.
Definition tmc5160_types.hpp:461
uint32_t vstart
VSTART 0 to 2^18 - 1.
Definition tmc5160_types.hpp:453
uint32_t dmax
DMAX 0 to 65535.
Definition tmc5160_types.hpp:458
uint32_t vstop
VSTOP 1 to 2^18 - 1.
Definition tmc5160_types.hpp:460
uint32_t a1
A1 0 to 65535.
Definition tmc5160_types.hpp:454
uint32_t vmax
VMAX 0 to 2^23 - 512.
Definition tmc5160_types.hpp:457
static constexpr Result Failure(Error e) noexcept
A failed result.
Definition tmc5160_types.hpp:165
constexpr bool ok() const noexcept
Definition tmc5160_types.hpp:148
static constexpr Result Success() noexcept
Definition tmc5160_types.hpp:157
The value of a call, or why there is none.
Definition tmc5160_types.hpp:111
constexpr bool ok() const noexcept
Definition tmc5160_types.hpp:116
static constexpr Result Failure(Error e) noexcept
A failed result.
Definition tmc5160_types.hpp:137
static constexpr Result Success(T v) noexcept
A successful result.
Definition tmc5160_types.hpp:129
The SPI options (used by an SPI port only).
Definition tmc5160_types.hpp:530
uint8_t chain_index
This chip: 0 is the one whose SDI is on MOSI.
Definition tmc5160_types.hpp:532
uint8_t chain_length
Chips in the daisy chain, 1 to kMaxChain.
Definition tmc5160_types.hpp:531
StallGuard2 (COOLCONF, section 13, page 87).
Definition tmc5160_types.hpp:393
bool filter
SFILT.
Definition tmc5160_types.hpp:395
int8_t threshold
SGT, -64 to 63.
Definition tmc5160_types.hpp:394
Standstill behaviour (IHOLD_IRUN.IHOLDDELAY, TPOWERDOWN, GCONF.faststandstill).
Definition tmc5160_types.hpp:338
uint8_t hold_delay
IHOLDDELAY 0 to 15, in 2^18 clocks per step of the current ramp.
Definition tmc5160_types.hpp:339
uint8_t power_down_delay
TPOWERDOWN 0 to 255, in 2^18 clocks; reset value 10.
Definition tmc5160_types.hpp:340
bool fast_standstill
true: standstill after 2^18 clocks instead of 2^20.
Definition tmc5160_types.hpp:341
StealthChop settings (PWMCONF, GCONF.en_pwm_mode, section 6.5.2 page 54).
Definition tmc5160_types.hpp:363
uint8_t reg
PWM_REG 1 to 15 (reset 4).
Definition tmc5160_types.hpp:368
bool autograd
Automatic gradient adaptation (reset 1).
Definition tmc5160_types.hpp:366
uint8_t grad
PWM_GRAD 0 to 255.
Definition tmc5160_types.hpp:371
Freewheel freewheel
Standstill mode with IHOLD = 0.
Definition tmc5160_types.hpp:372
bool autoscale
Automatic current control (reset 1).
Definition tmc5160_types.hpp:365
bool enable
GCONF.en_pwm_mode.
Definition tmc5160_types.hpp:364
uint8_t lim
PWM_LIM 0 to 15 (reset 12).
Definition tmc5160_types.hpp:369
uint8_t freq
PWM frequency 0 to 3: 2/1024, 2/683, 2/512, 2/410 of fCLK.
Definition tmc5160_types.hpp:367
uint8_t ofs
PWM_OFS 0 to 255 (reset 30).
Definition tmc5160_types.hpp:370
Reference switches and stop behaviour (SW_MODE, page 43).
Definition tmc5160_types.hpp:465
bool soft_stop
Stop with the deceleration ramp.
Definition tmc5160_types.hpp:477
bool stop_right
Stop at an active right switch.
Definition tmc5160_types.hpp:467
bool latch_left_inactive
Latch XACTUAL on the inactive edge of left.
Definition tmc5160_types.hpp:472
bool stop_left
Stop at an active left switch.
Definition tmc5160_types.hpp:466
bool stop_on_stall
Stop on stall (sg_stop).
Definition tmc5160_types.hpp:476
bool right_active_low
The right switch is active low.
Definition tmc5160_types.hpp:469
bool swap
Swap left and right.
Definition tmc5160_types.hpp:470
bool latch_left_active
Latch XACTUAL on the active edge of left.
Definition tmc5160_types.hpp:471
bool latch_right_inactive
Latch XACTUAL on the inactive edge of right.
Definition tmc5160_types.hpp:474
bool left_active_low
The left switch is active low.
Definition tmc5160_types.hpp:468
bool latch_encoder
Latch X_ENC as well.
Definition tmc5160_types.hpp:475
bool latch_right_active
Latch XACTUAL on the active edge of right.
Definition tmc5160_types.hpp:473
Velocity thresholds as TSTEP values (page 39); 0 disables.
Definition tmc5160_types.hpp:376
uint32_t pwm
TPWMTHRS: StealthChop below this velocity, 0 to 2^20 - 1.
Definition tmc5160_types.hpp:377
uint32_t cool
TCOOLTHRS: CoolStep and stall output above this velocity.
Definition tmc5160_types.hpp:378
bool small_hysteresis
GCONF.small_hysteresis: 1/32 instead of 1/16.
Definition tmc5160_types.hpp:380
uint32_t high
THIGH: the high-velocity chopper mode above this velocity.
Definition tmc5160_types.hpp:379
The UART options (used by a UART port only).
Definition tmc5160_types.hpp:522
bool multi_node
Several nodes share the line.
Definition tmc5160_types.hpp:525
uint8_t node
NODEADDR 0 to 253 (254 only inside the addressing procedure).
Definition tmc5160_types.hpp:523
uint32_t baud
Baud rate the port runs; 9000 to fCLK / 16.
Definition tmc5160_types.hpp:526
uint8_t send_delay
SENDDELAY 0 to 15, in 8-bit times; at least 2 with multi_node.
Definition tmc5160_types.hpp:524
Compile-time helpers of the TMC5160 driver: typed integer literals, exact hook matching,...
TMC5160 register map: addresses, access rules, reserved bits and reset values.