Skip to content

drivers: stepper: introduce stepper_drv 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 5 commits into
base: main
Choose a base branch
from

Conversation

jilaypandya
Copy link
Member

@jilaypandya jilaypandya commented Jun 21, 2025

Update 06.07.25

Commit 1: Introduces stepper_drv api for stepper driver drivers
Commit 2: Split stepper_drv related functions from stepper_api and introduces stepper_idx in all motion_control functions
commit 3: Add stepper index selection functionality in shell
commit 4: add documentation about stepper_drv api


This PR aims to facilitate implementation of stepper motion controllers as device drivers. Hence the low-level step-dir drivers now shall implement a stepper_drv 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, implements motion control functions
  • Stepper Drv API is responsible for the configuration of stepper_drv drivers.
Drivers Stepper Driver (stepper_drv API) Stepper 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 x
adi,tmc51xx x x
zephyr,stepper-motion-controller (CPU Based) x

Stepper Driver Subsystem shall now have two APIs

stepper driver api (stepper_drv) stepper motion control api (stepper)
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

Code Snippet:
Before

/ {
     aliases {
        x_axis_stepper = &tmc50xx_stepper_x_axis@0;
        y_axis_stepper =  &tmc50xx_stepper_y_axis@1;
     };
};

spi {
    
     tmc50xx_motion_controller {
        compatible = "adi,tmc50xx"

        /* device_api: stepper api */
        tmc50xx_stepper_x_axis@0 {
        };

        /* device_api: stepper api */
        tmc50xx_stepper_y_axis@1 {
        };
    };
};

------------------------------------------------------------------------


stepper_move_to(x_axis_stepper, 200);
stepper_stop(y_axis_stepper);
stepper_disable(x_axis_stepper);
stepper_disable(y_axis_stepper);

After

/ {
     aliases {
        x_y_stepper_motion_controller = &tmc50xx_motion_controller;
        x_axis_stepper_driver = &tmc50xx_stepper_driver_x_axis@0;
        y_axis_stepper_driver =  &tmc50xx_stepper_driver_y_axis@1;
     };
};

spi {
    
      /* device_api: stepper api */
     tmc50xx_motion_controller {
        compatible = "adi,tmc50xx"

        /* device_api: stepper_drv api */
        tmc50xx_stepper_driver_x_axis@0 {
        };

        /* device_api: stepper_drv api */
        tmc50xx_stepper_driver_y_axis@1 {
        };
    };
};

------------------------------------------------------------------------

CONFIG_X_AXIS  0
CONFIG_Y_AXIS  1

stepper_move_to(x_y_stepper_motion_controller, CONFIG_X_AXIS, 200);
stepper_stop(x_y_stepper_motion_controller, CONFIG_Y_AXIS);
stepper_drv_disable(x_axis_stepper_driver);
stepper_drv_disable(y_axis_stepper_driver);

Stepper Implementation Comparison

Aspect Before Implementation After Implementation with stepper_drv API
Alias Structure Direct references to individual steppers:
- x_axis_stepper
- y_axis_stepper
Hierarchical approach:
- Motion controller: x_y_stepper_motion_controller
- Driver references: x_axis_stepper_driver, y_axis_stepper_driver
Device Tree Individual stepper motors as direct API devices:
tmc50xx_stepper_x_axis@0 {/* device_api: stepper api */}
tmc50xx_stepper_y_axis@1 {/* device_api: stepper api */}
Separation of concerns:
- Motion controller with stepper API: tmc50xx_motion_controller {/* device_api: stepper api */}
- Driver components with stepper_drv API: tmc50xx_stepper_driver_x_axis@0 {/* device_api: stepper_drv api */}
API Usage Direct stepper control:
stepper_move_to(x_axis_stepper, 200);
stepper_stop(y_axis_stepper);
stepper_disable(x_axis_stepper);
stepper_disable(y_axis_stepper);
Split responsibility:
- Motion control through controller with axis param:
  stepper_move_to(x_y_stepper_motion_controller, CONFIG_X_AXIS, 200);
  stepper_stop(x_y_stepper_motion_controller, CONFIG_Y_AXIS);
- Direct driver control remains:
  stepper_drv_disable(x_axis_stepper_driver);
  stepper_drv_disable(y_axis_stepper_driver);
Axis Identification Implicit through device reference Explicit using constants or Kconfigs:
CONFIG_X_AXIS 0
CONFIG_Y_AXIS 1
Architecture Flat architecture with each stepper as independent device Hierarchical with clear separation between controller and drivers

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
Contributor

@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
Contributor

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

What if you do #define STEPPER_GPIO_ENABLE_LEVEL DT_PROP(instance_nr, enable_level)
Then enable_level may be defined in .yaml as an integer or boolean as a required entry.
Then one can do: return gpio_pin_set_dt(&config->enable_pin, STEPPER_GPIO_ENABLE_LEVEL);.
I may be wrong - just my 2 cents here.

@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
@faxe1008
Copy link
Contributor

If i go for stepper_driver_* then the current stepper_drv_driver_api would be stepper_driver_driver_api, to me it sounds a bit funny :). We can for sure think of another name :)

True, have not thought about that. Fine with drv then :^)

@jilaypandya
Copy link
Member Author

True, have not thought about that. Fine with drv then :^)

Thx :).

@jilaypandya jilaypandya force-pushed the feature/introduce-step-dir-functions branch from 2983743 to 09ef29a Compare July 21, 2025 10:58
Copy link
Contributor

@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.

Fine for me

@jilaypandya
Copy link
Member Author

Hey Everyone, i have added a table Stepper Implementation Comparison alongwith some Code Snippets to show how this PR will affect the devicetree and the API usage :)

dipakgmx
dipakgmx previously approved these changes Jul 23, 2025
Copy link
Contributor

@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.

Missed a wrong assert message in the drv84xx/api tests in my last reviews.

@jilaypandya jilaypandya force-pushed the feature/introduce-step-dir-functions branch from 09ef29a to 15c4138 Compare July 24, 2025 09:32
introducing stepper_drv api which is to be implemented by
the stepper drivers

Add fault handling in drv84xx using a fault cb mechanism

With the introduction of the stepper_drv api, the goal is
to achieve better separation of concerns where motion
controllers are responsible for generating step pulses
whereaas stepper drivers do are responsible for stepping,
enabling, setting microstep resolution

Signed-off-by: Jilay Pandya <[email protected]>
jbehrensnx
jbehrensnx previously approved these changes Jul 24, 2025
@@ -3,22 +3,26 @@
Steppers
########

The stepper driver API provides a set of functions for controlling and configuring stepper drivers.
The stepper driver subsystem consists of two device driver APIs:

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe an explanation which driver is responsible for the step function?

And an explanation that the stepper driver API makes use of the steper_drv API internally? Some sort of diagram maybe with a driver which implements both vs one where you have to use zephyr,stepper-motion-control

Copy link
Member

Choose a reason for hiding this comment

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

+1 for some diagrams

@jilaypandya jilaypandya force-pushed the feature/introduce-step-dir-functions branch 2 times, most recently from 30464db to b1e061c Compare July 25, 2025 17:05
@jilaypandya
Copy link
Member Author

jilaypandya commented Jul 25, 2025

Hi Everyone, I have introduced one more commit. I think we should bring it in this PR, so that we don't have to introduce breaking changes on the API level in the stepper driver subsystem in the very vicinity of this PR being merged. Sorry for extending the scope of the PR again, but I think it's better that we bring this change in this PR. The most affected drivers are the trinamic ones for now :)

drivers: stepper: introduce event cb mechanism for stepper_drv

stall detection is a stepper_drv functionality and hence stepper_drv
api needs an event callback mechanism

- introduce stepper_index
- Drop stepper_drv functions from stepper_api
- Refactor Stepper Motion Controller
- Refactor Shell

Signed-off-by: Jilay Pandya <[email protected]>
Add stepper index  selection functionality in stepper shell to select
different steppers for the same stepper motion controller

Signed-off-by: Jilay Pandya <[email protected]>
@jilaypandya jilaypandya force-pushed the feature/introduce-step-dir-functions branch from b1e061c to eefa614 Compare July 25, 2025 17:55
@zephyrproject-rtos zephyrproject-rtos deleted a comment from sonarqubecloud bot Jul 25, 2025
@jilaypandya jilaypandya force-pushed the feature/introduce-step-dir-functions branch 2 times, most recently from adae983 to d96828c Compare July 25, 2025 18:59
add information about stepper_drv api and relevant functions
in stepper documentation.
rename zephyr,gpio-stepper to zephyr,h-bridge-stepper

Signed-off-by: Jilay Pandya <[email protected]>
@jilaypandya jilaypandya force-pushed the feature/introduce-step-dir-functions branch from d96828c to aa79d02 Compare July 25, 2025 19:33
stall detection is a stepper_drv functionality and hence stepper_drv
api needs an event callback mechanism to notify about the stepper driver
related events such as fault, stall and so on.

Signed-off-by: Jilay Pandya <[email protected]>
@jilaypandya jilaypandya force-pushed the feature/introduce-step-dir-functions branch from aa79d02 to 75bd6f3 Compare July 26, 2025 08:03
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.

Split Stepper Motion Controller <-> Stepper Driver drivers: stepper: Handling of Multiple Step-Dir Implementations
8 participants