Skip to content

Commit 4838c0f

Browse files
jilaypandyakartben
authored andcommitted
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 <[email protected]>
1 parent 18c6d61 commit 4838c0f

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

drivers/stepper/adi_tmc/adi_tmc50xx_stepper_controller.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ static int tmc50xx_stepper_is_moving(const struct device *dev, bool *is_moving)
305305
return -EIO;
306306
}
307307

308-
*is_moving = (FIELD_GET(TMC5XXX_DRV_STATUS_STST_BIT, reg_value) != 1U);
308+
*is_moving = (FIELD_GET(TMC5XXX_DRV_STATUS_STST_BIT, reg_value) == 1U);
309309
LOG_DBG("Stepper motor controller %s is moving: %d", dev->name, *is_moving);
310310
return 0;
311311
}
@@ -378,6 +378,11 @@ int tmc50xx_stepper_set_max_velocity(const struct device *dev, uint32_t velocity
378378
static int tmc50xx_stepper_set_micro_step_res(const struct device *dev,
379379
enum stepper_micro_step_resolution res)
380380
{
381+
if (!VALID_MICRO_STEP_RES(res)) {
382+
LOG_ERR("Invalid micro step resolution %d", res);
383+
return -ENOTSUP;
384+
}
385+
381386
const struct tmc50xx_stepper_config *config = dev->config;
382387
uint32_t reg_value;
383388
int err;

include/zephyr/drivers/stepper.h

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,6 @@
2929
extern "C" {
3030
#endif
3131

32-
/**
33-
* @brief Macro to calculate the index of the microstep resolution
34-
* @param res Microstep resolution
35-
*/
36-
#define MICRO_STEP_RES_INDEX(res) LOG2(res)
37-
3832
/**
3933
* @brief Stepper Motor micro-step resolution options
4034
*/
@@ -59,6 +53,19 @@ enum stepper_micro_step_resolution {
5953
STEPPER_MICRO_STEP_256 = 256,
6054
};
6155

56+
/**
57+
* @brief Macro to calculate the index of the microstep resolution
58+
* @param res Microstep resolution
59+
*/
60+
#define MICRO_STEP_RES_INDEX(res) LOG2(res)
61+
62+
#define VALID_MICRO_STEP_RES(res) \
63+
((res) == STEPPER_MICRO_STEP_1 || (res) == STEPPER_MICRO_STEP_2 || \
64+
(res) == STEPPER_MICRO_STEP_4 || (res) == STEPPER_MICRO_STEP_8 || \
65+
(res) == STEPPER_MICRO_STEP_16 || (res) == STEPPER_MICRO_STEP_32 || \
66+
(res) == STEPPER_MICRO_STEP_64 || (res) == STEPPER_MICRO_STEP_128 || \
67+
(res) == STEPPER_MICRO_STEP_256)
68+
6269
/**
6370
* @brief Stepper Motor direction options
6471
*/

tests/drivers/stepper/stepper_api/src/main.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,25 +106,35 @@ ZTEST_F(stepper, test_get_micro_step_res)
106106
ZTEST_F(stepper, test_set_micro_step_interval_invalid_zero)
107107
{
108108
int err = stepper_set_microstep_interval(fixture->dev, 0);
109-
109+
if (err == -ENOSYS) {
110+
ztest_test_skip();
111+
}
110112
zassert_equal(err, -EINVAL, "ustep interval cannot be zero");
111113
}
112114

113115
ZTEST_F(stepper, test_actual_position)
114116
{
115117
int32_t pos = 100u;
118+
int ret;
116119

117-
(void)stepper_set_reference_position(fixture->dev, pos);
118-
(void)stepper_get_actual_position(fixture->dev, &pos);
120+
ret = stepper_set_reference_position(fixture->dev, pos);
121+
zassert_equal(ret, 0, "Failed to set reference position");
122+
123+
ret = stepper_get_actual_position(fixture->dev, &pos);
124+
zassert_equal(ret, 0, "Failed to get actual position");
119125
zassert_equal(pos, 100u, "Actual position not set correctly");
120126
}
121127

122128
ZTEST_F(stepper, test_target_position_w_fixed_step_interval)
123129
{
124130
int32_t pos = 10u;
131+
int ret;
125132

126-
(void)stepper_set_microstep_interval(fixture->dev, 100 * USEC_PER_SEC);
133+
ret = stepper_set_microstep_interval(fixture->dev, 100 * USEC_PER_SEC);
127134

135+
if (ret == -ENOSYS) {
136+
ztest_test_skip();
137+
}
128138
/* Pass the function name as user data */
129139
(void)stepper_set_event_callback(fixture->dev, fixture->callback, (void *)fixture->dev);
130140

0 commit comments

Comments
 (0)