Skip to content

Commit 0687522

Browse files
jilaypandyahenrikbrixandersen
authored andcommitted
drivers: stepper: introduce invert-direction property to gpio-stepper
This commit introduces invert-direction property to gpio-stepper Signed-off-by: Jilay Pandya <[email protected]>
1 parent 367f853 commit 0687522

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

drivers/stepper/gpio_stepper_controller.c

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ static const uint8_t
2525

2626
struct gpio_stepper_config {
2727
const struct gpio_dt_spec *control_pins;
28+
bool invert_direction;
2829
};
2930

3031
struct gpio_stepper_data {
@@ -54,20 +55,38 @@ static int stepper_motor_set_coil_charge(const struct device *dev)
5455
return 0;
5556
}
5657

58+
static void increment_coil_charge(const struct device *dev)
59+
{
60+
struct gpio_stepper_data *data = dev->data;
61+
62+
if (data->coil_charge == NUM_CONTROL_PINS * MAX_MICRO_STEP_RES - data->step_gap) {
63+
data->coil_charge = 0;
64+
} else {
65+
data->coil_charge = data->coil_charge + data->step_gap;
66+
}
67+
}
68+
69+
static void decrement_coil_charge(const struct device *dev)
70+
{
71+
struct gpio_stepper_data *data = dev->data;
72+
73+
if (data->coil_charge == 0) {
74+
data->coil_charge = NUM_CONTROL_PINS * MAX_MICRO_STEP_RES - data->step_gap;
75+
} else {
76+
data->coil_charge = data->coil_charge - data->step_gap;
77+
}
78+
}
79+
5780
static void update_coil_charge(const struct device *dev)
5881
{
82+
const struct gpio_stepper_config *config = dev->config;
5983
struct gpio_stepper_data *data = dev->data;
6084

6185
if (data->direction == STEPPER_DIRECTION_POSITIVE) {
62-
data->coil_charge = data->coil_charge == 0
63-
? NUM_CONTROL_PINS * MAX_MICRO_STEP_RES - data->step_gap
64-
: data->coil_charge - data->step_gap;
86+
config->invert_direction ? decrement_coil_charge(dev) : increment_coil_charge(dev);
6587
data->actual_position++;
6688
} else if (data->direction == STEPPER_DIRECTION_NEGATIVE) {
67-
data->coil_charge =
68-
data->coil_charge == NUM_CONTROL_PINS * MAX_MICRO_STEP_RES - data->step_gap
69-
? 0
70-
: data->coil_charge + data->step_gap;
89+
config->invert_direction ? increment_coil_charge(dev) : decrement_coil_charge(dev);
7190
data->actual_position--;
7291
}
7392
}
@@ -89,8 +108,10 @@ static void update_remaining_steps(struct gpio_stepper_data *data)
89108
}
90109
}
91110

92-
static void update_direction_from_step_count(struct gpio_stepper_data *data)
111+
static void update_direction_from_step_count(const struct device *dev)
93112
{
113+
struct gpio_stepper_data *data = dev->data;
114+
94115
if (data->step_count > 0) {
95116
data->direction = STEPPER_DIRECTION_POSITIVE;
96117
} else if (data->step_count < 0) {
@@ -152,7 +173,7 @@ static int gpio_stepper_move(const struct device *dev, int32_t micro_steps)
152173
K_SPINLOCK(&data->lock) {
153174
data->run_mode = STEPPER_RUN_MODE_POSITION;
154175
data->step_count = micro_steps;
155-
update_direction_from_step_count(data);
176+
update_direction_from_step_count(dev);
156177
(void)k_work_reschedule(&data->stepper_dwork, K_NO_WAIT);
157178
}
158179
return 0;
@@ -189,7 +210,7 @@ static int gpio_stepper_set_target_position(const struct device *dev, int32_t po
189210
K_SPINLOCK(&data->lock) {
190211
data->run_mode = STEPPER_RUN_MODE_POSITION;
191212
data->step_count = position - data->actual_position;
192-
update_direction_from_step_count(data);
213+
update_direction_from_step_count(dev);
193214
(void)k_work_reschedule(&data->stepper_dwork, K_NO_WAIT);
194215
}
195216
return 0;
@@ -327,6 +348,7 @@ static int gpio_stepper_motor_controller_init(const struct device *dev)
327348
ARRAY_SIZE(gpio_stepper_motor_control_pins_##child) == 4, \
328349
"gpio_stepper_controller driver currently supports only 4 wire configuration"); \
329350
static const struct gpio_stepper_config gpio_stepper_config_##child = { \
351+
.invert_direction = DT_PROP(child, invert_direction), \
330352
.control_pins = gpio_stepper_motor_control_pins_##child};
331353

332354
#define GPIO_STEPPER_API_DEFINE(child) \

dts/bindings/stepper/zephyr,gpio-stepper.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ child-binding:
2525
- name: stepper-controller.yaml
2626
property-allowlist:
2727
- micro-step-res
28+
- invert-direction
2829

2930
properties:
3031
gpios:

0 commit comments

Comments
 (0)