trsync: Introduce new "trigger synchronization" support

Separate out the stepper stopping code from endstop.c into its own
trsync.c code file.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2021-02-04 10:07:13 -05:00
parent f3bd4e6acf
commit 05c2d51a12
7 changed files with 299 additions and 110 deletions

View file

@ -1,6 +1,6 @@
// Handling of end stops.
//
// Copyright (C) 2016-2019 Kevin O'Connor <kevin@koconnor.net>
// Copyright (C) 2016-2021 Kevin O'Connor <kevin@koconnor.net>
//
// This file may be distributed under the terms of the GNU GPLv3 license.
@ -9,30 +9,17 @@
#include "board/irq.h" // irq_disable
#include "command.h" // DECL_COMMAND
#include "sched.h" // struct timer
#include "stepper.h" // stepper_stop
#include "trsync.h" // trsync_do_trigger
struct endstop {
struct timer time;
struct gpio_in pin;
uint32_t rest_time, sample_time, nextwake;
uint8_t flags, stepper_count, sample_count, trigger_count;
struct stepper *steppers[0];
struct trsync *ts;
uint8_t flags, sample_count, trigger_count, trigger_reason;
};
enum { ESF_PIN_HIGH=1<<0, ESF_HOMING=1<<1, ESF_REPORT=1<<2 };
static struct task_wake endstop_wake;
static void
stop_steppers(struct endstop *e)
{
e->flags = ESF_REPORT;
uint8_t count = e->stepper_count;
while (count--)
if (e->steppers[count])
stepper_stop(e->steppers[count]);
sched_wake_task(&endstop_wake);
}
enum { ESF_PIN_HIGH=1<<0, ESF_HOMING=1<<1 };
static uint_fast8_t endstop_oversample_event(struct timer *t);
@ -68,7 +55,7 @@ endstop_oversample_event(struct timer *t)
}
uint8_t count = e->trigger_count - 1;
if (!count) {
stop_steppers(e);
trsync_do_trigger(e->ts, e->trigger_reason);
return SF_DONE;
}
e->trigger_count = count;
@ -79,28 +66,10 @@ endstop_oversample_event(struct timer *t)
void
command_config_endstop(uint32_t *args)
{
uint8_t stepper_count = args[3];
struct endstop *e = oid_alloc(
args[0], command_config_endstop
, sizeof(*e) + sizeof(e->steppers[0]) * stepper_count);
struct endstop *e = oid_alloc(args[0], command_config_endstop, sizeof(*e));
e->pin = gpio_in_setup(args[1], args[2]);
e->stepper_count = stepper_count;
e->sample_count = 1;
}
DECL_COMMAND(command_config_endstop,
"config_endstop oid=%c pin=%c pull_up=%c stepper_count=%c");
void
command_endstop_set_stepper(uint32_t *args)
{
struct endstop *e = oid_lookup(args[0], command_config_endstop);
uint8_t pos = args[1];
if (pos >= e->stepper_count)
shutdown("Set stepper past maximum stepper count");
e->steppers[pos] = stepper_oid_lookup(args[2]);
}
DECL_COMMAND(command_endstop_set_stepper,
"endstop_set_stepper oid=%c pos=%c stepper_oid=%c");
DECL_COMMAND(command_config_endstop, "config_endstop oid=%c pin=%c pull_up=%c");
// Home an axis
void
@ -113,6 +82,7 @@ command_endstop_home(uint32_t *args)
e->sample_count = args[3];
if (!e->sample_count) {
// Disable end stop checking
e->ts = NULL;
e->flags = 0;
return;
}
@ -120,45 +90,26 @@ command_endstop_home(uint32_t *args)
e->time.func = endstop_event;
e->trigger_count = e->sample_count;
e->flags = ESF_HOMING | (args[5] ? ESF_PIN_HIGH : 0);
e->ts = trsync_oid_lookup(args[6]);
e->trigger_reason = args[7];
sched_add_timer(&e->time);
}
DECL_COMMAND(command_endstop_home,
"endstop_home oid=%c clock=%u sample_ticks=%u sample_count=%c"
" rest_ticks=%u pin_value=%c");
static void
endstop_report(uint8_t oid, struct endstop *e)
{
irq_disable();
uint8_t eflags = e->flags;
e->flags &= ~ESF_REPORT;
uint32_t nextwake = e->nextwake;
irq_enable();
sendf("endstop_state oid=%c homing=%c next_clock=%u pin_value=%c"
, oid, !!(eflags & ESF_HOMING), nextwake, gpio_in_read(e->pin));
}
" rest_ticks=%u pin_value=%c trsync_oid=%c trigger_reason=%c");
void
command_endstop_query_state(uint32_t *args)
{
uint8_t oid = args[0];
struct endstop *e = oid_lookup(oid, command_config_endstop);
endstop_report(oid, e);
irq_disable();
uint8_t eflags = e->flags;
uint32_t nextwake = e->nextwake;
irq_enable();
sendf("endstop_state oid=%c homing=%c next_clock=%u pin_value=%c"
, oid, !!(eflags & ESF_HOMING), nextwake, gpio_in_read(e->pin));
}
DECL_COMMAND(command_endstop_query_state, "endstop_query_state oid=%c");
void
endstop_task(void)
{
if (!sched_check_wake(&endstop_wake))
return;
uint8_t oid;
struct endstop *e;
foreach_oid(oid, e, command_config_endstop) {
if (!(e->flags & ESF_REPORT))
continue;
endstop_report(oid, e);
}
}
DECL_TASK(endstop_task);