diff --git a/drivers/stepper/adi_tmc/adi_tmc50xx_stepper_controller.c b/drivers/stepper/adi_tmc/adi_tmc50xx_stepper_controller.c index 607fa59a051..dd54db9d139 100644 --- a/drivers/stepper/adi_tmc/adi_tmc50xx_stepper_controller.c +++ b/drivers/stepper/adi_tmc/adi_tmc50xx_stepper_controller.c @@ -305,7 +305,7 @@ static int tmc50xx_stepper_is_moving(const struct device *dev, bool *is_moving) return -EIO; } - *is_moving = (FIELD_GET(TMC5XXX_DRV_STATUS_STST_BIT, reg_value) != 1U); + *is_moving = (FIELD_GET(TMC5XXX_DRV_STATUS_STST_BIT, reg_value) == 1U); LOG_DBG("Stepper motor controller %s is moving: %d", dev->name, *is_moving); return 0; } @@ -378,6 +378,11 @@ int tmc50xx_stepper_set_max_velocity(const struct device *dev, uint32_t velocity static int tmc50xx_stepper_set_micro_step_res(const struct device *dev, enum stepper_micro_step_resolution res) { + if (!VALID_MICRO_STEP_RES(res)) { + LOG_ERR("Invalid micro step resolution %d", res); + return -ENOTSUP; + } + const struct tmc50xx_stepper_config *config = dev->config; uint32_t reg_value; int err; diff --git a/include/zephyr/drivers/stepper.h b/include/zephyr/drivers/stepper.h index 4bfd7e541c6..42e635279c3 100644 --- a/include/zephyr/drivers/stepper.h +++ b/include/zephyr/drivers/stepper.h @@ -29,12 +29,6 @@ extern "C" { #endif -/** - * @brief Macro to calculate the index of the microstep resolution - * @param res Microstep resolution - */ -#define MICRO_STEP_RES_INDEX(res) LOG2(res) - /** * @brief Stepper Motor micro-step resolution options */ @@ -59,6 +53,19 @@ enum stepper_micro_step_resolution { STEPPER_MICRO_STEP_256 = 256, }; +/** + * @brief Macro to calculate the index of the microstep resolution + * @param res Microstep resolution + */ +#define MICRO_STEP_RES_INDEX(res) LOG2(res) + +#define VALID_MICRO_STEP_RES(res) \ + ((res) == STEPPER_MICRO_STEP_1 || (res) == STEPPER_MICRO_STEP_2 || \ + (res) == STEPPER_MICRO_STEP_4 || (res) == STEPPER_MICRO_STEP_8 || \ + (res) == STEPPER_MICRO_STEP_16 || (res) == STEPPER_MICRO_STEP_32 || \ + (res) == STEPPER_MICRO_STEP_64 || (res) == STEPPER_MICRO_STEP_128 || \ + (res) == STEPPER_MICRO_STEP_256) + /** * @brief Stepper Motor direction options */ diff --git a/tests/drivers/stepper/stepper_api/src/main.c b/tests/drivers/stepper/stepper_api/src/main.c index c2a2b69ad95..6d264712cfc 100644 --- a/tests/drivers/stepper/stepper_api/src/main.c +++ b/tests/drivers/stepper/stepper_api/src/main.c @@ -106,25 +106,35 @@ ZTEST_F(stepper, test_get_micro_step_res) ZTEST_F(stepper, test_set_micro_step_interval_invalid_zero) { int err = stepper_set_microstep_interval(fixture->dev, 0); - + if (err == -ENOSYS) { + ztest_test_skip(); + } zassert_equal(err, -EINVAL, "ustep interval cannot be zero"); } ZTEST_F(stepper, test_actual_position) { int32_t pos = 100u; + int ret; - (void)stepper_set_reference_position(fixture->dev, pos); - (void)stepper_get_actual_position(fixture->dev, &pos); + ret = stepper_set_reference_position(fixture->dev, pos); + zassert_equal(ret, 0, "Failed to set reference position"); + + ret = stepper_get_actual_position(fixture->dev, &pos); + zassert_equal(ret, 0, "Failed to get actual position"); zassert_equal(pos, 100u, "Actual position not set correctly"); } ZTEST_F(stepper, test_target_position_w_fixed_step_interval) { int32_t pos = 10u; + int ret; - (void)stepper_set_microstep_interval(fixture->dev, 100 * USEC_PER_SEC); + ret = stepper_set_microstep_interval(fixture->dev, 100 * USEC_PER_SEC); + if (ret == -ENOSYS) { + ztest_test_skip(); + } /* Pass the function name as user data */ (void)stepper_set_event_callback(fixture->dev, fixture->callback, (void *)fixture->dev);