diff --git a/klippy/chelper.py b/klippy/chelper.py index 4384fe28..3603d839 100644 --- a/klippy/chelper.py +++ b/klippy/chelper.py @@ -33,6 +33,7 @@ defs_stepcompress = """ , double accel_multiplier, double step_dist, double height , double closestxy_d, double closest_height2, double movez_r); void stepcompress_reset(struct stepcompress *sc, uint64_t last_step_clock); + void stepcompress_set_homing(struct stepcompress *sc, uint64_t homing_clock); void stepcompress_queue_msg(struct stepcompress *sc , uint32_t *data, int len); uint32_t stepcompress_get_errors(struct stepcompress *sc); diff --git a/klippy/mcu.py b/klippy/mcu.py index c9927841..6fc2db0d 100644 --- a/klippy/mcu.py +++ b/klippy/mcu.py @@ -58,7 +58,10 @@ class MCU_stepper: self._mcu_position_offset = pos - self.commanded_position def get_mcu_position(self): return self.commanded_position + self._mcu_position_offset - def note_stepper_stop(self): + def note_homing_start(self, homing_clock): + self.ffi_lib.stepcompress_set_homing(self._stepqueue, homing_clock) + def note_homing_finalized(self): + self.ffi_lib.stepcompress_set_homing(self._stepqueue, 0) self.ffi_lib.stepcompress_reset(self._stepqueue, 0) def reset_step_clock(self, mcu_time): clock = int(mcu_time * self._mcu_freq) @@ -144,11 +147,9 @@ class MCU_endstop: msg = self._home_cmd.encode( self._oid, clock, rest_ticks, 1 ^ self._invert) self._mcu.send(msg, reqclock=clock, cq=self._cmd_queue) + self._stepper.note_homing_start(clock) def home_finalize(self, mcu_time): - # XXX - this flushes the serial port of messages ready to be - # sent, but doesn't flush messages if they had an unmet minclock - self._mcu.serial.send_flush() - self._stepper.note_stepper_stop() + self._stepper.note_homing_finalized() self._home_timeout_clock = int(mcu_time * self._mcu_freq) def home_wait(self): eventtime = time.time() diff --git a/klippy/stepcompress.c b/klippy/stepcompress.c index 6bcdb284..2100aa3e 100644 --- a/klippy/stepcompress.c +++ b/klippy/stepcompress.c @@ -34,7 +34,7 @@ struct stepcompress { // Error checking uint32_t errors; // Message generation - uint64_t last_step_clock; + uint64_t last_step_clock, homing_clock; struct list_head msg_queue; uint32_t queue_step_msgid, set_next_step_dir_msgid, oid; int sdir, invert_sdir; @@ -339,6 +339,9 @@ stepcompress_flush(struct stepcompress *sc, uint64_t move_clock) uint32_t ticks = move.add*addfactor + move.interval*move.count; sc->last_step_clock += ticks; } + if (sc->homing_clock) + // When homing, all steps should be sent prior to homing_clock + qm->min_clock = qm->req_clock = sc->homing_clock; list_add_tail(&qm->node, &sc->msg_queue); if (sc->queue_pos + move.count >= sc->queue_next) { @@ -359,7 +362,7 @@ set_next_step_dir(struct stepcompress *sc, int sdir) sc->set_next_step_dir_msgid, sc->oid, sdir ^ sc->invert_sdir }; struct queue_message *qm = message_alloc_and_encode(msg, 3); - qm->req_clock = sc->last_step_clock; + qm->req_clock = sc->homing_clock ?: sc->last_step_clock; list_add_tail(&qm->node, &sc->msg_queue); } @@ -557,6 +560,14 @@ stepcompress_reset(struct stepcompress *sc, uint64_t last_step_clock) sc->sdir = -1; } +// Indicate the stepper is in homing mode (or done homing if zero) +void +stepcompress_set_homing(struct stepcompress *sc, uint64_t homing_clock) +{ + stepcompress_flush(sc, UINT64_MAX); + sc->homing_clock = homing_clock; +} + // Queue an mcu command to go out in order with stepper commands void stepcompress_queue_msg(struct stepcompress *sc, uint32_t *data, int len) @@ -564,7 +575,7 @@ stepcompress_queue_msg(struct stepcompress *sc, uint32_t *data, int len) stepcompress_flush(sc, UINT64_MAX); struct queue_message *qm = message_alloc_and_encode(data, len); - qm->req_clock = sc->last_step_clock; + qm->req_clock = sc->homing_clock ?: sc->last_step_clock; list_add_tail(&qm->node, &sc->msg_queue); }