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-License-Identifier: Apache-2.0
11 */
12
13#ifndef ZEPHYR_INCLUDE_DRIVERS_STEPPER_H_
14#define ZEPHYR_INCLUDE_DRIVERS_STEPPER_H_
15
23#include <zephyr/kernel.h>
24#include <zephyr/device.h>
25#include <errno.h>
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
31#define MICRO_STEP_RES_INDEX(res) LOG2(res)
32
56
66
78
92
105typedef int (*stepper_enable_t)(const struct device *dev, const bool enable);
106
112typedef int (*stepper_move_t)(const struct device *dev, const int32_t micro_steps);
113
119typedef int (*stepper_set_max_velocity_t)(const struct device *dev,
120 const uint32_t micro_steps_per_second);
121
127typedef int (*stepper_set_micro_step_res_t)(const struct device *dev,
128 const enum stepper_micro_step_resolution resolution);
129
135typedef int (*stepper_get_micro_step_res_t)(const struct device *dev,
136 enum stepper_micro_step_resolution *resolution);
142typedef int (*stepper_set_actual_position_t)(const struct device *dev, const int32_t value);
143
149typedef int (*stepper_get_actual_position_t)(const struct device *dev, int32_t *value);
150
156typedef int (*stepper_set_target_position_t)(const struct device *dev, const int32_t value);
157
163typedef int (*stepper_is_moving_t)(const struct device *dev, bool *is_moving);
164
170typedef int (*stepper_enable_constant_velocity_mode_t)(const struct device *dev,
171 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_t move;
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_actual_position_t set_actual_position;
198 stepper_get_actual_position_t get_actual_position;
199 stepper_set_target_position_t set_target_position;
200 stepper_is_moving_t is_moving;
201 stepper_enable_constant_velocity_mode_t enable_constant_velocity_mode;
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
236__syscall int stepper_move(const struct device *dev, int32_t micro_steps);
237
238static inline int z_impl_stepper_move(const struct device *dev, const int32_t micro_steps)
239{
240 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
241
242 return api->move(dev, micro_steps);
243}
244
261__syscall int stepper_set_max_velocity(const struct device *dev, uint32_t micro_steps_per_second);
262
263static inline int z_impl_stepper_set_max_velocity(const struct device *dev,
264 const uint32_t micro_steps_per_second)
265{
266 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
267
268 return api->set_max_velocity(dev, micro_steps_per_second);
269}
270
282__syscall int stepper_set_micro_step_res(const struct device *dev,
283 enum stepper_micro_step_resolution resolution);
284
285static inline int z_impl_stepper_set_micro_step_res(const struct device *dev,
286 enum stepper_micro_step_resolution resolution)
287{
288 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
289
290 if (api->set_micro_step_res == NULL) {
291 return -ENOSYS;
292 }
293 return api->set_micro_step_res(dev, resolution);
294}
295
306__syscall int stepper_get_micro_step_res(const struct device *dev,
307 enum stepper_micro_step_resolution *resolution);
308
309static inline int z_impl_stepper_get_micro_step_res(const struct device *dev,
310 enum stepper_micro_step_resolution *resolution)
311{
312 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
313
314 if (api->get_micro_step_res == NULL) {
315 return -ENOSYS;
316 }
317 return api->get_micro_step_res(dev, resolution);
318}
319
330__syscall int stepper_set_actual_position(const struct device *dev, int32_t value);
331
332static inline int z_impl_stepper_set_actual_position(const struct device *dev, const int32_t value)
333{
334 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
335
336 if (api->set_actual_position == NULL) {
337 return -ENOSYS;
338 }
339 return api->set_actual_position(dev, value);
340}
341
352__syscall int stepper_get_actual_position(const struct device *dev, int32_t *value);
353
354static inline int z_impl_stepper_get_actual_position(const struct device *dev, int32_t *value)
355{
356 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
357
358 if (api->get_actual_position == NULL) {
359 return -ENOSYS;
360 }
361 return api->get_actual_position(dev, value);
362}
363
374__syscall int stepper_set_target_position(const struct device *dev, int32_t value);
375
376static inline int z_impl_stepper_set_target_position(const struct device *dev, const int32_t value)
377{
378 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
379
380 if (api->set_target_position == NULL) {
381 return -ENOSYS;
382 }
383 return api->set_target_position(dev, value);
384}
385
396__syscall int stepper_is_moving(const struct device *dev, bool *is_moving);
397
398static inline int z_impl_stepper_is_moving(const struct device *dev, bool *is_moving)
399{
400 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
401
402 if (api->is_moving == NULL) {
403 return -ENOSYS;
404 }
405 return api->is_moving(dev, is_moving);
406}
407
426__syscall int stepper_enable_constant_velocity_mode(const struct device *dev,
427 enum stepper_direction direction,
428 uint32_t value);
429
430static inline int z_impl_stepper_enable_constant_velocity_mode(
431 const struct device *dev, const enum stepper_direction direction, const uint32_t value)
432{
433 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
434
435 if (api->enable_constant_velocity_mode == NULL) {
436 return -ENOSYS;
437 }
438 return api->enable_constant_velocity_mode(dev, direction, value);
439}
440
452__syscall int stepper_set_event_callback(const struct device *dev,
453 stepper_event_callback_t callback, void *user_data);
454
455static inline int z_impl_stepper_set_event_callback(const struct device *dev,
456 stepper_event_callback_t callback,
457 void *user_data)
458{
459 const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
460
461 if (api->set_event_callback == NULL) {
462 return -ENOSYS;
463 }
464 return api->set_event_callback(dev, callback, user_data);
465}
466
471#ifdef __cplusplus
472}
473#endif
474
475#include <zephyr/syscalls/stepper.h>
476
477#endif /* ZEPHYR_INCLUDE_DRIVERS_STEPPER_H_ */
System error numbers.
stepper_direction
Stepper Motor direction options.
Definition stepper.h:60
int stepper_set_target_position(const struct device *dev, int32_t value)
Set the absolute target position of the stepper.
int stepper_enable(const struct device *dev, const bool enable)
Enable or Disable Motor Controller.
int stepper_enable_constant_velocity_mode(const struct device *dev, enum stepper_direction direction, uint32_t value)
Enable constant velocity mode for the stepper with a given velocity.
stepper_run_mode
Stepper Motor run mode options.
Definition stepper.h:70
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(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:82
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:36
int stepper_set_actual_position(const struct device *dev, int32_t value)
Set the actual a.k.a reference position of the stepper.
@ STEPPER_DIRECTION_POSITIVE
Positive direction.
Definition stepper.h:64
@ STEPPER_DIRECTION_NEGATIVE
Negative direction.
Definition stepper.h:62
@ STEPPER_RUN_MODE_VELOCITY
Velocity Mode.
Definition stepper.h:76
@ STEPPER_RUN_MODE_HOLD
Hold Mode.
Definition stepper.h:72
@ STEPPER_RUN_MODE_POSITION
Position Mode.
Definition stepper.h:74
@ STEPPER_EVENT_LEFT_END_STOP_DETECTED
Left end switch status changes to pressed.
Definition stepper.h:88
@ STEPPER_EVENT_RIGHT_END_STOP_DETECTED
Right end switch status changes to pressed.
Definition stepper.h:90
@ STEPPER_EVENT_STEPS_COMPLETED
Steps set using move or set_target_position have been executed.
Definition stepper.h:84
@ STEPPER_EVENT_STALL_DETECTED
Stall detected.
Definition stepper.h:86
@ 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 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:412
const void * api
Address of the API structure exposed by the device instance.
Definition device.h:418