drivers: stepper: introduce valid micro-step resolution check

Introduce macro to check for valid microstep resolution in stepper api
Use this macro in tmc50xx driver. Stepper api tests adjusted in order to
accomodate the not implemented stepper api functions.

Signed-off-by: Jilay Pandya <jilay.pandya@outlook.com>
This commit is contained in:
Jilay Pandya 2025-04-03 18:05:11 +02:00 committed by Benjamin Cabé
parent 18c6d616c1
commit 4838c0f200
3 changed files with 33 additions and 11 deletions

View File

@ -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;

View File

@ -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
*/

View File

@ -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);