@@ -11,7 +11,6 @@ LOG_MODULE_REGISTER(step_dir_stepper, CONFIG_STEPPER_LOG_LEVEL);
11
11
static inline int step_dir_stepper_perform_step (const struct device * dev )
12
12
{
13
13
const struct step_dir_stepper_common_config * config = dev -> config ;
14
- struct step_dir_stepper_common_data * data = dev -> data ;
15
14
int ret ;
16
15
17
16
ret = gpio_pin_toggle_dt (& config -> step_pin );
@@ -28,12 +27,6 @@ static inline int step_dir_stepper_perform_step(const struct device *dev)
28
27
}
29
28
}
30
29
31
- if (data -> direction == STEPPER_DIRECTION_POSITIVE ) {
32
- data -> actual_position ++ ;
33
- } else {
34
- data -> actual_position -- ;
35
- }
36
-
37
30
return 0 ;
38
31
}
39
32
@@ -120,25 +113,20 @@ static void stepper_work_event_handler(struct k_work *work)
120
113
121
114
static void update_remaining_steps (struct step_dir_stepper_common_data * data )
122
115
{
123
- const struct step_dir_stepper_common_config * config = data -> dev -> config ;
124
-
125
- if (data -> step_count > 0 ) {
126
- data -> step_count -- ;
127
- } else if (data -> step_count < 0 ) {
128
- data -> step_count ++ ;
129
- } else {
130
- stepper_trigger_callback (data -> dev , STEPPER_EVENT_STEPS_COMPLETED );
131
- config -> timing_source -> stop (data -> dev );
116
+ if (atomic_get (& data -> step_count ) > 0 ) {
117
+ atomic_dec (& data -> step_count );
118
+ } else if (atomic_get (& data -> step_count ) < 0 ) {
119
+ atomic_inc (& data -> step_count );
132
120
}
133
121
}
134
122
135
123
static void update_direction_from_step_count (const struct device * dev )
136
124
{
137
125
struct step_dir_stepper_common_data * data = dev -> data ;
138
126
139
- if (data -> step_count > 0 ) {
127
+ if (atomic_get ( & data -> step_count ) > 0 ) {
140
128
data -> direction = STEPPER_DIRECTION_POSITIVE ;
141
- } else if (data -> step_count < 0 ) {
129
+ } else if (atomic_get ( & data -> step_count ) < 0 ) {
142
130
data -> direction = STEPPER_DIRECTION_NEGATIVE ;
143
131
} else {
144
132
LOG_ERR ("Step count is zero" );
@@ -150,23 +138,20 @@ static void position_mode_task(const struct device *dev)
150
138
struct step_dir_stepper_common_data * data = dev -> data ;
151
139
const struct step_dir_stepper_common_config * config = dev -> config ;
152
140
153
- if (data -> step_count ) {
154
- (void )step_dir_stepper_perform_step (dev );
155
- }
141
+ update_remaining_steps (dev -> data );
156
142
157
- if (config -> timing_source -> needs_reschedule (dev ) && data -> step_count != 0 ) {
143
+ if (config -> timing_source -> needs_reschedule (dev ) && atomic_get ( & data -> step_count ) != 0 ) {
158
144
(void )config -> timing_source -> start (dev );
145
+ } else if (atomic_get (& data -> step_count ) == 0 ) {
146
+ stepper_trigger_callback (data -> dev , STEPPER_EVENT_STEPS_COMPLETED );
147
+ config -> timing_source -> stop (data -> dev );
159
148
}
160
-
161
- update_remaining_steps (dev -> data );
162
149
}
163
150
164
151
static void velocity_mode_task (const struct device * dev )
165
152
{
166
153
const struct step_dir_stepper_common_config * config = dev -> config ;
167
154
168
- (void )step_dir_stepper_perform_step (dev );
169
-
170
155
if (config -> timing_source -> needs_reschedule (dev )) {
171
156
(void )config -> timing_source -> start (dev );
172
157
}
@@ -176,18 +161,23 @@ void stepper_handle_timing_signal(const struct device *dev)
176
161
{
177
162
struct step_dir_stepper_common_data * data = dev -> data ;
178
163
179
- K_SPINLOCK (& data -> lock ) {
180
- switch (data -> run_mode ) {
181
- case STEPPER_RUN_MODE_POSITION :
182
- position_mode_task (dev );
183
- break ;
184
- case STEPPER_RUN_MODE_VELOCITY :
185
- velocity_mode_task (dev );
186
- break ;
187
- default :
188
- LOG_WRN ("Unsupported run mode: %d" , data -> run_mode );
189
- break ;
190
- }
164
+ (void )step_dir_stepper_perform_step (dev );
165
+ if (data -> direction == STEPPER_DIRECTION_POSITIVE ) {
166
+ atomic_inc (& data -> actual_position );
167
+ } else {
168
+ atomic_dec (& data -> actual_position );
169
+ }
170
+
171
+ switch (data -> run_mode ) {
172
+ case STEPPER_RUN_MODE_POSITION :
173
+ position_mode_task (dev );
174
+ break ;
175
+ case STEPPER_RUN_MODE_VELOCITY :
176
+ velocity_mode_task (dev );
177
+ break ;
178
+ default :
179
+ LOG_WRN ("Unsupported run mode: %d" , data -> run_mode );
180
+ break ;
191
181
}
192
182
}
193
183
@@ -251,7 +241,7 @@ int step_dir_stepper_common_move_by(const struct device *dev, const int32_t micr
251
241
252
242
K_SPINLOCK (& data -> lock ) {
253
243
data -> run_mode = STEPPER_RUN_MODE_POSITION ;
254
- data -> step_count = micro_steps ;
244
+ atomic_set ( & data -> step_count , micro_steps ) ;
255
245
update_direction_from_step_count (dev );
256
246
ret = update_dir_pin (dev );
257
247
if (ret < 0 ) {
@@ -265,7 +255,7 @@ int step_dir_stepper_common_move_by(const struct device *dev, const int32_t micr
265
255
}
266
256
267
257
int step_dir_stepper_common_set_microstep_interval (const struct device * dev ,
268
- const uint64_t microstep_interval_ns )
258
+ const uint64_t microstep_interval_ns )
269
259
{
270
260
struct step_dir_stepper_common_data * data = dev -> data ;
271
261
const struct step_dir_stepper_common_config * config = dev -> config ;
@@ -298,9 +288,7 @@ int step_dir_stepper_common_get_actual_position(const struct device *dev, int32_
298
288
{
299
289
struct step_dir_stepper_common_data * data = dev -> data ;
300
290
301
- K_SPINLOCK (& data -> lock ) {
302
- * value = data -> actual_position ;
303
- }
291
+ * value = atomic_get (& data -> actual_position );
304
292
305
293
return 0 ;
306
294
}
@@ -311,9 +299,7 @@ int step_dir_stepper_common_move_to(const struct device *dev, const int32_t valu
311
299
int32_t steps_to_move ;
312
300
313
301
/* Calculate the relative movement required */
314
- K_SPINLOCK (& data -> lock ) {
315
- steps_to_move = value - data -> actual_position ;
316
- }
302
+ steps_to_move = value - atomic_get (& data -> actual_position );
317
303
318
304
return step_dir_stepper_common_move_by (dev , steps_to_move );
319
305
}
0 commit comments