Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drivers: stepper: api: add -ECANCELLED error code for move operations #83241

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions drivers/stepper/gpio_stepper_controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ struct gpio_stepper_data {
int32_t actual_position;
uint32_t delay_in_us;
int32_t step_count;
bool is_enabled;
stepper_event_callback_t callback;
void *event_cb_user_data;
};
Expand Down Expand Up @@ -181,6 +182,11 @@ static int gpio_stepper_move_by(const struct device *dev, int32_t micro_steps)
{
struct gpio_stepper_data *data = dev->data;

if (!data->is_enabled) {
LOG_ERR("Stepper motor is not enabled");
return -ECANCELED;
}

if (data->delay_in_us == 0) {
LOG_ERR("Velocity not set or invalid velocity set");
return -EINVAL;
Expand Down Expand Up @@ -218,6 +224,11 @@ static int gpio_stepper_move_to(const struct device *dev, int32_t micro_steps)
{
struct gpio_stepper_data *data = dev->data;

if (!data->is_enabled) {
LOG_ERR("Stepper motor is not enabled");
return -ECANCELED;
}

if (data->delay_in_us == 0) {
LOG_ERR("Velocity not set or invalid velocity set");
return -EINVAL;
Expand Down Expand Up @@ -266,6 +277,11 @@ static int gpio_stepper_run(const struct device *dev, const enum stepper_directi
{
struct gpio_stepper_data *data = dev->data;

if (!data->is_enabled) {
LOG_ERR("Stepper motor is not enabled");
return -ECANCELED;
}

K_SPINLOCK(&data->lock) {
data->run_mode = STEPPER_RUN_MODE_VELOCITY;
data->direction = direction;
Expand Down Expand Up @@ -323,6 +339,9 @@ static int gpio_stepper_enable(const struct device *dev, bool enable)
struct gpio_stepper_data *data = dev->data;

K_SPINLOCK(&data->lock) {

data->is_enabled = enable;

if (enable) {
(void)k_work_reschedule(&data->stepper_dwork, K_NO_WAIT);
} else {
Expand Down
3 changes: 3 additions & 0 deletions include/zephyr/drivers/stepper.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ static inline int z_impl_stepper_enable(const struct device *dev, const bool ena
* @param dev pointer to the stepper motor controller instance
* @param micro_steps target micro_steps to be moved from the current position
*
* @retval -ECANCELED If the stepper is disabled
* @retval -EIO General input / output error
* @retval 0 Success
*/
Expand Down Expand Up @@ -374,6 +375,7 @@ static inline int z_impl_stepper_get_actual_position(const struct device *dev, i
* @param dev pointer to the stepper motor controller instance
* @param micro_steps target position to set in micro_steps
*
* @retval -ECANCELED If the stepper is disabled
* @retval -EIO General input / output error
* @retval -ENOSYS If not implemented by device driver
* @retval 0 Success
Expand Down Expand Up @@ -425,6 +427,7 @@ static inline int z_impl_stepper_is_moving(const struct device *dev, bool *is_mo
* - > 0: Run the stepper with the given velocity in a given direction
* - 0: Stop the stepper
*
* @retval -ECANCELED If the stepper is disabled
* @retval -EIO General input / output error
* @retval -ENOSYS If not implemented by device driver
* @retval 0 Success
Expand Down
Loading