Prusa-MMU-Private
PrusaMultiMaterialUpgradev3firmwareforMK3SMK4
tmc2130.h
Go to the documentation of this file.
1 #pragma once
3 #include "../config/config.h"
4 #include "../hal/gpio.h"
5 #include "../hal/shr16.h"
6 #include "../hal/spi.h"
7 
8 namespace hal {
9 
13 namespace tmc2130 {
14 
15 enum MotorMode : uint8_t {
16  Stealth,
17  Normal
18 };
19 
20 struct MotorParams {
21  const hal::spi::SPI_TypeDef *const spi;
22  uint8_t idx;
23  bool dirOn;
28  int8_t sg_thrs;
29  uint8_t axis;
30 };
31 
32 struct MotorCurrents {
33  bool vSense;
34  uint8_t iRun;
35  uint8_t iHold;
36 
37  constexpr MotorCurrents(uint8_t ir, uint8_t ih)
38  : vSense((ir < 32) ? 1 : 0)
39  , iRun((ir < 32) ? ir : (ir >> 1))
40  , iHold((ir < 32) ? ih : (ih >> 1)) {}
41 };
42 
43 struct __attribute__((packed)) ErrorFlags {
44  uint8_t reset_flag : 1;
45  uint8_t uv_cp : 1;
46  uint8_t s2g : 1;
47  uint8_t otpw : 1;
48  uint8_t ot : 1;
49  inline constexpr ErrorFlags()
50  : reset_flag(0)
51  , uv_cp(0)
52  , s2g(0)
53  , otpw(0)
54  , ot(0) {}
55  inline bool Good() const { return reset_flag == 0 && uv_cp == 0 && s2g == 0 && otpw == 0 && ot == 0; }
56 };
57 
59 class TMC2130 {
60 public:
62  enum class Registers : uint8_t {
64  GCONF = 0x00,
65  GSTAT = 0x01,
66  IOIN = 0x04,
67 
69  IHOLD_IRUN = 0x10,
70  TPOWERDOWN = 0x11,
71  TSTEP = 0x12,
72  TPWMTHRS = 0x13,
73  TCOOLTHRS = 0x14,
74  THIGH = 0x15,
75 
77  MSCNT = 0x6A,
78  CHOPCONF = 0x6C,
79  COOLCONF = 0x6D,
80  DRV_STATUS = 0x6F,
81  PWMCONF = 0x70,
82  };
83 
85  TMC2130() = default;
86 
90  bool Init(const MotorParams &params,
91  const MotorCurrents &currents,
92  MotorMode mode);
93 
95  void SetMode(const MotorParams &params, MotorMode mode);
96 
98  void SetBridgeOutput(const MotorParams &params, bool on);
99 
101  void SetCurrents(const MotorParams &params, const MotorCurrents &currents);
102 
107  void SetSGTHRS(const MotorParams &params);
108 
110  const bool Enabled() const {
111  return enabled;
112  }
113 
115  void SetEnabled(const MotorParams &params, bool enabled);
116 
118  static inline void SetDir(const MotorParams &params, bool dir) {
119  hal::shr16::shr16.SetTMCDir(params.idx, dir ^ params.dirOn);
120  }
121 
123  static inline void SetRawDir(const MotorParams &params, bool dir) {
124  // Also cancels the inversion in SetTMCDir
125  hal::shr16::shr16.SetTMCDir(params.idx, !dir);
126  }
127 
129  static inline void Step(const MotorParams &params) {
130  gpio::TogglePin(params.stepPin); // assumes DEDGE
131  }
132 
134  static inline void SetStep(const MotorParams &params, bool state) {
135  gpio::WritePin(params.stepPin, (state ? gpio::Level::high : gpio::Level::low));
136  }
137 
139  static inline bool SampleDiag(const MotorParams &params) {
140  return gpio::ReadPin(params.sgPin) == gpio::Level::low;
141  }
142 
143  inline bool Stalled() const {
144  return sg_filter_counter == 0;
145  }
146 
147  inline void ClearStallguard() {
148  sg_filter_counter = sg_filter_threshold;
149  }
150 
152  bool CheckForErrors(const MotorParams &params);
153 
154  inline ErrorFlags GetErrorFlags() const {
155  return errorFlags;
156  }
157 
158 #ifdef UNITTEST
161  inline void SetErrorFlags(ErrorFlags ef) {
162  errorFlags = ef;
163  }
164 #endif
165 
167  uint32_t ReadRegister(const MotorParams &params, Registers reg);
168 
170  void WriteRegister(const MotorParams &params, Registers reg, uint32_t data);
171 
173  void Isr(const MotorParams &params);
174 
175 private:
176  void _spi_tx_rx(const MotorParams &params, uint8_t (&pData)[5]);
177  void _handle_spi_status(const MotorParams &params, uint8_t status);
178 
179  ErrorFlags errorFlags;
180  bool initialized = false;
181  bool enabled = false;
182  uint8_t sg_filter_threshold = 0;
183  uint8_t sg_filter_counter = 0;
184 };
185 
186 } // namespace tmc2130
187 } // namespace hal
TMC2130 interface - instances of this class are hidden in modules::motion::Motion::AxisData.
Definition: tmc2130.h:59
void SetBridgeOutput(const MotorParams &params, bool on)
Disables the output by setting or clearing CHOPCONF's TOFF.
Definition: tmc2130.cpp:204
void SetCurrents(const MotorParams &params, const MotorCurrents &currents)
Set the current motor currents.
Definition: tmc2130.cpp:213
uint32_t ReadRegister(const MotorParams &params, Registers reg)
Reads a driver register and updates the status flags.
Definition: tmc2130.cpp:309
const bool Enabled() const
Return enabled state.
Definition: tmc2130.h:110
bool Init(const MotorParams &params, const MotorCurrents &currents, MotorMode mode)
TMC2130()=default
Constructor.
void WriteRegister(const MotorParams &params, Registers reg, uint32_t data)
Writes a driver register and updates the status flags.
Definition: tmc2130.cpp:318
void SetMode(const MotorParams &params, MotorMode mode)
Set the current motor mode.
Definition: tmc2130.cpp:199
Registers
TMC2130 register addresses.
Definition: tmc2130.h:62
@ GCONF
General Configuration Registers.
@ MSCNT
Motor Driver Registers.
@ IHOLD_IRUN
Velocity Dependent Driver Feature Control Register Set.
static void SetStep(const MotorParams &params, bool state)
Set step to an explicit state.
Definition: tmc2130.h:134
void SetSGTHRS(const MotorParams &params)
Definition: tmc2130.cpp:263
static void Step(const MotorParams &params)
Step the motor.
Definition: tmc2130.h:129
static bool SampleDiag(const MotorParams &params)
Return SG state.
Definition: tmc2130.h:139
void Isr(const MotorParams &params)
Used for polling the DIAG pin. Should be called from the stepper isr periodically when moving.
Definition: tmc2130.cpp:324
bool CheckForErrors(const MotorParams &params)
Should be called periodically. Maybe not all the time. Once every 10 ms is probably enough.
void SetEnabled(const MotorParams &params, bool enabled)
Enable/Disable the motor.
Definition: tmc2130.cpp:287
static void SetRawDir(const MotorParams &params, bool dir)
Set direction, raw value (i.e. ignore Params).
Definition: tmc2130.h:123
static void SetDir(const MotorParams &params, bool dir)
Set direction.
Definition: tmc2130.h:118
MRes
Available microstepping resolutions.
Definition: axis.h:11
bool __attribute__((noinline)) TMC2130
Definition: tmc2130.cpp:86
Definition: gpio.h:51
Definition: spi.h:15
Definition: tmc2130.h:32
bool vSense
VSense current scaling.
Definition: tmc2130.h:33
uint8_t iHold
Holding current.
Definition: tmc2130.h:35
uint8_t iRun
Running current.
Definition: tmc2130.h:34
Definition: tmc2130.h:20
bool dirOn
forward direction
Definition: tmc2130.h:23
gpio::GPIO_pin csPin
CS pin.
Definition: tmc2130.h:24
gpio::GPIO_pin sgPin
StallGuard pin.
Definition: tmc2130.h:26
uint8_t idx
SHR16 index.
Definition: tmc2130.h:22
config::MRes mRes
microstep resolution
Definition: tmc2130.h:27
gpio::GPIO_pin stepPin
step pin
Definition: tmc2130.h:25