Skip to content
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

MINIMUM_STEPPER_PULSE to MINIMUM_STEPPER_PULSE_NS #27113

Merged

Conversation

mh-dm
Copy link
Contributor

@mh-dm mh-dm commented May 20, 2024

Description

Switch from MINIMUM_STEPPER_PULSE (in µs) to MINIMUM_STEPPER_PULSE_NS (in nanoseconds). This PR is basically just code refactoring. Note that MINIMUM_STEPPER_PULSE is in Configuration_adv.h and if left undefined it is automatically configured by Conditionals_adv.h based on the used stepper drivers.

Benefits

Cleaner code for handling stepper motor drivers that can do sub microsecond step pulse widths (like LV8729 and TMC2xxx). Allows easier user experimentation with sub microsecond step pulse widths.

@thisiskeithb
Copy link
Member

Since a config option has changed / been renamed, you’ll need to add it to Changes.h.

@mh-dm
Copy link
Contributor Author

mh-dm commented May 20, 2024

Thanks, done.

Marlin/src/module/stepper.cpp Outdated Show resolved Hide resolved
@thinkyhead
Copy link
Member

Some of these things defined as hal_timer_t should probably be changed to uint32_t. Both AVR and HC32 use 16-bit timer width, so it's very likely to overflow. If we're curious, we could add tests first for to see if any hal_timer_t value is currently overflowing HAL_TIMER_TYPE_MAX and add a build test with plausible high step time values to see if that leads to overflow.

@@ -36,13 +36,13 @@
typedef uint32_t hal_timer_t;
#define HAL_TIMER_TYPE_MAX 0xFFFFFFFE

#define GPT_TIMER_RATE F_BUS_ACTUAL // 150MHz
#define GPT_TIMER_RATE F_BUS // 150MHz (Can't use F_BUS_ACTUAL because it's extern volatile)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this is only 1/4 of the full F_CPU speed, but it's unclear whether we can run interrupt timers at such a high resolution as 1/1, or that we need to. All counts would require two extra bits.

Since F_BUS_ACTUAL is volatile, perhaps the system clock speed is able to change dynamically, or maybe this is only done under program control. If Marlin is able to keep the clock at a constant rate then we should be ok just using F_BUS, which is provided by the Teensy platform. I'm not sure yet how F_CPU and F_BUS are defined, since you can change the CPU speed with PlatformIO option board_build.f_cpu. If Teensy isn't picking up board_build.f_cpu then we may add our own -DF_CPU or some other define that Teensy platform is expecting.

@mh-dm
Copy link
Contributor Author

mh-dm commented May 23, 2024

Some of these things defined as hal_timer_t should probably be changed to uint32_t. Both AVR and HC32 use 16-bit timer width, so it's very likely to overflow. If we're curious, we could add tests first for to see if any hal_timer_t value is currently overflowing HAL_TIMER_TYPE_MAX and add a build test with plausible high step time values to see if that leads to overflow.

hal_timer_t is for timer ticks which is usually cycles/4. And given that most things changed look like cycles they should indeed be uint32_t instead.

If there is indeed some math / comparison being done between timer ticks/hal_timer_t and cycles/uint32_t without a clear conversion being done then that's revealing of a bug. If my PR didn't introduce it I recommend it be addressed in a separate PR since this one is already getting too big for my comfort :).

Marlin/src/module/stepper.cpp Outdated Show resolved Hide resolved
Marlin/src/module/stepper/cycles.h Outdated Show resolved Hide resolved
Marlin/src/module/stepper/cycles.h Outdated Show resolved Hide resolved
Marlin/src/module/stepper.cpp Outdated Show resolved Hide resolved
thinkyhead added a commit to MarlinFirmware/Configurations that referenced this pull request May 30, 2024
MarlinFirmware/Marlin#27113

Co-Authored-By: Mihai <299015+mh-dm@users.noreply.github.com>
Copy link
Contributor Author

@mh-dm mh-dm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did a pass over everything at 92fe629 commit and it looks good.

Marlin/src/core/types.h Outdated Show resolved Hide resolved
@thinkyhead
Copy link
Member

I did a pass over everything…

Thanks for the review. If more variables, methods, and parameters should be working with uint32_t (especially under known circumstances) we can certainly apply that. For AVR we generally want to use the smallest types required, so that is specifically where hal_timer_t comes in. It generally makes sense to convert #define values to 32-bit and let the compiler decide how to deal with type conversion. Any of the constexpr uint32_t defined with a value under 65536 could be changed to uint16_t and that may be better for some code on AVR, or it may make no difference. As we are further fixing up and optimizing the motion code with these PRs I won't sweat that small detail until some profiling shows that it matters.

@thinkyhead thinkyhead merged commit 65c19f8 into MarlinFirmware:bugfix-2.1.x Jun 6, 2024
62 checks passed
sbwilson pushed a commit to sbwilson/Marlin-Ender2-Gantry that referenced this pull request Jun 11, 2024
… into bugfix-2.1.x

* 'bugfix-2.1.x' of https://github.com/MarlinFirmware/Marlin:
  ✏️ Fix comma typo (MarlinFirmware#27138)
  [cron] Bump distribution date (2024-06-08)
  📝 Remove dead video links
  [cron] Bump distribution date (2024-06-06)
  🔧 Minimum Stepper Pulse in Nanoseconds (MarlinFirmware#27113)
thinkyhead pushed a commit that referenced this pull request Jun 14, 2024
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.

None yet

3 participants