Skip to content

drivers: stepper: step_dir: Adjust toggeling speed of step pin #88282

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 1 commit into
base: main
Choose a base branch
from

Conversation

faxe1008
Copy link
Collaborator

@faxe1008 faxe1008 commented Apr 8, 2025

Changes the timing source to perform the stepping in half of the given frequency to space out the toggeling of the step pin more. This fixes misstepping for configurations where the speed of successive toggeling exceeds what the stepper driver can handle.

Fixes #87698.

@@ -74,6 +74,7 @@ struct step_dir_stepper_common_data {
int32_t actual_position;
uint64_t microstep_interval_ns;
int32_t step_count;
uint8_t toggle_count;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Use a bool that is flipped instead, same memory and you don't rely on integer overflow to work as you expect.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah had that originally, but I thought the naming of a uint8_t toggle_count is more readable then somthing like step_performed. Changed it. If you have a better name suggestion feel free :^)

Copy link
Collaborator

Choose a reason for hiding this comment

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

For the acceleration step-dir i am currently using step_pin_low.

Changes the timing source to perform the stepping in half of the given
frequency to space out the toggeling of the step pin more. This fixes
misstepping for configurations where the speed of successive toggeling
exceeds what the stepper driver can handle.

Fixes zephyrproject-rtos#87698.

Signed-off-by: Fabian Blatz <[email protected]>
@faxe1008 faxe1008 force-pushed the fix_step_dir_toggle_speed branch from 9eda3d9 to 443fa53 Compare April 8, 2025 08:00
Copy link
Member

@jilaypandya jilaypandya left a comment

Choose a reason for hiding this comment

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

toggling. Typo in commit message?

if (config->dual_edge) {
return K_NSEC(data->microstep_interval_ns);
} else {
return K_NSEC(data->microstep_interval_ns / 2);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
return K_NSEC(data->microstep_interval_ns / 2);
return K_NSEC(data->microstep_interval_ns >> 1);

if (config->dual_edge) {
data->counter_top_cfg.ticks = DIV_ROUND_UP(toggle_freq, NSEC_PER_SEC);
} else {
data->counter_top_cfg.ticks = DIV_ROUND_UP(toggle_freq / 2, NSEC_PER_SEC);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
data->counter_top_cfg.ticks = DIV_ROUND_UP(toggle_freq / 2, NSEC_PER_SEC);
data->counter_top_cfg.ticks = DIV_ROUND_UP(toggle_freq >> 1, NSEC_PER_SEC);

Copy link
Collaborator

Choose a reason for hiding this comment

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

I would go with NSEC_PER_SEC * 2 instead, as it makes easier to understand what is happening.

@faxe1008
Copy link
Collaborator Author

faxe1008 commented Apr 8, 2025

Hmm

west twister -p native_sim/native -s tests/drivers/stepper/drv8424/api/drivers.stepper.drv8424.api

fails, the step count is only half of what it is supposed to be.

@jbehrensnx
Copy link
Collaborator

jbehrensnx commented Apr 8, 2025

Hmm

west twister -p native_sim/native -s tests/drivers/stepper/drv8424/api/drivers.stepper.drv8424.api

fails, the step count is only half of what it is supposed to be.

This issue occurs with all step-dir drivers (look at the stepper_api test suite).

Can you check if it works on real hardware? There are some strict limitations on the native_sim counter that could possibly be the source of the issue.

@jilaypandya
Copy link
Member

jilaypandya commented Apr 18, 2025

@faxe1008, Do you think we should introduce something like min-pulse-length in stepper-controller.yaml, if the user tries to set_step_interval < 2 * min_pulse_length_in_ns, then we throw an errror.

@faxe1008
Copy link
Collaborator Author

@jilaypandya have not gotten around working on this PR yet.

Regarding your question: it does not hurt but I think since there is no convenient way for us to default the value to the one specified in a drivers datasheet this means the user has to set it. If speed really is a concern the user probably calculates the step interval from the speed anyways and he should know best what value to pick. On the other hand this would make hardware-depedant limits move to devicetree, in case the user choses to use a new stepper IC.

Hmm I think it makes sense to have this :^)

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.

drivers:stepper Step-Dir Pin toggle Frequency to High
3 participants