1
1
/*
2
2
* SPDX-FileCopyrightText: Copyright (c) 2024 Fabian Blatz <fabianblatz@gmail.com>
3
+ * SPDX-FileCopyrightText: Copyright (c) 2025 Andre Stefanov <mail@andrestefanov.de>
3
4
* SPDX-License-Identifier: Apache-2.0
4
5
*/
5
6
6
- #include "../step_dir/step_dir_stepper_common.h"
7
+ #include "../motion_controller/stepper_motion_controller.h"
8
+ #include "../interface/stepper_interface_step_dir.h"
9
+ #include "../stepper_common.h"
10
+
11
+ #include <zephyr/drivers/gpio.h>
7
12
8
13
#include <zephyr/logging/log.h>
9
14
LOG_MODULE_REGISTER (tmc22xx , CONFIG_STEPPER_LOG_LEVEL );
@@ -12,33 +17,23 @@ LOG_MODULE_REGISTER(tmc22xx, CONFIG_STEPPER_LOG_LEVEL);
12
17
#define MSX_PIN_STATE_COUNT 4
13
18
14
19
struct tmc22xx_config {
15
- struct step_dir_stepper_common_config common ;
16
- const struct gpio_dt_spec enable_pin ;
20
+ const struct stepper_common_config * common ;
17
21
const struct gpio_dt_spec * msx_pins ;
18
22
enum stepper_micro_step_resolution * msx_resolutions ;
19
23
};
20
24
21
25
struct tmc22xx_data {
22
- struct step_dir_stepper_common_data common ;
26
+ struct stepper_common_data * const common ;
23
27
enum stepper_micro_step_resolution resolution ;
24
28
};
25
29
26
- STEP_DIR_STEPPER_STRUCT_CHECK (struct tmc22xx_config , struct tmc22xx_data );
27
-
28
- static int tmc22xx_stepper_enable (const struct device * dev )
29
- {
30
- const struct tmc22xx_config * config = dev -> config ;
31
-
32
- LOG_DBG ("Enabling Stepper motor controller %s" , dev -> name );
33
- return gpio_pin_set_dt (& config -> enable_pin , 1 );
34
- }
35
-
36
- static int tmc22xx_stepper_disable (const struct device * dev )
30
+ static int tmc22xx_stepper_get_micro_step_res (const struct device * dev ,
31
+ enum stepper_micro_step_resolution * micro_step_res )
37
32
{
38
- const struct tmc22xx_config * config = dev -> config ;
33
+ const struct tmc22xx_data * data = dev -> data ;
39
34
40
- LOG_DBG ( "Disabling Stepper motor controller %s" , dev -> name ) ;
41
- return gpio_pin_set_dt ( & config -> enable_pin , 0 ) ;
35
+ * micro_step_res = data -> resolution ;
36
+ return 0 ;
42
37
}
43
38
44
39
static int tmc22xx_stepper_set_micro_step_res (const struct device * dev ,
@@ -78,15 +73,6 @@ static int tmc22xx_stepper_set_micro_step_res(const struct device *dev,
78
73
return - ENOTSUP ;
79
74
}
80
75
81
- static int tmc22xx_stepper_get_micro_step_res (const struct device * dev ,
82
- enum stepper_micro_step_resolution * micro_step_res )
83
- {
84
- struct tmc22xx_data * data = dev -> data ;
85
-
86
- * micro_step_res = data -> resolution ;
87
- return 0 ;
88
- }
89
-
90
76
static int tmc22xx_stepper_configure_msx_pins (const struct device * dev )
91
77
{
92
78
const struct tmc22xx_config * config = dev -> config ;
@@ -110,17 +96,11 @@ static int tmc22xx_stepper_configure_msx_pins(const struct device *dev)
110
96
static int tmc22xx_stepper_init (const struct device * dev )
111
97
{
112
98
const struct tmc22xx_config * config = dev -> config ;
113
- struct tmc22xx_data * data = dev -> data ;
114
- int ret ;
99
+ const struct tmc22xx_data * data = dev -> data ;
115
100
116
- if (!gpio_is_ready_dt (& config -> enable_pin )) {
117
- LOG_ERR ("GPIO pins are not ready" );
118
- return - ENODEV ;
119
- }
120
-
121
- ret = gpio_pin_configure_dt (& config -> enable_pin , GPIO_OUTPUT );
101
+ int ret = step_dir_interface_init (dev );
122
102
if (ret < 0 ) {
123
- LOG_ERR ("Failed to configure enable pin : %d" , ret );
103
+ LOG_ERR ("Failed to init step dir interface : %d" , ret );
124
104
return ret ;
125
105
}
126
106
@@ -138,7 +118,7 @@ static int tmc22xx_stepper_init(const struct device *dev)
138
118
}
139
119
}
140
120
141
- ret = step_dir_stepper_common_init (dev );
121
+ ret = stepper_motion_controller_init (dev );
142
122
if (ret < 0 ) {
143
123
LOG_ERR ("Failed to init step dir common stepper: %d" , ret );
144
124
return ret ;
@@ -147,43 +127,55 @@ static int tmc22xx_stepper_init(const struct device *dev)
147
127
return 0 ;
148
128
}
149
129
130
+ static const struct stepper_motion_controller_callbacks_api motion_controller_callbacks = {
131
+ .step = stepper_common_step ,
132
+ .set_direction = stepper_common_set_direction ,
133
+ .event = stepper_common_handle_event ,
134
+ };
135
+
150
136
static DEVICE_API (stepper , tmc22xx_stepper_api ) = {
151
- .enable = tmc22xx_stepper_enable ,
152
- .disable = tmc22xx_stepper_disable ,
153
- .move_by = step_dir_stepper_common_move_by ,
154
- .is_moving = step_dir_stepper_common_is_moving ,
155
- .set_reference_position = step_dir_stepper_common_set_reference_position ,
156
- .get_actual_position = step_dir_stepper_common_get_actual_position ,
157
- .move_to = step_dir_stepper_common_move_to ,
158
- .set_microstep_interval = step_dir_stepper_common_set_microstep_interval ,
159
- .run = step_dir_stepper_common_run ,
160
- .stop = step_dir_stepper_common_stop ,
161
- .set_event_callback = step_dir_stepper_common_set_event_callback ,
137
+ .enable = stepper_common_enable ,
138
+ .disable = stepper_common_disable ,
139
+ .move_by = stepper_common_move_by ,
140
+ .is_moving = stepper_common_is_moving ,
141
+ .set_reference_position = stepper_common_set_position ,
142
+ .get_actual_position = stepper_common_get_position ,
143
+ .move_to = stepper_common_move_to ,
144
+ .run = stepper_common_run ,
145
+ .stop = stepper_common_stop ,
146
+ .set_event_callback = stepper_common_set_event_callback ,
162
147
.set_micro_step_res = tmc22xx_stepper_set_micro_step_res ,
163
148
.get_micro_step_res = tmc22xx_stepper_get_micro_step_res ,
149
+ .set_ramp = stepper_motion_controller_set_ramp ,
164
150
};
165
151
166
152
#define TMC22XX_STEPPER_DEFINE (inst , msx_table ) \
167
- IF_ENABLED(DT_INST_NODE_HAS_PROP(inst, msx_gpios), ( \
168
- static const struct gpio_dt_spec tmc22xx_stepper_msx_pins_##inst[] = { \
169
- DT_INST_FOREACH_PROP_ELEM_SEP( \
170
- inst, msx_gpios, GPIO_DT_SPEC_GET_BY_IDX, (,) \
171
- ), \
153
+ STEPPER_MOTION_CONTROLLER_DT_INST_DEFINE(inst, &motion_controller_callbacks) \
154
+ STEPPER_INTERFACE_STEP_DIR_DT_INST_DEFINE(inst) \
155
+ static const struct stepper_common_config common_cfg_##inst = { \
156
+ .motion_controller = STEPPER_MOTION_CONTROLLER_DT_INST_GET(inst), \
157
+ .interface = STEPPER_INTERFACE_STEP_DIR_DT_INST_GET(inst), \
158
+ .step = step_dir_interface_step, \
159
+ .set_direction = step_dir_interface_set_dir, \
172
160
}; \
173
- BUILD_ASSERT( \
174
- ARRAY_SIZE(tmc22xx_stepper_msx_pins_##inst) == MSX_PIN_COUNT, \
175
- "Two microstep config pins needed"); \
176
- )) \
177
- \
161
+ static struct stepper_common_data common_data_##inst = {}; \
162
+ IF_ENABLED(DT_INST_NODE_HAS_PROP(inst, msx_gpios), ( \
163
+ static const struct gpio_dt_spec tmc22xx_stepper_msx_pins_## inst[] = { \
164
+ DT_INST_FOREACH_PROP_ELEM_SEP( \
165
+ inst, msx_gpios, GPIO_DT_SPEC_GET_BY_IDX, (,) \
166
+ ), \
167
+ }; \
168
+ BUILD_ASSERT( \
169
+ ARRAY_SIZE(tmc22xx_stepper_msx_pins_## inst) == MSX_PIN_COUNT, \
170
+ "Two microstep config pins needed"); \
171
+ )) \
178
172
static const struct tmc22xx_config tmc22xx_config_##inst = { \
179
- .common = STEP_DIR_STEPPER_DT_INST_COMMON_CONFIG_INIT(inst), \
180
- .enable_pin = GPIO_DT_SPEC_INST_GET(inst, en_gpios), \
173
+ .common = &common_cfg_##inst, \
181
174
.msx_resolutions = msx_table, \
182
- IF_ENABLED(DT_INST_NODE_HAS_PROP(inst, msx_gpios), \
183
- (.msx_pins = tmc22xx_stepper_msx_pins_##inst)) \
184
- }; \
175
+ IF_ENABLED(DT_INST_NODE_HAS_PROP(inst, msx_gpios), \
176
+ (.msx_pins = tmc22xx_stepper_msx_pins_## inst)) }; \
185
177
static struct tmc22xx_data tmc22xx_data_##inst = { \
186
- .common = STEP_DIR_STEPPER_DT_INST_COMMON_DATA_INIT( inst), \
178
+ .common = &common_data_## inst, \
187
179
.resolution = DT_INST_PROP(inst, micro_step_res), \
188
180
}; \
189
181
DEVICE_DT_INST_DEFINE(inst, tmc22xx_stepper_init, NULL, &tmc22xx_data_##inst, \
@@ -198,4 +190,5 @@ static enum stepper_micro_step_resolution tmc2209_msx_resolutions[MSX_PIN_STATE_
198
190
STEPPER_MICRO_STEP_16 ,
199
191
};
200
192
DT_INST_FOREACH_STATUS_OKAY_VARGS (TMC22XX_STEPPER_DEFINE , tmc2209_msx_resolutions )
193
+
201
194
#undef DT_DRV_COMPAT
0 commit comments