Skip to content

Commit adae983

Browse files
committed
drivers: stepper: introduce event cb mechanism for stepper_drv
stall detection is a stepper_drv functionality and hence stepper_drv api needs an event callback mechanism Signed-off-by: Jilay Pandya <[email protected]>
1 parent 92eee51 commit adae983

File tree

10 files changed

+110
-31
lines changed

10 files changed

+110
-31
lines changed

drivers/stepper/adi_tmc/tmc50xx.c

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ struct tmc50xx_stepper_data {
3737
const struct device *stepper;
3838
stepper_event_callback_t callback;
3939
void *event_cb_user_data;
40+
stepper_drv_event_cb_t drv_event_cb;
41+
void *drv_event_cb_user_data;
4042
};
4143

4244
struct tmc50xx_stepper_config {
@@ -116,6 +118,17 @@ static int tmc50xx_set_event_callback(const struct device *controller, const uin
116118
return 0;
117119
}
118120

121+
static int tmc50xx_set_stepper_drv_event_callback(const struct device *stepper,
122+
stepper_drv_event_cb_t callback, void *user_data)
123+
{
124+
struct tmc50xx_stepper_data *data = stepper->data;
125+
126+
data->drv_event_cb = callback;
127+
data->drv_event_cb_user_data = user_data;
128+
129+
return 0;
130+
}
131+
119132
static int read_vactual(const struct device *controller, const uint8_t stepper_index,
120133
int32_t *actual_velocity)
121134
{
@@ -206,6 +219,17 @@ static void execute_callback(const struct device *controller, const uint8_t step
206219
data->callback(controller, stepper_index, event, data->event_cb_user_data);
207220
}
208221

222+
static void execute_stepper_drv_cb(const struct device *stepper, const enum stepper_drv_event event)
223+
{
224+
struct tmc50xx_stepper_data *stepper_data = stepper->data;
225+
226+
if (stepper_data->drv_event_cb) {
227+
stepper_data->drv_event_cb(stepper, event, stepper_data->drv_event_cb_user_data);
228+
} else {
229+
LOG_WRN_ONCE("%s: No callback registered", stepper->name);
230+
}
231+
}
232+
209233
#ifdef CONFIG_STEPPER_ADI_TMC50XX_RAMPSTAT_POLL_STALLGUARD_LOG
210234

211235
static void log_stallguard(struct tmc50xx_stepper_data *stepper_data, const uint32_t drv_status)
@@ -305,8 +329,8 @@ static void rampstat_work_handler(struct k_work *work)
305329
case TMC5XXX_STOP_SG_EVENT:
306330
LOG_DBG("RAMPSTAT %s:Stall detected", stepper_data->stepper->name);
307331
stallguard_enable(stepper_config->controller, stepper_config->index, false);
308-
execute_callback(stepper_config->controller, stepper_config->index,
309-
STEPPER_EVENT_STALL_DETECTED);
332+
execute_stepper_drv_cb(stepper_data->stepper,
333+
STEPPER_DRV_EVENT_STALL_DETETCTED);
310334
break;
311335
default:
312336
LOG_ERR("Illegal ramp stat bit field");
@@ -757,6 +781,7 @@ static DEVICE_API(stepper_drv, tmc50xx_stepper_drv_api) = {
757781
.disable = tmc50xx_stepper_disable,
758782
.set_micro_step_res = tmc50xx_stepper_set_micro_step_res,
759783
.get_micro_step_res = tmc50xx_stepper_get_micro_step_res,
784+
.set_event_cb = tmc50xx_set_stepper_drv_event_callback,
760785
};
761786

762787
static DEVICE_API(stepper, tmc50xx_stepper_api) = {

drivers/stepper/adi_tmc/tmc51xx/tmc51xx.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ static int tmc51xx_read(const struct device *dev, const uint8_t reg_addr, uint32
6868
return 0;
6969
}
7070

71+
static int tmc51xx_stepper_drv_set_event_cb(const struct device *stepper,
72+
stepper_drv_event_cb_t callback, void *user_data)
73+
{
74+
struct tmc51xx_stepper_data *data = stepper->data;
75+
76+
data->drv_event_cb = callback;
77+
data->drv_event_cb_user_data = user_data;
78+
79+
return 0;
80+
}
81+
7182
static int tmc51xx_stepper_set_event_callback(const struct device *controller,
7283
const uint8_t stepper_idx,
7384
stepper_event_callback_t callback, void *user_data)
@@ -213,6 +224,17 @@ static void stepper_trigger_callback(const struct device *dev, const enum steppe
213224
data->callback(dev, 0, event, data->event_cb_user_data);
214225
}
215226

227+
static void trigger_stepper_drv_cb(const struct device *stepper, const enum stepper_drv_event event)
228+
{
229+
struct tmc51xx_stepper_data *stepper_data = stepper->data;
230+
231+
if (stepper_data->drv_event_cb) {
232+
stepper_data->drv_event_cb(stepper, event, stepper_data->drv_event_cb_user_data);
233+
} else {
234+
LOG_WRN_ONCE("%s: No callback registered", stepper->name);
235+
}
236+
}
237+
216238
#ifdef CONFIG_STEPPER_ADI_TMC51XX_RAMPSTAT_POLL_STALLGUARD_LOG
217239

218240
static void log_stallguard(const struct device *dev, const uint32_t drv_status)
@@ -309,7 +331,7 @@ static void rampstat_work_handler(struct k_work *work)
309331
case TMC5XXX_STOP_SG_EVENT:
310332
LOG_DBG("RAMPSTAT %s:Stall detected", dev->name);
311333
stallguard_enable(dev, false);
312-
stepper_trigger_callback(dev, STEPPER_EVENT_STALL_DETECTED);
334+
trigger_stepper_drv_cb(dev, STEPPER_DRV_EVENT_STALL_DETETCTED);
313335
break;
314336
default:
315337
LOG_ERR("Illegal ramp stat bit field 0x%x", ramp_stat_values);
@@ -845,6 +867,7 @@ static DEVICE_API(stepper_drv, tmc51xx_drv_api) = {
845867
.disable = tmc51xx_stepper_disable,
846868
.set_micro_step_res = tmc51xx_stepper_set_micro_step_res,
847869
.get_micro_step_res = tmc51xx_stepper_get_micro_step_res,
870+
.set_event_cb = tmc51xx_stepper_drv_set_event_cb,
848871
};
849872

850873
static DEVICE_API(stepper, tmc51xx_api) = {

drivers/stepper/adi_tmc/tmc51xx/tmc51xx.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ struct tmc51xx_stepper_data {
6161
const struct device *stepper;
6262
stepper_event_callback_t callback;
6363
void *event_cb_user_data;
64+
stepper_drv_event_cb_t drv_event_cb;
65+
void *drv_event_cb_user_data;
6466
};
6567

6668
#if TMC51XX_BUS_SPI

drivers/stepper/fake_stepper_drv.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ DEFINE_FAKE_VALUE_FUNC(int, fake_stepper_set_micro_step_res, const struct device
2828
DEFINE_FAKE_VALUE_FUNC(int, fake_stepper_get_micro_step_res, const struct device *,
2929
enum stepper_micro_step_resolution *);
3030

31+
DEFINE_FAKE_VALUE_FUNC(void, fake_stepper_drv_set_event_callback, const struct device *,
32+
stepper_event_callback_t, void *);
33+
3134
static int fake_stepper_set_micro_step_res_delegate(const struct device *dev,
3235
const enum stepper_micro_step_resolution res)
3336
{
@@ -79,6 +82,7 @@ static DEVICE_API(stepper_drv, fake_stepper_driver_api) = {
7982
.disable = fake_stepper_disable,
8083
.set_micro_step_res = fake_stepper_set_micro_step_res,
8184
.get_micro_step_res = fake_stepper_get_micro_step_res,
85+
.set_event_cb = fake_stepper_drv_set_event_callback,
8286
};
8387

8488
#define FAKE_STEPPER_INIT(inst) \

drivers/stepper/stepper_shell.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,30 @@ struct stepper_control_idx_map {
5454
.stepper_idx = _idx, \
5555
}
5656

57+
static void print_stepper_drv_event_cb(const struct device *dev, const enum stepper_drv_event event,
58+
void *user_data)
59+
{
60+
const struct shell *sh = user_data;
61+
62+
if (!sh) {
63+
return;
64+
}
65+
66+
switch (event) {
67+
case STEPPER_DRV_EVENT_STALL_DETETCTED:
68+
shell_info(sh, "%s: Stall detected.", dev->name);
69+
break;
70+
case STEPPER_DRV_EVENT_FAULT_DETECTED:
71+
shell_info(sh, "%s: Fault detected.", dev->name);
72+
break;
73+
}
74+
}
75+
5776
static void print_callback(const struct device *dev, const uint8_t stepper_idx,
5877
const enum stepper_event event, void *user_data)
5978
{
6079
const struct shell *sh = user_data;
80+
6181
if (!sh) {
6282
return;
6383
}
@@ -66,9 +86,6 @@ static void print_callback(const struct device *dev, const uint8_t stepper_idx,
6686
case STEPPER_EVENT_STEPS_COMPLETED:
6787
shell_info(sh, "%s: Steps completed.", dev->name);
6888
break;
69-
case STEPPER_EVENT_STALL_DETECTED:
70-
shell_info(sh, "%s: Stall detected.", dev->name);
71-
break;
7289
case STEPPER_EVENT_LEFT_END_STOP_DETECTED:
7390
shell_info(sh, "%s: Left limit switch pressed.", dev->name);
7491
break;
@@ -255,6 +272,10 @@ static int cmd_stepper_enable(const struct shell *sh, size_t argc, char **argv)
255272
shell_error(sh, "Error: %d", err);
256273
}
257274

275+
err = stepper_drv_set_event_cb(dev, print_stepper_drv_event_cb, (void *)sh);
276+
if (err) {
277+
shell_error(sh, "Failed to set stepper driver event callback: %d", err);
278+
}
258279
return err;
259280
}
260281

drivers/stepper/ti/drv84xx.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ struct drv84xx_data {
5050
struct drv84xx_pin_states pin_states;
5151
enum stepper_micro_step_resolution ustep_res;
5252
struct gpio_callback fault_cb_data;
53-
stepper_drv_fault_cb_t fault_cb;
53+
stepper_drv_event_cb_t fault_cb;
5454
void *fault_cb_user_data;
5555
};
5656

@@ -255,7 +255,7 @@ static int drv84xx_disable(const struct device *dev)
255255
return ret;
256256
}
257257

258-
static int drv84xx_set_fault_cb(const struct device *dev, stepper_drv_fault_cb_t fault_cb,
258+
static int drv84xx_set_fault_cb(const struct device *dev, stepper_drv_event_cb_t fault_cb,
259259
void *user_data)
260260
{
261261
struct drv84xx_data *data = dev->data;
@@ -360,7 +360,8 @@ void fault_event(const struct device *dev, struct gpio_callback *cb, uint32_t pi
360360
struct drv84xx_data *data = CONTAINER_OF(cb, struct drv84xx_data, fault_cb_data);
361361

362362
if (data->fault_cb != NULL) {
363-
data->fault_cb(data->dev, data->fault_cb_user_data);
363+
data->fault_cb(data->dev, STEPPER_DRV_EVENT_FAULT_DETECTED,
364+
data->fault_cb_user_data);
364365
} else {
365366
LOG_WRN_ONCE("%s: Fault pin triggered but no callback is set", dev->name);
366367
}
@@ -450,7 +451,7 @@ static int drv84xx_init(const struct device *dev)
450451
static DEVICE_API(stepper_drv, drv84xx_stepper_api) = {
451452
.enable = drv84xx_enable,
452453
.disable = drv84xx_disable,
453-
.set_fault_cb = drv84xx_set_fault_cb,
454+
.set_event_cb = drv84xx_set_fault_cb,
454455
.set_micro_step_res = drv84xx_set_micro_step_res,
455456
.get_micro_step_res = drv84xx_get_micro_step_res,
456457
.step = step_dir_stepper_common_step,

include/zephyr/drivers/stepper.h

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,12 @@ enum stepper_run_mode {
105105
enum stepper_event {
106106
/** Steps set using move_by or move_to have been executed */
107107
STEPPER_EVENT_STEPS_COMPLETED = 0,
108-
/** Stall detected */
109-
STEPPER_EVENT_STALL_DETECTED = 1,
110108
/** Left end switch status changes to pressed */
111-
STEPPER_EVENT_LEFT_END_STOP_DETECTED = 2,
109+
STEPPER_EVENT_LEFT_END_STOP_DETECTED = 1,
112110
/** Right end switch status changes to pressed */
113-
STEPPER_EVENT_RIGHT_END_STOP_DETECTED = 3,
111+
STEPPER_EVENT_RIGHT_END_STOP_DETECTED = 2,
114112
/** Stepper has stopped */
115-
STEPPER_EVENT_STOPPED = 4,
113+
STEPPER_EVENT_STOPPED = 3,
116114
};
117115

118116
/**
@@ -480,6 +478,13 @@ static inline int z_impl_stepper_is_moving(const struct device *dev, const uint8
480478
* @{
481479
*/
482480

481+
enum stepper_drv_event {
482+
/** Stepper driver stall detected */
483+
STEPPER_DRV_EVENT_STALL_DETETCTED = 0,
484+
/** Stepper driver fault detected */
485+
STEPPER_DRV_EVENT_FAULT_DETECTED = 1,
486+
};
487+
483488
/**
484489
* @cond INTERNAL_HIDDEN
485490
*
@@ -533,17 +538,18 @@ typedef int (*stepper_drv_get_micro_step_res_t)(const struct device *dev,
533538
enum stepper_micro_step_resolution *resolution);
534539

535540
/**
536-
* @brief Callback function for stepper fault events
541+
* @brief Callback function for stepper driver events
537542
*/
538-
typedef int (*stepper_drv_fault_cb_t)(const struct device *dev, void *user_data);
543+
typedef void (*stepper_drv_event_cb_t)(const struct device *dev, const enum stepper_drv_event event,
544+
void *user_data);
539545

540546
/**
541547
* @brief Set the callback function to be called when a stepper driver fault occurs
542548
*
543549
* @see stepper_drv_set_fault_callback() for details.
544550
*/
545-
typedef int (*stepper_drv_set_fault_callback_t)(const struct device *dev,
546-
stepper_drv_fault_cb_t callback, void *user_data);
551+
typedef int (*stepper_drv_set_event_callback_t)(const struct device *dev,
552+
stepper_drv_event_cb_t callback, void *user_data);
547553

548554
/**
549555
* @brief Stepper DRV Driver API
@@ -555,7 +561,7 @@ __subsystem struct stepper_drv_driver_api {
555561
stepper_drv_step_t step;
556562
stepper_drv_set_micro_step_res_t set_micro_step_res;
557563
stepper_drv_get_micro_step_res_t get_micro_step_res;
558-
stepper_drv_set_fault_callback_t set_fault_cb;
564+
stepper_drv_set_event_callback_t set_event_cb;
559565
};
560566

561567
/**
@@ -718,20 +724,20 @@ static inline int z_impl_stepper_drv_get_micro_step_res(const struct device *dev
718724
* @retval -ENOSYS If not implemented by device driver
719725
* @retval 0 Success
720726
*/
721-
__syscall int stepper_drv_set_fault_cb(const struct device *dev, stepper_drv_fault_cb_t callback,
727+
__syscall int stepper_drv_set_event_cb(const struct device *dev, stepper_drv_event_cb_t callback,
722728
void *user_data);
723729

724-
static inline int z_impl_stepper_drv_set_fault_cb(const struct device *dev,
725-
stepper_drv_fault_cb_t callback, void *user_data)
730+
static inline int z_impl_stepper_drv_set_event_cb(const struct device *dev,
731+
stepper_drv_event_cb_t cb, void *user_data)
726732
{
727733
__ASSERT_NO_MSG(dev != NULL);
728734
const struct stepper_drv_driver_api *api = (const struct stepper_drv_driver_api *)dev->api;
729735

730-
if (api->set_fault_cb == NULL) {
736+
if (api->set_event_cb == NULL) {
731737
return -ENOSYS;
732738
}
733739

734-
return api->set_fault_cb(dev, callback, user_data);
740+
return api->set_event_cb(dev, cb, user_data);
735741
}
736742

737743
/**

include/zephyr/drivers/stepper/stepper_fake.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ DECLARE_FAKE_VALUE_FUNC(int, fake_stepper_set_micro_step_res, const struct devic
2424
DECLARE_FAKE_VALUE_FUNC(int, fake_stepper_get_micro_step_res, const struct device *,
2525
enum stepper_micro_step_resolution *);
2626

27+
DECLARE_FAKE_VALUE_FUNC(void, fake_stepper_drv_set_event_callback, const struct device *,
28+
stepper_event_callback_t, void *);
29+
2730
DECLARE_FAKE_VALUE_FUNC(int, fake_stepper_move_by, const struct device *, uint8_t, int32_t);
2831

2932
DECLARE_FAKE_VALUE_FUNC(int, fake_stepper_set_microstep_interval, const struct device *, uint8_t,

tests/drivers/stepper/drv84xx/api/src/main.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ static void drv84xx_api_print_event_callback(const struct device *dev, const uin
3333
case STEPPER_EVENT_RIGHT_END_STOP_DETECTED:
3434
k_poll_signal_raise(&stepper_signal, STEPPER_EVENT_RIGHT_END_STOP_DETECTED);
3535
break;
36-
case STEPPER_EVENT_STALL_DETECTED:
37-
k_poll_signal_raise(&stepper_signal, STEPPER_EVENT_STALL_DETECTED);
38-
break;
3936
default:
4037
break;
4138
}

tests/drivers/stepper/stepper_api/src/main.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ static void stepper_print_event_callback(const struct device *dev, const uint8_t
4646
case STEPPER_EVENT_RIGHT_END_STOP_DETECTED:
4747
k_poll_signal_raise(&stepper_signal, STEPPER_EVENT_RIGHT_END_STOP_DETECTED);
4848
break;
49-
case STEPPER_EVENT_STALL_DETECTED:
50-
k_poll_signal_raise(&stepper_signal, STEPPER_EVENT_STALL_DETECTED);
51-
break;
5249
case STEPPER_EVENT_STOPPED:
5350
k_poll_signal_raise(&stepper_signal, STEPPER_EVENT_STOPPED);
5451
break;

0 commit comments

Comments
 (0)