Skip to content

TMC2240 Stepper Driver fix and tune #27901

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: bugfix-2.1.x
Choose a base branch
from

Conversation

thinkyhead
Copy link
Member

TMC2240 is brand new and there are still a lot of details to sort out with initialization, etc. This PR is for testing and feedback of TMC2240 performance until we can achieve optimum settings.

@thinkyhead thinkyhead requested a review from thisiskeithb May 31, 2025 20:57
@thinkyhead thinkyhead force-pushed the bf2_tmc2240_ongoing_PR branch 2 times, most recently from 5abd0b7 to 0e7d5e3 Compare May 31, 2025 22:01
@thinkyhead thinkyhead force-pushed the bf2_tmc2240_ongoing_PR branch from 0e7d5e3 to 70ca79b Compare May 31, 2025 23:18
@thisiskeithb thisiskeithb linked an issue May 31, 2025 that may be closed by this pull request
1 task
@thisiskeithb
Copy link
Member

Can we host TMC2240_Datasheet.pdf in the docs repo / link to it instead of hosting it within this one?

I'll get these changes pulled down and try out sensorless homing on the BX again. I've also pinged gonzo in the #trinamic channel on Discord to see if reboots are fixed for them.

@thisiskeithb
Copy link
Member

thisiskeithb commented May 31, 2025

Tested with thinkyhead@70ca79b

I cleared the /.pio/build/libdeps/ folder to ensure a fresh copy of TMCStepper 0.8.5 (MarlinFirmware/TMCStepper@0101c8f) was pulled down and sensorless homing still fails like I describe in #27885

M122 output after booting (Z1 & Z2 are active):

Send: M122
		X	Y	Z	Z2	E
Enabled		false	false	true	true	false
Set current	1050	1150	1050	1050	800
RMS current	1033	1131	1033	1033	513
MAX current	1457	1595	1457	1457	723
Run current	31/31	31/31	31/31	31/31	20/31
Hold current	15/31	15/31	15/31	15/31	10/31
CS actual
PWM scale
vsense
stealthChop	true	true	true	true	true
msteps		16	16	16	16	16
interp		true	true	true	true	true
tstep		max	max	max	max	max
PWM thresh.
[mm/s]
OT prewarn	false	false	false	false	false
off time	3	3	3	3	4
blank time
hysteresis
 -end		-1	-1	-1	-1	2
 -start		5	5	5	5	1
Stallguard thrs
uStep count	16	16	16	16	16
DRVSTATUS	X	Y	Z	Z2	E
sg_result	0	0	0	0	0
stallguard	*	*	*	*	*
fsactive
stst
olb
ola
s2gb
s2ga
otpw
ot
s2vsa
s2vsb
Driver registers:
		X	0x81:00:40:00
		Y	0x81:00:40:00
		Z	0x81:0F:40:00
		Z2	0x81:0F:40:00
		E	0x81:00:40:00


Testing X connection... OK
Testing Y connection... OK
Testing Z connection... OK
Testing Z2 connection... OK
Testing E connection... OK

M122 report after a G28 and X & Y motors can move freely by hand / drivers are shutdown:

Send: M122
		X	Y	Z	Z2	E
Enabled		true	true	true	true	false
Set current	1050	1150	1050	1050	800
RMS current	1033	1131	1033	1033	513
MAX current	1457	1595	1457	1457	723
Run current	31/31	31/31	31/31	31/31	20/31
Hold current	15/31	15/31	15/31	15/31	10/31
CS actual
PWM scale
vsense
stealthChop	true	true	true	true	true
msteps		16	16	16	16	16
interp		true	true	true	true	true
tstep		max	max	max	max	max
PWM thresh.
[mm/s]
OT prewarn	false	false	false	false	false
off time	3	3	3	3	4
blank time
hysteresis
 -end		-1	-1	-1	-1	2
 -start		5	5	5	5	1
Stallguard thrs
uStep count	16	16	16	16	16
DRVSTATUS	X	Y	Z	Z2	E
sg_result	18	16	42	48	0
stallguard					*
fsactive
stst
olb
ola
s2gb
s2ga
otpw
ot
s2vsa		*	*
s2vsb
Driver registers:
		X	0x80:0F:50:12
		Y	0x80:0F:50:10
		Z	0x80:1F:40:2A
		Z2	0x80:1F:40:30
		E	0x81:00:40:00


Testing X connection... OK
Testing Y connection... OK
Testing Z connection... OK
Testing Z2 connection... OK
Testing E connection... OK

@thinkyhead
Copy link
Member Author

Can we host TMC2240_Datasheet.pdf in the docs repo / link to it instead of hosting it within this one?

I don't have any plan to commit it. It is here in this PR for reference.

@thinkyhead
Copy link
Member Author

This PR is for experimentation, so please try various adjustment to the TMC2240 initialization values to see if you can get to a point where it doesn't shut down when attempting to move a stepper by any distance at boot (e.g., G1 X1). We don't need to go crazy and jump straight to G28 until we have this much working. Some non-authoritative sample init values are included in a comment in the init method, but refer to the datasheet to find out what the expected effect should be prior to making any adjustments.


The s2vsa (Short to VS A) flag on the Trinamic TMC2240 indicates a short circuit from one of the motor outputs to the supply voltage (VS), specifically on channel A. This typically suggests:

  • A direct or partial short from the motor coil pin (OA) to the motor power supply (VS).
  • A serious hardware fault, not just a mild misconfiguration.

It's not typically triggered by bad settings, but there are edge cases where driver settings might lead to false positives or exacerbate electrical conditions that trigger this:

  1. Too-high current limits (configured via IHOLD/IRUN) can cause sharp current spikes that stress the MOSFETs or wires.
  2. Improper sense resistor values or incorrect VSENSE configuration could cause the chip to misinterpret normal current levels.
  3. Fast decay or improper chopper timing (CHOPCONF) might cause ringing or voltage spikes on the outputs.
  4. Floating motor outputs (no motor connected or loose wires) can create unstable conditions that confuse the detection logic.

More likely causes (hardware/wiring):

  • Miswiring, e.g. one coil wire shorted to the motor supply line.
  • Motor failure or internal shorts in the windings.
  • PCB or solder bridge between OA and VS lines.
  • Induced spikes or noise from poor PCB layout or inadequate decoupling.

What to check:

  1. Inspect the wiring and PCB for shorts between motor outputs and VS.
  2. Try a different motor or disconnect the motor entirely.
  3. Lower the current limits to the minimum and see if the flag still sets.
  4. Use oscilloscope to look at the output waveform if available.
  5. Double-check all driver configuration registers, especially current settings and chopper mode.

@thisiskeithb
Copy link
Member

see if you can get to a point where it doesn't shut down when attempting to move a stepper by any distance at boot

I can command motors to move after a fresh boot without issue.

The problem is that as soon as I try to home, X & Y drivers shut down.

@thisiskeithb thisiskeithb removed their request for review June 1, 2025 01:30
@MarathonMAN1
Copy link

MarathonMAN1 commented Jun 1, 2025

I can confirm reboot from leveling bed to event after G29 has been fixed. Drivers also act normal and run like a scalded dog. Name change another long story.

@thisiskeithb
Copy link
Member

We should remove the TMC2240_STANDALONE option since these drivers can only be configured via SPI. There's no potentiometer to set current for a "standalone" mode.

I also verified that TMC2240_STANDALONE does not work on real hardware.

@thisiskeithb
Copy link
Member

The TMC2240 reference should be dropped from the first column since sensitivity values are -64 to 63 based on testing & the datasheet (h/t to @ellensp for confirming):

* Sensitivity TMC2209/2240 Others
* HIGHEST 255 -64 (Too sensitive => False positive)
* LOWEST 0 63 (Too insensitive => No trigger)

I moved a couple TMC2240s from my BX to my MK3S Bear and I'm experiencing the same issue with sensorless homing except motors stay engaged and drivers don't shutdown. They just trigger instantly on any value from -64 all the way to 63. Motors can be commanded to move after a homing attempt, but sometimes they buzz/don't move correctly. If I switch back to 5160s on my Bear, everything works as expected.

@ellensp
Copy link
Contributor

ellensp commented Jun 1, 2025

Issue is not so simple.

* Sensitivity TMC2209/2240 Others
* HIGHEST 255 -64 (Too sensitive => False positive)
* LOWEST 0 63 (Too insensitive => No trigger)

tmc2240 has stallguard2 which is -64 to 64
but it also has stallguard4 which is 0 to 255

current code does update sgt, which is stallguard2

@thinkyhead thinkyhead force-pushed the bf2_tmc2240_ongoing_PR branch 2 times, most recently from e5e6067 to f5c29c1 Compare June 1, 2025 21:41
@thinkyhead
Copy link
Member Author

We'll get Stallguard2 sorted out before we get into Stallguard4, which has some extra complexities but also some extra goodies that could be useful in the future.

@thinkyhead
Copy link
Member Author

thinkyhead commented Jun 1, 2025

I moved a couple TMC2240s from my BX to my MK3S Bear and I'm experiencing the same issue with sensorless homing except motors stay engaged and drivers don't shutdown.

Does M119 show "TRIGGERED" all the time? If so, the ENDSTOP_HIT_STATE will need to be updated to require LOW instead of HIGH. This will match all other drivers except TMC2209.

Note: The opposite DIAG state required for TMC2209 might not apply to TMC2209 modules from every vendor, but currently it seems to be the standard.

@thisiskeithb
Copy link
Member

Does M119 show "TRIGGERED" all the time?

Indeed.

Send: M119
Reporting endstop status
x_min: TRIGGERED
y_min: TRIGGERED
z_min: open
filament: TRIGGERED

@thinkyhead thinkyhead force-pushed the bf2_tmc2240_ongoing_PR branch from f5c29c1 to 112b79e Compare June 1, 2025 22:19
@thinkyhead
Copy link
Member Author

So we have some suggestions provided by ChatGPT. I will boil them down. Let me know how the driver behaves after experimenting with some of these suggestions.

  1. Current range and configuration
  • You’re setting DRV_CONF.current_range, but the TMC2240 supports wider current scaling than the 2208, and an incorrect range may cause overshoot or undervaluation of RMS current.
  • 👉 Double-check that your TMC2240_CURRENT_RANGE is appropriate for the selected sense resistor and the current setting (st.rms_current(mA)).
  1. Slope control
  • DRV_CONF.slope_control is being set, but TMC2240 has aggressive slope tuning options. Too sharp edges can cause ringing, which might be seen as a short.
  • 👉 Try the default (0) slope control setting to test stability, or explicitly use more gentle rise/fall settings.
  1. TOFF and blanking time (TBL)
  • You’ve set toff = 3, tbl = 0b10 (36 tCLK). Compare that to the 2208 where tbl = 0b01 (24 tCLK).
  • TOFF 3 is reasonable, but combined with longer blanking (tbl = 0b10) it can delay off-time enough to create undesired current peaks, especially on a fast step edge.
  • 👉 Try tbl = 0b01 and/or toff = 4 to see if it improves things.
  1. PWMCONF parameters
  • You’re using: pwmconf.pwm_ofs = 30; pwmconf.pwm_reg = 4; compared to the TMC2208 pwmconf.pwm_ofs = 36; pwmconf.pwm_reg = 8;. This means your TMC2240 has a lower PWM offset and regulation strength, which can affect chop stability at low speeds or with light load. If the PWM waveform sags, it might cause an s2vsa detection when driving certain motors.
  • 👉 Try matching pwm_ofs and pwm_reg closer to the 2208 values (e.g. ofs = 36, reg = 8) and monitor for changes.
  1. Other diagnostic suggestions
  • Try SpreadCycle mode: Temporarily disable StealthChop to rule out bad PWM behavior at low speed.

What about the settings borrowed from Makerbase, you may ask? So I did.

pwmconf.pwm_ofs            = 29;
pwmconf.pwm_grad           = 30;
pwmconf.pwm_freq           = 0b00;
pwmconf.pwm_autoscale      = true;
pwmconf.pwm_autograd       = true;
pwmconf.freewheel          = 0;
pwmconf.pwm_meas_sd_enable = false;
pwmconf.pwm_dis_reg_stst   = false;
pwmconf.pwm_reg            = 4;
pwmconf.pwm_lim            = 12;
Field Your previous New version Notes
pwm_ofs 30 29 Very close. 28–36 is a good range. Lower values save power but reduce headroom.
pwm_grad (missing) 30 Important. Higher grad gives faster response to load. 30 is good.
pwm_reg 4 4 OK. Often 4–8. Affects regulation response strength.
pwm_lim 12 12 Standard.
pwm_freq (missing) 0b00 1/1024 fCLK (≈39kHz @ 40MHz). Default and safe.
pwm_autoscale true true Good. Enables adaptive PWM for quietness.
pwm_autograd true true Good. Works with autoscale.
freewheel (not specified) 0 Normal operation.
pwm_meas_sd_enable (not specified) false OK unless you’re measuring sense resistor voltage.
pwm_dis_reg_stst (not specified) false Good. Allows regulation as needed.

👍 Why this is better:

  • pwm_grad = 30 and pwm_ofs = 29 strike a good balance between quietness and responsiveness.
  • pwm_freq = 0b00 gives a low-frequency PWM suitable for 24V operation (avoids high-frequency losses).
  • autoscale + autograd ensure the driver adjusts gracefully to load changes.

These values match what Trinamic shows in application notes for StealthChop-enabled axes (like X, Y, or Z) in 3D printing or CNC.

🛠 Suggestion

Try using these PWM settings in your TMC2240 init, especially in combination with:

  • stealthChop mode on
  • CHOPCONF.toff = 4
    TBL = 0b01
    
  • Slightly more conservative DRV_CONF.slope_control

Also, log or monitor the DRV_STATUS and GSTAT periodically during testing to catch any other driver-side warnings before failure.

@dbuezas
Copy link
Contributor

dbuezas commented Jun 2, 2025

Diag0 defaults to open drain, does the endstop pin have a pullup, and is the endstop configured as active low?

Otherwise set the diag0_pushpull bit of the gconf register to 1 which also makes it active high.

@thinkyhead thinkyhead force-pushed the bugfix-2.1.x branch 3 times, most recently from 3791e7d to 6ea4a16 Compare June 2, 2025 21:51
@thinkyhead thinkyhead force-pushed the bf2_tmc2240_ongoing_PR branch from 112b79e to 1f2e1f7 Compare June 2, 2025 22:37
@thinkyhead thinkyhead force-pushed the bf2_tmc2240_ongoing_PR branch 2 times, most recently from f73d12f to d81c359 Compare June 3, 2025 00:08
@thinkyhead
Copy link
Member Author

The TMC2240 drivers I ordered will arrive tomorrow and then I can put them through various tests to find the best defaults for PWM, stealthChop, etc. I will also compare our settings to those of other softwares that support this model of stepper driver. And perhaps I can even get advice from a Trinamic engineer. I hear they are very friendly.

@thinkyhead thinkyhead force-pushed the bf2_tmc2240_ongoing_PR branch 2 times, most recently from b863484 to 4db4730 Compare June 8, 2025 21:55
@thinkyhead thinkyhead force-pushed the bf2_tmc2240_ongoing_PR branch 3 times, most recently from 6acbd11 to ec8497b Compare June 8, 2025 22:43
@thinkyhead thinkyhead force-pushed the bf2_tmc2240_ongoing_PR branch from ec8497b to 4cb7674 Compare June 9, 2025 03:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Sensorless homing does not work with 2240s & potential reboots
5 participants