Skip to content

Commit

Permalink
tests: drivers: stepper: stepper_api: test cb user_data
Browse files Browse the repository at this point in the history
This commit tests set_callback and user_data

Signed-off-by: Jilay Pandya <[email protected]>
  • Loading branch information
jilaypandya committed Oct 19, 2024
1 parent 6f64f77 commit caa5ff5
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion drivers/stepper/adi_tmc/adi_tmc5041_stepper_controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ static void execute_callback(const struct device *dev, const enum stepper_event
LOG_WRN_ONCE("No callback registered");
return;
}
data->callback(dev, event);
data->callback(dev, event, data->event_cb_user_data);
}

static void rampstat_work_handler(struct k_work *work)
Expand Down
4 changes: 2 additions & 2 deletions drivers/stepper/gpio_stepper_controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ struct gpio_stepper_data {
uint8_t step_gap;
uint8_t coil_charge;
struct k_work_delayable stepper_dwork;
stepper_event_callback_t callback;
int32_t actual_position;
uint32_t delay_in_us;
int32_t step_count;
stepper_event_callback_t callback;
void *event_cb_user_data;
};

Expand Down Expand Up @@ -84,7 +84,7 @@ static void update_remaining_steps(struct gpio_stepper_data *data)
LOG_WRN_ONCE("No callback set");
return;
}
data->callback(data->dev, STEPPER_EVENT_STEPS_COMPLETED);
data->callback(data->dev, STEPPER_EVENT_STEPS_COMPLETED, data->event_cb_user_data);
}
}

Expand Down
3 changes: 2 additions & 1 deletion include/zephyr/drivers/stepper.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ typedef int (*stepper_enable_constant_velocity_mode_t)(const struct device *dev,
/**
* @brief Callback function for stepper events
*/
typedef void (*stepper_event_callback_t)(const struct device *dev, const enum stepper_event event);
typedef void (*stepper_event_callback_t)(const struct device *dev, const enum stepper_event event,
void *user_data);

/**
* @brief Set the callback function to be called when a stepper event occurs
Expand Down
13 changes: 11 additions & 2 deletions tests/drivers/stepper/stepper_api/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ struct stepper_fixture {

struct k_poll_signal stepper_signal;
struct k_poll_event stepper_event;
void *user_data_received;

static void stepper_print_event_callback(const struct device *dev, enum stepper_event event)
static void stepper_print_event_callback(const struct device *dev, enum stepper_event event,
void *user_data)
{

Check notice on line 21 in tests/drivers/stepper/stepper_api/src/main.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

tests/drivers/stepper/stepper_api/src/main.c:21 - void *user_data) + void *user_data)

Check notice on line 21 in tests/drivers/stepper/stepper_api/src/main.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

tests/drivers/stepper/stepper_api/src/main.c:21 - void *user_data) + void *user_data)
user_data_received = user_data;
switch (event) {
case STEPPER_EVENT_STEPS_COMPLETED:
k_poll_signal_raise(&stepper_signal, STEPPER_EVENT_STEPS_COMPLETED);
Expand Down Expand Up @@ -80,8 +83,13 @@ ZTEST_F(stepper, test_target_position)
int32_t pos = 100u;

(void)stepper_set_max_velocity(fixture->dev, 100u);
(void)stepper_set_callback(fixture->dev, fixture->callback, NULL);

/* Pass the function name as user data */
void *user_data_set = (void *)__func__;
(void)stepper_set_callback(fixture->dev, fixture->callback, user_data_set);

(void)stepper_set_target_position(fixture->dev, pos);

(void)k_poll(&stepper_event, 1, K_SECONDS(5));
unsigned int signaled;
int result;
Expand All @@ -91,4 +99,5 @@ ZTEST_F(stepper, test_target_position)
zassert_equal(result, STEPPER_EVENT_STEPS_COMPLETED, "Signal not set");
(void)stepper_get_actual_position(fixture->dev, &pos);
zassert_equal(pos, 100u, "Target position should be %d but is %d", 100u, pos);
zassert_equal(user_data_received, __func__, "User data not received");
}

0 comments on commit caa5ff5

Please sign in to comment.