2016-05-25 17:37:40 +02:00
|
|
|
// Stepper pulse schedule compression
|
|
|
|
//
|
2021-02-04 22:33:03 +01:00
|
|
|
// Copyright (C) 2016-2021 Kevin O'Connor <kevin@koconnor.net>
|
2016-05-25 17:37:40 +02:00
|
|
|
//
|
|
|
|
// This file may be distributed under the terms of the GNU GPLv3 license.
|
2020-02-25 03:59:39 +01:00
|
|
|
|
2016-05-25 17:37:40 +02:00
|
|
|
// The goal of this code is to take a series of scheduled stepper
|
|
|
|
// pulse times and compress them into a handful of commands that can
|
|
|
|
// be efficiently transmitted and executed on a microcontroller (mcu).
|
|
|
|
// The mcu accepts step pulse commands that take interval, count, and
|
|
|
|
// add parameters such that 'count' pulses occur, with each step event
|
|
|
|
// calculating the next step event time using:
|
|
|
|
// next_wake_time = last_wake_time + interval; interval += add
|
2018-06-04 05:51:30 +02:00
|
|
|
// This code is written in C (instead of python) for processing
|
2016-05-25 17:37:40 +02:00
|
|
|
// efficiency - the repetitive integer math is vastly faster in C.
|
|
|
|
|
2021-02-22 23:57:36 +01:00
|
|
|
#include <math.h> // sqrt
|
2016-05-25 17:37:40 +02:00
|
|
|
#include <stddef.h> // offsetof
|
|
|
|
#include <stdint.h> // uint32_t
|
2016-12-13 16:44:26 +01:00
|
|
|
#include <stdio.h> // fprintf
|
2016-05-25 17:37:40 +02:00
|
|
|
#include <stdlib.h> // malloc
|
|
|
|
#include <string.h> // memset
|
2018-06-06 03:25:37 +02:00
|
|
|
#include "compiler.h" // DIV_ROUND_UP
|
2016-11-30 18:04:28 +01:00
|
|
|
#include "pyhelper.h" // errorf
|
2016-05-25 17:37:40 +02:00
|
|
|
#include "serialqueue.h" // struct queue_message
|
2018-06-06 22:49:44 +02:00
|
|
|
#include "stepcompress.h" // stepcompress_alloc
|
2016-05-25 17:37:40 +02:00
|
|
|
|
|
|
|
#define CHECK_LINES 1
|
|
|
|
#define QUEUE_START_SIZE 1024
|
|
|
|
|
|
|
|
struct stepcompress {
|
|
|
|
// Buffer management
|
2017-08-30 18:42:53 +02:00
|
|
|
uint32_t *queue, *queue_end, *queue_pos, *queue_next;
|
2016-05-25 17:37:40 +02:00
|
|
|
// Internal tracking
|
2016-11-02 00:38:37 +01:00
|
|
|
uint32_t max_error;
|
2020-02-25 03:59:39 +01:00
|
|
|
double mcu_time_offset, mcu_freq, last_step_print_time;
|
2016-05-25 17:37:40 +02:00
|
|
|
// Message generation
|
2019-06-27 19:23:53 +02:00
|
|
|
uint64_t last_step_clock;
|
2016-05-25 17:37:40 +02:00
|
|
|
struct list_head msg_queue;
|
2021-02-04 22:33:03 +01:00
|
|
|
uint32_t oid;
|
|
|
|
int32_t queue_step_msgtag, set_next_step_dir_msgtag;
|
2016-11-10 18:44:04 +01:00
|
|
|
int sdir, invert_sdir;
|
2020-02-25 18:54:55 +01:00
|
|
|
// Step+dir+step filter
|
|
|
|
uint64_t next_step_clock;
|
|
|
|
int next_step_dir;
|
2021-02-22 23:57:36 +01:00
|
|
|
// History tracking
|
|
|
|
int64_t last_position;
|
|
|
|
struct list_head history_list;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct step_move {
|
|
|
|
uint32_t interval;
|
|
|
|
uint16_t count;
|
|
|
|
int16_t add;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define HISTORY_EXPIRE (30.0)
|
|
|
|
|
2021-07-19 20:40:50 +02:00
|
|
|
struct history_steps {
|
2021-02-22 23:57:36 +01:00
|
|
|
struct list_node node;
|
|
|
|
uint64_t first_clock, last_clock;
|
|
|
|
int64_t start_position;
|
2021-07-19 20:51:05 +02:00
|
|
|
int step_count, interval, add;
|
2016-05-25 17:37:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************
|
|
|
|
* Step compression
|
|
|
|
****************************************************************/
|
|
|
|
|
|
|
|
static inline int32_t
|
|
|
|
idiv_up(int32_t n, int32_t d)
|
|
|
|
{
|
2018-06-06 03:25:37 +02:00
|
|
|
return (n>=0) ? DIV_ROUND_UP(n,d) : (n/d);
|
2016-05-25 17:37:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int32_t
|
|
|
|
idiv_down(int32_t n, int32_t d)
|
|
|
|
{
|
|
|
|
return (n>=0) ? (n/d) : (n - d + 1) / d;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct points {
|
|
|
|
int32_t minp, maxp;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Given a requested step time, return the minimum and maximum
|
|
|
|
// acceptable times
|
2016-12-13 16:44:26 +01:00
|
|
|
static inline struct points
|
2017-08-30 18:42:53 +02:00
|
|
|
minmax_point(struct stepcompress *sc, uint32_t *pos)
|
2016-05-25 17:37:40 +02:00
|
|
|
{
|
2017-08-31 16:07:44 +02:00
|
|
|
uint32_t lsc = sc->last_step_clock, point = *pos - lsc;
|
2017-08-30 18:42:53 +02:00
|
|
|
uint32_t prevpoint = pos > sc->queue_pos ? *(pos-1) - lsc : 0;
|
2016-05-25 17:37:40 +02:00
|
|
|
uint32_t max_error = (point - prevpoint) / 2;
|
|
|
|
if (max_error > sc->max_error)
|
|
|
|
max_error = sc->max_error;
|
|
|
|
return (struct points){ point - max_error, point };
|
|
|
|
}
|
|
|
|
|
|
|
|
// The maximum add delta between two valid quadratic sequences of the
|
|
|
|
// form "add*count*(count-1)/2 + interval*count" is "(6 + 4*sqrt(2)) *
|
|
|
|
// maxerror / (count*count)". The "6 + 4*sqrt(2)" is 11.65685, but
|
2016-12-14 16:59:15 +01:00
|
|
|
// using 11 works well in practice.
|
2016-05-25 17:37:40 +02:00
|
|
|
#define QUADRATIC_DEV 11
|
|
|
|
|
|
|
|
// Find a 'step_move' that covers a series of step times
|
|
|
|
static struct step_move
|
|
|
|
compress_bisect_add(struct stepcompress *sc)
|
|
|
|
{
|
2017-08-30 18:42:53 +02:00
|
|
|
uint32_t *qlast = sc->queue_next;
|
|
|
|
if (qlast > sc->queue_pos + 65535)
|
|
|
|
qlast = sc->queue_pos + 65535;
|
2016-05-25 17:37:40 +02:00
|
|
|
struct points point = minmax_point(sc, sc->queue_pos);
|
2016-09-22 22:40:00 +02:00
|
|
|
int32_t outer_mininterval = point.minp, outer_maxinterval = point.maxp;
|
2016-12-14 16:59:15 +01:00
|
|
|
int32_t add = 0, minadd = -0x8000, maxadd = 0x7fff;
|
2016-12-07 01:01:00 +01:00
|
|
|
int32_t bestinterval = 0, bestcount = 1, bestadd = 1, bestreach = INT32_MIN;
|
2016-12-19 03:56:30 +01:00
|
|
|
int32_t zerointerval = 0, zerocount = 0;
|
2016-05-25 17:37:40 +02:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
// Find longest valid sequence with the given 'add'
|
2016-12-13 16:44:26 +01:00
|
|
|
struct points nextpoint;
|
|
|
|
int32_t nextmininterval = outer_mininterval;
|
|
|
|
int32_t nextmaxinterval = outer_maxinterval, interval = nextmaxinterval;
|
|
|
|
int32_t nextcount = 1;
|
2016-05-25 17:37:40 +02:00
|
|
|
for (;;) {
|
2016-12-13 16:44:26 +01:00
|
|
|
nextcount++;
|
2017-08-30 18:42:53 +02:00
|
|
|
if (&sc->queue_pos[nextcount-1] >= qlast) {
|
2016-12-13 16:44:26 +01:00
|
|
|
int32_t count = nextcount - 1;
|
|
|
|
return (struct step_move){ interval, count, add };
|
2016-11-02 04:08:16 +01:00
|
|
|
}
|
2016-12-13 16:44:26 +01:00
|
|
|
nextpoint = minmax_point(sc, sc->queue_pos + nextcount - 1);
|
2016-12-26 18:47:17 +01:00
|
|
|
int32_t nextaddfactor = nextcount*(nextcount-1)/2;
|
2016-12-13 16:44:26 +01:00
|
|
|
int32_t c = add*nextaddfactor;
|
|
|
|
if (nextmininterval*nextcount < nextpoint.minp - c)
|
2020-01-25 20:15:57 +01:00
|
|
|
nextmininterval = idiv_up(nextpoint.minp - c, nextcount);
|
2016-12-13 16:44:26 +01:00
|
|
|
if (nextmaxinterval*nextcount > nextpoint.maxp - c)
|
2020-01-25 20:15:57 +01:00
|
|
|
nextmaxinterval = idiv_down(nextpoint.maxp - c, nextcount);
|
2016-05-25 17:37:40 +02:00
|
|
|
if (nextmininterval > nextmaxinterval)
|
|
|
|
break;
|
2016-12-13 16:44:26 +01:00
|
|
|
interval = nextmaxinterval;
|
2016-05-25 17:37:40 +02:00
|
|
|
}
|
2016-09-22 22:40:00 +02:00
|
|
|
|
|
|
|
// Check if this is the best sequence found so far
|
2016-12-26 18:47:17 +01:00
|
|
|
int32_t count = nextcount - 1, addfactor = count*(count-1)/2;
|
2016-12-13 16:44:26 +01:00
|
|
|
int32_t reach = add*addfactor + interval*count;
|
2016-12-19 04:11:20 +01:00
|
|
|
if (reach > bestreach
|
|
|
|
|| (reach == bestreach && interval > bestinterval)) {
|
2016-12-13 16:44:26 +01:00
|
|
|
bestinterval = interval;
|
2016-05-25 17:37:40 +02:00
|
|
|
bestcount = count;
|
|
|
|
bestadd = add;
|
2016-09-22 22:40:00 +02:00
|
|
|
bestreach = reach;
|
2016-12-19 03:56:30 +01:00
|
|
|
if (!add) {
|
|
|
|
zerointerval = interval;
|
|
|
|
zerocount = count;
|
|
|
|
}
|
2016-12-26 18:47:17 +01:00
|
|
|
if (count > 0x200)
|
|
|
|
// No 'add' will improve sequence; avoid integer overflow
|
|
|
|
break;
|
2016-05-25 17:37:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if a greater or lesser add could extend the sequence
|
2016-12-26 18:47:17 +01:00
|
|
|
int32_t nextaddfactor = nextcount*(nextcount-1)/2;
|
2016-12-13 16:44:26 +01:00
|
|
|
int32_t nextreach = add*nextaddfactor + interval*nextcount;
|
|
|
|
if (nextreach < nextpoint.minp) {
|
2016-12-14 16:59:15 +01:00
|
|
|
minadd = add + 1;
|
2016-12-13 16:44:26 +01:00
|
|
|
outer_maxinterval = nextmaxinterval;
|
2016-07-06 19:19:21 +02:00
|
|
|
} else {
|
2016-12-14 16:59:15 +01:00
|
|
|
maxadd = add - 1;
|
2016-12-13 16:44:26 +01:00
|
|
|
outer_mininterval = nextmininterval;
|
2016-07-06 19:19:21 +02:00
|
|
|
}
|
2016-05-25 17:37:40 +02:00
|
|
|
|
|
|
|
// The maximum valid deviation between two quadratic sequences
|
|
|
|
// can be calculated and used to further limit the add range.
|
|
|
|
if (count > 1) {
|
2016-12-14 16:59:15 +01:00
|
|
|
int32_t errdelta = sc->max_error*QUADRATIC_DEV / (count*count);
|
2016-05-25 17:37:40 +02:00
|
|
|
if (minadd < add - errdelta)
|
|
|
|
minadd = add - errdelta;
|
|
|
|
if (maxadd > add + errdelta)
|
|
|
|
maxadd = add + errdelta;
|
|
|
|
}
|
|
|
|
|
2016-09-22 22:40:00 +02:00
|
|
|
// See if next point would further limit the add range
|
2016-12-13 16:44:26 +01:00
|
|
|
int32_t c = outer_maxinterval * nextcount;
|
2016-12-14 16:59:15 +01:00
|
|
|
if (minadd*nextaddfactor < nextpoint.minp - c)
|
|
|
|
minadd = idiv_up(nextpoint.minp - c, nextaddfactor);
|
2016-12-13 16:44:26 +01:00
|
|
|
c = outer_mininterval * nextcount;
|
2016-12-14 16:59:15 +01:00
|
|
|
if (maxadd*nextaddfactor > nextpoint.maxp - c)
|
|
|
|
maxadd = idiv_down(nextpoint.maxp - c, nextaddfactor);
|
2016-09-22 22:40:00 +02:00
|
|
|
|
2016-05-25 17:37:40 +02:00
|
|
|
// Bisect valid add range and try again with new 'add'
|
2016-12-14 16:59:15 +01:00
|
|
|
if (minadd > maxadd)
|
2016-05-25 17:37:40 +02:00
|
|
|
break;
|
2016-12-13 16:53:13 +01:00
|
|
|
add = maxadd - (maxadd - minadd) / 4;
|
2016-05-25 17:37:40 +02:00
|
|
|
}
|
2016-12-19 03:56:30 +01:00
|
|
|
if (zerocount + zerocount/16 >= bestcount)
|
|
|
|
// Prefer add=0 if it's similar to the best found sequence
|
|
|
|
return (struct step_move){ zerointerval, zerocount, 0 };
|
2016-05-25 17:37:40 +02:00
|
|
|
return (struct step_move){ bestinterval, bestcount, bestadd };
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************
|
|
|
|
* Step compress checking
|
|
|
|
****************************************************************/
|
|
|
|
|
|
|
|
// Verify that a given 'step_move' matches the actual step times
|
2017-02-06 17:37:03 +01:00
|
|
|
static int
|
2016-05-25 17:37:40 +02:00
|
|
|
check_line(struct stepcompress *sc, struct step_move move)
|
|
|
|
{
|
|
|
|
if (!CHECK_LINES)
|
2017-02-06 17:37:03 +01:00
|
|
|
return 0;
|
2017-08-30 18:42:53 +02:00
|
|
|
if (!move.count || (!move.interval && !move.add && move.count > 1)
|
2016-12-13 16:46:46 +01:00
|
|
|
|| move.interval >= 0x80000000) {
|
2017-03-12 17:29:20 +01:00
|
|
|
errorf("stepcompress o=%d i=%d c=%d a=%d: Invalid sequence"
|
|
|
|
, sc->oid, move.interval, move.count, move.add);
|
2017-02-06 17:37:03 +01:00
|
|
|
return ERROR_RET;
|
2016-05-25 17:37:40 +02:00
|
|
|
}
|
2016-11-02 04:08:16 +01:00
|
|
|
uint32_t interval = move.interval, p = 0;
|
2016-05-25 17:37:40 +02:00
|
|
|
uint16_t i;
|
|
|
|
for (i=0; i<move.count; i++) {
|
|
|
|
struct points point = minmax_point(sc, sc->queue_pos + i);
|
2016-11-02 04:08:16 +01:00
|
|
|
p += interval;
|
2016-05-25 17:37:40 +02:00
|
|
|
if (p < point.minp || p > point.maxp) {
|
2017-03-12 17:29:20 +01:00
|
|
|
errorf("stepcompress o=%d i=%d c=%d a=%d: Point %d: %d not in %d:%d"
|
|
|
|
, sc->oid, move.interval, move.count, move.add
|
|
|
|
, i+1, p, point.minp, point.maxp);
|
2017-02-06 17:37:03 +01:00
|
|
|
return ERROR_RET;
|
2016-05-25 17:37:40 +02:00
|
|
|
}
|
2016-11-02 04:08:16 +01:00
|
|
|
if (interval >= 0x80000000) {
|
2017-03-12 17:29:20 +01:00
|
|
|
errorf("stepcompress o=%d i=%d c=%d a=%d:"
|
|
|
|
" Point %d: interval overflow %d"
|
|
|
|
, sc->oid, move.interval, move.count, move.add
|
|
|
|
, i+1, interval);
|
2017-02-06 17:37:03 +01:00
|
|
|
return ERROR_RET;
|
2016-11-02 04:08:16 +01:00
|
|
|
}
|
2016-05-25 17:37:40 +02:00
|
|
|
interval += move.add;
|
|
|
|
}
|
2017-02-06 17:37:03 +01:00
|
|
|
return 0;
|
2016-05-25 17:37:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************
|
|
|
|
* Step compress interface
|
|
|
|
****************************************************************/
|
|
|
|
|
|
|
|
// Allocate a new 'stepcompress' object
|
2018-06-06 03:40:32 +02:00
|
|
|
struct stepcompress * __visible
|
2018-06-08 23:02:20 +02:00
|
|
|
stepcompress_alloc(uint32_t oid)
|
2016-05-25 17:37:40 +02:00
|
|
|
{
|
|
|
|
struct stepcompress *sc = malloc(sizeof(*sc));
|
|
|
|
memset(sc, 0, sizeof(*sc));
|
|
|
|
list_init(&sc->msg_queue);
|
2021-02-22 23:57:36 +01:00
|
|
|
list_init(&sc->history_list);
|
2016-05-25 17:37:40 +02:00
|
|
|
sc->oid = oid;
|
2016-11-10 18:44:04 +01:00
|
|
|
sc->sdir = -1;
|
2016-05-25 17:37:40 +02:00
|
|
|
return sc;
|
|
|
|
}
|
|
|
|
|
2018-06-08 23:02:20 +02:00
|
|
|
// Fill message id information
|
|
|
|
void __visible
|
|
|
|
stepcompress_fill(struct stepcompress *sc, uint32_t max_error
|
2022-01-30 01:13:31 +01:00
|
|
|
, int32_t queue_step_msgtag, int32_t set_next_step_dir_msgtag)
|
2018-06-08 23:02:20 +02:00
|
|
|
{
|
|
|
|
sc->max_error = max_error;
|
2021-02-04 22:33:03 +01:00
|
|
|
sc->queue_step_msgtag = queue_step_msgtag;
|
|
|
|
sc->set_next_step_dir_msgtag = set_next_step_dir_msgtag;
|
2018-06-08 23:02:20 +02:00
|
|
|
}
|
|
|
|
|
2022-01-30 01:13:31 +01:00
|
|
|
// Set the inverted stepper direction flag
|
|
|
|
void __visible
|
|
|
|
stepcompress_set_invert_sdir(struct stepcompress *sc, uint32_t invert_sdir)
|
|
|
|
{
|
|
|
|
invert_sdir = !!invert_sdir;
|
|
|
|
if (invert_sdir != sc->invert_sdir) {
|
|
|
|
sc->invert_sdir = invert_sdir;
|
|
|
|
if (sc->sdir >= 0)
|
|
|
|
sc->sdir ^= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-22 23:57:36 +01:00
|
|
|
// Helper to free items from the history_list
|
|
|
|
static void
|
|
|
|
free_history(struct stepcompress *sc, uint64_t end_clock)
|
|
|
|
{
|
|
|
|
while (!list_empty(&sc->history_list)) {
|
2021-07-19 20:40:50 +02:00
|
|
|
struct history_steps *hs = list_last_entry(
|
|
|
|
&sc->history_list, struct history_steps, node);
|
|
|
|
if (hs->last_clock > end_clock)
|
2021-02-22 23:57:36 +01:00
|
|
|
break;
|
2021-07-19 20:40:50 +02:00
|
|
|
list_del(&hs->node);
|
|
|
|
free(hs);
|
2021-02-22 23:57:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-30 07:58:45 +01:00
|
|
|
// Free memory associated with a 'stepcompress' object
|
2018-06-06 03:40:32 +02:00
|
|
|
void __visible
|
2016-11-30 07:58:45 +01:00
|
|
|
stepcompress_free(struct stepcompress *sc)
|
|
|
|
{
|
|
|
|
if (!sc)
|
|
|
|
return;
|
|
|
|
free(sc->queue);
|
|
|
|
message_queue_free(&sc->msg_queue);
|
2021-02-22 23:57:36 +01:00
|
|
|
free_history(sc, UINT64_MAX);
|
2016-11-30 07:58:45 +01:00
|
|
|
free(sc);
|
|
|
|
}
|
|
|
|
|
2020-03-08 16:14:26 +01:00
|
|
|
uint32_t
|
|
|
|
stepcompress_get_oid(struct stepcompress *sc)
|
|
|
|
{
|
|
|
|
return sc->oid;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
stepcompress_get_step_dir(struct stepcompress *sc)
|
|
|
|
{
|
2020-02-25 18:54:55 +01:00
|
|
|
return sc->next_step_dir;
|
2020-03-08 16:14:26 +01:00
|
|
|
}
|
|
|
|
|
2020-02-25 03:59:39 +01:00
|
|
|
// Determine the "print time" of the last_step_clock
|
|
|
|
static void
|
|
|
|
calc_last_step_print_time(struct stepcompress *sc)
|
|
|
|
{
|
|
|
|
double lsc = sc->last_step_clock;
|
|
|
|
sc->last_step_print_time = sc->mcu_time_offset + (lsc - .5) / sc->mcu_freq;
|
2021-02-22 23:57:36 +01:00
|
|
|
|
|
|
|
if (lsc > sc->mcu_freq * HISTORY_EXPIRE)
|
|
|
|
free_history(sc, lsc - sc->mcu_freq * HISTORY_EXPIRE);
|
2020-02-25 03:59:39 +01:00
|
|
|
}
|
|
|
|
|
2020-03-08 16:14:26 +01:00
|
|
|
// Set the conversion rate of 'print_time' to mcu clock
|
|
|
|
static void
|
|
|
|
stepcompress_set_time(struct stepcompress *sc
|
|
|
|
, double time_offset, double mcu_freq)
|
|
|
|
{
|
|
|
|
sc->mcu_time_offset = time_offset;
|
|
|
|
sc->mcu_freq = mcu_freq;
|
|
|
|
calc_last_step_print_time(sc);
|
|
|
|
}
|
|
|
|
|
2021-02-22 23:57:36 +01:00
|
|
|
// Maximium clock delta between messages in the queue
|
|
|
|
#define CLOCK_DIFF_MAX (3<<28)
|
|
|
|
|
|
|
|
// Helper to create a queue_step command from a 'struct step_move'
|
|
|
|
static void
|
|
|
|
add_move(struct stepcompress *sc, uint64_t first_clock, struct step_move *move)
|
|
|
|
{
|
|
|
|
int32_t addfactor = move->count*(move->count-1)/2;
|
|
|
|
uint32_t ticks = move->add*addfactor + move->interval*(move->count-1);
|
|
|
|
uint64_t last_clock = first_clock + ticks;
|
|
|
|
|
|
|
|
// Create and queue a queue_step command
|
|
|
|
uint32_t msg[5] = {
|
|
|
|
sc->queue_step_msgtag, sc->oid, move->interval, move->count, move->add
|
|
|
|
};
|
|
|
|
struct queue_message *qm = message_alloc_and_encode(msg, 5);
|
|
|
|
qm->min_clock = qm->req_clock = sc->last_step_clock;
|
|
|
|
if (move->count == 1 && first_clock >= sc->last_step_clock + CLOCK_DIFF_MAX)
|
|
|
|
qm->req_clock = first_clock;
|
|
|
|
list_add_tail(&qm->node, &sc->msg_queue);
|
|
|
|
sc->last_step_clock = last_clock;
|
|
|
|
|
|
|
|
// Create and store move in history tracking
|
2021-07-19 20:40:50 +02:00
|
|
|
struct history_steps *hs = malloc(sizeof(*hs));
|
|
|
|
hs->first_clock = first_clock;
|
|
|
|
hs->last_clock = last_clock;
|
|
|
|
hs->start_position = sc->last_position;
|
2021-07-19 20:51:05 +02:00
|
|
|
hs->interval = move->interval;
|
|
|
|
hs->add = move->add;
|
2021-07-19 20:40:50 +02:00
|
|
|
hs->step_count = sc->sdir ? move->count : -move->count;
|
|
|
|
sc->last_position += hs->step_count;
|
|
|
|
list_add_head(&hs->node, &sc->history_list);
|
2021-02-22 23:57:36 +01:00
|
|
|
}
|
|
|
|
|
2016-11-13 04:55:27 +01:00
|
|
|
// Convert previously scheduled steps into commands for the mcu
|
2017-02-06 17:37:03 +01:00
|
|
|
static int
|
2020-02-25 18:54:55 +01:00
|
|
|
queue_flush(struct stepcompress *sc, uint64_t move_clock)
|
2016-11-13 04:55:27 +01:00
|
|
|
{
|
|
|
|
if (sc->queue_pos >= sc->queue_next)
|
2017-02-06 17:37:03 +01:00
|
|
|
return 0;
|
2017-08-30 18:42:53 +02:00
|
|
|
while (sc->last_step_clock < move_clock) {
|
2016-11-13 04:55:27 +01:00
|
|
|
struct step_move move = compress_bisect_add(sc);
|
2017-02-06 17:37:03 +01:00
|
|
|
int ret = check_line(sc, move);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2016-11-13 04:55:27 +01:00
|
|
|
|
2021-02-22 23:57:36 +01:00
|
|
|
add_move(sc, sc->last_step_clock + move.interval, &move);
|
2016-11-13 04:55:27 +01:00
|
|
|
|
|
|
|
if (sc->queue_pos + move.count >= sc->queue_next) {
|
|
|
|
sc->queue_pos = sc->queue_next = sc->queue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sc->queue_pos += move.count;
|
|
|
|
}
|
2020-02-25 03:59:39 +01:00
|
|
|
calc_last_step_print_time(sc);
|
2017-02-06 17:37:03 +01:00
|
|
|
return 0;
|
2016-11-13 04:55:27 +01:00
|
|
|
}
|
|
|
|
|
2017-08-30 18:42:53 +02:00
|
|
|
// Generate a queue_step for a step far in the future from the last step
|
|
|
|
static int
|
|
|
|
stepcompress_flush_far(struct stepcompress *sc, uint64_t abs_step_clock)
|
|
|
|
{
|
2021-02-22 23:57:36 +01:00
|
|
|
struct step_move move = { abs_step_clock - sc->last_step_clock, 1, 0 };
|
|
|
|
add_move(sc, abs_step_clock, &move);
|
2020-02-25 03:59:39 +01:00
|
|
|
calc_last_step_print_time(sc);
|
2017-08-30 18:42:53 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-11-10 18:44:04 +01:00
|
|
|
// Send the set_next_step_dir command
|
2018-06-14 20:01:33 +02:00
|
|
|
static int
|
2016-11-10 18:44:04 +01:00
|
|
|
set_next_step_dir(struct stepcompress *sc, int sdir)
|
|
|
|
{
|
2016-12-31 19:21:53 +01:00
|
|
|
if (sc->sdir == sdir)
|
2017-02-06 17:37:03 +01:00
|
|
|
return 0;
|
2020-02-25 18:54:55 +01:00
|
|
|
int ret = queue_flush(sc, UINT64_MAX);
|
2017-02-06 17:37:03 +01:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2021-02-22 23:57:36 +01:00
|
|
|
sc->sdir = sdir;
|
2016-11-10 18:44:04 +01:00
|
|
|
uint32_t msg[3] = {
|
2021-02-04 22:33:03 +01:00
|
|
|
sc->set_next_step_dir_msgtag, sc->oid, sdir ^ sc->invert_sdir
|
2016-11-10 18:44:04 +01:00
|
|
|
};
|
|
|
|
struct queue_message *qm = message_alloc_and_encode(msg, 3);
|
2019-06-27 19:23:53 +02:00
|
|
|
qm->req_clock = sc->last_step_clock;
|
2016-11-10 18:44:04 +01:00
|
|
|
list_add_tail(&qm->node, &sc->msg_queue);
|
2017-02-06 17:37:03 +01:00
|
|
|
return 0;
|
2016-11-10 18:44:04 +01:00
|
|
|
}
|
|
|
|
|
2020-02-25 18:54:55 +01:00
|
|
|
// Slow path for queue_append() - handle next step far in future
|
2017-08-30 16:43:57 +02:00
|
|
|
static int
|
2020-02-25 18:54:55 +01:00
|
|
|
queue_append_far(struct stepcompress *sc)
|
2017-08-30 16:43:57 +02:00
|
|
|
{
|
2020-02-25 18:54:55 +01:00
|
|
|
uint64_t step_clock = sc->next_step_clock;
|
|
|
|
sc->next_step_clock = 0;
|
|
|
|
int ret = queue_flush(sc, step_clock - CLOCK_DIFF_MAX + 1);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
if (step_clock >= sc->last_step_clock + CLOCK_DIFF_MAX)
|
|
|
|
return stepcompress_flush_far(sc, step_clock);
|
|
|
|
*sc->queue_next++ = step_clock;
|
|
|
|
return 0;
|
|
|
|
}
|
2017-08-30 18:42:53 +02:00
|
|
|
|
2020-02-25 18:54:55 +01:00
|
|
|
// Slow path for queue_append() - expand the internal queue storage
|
|
|
|
static int
|
|
|
|
queue_append_extend(struct stepcompress *sc)
|
|
|
|
{
|
2017-08-30 16:43:57 +02:00
|
|
|
if (sc->queue_next - sc->queue_pos > 65535 + 2000) {
|
|
|
|
// No point in keeping more than 64K steps in memory
|
2019-02-27 20:18:07 +01:00
|
|
|
uint32_t flush = (*(sc->queue_next-65535)
|
|
|
|
- (uint32_t)sc->last_step_clock);
|
2020-02-25 18:54:55 +01:00
|
|
|
int ret = queue_flush(sc, sc->last_step_clock + flush);
|
2017-08-30 16:43:57 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-11-30 02:16:05 +01:00
|
|
|
if (sc->queue_next >= sc->queue_end) {
|
|
|
|
// Make room in the queue
|
|
|
|
int in_use = sc->queue_next - sc->queue_pos;
|
|
|
|
if (sc->queue_pos > sc->queue) {
|
|
|
|
// Shuffle the internal queue to avoid having to allocate more ram
|
|
|
|
memmove(sc->queue, sc->queue_pos, in_use * sizeof(*sc->queue));
|
|
|
|
} else {
|
|
|
|
// Expand the internal queue of step times
|
|
|
|
int alloc = sc->queue_end - sc->queue;
|
|
|
|
if (!alloc)
|
|
|
|
alloc = QUEUE_START_SIZE;
|
|
|
|
while (in_use >= alloc)
|
|
|
|
alloc *= 2;
|
|
|
|
sc->queue = realloc(sc->queue, alloc * sizeof(*sc->queue));
|
|
|
|
sc->queue_end = sc->queue + alloc;
|
|
|
|
}
|
|
|
|
sc->queue_pos = sc->queue;
|
|
|
|
sc->queue_next = sc->queue + in_use;
|
2017-08-30 16:43:57 +02:00
|
|
|
}
|
2017-11-30 02:16:05 +01:00
|
|
|
|
2020-02-25 18:54:55 +01:00
|
|
|
*sc->queue_next++ = sc->next_step_clock;
|
|
|
|
sc->next_step_clock = 0;
|
2017-08-30 16:43:57 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-02-25 03:59:39 +01:00
|
|
|
// Add a step time to the queue (flushing the queue if needed)
|
2020-02-25 18:54:55 +01:00
|
|
|
static int
|
|
|
|
queue_append(struct stepcompress *sc)
|
2017-08-30 16:43:57 +02:00
|
|
|
{
|
2020-02-25 18:54:55 +01:00
|
|
|
if (unlikely(sc->next_step_dir != sc->sdir)) {
|
|
|
|
int ret = set_next_step_dir(sc, sc->next_step_dir);
|
2020-02-25 03:59:39 +01:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-08-30 16:43:57 +02:00
|
|
|
}
|
2020-02-25 18:54:55 +01:00
|
|
|
if (unlikely(sc->next_step_clock >= sc->last_step_clock + CLOCK_DIFF_MAX))
|
|
|
|
return queue_append_far(sc);
|
|
|
|
if (unlikely(sc->queue_next >= sc->queue_end))
|
|
|
|
return queue_append_extend(sc);
|
|
|
|
*sc->queue_next++ = sc->next_step_clock;
|
|
|
|
sc->next_step_clock = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define SDS_FILTER_TIME .000750
|
|
|
|
|
|
|
|
// Add next step time
|
|
|
|
int
|
|
|
|
stepcompress_append(struct stepcompress *sc, int sdir
|
|
|
|
, double print_time, double step_time)
|
|
|
|
{
|
|
|
|
// Calculate step clock
|
2020-02-25 03:59:39 +01:00
|
|
|
double offset = print_time - sc->last_step_print_time;
|
|
|
|
double rel_sc = (step_time + offset) * sc->mcu_freq;
|
2020-02-25 18:54:55 +01:00
|
|
|
uint64_t step_clock = sc->last_step_clock + (uint64_t)rel_sc;
|
|
|
|
// Flush previous pending step (if any)
|
|
|
|
if (sc->next_step_clock) {
|
|
|
|
if (unlikely(sdir != sc->next_step_dir)) {
|
2021-04-11 18:11:03 +02:00
|
|
|
double diff = (int64_t)(step_clock - sc->next_step_clock);
|
2020-02-25 18:54:55 +01:00
|
|
|
if (diff < SDS_FILTER_TIME * sc->mcu_freq) {
|
|
|
|
// Rollback last step to avoid rapid step+dir+step
|
|
|
|
sc->next_step_clock = 0;
|
|
|
|
sc->next_step_dir = sdir;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int ret = queue_append(sc);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
// Store this step as the next pending step
|
|
|
|
sc->next_step_clock = step_clock;
|
|
|
|
sc->next_step_dir = sdir;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit next pending step (ie, do not allow a rollback)
|
|
|
|
int
|
|
|
|
stepcompress_commit(struct stepcompress *sc)
|
|
|
|
{
|
|
|
|
if (sc->next_step_clock)
|
|
|
|
return queue_append(sc);
|
2018-06-09 01:55:24 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-02-25 18:54:55 +01:00
|
|
|
// Flush pending steps
|
|
|
|
static int
|
|
|
|
stepcompress_flush(struct stepcompress *sc, uint64_t move_clock)
|
|
|
|
{
|
|
|
|
if (sc->next_step_clock && move_clock >= sc->next_step_clock) {
|
|
|
|
int ret = queue_append(sc);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return queue_flush(sc, move_clock);
|
|
|
|
}
|
|
|
|
|
2020-03-08 16:14:26 +01:00
|
|
|
// Reset the internal state of the stepcompress object
|
|
|
|
int __visible
|
|
|
|
stepcompress_reset(struct stepcompress *sc, uint64_t last_step_clock)
|
|
|
|
{
|
|
|
|
int ret = stepcompress_flush(sc, UINT64_MAX);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
sc->last_step_clock = last_step_clock;
|
|
|
|
sc->sdir = -1;
|
|
|
|
calc_last_step_print_time(sc);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-02-22 23:57:36 +01:00
|
|
|
// Set last_position in the stepcompress object
|
|
|
|
int __visible
|
2021-08-06 15:56:06 +02:00
|
|
|
stepcompress_set_last_position(struct stepcompress *sc, uint64_t clock
|
|
|
|
, int64_t last_position)
|
2021-02-22 23:57:36 +01:00
|
|
|
{
|
|
|
|
int ret = stepcompress_flush(sc, UINT64_MAX);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
sc->last_position = last_position;
|
2021-07-19 20:51:05 +02:00
|
|
|
|
|
|
|
// Add a marker to the history list
|
|
|
|
struct history_steps *hs = malloc(sizeof(*hs));
|
|
|
|
memset(hs, 0, sizeof(*hs));
|
2021-08-06 15:56:06 +02:00
|
|
|
hs->first_clock = hs->last_clock = clock;
|
2021-07-19 20:51:05 +02:00
|
|
|
hs->start_position = last_position;
|
|
|
|
list_add_head(&hs->node, &sc->history_list);
|
2021-02-22 23:57:36 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search history of moves to find a past position at a given clock
|
|
|
|
int64_t __visible
|
|
|
|
stepcompress_find_past_position(struct stepcompress *sc, uint64_t clock)
|
|
|
|
{
|
|
|
|
int64_t last_position = sc->last_position;
|
2021-07-19 20:40:50 +02:00
|
|
|
struct history_steps *hs;
|
|
|
|
list_for_each_entry(hs, &sc->history_list, node) {
|
|
|
|
if (clock < hs->first_clock) {
|
|
|
|
last_position = hs->start_position;
|
2021-02-22 23:57:36 +01:00
|
|
|
continue;
|
|
|
|
}
|
2021-07-19 20:40:50 +02:00
|
|
|
if (clock >= hs->last_clock)
|
|
|
|
return hs->start_position + hs->step_count;
|
2021-07-19 20:51:05 +02:00
|
|
|
int32_t interval = hs->interval, add = hs->add;
|
2021-07-19 20:40:50 +02:00
|
|
|
int32_t ticks = (int32_t)(clock - hs->first_clock) + interval, offset;
|
2021-02-22 23:57:36 +01:00
|
|
|
if (!add) {
|
|
|
|
offset = ticks / interval;
|
|
|
|
} else {
|
|
|
|
// Solve for "count" using quadratic formula
|
|
|
|
double a = .5 * add, b = interval - .5 * add, c = -ticks;
|
|
|
|
offset = (sqrt(b*b - 4*a*c) - b) / (2. * a);
|
|
|
|
}
|
2021-07-19 20:40:50 +02:00
|
|
|
if (hs->step_count < 0)
|
|
|
|
return hs->start_position - offset;
|
|
|
|
return hs->start_position + offset;
|
2021-02-22 23:57:36 +01:00
|
|
|
}
|
|
|
|
return last_position;
|
|
|
|
}
|
|
|
|
|
2020-03-08 16:14:26 +01:00
|
|
|
// Queue an mcu command to go out in order with stepper commands
|
|
|
|
int __visible
|
|
|
|
stepcompress_queue_msg(struct stepcompress *sc, uint32_t *data, int len)
|
|
|
|
{
|
|
|
|
int ret = stepcompress_flush(sc, UINT64_MAX);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
struct queue_message *qm = message_alloc_and_encode(data, len);
|
|
|
|
qm->req_clock = sc->last_step_clock;
|
|
|
|
list_add_tail(&qm->node, &sc->msg_queue);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-07-19 20:51:05 +02:00
|
|
|
// Return history of queue_step commands
|
|
|
|
int __visible
|
|
|
|
stepcompress_extract_old(struct stepcompress *sc, struct pull_history_steps *p
|
|
|
|
, int max, uint64_t start_clock, uint64_t end_clock)
|
|
|
|
{
|
|
|
|
int res = 0;
|
|
|
|
struct history_steps *hs;
|
|
|
|
list_for_each_entry(hs, &sc->history_list, node) {
|
|
|
|
if (start_clock >= hs->last_clock || res >= max)
|
|
|
|
break;
|
|
|
|
if (end_clock <= hs->first_clock)
|
|
|
|
continue;
|
|
|
|
p->first_clock = hs->first_clock;
|
|
|
|
p->last_clock = hs->last_clock;
|
|
|
|
p->start_position = hs->start_position;
|
|
|
|
p->step_count = hs->step_count;
|
|
|
|
p->interval = hs->interval;
|
|
|
|
p->add = hs->add;
|
|
|
|
p++;
|
|
|
|
res++;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-08-30 16:43:57 +02:00
|
|
|
|
2016-05-25 17:37:40 +02:00
|
|
|
/****************************************************************
|
|
|
|
* Step compress synchronization
|
|
|
|
****************************************************************/
|
|
|
|
|
|
|
|
// The steppersync object is used to synchronize the output of mcu
|
|
|
|
// step commands. The mcu can only queue a limited number of step
|
|
|
|
// commands - this code tracks when items on the mcu step queue become
|
|
|
|
// free so that new commands can be transmitted. It also ensures the
|
|
|
|
// mcu step queue is ordered between steppers so that no stepper
|
|
|
|
// starves the other steppers of space in the mcu step queue.
|
|
|
|
|
|
|
|
struct steppersync {
|
|
|
|
// Serial port
|
|
|
|
struct serialqueue *sq;
|
|
|
|
struct command_queue *cq;
|
|
|
|
// Storage for associated stepcompress objects
|
|
|
|
struct stepcompress **sc_list;
|
|
|
|
int sc_num;
|
|
|
|
// Storage for list of pending move clocks
|
|
|
|
uint64_t *move_clocks;
|
|
|
|
int num_move_clocks;
|
|
|
|
};
|
|
|
|
|
2016-07-13 20:47:03 +02:00
|
|
|
// Allocate a new 'steppersync' object
|
2018-06-06 03:40:32 +02:00
|
|
|
struct steppersync * __visible
|
2016-05-25 17:37:40 +02:00
|
|
|
steppersync_alloc(struct serialqueue *sq, struct stepcompress **sc_list
|
|
|
|
, int sc_num, int move_num)
|
|
|
|
{
|
|
|
|
struct steppersync *ss = malloc(sizeof(*ss));
|
|
|
|
memset(ss, 0, sizeof(*ss));
|
|
|
|
ss->sq = sq;
|
|
|
|
ss->cq = serialqueue_alloc_commandqueue();
|
|
|
|
|
|
|
|
ss->sc_list = malloc(sizeof(*sc_list)*sc_num);
|
|
|
|
memcpy(ss->sc_list, sc_list, sizeof(*sc_list)*sc_num);
|
|
|
|
ss->sc_num = sc_num;
|
|
|
|
|
|
|
|
ss->move_clocks = malloc(sizeof(*ss->move_clocks)*move_num);
|
|
|
|
memset(ss->move_clocks, 0, sizeof(*ss->move_clocks)*move_num);
|
|
|
|
ss->num_move_clocks = move_num;
|
|
|
|
|
|
|
|
return ss;
|
|
|
|
}
|
|
|
|
|
2016-11-30 07:58:45 +01:00
|
|
|
// Free memory associated with a 'steppersync' object
|
2018-06-06 03:40:32 +02:00
|
|
|
void __visible
|
2016-11-30 07:58:45 +01:00
|
|
|
steppersync_free(struct steppersync *ss)
|
|
|
|
{
|
|
|
|
if (!ss)
|
|
|
|
return;
|
|
|
|
free(ss->sc_list);
|
|
|
|
free(ss->move_clocks);
|
|
|
|
serialqueue_free_commandqueue(ss->cq);
|
|
|
|
free(ss);
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:59:26 +02:00
|
|
|
// Set the conversion rate of 'print_time' to mcu clock
|
2018-06-06 03:40:32 +02:00
|
|
|
void __visible
|
2019-02-27 20:18:07 +01:00
|
|
|
steppersync_set_time(struct steppersync *ss, double time_offset
|
|
|
|
, double mcu_freq)
|
2017-09-13 14:59:26 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i=0; i<ss->sc_num; i++) {
|
|
|
|
struct stepcompress *sc = ss->sc_list[i];
|
|
|
|
stepcompress_set_time(sc, time_offset, mcu_freq);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-25 17:37:40 +02:00
|
|
|
// Implement a binary heap algorithm to track when the next available
|
|
|
|
// 'struct move' in the mcu will be available
|
|
|
|
static void
|
|
|
|
heap_replace(struct steppersync *ss, uint64_t req_clock)
|
|
|
|
{
|
|
|
|
uint64_t *mc = ss->move_clocks;
|
|
|
|
int nmc = ss->num_move_clocks, pos = 0;
|
|
|
|
for (;;) {
|
|
|
|
int child1_pos = 2*pos+1, child2_pos = 2*pos+2;
|
|
|
|
uint64_t child2_clock = child2_pos < nmc ? mc[child2_pos] : UINT64_MAX;
|
|
|
|
uint64_t child1_clock = child1_pos < nmc ? mc[child1_pos] : UINT64_MAX;
|
|
|
|
if (req_clock <= child1_clock && req_clock <= child2_clock) {
|
|
|
|
mc[pos] = req_clock;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (child1_clock < child2_clock) {
|
|
|
|
mc[pos] = child1_clock;
|
|
|
|
pos = child1_pos;
|
|
|
|
} else {
|
|
|
|
mc[pos] = child2_clock;
|
|
|
|
pos = child2_pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find and transmit any scheduled steps prior to the given 'move_clock'
|
2018-06-06 03:40:32 +02:00
|
|
|
int __visible
|
2016-05-25 17:37:40 +02:00
|
|
|
steppersync_flush(struct steppersync *ss, uint64_t move_clock)
|
|
|
|
{
|
|
|
|
// Flush each stepcompress to the specified move_clock
|
|
|
|
int i;
|
2017-02-06 17:37:03 +01:00
|
|
|
for (i=0; i<ss->sc_num; i++) {
|
|
|
|
int ret = stepcompress_flush(ss->sc_list[i], move_clock);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
2016-05-25 17:37:40 +02:00
|
|
|
|
|
|
|
// Order commands by the reqclock of each pending command
|
|
|
|
struct list_head msgs;
|
|
|
|
list_init(&msgs);
|
|
|
|
for (;;) {
|
|
|
|
// Find message with lowest reqclock
|
|
|
|
uint64_t req_clock = MAX_CLOCK;
|
|
|
|
struct queue_message *qm = NULL;
|
|
|
|
for (i=0; i<ss->sc_num; i++) {
|
|
|
|
struct stepcompress *sc = ss->sc_list[i];
|
|
|
|
if (!list_empty(&sc->msg_queue)) {
|
|
|
|
struct queue_message *m = list_first_entry(
|
|
|
|
&sc->msg_queue, struct queue_message, node);
|
|
|
|
if (m->req_clock < req_clock) {
|
|
|
|
qm = m;
|
|
|
|
req_clock = m->req_clock;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-02 04:11:27 +01:00
|
|
|
if (!qm || (qm->min_clock && req_clock > move_clock))
|
2016-05-25 17:37:40 +02:00
|
|
|
break;
|
|
|
|
|
2016-11-02 04:11:27 +01:00
|
|
|
uint64_t next_avail = ss->move_clocks[0];
|
|
|
|
if (qm->min_clock)
|
|
|
|
// The qm->min_clock field is overloaded to indicate that
|
|
|
|
// the command uses the 'move queue' and to store the time
|
|
|
|
// that move queue item becomes available.
|
|
|
|
heap_replace(ss, qm->min_clock);
|
|
|
|
// Reset the min_clock to its normal meaning (minimum transmit time)
|
|
|
|
qm->min_clock = next_avail;
|
2016-05-25 17:37:40 +02:00
|
|
|
|
|
|
|
// Batch this command
|
|
|
|
list_del(&qm->node);
|
|
|
|
list_add_tail(&qm->node, &msgs);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transmit commands
|
|
|
|
if (!list_empty(&msgs))
|
|
|
|
serialqueue_send_batch(ss->sq, ss->cq, &msgs);
|
2017-02-06 17:37:03 +01:00
|
|
|
return 0;
|
2016-05-25 17:37:40 +02:00
|
|
|
}
|