Skip to content

Commit e2dd143

Browse files
committed
drivers: stepper: add fault handling in drv84xx
Add fault handling in drv84xx using a fault cb mechanism Signed-off-by: Jilay Pandya <[email protected]>
1 parent dcb8179 commit e2dd143

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

drivers/stepper/ti/drv84xx.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ struct drv84xx_data {
5353
struct drv84xx_pin_states pin_states;
5454
enum stepper_micro_step_resolution ustep_res;
5555
struct gpio_callback fault_cb_data;
56+
stepper_fault_callback_t fault_cb;
57+
void *fault_cb_user_data;
5658
};
5759

5860
STEP_DIR_STEPPER_STRUCT_CHECK(struct drv84xx_config, struct drv84xx_data);
@@ -260,6 +262,17 @@ static int drv84xx_disable(const struct device *dev)
260262
return ret;
261263
}
262264

265+
static int drv84xx_set_fault_cb(const struct device *dev,
266+
stepper_fault_callback_t fault_cb, void *user_data)
267+
{
268+
struct drv84xx_data *data = dev->data;
269+
270+
data->fault_cb = fault_cb;
271+
data->fault_cb_user_data = user_data;
272+
273+
return 0;
274+
}
275+
263276
static int drv84xx_set_micro_step_res(const struct device *dev,
264277
enum stepper_micro_step_resolution micro_step_res)
265278
{
@@ -353,7 +366,11 @@ void fault_event(const struct device *dev, struct gpio_callback *cb, uint32_t pi
353366
{
354367
struct drv84xx_data *data = CONTAINER_OF(cb, struct drv84xx_data, fault_cb_data);
355368

356-
/* Todo: Handle fault event. */
369+
if (data->fault_cb != NULL) {
370+
data->fault_cb(data->dev, data->fault_cb_user_data);
371+
} else {
372+
LOG_WRN_ONCE("%s: Fault pin triggered but no callback is set", dev->name);
373+
}
357374
}
358375

359376
static int drv84xx_init(const struct device *dev)
@@ -440,6 +457,7 @@ static int drv84xx_init(const struct device *dev)
440457
static DEVICE_API(stepper, drv84xx_stepper_api) = {
441458
.enable = drv84xx_enable,
442459
.disable = drv84xx_disable,
460+
.set_fault_cb = drv84xx_set_fault_cb,
443461
.set_micro_step_res = drv84xx_set_micro_step_res,
444462
.get_micro_step_res = drv84xx_get_micro_step_res,
445463
.step = step_dir_stepper_common_step,

include/zephyr/drivers/stepper.h

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,6 @@ enum stepper_event {
102102
STEPPER_EVENT_RIGHT_END_STOP_DETECTED = 3,
103103
/** Stepper has stopped */
104104
STEPPER_EVENT_STOPPED = 4,
105-
/** Fault with the stepper controller detected */
106-
STEPPER_EVENT_FAULT_DETECTED = 5,
107105
};
108106

109107
/**
@@ -177,13 +175,26 @@ typedef int (*stepper_get_actual_position_t)(const struct device *dev, int32_t *
177175
typedef void (*stepper_event_callback_t)(const struct device *dev, const enum stepper_event event,
178176
void *user_data);
179177

178+
/**
179+
* @brief Callback function for stepper fault events
180+
*/
181+
typedef void (*stepper_fault_callback_t)(const struct device *dev, void *user_data);
182+
180183
/**
181184
* @brief Set the callback function to be called when a stepper event occurs
182185
*
183186
* @see stepper_set_event_callback() for details.
184187
*/
185188
typedef int (*stepper_set_event_callback_t)(const struct device *dev,
186189
stepper_event_callback_t callback, void *user_data);
190+
191+
/**
192+
* @brief Set the callback function to be called when a stepper fault occurs
193+
*
194+
* @see stepper_set_fault_callback() for details.
195+
*/
196+
typedef int (*stepper_set_fault_callback_t)(const struct device *dev,
197+
stepper_fault_callback_t callback, void *user_data);
187198
/**
188199
* @brief Set the time interval between steps in nanoseconds.
189200
*
@@ -236,6 +247,8 @@ __subsystem struct stepper_driver_api {
236247
stepper_get_micro_step_res_t get_micro_step_res;
237248
stepper_set_direction_t set_direction;
238249
stepper_step_t step;
250+
stepper_set_fault_callback_t set_fault_cb;
251+
239252
stepper_set_reference_position_t set_reference_position;
240253
stepper_get_actual_position_t get_actual_position;
241254
stepper_set_event_callback_t set_event_callback;
@@ -393,6 +406,33 @@ static inline int z_impl_stepper_get_micro_step_res(const struct device *dev,
393406
return api->get_micro_step_res(dev, resolution);
394407
}
395408

409+
/**
410+
* @brief Set the callback function to be called when a stepper fault occurs
411+
*
412+
* @param dev pointer to the stepper driver instance
413+
* @param callback Callback function to be called when a stepper fault occurs
414+
* passing NULL will disable the callback
415+
* @param user_data User data to be passed to the callback function
416+
*
417+
* @retval -ENOSYS If not implemented by device driver
418+
* @retval 0 Success
419+
*/
420+
__syscall int stepper_set_fault_callback(const struct device *dev,
421+
stepper_fault_callback_t callback, void *user_data);
422+
423+
static inline int z_impl_stepper_set_fault_callback(const struct device *dev,
424+
stepper_fault_callback_t callback,
425+
void *user_data)
426+
{
427+
const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
428+
429+
if (api->set_fault_cb == NULL) {
430+
return -ENOSYS;
431+
}
432+
433+
return api->set_fault_cb(dev, callback, user_data);
434+
}
435+
396436
/**
397437
* @brief Set the reference position of the stepper
398438
*

0 commit comments

Comments
 (0)