3 #include "../config/config.h"
4 #include "../hal/gpio.h"
5 #include "../hal/shr16.h"
6 #include "../hal/spi.h"
15 enum MotorMode : uint8_t {
38 :
vSense((ir < 32) ? 1 : 0)
39 ,
iRun((ir < 32) ? ir : (ir >> 1))
40 ,
iHold((ir < 32) ? ih : (ih >> 1)) {}
44 uint8_t reset_flag : 1;
49 inline constexpr ErrorFlags()
55 inline bool Good()
const {
return reset_flag == 0 && uv_cp == 0 && s2g == 0 && otpw == 0 && ot == 0; }
119 hal::shr16::shr16.SetTMCDir(params.
idx, dir ^ params.
dirOn);
125 hal::shr16::shr16.SetTMCDir(params.
idx, !dir);
130 gpio::TogglePin(params.
stepPin);
135 gpio::WritePin(params.
stepPin, (state ? gpio::Level::high : gpio::Level::low));
140 return gpio::ReadPin(params.
sgPin) == gpio::Level::low;
143 inline bool Stalled()
const {
144 return sg_filter_counter == 0;
147 inline void ClearStallguard() {
148 sg_filter_counter = sg_filter_threshold;
154 inline ErrorFlags GetErrorFlags()
const {
161 inline void SetErrorFlags(ErrorFlags ef) {
173 void Isr(
const MotorParams ¶ms);
176 void _spi_tx_rx(
const MotorParams ¶ms, uint8_t (&pData)[5]);
177 void _handle_spi_status(
const MotorParams ¶ms, uint8_t status);
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;
TMC2130 interface - instances of this class are hidden in modules::motion::Motion::AxisData.
Definition: tmc2130.h:59
void SetBridgeOutput(const MotorParams ¶ms, bool on)
Disables the output by setting or clearing CHOPCONF's TOFF.
Definition: tmc2130.cpp:204
void SetCurrents(const MotorParams ¶ms, const MotorCurrents ¤ts)
Set the current motor currents.
Definition: tmc2130.cpp:213
uint32_t ReadRegister(const MotorParams ¶ms, 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 ¶ms, const MotorCurrents ¤ts, MotorMode mode)
TMC2130()=default
Constructor.
void WriteRegister(const MotorParams ¶ms, Registers reg, uint32_t data)
Writes a driver register and updates the status flags.
Definition: tmc2130.cpp:318
void SetMode(const MotorParams ¶ms, 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 ¶ms, bool state)
Set step to an explicit state.
Definition: tmc2130.h:134
void SetSGTHRS(const MotorParams ¶ms)
Definition: tmc2130.cpp:263
static void Step(const MotorParams ¶ms)
Step the motor.
Definition: tmc2130.h:129
static bool SampleDiag(const MotorParams ¶ms)
Return SG state.
Definition: tmc2130.h:139
void Isr(const MotorParams ¶ms)
Used for polling the DIAG pin. Should be called from the stepper isr periodically when moving.
Definition: tmc2130.cpp:324
bool CheckForErrors(const MotorParams ¶ms)
Should be called periodically. Maybe not all the time. Once every 10 ms is probably enough.
void SetEnabled(const MotorParams ¶ms, bool enabled)
Enable/Disable the motor.
Definition: tmc2130.cpp:287
static void SetRawDir(const MotorParams ¶ms, bool dir)
Set direction, raw value (i.e. ignore Params).
Definition: tmc2130.h:123
static void SetDir(const MotorParams ¶ms, bool dir)
Set direction.
Definition: tmc2130.h:118
MRes
Available microstepping resolutions.
Definition: axis.h:11
bool __attribute__((noinline)) TMC2130
Definition: tmc2130.cpp:86
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
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