4 Copyright 2008-2012 Michel Pollet <buserror@gmail.com>
6 This file is part of simavr.
8 simavr is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 simavr is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with simavr. If not, see <http://www.gnu.org/licenses/>.
31 struct avr_irq_t * irq,
35 stepper_p p = (stepper_p)param;
36 printf("%s (%s) %d\n", __func__, p->name, value);
42 struct avr_irq_t * irq,
46 stepper_p p = (stepper_p)param;
48 printf("%s (%s) %d pos %.4f\n", __func__, p->name, p->enable != 0, p->position);
49 avr_raise_irq(p->irq + IRQ_STEPPER_ENDSTOP_OUT, p->position == p->endstop);
54 struct avr_irq_t * irq,
58 stepper_p p = (stepper_p)param;
63 p->position += (p->dir ? -1.0 : 1.0) / p->steps_per_mm;
66 if (p->max_position > 0 && p->position > p->max_position)
67 p->position = p->max_position;
68 printf("%s (%s) %.4f\n", __func__, p->name, p->position);
72 } m = { .f = p->position };
73 avr_raise_irq(p->irq + IRQ_STEPPER_POSITION_OUT, m.i);
74 avr_raise_irq(p->irq + IRQ_STEPPER_ENDSTOP_OUT, p->position == p->endstop);
77 static const char * irq_names[IRQ_STEPPER_COUNT] = {
78 [IRQ_STEPPER_DIR_IN] = "1<stepper.direction",
79 [IRQ_STEPPER_STEP_IN] = "1>stepper.step",
80 [IRQ_STEPPER_ENABLE_IN] = "1<stepper.enable",
81 [IRQ_STEPPER_POSITION_OUT] = "32<stepper.position",
82 [IRQ_STEPPER_ENDSTOP_OUT] = "1<stepper.endstop",
93 float endstop_position)
96 strcpy(p->name, name);
97 p->irq = avr_alloc_irq(&avr->irq_pool, 0, IRQ_STEPPER_COUNT, irq_names);
98 avr_irq_register_notify(p->irq + IRQ_STEPPER_DIR_IN, stepper_dir_hook, p);
99 avr_irq_register_notify(p->irq + IRQ_STEPPER_STEP_IN, stepper_step_hook, p);
100 avr_irq_register_notify(p->irq + IRQ_STEPPER_ENABLE_IN, stepper_enable_hook, p);
102 p->steps_per_mm = steps_per_mm;
103 p->position = start_position;
104 p->max_position = max_position;
105 p->endstop = endstop_position;
117 avr_connect_irq(step, p->irq + IRQ_STEPPER_STEP_IN);
118 avr_connect_irq(dir, p->irq + IRQ_STEPPER_DIR_IN);
119 avr_connect_irq(enable, p->irq + IRQ_STEPPER_ENABLE_IN);
120 p->irq[IRQ_STEPPER_ENDSTOP_OUT].flags |= IRQ_FLAG_FILTERED;
122 avr_connect_irq(p->irq + IRQ_STEPPER_ENDSTOP_OUT, endstop);
123 if (flags & stepper_endstop_inverted)
124 p->irq[IRQ_STEPPER_ENDSTOP_OUT].flags |= IRQ_FLAG_NOT;