13#ifndef ZEPHYR_INCLUDE_DRIVERS_STEPPER_H_
14#define ZEPHYR_INCLUDE_DRIVERS_STEPPER_H_
81typedef int (*stepper_set_reference_position_t)(
const struct device *dev,
const int32_t value);
88typedef int (*stepper_get_actual_position_t)(
const struct device *dev,
int32_t *value);
93typedef void (*stepper_event_callback_t)(
const struct device *dev,
const enum stepper_event event,
101typedef int (*stepper_set_event_callback_t)(
const struct device *dev,
102 stepper_event_callback_t callback,
void *user_data);
109typedef int (*stepper_set_microstep_interval_t)(
const struct device *dev,
110 const uint64_t microstep_interval_ns);
116typedef int (*stepper_move_by_t)(
const struct device *dev,
const int32_t micro_steps);
123typedef int (*stepper_move_to_t)(
const struct device *dev,
const int32_t micro_steps);
137typedef int (*stepper_stop_t)(
const struct device *dev);
144typedef int (*stepper_is_moving_t)(
const struct device *dev,
bool *is_moving);
149__subsystem
struct stepper_driver_api {
150 stepper_set_reference_position_t set_reference_position;
151 stepper_get_actual_position_t get_actual_position;
152 stepper_set_event_callback_t set_event_callback;
153 stepper_set_microstep_interval_t set_microstep_interval;
154 stepper_move_by_t move_by;
155 stepper_move_to_t move_to;
158 stepper_is_moving_t is_moving;
177static inline int z_impl_stepper_set_reference_position(
const struct device *dev,
180 __ASSERT_NO_MSG(dev !=
NULL);
181 const struct stepper_driver_api *api = (
const struct stepper_driver_api *)dev->
api;
183 if (api->set_reference_position ==
NULL) {
186 return api->set_reference_position(dev, value);
203static inline int z_impl_stepper_get_actual_position(
const struct device *dev,
int32_t *value)
205 __ASSERT_NO_MSG(dev !=
NULL);
206 __ASSERT_NO_MSG(value !=
NULL);
207 const struct stepper_driver_api *api = (
const struct stepper_driver_api *)dev->
api;
209 if (api->get_actual_position ==
NULL) {
212 return api->get_actual_position(dev, value);
227 stepper_event_callback_t callback,
void *user_data);
229static inline int z_impl_stepper_set_event_callback(
const struct device *dev,
230 stepper_event_callback_t callback,
233 __ASSERT_NO_MSG(dev !=
NULL);
234 const struct stepper_driver_api *api = (
const struct stepper_driver_api *)dev->
api;
236 if (api->set_event_callback ==
NULL) {
239 return api->set_event_callback(dev, callback, user_data);
259static inline int z_impl_stepper_set_microstep_interval(
const struct device *dev,
260 const uint64_t microstep_interval_ns)
262 __ASSERT_NO_MSG(dev !=
NULL);
263 const struct stepper_driver_api *api = (
const struct stepper_driver_api *)dev->
api;
265 if (api->set_microstep_interval ==
NULL) {
268 return api->set_microstep_interval(dev, microstep_interval_ns);
286static inline int z_impl_stepper_move_by(
const struct device *dev,
const int32_t micro_steps)
288 __ASSERT_NO_MSG(dev !=
NULL);
289 const struct stepper_driver_api *api = (
const struct stepper_driver_api *)dev->
api;
291 return api->move_by(dev, micro_steps);
309static inline int z_impl_stepper_move_to(
const struct device *dev,
const int32_t micro_steps)
311 __ASSERT_NO_MSG(dev !=
NULL);
312 const struct stepper_driver_api *api = (
const struct stepper_driver_api *)dev->
api;
314 return api->move_to(dev, micro_steps);
334static inline int z_impl_stepper_run(
const struct device *dev,
337 __ASSERT_NO_MSG(dev !=
NULL);
338 const struct stepper_driver_api *api = (
const struct stepper_driver_api *)dev->
api;
340 if (api->run ==
NULL) {
343 return api->run(dev, direction);
358static inline int z_impl_stepper_stop(
const struct device *dev)
360 __ASSERT_NO_MSG(dev !=
NULL);
361 const struct stepper_driver_api *api = (
const struct stepper_driver_api *)dev->
api;
363 if (api->stop ==
NULL) {
366 return api->stop(dev);
381static inline int z_impl_stepper_is_moving(
const struct device *dev,
bool *is_moving)
383 __ASSERT_NO_MSG(dev !=
NULL);
384 __ASSERT_NO_MSG(is_moving !=
NULL);
385 const struct stepper_driver_api *api = (
const struct stepper_driver_api *)dev->
api;
387 if (api->is_moving ==
NULL) {
390 return api->is_moving(dev, is_moving);
434#define MICRO_STEP_RES_INDEX(res) LOG2(res)
436#define VALID_MICRO_STEP_RES(res) \
437 ((res) == STEPPER_DRV_MICRO_STEP_1 || (res) == STEPPER_DRV_MICRO_STEP_2 || \
438 (res) == STEPPER_DRV_MICRO_STEP_4 || (res) == STEPPER_DRV_MICRO_STEP_8 || \
439 (res) == STEPPER_DRV_MICRO_STEP_16 || (res) == STEPPER_DRV_MICRO_STEP_32 || \
440 (res) == STEPPER_DRV_MICRO_STEP_64 || (res) == STEPPER_DRV_MICRO_STEP_128 || \
441 (res) == STEPPER_DRV_MICRO_STEP_256)
462typedef int (*stepper_drv_enable_t)(
const struct device *dev);
469typedef int (*stepper_drv_disable_t)(
const struct device *dev);
476typedef int (*stepper_drv_set_micro_step_res_t)(
484typedef int (*stepper_drv_get_micro_step_res_t)(
const struct device *dev,
498typedef int (*stepper_drv_set_event_callback_t)(
const struct device *dev,
499 stepper_drv_event_cb_t callback,
void *user_data);
504__subsystem
struct stepper_drv_driver_api {
505 stepper_drv_enable_t enable;
506 stepper_drv_disable_t disable;
507 stepper_drv_set_micro_step_res_t set_micro_step_res;
508 stepper_drv_get_micro_step_res_t get_micro_step_res;
509 stepper_drv_set_event_callback_t set_event_cb;
528static inline int z_impl_stepper_drv_enable(
const struct device *dev)
530 __ASSERT_NO_MSG(dev !=
NULL);
531 const struct stepper_drv_driver_api *api = (
const struct stepper_drv_driver_api *)dev->
api;
533 return api->enable(dev);
549static inline int z_impl_stepper_drv_disable(
const struct device *dev)
551 __ASSERT_NO_MSG(dev !=
NULL);
552 const struct stepper_drv_driver_api *api = (
const struct stepper_drv_driver_api *)dev->
api;
554 return api->disable(dev);
571static inline int z_impl_stepper_drv_set_micro_step_res(
const struct device *dev,
574 __ASSERT_NO_MSG(dev !=
NULL);
575 const struct stepper_drv_driver_api *api = (
const struct stepper_drv_driver_api *)dev->
api;
580 return api->set_micro_step_res(dev, res);
595static inline int z_impl_stepper_drv_get_micro_step_res(
const struct device *dev,
598 __ASSERT_NO_MSG(dev !=
NULL);
599 __ASSERT_NO_MSG(res !=
NULL);
600 const struct stepper_drv_driver_api *api = (
const struct stepper_drv_driver_api *)dev->
api;
602 return api->get_micro_step_res(dev, res);
619static inline int z_impl_stepper_drv_set_event_cb(
const struct device *dev,
620 stepper_drv_event_cb_t cb,
void *user_data)
622 __ASSERT_NO_MSG(dev !=
NULL);
623 const struct stepper_drv_driver_api *api = (
const struct stepper_drv_driver_api *)dev->
api;
625 if (api->set_event_cb ==
NULL) {
629 return api->set_event_cb(dev, cb, user_data);
640#include <zephyr/syscalls/stepper.h>
stepper_drv_micro_step_resolution
Stepper Motor micro-step resolution options.
Definition stepper.h:409
#define VALID_MICRO_STEP_RES(res)
Definition stepper.h:436
int stepper_drv_get_micro_step_res(const struct device *dev, enum stepper_drv_micro_step_resolution *res)
Get the micro-step resolution in stepper driver.
int stepper_drv_set_event_cb(const struct device *dev, stepper_drv_event_cb_t callback, void *user_data)
Set the callback function to be called when a stepper_drv_event occurs.
stepper_drv_event
Definition stepper.h:443
int stepper_drv_disable(const struct device *dev)
Disable stepper driver.
int stepper_drv_set_micro_step_res(const struct device *dev, enum stepper_drv_micro_step_resolution res)
Set the micro-step resolution in stepper driver.
int stepper_drv_enable(const struct device *dev)
Enable stepper driver.
@ STEPPER_DRV_MICRO_STEP_2
2 micro-steps per full step
Definition stepper.h:413
@ STEPPER_DRV_MICRO_STEP_64
64 micro-steps per full step
Definition stepper.h:423
@ STEPPER_DRV_MICRO_STEP_128
128 micro-steps per full step
Definition stepper.h:425
@ STEPPER_DRV_MICRO_STEP_8
8 micro-steps per full step
Definition stepper.h:417
@ STEPPER_DRV_MICRO_STEP_1
Full step resolution.
Definition stepper.h:411
@ STEPPER_DRV_MICRO_STEP_32
32 micro-steps per full step
Definition stepper.h:421
@ STEPPER_DRV_MICRO_STEP_16
16 micro-steps per full step
Definition stepper.h:419
@ STEPPER_DRV_MICRO_STEP_4
4 micro-steps per full step
Definition stepper.h:415
@ STEPPER_DRV_MICRO_STEP_256
256 micro-steps per full step
Definition stepper.h:427
@ STEPPER_DRV_EVENT_STALL_DETECTED
Stepper driver stall detected.
Definition stepper.h:445
@ STEPPER_DRV_EVENT_FAULT_DETECTED
Stepper driver fault detected.
Definition stepper.h:447
stepper_direction
Stepper Motor direction options.
Definition stepper.h:36
int stepper_set_reference_position(const struct device *dev, int32_t value)
Set the reference position of the stepper.
stepper_run_mode
Stepper Motor run mode options.
Definition stepper.h:46
int stepper_set_microstep_interval(const struct device *dev, uint64_t microstep_interval_ns)
Set the time interval between steps in nanoseconds with immediate effect.
int stepper_get_actual_position(const struct device *dev, int32_t *value)
Get the actual step count for a given stepper.
int stepper_move_to(const struct device *dev, int32_t micro_steps)
Set the absolute target position of the stepper.
int stepper_move_by(const struct device *dev, int32_t micro_steps)
Set the micro-steps to be moved from the current position i.e.
int stepper_run(const struct device *dev, enum stepper_direction direction)
Run the stepper with a given step interval in a given direction.
int stepper_stop(const struct device *dev)
Stop the stepper.
int stepper_is_moving(const struct device *dev, bool *is_moving)
Check if the stepper is currently moving.
stepper_event
Stepper Events.
Definition stepper.h:58
int stepper_set_event_callback(const struct device *dev, stepper_event_callback_t callback, void *user_data)
Set the callback function to be called when a stepper event occurs.
@ STEPPER_DIRECTION_POSITIVE
Positive direction.
Definition stepper.h:40
@ STEPPER_DIRECTION_NEGATIVE
Negative direction.
Definition stepper.h:38
@ STEPPER_RUN_MODE_VELOCITY
Velocity Mode.
Definition stepper.h:52
@ STEPPER_RUN_MODE_HOLD
Hold Mode.
Definition stepper.h:48
@ STEPPER_RUN_MODE_POSITION
Position Mode.
Definition stepper.h:50
@ STEPPER_EVENT_LEFT_END_STOP_DETECTED
Left end switch status changes to pressed.
Definition stepper.h:62
@ STEPPER_EVENT_RIGHT_END_STOP_DETECTED
Right end switch status changes to pressed.
Definition stepper.h:64
@ STEPPER_EVENT_STEPS_COMPLETED
Steps set using move_by or move_to have been executed.
Definition stepper.h:60
@ STEPPER_EVENT_STOPPED
Stepper has stopped.
Definition stepper.h:66
#define EINVAL
Invalid argument.
Definition errno.h:60
#define ENOSYS
Function not implemented.
Definition errno.h:82
#define NULL
Definition iar_missing_defs.h:20
__INT32_TYPE__ int32_t
Definition stdint.h:74
__UINT64_TYPE__ uint64_t
Definition stdint.h:91
Runtime device structure (in ROM) per driver instance.
Definition device.h:513
const void * api
Address of the API structure exposed by the device instance.
Definition device.h:519