From: Andy Spencer Date: Sat, 15 Mar 2014 21:55:50 +0000 (+0000) Subject: Fix event scheduling bug X-Git-Url: http://pileus.org/git/?p=~andy%2Fcsm213a-hw;a=commitdiff_plain;h=3519f0f7a6ced0a3a4419eac56fe49cd2c2fa938 Fix event scheduling bug --- diff --git a/hw2/main_comm.c b/hw2/main_comm.c index d5c66b1..118f6b3 100644 --- a/hw2/main_comm.c +++ b/hw2/main_comm.c @@ -228,12 +228,6 @@ void comm_handle_init(header_t *head, init_msg_t *body) if (body->control & MSG_CTL_VALID_DEVICE) comm_device_id = body->device; - if (body->control & MSG_CTL_VALID_START) - emit_set_start(comm_read_time(body->start)); - - if (body->control & MSG_CTL_VALID_PERIOD) - emit_set_period(comm_read_time(body->period)); - if (body->control & MSG_CTL_VALID_WORLD) time_ext_init(tdma_time(), comm_read_time(body->world)); @@ -244,6 +238,13 @@ void comm_handle_init(header_t *head, init_msg_t *body) if (body->control & MSG_CTL_BEGIN_SYNC) comm_sync_due = tdma_time() + comm_sync_delay; + + // Run these after world time is valid + if (body->control & MSG_CTL_VALID_START) + emit_set_start(comm_read_time(body->start)); + + if (body->control & MSG_CTL_VALID_PERIOD) + emit_set_period(comm_read_time(body->period)); } /** diff --git a/hw2/main_emit.c b/hw2/main_emit.c index bb751a6..e7a8bac 100644 --- a/hw2/main_emit.c +++ b/hw2/main_emit.c @@ -57,15 +57,14 @@ static void emit_setup(void) // | @ | @ | @ | // utc-^ last-^ now-^ if (emit_period) { - uint64_t now = tdma_time(); + uint64_t now = time_to_world(tdma_time()); uint64_t phase = emit_start % emit_period; uint64_t last = (now / emit_period) * emit_period; emit_due = last + phase; + time_printf("emit scheduled", emit_due); } else { emit_due = 0; } - - time_printf("emit scheduled", emit_due); } void emit_init(int alt, PinName pin, PinMode mode) @@ -192,12 +191,15 @@ void emit_transmit(uint64_t local, uint64_t world) prev = local; // Schedule task if needed - if (emit_due && emit_period && - world+emit_slack > emit_due) { + if (emit_period && world+emit_slack > emit_due) { emit_schedule(emit_due); emit_due += emit_period; - if (emit_due < local) - sirq_printf("missed emit deadline\r\n"); + } + if (emit_period && emit_due < world) { + sirq_printf("missed emit deadline\r\n"); + time_printf(" due ", emit_due); + time_printf(" period", emit_period); + time_printf(" world ", world); + emit_setup(); } } -