aboutsummaryrefslogtreecommitdiffstats
path: root/src/endstop.c
blob: 5bd863ea7ee54e99816ea4908d940df4c2866226 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Handling of end stops.
//
// Copyright (C) 2016  Kevin O'Connor <kevin@koconnor.net>
//
// This file may be distributed under the terms of the GNU GPLv3 license.

#include "basecmd.h" // oid_alloc
#include "board/gpio.h" // struct gpio
#include "board/irq.h" // irq_disable
#include "command.h" // DECL_COMMAND
#include "sched.h" // struct timer
#include "stepper.h" // stepper_stop

struct end_stop {
    struct timer time;
    uint32_t rest_time;
    struct gpio_in pin;
    uint8_t flags, stepper_count;
    struct stepper *steppers[0];
};

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 end_stop *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);
}

// Timer callback for an end stop
static uint_fast8_t
end_stop_event(struct timer *t)
{
    struct end_stop *e = container_of(t, struct end_stop, time);
    uint8_t val = gpio_in_read(e->pin);
    if ((val ? ~e->flags : e->flags) & ESF_PIN_HIGH) {
        // No match - reschedule for the next attempt
        e->time.waketime += e->rest_time;
        return SF_RESCHEDULE;
    }
    stop_steppers(e);
    return SF_DONE;
}

void
command_config_end_stop(uint32_t *args)
{
    uint8_t stepper_count = args[3];
    struct end_stop *e = oid_alloc(
        args[0], command_config_end_stop
        , sizeof(*e) + sizeof(e->steppers[0]) * stepper_count);
    e->time.func = end_stop_event;
    e->pin = gpio_in_setup(args[1], args[2]);
    e->stepper_count = stepper_count;
}
DECL_COMMAND(command_config_end_stop,
             "config_end_stop oid=%c pin=%c pull_up=%c stepper_count=%c");

void
command_end_stop_set_stepper(uint32_t *args)
{
    struct end_stop *e = oid_lookup(args[0], command_config_end_stop);
    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_end_stop_set_stepper,
             "end_stop_set_stepper oid=%c pos=%c stepper_oid=%c");

// Home an axis
void
command_end_stop_home(uint32_t *args)
{
    struct end_stop *e = oid_lookup(args[0], command_config_end_stop);
    sched_del_timer(&e->time);
    e->time.waketime = args[1];
    e->rest_time = args[2];
    if (!e->rest_time) {
        // Disable end stop checking
        e->flags = 0;
        return;
    }
    e->flags = ESF_HOMING | (args[3] ? ESF_PIN_HIGH : 0);
    sched_add_timer(&e->time);
}
DECL_COMMAND(command_end_stop_home,
             "end_stop_home oid=%c clock=%u rest_ticks=%u pin_value=%c");

static void
end_stop_report(uint8_t oid, struct end_stop *e)
{
    irq_disable();
    uint8_t eflags = e->flags;
    e->flags &= ~ESF_REPORT;
    irq_enable();

    sendf("end_stop_state oid=%c homing=%c pin=%c"
          , oid, !!(eflags & ESF_HOMING), gpio_in_read(e->pin));
}

void
command_end_stop_query(uint32_t *args)
{
    uint8_t oid = args[0];
    struct end_stop *e = oid_lookup(oid, command_config_end_stop);
    end_stop_report(oid, e);
}
DECL_COMMAND(command_end_stop_query, "end_stop_query oid=%c");

void
end_stop_task(void)
{
    if (!sched_check_wake(&endstop_wake))
        return;
    uint8_t oid;
    struct end_stop *e;
    foreach_oid(oid, e, command_config_end_stop) {
        if (!(e->flags & ESF_REPORT))
            continue;
        end_stop_report(oid, e);
    }
}
DECL_TASK(end_stop_task);