stepper: Add some comments on s->position tracking

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2020-03-04 13:30:06 -05:00
parent 63ba7d231e
commit e9e0f59204
1 changed files with 5 additions and 0 deletions

View File

@ -62,6 +62,7 @@ stepper_load_next(struct stepper *s, uint32_t min_next_time)
{
struct stepper_move *m = s->first;
if (!m) {
// There is no next move - the queue is empty
if (s->interval - s->add < s->min_stop_interval
&& !(s->flags & SF_NO_NEXT_CHECK))
shutdown("No next step");
@ -69,6 +70,7 @@ stepper_load_next(struct stepper *s, uint32_t min_next_time)
return SF_DONE;
}
// Load next 'struct stepper_move' into 'struct stepper'
s->next_step_time += m->interval;
s->add = m->add;
s->interval = m->interval + m->add;
@ -91,6 +93,7 @@ stepper_load_next(struct stepper *s, uint32_t min_next_time)
}
s->count = (uint32_t)m->count * 2;
}
// Add all steps to s->position (stepper_get_position() can calc mid-move)
if (m->flags & MF_DIR) {
s->position = -s->position + m->count;
gpio_out_toggle_noirq(s->dir_pin);
@ -274,10 +277,12 @@ static uint32_t
stepper_get_position(struct stepper *s)
{
uint32_t position = s->position;
// If stepper is mid-move, subtract out steps not yet taken
if (CONFIG_STEP_DELAY <= 0)
position -= s->count;
else
position -= s->count / 2;
// The top bit of s->position is an optimized reverse direction flag
if (position & 0x80000000)
return -position;
return position;