drivers: stepper: api: rename enable_constant_velocity_mode to run

rename enable_constant_velocity_mode to run in following files:
- include/zephyr/drivers/stepper.h
- include/zephyr/drivers/stepper/stepper_fake.h
- doc/hardware/peripherals/stepper.rst
- doc/releases/migration-guide-4.1.rst
- drivers/stepper/adi_tmc/adi_tmc5041_stepper_controller.c
- drivers/stepper/fake_stepper_controller.c
- drivers/stepper/gpio_stepper_controller.c
- drivers/stepper/stepper_shell.c
- tests/drivers/stepper/shell/src/main.c

Signed-off-by: Jilay Pandya <jilay.pandya@outlook.com>
This commit is contained in:
Jilay Pandya 2024-11-27 17:26:17 +01:00 committed by Benjamin Cabé
commit af68d97507
9 changed files with 48 additions and 58 deletions

View file

@ -22,7 +22,7 @@ Control Stepper
- **Move to** a specific position also known as **absolute movement** - **Move to** a specific position also known as **absolute movement**
using :c:func:`stepper_set_target_position`. using :c:func:`stepper_set_target_position`.
- Run continuously with a **constant velocity** in a specific direction until - Run continuously with a **constant velocity** in a specific direction until
a stop is detected using :c:func:`stepper_enable_constant_velocity_mode`. a stop is detected using :c:func:`stepper_run`.
- Check if the stepper is **moving** using :c:func:`stepper_is_moving`. - Check if the stepper is **moving** using :c:func:`stepper_is_moving`.
- Register an **event callback** using :c:func:`stepper_set_event_callback`. - Register an **event callback** using :c:func:`stepper_set_event_callback`.

View file

@ -163,6 +163,7 @@ Stepper
* Renamed the ``compatible`` from ``zephyr,gpio-steppers`` to :dtcompatible:`zephyr,gpio-stepper`. * Renamed the ``compatible`` from ``zephyr,gpio-steppers`` to :dtcompatible:`zephyr,gpio-stepper`.
* Renamed the ``stepper_set_actual_position`` function to :c:func:`stepper_set_reference_position`. * Renamed the ``stepper_set_actual_position`` function to :c:func:`stepper_set_reference_position`.
* Renamed the ``stepper_enable_constant_velocity_mode`` function to :c:func:`stepper_run`.
SPI SPI
=== ===

View file

@ -477,11 +477,10 @@ static int tmc5041_stepper_set_target_position(const struct device *dev, const i
return 0; return 0;
} }
static int tmc5041_stepper_enable_constant_velocity_mode(const struct device *dev, static int tmc5041_stepper_run(const struct device *dev, const enum stepper_direction direction,
const enum stepper_direction direction, const uint32_t velocity)
const uint32_t velocity)
{ {
LOG_DBG("Stepper motor controller %s enable constant velocity mode", dev->name); LOG_DBG("Stepper motor controller %s run with velocity %d", dev->name, velocity);
const struct tmc5041_stepper_config *config = dev->config; const struct tmc5041_stepper_config *config = dev->config;
const struct tmc5041_config *tmc5041_config = config->controller->config; const struct tmc5041_config *tmc5041_config = config->controller->config;
struct tmc5041_stepper_data *data = dev->data; struct tmc5041_stepper_data *data = dev->data;
@ -731,7 +730,7 @@ static int tmc5041_stepper_init(const struct device *dev)
.set_reference_position = tmc5041_stepper_set_reference_position, \ .set_reference_position = tmc5041_stepper_set_reference_position, \
.get_actual_position = tmc5041_stepper_get_actual_position, \ .get_actual_position = tmc5041_stepper_get_actual_position, \
.set_target_position = tmc5041_stepper_set_target_position, \ .set_target_position = tmc5041_stepper_set_target_position, \
.enable_constant_velocity_mode = tmc5041_stepper_enable_constant_velocity_mode, \ .run = tmc5041_stepper_run, \
.set_event_callback = tmc5041_stepper_set_event_callback, }; .set_event_callback = tmc5041_stepper_set_event_callback, };
#define TMC5041_STEPPER_DEFINE(child) \ #define TMC5041_STEPPER_DEFINE(child) \

View file

@ -39,8 +39,8 @@ DEFINE_FAKE_VALUE_FUNC(int, fake_stepper_get_actual_position, const struct devic
DEFINE_FAKE_VALUE_FUNC(int, fake_stepper_set_target_position, const struct device *, int32_t); DEFINE_FAKE_VALUE_FUNC(int, fake_stepper_set_target_position, const struct device *, int32_t);
DEFINE_FAKE_VALUE_FUNC(int, fake_stepper_enable_constant_velocity_mode, const struct device *, DEFINE_FAKE_VALUE_FUNC(int, fake_stepper_run, const struct device *, enum stepper_direction,
enum stepper_direction, uint32_t); uint32_t);
DEFINE_FAKE_VALUE_FUNC(int, fake_stepper_set_event_callback, const struct device *, DEFINE_FAKE_VALUE_FUNC(int, fake_stepper_set_event_callback, const struct device *,
stepper_event_callback_t, void *); stepper_event_callback_t, void *);
@ -98,7 +98,7 @@ static void fake_stepper_reset_rule_before(const struct ztest_unit_test *test, v
RESET_FAKE(fake_stepper_set_reference_position); RESET_FAKE(fake_stepper_set_reference_position);
RESET_FAKE(fake_stepper_get_actual_position); RESET_FAKE(fake_stepper_get_actual_position);
RESET_FAKE(fake_stepper_set_target_position); RESET_FAKE(fake_stepper_set_target_position);
RESET_FAKE(fake_stepper_enable_constant_velocity_mode); RESET_FAKE(fake_stepper_run);
/* Install custom fakes for the setter and getter functions */ /* Install custom fakes for the setter and getter functions */
fake_stepper_set_micro_step_res_fake.custom_fake = fake_stepper_set_micro_step_res_delegate; fake_stepper_set_micro_step_res_fake.custom_fake = fake_stepper_set_micro_step_res_delegate;
@ -134,7 +134,7 @@ static DEVICE_API(stepper, fake_stepper_driver_api) = {
.set_reference_position = fake_stepper_set_reference_position, .set_reference_position = fake_stepper_set_reference_position,
.get_actual_position = fake_stepper_get_actual_position, .get_actual_position = fake_stepper_get_actual_position,
.set_target_position = fake_stepper_set_target_position, .set_target_position = fake_stepper_set_target_position,
.enable_constant_velocity_mode = fake_stepper_enable_constant_velocity_mode, .run = fake_stepper_run,
.set_event_callback = fake_stepper_set_event_callback, .set_event_callback = fake_stepper_set_event_callback,
}; };

View file

@ -261,17 +261,16 @@ static int gpio_stepper_set_max_velocity(const struct device *dev, uint32_t velo
return 0; return 0;
} }
static int gpio_stepper_enable_constant_velocity_mode(const struct device *dev, static int gpio_stepper_run(const struct device *dev, const enum stepper_direction direction,
const enum stepper_direction direction, const uint32_t velocity)
const uint32_t value)
{ {
struct gpio_stepper_data *data = dev->data; struct gpio_stepper_data *data = dev->data;
K_SPINLOCK(&data->lock) { K_SPINLOCK(&data->lock) {
data->run_mode = STEPPER_RUN_MODE_VELOCITY; data->run_mode = STEPPER_RUN_MODE_VELOCITY;
data->direction = direction; data->direction = direction;
if (value != 0) { if (velocity != 0) {
data->delay_in_us = USEC_PER_SEC / value; data->delay_in_us = USEC_PER_SEC / velocity;
(void)k_work_reschedule(&data->stepper_dwork, K_NO_WAIT); (void)k_work_reschedule(&data->stepper_dwork, K_NO_WAIT);
} else { } else {
(void)k_work_cancel_delayable(&data->stepper_dwork); (void)k_work_cancel_delayable(&data->stepper_dwork);
@ -360,7 +359,7 @@ static DEVICE_API(stepper, gpio_stepper_api) = {
.get_actual_position = gpio_stepper_get_actual_position, .get_actual_position = gpio_stepper_get_actual_position,
.set_target_position = gpio_stepper_set_target_position, .set_target_position = gpio_stepper_set_target_position,
.set_max_velocity = gpio_stepper_set_max_velocity, .set_max_velocity = gpio_stepper_set_max_velocity,
.enable_constant_velocity_mode = gpio_stepper_enable_constant_velocity_mode, .run = gpio_stepper_run,
.set_micro_step_res = gpio_stepper_set_micro_step_res, .set_micro_step_res = gpio_stepper_set_micro_step_res,
.get_micro_step_res = gpio_stepper_get_micro_step_res, .get_micro_step_res = gpio_stepper_get_micro_step_res,
.set_event_callback = gpio_stepper_set_event_callback, .set_event_callback = gpio_stepper_set_event_callback,

View file

@ -366,8 +366,7 @@ static int cmd_stepper_set_target_position(const struct shell *sh, size_t argc,
return err; return err;
} }
static int cmd_stepper_enable_constant_velocity_mode(const struct shell *sh, size_t argc, static int cmd_stepper_run(const struct shell *sh, size_t argc, char **argv)
char **argv)
{ {
const struct device *dev; const struct device *dev;
int err = -EINVAL; int err = -EINVAL;
@ -401,7 +400,7 @@ static int cmd_stepper_enable_constant_velocity_mode(const struct shell *sh, siz
shell_error(sh, "Failed to set callback: %d", err); shell_error(sh, "Failed to set callback: %d", err);
} }
err = stepper_enable_constant_velocity_mode(dev, direction, velocity); err = stepper_run(dev, direction, velocity);
if (err) { if (err) {
shell_error(sh, "Error: %d", err); shell_error(sh, "Error: %d", err);
return err; return err;
@ -468,9 +467,8 @@ SHELL_STATIC_SUBCMD_SET_CREATE(
cmd_stepper_get_actual_position, 2, 0), cmd_stepper_get_actual_position, 2, 0),
SHELL_CMD_ARG(set_target_position, &dsub_pos_stepper_motor_name, "<device> <micro_steps>", SHELL_CMD_ARG(set_target_position, &dsub_pos_stepper_motor_name, "<device> <micro_steps>",
cmd_stepper_set_target_position, 3, 0), cmd_stepper_set_target_position, 3, 0),
SHELL_CMD_ARG(enable_constant_velocity_mode, &dsub_pos_stepper_motor_name_dir, SHELL_CMD_ARG(run, &dsub_pos_stepper_motor_name_dir, "<device> <direction> <velocity>",
"<device> <direction> <velocity>", cmd_stepper_enable_constant_velocity_mode, cmd_stepper_run, 4, 0),
4, 0),
SHELL_CMD_ARG(info, &dsub_pos_stepper_motor_name, "<device>", cmd_stepper_info, 2, 0), SHELL_CMD_ARG(info, &dsub_pos_stepper_motor_name, "<device>", cmd_stepper_info, 2, 0),
SHELL_SUBCMD_SET_END); SHELL_SUBCMD_SET_END);

View file

@ -164,13 +164,12 @@ typedef int (*stepper_set_target_position_t)(const struct device *dev, const int
typedef int (*stepper_is_moving_t)(const struct device *dev, bool *is_moving); typedef int (*stepper_is_moving_t)(const struct device *dev, bool *is_moving);
/** /**
* @brief Enable constant velocity mode for the stepper with a given velocity * @brief Run the stepper with a given velocity in a given direction
* *
* @see stepper_enable_constant_velocity_mode() for details. * @see stepper_run() for details.
*/ */
typedef int (*stepper_enable_constant_velocity_mode_t)(const struct device *dev, typedef int (*stepper_run_t)(const struct device *dev, const enum stepper_direction direction,
const enum stepper_direction direction, const uint32_t value);
const uint32_t value);
/** /**
* @brief Callback function for stepper events * @brief Callback function for stepper events
@ -199,7 +198,7 @@ __subsystem struct stepper_driver_api {
stepper_get_actual_position_t get_actual_position; stepper_get_actual_position_t get_actual_position;
stepper_set_target_position_t set_target_position; stepper_set_target_position_t set_target_position;
stepper_is_moving_t is_moving; stepper_is_moving_t is_moving;
stepper_enable_constant_velocity_mode_t enable_constant_velocity_mode; stepper_run_t run;
stepper_set_event_callback_t set_event_callback; stepper_set_event_callback_t set_event_callback;
}; };
@ -408,36 +407,34 @@ static inline int z_impl_stepper_is_moving(const struct device *dev, bool *is_mo
} }
/** /**
* @brief Enable constant velocity mode for the stepper with a given velocity * @brief Run the stepper with a given velocity in a given direction
* *
* @details activate constant velocity mode with the given velocity in micro_steps_per_second. * @details If velocity > 0, motor shall be set into motion and run incessantly until and unless
* If velocity > 0, motor shall be set into motion and run incessantly until and unless stalled or * stalled or stopped using some other command, for instance, motor_enable(false).
* stopped using some other command, for instance, motor_enable(false).
* *
* @param dev pointer to the stepper motor controller instance * @param dev pointer to the stepper motor controller instance
* @param direction The direction to set * @param direction The direction to set
* @param value The velocity to set in steps per second where one step is dependent on the current * @param velocity The velocity to set in microsteps per second
* microstepping resolution: * - > 0: Run the stepper with the given velocity in a given direction
* > 0: Enable constant velocity mode with the given velocity in a given direction * - 0: Stop the stepper
* 0: Disable constant velocity mode
* *
* @retval -EIO General input / output error * @retval -EIO General input / output error
* @retval -ENOSYS If not implemented by device driver * @retval -ENOSYS If not implemented by device driver
* @retval 0 Success * @retval 0 Success
*/ */
__syscall int stepper_enable_constant_velocity_mode(const struct device *dev, __syscall int stepper_run(const struct device *dev, enum stepper_direction direction,
enum stepper_direction direction, uint32_t velocity);
uint32_t value);
static inline int z_impl_stepper_enable_constant_velocity_mode( static inline int z_impl_stepper_run(const struct device *dev,
const struct device *dev, const enum stepper_direction direction, const uint32_t value) const enum stepper_direction direction,
const uint32_t velocity)
{ {
const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api; const struct stepper_driver_api *api = (const struct stepper_driver_api *)dev->api;
if (api->enable_constant_velocity_mode == NULL) { if (api->run == NULL) {
return -ENOSYS; return -ENOSYS;
} }
return api->enable_constant_velocity_mode(dev, direction, value); return api->run(dev, direction, velocity);
} }
/** /**

View file

@ -34,8 +34,8 @@ DECLARE_FAKE_VALUE_FUNC(int, fake_stepper_set_target_position, const struct devi
DECLARE_FAKE_VALUE_FUNC(int, fake_stepper_is_moving, const struct device *, bool *); DECLARE_FAKE_VALUE_FUNC(int, fake_stepper_is_moving, const struct device *, bool *);
DECLARE_FAKE_VALUE_FUNC(int, fake_stepper_enable_constant_velocity_mode, const struct device *, DECLARE_FAKE_VALUE_FUNC(int, fake_stepper_run, const struct device *, enum stepper_direction,
enum stepper_direction, uint32_t); uint32_t);
DECLARE_FAKE_VALUE_FUNC(int, fake_stepper_set_event_callback, const struct device *, DECLARE_FAKE_VALUE_FUNC(int, fake_stepper_set_event_callback, const struct device *,
stepper_event_callback_t, void *); stepper_event_callback_t, void *);

View file

@ -127,27 +127,23 @@ ZTEST(stepper_shell, test_stepper_set_target_position)
"wrong target position value"); "wrong target position value");
} }
ZTEST(stepper_shell, test_stepper_enable_constant_velocity_mode) ZTEST(stepper_shell, test_stepper_run)
{ {
const struct shell *sh = shell_backend_dummy_get_ptr(); const struct shell *sh = shell_backend_dummy_get_ptr();
int err = shell_execute_cmd(sh, "stepper enable_constant_velocity_mode " FAKE_STEPPER_NAME int err = shell_execute_cmd(sh, "stepper run " FAKE_STEPPER_NAME " positive 200");
" positive 200");
ASSERT_STEPPER_FUNC_CALLED(fake_stepper_enable_constant_velocity_mode_fake, err); ASSERT_STEPPER_FUNC_CALLED(fake_stepper_run_fake, err);
zassert_equal(fake_stepper_enable_constant_velocity_mode_fake.arg1_val, zassert_equal(fake_stepper_run_fake.arg1_val, STEPPER_DIRECTION_POSITIVE,
STEPPER_DIRECTION_POSITIVE, "wrong direction value"); "wrong direction value");
zassert_equal(fake_stepper_enable_constant_velocity_mode_fake.arg2_val, 200, zassert_equal(fake_stepper_run_fake.arg2_val, 200, "wrong velocity value");
"wrong velocity value");
} }
ZTEST(stepper_shell, test_stepper_enable_constant_velocity_mode_invalid_direction) ZTEST(stepper_shell, test_stepper_run_invalid_direction)
{ {
const struct shell *sh = shell_backend_dummy_get_ptr(); const struct shell *sh = shell_backend_dummy_get_ptr();
int err = shell_execute_cmd(sh, "stepper enable_constant_velocity_mode " FAKE_STEPPER_NAME int err = shell_execute_cmd(sh, "stepper run " FAKE_STEPPER_NAME " foo 200");
" foo 200");
zassert_not_equal(err, 0, zassert_not_equal(err, 0, " executed run with invalid direction value");
" executed enable_constant_velocity_mode with invalid direction value");
} }
ZTEST(stepper_shell, test_stepper_info) ZTEST(stepper_shell, test_stepper_info)