diff options
author | Kevin O'Connor <kevin@koconnor.net> | 2017-03-05 15:00:15 -0500 |
---|---|---|
committer | Kevin O'Connor <kevin@koconnor.net> | 2017-03-09 14:54:52 -0500 |
commit | a38437f378ece33cb762b482120be629c78d3540 (patch) | |
tree | a4a4ec60221aecbb3e41a5854f75bbaf629065d4 /src/stepper.c | |
parent | 8d6ecd9af87d3e0da312588bacb1c43332412107 (diff) | |
download | kutter-a38437f378ece33cb762b482120be629c78d3540.tar.gz kutter-a38437f378ece33cb762b482120be629c78d3540.tar.xz kutter-a38437f378ece33cb762b482120be629c78d3540.zip |
stepper: Introduce stepper_get_position command and remove from endstop.c
Move the logic to calculate and report the stepper's current position
from endstop.c to stepper.c. This localizes the stepper code into
stepper.c.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Diffstat (limited to 'src/stepper.c')
-rw-r--r-- | src/stepper.c | 32 |
1 files changed, 27 insertions, 5 deletions
diff --git a/src/stepper.c b/src/stepper.c index 39fdab54..b196dc61 100644 --- a/src/stepper.c +++ b/src/stepper.c @@ -46,6 +46,8 @@ struct stepper { uint8_t flags : 8; }; +enum { POSITION_BIAS=0x40000000 }; + enum { SF_LAST_DIR=1<<0, SF_NEXT_DIR=1<<1, SF_INVERT_STEP=1<<2, SF_HAVE_ADD=1<<3, SF_LAST_RESET=1<<4, SF_NO_NEXT_CHECK=1<<5 }; @@ -152,18 +154,25 @@ command_config_stepper(uint32_t *args) s->step_pin = gpio_out_setup(args[1], s->flags & SF_INVERT_STEP ? 1 : 0); s->dir_pin = gpio_out_setup(args[2], 0); s->min_stop_interval = args[3]; - s->position = -STEPPER_POSITION_BIAS; + s->position = -POSITION_BIAS; move_request_size(sizeof(struct stepper_move)); } DECL_COMMAND(command_config_stepper, "config_stepper oid=%c step_pin=%c dir_pin=%c" " min_stop_interval=%u invert_step=%c"); +// Return the 'struct stepper' for a given stepper oid +struct stepper * +stepper_oid_lookup(uint8_t oid) +{ + return oid_lookup(oid, command_config_stepper); +} + // Schedule a set of steps with a given timing void command_queue_step(uint32_t *args) { - struct stepper *s = oid_lookup(args[0], command_config_stepper); + struct stepper *s = stepper_oid_lookup(args[0]); struct stepper_move *m = move_alloc(); m->interval = args[1]; m->count = args[2]; @@ -204,7 +213,7 @@ DECL_COMMAND(command_queue_step, void command_set_next_step_dir(uint32_t *args) { - struct stepper *s = oid_lookup(args[0], command_config_stepper); + struct stepper *s = stepper_oid_lookup(args[0]); uint8_t nextdir = args[1] ? SF_NEXT_DIR : 0; irq_disable(); s->flags = (s->flags & ~SF_NEXT_DIR) | nextdir; @@ -216,7 +225,7 @@ DECL_COMMAND(command_set_next_step_dir, "set_next_step_dir oid=%c dir=%c"); void command_reset_step_clock(uint32_t *args) { - struct stepper *s = oid_lookup(args[0], command_config_stepper); + struct stepper *s = stepper_oid_lookup(args[0]); uint32_t waketime = args[1]; irq_disable(); if (s->count) @@ -228,7 +237,7 @@ command_reset_step_clock(uint32_t *args) DECL_COMMAND(command_reset_step_clock, "reset_step_clock oid=%c clock=%u"); // Return the current stepper position. Caller must disable irqs. -uint32_t +static uint32_t stepper_get_position(struct stepper *s) { uint32_t position = s->position; @@ -241,6 +250,19 @@ stepper_get_position(struct stepper *s) return position; } +// Report the current position of the stepper +void +command_stepper_get_position(uint32_t *args) +{ + uint8_t oid = args[0]; + struct stepper *s = stepper_oid_lookup(oid); + irq_disable(); + uint32_t position = stepper_get_position(s); + irq_enable(); + sendf("stepper_position oid=%c pos=%i", oid, position - POSITION_BIAS); +} +DECL_COMMAND(command_stepper_get_position, "stepper_get_position oid=%c"); + // Stop all moves for a given stepper (used in end stop homing). IRQs // must be off. void |