Skip to content

Commit 26b2f12

Browse files
committed
drivers: stepper: unify msx-gpios
currently there are two different ways how microstepping gpios are handled in step/dir drivers. This PR aims to unify the two different approaches. Signed-off-by: Jilay Pandya <[email protected]>
1 parent f922014 commit 26b2f12

File tree

10 files changed

+87
-84
lines changed

10 files changed

+87
-84
lines changed

drivers/stepper/adi_tmc/adi_tmc22xx_stepper_controller.c

+8-10
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ LOG_MODULE_REGISTER(tmc22xx, CONFIG_STEPPER_LOG_LEVEL);
1414
struct tmc22xx_config {
1515
struct step_dir_stepper_common_config common;
1616
const struct gpio_dt_spec enable_pin;
17-
const struct gpio_dt_spec *msx_pins;
1817
enum stepper_micro_step_resolution *msx_resolutions;
1918
};
2019

@@ -48,7 +47,7 @@ static int tmc22xx_stepper_set_micro_step_res(const struct device *dev,
4847
const struct tmc22xx_config *config = dev->config;
4948
int ret;
5049

51-
if (!config->msx_pins) {
50+
if (!config->common.msx_pins) {
5251
LOG_ERR("Microstep resolution pins are not configured");
5352
return -ENODEV;
5453
}
@@ -58,13 +57,13 @@ static int tmc22xx_stepper_set_micro_step_res(const struct device *dev,
5857
continue;
5958
}
6059

61-
ret = gpio_pin_set_dt(&config->msx_pins[0], i & 0x01);
60+
ret = gpio_pin_set_dt(&config->common.msx_pins[0], i & 0x01);
6261
if (ret < 0) {
6362
LOG_ERR("Failed to set MS1 pin: %d", ret);
6463
return ret;
6564
}
6665

67-
ret = gpio_pin_set_dt(&config->msx_pins[1], (i & 0x02) >> 1);
66+
ret = gpio_pin_set_dt(&config->common.msx_pins[1], (i & 0x02) >> 1);
6867
if (ret < 0) {
6968
LOG_ERR("Failed to set MS2 pin: %d", ret);
7069
return ret;
@@ -93,12 +92,12 @@ static int tmc22xx_stepper_configure_msx_pins(const struct device *dev)
9392
int ret;
9493

9594
for (uint8_t i = 0; i < MSX_PIN_COUNT; i++) {
96-
if (!gpio_is_ready_dt(&config->msx_pins[i])) {
95+
if (!gpio_is_ready_dt(&config->common.msx_pins[i])) {
9796
LOG_ERR("MSX pin %u are not ready", i);
9897
return -ENODEV;
9998
}
10099

101-
ret = gpio_pin_configure_dt(&config->msx_pins[i], GPIO_OUTPUT);
100+
ret = gpio_pin_configure_dt(&config->common.msx_pins[i], GPIO_OUTPUT);
102101
if (ret < 0) {
103102
LOG_ERR("Failed to configure msx pin %u", i);
104103
return ret;
@@ -124,7 +123,7 @@ static int tmc22xx_stepper_init(const struct device *dev)
124123
return ret;
125124
}
126125

127-
if (config->msx_pins) {
126+
if (config->common.msx_pins) {
128127
ret = tmc22xx_stepper_configure_msx_pins(dev);
129128
if (ret < 0) {
130129
LOG_ERR("Failed to configure MSX pins: %d", ret);
@@ -176,11 +175,10 @@ static DEVICE_API(stepper, tmc22xx_stepper_api) = {
176175
)) \
177176
\
178177
static const struct tmc22xx_config tmc22xx_config_##inst = { \
179-
.common = STEP_DIR_STEPPER_DT_INST_COMMON_CONFIG_INIT(inst), \
178+
.common = STEP_DIR_STEPPER_DT_INST_COMMON_CONFIG_INIT( \
179+
inst, tmc22xx_stepper_msx_pins_##inst), \
180180
.enable_pin = GPIO_DT_SPEC_INST_GET(inst, en_gpios), \
181181
.msx_resolutions = msx_table, \
182-
IF_ENABLED(DT_INST_NODE_HAS_PROP(inst, msx_gpios), \
183-
(.msx_pins = tmc22xx_stepper_msx_pins_##inst)) \
184182
}; \
185183
static struct tmc22xx_data tmc22xx_data_##inst = { \
186184
.common = STEP_DIR_STEPPER_DT_INST_COMMON_DATA_INIT(inst), \

drivers/stepper/allegro/a4979.c

+29-23
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ struct a4979_config {
1717
const struct step_dir_stepper_common_config common;
1818
const struct gpio_dt_spec en_pin;
1919
const struct gpio_dt_spec reset_pin;
20-
const struct gpio_dt_spec m0_pin;
21-
const struct gpio_dt_spec m1_pin;
2220
};
2321

2422
struct a4979_data {
@@ -130,11 +128,11 @@ static int a4979_stepper_set_micro_step_res(const struct device *dev,
130128
return -ENOTSUP;
131129
}
132130

133-
ret = a4979_set_microstep_pin(dev, &config->m0_pin, m0_value);
131+
ret = a4979_set_microstep_pin(dev, &config->common.msx_pins[0], m0_value);
134132
if (ret != 0) {
135133
return ret;
136134
}
137-
ret = a4979_set_microstep_pin(dev, &config->m1_pin, m1_value);
135+
ret = a4979_set_microstep_pin(dev, &config->common.msx_pins[1], m1_value);
138136
if (ret != 0) {
139137
return ret;
140138
}
@@ -228,22 +226,22 @@ static int a4979_init(const struct device *dev)
228226
}
229227

230228
/* Configure microstep pin 0 */
231-
if (!gpio_is_ready_dt(&config->m0_pin)) {
229+
if (!gpio_is_ready_dt(&config->common.msx_pins[0])) {
232230
LOG_ERR("m0 Pin is not ready");
233231
return -ENODEV;
234232
}
235-
ret = gpio_pin_configure_dt(&config->m0_pin, GPIO_OUTPUT_INACTIVE);
233+
ret = gpio_pin_configure_dt(&config->common.msx_pins[0], GPIO_OUTPUT_INACTIVE);
236234
if (ret != 0) {
237235
LOG_ERR("%s: Failed to configure m0_pin (error: %d)", dev->name, ret);
238236
return ret;
239237
}
240238

241239
/* Configure microstep pin 1 */
242-
if (!gpio_is_ready_dt(&config->m1_pin)) {
240+
if (!gpio_is_ready_dt(&config->common.msx_pins[1])) {
243241
LOG_ERR("m1 Pin is not ready");
244242
return -ENODEV;
245243
}
246-
ret = gpio_pin_configure_dt(&config->m1_pin, GPIO_OUTPUT_INACTIVE);
244+
ret = gpio_pin_configure_dt(&config->common.msx_pins[1], GPIO_OUTPUT_INACTIVE);
247245
if (ret != 0) {
248246
LOG_ERR("%s: Failed to configure m1_pin (error: %d)", dev->name, ret);
249247
return ret;
@@ -282,21 +280,29 @@ static DEVICE_API(stepper, a4979_stepper_api) = {
282280
};
283281

284282
#define A4979_DEVICE(inst) \
285-
\
286-
static const struct a4979_config a4979_config_##inst = { \
287-
.common = STEP_DIR_STEPPER_DT_INST_COMMON_CONFIG_INIT(inst), \
288-
.en_pin = GPIO_DT_SPEC_INST_GET_OR(inst, en_gpios, {0}), \
289-
.reset_pin = GPIO_DT_SPEC_INST_GET_OR(inst, reset_gpios, {0}), \
290-
.m0_pin = GPIO_DT_SPEC_INST_GET(inst, m0_gpios), \
291-
.m1_pin = GPIO_DT_SPEC_INST_GET(inst, m1_gpios), \
292-
}; \
293-
\
294-
static struct a4979_data a4979_data_##inst = { \
295-
.common = STEP_DIR_STEPPER_DT_INST_COMMON_DATA_INIT(inst), \
296-
.micro_step_res = DT_INST_PROP(inst, micro_step_res), \
297-
}; \
298-
\
299-
DEVICE_DT_INST_DEFINE(inst, a4979_init, NULL, &a4979_data_##inst, &a4979_config_##inst, \
283+
IF_ENABLED(DT_INST_NODE_HAS_PROP(inst, msx_gpios), ( \
284+
static const struct gpio_dt_spec a4979_stepper_msx_pins_##inst[] = { \
285+
DT_INST_FOREACH_PROP_ELEM_SEP( \
286+
inst, msx_gpios, GPIO_DT_SPEC_GET_BY_IDX, (,) \
287+
), \
288+
}; \
289+
BUILD_ASSERT( \
290+
ARRAY_SIZE(a4979_stepper_msx_pins_##inst) == 2, \
291+
"Two microstep config pins needed"); \
292+
)) \
293+
static const struct a4979_config a4979_config_##inst = { \
294+
.common = STEP_DIR_STEPPER_DT_INST_COMMON_CONFIG_INIT( \
295+
inst, a4979_stepper_msx_pins_##inst), \
296+
.en_pin = GPIO_DT_SPEC_INST_GET_OR(inst, en_gpios, {0}), \
297+
.reset_pin = GPIO_DT_SPEC_INST_GET_OR(inst, reset_gpios, {0}), \
298+
}; \
299+
\
300+
static struct a4979_data a4979_data_##inst = { \
301+
.common = STEP_DIR_STEPPER_DT_INST_COMMON_DATA_INIT(inst), \
302+
.micro_step_res = DT_INST_PROP(inst, micro_step_res), \
303+
}; \
304+
\
305+
DEVICE_DT_INST_DEFINE(inst, a4979_init, NULL, &a4979_data_##inst, &a4979_config_##inst, \
300306
POST_KERNEL, CONFIG_STEPPER_INIT_PRIORITY, &a4979_stepper_api);
301307

302308
DT_INST_FOREACH_STATUS_OKAY(A4979_DEVICE)

drivers/stepper/step_dir/step_dir_stepper_common.h

+8-6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
struct step_dir_stepper_common_config {
3030
const struct gpio_dt_spec step_pin;
3131
const struct gpio_dt_spec dir_pin;
32+
const struct gpio_dt_spec *msx_pins;
3233
bool dual_edge;
3334
const struct stepper_timing_source_api *timing_source;
3435
const struct device *counter;
@@ -42,24 +43,25 @@ struct step_dir_stepper_common_config {
4243
*
4344
* @param node_id The devicetree node identifier.
4445
*/
45-
#define STEP_DIR_STEPPER_DT_COMMON_CONFIG_INIT(node_id) \
46-
{ \
46+
#define STEP_DIR_STEPPER_DT_COMMON_CONFIG_INIT(node_id, msx_gpio_array) \
47+
{ \
48+
IF_ENABLED( DT_NODE_HAS_PROP(node_id, msx_gpios), (.msx_pins = msx_gpio_array,)) \
4749
.step_pin = GPIO_DT_SPEC_GET(node_id, step_gpios), \
4850
.dir_pin = GPIO_DT_SPEC_GET(node_id, dir_gpios), \
4951
.dual_edge = DT_PROP_OR(node_id, dual_edge_step, false), \
5052
.counter = DEVICE_DT_GET_OR_NULL(DT_PHANDLE(node_id, counter)), \
5153
.invert_direction = DT_PROP(node_id, invert_direction), \
5254
.timing_source = COND_CODE_1(DT_NODE_HAS_PROP(node_id, counter), \
5355
(&step_counter_timing_source_api), \
54-
(&step_work_timing_source_api)), \
56+
(&step_work_timing_source_api)), \
5557
}
5658

5759
/**
5860
* @brief Initialize common step direction stepper config from devicetree instance.
5961
* @param inst Instance.
6062
*/
61-
#define STEP_DIR_STEPPER_DT_INST_COMMON_CONFIG_INIT(inst) \
62-
STEP_DIR_STEPPER_DT_COMMON_CONFIG_INIT(DT_DRV_INST(inst))
63+
#define STEP_DIR_STEPPER_DT_INST_COMMON_CONFIG_INIT(inst, msx_gpio_array) \
64+
STEP_DIR_STEPPER_DT_COMMON_CONFIG_INIT(DT_DRV_INST(inst), msx_gpio_array)
6365

6466
/**
6567
* @brief Common step direction stepper data.
@@ -150,7 +152,7 @@ int step_dir_stepper_common_move_by(const struct device *dev, const int32_t micr
150152
* @return 0 on success, or a negative error code on failure.
151153
*/
152154
int step_dir_stepper_common_set_microstep_interval(const struct device *dev,
153-
const uint64_t microstep_interval_ns);
155+
const uint64_t microstep_interval_ns);
154156

155157
/**
156158
* @brief Set the reference position of the stepper motor.

drivers/stepper/ti/drv84xx.c

+23-17
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ struct drv84xx_config {
3030
struct step_dir_stepper_common_config common;
3131
struct gpio_dt_spec sleep_pin;
3232
struct gpio_dt_spec en_pin;
33-
struct gpio_dt_spec m0_pin;
34-
struct gpio_dt_spec m1_pin;
3533
struct gpio_dt_spec fault_pin;
3634
};
3735

@@ -106,14 +104,14 @@ int drv84xx_microstep_recovery(const struct device *dev)
106104
uint8_t m0_value = data->pin_states.m0;
107105
uint8_t m1_value = data->pin_states.m1;
108106

109-
ret = drv84xx_set_microstep_pin(dev, &config->m0_pin, m0_value);
107+
ret = drv84xx_set_microstep_pin(dev, &config->common.msx_pins[0], m0_value);
110108
if (ret != 0) {
111109
LOG_ERR("%s: Failed to restore microstep configuration (error: %d)", dev->name,
112110
ret);
113111
return ret;
114112
}
115113

116-
ret = drv84xx_set_microstep_pin(dev, &config->m1_pin, m1_value);
114+
ret = drv84xx_set_microstep_pin(dev, &config->common.msx_pins[1], m1_value);
117115
if (ret != 0) {
118116
LOG_ERR("%s: Failed to restore microstep configuration (error: %d)", dev->name,
119117
ret);
@@ -272,7 +270,7 @@ static int drv84xx_set_micro_step_res(const struct device *dev,
272270
uint8_t m0_value = 0;
273271
uint8_t m1_value = 0;
274272

275-
if ((config->m0_pin.port == NULL) || (config->m1_pin.port == NULL)) {
273+
if ((config->common.msx_pins[0].port == NULL) || (config->common.msx_pins[1].port == NULL)) {
276274

277275
LOG_ERR("%s: Failed to set microstep resolution: microstep pins are not defined "
278276
"(error: %d)",
@@ -326,12 +324,12 @@ static int drv84xx_set_micro_step_res(const struct device *dev,
326324
return -ENOTSUP;
327325
};
328326

329-
ret = drv84xx_set_microstep_pin(dev, &config->m0_pin, m0_value);
327+
ret = drv84xx_set_microstep_pin(dev, &config->common.msx_pins[0], m0_value);
330328
if (ret != 0) {
331329
return ret;
332330
}
333331

334-
ret = drv84xx_set_microstep_pin(dev, &config->m1_pin, m1_value);
332+
ret = drv84xx_set_microstep_pin(dev, &config->common.msx_pins[1], m1_value);
335333
if (ret != 0) {
336334
return ret;
337335
}
@@ -421,8 +419,8 @@ static int drv84xx_init(const struct device *dev)
421419
}
422420

423421
/* Configure microstep pin 0 if it is available */
424-
if (config->m0_pin.port != NULL) {
425-
ret = gpio_pin_configure_dt(&config->m0_pin, GPIO_OUTPUT_INACTIVE);
422+
if (config->common.msx_pins[0].port != NULL) {
423+
ret = gpio_pin_configure_dt(&config->common.msx_pins[0], GPIO_OUTPUT_INACTIVE);
426424
if (ret != 0) {
427425
LOG_ERR("%s: Failed to configure m0_pin (error: %d)", dev->name, ret);
428426
return ret;
@@ -431,16 +429,16 @@ static int drv84xx_init(const struct device *dev)
431429
}
432430

433431
/* Configure microstep pin 1 if it is available */
434-
if (config->m1_pin.port != NULL) {
435-
ret = gpio_pin_configure_dt(&config->m1_pin, GPIO_OUTPUT_INACTIVE);
432+
if (config->common.msx_pins[1].port != NULL) {
433+
ret = gpio_pin_configure_dt(&config->common.msx_pins[1], GPIO_OUTPUT_INACTIVE);
436434
if (ret != 0) {
437435
LOG_ERR("%s: Failed to configure m1_pin (error: %d)", dev->name, ret);
438436
return ret;
439437
}
440438
data->pin_states.m1 = 0U;
441439
}
442440

443-
if ((config->m0_pin.port != NULL) && (config->m1_pin.port != NULL)) {
441+
if ((config->common.msx_pins[0].port != NULL) && (config->common.msx_pins[1].port != NULL)) {
444442
ret = drv84xx_set_micro_step_res(dev, data->ustep_res);
445443
if (ret != 0) {
446444
return ret;
@@ -492,13 +490,21 @@ static DEVICE_API(stepper, drv84xx_stepper_api) = {
492490
};
493491

494492
#define DRV84XX_DEVICE(inst) \
495-
\
496-
static const struct drv84xx_config drv84xx_config_##inst = { \
497-
.common = STEP_DIR_STEPPER_DT_INST_COMMON_CONFIG_INIT(inst), \
493+
IF_ENABLED(DT_INST_NODE_HAS_PROP(inst, msx_gpios), ( \
494+
static const struct gpio_dt_spec drv84xx_stepper_msx_pins_##inst[] = { \
495+
DT_INST_FOREACH_PROP_ELEM_SEP( \
496+
inst, msx_gpios, GPIO_DT_SPEC_GET_BY_IDX, (,) \
497+
), \
498+
}; \
499+
BUILD_ASSERT( \
500+
ARRAY_SIZE(drv84xx_stepper_msx_pins_##inst) == 2, \
501+
"Two microstep config pins needed"); \
502+
)) \
503+
static const struct drv84xx_config drv84xx_config_##inst = { \
504+
.common = STEP_DIR_STEPPER_DT_INST_COMMON_CONFIG_INIT( \
505+
inst, drv84xx_stepper_msx_pins_##inst), \
498506
.sleep_pin = GPIO_DT_SPEC_INST_GET_OR(inst, sleep_gpios, {0}), \
499507
.en_pin = GPIO_DT_SPEC_INST_GET_OR(inst, en_gpios, {0}), \
500-
.m0_pin = GPIO_DT_SPEC_INST_GET_OR(inst, m0_gpios, {0}), \
501-
.m1_pin = GPIO_DT_SPEC_INST_GET_OR(inst, m1_gpios, {0}), \
502508
.fault_pin = GPIO_DT_SPEC_INST_GET_OR(inst, fault_gpios, {0}), \
503509
}; \
504510
\

dts/bindings/stepper/adi/adi,tmc2209.yaml

-8
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,6 @@ include:
2121
- name: stepper-controller.yaml
2222

2323
properties:
24-
msx-gpios:
25-
type: phandle-array
26-
description: |
27-
An array of GPIO pins for configuring the microstep resolution of the driver.
28-
The pins should be listed in the following order:
29-
- MS1
30-
- MS2
31-
3224
dual-edge-step:
3325
type: boolean
3426
description: |

dts/bindings/stepper/allegro/allegro,a4979.yml

+2-12
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ description: |
1616
dir-gpios = <&gpiod 14 GPIO_ACTIVE_HIGH>;
1717
step-gpios = <&gpiod 15 GPIO_ACTIVE_HIGH>;
1818
en-gpios = <&gpiod 11 GPIO_ACTIVE_HIGH>;
19-
m0-gpios = <&gpiod 13 0>;
20-
m1-gpios = <&gpiod 12 0>;
19+
msx-gpios = <&gpiod 13 0>,
20+
<&gpiod 12 0>;
2121
counter = <&counter5>;
2222
};
2323
@@ -27,16 +27,6 @@ include:
2727
- name: stepper-controller.yaml
2828

2929
properties:
30-
m0-gpios:
31-
required: true
32-
type: phandle-array
33-
description: Microstep configuration pin 0.
34-
35-
m1-gpios:
36-
required: true
37-
type: phandle-array
38-
description: Microstep configuration pin 1.
39-
4030
reset-gpios:
4131
type: phandle-array
4232
required: true

dts/bindings/stepper/stepper-controller.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ properties:
4141
The GPIO pins used to send direction signals to the stepper motor.
4242
Pin will be driven high for forward direction and low for reverse direction.
4343
44+
msx-gpios:
45+
type: phandle-array
46+
description: |
47+
An array of GPIO pins for configuring the microstep resolution of the driver.
48+
The pins should be listed in the following order:
49+
- MS1
50+
- MS2
51+
- MS3
52+
4453
counter:
4554
type: phandle
4655
description: Counter used for generating step-accurate pulse signals.

0 commit comments

Comments
 (0)