2016-05-25 18:37:40 +03:00
|
|
|
// Handling of end stops.
|
|
|
|
//
|
2019-03-19 17:48:33 +03:00
|
|
|
// Copyright (C) 2016-2019 Kevin O'Connor <kevin@koconnor.net>
|
2016-05-25 18:37:40 +03:00
|
|
|
//
|
|
|
|
// This file may be distributed under the terms of the GNU GPLv3 license.
|
|
|
|
|
2017-03-09 21:49:03 +03:00
|
|
|
#include "basecmd.h" // oid_alloc
|
2016-05-25 18:37:40 +03:00
|
|
|
#include "board/gpio.h" // struct gpio
|
2016-06-09 03:51:00 +03:00
|
|
|
#include "board/irq.h" // irq_disable
|
2016-05-25 18:37:40 +03:00
|
|
|
#include "command.h" // DECL_COMMAND
|
|
|
|
#include "sched.h" // struct timer
|
|
|
|
#include "stepper.h" // stepper_stop
|
|
|
|
|
2019-07-12 15:09:30 +03:00
|
|
|
struct endstop {
|
2016-05-25 18:37:40 +03:00
|
|
|
struct timer time;
|
|
|
|
struct gpio_in pin;
|
2017-10-12 17:39:46 +03:00
|
|
|
uint32_t rest_time, sample_time, nextwake;
|
2017-10-01 23:31:05 +03:00
|
|
|
uint8_t flags, stepper_count, sample_count, trigger_count;
|
2017-03-05 23:30:04 +03:00
|
|
|
struct stepper *steppers[0];
|
2016-05-25 18:37:40 +03:00
|
|
|
};
|
|
|
|
|
2017-03-05 22:10:19 +03:00
|
|
|
enum { ESF_PIN_HIGH=1<<0, ESF_HOMING=1<<1, ESF_REPORT=1<<2 };
|
2016-05-25 18:37:40 +03:00
|
|
|
|
2017-07-27 19:05:29 +03:00
|
|
|
static struct task_wake endstop_wake;
|
|
|
|
|
2017-05-11 18:56:11 +03:00
|
|
|
static void
|
2019-07-12 15:09:30 +03:00
|
|
|
stop_steppers(struct endstop *e)
|
2017-03-05 23:30:04 +03:00
|
|
|
{
|
|
|
|
e->flags = ESF_REPORT;
|
|
|
|
uint8_t count = e->stepper_count;
|
|
|
|
while (count--)
|
|
|
|
if (e->steppers[count])
|
|
|
|
stepper_stop(e->steppers[count]);
|
2017-07-27 19:05:29 +03:00
|
|
|
sched_wake_task(&endstop_wake);
|
2017-03-05 23:30:04 +03:00
|
|
|
}
|
|
|
|
|
2019-07-12 15:09:30 +03:00
|
|
|
static uint_fast8_t endstop_oversample_event(struct timer *t);
|
2017-10-01 23:31:05 +03:00
|
|
|
|
2016-05-25 18:37:40 +03:00
|
|
|
// Timer callback for an end stop
|
2016-06-09 04:33:50 +03:00
|
|
|
static uint_fast8_t
|
2019-07-12 15:09:30 +03:00
|
|
|
endstop_event(struct timer *t)
|
2016-05-25 18:37:40 +03:00
|
|
|
{
|
2019-07-12 15:09:30 +03:00
|
|
|
struct endstop *e = container_of(t, struct endstop, time);
|
2016-05-25 18:37:40 +03:00
|
|
|
uint8_t val = gpio_in_read(e->pin);
|
2017-10-12 17:39:46 +03:00
|
|
|
uint32_t nextwake = e->time.waketime + e->rest_time;
|
2017-03-05 22:10:19 +03:00
|
|
|
if ((val ? ~e->flags : e->flags) & ESF_PIN_HIGH) {
|
|
|
|
// No match - reschedule for the next attempt
|
2017-10-12 17:39:46 +03:00
|
|
|
e->time.waketime = nextwake;
|
2016-05-25 18:37:40 +03:00
|
|
|
return SF_RESCHEDULE;
|
|
|
|
}
|
2017-10-12 17:39:46 +03:00
|
|
|
e->nextwake = nextwake;
|
2019-07-12 15:09:30 +03:00
|
|
|
e->time.func = endstop_oversample_event;
|
|
|
|
return endstop_oversample_event(t);
|
2017-10-01 23:31:05 +03:00
|
|
|
}
|
|
|
|
|
2017-10-02 05:38:28 +03:00
|
|
|
// Timer callback for an end stop that is sampling extra times
|
2017-10-01 23:31:05 +03:00
|
|
|
static uint_fast8_t
|
2019-07-12 15:09:30 +03:00
|
|
|
endstop_oversample_event(struct timer *t)
|
2017-10-01 23:31:05 +03:00
|
|
|
{
|
2019-07-12 15:09:30 +03:00
|
|
|
struct endstop *e = container_of(t, struct endstop, time);
|
2017-10-01 23:31:05 +03:00
|
|
|
uint8_t val = gpio_in_read(e->pin);
|
|
|
|
if ((val ? ~e->flags : e->flags) & ESF_PIN_HIGH) {
|
|
|
|
// No longer matching - reschedule for the next attempt
|
2019-07-12 15:09:30 +03:00
|
|
|
e->time.func = endstop_event;
|
2017-10-12 17:39:46 +03:00
|
|
|
e->time.waketime = e->nextwake;
|
2017-10-01 23:31:05 +03:00
|
|
|
e->trigger_count = e->sample_count;
|
|
|
|
return SF_RESCHEDULE;
|
|
|
|
}
|
|
|
|
uint8_t count = e->trigger_count - 1;
|
|
|
|
if (!count) {
|
|
|
|
stop_steppers(e);
|
|
|
|
return SF_DONE;
|
|
|
|
}
|
|
|
|
e->trigger_count = count;
|
|
|
|
e->time.waketime += e->sample_time;
|
|
|
|
return SF_RESCHEDULE;
|
2016-05-25 18:37:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-07-12 15:09:30 +03:00
|
|
|
command_config_endstop(uint32_t *args)
|
2016-05-25 18:37:40 +03:00
|
|
|
{
|
2017-03-05 23:30:04 +03:00
|
|
|
uint8_t stepper_count = args[3];
|
2019-07-12 15:09:30 +03:00
|
|
|
struct endstop *e = oid_alloc(
|
|
|
|
args[0], command_config_endstop
|
2017-03-05 23:30:04 +03:00
|
|
|
, sizeof(*e) + sizeof(e->steppers[0]) * stepper_count);
|
2016-05-25 18:37:40 +03:00
|
|
|
e->pin = gpio_in_setup(args[1], args[2]);
|
2017-03-05 23:30:04 +03:00
|
|
|
e->stepper_count = stepper_count;
|
2017-10-01 23:31:05 +03:00
|
|
|
e->sample_count = 1;
|
2016-05-25 18:37:40 +03:00
|
|
|
}
|
2019-07-12 15:09:30 +03:00
|
|
|
DECL_COMMAND(command_config_endstop,
|
|
|
|
"config_endstop oid=%c pin=%c pull_up=%c stepper_count=%c");
|
2017-03-05 23:30:04 +03:00
|
|
|
|
|
|
|
void
|
2019-07-12 15:09:30 +03:00
|
|
|
command_endstop_set_stepper(uint32_t *args)
|
2017-03-05 23:30:04 +03:00
|
|
|
{
|
2019-07-12 15:09:30 +03:00
|
|
|
struct endstop *e = oid_lookup(args[0], command_config_endstop);
|
2017-03-05 23:30:04 +03:00
|
|
|
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]);
|
|
|
|
}
|
2019-07-12 15:09:30 +03:00
|
|
|
DECL_COMMAND(command_endstop_set_stepper,
|
|
|
|
"endstop_set_stepper oid=%c pos=%c stepper_oid=%c");
|
2016-05-25 18:37:40 +03:00
|
|
|
|
|
|
|
// Home an axis
|
|
|
|
void
|
2019-07-12 15:09:30 +03:00
|
|
|
command_endstop_home(uint32_t *args)
|
2016-05-25 18:37:40 +03:00
|
|
|
{
|
2019-07-12 15:09:30 +03:00
|
|
|
struct endstop *e = oid_lookup(args[0], command_config_endstop);
|
2016-05-25 18:37:40 +03:00
|
|
|
sched_del_timer(&e->time);
|
|
|
|
e->time.waketime = args[1];
|
2017-10-12 17:39:46 +03:00
|
|
|
e->sample_time = args[2];
|
|
|
|
e->sample_count = args[3];
|
|
|
|
if (!e->sample_count) {
|
2016-05-25 18:37:40 +03:00
|
|
|
// Disable end stop checking
|
|
|
|
e->flags = 0;
|
|
|
|
return;
|
|
|
|
}
|
2017-10-12 17:39:46 +03:00
|
|
|
e->rest_time = args[4];
|
2019-07-12 15:09:30 +03:00
|
|
|
e->time.func = endstop_event;
|
2017-10-01 23:31:05 +03:00
|
|
|
e->trigger_count = e->sample_count;
|
2017-10-12 17:39:46 +03:00
|
|
|
e->flags = ESF_HOMING | (args[5] ? ESF_PIN_HIGH : 0);
|
2017-03-11 06:12:05 +03:00
|
|
|
sched_add_timer(&e->time);
|
2016-05-25 18:37:40 +03:00
|
|
|
}
|
2019-07-12 15:09:30 +03:00
|
|
|
DECL_COMMAND(command_endstop_home,
|
|
|
|
"endstop_home oid=%c clock=%u sample_ticks=%u sample_count=%c"
|
2017-10-12 17:39:46 +03:00
|
|
|
" rest_ticks=%u pin_value=%c");
|
2016-05-25 18:37:40 +03:00
|
|
|
|
|
|
|
static void
|
2019-07-12 15:09:30 +03:00
|
|
|
endstop_report(uint8_t oid, struct endstop *e)
|
2016-05-25 18:37:40 +03:00
|
|
|
{
|
2016-06-09 03:51:00 +03:00
|
|
|
irq_disable();
|
2016-05-25 18:37:40 +03:00
|
|
|
uint8_t eflags = e->flags;
|
|
|
|
e->flags &= ~ESF_REPORT;
|
2016-06-09 03:51:00 +03:00
|
|
|
irq_enable();
|
2016-05-25 18:37:40 +03:00
|
|
|
|
2019-07-12 15:09:30 +03:00
|
|
|
sendf("endstop_state oid=%c homing=%c pin_value=%c"
|
2017-03-05 23:00:15 +03:00
|
|
|
, oid, !!(eflags & ESF_HOMING), gpio_in_read(e->pin));
|
2016-05-25 18:37:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-07-12 15:09:30 +03:00
|
|
|
command_endstop_query_state(uint32_t *args)
|
2016-05-25 18:37:40 +03:00
|
|
|
{
|
|
|
|
uint8_t oid = args[0];
|
2019-07-12 15:09:30 +03:00
|
|
|
struct endstop *e = oid_lookup(oid, command_config_endstop);
|
|
|
|
endstop_report(oid, e);
|
2016-05-25 18:37:40 +03:00
|
|
|
}
|
2019-07-12 15:09:30 +03:00
|
|
|
DECL_COMMAND(command_endstop_query_state, "endstop_query_state oid=%c");
|
2016-05-25 18:37:40 +03:00
|
|
|
|
2017-05-26 16:14:26 +03:00
|
|
|
void
|
2019-07-12 15:09:30 +03:00
|
|
|
endstop_task(void)
|
2016-05-25 18:37:40 +03:00
|
|
|
{
|
2017-07-27 19:05:29 +03:00
|
|
|
if (!sched_check_wake(&endstop_wake))
|
2016-05-25 18:37:40 +03:00
|
|
|
return;
|
|
|
|
uint8_t oid;
|
2019-07-12 15:09:30 +03:00
|
|
|
struct endstop *e;
|
|
|
|
foreach_oid(oid, e, command_config_endstop) {
|
2016-05-25 18:37:40 +03:00
|
|
|
if (!(e->flags & ESF_REPORT))
|
|
|
|
continue;
|
2019-07-12 15:09:30 +03:00
|
|
|
endstop_report(oid, e);
|
2016-05-25 18:37:40 +03:00
|
|
|
}
|
|
|
|
}
|
2019-07-12 15:09:30 +03:00
|
|
|
DECL_TASK(endstop_task);
|