Zephyr API Documentation 4.0.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
stepper.h
Go to the documentation of this file.
1
8/*
9 * SPDX-FileCopyrightText: Copyright (c) 2024 Carl Zeiss Meditec AG
10 * SPDX-FileCopyrightText: Copyright (c) 2024 Jilay Sandeep Pandya
11 * SPDX-License-Identifier: Apache-2.0
12 */
13
14#ifndef ZEPHYR_INCLUDE_DRIVERS_STEPPER_H_
15#define ZEPHYR_INCLUDE_DRIVERS_STEPPER_H_
16
24#include <zephyr/kernel.h>
25#include <zephyr/device.h>
26#include <errno.h>
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
36#define MICRO_STEP_RES_INDEX(res) LOG2(res)
37
61
71
83
97
110typedef int (*stepper_enable_t)(const struct device *dev, const bool enable);
111
117typedef int (*stepper_move_by_t)(const struct device *dev, const int32_t micro_steps);
118
124typedef int (*stepper_set_microstep_interval_t)(const struct device *dev,
125 const uint64_t microstep_interval_ns);
126
132typedef int (*stepper_set_micro_step_res_t)(const struct device *dev,
133 const enum stepper_micro_step_resolution resolution);
134
140typedef int (*stepper_get_micro_step_res_t)(const struct device *dev,
141 enum stepper_micro_step_resolution *resolution);
147typedef int (*stepper_set_reference_position_t)(const struct device *dev, const int32_t value);
148
154typedef int (*stepper_get_actual_position_t)(const struct device *dev, int32_t *value);
155
161typedef int (*stepper_move_to_t)(const struct device *dev, const int32_t micro_steps);
162
168typedef int (*stepper_is_moving_t)(const struct device *dev, bool *is_moving);
169
175typedef int (*stepper_run_t)(const struct device *dev, const enum stepper_direction direction);
176
180typedef void (*stepper_event_callback_t)(const struct device *dev, const enum stepper_event event,
181 void *user_data);
182
188typedef int (*stepper_set_event_callback_t)(const struct device *dev,
189 stepper_event_callback_t callback, void *user_data);
190
194__subsystem struct stepper_driver_api {
195 stepper_enable_t enable;
196 stepper_move_by_t move_by;
197 stepper_set_microstep_interval_t set_microstep_interval;
198 stepper_set_micro_step_res_t set_micro_step_res;
199 stepper_get_micro_step_res_t get_micro_step_res;
200 stepper_set_reference_position_t set_reference_position;
201 stepper_get_actual_position_t get_actual_position;
202 stepper_move_to_t move_to;
203 stepper_is_moving_t is_moving;
204 stepper_run_t run;
205 stepper_set_event_callback_t set_event_callback;
206};
207
221__syscall int stepper_enable(const struct device *dev, const bool enable);
222
223static inline int z_impl_stepper_enable(const struct device *dev, const bool enable)
224{
225 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
226
227 return api->enable(dev, enable);
228}
229
243__syscall int stepper_move_by(const struct device *dev, int32_t micro_steps);
244
245static inline int z_impl_stepper_move_by(const struct device *dev, const int32_t micro_steps)
246{
247 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
248
249 return api->move_by(dev, micro_steps);
250}
251
268__syscall int stepper_set_microstep_interval(const struct device *dev,
269 uint64_t microstep_interval_ns);
270
271static inline int z_impl_stepper_set_microstep_interval(const struct device *dev,
272 const uint64_t microstep_interval_ns)
273{
274 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
275
276 if (api->set_microstep_interval == NULL) {
277 return -ENOSYS;
278 }
279 return api->set_microstep_interval(dev, microstep_interval_ns);
280}
281
293__syscall int stepper_set_micro_step_res(const struct device *dev,
294 enum stepper_micro_step_resolution resolution);
295
296static inline int z_impl_stepper_set_micro_step_res(const struct device *dev,
297 enum stepper_micro_step_resolution resolution)
298{
299 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
300
301 if (api->set_micro_step_res == NULL) {
302 return -ENOSYS;
303 }
304 return api->set_micro_step_res(dev, resolution);
305}
306
317__syscall int stepper_get_micro_step_res(const struct device *dev,
318 enum stepper_micro_step_resolution *resolution);
319
320static inline int z_impl_stepper_get_micro_step_res(const struct device *dev,
321 enum stepper_micro_step_resolution *resolution)
322{
323 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
324
325 if (api->get_micro_step_res == NULL) {
326 return -ENOSYS;
327 }
328 return api->get_micro_step_res(dev, resolution);
329}
330
341__syscall int stepper_set_reference_position(const struct device *dev, int32_t value);
342
343static inline int z_impl_stepper_set_reference_position(const struct device *dev,
344 const int32_t value)
345{
346 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
347
348 if (api->set_reference_position == NULL) {
349 return -ENOSYS;
350 }
351 return api->set_reference_position(dev, value);
352}
353
364__syscall int stepper_get_actual_position(const struct device *dev, int32_t *value);
365
366static inline int z_impl_stepper_get_actual_position(const struct device *dev, int32_t *value)
367{
368 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
369
370 if (api->get_actual_position == NULL) {
371 return -ENOSYS;
372 }
373 return api->get_actual_position(dev, value);
374}
375
390__syscall int stepper_move_to(const struct device *dev, int32_t micro_steps);
391
392static inline int z_impl_stepper_move_to(const struct device *dev, const int32_t micro_steps)
393{
394 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
395
396 if (api->move_to == NULL) {
397 return -ENOSYS;
398 }
399 return api->move_to(dev, micro_steps);
400}
401
412__syscall int stepper_is_moving(const struct device *dev, bool *is_moving);
413
414static inline int z_impl_stepper_is_moving(const struct device *dev, bool *is_moving)
415{
416 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
417
418 if (api->is_moving == NULL) {
419 return -ENOSYS;
420 }
421 return api->is_moving(dev, is_moving);
422}
423
439__syscall int stepper_run(const struct device *dev, enum stepper_direction direction);
440
441static inline int z_impl_stepper_run(const struct device *dev,
442 const enum stepper_direction direction)
443{
444 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
445
446 if (api->run == NULL) {
447 return -ENOSYS;
448 }
449 return api->run(dev, direction);
450}
451
463__syscall int stepper_set_event_callback(const struct device *dev,
464 stepper_event_callback_t callback, void *user_data);
465
466static inline int z_impl_stepper_set_event_callback(const struct device *dev,
467 stepper_event_callback_t callback,
468 void *user_data)
469{
470 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
471
472 if (api->set_event_callback == NULL) {
473 return -ENOSYS;
474 }
475 return api->set_event_callback(dev, callback, user_data);
476}
477
482#ifdef __cplusplus
483}
484#endif
485
486#include <zephyr/syscalls/stepper.h>
487
488#endif /* ZEPHYR_INCLUDE_DRIVERS_STEPPER_H_ */
System error numbers.
stepper_direction
Stepper Motor direction options.
Definition stepper.h:65
int stepper_enable(const struct device *dev, const bool enable)
Enable or disable motor controller.
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:75
int stepper_set_microstep_interval(const struct device *dev, uint64_t microstep_interval_ns)
Set the time interval between steps in microseconds.
int stepper_get_actual_position(const struct device *dev, int32_t *value)
Get the actual a.k.a reference position of the stepper.
int stepper_get_micro_step_res(const struct device *dev, enum stepper_micro_step_resolution *resolution)
Get the microstep resolution in stepper motor controller.
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 microsteps 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_is_moving(const struct device *dev, bool *is_moving)
Check if the stepper motor is currently moving.
stepper_event
Stepper Events.
Definition stepper.h:87
int stepper_set_micro_step_res(const struct device *dev, enum stepper_micro_step_resolution resolution)
Set the microstep resolution in stepper motor controller.
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_micro_step_resolution
Stepper Motor microstep resolution options.
Definition stepper.h:41
@ STEPPER_DIRECTION_POSITIVE
Positive direction.
Definition stepper.h:69
@ STEPPER_DIRECTION_NEGATIVE
Negative direction.
Definition stepper.h:67
@ STEPPER_RUN_MODE_VELOCITY
Velocity Mode.
Definition stepper.h:81
@ STEPPER_RUN_MODE_HOLD
Hold Mode.
Definition stepper.h:77
@ STEPPER_RUN_MODE_POSITION
Position Mode.
Definition stepper.h:79
@ STEPPER_EVENT_LEFT_END_STOP_DETECTED
Left end switch status changes to pressed.
Definition stepper.h:93
@ STEPPER_EVENT_RIGHT_END_STOP_DETECTED
Right end switch status changes to pressed.
Definition stepper.h:95
@ STEPPER_EVENT_STEPS_COMPLETED
Steps set using move_by or move_to have been executed.
Definition stepper.h:89
@ STEPPER_EVENT_STALL_DETECTED
Stall detected.
Definition stepper.h:91
@ STEPPER_MICRO_STEP_64
64 microsteps per full step
Definition stepper.h:55
@ STEPPER_MICRO_STEP_4
4 microsteps per full step
Definition stepper.h:47
@ STEPPER_MICRO_STEP_1
Full step resolution.
Definition stepper.h:43
@ STEPPER_MICRO_STEP_2
2 microsteps per full step
Definition stepper.h:45
@ STEPPER_MICRO_STEP_256
256 microsteps per full step
Definition stepper.h:59
@ STEPPER_MICRO_STEP_8
8 microsteps per full step
Definition stepper.h:49
@ STEPPER_MICRO_STEP_16
16 microsteps per full step
Definition stepper.h:51
@ STEPPER_MICRO_STEP_32
32 microsteps per full step
Definition stepper.h:53
@ STEPPER_MICRO_STEP_128
128 microsteps per full step
Definition stepper.h:57
#define ENOSYS
Function not implemented.
Definition errno.h:82
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:411
const void * api
Address of the API structure exposed by the device instance.
Definition device.h:417