static uint32_t serial_device_id = 0;
-const uint64_t serial_sync_delay = NSEC_PER_SEC / 100; // 1hz
+const uint64_t serial_sync_delay = NSEC_PER_SEC / 100;
static uint64_t serial_sync_due = 0;
static tdma_t *serial_tdma_rcv = NULL;
static tdma_t *serial_tdma_xmt = NULL;
-static uint64_t serial_prev_local = 0;
-static uint64_t serial_prev_seq = 0;
-
-static uint64_t serial_xmt_local = 0;
-static uint64_t serial_xmt_seq = 0;
-
/**
* Convert world to local time
*/
return buf;
}
+int serial_time_stamp(tdma_t *port, uint64_t *local, uint64_t *world,
+ const char *msg)
+{
+ int valid = tdma_stamp(port, local);
+ *world = time_to_world(*local);
+
+ if (!valid)
+ sirq_printf("%s -- missing\r\n", msg);
+ //else
+ // time_printf(msg, current);
+
+ return valid;
+}
+
/**
* Output initialization message init message
*/
if (serial_sync_due == 0 || now < serial_sync_due)
return; // not ready
- //sirq_printf("sending sync\r\n");
-
- // Calculate world time
- uint64_t local = 0;
- uint64_t world = time_to_world(serial_xmt_local);
-
// Message data
header_t head;
sync_msg_t body;
- // Transmit sync message
+ // Write header
head.header = MSG_HEADER;
head.msgid = MSG_ID_SYNC;
head.length = sizeof(body);
head.cksum = 0; // todo
- body.seq = serial_xmt_seq;
- body.time.seconds = world / NSEC_PER_SEC;
- body.time.nanosec = world % NSEC_PER_SEC;
-
- tdma_stop(serial_tdma_rcv);
-
+ tdma_stop(serial_tdma_rcv, 0);
tdma_start(serial_tdma_xmt);
- sirq_write(port, &head, sizeof(head));
- sirq_write(port, &body, sizeof(body));
- tdma_stop(serial_tdma_xmt);
- // save transmit time
- int valid = tdma_stamp(serial_tdma_xmt, &local);
- if (!valid)
- sirq_printf("sync transmit time -- missed\r\n");
- else
- //time_printf("sync transmit time ", local);
+ sirq_write(port, &head, sizeof(head));
+ tdma_stop(serial_tdma_xmt, 100);
tdma_start(serial_tdma_rcv);
- serial_xmt_seq += 1;
+ // Save transmit time
+ uint64_t local = 0, world = 0;
+ serial_time_stamp(serial_tdma_xmt, &local, &world,
+ "sync time transmit");
+
+ // Debug output
+ //sirq_printf("sync time transmit\r\n");
+ //time_printf(" local", local);
+ //time_printf(" world", world);
+
+ // Write body with updated time and send
+ body.time = serial_write_time(world);
+
+ sirq_write(port, &body, sizeof(body));
+
+ // Queue next transmit time
serial_sync_due = 0;
- serial_xmt_local = local;
}
/**
// Convert timestamp
uint64_t world = time_to_world(local);
- ntime_t ntime = serial_write_time(world);
+ ntime_t ltime = serial_write_time(local);
+ ntime_t wtime = serial_write_time(world);
// Message data
header_t head = {};
body.device = serial_device_id;
body.event = event;
- body.time = ntime;
+ body.world = wtime;
+ body.local = ltime;
// Transmit message to BBB
sirq_write(port, &head, sizeof(head));
*/
void serial_handle_sync(sync_msg_t *msg)
{
- // Read receive timestamp for next time sync message
- uint64_t current = 0;
- int valid = tdma_stamp(serial_tdma_rcv, ¤t);
- if (!valid)
- sirq_printf("sync receive time -- missing\r\n");
- //else
- // time_printf("sync receive time ", current);
- tdma_stop(serial_tdma_rcv);
+ // Read receive timestamp
+ uint64_t local = 0, world = 0;
+ serial_time_stamp(serial_tdma_rcv, &local, &world,
+ "sync time receive ");
+ tdma_stop(serial_tdma_rcv, 0);
- // Lookup times
- uint64_t world = ((uint64_t)msg->time.seconds) * NSEC_PER_SEC
- + ((uint64_t)msg->time.nanosec);
+ // Lookup reference time from message
+ uint64_t reference = serial_read_time(msg->time);
- // Valid times timestamp
- if (serial_prev_seq == (msg->seq-1)) {
- uint64_t local = serial_prev_local;
- time_ext_sync(local, world);
- }
+ // Debug output
+ //sirq_printf("sync time receive\r\n");
+ //time_printf(" local", local);
+ //time_printf(" world", world);
+ //time_printf(" ref ", reference);
+
+ // Synchronize the clocks
+ time_ext_sync(local, reference);
// Queue transmit to other board
serial_sync_due = tdma_time() + serial_sync_delay;
-
- // Update states
- serial_prev_local = current;
- serial_prev_seq = msg->seq;
}
/**
if (tdma_stamp(tdma_evt, &event))
serial_send_event(sirq_bbb, 0, event);
- tdma_stop(tdma_evt);
+ tdma_stop(tdma_evt, 0);
tdma_start(tdma_evt);
}
//sirq_debug(sirq_mbed);
+ serial_send_event(sirq_bbb, 1, local);
+
#ifdef VERBOSE
sirq_printf("background - %6u.%02u -> %u.%02u\r\n",
(uint32_t)(local / NSEC_PER_SEC),