Zephyr Project API 4.1.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#ifndef ZEPHYR_INCLUDE_DRIVERS_STEPPER_H_
13#define ZEPHYR_INCLUDE_DRIVERS_STEPPER_H_
14
24#include <zephyr/kernel.h>
25#include <zephyr/device.h>
26#include <errno.h>
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
55
60#define MICRO_STEP_RES_INDEX(res) LOG2(res)
61
62#define VALID_MICRO_STEP_RES(res) \
63 ((res) == STEPPER_MICRO_STEP_1 || (res) == STEPPER_MICRO_STEP_2 || \
64 (res) == STEPPER_MICRO_STEP_4 || (res) == STEPPER_MICRO_STEP_8 || \
65 (res) == STEPPER_MICRO_STEP_16 || (res) == STEPPER_MICRO_STEP_32 || \
66 (res) == STEPPER_MICRO_STEP_64 || (res) == STEPPER_MICRO_STEP_128 || \
67 (res) == STEPPER_MICRO_STEP_256)
68
78
90
108
121typedef int (*stepper_enable_t)(const struct device *dev);
122
128typedef int (*stepper_disable_t)(const struct device *dev);
129
135typedef int (*stepper_set_micro_step_res_t)(const struct device *dev,
136 const enum stepper_micro_step_resolution resolution);
137
143typedef int (*stepper_get_micro_step_res_t)(const struct device *dev,
144 enum stepper_micro_step_resolution *resolution);
150typedef int (*stepper_set_reference_position_t)(const struct device *dev, const int32_t value);
151
157typedef int (*stepper_get_actual_position_t)(const struct device *dev, int32_t *value);
158
162typedef void (*stepper_event_callback_t)(const struct device *dev, const enum stepper_event event,
163 void *user_data);
164
170typedef int (*stepper_set_event_callback_t)(const struct device *dev,
171 stepper_event_callback_t callback, void *user_data);
177typedef int (*stepper_set_microstep_interval_t)(const struct device *dev,
178 const uint64_t microstep_interval_ns);
184typedef int (*stepper_move_by_t)(const struct device *dev, const int32_t micro_steps);
185
191typedef int (*stepper_move_to_t)(const struct device *dev, const int32_t micro_steps);
192
198typedef int (*stepper_run_t)(const struct device *dev, const enum stepper_direction direction);
199
205typedef int (*stepper_stop_t)(const struct device *dev);
206
212typedef int (*stepper_is_moving_t)(const struct device *dev, bool *is_moving);
213
217__subsystem struct stepper_driver_api {
218 stepper_enable_t enable;
219 stepper_disable_t disable;
220 stepper_set_micro_step_res_t set_micro_step_res;
221 stepper_get_micro_step_res_t get_micro_step_res;
222 stepper_set_reference_position_t set_reference_position;
223 stepper_get_actual_position_t get_actual_position;
224 stepper_set_event_callback_t set_event_callback;
225 stepper_set_microstep_interval_t set_microstep_interval;
226 stepper_move_by_t move_by;
227 stepper_move_to_t move_to;
228 stepper_run_t run;
229 stepper_stop_t stop;
230 stepper_is_moving_t is_moving;
231};
232
247__syscall int stepper_enable(const struct device *dev);
248
249static inline int z_impl_stepper_enable(const struct device *dev)
250{
251 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
252
253 return api->enable(dev);
254}
255
266__syscall int stepper_disable(const struct device *dev);
267
268static inline int z_impl_stepper_disable(const struct device *dev)
269{
270 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
271
272 return api->disable(dev);
273}
274
287__syscall int stepper_set_micro_step_res(const struct device *dev,
288 enum stepper_micro_step_resolution resolution);
289
290static inline int z_impl_stepper_set_micro_step_res(const struct device *dev,
291 enum stepper_micro_step_resolution resolution)
292{
293 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
294
295 if (api->set_micro_step_res == NULL) {
296 return -ENOSYS;
297 }
298
299 if (!VALID_MICRO_STEP_RES(resolution)) {
300 return -EINVAL;
301 }
302 return api->set_micro_step_res(dev, resolution);
303}
304
315__syscall int stepper_get_micro_step_res(const struct device *dev,
316 enum stepper_micro_step_resolution *resolution);
317
318static inline int z_impl_stepper_get_micro_step_res(const struct device *dev,
319 enum stepper_micro_step_resolution *resolution)
320{
321 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
322
323 if (api->get_micro_step_res == NULL) {
324 return -ENOSYS;
325 }
326 return api->get_micro_step_res(dev, resolution);
327}
328
339__syscall int stepper_set_reference_position(const struct device *dev, int32_t value);
340
341static inline int z_impl_stepper_set_reference_position(const struct device *dev,
342 const int32_t value)
343{
344 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
345
346 if (api->set_reference_position == NULL) {
347 return -ENOSYS;
348 }
349 return api->set_reference_position(dev, value);
350}
351
362__syscall int stepper_get_actual_position(const struct device *dev, int32_t *value);
363
364static inline int z_impl_stepper_get_actual_position(const struct device *dev, int32_t *value)
365{
366 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
367
368 if (api->get_actual_position == NULL) {
369 return -ENOSYS;
370 }
371 return api->get_actual_position(dev, value);
372}
373
385__syscall int stepper_set_event_callback(const struct device *dev,
386 stepper_event_callback_t callback, void *user_data);
387
388static inline int z_impl_stepper_set_event_callback(const struct device *dev,
389 stepper_event_callback_t callback,
390 void *user_data)
391{
392 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
393
394 if (api->set_event_callback == NULL) {
395 return -ENOSYS;
396 }
397 return api->set_event_callback(dev, callback, user_data);
398}
399
414__syscall int stepper_set_microstep_interval(const struct device *dev,
415 uint64_t microstep_interval_ns);
416
417static inline int z_impl_stepper_set_microstep_interval(const struct device *dev,
418 const uint64_t microstep_interval_ns)
419{
420 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
421
422 if (api->set_microstep_interval == NULL) {
423 return -ENOSYS;
424 }
425 return api->set_microstep_interval(dev, microstep_interval_ns);
426}
427
441__syscall int stepper_move_by(const struct device *dev, int32_t micro_steps);
442
443static inline int z_impl_stepper_move_by(const struct device *dev, const int32_t micro_steps)
444{
445 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
446
447 return api->move_by(dev, micro_steps);
448}
449
464__syscall int stepper_move_to(const struct device *dev, int32_t micro_steps);
465
466static inline int z_impl_stepper_move_to(const struct device *dev, const int32_t micro_steps)
467{
468 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
469
470 if (api->move_to == NULL) {
471 return -ENOSYS;
472 }
473 return api->move_to(dev, micro_steps);
474}
475
491__syscall int stepper_run(const struct device *dev, enum stepper_direction direction);
492
493static inline int z_impl_stepper_run(const struct device *dev,
494 const enum stepper_direction direction)
495{
496 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
497
498 if (api->run == NULL) {
499 return -ENOSYS;
500 }
501 return api->run(dev, direction);
502}
503
514__syscall int stepper_stop(const struct device *dev);
515
516static inline int z_impl_stepper_stop(const struct device *dev)
517{
518 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
519
520 if (api->stop == NULL) {
521 return -ENOSYS;
522 }
523 return api->stop(dev);
524}
525
536__syscall int stepper_is_moving(const struct device *dev, bool *is_moving);
537
538static inline int z_impl_stepper_is_moving(const struct device *dev, bool *is_moving)
539{
540 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
541
542 if (api->is_moving == NULL) {
543 return -ENOSYS;
544 }
545 return api->is_moving(dev, is_moving);
546}
547
552#ifdef __cplusplus
553}
554#endif
555
556#include <zephyr/syscalls/stepper.h>
557
558#endif /* ZEPHYR_INCLUDE_DRIVERS_STEPPER_H_ */
System error numbers.
stepper_direction
Stepper Motor direction options.
Definition stepper.h:72
#define VALID_MICRO_STEP_RES(res)
Definition stepper.h:62
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:82
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:94
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:35
@ STEPPER_DIRECTION_POSITIVE
Positive direction.
Definition stepper.h:76
@ STEPPER_DIRECTION_NEGATIVE
Negative direction.
Definition stepper.h:74
@ STEPPER_RUN_MODE_VELOCITY
Velocity Mode.
Definition stepper.h:88
@ STEPPER_RUN_MODE_HOLD
Hold Mode.
Definition stepper.h:84
@ STEPPER_RUN_MODE_POSITION
Position Mode.
Definition stepper.h:86
@ STEPPER_EVENT_LEFT_END_STOP_DETECTED
Left end switch status changes to pressed.
Definition stepper.h:100
@ STEPPER_EVENT_RIGHT_END_STOP_DETECTED
Right end switch status changes to pressed.
Definition stepper.h:102
@ STEPPER_EVENT_STEPS_COMPLETED
Steps set using move_by or move_to have been executed.
Definition stepper.h:96
@ STEPPER_EVENT_STALL_DETECTED
Stall detected.
Definition stepper.h:98
@ STEPPER_EVENT_FAULT_DETECTED
Fault with the stepper controller detected.
Definition stepper.h:106
@ STEPPER_EVENT_STOPPED
Stepper has stopped.
Definition stepper.h:104
@ STEPPER_MICRO_STEP_64
64 micro-steps per full step
Definition stepper.h:49
@ STEPPER_MICRO_STEP_4
4 micro-steps per full step
Definition stepper.h:41
@ STEPPER_MICRO_STEP_1
Full step resolution.
Definition stepper.h:37
@ STEPPER_MICRO_STEP_2
2 micro-steps per full step
Definition stepper.h:39
@ STEPPER_MICRO_STEP_256
256 micro-steps per full step
Definition stepper.h:53
@ STEPPER_MICRO_STEP_8
8 micro-steps per full step
Definition stepper.h:43
@ STEPPER_MICRO_STEP_16
16 micro-steps per full step
Definition stepper.h:45
@ STEPPER_MICRO_STEP_32
32 micro-steps per full step
Definition stepper.h:47
@ STEPPER_MICRO_STEP_128
128 micro-steps per full step
Definition stepper.h:51
#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