Zephyr API Documentation 4.3.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
stepper.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: Copyright (c) 2024 Carl Zeiss Meditec AG
3 * SPDX-FileCopyrightText: Copyright (c) 2024 Jilay Sandeep Pandya
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
12
13#ifndef ZEPHYR_INCLUDE_DRIVERS_STEPPER_H_
14#define ZEPHYR_INCLUDE_DRIVERS_STEPPER_H_
15
24
25#include <zephyr/kernel.h>
26#include <zephyr/device.h>
27#include <errno.h>
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
42
54
68
75
81typedef int (*stepper_set_reference_position_t)(const struct device *dev, const int32_t value);
82
88typedef int (*stepper_get_actual_position_t)(const struct device *dev, int32_t *value);
89
93typedef void (*stepper_event_callback_t)(const struct device *dev, const enum stepper_event event,
94 void *user_data);
95
101typedef int (*stepper_set_event_callback_t)(const struct device *dev,
102 stepper_event_callback_t callback, void *user_data);
103
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);
117
123typedef int (*stepper_move_to_t)(const struct device *dev, const int32_t micro_steps);
124
130typedef int (*stepper_run_t)(const struct device *dev, const enum stepper_direction direction);
131
137typedef int (*stepper_stop_t)(const struct device *dev);
138
144typedef int (*stepper_is_moving_t)(const struct device *dev, bool *is_moving);
145
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;
156 stepper_run_t run;
157 stepper_stop_t stop;
158 stepper_is_moving_t is_moving;
159};
160
164
175__syscall int stepper_set_reference_position(const struct device *dev, int32_t value);
176
177static inline int z_impl_stepper_set_reference_position(const struct device *dev,
178 const int32_t value)
179{
180 __ASSERT_NO_MSG(dev != NULL);
181 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
182
183 if (api->set_reference_position == NULL) {
184 return -ENOSYS;
185 }
186 return api->set_reference_position(dev, value);
187}
188
201__syscall int stepper_get_actual_position(const struct device *dev, int32_t *value);
202
203static inline int z_impl_stepper_get_actual_position(const struct device *dev, int32_t *value)
204{
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;
208
209 if (api->get_actual_position == NULL) {
210 return -ENOSYS;
211 }
212 return api->get_actual_position(dev, value);
213}
214
226__syscall int stepper_set_event_callback(const struct device *dev,
227 stepper_event_callback_t callback, void *user_data);
228
229static inline int z_impl_stepper_set_event_callback(const struct device *dev,
230 stepper_event_callback_t callback,
231 void *user_data)
232{
233 __ASSERT_NO_MSG(dev != NULL);
234 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
235
236 if (api->set_event_callback == NULL) {
237 return -ENOSYS;
238 }
239 return api->set_event_callback(dev, callback, user_data);
240}
241
256__syscall int stepper_set_microstep_interval(const struct device *dev,
257 uint64_t microstep_interval_ns);
258
259static inline int z_impl_stepper_set_microstep_interval(const struct device *dev,
260 const uint64_t microstep_interval_ns)
261{
262 __ASSERT_NO_MSG(dev != NULL);
263 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
264
265 if (api->set_microstep_interval == NULL) {
266 return -ENOSYS;
267 }
268 return api->set_microstep_interval(dev, microstep_interval_ns);
269}
270
284__syscall int stepper_move_by(const struct device *dev, int32_t micro_steps);
285
286static inline int z_impl_stepper_move_by(const struct device *dev, const int32_t micro_steps)
287{
288 __ASSERT_NO_MSG(dev != NULL);
289 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
290
291 return api->move_by(dev, micro_steps);
292}
293
307__syscall int stepper_move_to(const struct device *dev, int32_t micro_steps);
308
309static inline int z_impl_stepper_move_to(const struct device *dev, const int32_t micro_steps)
310{
311 __ASSERT_NO_MSG(dev != NULL);
312 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
313
314 return api->move_to(dev, micro_steps);
315}
316
332__syscall int stepper_run(const struct device *dev, enum stepper_direction direction);
333
334static inline int z_impl_stepper_run(const struct device *dev,
335 const enum stepper_direction direction)
336{
337 __ASSERT_NO_MSG(dev != NULL);
338 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
339
340 if (api->run == NULL) {
341 return -ENOSYS;
342 }
343 return api->run(dev, direction);
344}
345
356__syscall int stepper_stop(const struct device *dev);
357
358static inline int z_impl_stepper_stop(const struct device *dev)
359{
360 __ASSERT_NO_MSG(dev != NULL);
361 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
362
363 if (api->stop == NULL) {
364 return -ENOSYS;
365 }
366 return api->stop(dev);
367}
368
379__syscall int stepper_is_moving(const struct device *dev, bool *is_moving);
380
381static inline int z_impl_stepper_is_moving(const struct device *dev, bool *is_moving)
382{
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;
386
387 if (api->is_moving == NULL) {
388 return -ENOSYS;
389 }
390 return api->is_moving(dev, is_moving);
391}
392
396
405
429
434#define MICRO_STEP_RES_INDEX(res) LOG2(res)
435
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)
442
449
456
462typedef int (*stepper_drv_enable_t)(const struct device *dev);
463
469typedef int (*stepper_drv_disable_t)(const struct device *dev);
470
476typedef int (*stepper_drv_set_micro_step_res_t)(
477 const struct device *dev, const enum stepper_drv_micro_step_resolution resolution);
478
484typedef int (*stepper_drv_get_micro_step_res_t)(const struct device *dev,
485 enum stepper_drv_micro_step_resolution *resolution);
486
490typedef void (*stepper_drv_event_cb_t)(const struct device *dev, const enum stepper_drv_event event,
491 void *user_data);
492
498typedef int (*stepper_drv_set_event_callback_t)(const struct device *dev,
499 stepper_drv_event_cb_t callback, void *user_data);
500
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;
510};
511
515
526__syscall int stepper_drv_enable(const struct device *dev);
527
528static inline int z_impl_stepper_drv_enable(const struct device *dev)
529{
530 __ASSERT_NO_MSG(dev != NULL);
531 const struct stepper_drv_driver_api *api = (const struct stepper_drv_driver_api *)dev->api;
532
533 return api->enable(dev);
534}
535
547__syscall int stepper_drv_disable(const struct device *dev);
548
549static inline int z_impl_stepper_drv_disable(const struct device *dev)
550{
551 __ASSERT_NO_MSG(dev != NULL);
552 const struct stepper_drv_driver_api *api = (const struct stepper_drv_driver_api *)dev->api;
553
554 return api->disable(dev);
555}
556
568__syscall int stepper_drv_set_micro_step_res(const struct device *dev,
570
571static inline int z_impl_stepper_drv_set_micro_step_res(const struct device *dev,
573{
574 __ASSERT_NO_MSG(dev != NULL);
575 const struct stepper_drv_driver_api *api = (const struct stepper_drv_driver_api *)dev->api;
576
577 if (!VALID_MICRO_STEP_RES(res)) {
578 return -EINVAL;
579 }
580 return api->set_micro_step_res(dev, res);
581}
582
592__syscall int stepper_drv_get_micro_step_res(const struct device *dev,
594
595static inline int z_impl_stepper_drv_get_micro_step_res(const struct device *dev,
597{
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;
601
602 return api->get_micro_step_res(dev, res);
603}
604
616__syscall int stepper_drv_set_event_cb(const struct device *dev, stepper_drv_event_cb_t callback,
617 void *user_data);
618
619static inline int z_impl_stepper_drv_set_event_cb(const struct device *dev,
620 stepper_drv_event_cb_t cb, void *user_data)
621{
622 __ASSERT_NO_MSG(dev != NULL);
623 const struct stepper_drv_driver_api *api = (const struct stepper_drv_driver_api *)dev->api;
624
625 if (api->set_event_cb == NULL) {
626 return -ENOSYS;
627 }
628
629 return api->set_event_cb(dev, cb, user_data);
630}
631
635
636#ifdef __cplusplus
637}
638#endif
639
640#include <zephyr/syscalls/stepper.h>
641
642#endif /* ZEPHYR_INCLUDE_DRIVERS_STEPPER_H_ */
System error numbers.
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
Public kernel APIs.
__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