Skip to content

drivers: stepper: refactor ISR safe event mechanism #90424

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

Conversation

jilaypandya
Copy link
Member

@jilaypandya jilaypandya commented May 24, 2025

Refactor ISR safe event handling mechanism from step_dir common to a dedicated module .

@jilaypandya jilaypandya force-pushed the refactor/drop_event_mechanism branch from c60bfe4 to ebdf323 Compare May 24, 2025 09:15
@jilaypandya jilaypandya marked this pull request as ready for review May 24, 2025 09:54
@dipakgmx
Copy link
Member

I am not sure about the context of the ISR callbacks, so had 2 questions:

  1. Why was this check for callback made, if the callback was called in ISR
  2. It seems, right now, we are locking the data in spinlock and calling the callback, i mean something like this:
K_SPINLOCK(lock) {
stepper_handle_timing_signal -> 
  position_mode_task ->  
    update_remaining_steps -> 
      stepper_trigger_callback -> 
        data->callback
}

This, am not sure is the right approach. Guess, the callbacks need to be called from some work, rather than cascading the calls.

drop ISR safe event mechanism handling from step_dir_common

Signed-off-by: Jilay Pandya <jilay.pandya@outlook.com>
@jilaypandya
Copy link
Member Author

jilaypandya commented May 24, 2025

I am not sure about the context of the ISR callbacks, so had 2 questions:

  1. Why was this check for callback made, if the callback was called in ISR
  2. It seems, right now, we are locking the data in spinlock and calling the callback, i mean something like this:
K_SPINLOCK(lock) {
stepper_handle_timing_signal -> 
  position_mode_task ->  
    update_remaining_steps -> 
      stepper_trigger_callback -> 
        data->callback
}

This, am not sure is the right approach. Guess, the callbacks need to be called from some work, rather than cascading the calls.

Yes, you are right, i have refactored the event handling out to a seperate module called stepper_event_handler, so that other drivers can benefit from it as well. Could you give a second look at the way i am using Iterable Sections?

@jilaypandya jilaypandya changed the title drivers: stepper: drop ISR safe event mechanism drivers: stepper: refactor ISR safe event mechanism May 24, 2025
@jilaypandya jilaypandya force-pushed the refactor/drop_event_mechanism branch 4 times, most recently from 6c5cf91 to 44c61e5 Compare May 25, 2025 09:55
Event handling mechanism was hardcoded in step-dir drivers.
This commit refactors the event handling in a dedicated module.

Signed-off-by: Jilay Pandya <jilay.pandya@outlook.com>
@jilaypandya jilaypandya force-pushed the refactor/drop_event_mechanism branch from 44c61e5 to 4549a11 Compare May 25, 2025 11:06
@jilaypandya jilaypandya requested a review from Copilot May 25, 2025 11:13
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR extracts the ISR-safe event queuing logic from the step_dir common code into a dedicated event_handler module and updates all stepper drivers to use the new stepper_post_event API.

  • Moved message‐queue and work handler logic into a standalone stepper_event_handler module
  • Added stepper_post_event function and iterable‐section registrations for each device
  • Updated all drivers and sample code to call stepper_post_event and use STEPPER_DEVICE_DT_DEFINE

Reviewed Changes

Copilot reviewed 26 out of 26 changed files in this pull request and generated no comments.

Show a summary per file
File Description
samples/drivers/stepper/generic/src/main.c Added debug log for STEPPER_EVENT_STEPS_COMPLETED
include/zephyr/drivers/stepper.h Introduced macros and structs for STEPPER_EVENT_HANDLER
drivers/stepper/ti/Kconfig.drv84xx Removed old ISR-safe event select
drivers/stepper/ti/drv84xx.c Switched to STEPPER_DEVICE_DT_INST_DEFINE
drivers/stepper/step_dir/step_dir_stepper_common.h Removed inlined ISR-safe event fields
drivers/stepper/step_dir/step_dir_stepper_common.c Replaced local ISR dispatch with stepper_post_event
drivers/stepper/step_dir/Kconfig Dropped legacy event-template include
drivers/stepper/gpio_stepper_controller.c Switched to stepper_post_event calls and DT define
drivers/stepper/fake_stepper_controller.c Switched to STEPPER_DEVICE_DT_INST_DEFINE
drivers/stepper/allegro/a4979.c Switched to STEPPER_DEVICE_DT_INST_DEFINE
drivers/stepper/adi_tmc/tmc22xx.c Switched to STEPPER_DEVICE_DT_INST_DEFINE
drivers/stepper/event_handler/stepper_event_handler.h New public header for event handler API
drivers/stepper/event_handler/stepper_event_handler.c New implementation of queued ISR-safe event dispatch
drivers/stepper/event_handler/iterables.ld Linker script for iterable sections
drivers/stepper/event_handler/Kconfig Kconfig for enabling/configuring the event handler
drivers/stepper/event_handler/CMakeLists.txt CMake rules to build the event handler module
drivers/stepper/Kconfig Hooked in new event_handler Kconfig
drivers/stepper/CMakeLists.txt Added subdirectory for event_handler
Comments suppressed due to low confidence (4)

drivers/stepper/ti/Kconfig.drv84xx:9

  • After removing the old config, the TI driver no longer selects the new STEPPER_EVENT_HANDLER option. You should add select STEPPER_EVENT_HANDLER to ensure the event handler is enabled.
-	select STEPPER_STEP_DIR_GENERATE_ISR_SAFE_EVENTS

drivers/stepper/event_handler/stepper_event_handler.h:6

  • The header guard does not match the directory hierarchy. It should be ZEPHYR_DRIVERS_STEPPER_EVENT_HANDLER_H_ to align with the include path.
#ifndef ZEPHYR_DRIVER_STEPPER_STEPPER_EVENT_HANDLER_H_

include/zephyr/drivers/stepper.h:543

  • The macros use STRUCT_SECTION_ITERABLE but this header does not include <zephyr/sys/iterable_sections.h>, which can lead to compilation errors. Add the necessary include.
#ifdef CONFIG_STEPPER_EVENT_HANDLER

drivers/stepper/step_dir/step_dir_stepper_common.c:6

  • [nitpick] Use a project‐style include path (e.g., <zephyr/drivers/stepper/event_handler/stepper_event_handler.h>) instead of a relative path for consistency.
#include "../event_handler/stepper_event_handler.h"

Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants