Skip to content

drivers: stepper: introduce step-dir api to facilitate implementing motion controllers as device drivers #91979

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

jilaypandya
Copy link
Member

@jilaypandya jilaypandya commented Jun 21, 2025

This PR aims to facilitate implementation of stepper motion controllers as device drivers. Hence the low-level step-dir drivers now shall implement a step-dir-driver-api, effectively also signifying what the IC actually supports i.e enable, step-dir and in some cases fault detection

  • Stepper API as all of us know it, remains unchanged.
  • Step-Dir API which is just a wrapper as of now for some gpios, does all the low-level work.
Drivers Stepper Step-Dir Driver (step-dir API) Stepper Integrated Motion Control Driver ( stepper API)
adi,tmc22xx x
allegro,a4979 x
ti,drv84xx x
zephyr,gpio-stepper(h-bridge based driver) x
adi,tmc50xx x
adi,tmc51xx x
zephyr,stepper-motion-controller (CPU Based) x

Notes:

This is an intermediate step. Final Goal is having two APIs as follows,

stepper driver api stepper motion control api
enable / disable impl
microstep_resolution impl
step/dir impl
move_to/move_by/run/stop impl
set_reference_position impl
*is_moving impl
get_actual_position impl
set_reference_position impl

Teaser:

  • I am trying locally to implement both stepper-driver-api currently step-dir api and stepper-motion-control-api currently stepper api in tmc50xx using child-bindings.
  • This will be achieved by adding index to stepper_move_to
#define X_AXIS  0
#define Y_AXIS  1

stepper_move_to(tmc50xx_motion_controller, X_AXIS, 200);
stepper_stop(tmc50xx_motion_controller, Y_AXIS);
stepper_disable(tmc50xx_stepper_driver_x_axis@0);
stepper_disanle(tmc50xx_stepper_driver_y_axis@1);

spi {
    
      /* Implements stepper motion control api */
     tmc50xx_motion_controller {
        compatible = "adi,tmc50xx"

        /* Implements stepper driver api */
        tmc50xx_stepper_driver_x_axis@0 {
        };

        /* Implements stepper driver api */
        tmc50xx_stepper_driver_y_axis@1 {
        };
    };
};

Side effects:

  1. A lot of common code is refactored out and the stepper step-dir or h-bridge based drivers only do what the ic supports. i.e. enable/disable, set/get ustep resolution, step/dir.

  2. Drv84xx handles fault events as of now, which is good. However, handling of fault event has to be refactored out of stepper_api, as mentioned in the RFC Split Stepper Motion Controller <-> Stepper Driver #89786 .
    Introduce set_fault_event_callback, remove fault_event from the current stepper_event enum and rename stepper_event_callback to stepper_motion_control_event_callback.

@jilaypandya jilaypandya force-pushed the feature/introduce-step-dir-functions branch from 0429ffb to 5c9c369 Compare June 21, 2025 22:35
@jilaypandya jilaypandya changed the title wip drivers: stepper: introduce step-dir functions in stepper api to facilitate implementing motion controllers as device drivers Jun 21, 2025
@jilaypandya jilaypandya force-pushed the feature/introduce-step-dir-functions branch 9 times, most recently from 676d5fd to 1d51595 Compare June 22, 2025 12:25
Copy link
Collaborator

@faxe1008 faxe1008 left a comment

Choose a reason for hiding this comment

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

Small little pass, will do some in depth review later on :^)


static int tmc22xx_stepper_enable(const struct device *dev)
{
const struct tmc22xx_config *config = dev->config;

LOG_DBG("Enabling Stepper motor controller %s", dev->name);
return gpio_pin_set_dt(&config->enable_pin, 1);
return gpio_pin_set_dt(&config->enable_pin, 0);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Generally you would set this to 1 and change the actual output to be active low via devicetree

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah that makes sense :) Thanks :^)

Ping @andre-stefanov

Choose a reason for hiding this comment

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

yes this is exactly what i came up with in my implementation in the end. It is a bit inconvenient because the user of e.g. tmc2209 has to know this inverted EN logic during DTS definition instead of just saying "i define the pins, the driver knows which level has to be used for enable/disable". But unfortunately i could not provide gpio port/pin references without output level flag in DTS so i left it as faxe is proposing.

}

static int tmc22xx_stepper_disable(const struct device *dev)
{
const struct tmc22xx_config *config = dev->config;

LOG_DBG("Disabling Stepper motor controller %s", dev->name);
return gpio_pin_set_dt(&config->enable_pin, 0);
return gpio_pin_set_dt(&config->enable_pin, 1);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here

Copy link
Member Author

Choose a reason for hiding this comment

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

done

@@ -121,7 +120,6 @@ static int a4979_stepper_set_micro_step_res(const struct device *dev,
case STEPPER_MICRO_STEP_4:
m0_value = 0;
m1_value = 1;
break;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess this was an accident

Copy link
Member Author

Choose a reason for hiding this comment

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

yep :)

const struct gpio_stepper_config *config = dev->config;

LOG_DBG("Initializing %s gpio_stepper with %d pin", dev->name, NUM_CONTROL_PINS);
for (uint8_t n_pin = 0; n_pin < NUM_CONTROL_PINS; n_pin++) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Checking readyness via gpio_ready_dt

Copy link
Member Author

Choose a reason for hiding this comment

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

done

@jilaypandya jilaypandya force-pushed the feature/introduce-step-dir-functions branch 2 times, most recently from 9ebf11f to d3c27b5 Compare June 22, 2025 13:17
@jilaypandya jilaypandya added this to the v4.3.0 milestone Jun 22, 2025
@jilaypandya jilaypandya force-pushed the feature/introduce-step-dir-functions branch 2 times, most recently from 3b94e61 to 703d030 Compare June 22, 2025 14:16
#include <zephyr/sys/__assert.h>

#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(gpio_stepper_motor_controller, CONFIG_STEPPER_LOG_LEVEL);
Copy link
Member

Choose a reason for hiding this comment

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

Nit: gpio_stepper_motor_controller, just a bit too long :)

@jilaypandya jilaypandya force-pushed the feature/introduce-step-dir-functions branch 4 times, most recently from 4a60ace to e4b6c2d Compare June 22, 2025 15:31
@jilaypandya jilaypandya force-pushed the feature/introduce-step-dir-functions branch 4 times, most recently from 84392f5 to 060b707 Compare June 22, 2025 17:35
@jilaypandya jilaypandya force-pushed the feature/introduce-step-dir-functions branch from 060b707 to e2dd143 Compare June 22, 2025 19:16
@github-actions github-actions bot requested a review from ttmut June 22, 2025 19:22
@jilaypandya jilaypandya force-pushed the feature/introduce-step-dir-functions branch 10 times, most recently from 6b2aadc to 804ed2e Compare June 23, 2025 16:20
@jilaypandya jilaypandya changed the title drivers: stepper: introduce step-dir functions in stepper api to facilitate implementing motion controllers as device drivers drivers: stepper: introduce step-dir api to facilitate implementing motion controllers as device drivers Jun 23, 2025
@jilaypandya jilaypandya linked an issue Jun 23, 2025 that may be closed by this pull request
@jilaypandya jilaypandya force-pushed the feature/introduce-step-dir-functions branch 2 times, most recently from c9641a7 to 9b36dbc Compare June 23, 2025 17:45
Copy link
Collaborator

@jbehrensnx jbehrensnx left a comment

Choose a reason for hiding this comment

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

Mostly minor stuff from me. Overall I am pretty happy with it and I think for an interim solution it is fine.

There are some aspects of the final intended architecture that I feel are worth discussing, but I also feel that they would be beyond the scope of this PR.
The one exception to this is the fault event, as I feel that motion-controllers should be able to raise fault events as well.

Personally, I would prefer if the stepper_motion_controller got renamed to indicate that it is for step-dir drivers, but that is not a hard requirement for me.

@jilaypandya jilaypandya force-pushed the feature/introduce-step-dir-functions branch 6 times, most recently from de5b453 to e41a6df Compare June 24, 2025 19:12
@jilaypandya
Copy link
Member Author

Hey Everyone, here is the wip implementation of splitted stepper_api. stepper_api implements motion_control functions whereas step_dir_api implements stepper_driver functions.

MotionControl functions look highly similar to the ones now from the very first stepper_api #26221.

The tmc50xx sample shows how the combined use of the two apis would look like. Let me know of your opinions :)

introduce step_dir api which is to be implemented by
the step_dir stepper drivers

Add fault handling in drv84xx using a fault cb mechanism

With the introduction of the step-dir api,different motion
controllers to consume various step-dir drivers allowing
for further upstream as well as downstream stepper motion
controllers to be implemented

Signed-off-by: Jilay Pandya <[email protected]>
@jilaypandya jilaypandya force-pushed the feature/introduce-step-dir-functions branch from e41a6df to 1710929 Compare June 24, 2025 19:52
Copy link

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.

5 participants