Skip to content

Commit c0cc750

Browse files
committed
drivers: stepper: include enable and microstep pins in common config
- Include the enable, m0, and m1 microstep to the common init config. - Refactor drivers that manually init these pins to use the pins from the common config. Signed-off-by: Hong Nguyen <[email protected]>
1 parent 29059ec commit c0cc750

File tree

4 files changed

+47
-59
lines changed

4 files changed

+47
-59
lines changed

drivers/stepper/adi_tmc/tmc22xx/tmc22xx.c

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ LOG_MODULE_REGISTER(tmc22xx, CONFIG_STEPPER_LOG_LEVEL);
1515

1616
struct tmc22xx_config {
1717
struct step_dir_stepper_common_config common;
18-
const struct gpio_dt_spec enable_pin;
19-
struct gpio_dt_spec m0_pin;
20-
struct gpio_dt_spec m1_pin;
2118
enum stepper_drv_micro_step_resolution *msx_resolutions;
2219
};
2320

@@ -32,15 +29,15 @@ static int tmc22xx_enable(const struct device *dev)
3229
const struct tmc22xx_config *config = dev->config;
3330

3431
LOG_DBG("Enabling Stepper motor controller %s", dev->name);
35-
return gpio_pin_set_dt(&config->enable_pin, 1);
32+
return gpio_pin_set_dt(&config->common.en_pin, 1);
3633
}
3734

3835
static int tmc22xx_disable(const struct device *dev)
3936
{
4037
const struct tmc22xx_config *config = dev->config;
4138

4239
LOG_DBG("Disabling Stepper motor controller %s", dev->name);
43-
return gpio_pin_set_dt(&config->enable_pin, 0);
40+
return gpio_pin_set_dt(&config->common.en_pin, 0);
4441
}
4542

4643
static int tmc22xx_set_micro_step_res(const struct device *dev,
@@ -50,7 +47,7 @@ static int tmc22xx_set_micro_step_res(const struct device *dev,
5047
const struct tmc22xx_config *config = dev->config;
5148
int ret;
5249

53-
if ((config->m0_pin.port == NULL) || (config->m1_pin.port == NULL)) {
50+
if ((config->common.m0_pin.port == NULL) || (config->common.m1_pin.port == NULL)) {
5451
LOG_ERR("%s: Failed to set microstep resolution: microstep pins are not defined "
5552
"(error: %d)",
5653
dev->name, -ENOTSUP);
@@ -62,13 +59,13 @@ static int tmc22xx_set_micro_step_res(const struct device *dev,
6259
continue;
6360
}
6461

65-
ret = gpio_pin_set_dt(&config->m0_pin, i & 0x01);
62+
ret = gpio_pin_set_dt(&config->common.m0_pin, i & 0x01);
6663
if (ret < 0) {
6764
LOG_ERR("Failed to set MS1 pin: %d", ret);
6865
return ret;
6966
}
7067

71-
ret = gpio_pin_set_dt(&config->m1_pin, (i & 0x02) >> 1);
68+
ret = gpio_pin_set_dt(&config->common.m1_pin, (i & 0x02) >> 1);
7269
if (ret < 0) {
7370
LOG_ERR("Failed to set MS2 pin: %d", ret);
7471
return ret;
@@ -97,23 +94,23 @@ static int tmc22xx_stepper_configure_msx_pins(const struct device *dev)
9794
const struct tmc22xx_config *config = dev->config;
9895
int ret;
9996

100-
if (!gpio_is_ready_dt(&config->m0_pin)) {
97+
if (!gpio_is_ready_dt(&config->common.m0_pin)) {
10198
LOG_ERR("MS1 pin not ready");
10299
return -ENODEV;
103100
}
104101

105-
ret = gpio_pin_configure_dt(&config->m0_pin, GPIO_OUTPUT);
102+
ret = gpio_pin_configure_dt(&config->common.m0_pin, GPIO_OUTPUT);
106103
if (ret < 0) {
107104
LOG_ERR("Failed to configure ms1 pin");
108105
return ret;
109106
}
110107

111-
if (!gpio_is_ready_dt(&config->m1_pin)) {
108+
if (!gpio_is_ready_dt(&config->common.m1_pin)) {
112109
LOG_ERR("MS2 pin not ready");
113110
return -ENODEV;
114111
}
115112

116-
ret = gpio_pin_configure_dt(&config->m1_pin, GPIO_OUTPUT);
113+
ret = gpio_pin_configure_dt(&config->common.m1_pin, GPIO_OUTPUT);
117114
if (ret < 0) {
118115
LOG_ERR("Failed to configure ms2 pin");
119116
return ret;
@@ -128,18 +125,18 @@ static int tmc22xx_stepper_init(const struct device *dev)
128125
struct tmc22xx_data *data = dev->data;
129126
int ret;
130127

131-
if (!gpio_is_ready_dt(&config->enable_pin)) {
128+
if (!gpio_is_ready_dt(&config->common.en_pin)) {
132129
LOG_ERR("GPIO pins are not ready");
133130
return -ENODEV;
134131
}
135132

136-
ret = gpio_pin_configure_dt(&config->enable_pin, GPIO_OUTPUT);
133+
ret = gpio_pin_configure_dt(&config->common.en_pin, GPIO_OUTPUT);
137134
if (ret < 0) {
138135
LOG_ERR("Failed to configure enable pin: %d", ret);
139136
return ret;
140137
}
141138

142-
if ((config->m0_pin.port != NULL) && (config->m1_pin.port != NULL)) {
139+
if ((config->common.m0_pin.port != NULL) && (config->common.m1_pin.port != NULL)) {
143140
ret = tmc22xx_stepper_configure_msx_pins(dev);
144141
if (ret < 0) {
145142
LOG_ERR("Failed to configure MSX pins: %d", ret);
@@ -166,10 +163,7 @@ static DEVICE_API(stepper_drv, tmc22xx_stepper_api) = {
166163
#define TMC22XX_STEPPER_DEFINE(inst, msx_table) \
167164
static const struct tmc22xx_config tmc22xx_config_##inst = { \
168165
.common = STEP_DIR_STEPPER_DT_INST_COMMON_CONFIG_INIT(inst), \
169-
.enable_pin = GPIO_DT_SPEC_INST_GET(inst, en_gpios), \
170166
.msx_resolutions = msx_table, \
171-
.m0_pin = GPIO_DT_SPEC_INST_GET_OR(inst, m0_gpios, {0}), \
172-
.m1_pin = GPIO_DT_SPEC_INST_GET_OR(inst, m1_gpios, {0}), \
173167
}; \
174168
static struct tmc22xx_data tmc22xx_data_##inst = { \
175169
.resolution = DT_INST_PROP(inst, micro_step_res), \

drivers/stepper/allegro/a4979.c

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ LOG_MODULE_REGISTER(a4979, CONFIG_STEPPER_LOG_LEVEL);
1515

1616
struct a4979_config {
1717
const struct step_dir_stepper_common_config common;
18-
const struct gpio_dt_spec en_pin;
1918
const struct gpio_dt_spec reset_pin;
20-
const struct gpio_dt_spec m0_pin;
21-
const struct gpio_dt_spec m1_pin;
2219
};
2320

2421
struct a4979_data {
@@ -51,7 +48,7 @@ static int a4979_set_microstep_pin(const struct device *dev, const struct gpio_d
5148
static int a4979_enable(const struct device *dev)
5249
{
5350
const struct a4979_config *config = dev->config;
54-
bool has_enable_pin = config->en_pin.port != NULL;
51+
bool has_enable_pin = config->common.en_pin.port != NULL;
5552
int ret;
5653

5754
/* Check availability of enable pin, as it might be hardwired. */
@@ -60,7 +57,7 @@ static int a4979_enable(const struct device *dev)
6057
return -ENOTSUP;
6158
}
6259

63-
ret = gpio_pin_set_dt(&config->en_pin, 1);
60+
ret = gpio_pin_set_dt(&config->common.en_pin, 1);
6461
if (ret != 0) {
6562
LOG_ERR("%s: Failed to set en_pin (error: %d)", dev->name, ret);
6663
return ret;
@@ -72,7 +69,7 @@ static int a4979_enable(const struct device *dev)
7269
static int a4979_disable(const struct device *dev)
7370
{
7471
const struct a4979_config *config = dev->config;
75-
bool has_enable_pin = config->en_pin.port != NULL;
72+
bool has_enable_pin = config->common.en_pin.port != NULL;
7673
int ret;
7774

7875
/* Check availability of enable pin, as it might be hardwired. */
@@ -81,7 +78,7 @@ static int a4979_disable(const struct device *dev)
8178
return -ENOTSUP;
8279
}
8380

84-
ret = gpio_pin_set_dt(&config->en_pin, 0);
81+
ret = gpio_pin_set_dt(&config->common.en_pin, 0);
8582
if (ret != 0) {
8683
LOG_ERR("%s: Failed to set en_pin (error: %d)", dev->name, ret);
8784
return ret;
@@ -122,11 +119,11 @@ static int a4979_set_micro_step_res(const struct device *dev,
122119
return -ENOTSUP;
123120
}
124121

125-
ret = a4979_set_microstep_pin(dev, &config->m0_pin, m0_value);
122+
ret = a4979_set_microstep_pin(dev, &config->common.m0_pin, m0_value);
126123
if (ret != 0) {
127124
return ret;
128125
}
129-
ret = a4979_set_microstep_pin(dev, &config->m1_pin, m1_value);
126+
ret = a4979_set_microstep_pin(dev, &config->common.m1_pin, m1_value);
130127
if (ret != 0) {
131128
return ret;
132129
}
@@ -150,7 +147,7 @@ static int a4979_init(const struct device *dev)
150147
const struct a4979_data *data = dev->data;
151148
int ret;
152149

153-
bool has_enable_pin = config->en_pin.port != NULL;
150+
bool has_enable_pin = config->common.en_pin.port != NULL;
154151
bool has_reset_pin = config->reset_pin.port != NULL;
155152

156153
LOG_DBG("Initializing %s gpios", dev->name);
@@ -171,35 +168,35 @@ static int a4979_init(const struct device *dev)
171168

172169
/* Configure enable pin if it is available */
173170
if (has_enable_pin) {
174-
if (!gpio_is_ready_dt(&config->en_pin)) {
171+
if (!gpio_is_ready_dt(&config->common.en_pin)) {
175172
LOG_ERR("Enable Pin is not ready");
176173
return -ENODEV;
177174
}
178175

179-
ret = gpio_pin_configure_dt(&config->en_pin, GPIO_OUTPUT_INACTIVE);
176+
ret = gpio_pin_configure_dt(&config->common.en_pin, GPIO_OUTPUT_INACTIVE);
180177
if (ret != 0) {
181178
LOG_ERR("%s: Failed to configure en_pin (error: %d)", dev->name, ret);
182179
return ret;
183180
}
184181
}
185182

186183
/* Configure microstep pin 0 */
187-
if (!gpio_is_ready_dt(&config->m0_pin)) {
184+
if (!gpio_is_ready_dt(&config->common.m0_pin)) {
188185
LOG_ERR("m0 Pin is not ready");
189186
return -ENODEV;
190187
}
191-
ret = gpio_pin_configure_dt(&config->m0_pin, GPIO_OUTPUT_INACTIVE);
188+
ret = gpio_pin_configure_dt(&config->common.m0_pin, GPIO_OUTPUT_INACTIVE);
192189
if (ret != 0) {
193190
LOG_ERR("%s: Failed to configure m0_pin (error: %d)", dev->name, ret);
194191
return ret;
195192
}
196193

197194
/* Configure microstep pin 1 */
198-
if (!gpio_is_ready_dt(&config->m1_pin)) {
195+
if (!gpio_is_ready_dt(&config->common.m1_pin)) {
199196
LOG_ERR("m1 Pin is not ready");
200197
return -ENODEV;
201198
}
202-
ret = gpio_pin_configure_dt(&config->m1_pin, GPIO_OUTPUT_INACTIVE);
199+
ret = gpio_pin_configure_dt(&config->common.m1_pin, GPIO_OUTPUT_INACTIVE);
203200
if (ret != 0) {
204201
LOG_ERR("%s: Failed to configure m1_pin (error: %d)", dev->name, ret);
205202
return ret;
@@ -225,10 +222,7 @@ static DEVICE_API(stepper_drv, a4979_stepper_api) = {
225222
\
226223
static const struct a4979_config a4979_config_##inst = { \
227224
.common = STEP_DIR_STEPPER_DT_INST_COMMON_CONFIG_INIT(inst), \
228-
.en_pin = GPIO_DT_SPEC_INST_GET_OR(inst, en_gpios, {0}), \
229225
.reset_pin = GPIO_DT_SPEC_INST_GET_OR(inst, reset_gpios, {0}), \
230-
.m0_pin = GPIO_DT_SPEC_INST_GET(inst, m0_gpios), \
231-
.m1_pin = GPIO_DT_SPEC_INST_GET(inst, m1_gpios), \
232226
}; \
233227
\
234228
static struct a4979_data a4979_data_##inst = { \

drivers/stepper/step_dir/include/step_dir_stepper_common.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
* This structure **must** be placed first in the driver's config structure.
2222
*/
2323
struct step_dir_stepper_common_config {
24+
struct gpio_dt_spec en_pin;
25+
struct gpio_dt_spec m0_pin;
26+
struct gpio_dt_spec m1_pin;
2427
uint32_t step_width_ns;
2528
bool dual_edge;
2629
};
@@ -34,6 +37,9 @@ struct step_dir_stepper_common_config {
3437
*/
3538
#define STEP_DIR_STEPPER_DT_COMMON_CONFIG_INIT(node_id) \
3639
{ \
40+
.en_pin = GPIO_DT_SPEC_GET_OR(node_id, en_gpios, {0}), \
41+
.m0_pin = GPIO_DT_SPEC_GET_OR(node_id, m0_gpios, {0}), \
42+
.m1_pin = GPIO_DT_SPEC_GET_OR(node_id, m1_gpios, {0}), \
3743
.dual_edge = DT_PROP_OR(node_id, dual_edge_step, false), \
3844
.step_width_ns = DT_PROP(node_id, step_width_ns), \
3945
}

drivers/stepper/ti/drv84xx.c

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ LOG_MODULE_REGISTER(drv84xx, CONFIG_STEPPER_LOG_LEVEL);
2828
struct drv84xx_config {
2929
struct step_dir_stepper_common_config common;
3030
struct gpio_dt_spec sleep_pin;
31-
struct gpio_dt_spec en_pin;
32-
struct gpio_dt_spec m0_pin;
33-
struct gpio_dt_spec m1_pin;
3431
struct gpio_dt_spec fault_pin;
3532
};
3633

@@ -105,14 +102,14 @@ int drv84xx_microstep_recovery(const struct device *dev)
105102
uint8_t m0_value = data->pin_states.m0;
106103
uint8_t m1_value = data->pin_states.m1;
107104

108-
ret = drv84xx_set_microstep_pin(dev, &config->m0_pin, m0_value);
105+
ret = drv84xx_set_microstep_pin(dev, &config->common.m0_pin, m0_value);
109106
if (ret != 0) {
110107
LOG_ERR("%s: Failed to restore microstep configuration (error: %d)", dev->name,
111108
ret);
112109
return ret;
113110
}
114111

115-
ret = drv84xx_set_microstep_pin(dev, &config->m1_pin, m1_value);
112+
ret = drv84xx_set_microstep_pin(dev, &config->common.m1_pin, m1_value);
116113
if (ret != 0) {
117114
LOG_ERR("%s: Failed to restore microstep configuration (error: %d)", dev->name,
118115
ret);
@@ -125,7 +122,7 @@ int drv84xx_microstep_recovery(const struct device *dev)
125122
static int drv84xx_check_en_sleep_pin(const struct drv84xx_config *config)
126123
{
127124
bool has_sleep_pin = config->sleep_pin.port != NULL;
128-
bool has_enable_pin = config->en_pin.port != NULL;
125+
bool has_enable_pin = config->common.en_pin.port != NULL;
129126

130127
if (!has_sleep_pin && !has_enable_pin) {
131128
LOG_ERR("Failed to enable/disable device, neither sleep pin nor enable pin are "
@@ -140,11 +137,11 @@ static int drv84xx_set_en_pin_state(const struct device *dev, bool enable)
140137
{
141138
const struct drv84xx_config *config = dev->config;
142139
struct drv84xx_data *data = dev->data;
143-
bool has_enable_pin = config->en_pin.port != NULL;
140+
bool has_enable_pin = config->common.en_pin.port != NULL;
144141
int ret;
145142

146143
if (has_enable_pin) {
147-
ret = gpio_pin_set_dt(&config->en_pin, enable);
144+
ret = gpio_pin_set_dt(&config->common.en_pin, enable);
148145
if (ret != 0) {
149146
LOG_ERR("%s: Failed to set en_pin (error: %d)", dev->name, ret);
150147
return ret;
@@ -178,7 +175,7 @@ static int drv84xx_enable(const struct device *dev)
178175
{
179176
const struct drv84xx_config *config = dev->config;
180177
struct drv84xx_data *data = dev->data;
181-
bool has_enable_pin = config->en_pin.port != NULL;
178+
bool has_enable_pin = config->common.en_pin.port != NULL;
182179
bool has_sleep_pin = config->sleep_pin.port != NULL;
183180
bool has_fault_pin = config->fault_pin.port != NULL;
184181
k_timeout_t enable_timeout;
@@ -276,7 +273,7 @@ static int drv84xx_set_micro_step_res(const struct device *dev,
276273
uint8_t m0_value = 0;
277274
uint8_t m1_value = 0;
278275

279-
if ((config->m0_pin.port == NULL) || (config->m1_pin.port == NULL)) {
276+
if ((config->common.m0_pin.port == NULL) || (config->common.m1_pin.port == NULL)) {
280277

281278
LOG_ERR("%s: Failed to set microstep resolution: microstep pins are not defined "
282279
"(error: %d)",
@@ -330,12 +327,12 @@ static int drv84xx_set_micro_step_res(const struct device *dev,
330327
return -ENOTSUP;
331328
};
332329

333-
ret = drv84xx_set_microstep_pin(dev, &config->m0_pin, m0_value);
330+
ret = drv84xx_set_microstep_pin(dev, &config->common.m0_pin, m0_value);
334331
if (ret != 0) {
335332
return ret;
336333
}
337334

338-
ret = drv84xx_set_microstep_pin(dev, &config->m1_pin, m1_value);
335+
ret = drv84xx_set_microstep_pin(dev, &config->common.m1_pin, m1_value);
339336
if (ret != 0) {
340337
return ret;
341338
}
@@ -384,8 +381,8 @@ static int drv84xx_init(const struct device *dev)
384381
}
385382

386383
/* Configure enable pin if it is available */
387-
if (config->en_pin.port != NULL) {
388-
ret = gpio_pin_configure_dt(&config->en_pin, GPIO_OUTPUT_INACTIVE);
384+
if (config->common.en_pin.port != NULL) {
385+
ret = gpio_pin_configure_dt(&config->common.en_pin, GPIO_OUTPUT_INACTIVE);
389386
if (ret != 0) {
390387
LOG_ERR("%s: Failed to configure en_pin (error: %d)", dev->name, ret);
391388
return ret;
@@ -394,8 +391,8 @@ static int drv84xx_init(const struct device *dev)
394391
}
395392

396393
/* Configure microstep pin 0 if it is available */
397-
if (config->m0_pin.port != NULL) {
398-
ret = gpio_pin_configure_dt(&config->m0_pin, GPIO_OUTPUT_INACTIVE);
394+
if (config->common.m0_pin.port != NULL) {
395+
ret = gpio_pin_configure_dt(&config->common.m0_pin, GPIO_OUTPUT_INACTIVE);
399396
if (ret != 0) {
400397
LOG_ERR("%s: Failed to configure m0_pin (error: %d)", dev->name, ret);
401398
return ret;
@@ -404,16 +401,16 @@ static int drv84xx_init(const struct device *dev)
404401
}
405402

406403
/* Configure microstep pin 1 if it is available */
407-
if (config->m1_pin.port != NULL) {
408-
ret = gpio_pin_configure_dt(&config->m1_pin, GPIO_OUTPUT_INACTIVE);
404+
if (config->common.m1_pin.port != NULL) {
405+
ret = gpio_pin_configure_dt(&config->common.m1_pin, GPIO_OUTPUT_INACTIVE);
409406
if (ret != 0) {
410407
LOG_ERR("%s: Failed to configure m1_pin (error: %d)", dev->name, ret);
411408
return ret;
412409
}
413410
data->pin_states.m1 = 0U;
414411
}
415412

416-
if ((config->m0_pin.port != NULL) && (config->m1_pin.port != NULL)) {
413+
if ((config->common.m0_pin.port != NULL) && (config->common.m1_pin.port != NULL)) {
417414
ret = drv84xx_set_micro_step_res(dev, data->ustep_res);
418415
if (ret != 0) {
419416
return ret;
@@ -455,9 +452,6 @@ static DEVICE_API(stepper_drv, drv84xx_stepper_api) = {
455452
static const struct drv84xx_config drv84xx_config_##inst = { \
456453
.common = STEP_DIR_STEPPER_DT_INST_COMMON_CONFIG_INIT(inst), \
457454
.sleep_pin = GPIO_DT_SPEC_INST_GET_OR(inst, sleep_gpios, {0}), \
458-
.en_pin = GPIO_DT_SPEC_INST_GET_OR(inst, en_gpios, {0}), \
459-
.m0_pin = GPIO_DT_SPEC_INST_GET_OR(inst, m0_gpios, {0}), \
460-
.m1_pin = GPIO_DT_SPEC_INST_GET_OR(inst, m1_gpios, {0}), \
461455
.fault_pin = GPIO_DT_SPEC_INST_GET_OR(inst, fault_gpios, {0}), \
462456
}; \
463457
\

0 commit comments

Comments
 (0)