Zephyr Project API 4.2.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
13#ifndef ZEPHYR_INCLUDE_DRIVERS_STEPPER_H_
14#define ZEPHYR_INCLUDE_DRIVERS_STEPPER_H_
15
25#include <zephyr/kernel.h>
26#include <zephyr/device.h>
27#include <errno.h>
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
56
61#define MICRO_STEP_RES_INDEX(res) LOG2(res)
62
63#define VALID_MICRO_STEP_RES(res) \
64 ((res) == STEPPER_MICRO_STEP_1 || (res) == STEPPER_MICRO_STEP_2 || \
65 (res) == STEPPER_MICRO_STEP_4 || (res) == STEPPER_MICRO_STEP_8 || \
66 (res) == STEPPER_MICRO_STEP_16 || (res) == STEPPER_MICRO_STEP_32 || \
67 (res) == STEPPER_MICRO_STEP_64 || (res) == STEPPER_MICRO_STEP_128 || \
68 (res) == STEPPER_MICRO_STEP_256)
69
79
91
109
122typedef int (*stepper_enable_t)(const struct device *dev);
123
129typedef int (*stepper_disable_t)(const struct device *dev);
130
136typedef int (*stepper_set_micro_step_res_t)(const struct device *dev,
137 const enum stepper_micro_step_resolution resolution);
138
144typedef int (*stepper_get_micro_step_res_t)(const struct device *dev,
145 enum stepper_micro_step_resolution *resolution);
151typedef int (*stepper_set_reference_position_t)(const struct device *dev, const int32_t value);
152
158typedef int (*stepper_get_actual_position_t)(const struct device *dev, int32_t *value);
159
163typedef void (*stepper_event_callback_t)(const struct device *dev, const enum stepper_event event,
164 void *user_data);
165
171typedef int (*stepper_set_event_callback_t)(const struct device *dev,
172 stepper_event_callback_t callback, void *user_data);
178typedef int (*stepper_set_microstep_interval_t)(const struct device *dev,
179 const uint64_t microstep_interval_ns);
185typedef int (*stepper_move_by_t)(const struct device *dev, const int32_t micro_steps);
186
192typedef int (*stepper_move_to_t)(const struct device *dev, const int32_t micro_steps);
193
199typedef int (*stepper_run_t)(const struct device *dev, const enum stepper_direction direction);
200
206typedef int (*stepper_stop_t)(const struct device *dev);
207
213typedef int (*stepper_is_moving_t)(const struct device *dev, bool *is_moving);
214
218__subsystem struct stepper_driver_api {
219 stepper_enable_t enable;
220 stepper_disable_t disable;
221 stepper_set_micro_step_res_t set_micro_step_res;
222 stepper_get_micro_step_res_t get_micro_step_res;
223 stepper_set_reference_position_t set_reference_position;
224 stepper_get_actual_position_t get_actual_position;
225 stepper_set_event_callback_t set_event_callback;
226 stepper_set_microstep_interval_t set_microstep_interval;
227 stepper_move_by_t move_by;
228 stepper_move_to_t move_to;
229 stepper_run_t run;
230 stepper_stop_t stop;
231 stepper_is_moving_t is_moving;
232};
233
248__syscall int stepper_enable(const struct device *dev);
249
250static inline int z_impl_stepper_enable(const struct device *dev)
251{
252 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
253
254 return api->enable(dev);
255}
256
270__syscall int stepper_disable(const struct device *dev);
271
272static inline int z_impl_stepper_disable(const struct device *dev)
273{
274 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
275
276 return api->disable(dev);
277}
278
291__syscall int stepper_set_micro_step_res(const struct device *dev,
292 enum stepper_micro_step_resolution resolution);
293
294static inline int z_impl_stepper_set_micro_step_res(const struct device *dev,
295 enum stepper_micro_step_resolution resolution)
296{
297 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
298
299 if (api->set_micro_step_res == NULL) {
300 return -ENOSYS;
301 }
302
303 if (!VALID_MICRO_STEP_RES(resolution)) {
304 return -EINVAL;
305 }
306 return api->set_micro_step_res(dev, resolution);
307}
308
319__syscall int stepper_get_micro_step_res(const struct device *dev,
320 enum stepper_micro_step_resolution *resolution);
321
322static inline int z_impl_stepper_get_micro_step_res(const struct device *dev,
323 enum stepper_micro_step_resolution *resolution)
324{
325 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
326
327 if (api->get_micro_step_res == NULL) {
328 return -ENOSYS;
329 }
330 return api->get_micro_step_res(dev, resolution);
331}
332
343__syscall int stepper_set_reference_position(const struct device *dev, int32_t value);
344
345static inline int z_impl_stepper_set_reference_position(const struct device *dev,
346 const int32_t value)
347{
348 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
349
350 if (api->set_reference_position == NULL) {
351 return -ENOSYS;
352 }
353 return api->set_reference_position(dev, value);
354}
355
366__syscall int stepper_get_actual_position(const struct device *dev, int32_t *value);
367
368static inline int z_impl_stepper_get_actual_position(const struct device *dev, int32_t *value)
369{
370 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
371
372 if (api->get_actual_position == NULL) {
373 return -ENOSYS;
374 }
375 return api->get_actual_position(dev, value);
376}
377
389__syscall int stepper_set_event_callback(const struct device *dev,
390 stepper_event_callback_t callback, void *user_data);
391
392static inline int z_impl_stepper_set_event_callback(const struct device *dev,
393 stepper_event_callback_t callback,
394 void *user_data)
395{
396 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
397
398 if (api->set_event_callback == NULL) {
399 return -ENOSYS;
400 }
401 return api->set_event_callback(dev, callback, user_data);
402}
403
418__syscall int stepper_set_microstep_interval(const struct device *dev,
419 uint64_t microstep_interval_ns);
420
421static inline int z_impl_stepper_set_microstep_interval(const struct device *dev,
422 const uint64_t microstep_interval_ns)
423{
424 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
425
426 if (api->set_microstep_interval == NULL) {
427 return -ENOSYS;
428 }
429 return api->set_microstep_interval(dev, microstep_interval_ns);
430}
431
444__syscall int stepper_move_by(const struct device *dev, int32_t micro_steps);
445
446static inline int z_impl_stepper_move_by(const struct device *dev, const int32_t micro_steps)
447{
448 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
449
450 return api->move_by(dev, micro_steps);
451}
452
466__syscall int stepper_move_to(const struct device *dev, int32_t micro_steps);
467
468static inline int z_impl_stepper_move_to(const struct device *dev, const int32_t micro_steps)
469{
470 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
471
472 if (api->move_to == NULL) {
473 return -ENOSYS;
474 }
475 return api->move_to(dev, micro_steps);
476}
477
492__syscall int stepper_run(const struct device *dev, enum stepper_direction direction);
493
494static inline int z_impl_stepper_run(const struct device *dev,
495 const enum stepper_direction direction)
496{
497 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
498
499 if (api->run == NULL) {
500 return -ENOSYS;
501 }
502 return api->run(dev, direction);
503}
504
515__syscall int stepper_stop(const struct device *dev);
516
517static inline int z_impl_stepper_stop(const struct device *dev)
518{
519 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
520
521 if (api->stop == NULL) {
522 return -ENOSYS;
523 }
524 return api->stop(dev);
525}
526
537__syscall int stepper_is_moving(const struct device *dev, bool *is_moving);
538
539static inline int z_impl_stepper_is_moving(const struct device *dev, bool *is_moving)
540{
541 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
542
543 if (api->is_moving == NULL) {
544 return -ENOSYS;
545 }
546 return api->is_moving(dev, is_moving);
547}
548
553#ifdef __cplusplus
554}
555#endif
556
557#include <zephyr/syscalls/stepper.h>
558
559#endif /* ZEPHYR_INCLUDE_DRIVERS_STEPPER_H_ */
System error numbers.
stepper_direction
Stepper Motor direction options.
Definition stepper.h:73
#define VALID_MICRO_STEP_RES(res)
Definition stepper.h:63
int stepper_enable(const struct device *dev)
Enable stepper driver.
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:83
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 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 micro-step resolution in stepper driver.
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:95
int stepper_disable(const struct device *dev)
Disable stepper driver.
int stepper_set_micro_step_res(const struct device *dev, enum stepper_micro_step_resolution resolution)
Set the micro-step resolution in stepper driver.
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 micro-step resolution options.
Definition stepper.h:36
@ STEPPER_DIRECTION_POSITIVE
Positive direction.
Definition stepper.h:77
@ STEPPER_DIRECTION_NEGATIVE
Negative direction.
Definition stepper.h:75
@ STEPPER_RUN_MODE_VELOCITY
Velocity Mode.
Definition stepper.h:89
@ STEPPER_RUN_MODE_HOLD
Hold Mode.
Definition stepper.h:85
@ STEPPER_RUN_MODE_POSITION
Position Mode.
Definition stepper.h:87
@ STEPPER_EVENT_LEFT_END_STOP_DETECTED
Left end switch status changes to pressed.
Definition stepper.h:101
@ STEPPER_EVENT_RIGHT_END_STOP_DETECTED
Right end switch status changes to pressed.
Definition stepper.h:103
@ STEPPER_EVENT_STEPS_COMPLETED
Steps set using move_by or move_to have been executed.
Definition stepper.h:97
@ STEPPER_EVENT_STALL_DETECTED
Stall detected.
Definition stepper.h:99
@ STEPPER_EVENT_FAULT_DETECTED
Fault with the stepper controller detected.
Definition stepper.h:107
@ STEPPER_EVENT_STOPPED
Stepper has stopped.
Definition stepper.h:105
@ STEPPER_MICRO_STEP_64
64 micro-steps per full step
Definition stepper.h:50
@ STEPPER_MICRO_STEP_4
4 micro-steps per full step
Definition stepper.h:42
@ STEPPER_MICRO_STEP_1
Full step resolution.
Definition stepper.h:38
@ STEPPER_MICRO_STEP_2
2 micro-steps per full step
Definition stepper.h:40
@ STEPPER_MICRO_STEP_256
256 micro-steps per full step
Definition stepper.h:54
@ STEPPER_MICRO_STEP_8
8 micro-steps per full step
Definition stepper.h:44
@ STEPPER_MICRO_STEP_16
16 micro-steps per full step
Definition stepper.h:46
@ STEPPER_MICRO_STEP_32
32 micro-steps per full step
Definition stepper.h:48
@ STEPPER_MICRO_STEP_128
128 micro-steps per full step
Definition stepper.h:52
#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:510
const void * api
Address of the API structure exposed by the device instance.
Definition device.h:516