tests: drivers: stepper: stepper_api: test cb user_data
This commit does the following: 1. tests set_callback and user_data 2. fixes the api as well as the drivers by passing user_data back to the set callback Signed-off-by: Jilay Pandya <jilay.pandya@outlook.com>
This commit is contained in:
parent
fdae4d2e4f
commit
271aeaf5f9
6 changed files with 29 additions and 15 deletions
|
|
@ -2,5 +2,7 @@
|
|||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
zephyr_library()
|
||||
zephyr_library_property(ALLOW_EMPTY TRUE)
|
||||
|
||||
zephyr_library_sources_ifdef(CONFIG_STEPPER_ADI_TMC_SPI adi_tmc_spi.c)
|
||||
zephyr_library_sources_ifdef(CONFIG_STEPPER_ADI_TMC5041 adi_tmc5041_stepper_controller.c)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -9,9 +9,9 @@
|
|||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/sys_clock.h>
|
||||
#include <zephyr/drivers/stepper.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
#include <zephyr/sys/__assert.h>
|
||||
|
||||
#include <zephyr/logging/log.h>
|
||||
LOG_MODULE_REGISTER(gpio_stepper_motor_controller, CONFIG_STEPPER_LOG_LEVEL);
|
||||
|
||||
#define MAX_MICRO_STEP_RES STEPPER_MICRO_STEP_2
|
||||
|
|
@ -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;
|
||||
};
|
||||
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -40,23 +40,26 @@ struct stepper_direction_map {
|
|||
.microstep = _microstep, \
|
||||
}
|
||||
|
||||
static void print_callback(const struct device *dev, const enum stepper_event event)
|
||||
static void print_callback(const struct device *dev, const enum stepper_event event,
|
||||
void *user_data)
|
||||
{
|
||||
const struct shell *sh = user_data;
|
||||
|
||||
switch (event) {
|
||||
case STEPPER_EVENT_STEPS_COMPLETED:
|
||||
LOG_INF("%s: Steps completed.", dev->name);
|
||||
shell_info(sh, "%s: Steps completed.", dev->name);
|
||||
break;
|
||||
case STEPPER_EVENT_STALL_DETECTED:
|
||||
LOG_INF("%s: Stall detected.", dev->name);
|
||||
shell_info(sh, "%s: Stall detected.", dev->name);
|
||||
break;
|
||||
case STEPPER_EVENT_LEFT_END_STOP_DETECTED:
|
||||
LOG_INF("%s: Left limit switch pressed.", dev->name);
|
||||
shell_info(sh, "%s: Left limit switch pressed.", dev->name);
|
||||
break;
|
||||
case STEPPER_EVENT_RIGHT_END_STOP_DETECTED:
|
||||
LOG_INF("%s: Right limit switch pressed.", dev->name);
|
||||
shell_info(sh, "%s: Right limit switch pressed.", dev->name);
|
||||
break;
|
||||
default:
|
||||
LOG_INF("%s: Unknown signal received.", dev->name);
|
||||
shell_info(sh, "%s: Unknown signal received.", dev->name);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
@ -200,7 +203,7 @@ static int cmd_stepper_move(const struct shell *sh, size_t argc, char **argv)
|
|||
return err;
|
||||
}
|
||||
|
||||
err = stepper_set_callback(dev, print_callback, NULL);
|
||||
err = stepper_set_callback(dev, print_callback, (void *)sh);
|
||||
if (err != 0) {
|
||||
shell_error(sh, "Failed to set callback: %d", err);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
{
|
||||
user_data_received = user_data;
|
||||
switch (event) {
|
||||
case STEPPER_EVENT_STEPS_COMPLETED:
|
||||
k_poll_signal_raise(&stepper_signal, STEPPER_EVENT_STEPS_COMPLETED);
|
||||
|
|
@ -45,7 +48,7 @@ static void *stepper_setup(void)
|
|||
k_poll_signal_init(&stepper_signal);
|
||||
k_poll_event_init(&stepper_event, K_POLL_TYPE_SIGNAL, K_POLL_MODE_NOTIFY_ONLY,
|
||||
&stepper_signal);
|
||||
|
||||
user_data_received = NULL;
|
||||
zassert_not_null(fixture.dev);
|
||||
return &fixture;
|
||||
}
|
||||
|
|
@ -80,8 +83,12 @@ 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)stepper_set_callback(fixture->dev, fixture->callback, &fixture);
|
||||
|
||||
(void)stepper_set_target_position(fixture->dev, pos);
|
||||
|
||||
(void)k_poll(&stepper_event, 1, K_SECONDS(5));
|
||||
unsigned int signaled;
|
||||
int result;
|
||||
|
|
@ -91,4 +98,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, &fixture, "User data not received");
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue