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
32#define MICRO_STEP_RES_INDEX(res) LOG2(res)
33
57
67
79
93
106typedef int (*stepper_enable_t)(const struct device *dev, const bool enable);
107
113typedef int (*stepper_move_by_t)(const struct device *dev, const int32_t micro_steps);
114
120typedef int (*stepper_set_max_velocity_t)(const struct device *dev,
121 const uint32_t micro_steps_per_second);
122
128typedef int (*stepper_set_micro_step_res_t)(const struct device *dev,
129 const enum stepper_micro_step_resolution resolution);
130
136typedef int (*stepper_get_micro_step_res_t)(const struct device *dev,
137 enum stepper_micro_step_resolution *resolution);
143typedef int (*stepper_set_reference_position_t)(const struct device *dev, const int32_t value);
144
150typedef int (*stepper_get_actual_position_t)(const struct device *dev, int32_t *value);
151
157typedef int (*stepper_move_to_t)(const struct device *dev, const int32_t micro_steps);
158
164typedef int (*stepper_is_moving_t)(const struct device *dev, bool *is_moving);
165
171typedef int (*stepper_run_t)(const struct device *dev, const enum stepper_direction direction,
172 const uint32_t value);
173
177typedef void (*stepper_event_callback_t)(const struct device *dev, const enum stepper_event event,
178 void *user_data);
179
185typedef int (*stepper_set_event_callback_t)(const struct device *dev,
186 stepper_event_callback_t callback, void *user_data);
187
191__subsystem struct stepper_driver_api {
192 stepper_enable_t enable;
193 stepper_move_by_t move_by;
194 stepper_set_max_velocity_t set_max_velocity;
195 stepper_set_micro_step_res_t set_micro_step_res;
196 stepper_get_micro_step_res_t get_micro_step_res;
197 stepper_set_reference_position_t set_reference_position;
198 stepper_get_actual_position_t get_actual_position;
199 stepper_move_to_t move_to;
200 stepper_is_moving_t is_moving;
201 stepper_run_t run;
202 stepper_set_event_callback_t set_event_callback;
203};
204
218__syscall int stepper_enable(const struct device *dev, const bool enable);
219
220static inline int z_impl_stepper_enable(const struct device *dev, const bool enable)
221{
222 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
223
224 return api->enable(dev, enable);
225}
226
239__syscall int stepper_move_by(const struct device *dev, int32_t micro_steps);
240
241static inline int z_impl_stepper_move_by(const struct device *dev, const int32_t micro_steps)
242{
243 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
244
245 return api->move_by(dev, micro_steps);
246}
247
264__syscall int stepper_set_max_velocity(const struct device *dev, uint32_t micro_steps_per_second);
265
266static inline int z_impl_stepper_set_max_velocity(const struct device *dev,
267 const uint32_t micro_steps_per_second)
268{
269 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
270
271 return api->set_max_velocity(dev, micro_steps_per_second);
272}
273
285__syscall int stepper_set_micro_step_res(const struct device *dev,
286 enum stepper_micro_step_resolution resolution);
287
288static inline int z_impl_stepper_set_micro_step_res(const struct device *dev,
289 enum stepper_micro_step_resolution resolution)
290{
291 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
292
293 if (api->set_micro_step_res == NULL) {
294 return -ENOSYS;
295 }
296 return api->set_micro_step_res(dev, resolution);
297}
298
309__syscall int stepper_get_micro_step_res(const struct device *dev,
310 enum stepper_micro_step_resolution *resolution);
311
312static inline int z_impl_stepper_get_micro_step_res(const struct device *dev,
313 enum stepper_micro_step_resolution *resolution)
314{
315 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
316
317 if (api->get_micro_step_res == NULL) {
318 return -ENOSYS;
319 }
320 return api->get_micro_step_res(dev, resolution);
321}
322
333__syscall int stepper_set_reference_position(const struct device *dev, int32_t value);
334
335static inline int z_impl_stepper_set_reference_position(const struct device *dev,
336 const int32_t value)
337{
338 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
339
340 if (api->set_reference_position == NULL) {
341 return -ENOSYS;
342 }
343 return api->set_reference_position(dev, value);
344}
345
356__syscall int stepper_get_actual_position(const struct device *dev, int32_t *value);
357
358static inline int z_impl_stepper_get_actual_position(const struct device *dev, int32_t *value)
359{
360 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
361
362 if (api->get_actual_position == NULL) {
363 return -ENOSYS;
364 }
365 return api->get_actual_position(dev, value);
366}
367
381__syscall int stepper_move_to(const struct device *dev, int32_t micro_steps);
382
383static inline int z_impl_stepper_move_to(const struct device *dev, const int32_t micro_steps)
384{
385 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
386
387 if (api->move_to == NULL) {
388 return -ENOSYS;
389 }
390 return api->move_to(dev, micro_steps);
391}
392
403__syscall int stepper_is_moving(const struct device *dev, bool *is_moving);
404
405static inline int z_impl_stepper_is_moving(const struct device *dev, bool *is_moving)
406{
407 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
408
409 if (api->is_moving == NULL) {
410 return -ENOSYS;
411 }
412 return api->is_moving(dev, is_moving);
413}
414
432__syscall int stepper_run(const struct device *dev, enum stepper_direction direction,
433 uint32_t velocity);
434
435static inline int z_impl_stepper_run(const struct device *dev,
436 const enum stepper_direction direction,
437 const uint32_t velocity)
438{
439 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
440
441 if (api->run == NULL) {
442 return -ENOSYS;
443 }
444 return api->run(dev, direction, velocity);
445}
446
458__syscall int stepper_set_event_callback(const struct device *dev,
459 stepper_event_callback_t callback, void *user_data);
460
461static inline int z_impl_stepper_set_event_callback(const struct device *dev,
462 stepper_event_callback_t callback,
463 void *user_data)
464{
465 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
466
467 if (api->set_event_callback == NULL) {
468 return -ENOSYS;
469 }
470 return api->set_event_callback(dev, callback, user_data);
471}
472
477#ifdef __cplusplus
478}
479#endif
480
481#include <zephyr/syscalls/stepper.h>
482
483#endif /* ZEPHYR_INCLUDE_DRIVERS_STEPPER_H_ */
System error numbers.
stepper_direction
Stepper Motor direction options.
Definition stepper.h:61
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:71
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 micro_steps to be moved from the current position i.e.
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:83
int stepper_set_max_velocity(const struct device *dev, uint32_t micro_steps_per_second)
Set the target velocity to be reached by the motor.
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 micro step resolution options.
Definition stepper.h:37
int stepper_run(const struct device *dev, enum stepper_direction direction, uint32_t velocity)
Run the stepper with a given velocity in a given direction.
@ STEPPER_DIRECTION_POSITIVE
Positive direction.
Definition stepper.h:65
@ STEPPER_DIRECTION_NEGATIVE
Negative direction.
Definition stepper.h:63
@ STEPPER_RUN_MODE_VELOCITY
Velocity Mode.
Definition stepper.h:77
@ STEPPER_RUN_MODE_HOLD
Hold Mode.
Definition stepper.h:73
@ STEPPER_RUN_MODE_POSITION
Position Mode.
Definition stepper.h:75
@ STEPPER_EVENT_LEFT_END_STOP_DETECTED
Left end switch status changes to pressed.
Definition stepper.h:89
@ STEPPER_EVENT_RIGHT_END_STOP_DETECTED
Right end switch status changes to pressed.
Definition stepper.h:91
@ STEPPER_EVENT_STEPS_COMPLETED
Steps set using move or set_target_position have been executed.
Definition stepper.h:85
@ STEPPER_EVENT_STALL_DETECTED
Stall detected.
Definition stepper.h:87
@ STEPPER_MICRO_STEP_64
64 micro steps per full step
Definition stepper.h:51
@ STEPPER_MICRO_STEP_4
4 micro steps per full step
Definition stepper.h:43
@ STEPPER_MICRO_STEP_1
Full step resolution.
Definition stepper.h:39
@ STEPPER_MICRO_STEP_2
2 micro steps per full step
Definition stepper.h:41
@ STEPPER_MICRO_STEP_256
256 micro steps per full step
Definition stepper.h:55
@ STEPPER_MICRO_STEP_8
8 micro steps per full step
Definition stepper.h:45
@ STEPPER_MICRO_STEP_16
16 micro steps per full step
Definition stepper.h:47
@ STEPPER_MICRO_STEP_32
32 micro steps per full step
Definition stepper.h:49
@ STEPPER_MICRO_STEP_128
128 micro steps per full step
Definition stepper.h:53
#define ENOSYS
Function not implemented.
Definition errno.h:82
Public kernel APIs.
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__INT32_TYPE__ int32_t
Definition stdint.h:74
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