Skip to content

Commit 20db5bd

Browse files
committed
drivers: stepper: split api into stepper driver & stepper control driver
Currently Stepper Driver API combines two different motion control and the actual stepping of a motor behind a single API. However, these are two different areas and hence need to be seperated considering that there motion controllers out there which just do motion control and interface via step/dir with a step/dir driver Signed-off-by: Jilay Pandya <[email protected]>
1 parent e6fc73e commit 20db5bd

File tree

2 files changed

+396
-280
lines changed

2 files changed

+396
-280
lines changed

include/zephyr/drivers/stepper.h

Lines changed: 31 additions & 280 deletions
Original file line numberDiff line numberDiff line change
@@ -88,24 +88,6 @@ enum stepper_run_mode {
8888
STEPPER_RUN_MODE_VELOCITY = 2,
8989
};
9090

91-
/**
92-
* @brief Stepper Events
93-
*/
94-
enum stepper_event {
95-
/** Steps set using move_by or move_to have been executed */
96-
STEPPER_EVENT_STEPS_COMPLETED = 0,
97-
/** Stall detected */
98-
STEPPER_EVENT_STALL_DETECTED = 1,
99-
/** Left end switch status changes to pressed */
100-
STEPPER_EVENT_LEFT_END_STOP_DETECTED = 2,
101-
/** Right end switch status changes to pressed */
102-
STEPPER_EVENT_RIGHT_END_STOP_DETECTED = 3,
103-
/** Stepper has stopped */
104-
STEPPER_EVENT_STOPPED = 4,
105-
/** Fault with the stepper controller detected */
106-
STEPPER_EVENT_FAULT_DETECTED = 5,
107-
};
108-
10991
/**
11092
* @cond INTERNAL_HIDDEN
11193
*
@@ -127,6 +109,13 @@ typedef int (*stepper_enable_t)(const struct device *dev);
127109
*/
128110
typedef int (*stepper_disable_t)(const struct device *dev);
129111

112+
/**
113+
* @brief Stop the stepper
114+
*
115+
* @see stepper_stop() for details.
116+
*/
117+
typedef int (*stepper_stop_t)(const struct device *dev);
118+
130119
/**
131120
* @brief Set the micro-step resolution
132121
*
@@ -142,74 +131,21 @@ typedef int (*stepper_set_micro_step_res_t)(const struct device *dev,
142131
*/
143132
typedef int (*stepper_get_micro_step_res_t)(const struct device *dev,
144133
enum stepper_micro_step_resolution *resolution);
145-
/**
146-
* @brief Set the reference position of the stepper
147-
*
148-
* @see stepper_set_actual_position() for details.
149-
*/
150-
typedef int (*stepper_set_reference_position_t)(const struct device *dev, const int32_t value);
151-
152-
/**
153-
* @brief Get the actual a.k.a reference position of the stepper
154-
*
155-
* @see stepper_get_actual_position() for details.
156-
*/
157-
typedef int (*stepper_get_actual_position_t)(const struct device *dev, int32_t *value);
158-
159-
/**
160-
* @brief Callback function for stepper events
161-
*/
162-
typedef void (*stepper_event_callback_t)(const struct device *dev, const enum stepper_event event,
163-
void *user_data);
164-
165-
/**
166-
* @brief Set the callback function to be called when a stepper event occurs
167-
*
168-
* @see stepper_set_event_callback() for details.
169-
*/
170-
typedef int (*stepper_set_event_callback_t)(const struct device *dev,
171-
stepper_event_callback_t callback, void *user_data);
172-
/**
173-
* @brief Set the time interval between steps in nanoseconds.
174-
*
175-
* @see stepper_set_microstep_interval() for details.
176-
*/
177-
typedef int (*stepper_set_microstep_interval_t)(const struct device *dev,
178-
const uint64_t microstep_interval_ns);
179-
/**
180-
* @brief Move the stepper relatively by a given number of micro-steps.
181-
*
182-
* @see stepper_move_by() for details.
183-
*/
184-
typedef int (*stepper_move_by_t)(const struct device *dev, const int32_t micro_steps);
185-
186-
/**
187-
* @brief Move the stepper to an absolute position in micro-steps.
188-
*
189-
* @see stepper_move_to() for details.
190-
*/
191-
typedef int (*stepper_move_to_t)(const struct device *dev, const int32_t micro_steps);
192134

193135
/**
194-
* @brief Run the stepper with a given step interval in a given direction
195-
*
196-
* @see stepper_run() for details.
197-
*/
198-
typedef int (*stepper_run_t)(const struct device *dev, const enum stepper_direction direction);
199-
200-
/**
201-
* @brief Stop the stepper
136+
* @brief Set the stepper direction
202137
*
203-
* @see stepper_stop() for details.
138+
* @see stepper_set_direction() for details.
204139
*/
205-
typedef int (*stepper_stop_t)(const struct device *dev);
140+
typedef int (*stepper_set_direction_t)(const struct device *dev,
141+
const enum stepper_direction direction);
206142

207143
/**
208-
* @brief Is the target position fo the stepper reached
144+
* @brief Do a step
209145
*
210-
* @see stepper_is_moving() for details.
146+
* @see stepper_step() for details.
211147
*/
212-
typedef int (*stepper_is_moving_t)(const struct device *dev, bool *is_moving);
148+
typedef int (*stepper_step_t)(const struct device *dev);
213149

214150
/**
215151
* @brief Stepper Driver API
@@ -219,15 +155,8 @@ __subsystem struct stepper_driver_api {
219155
stepper_disable_t disable;
220156
stepper_set_micro_step_res_t set_micro_step_res;
221157
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;
158+
stepper_set_direction_t set_direction;
159+
stepper_step_t step;
231160
};
232161

233162
/**
@@ -322,222 +251,44 @@ static inline int z_impl_stepper_get_micro_step_res(const struct device *dev,
322251
}
323252

324253
/**
325-
* @brief Set the reference position of the stepper
326-
*
327-
* @param dev Pointer to the stepper driver instance.
328-
* @param value The reference position to set in micro-steps.
329-
*
330-
* @retval -EIO General input / output error
331-
* @retval -ENOSYS If not implemented by device driver
332-
* @retval 0 Success
333-
*/
334-
__syscall int stepper_set_reference_position(const struct device *dev, int32_t value);
335-
336-
static inline int z_impl_stepper_set_reference_position(const struct device *dev,
337-
const int32_t value)
338-
{
339-
const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
340-
341-
if (api->set_reference_position == NULL) {
342-
return -ENOSYS;
343-
}
344-
return api->set_reference_position(dev, value);
345-
}
346-
347-
/**
348-
* @brief Get the actual a.k.a reference position of the stepper
349-
*
350-
* @param dev pointer to the stepper driver instance
351-
* @param value The actual position to get in micro-steps
352-
*
353-
* @retval -EIO General input / output error
354-
* @retval -ENOSYS If not implemented by device driver
355-
* @retval 0 Success
356-
*/
357-
__syscall int stepper_get_actual_position(const struct device *dev, int32_t *value);
358-
359-
static inline int z_impl_stepper_get_actual_position(const struct device *dev, int32_t *value)
360-
{
361-
const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
362-
363-
if (api->get_actual_position == NULL) {
364-
return -ENOSYS;
365-
}
366-
return api->get_actual_position(dev, value);
367-
}
368-
369-
/**
370-
* @brief Set the callback function to be called when a stepper event occurs
371-
*
372-
* @param dev pointer to the stepper driver instance
373-
* @param callback Callback function to be called when a stepper event occurs
374-
* passing NULL will disable the callback
375-
* @param user_data User data to be passed to the callback function
376-
*
377-
* @retval -ENOSYS If not implemented by device driver
378-
* @retval 0 Success
379-
*/
380-
__syscall int stepper_set_event_callback(const struct device *dev,
381-
stepper_event_callback_t callback, void *user_data);
382-
383-
static inline int z_impl_stepper_set_event_callback(const struct device *dev,
384-
stepper_event_callback_t callback,
385-
void *user_data)
386-
{
387-
const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
388-
389-
if (api->set_event_callback == NULL) {
390-
return -ENOSYS;
391-
}
392-
return api->set_event_callback(dev, callback, user_data);
393-
}
394-
395-
/**
396-
* @brief Set the time interval between steps in nanoseconds with immediate effect.
397-
*
398-
* @note Setting step interval does not set the stepper into motion, a combination of
399-
* set_microstep_interval and move is required to set the stepper into motion.
400-
*
401-
* @param dev pointer to the stepper driver instance
402-
* @param microstep_interval_ns time interval between steps in nanoseconds
403-
*
404-
* @retval -EIO General input / output error
405-
* @retval -EINVAL If the requested step interval is not supported
406-
* @retval -ENOSYS If not implemented by device driver
407-
* @retval 0 Success
408-
*/
409-
__syscall int stepper_set_microstep_interval(const struct device *dev,
410-
uint64_t microstep_interval_ns);
411-
412-
static inline int z_impl_stepper_set_microstep_interval(const struct device *dev,
413-
const uint64_t microstep_interval_ns)
414-
{
415-
const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
416-
417-
if (api->set_microstep_interval == NULL) {
418-
return -ENOSYS;
419-
}
420-
return api->set_microstep_interval(dev, microstep_interval_ns);
421-
}
422-
423-
/**
424-
* @brief Set the micro-steps to be moved from the current position i.e. relative movement
425-
*
426-
* @details The stepper will move by the given number of micro-steps from the current position.
427-
* This function is non-blocking.
428-
*
429-
* @param dev pointer to the stepper driver instance
430-
* @param micro_steps target micro-steps to be moved from the current position
431-
*
432-
* @retval -ECANCELED If the stepper is disabled
433-
* @retval -EIO General input / output error
434-
* @retval 0 Success
435-
*/
436-
__syscall int stepper_move_by(const struct device *dev, int32_t micro_steps);
437-
438-
static inline int z_impl_stepper_move_by(const struct device *dev, const int32_t micro_steps)
439-
{
440-
const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
441-
442-
return api->move_by(dev, micro_steps);
443-
}
444-
445-
/**
446-
* @brief Set the absolute target position of the stepper
447-
*
448-
* @details The stepper will move to the given micro-steps position from the reference position.
449-
* This function is non-blocking.
450-
*
451-
* @param dev pointer to the stepper driver instance
452-
* @param micro_steps target position to set in micro-steps
453-
*
454-
* @retval -ECANCELED If the stepper is disabled
455-
* @retval -EIO General input / output error
456-
* @retval -ENOSYS If not implemented by device driver
457-
* @retval 0 Success
458-
*/
459-
__syscall int stepper_move_to(const struct device *dev, int32_t micro_steps);
460-
461-
static inline int z_impl_stepper_move_to(const struct device *dev, const int32_t micro_steps)
462-
{
463-
const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
464-
465-
if (api->move_to == NULL) {
466-
return -ENOSYS;
467-
}
468-
return api->move_to(dev, micro_steps);
469-
}
470-
471-
/**
472-
* @brief Run the stepper with a given step interval in a given direction
473-
*
474-
* @details The stepper shall be set into motion and run continuously until
475-
* stalled or stopped using some other command, for instance, stepper_enable(false). This
476-
* function is non-blocking.
254+
* @brief Set the stepper direction
477255
*
256+
* @note If STEPPER_CONTROL is enabled, check if the motor is not moving before changing the
257+
* direction.
478258
* @param dev pointer to the stepper driver instance
479-
* @param direction The direction to set
259+
* @param direction Direction to set
480260
*
481-
* @retval -ECANCELED If the stepper is disabled
482261
* @retval -EIO General input / output error
483-
* @retval -ENOSYS If not implemented by device driver
484262
* @retval 0 Success
485263
*/
486-
__syscall int stepper_run(const struct device *dev, enum stepper_direction direction);
264+
__syscall int stepper_set_direction(const struct device *dev,
265+
const enum stepper_direction direction);
487266

488-
static inline int z_impl_stepper_run(const struct device *dev,
489-
const enum stepper_direction direction)
267+
static inline int z_impl_stepper_set_direction(const struct device *dev,
268+
const enum stepper_direction direction)
490269
{
491270
const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
492271

493-
if (api->run == NULL) {
494-
return -ENOSYS;
495-
}
496-
return api->run(dev, direction);
272+
return api->set_direction(dev, direction);
497273
}
498274

499275
/**
500-
* @brief Stop the stepper
501-
* @details Cancel all active movements, however keep the coils energized.
502-
*
503-
* @param dev pointer to the stepper driver instance
276+
* @brief Do a step
504277
*
505-
* @retval -EIO General input / output error
506-
* @retval -ENOSYS If not implemented by device driver
507-
* @retval 0 Success
508-
*/
509-
__syscall int stepper_stop(const struct device *dev);
510-
511-
static inline int z_impl_stepper_stop(const struct device *dev)
512-
{
513-
const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
514-
515-
if (api->stop == NULL) {
516-
return -ENOSYS;
517-
}
518-
return api->stop(dev);
519-
}
520-
521-
/**
522-
* @brief Check if the stepper is currently moving
278+
* @note If STEPPER_CONTROL is enabled, calling this function will cause the stepper to move one
279+
* step without position being tracked.
523280
*
524281
* @param dev pointer to the stepper driver instance
525-
* @param is_moving Pointer to a boolean to store the moving status of the stepper
526-
*
527282
* @retval -EIO General input / output error
528-
* @retval -ENOSYS If not implemented by device driver
529283
* @retval 0 Success
530284
*/
531-
__syscall int stepper_is_moving(const struct device *dev, bool *is_moving);
285+
__syscall int stepper_step(const struct device *dev);
532286

533-
static inline int z_impl_stepper_is_moving(const struct device *dev, bool *is_moving)
287+
static inline int z_impl_stepper_step(const struct device *dev)
534288
{
535289
const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
536290

537-
if (api->is_moving == NULL) {
538-
return -ENOSYS;
539-
}
540-
return api->is_moving(dev, is_moving);
291+
return api->step(dev);
541292
}
542293

543294
/**

0 commit comments

Comments
 (0)