* Time synchronization is performed in both directions.
*/
-/****************
- * Testing vars *
- ****************/
-
-uint32_t test_xmt_enab = 0;
-uint64_t test_xmt_time0 = 0;
-uint64_t test_xmt_time1 = 0;
-
-uint32_t test_rcv_enab = 0;
-uint64_t test_rcv_time = 0;
-
/*******************
* Timer functions *
*******************/
uint64_t time_last_local; // timestamp at last time sync
uint64_t time_last_world; // offset at last time sync
-/**
- * Generate time stamp for an async event:
- * local: drift compensated wall-clock time
- * world: nanoseconds in world time world
- * valid: local timestamp at valid valid
- */
-//uint64_t time_to_local(uint64_t world, uint64_t valid)
-//{
-// uint64_t now =
-// local = + (stamp);
-//}
-
/**
* Generate time stamp for an async event:
* time: drift compensated wall-clock time
return time_last_world + elapsed;
}
-/**
- * Compensate the Real-Time-Clock oscillator for
- * temperature and drift errors. Called at 1Hz and
- * synchronous to the RTC 1Hz output.
- */
-void time_rtc_comp(void)
-{
- // todo
-}
-
/**
* Synchronize the timer internal state with updates
* from an external time sync message.
void time_ext_sync(uint64_t local, uint64_t world)
{
uint64_t guess = time_to_world(local);
- uint64_t error = world > guess ? world - guess :
- guess > world ? guess - world : 0;
- int ahead = guess > world;
time_last_local = local;
time_last_world = (guess/2) + (world/2);
world = time_last_world;
//#ifdef VERBOSE
-#if 0
+#if 1
+ uint64_t error = world > guess ? world - guess :
+ guess > world ? guess - world : 0;
+ int ahead = guess > world;
sirq_printf("syncing clocks: %6d=%d.%04u -> %d.%04u (err: %s%ld.%09lu)\r\n",
(int)((local / NSEC_PER_SEC)),
(int)((guess / NSEC_PER_SEC)),
(uint32_t)(error % (int64_t)NSEC_PER_SEC));
#endif
//#endif
-
- // .000000284
- // .000000253
- // .000000264
- // .000000451
- // .000000284
- // .000000267
- // .000000223
- // .000000326
-
}
void time_printf(const char *label, uint64_t local)
(int)(world % NSEC_PER_SEC));
}
+/*********************
+ * Signal generation *
+ *********************/
+
+#define EMIT_CLOCKS(nsec) ((uint16_t)((nsec) * 24 / 1000))
+#define EMIT_NSEC(clocks) ((uint16_t)((clocks) * 1000 / 24))
+
+static uint32_t *emit_pcr = 0; // transmit pin name
+
+static uint64_t emit_start = 0; // transmit start time (world time)
+static uint64_t emit_period = 0; // transmit period
+static uint64_t emit_due = 0; // next transmit (world time)
+
+static uint32_t emit_slack = 0; // how far ahead we need to schedule, in us
+static uint32_t emit_worst = 0; // worst-case latency in task table
+
+void emit_init(int alt, PinName pin, PinMode mode)
+{
+ // Find pin
+ emit_pcr = (uint32_t*)(PORTA_BASE + pin);
+
+ // Enable clocks
+ SIM->SCGC6 |= SIM_SCGC6_TPM1_MASK;
+ SIM->SOPT2 |= SIM_SOPT2_TPMSRC(1);
+
+ // Set pin mode
+ emit_pcr[0] = PORT_PCR_ISF_MASK
+ | PORT_PCR_MUX(alt)
+ | mode;
+
+ // Setup Timer/PWM Module
+ TPM1->SC = TPM_SC_TOF_MASK;
+ TPM1->CNT = TPM_CNT_COUNT(0);
+ TPM1->MOD = TPM_MOD_MOD(0xFFFF);
+
+ TPM1->CONTROLS[0].CnSC = TPM_CnSC_CHF_MASK // clear flag
+ | TPM_CnSC_MSB_MASK // set output highon match,
+ | TPM_CnSC_ELSB_MASK // cleared on overflow
+ | TPM_CnSC_ELSA_MASK; // ..
+
+ TPM1->STATUS = TPM_STATUS_CH0F_MASK
+ | TPM_STATUS_TOF_MASK;
+
+ TPM1->CONF = TPM_CONF_CSOO_MASK;
+}
+
+void emit_enable(uint64_t start, uint64_t period)
+{
+ const int slack_clocks = 0x8000; // tune based on emit_worst
+
+ emit_start = start;
+ emit_period = period;
+ emit_due = start + period;
+
+ // TODO - tune slack time
+ // TODO - check clock power
+ // TODO - TPM clock source
+ emit_slack = EMIT_NSEC(slack_clocks);
+
+ time_printf("emit scheduled", emit_due);
+}
+
+void emit_schedule(uint64_t when)
+{
+ uint64_t now = time_to_world(tdma_time());
+ uint64_t start = when - now; // transmit time
+ uint64_t stop = start + 100000; // 100 us pulse
+
+ // Disable timer
+ TPM1->SC = TPM_SC_TOF_MASK;
+
+ // Set transmit time
+ TPM1->CONTROLS[0].CnV = EMIT_CLOCKS(start);
+ TPM1->MOD = TPM_MOD_MOD(EMIT_CLOCKS(stop));
+
+ // Start the timer
+ TPM1->SC = TPM_SC_TOF_MASK
+ | TPM_SC_PS(1)
+ | TPM_SC_CMOD(1);
+
+ // Debug output
+ //sirq_printf("emitting event\r\n");
+}
+
+void emit_transmit(uint64_t local, uint64_t world)
+{
+ static uint64_t prev = 0;
+
+ // Record how how much time we have to reschedule
+ if (prev && (local-prev) > emit_worst)
+ emit_worst = (local-prev);
+ prev = local;
+
+ // Schedule task if needed
+ if (emit_due && emit_period &&
+ world+emit_slack > emit_due) {
+ emit_schedule(emit_due);
+ emit_due += emit_period;
+ }
+}
+
/************************
* Serial I/O functions *
************************/
uint8_t buffer[256];
} parser_t;
-const uint64_t serial_sync_delay = NSEC_PER_SEC / 100; // 1hz
-static uint64_t serial_sync_due = 0;
+static uint32_t serial_device_id = 0;
+
+const uint64_t serial_sync_delay = NSEC_PER_SEC / 100; // 1hz
+static uint64_t serial_sync_due = 0;
-static tdma_t *serial_tdma_rcv = NULL;
-static tdma_t *serial_tdma_xmt = NULL;
+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_prev_local = 0;
+static uint64_t serial_prev_seq = 0;
-static uint64_t serial_xmt_local = 0;
-static uint64_t serial_xmt_seq = 0;
+static uint64_t serial_xmt_local = 0;
+static uint64_t serial_xmt_seq = 0;
+
+/**
+ * Convert world to local time
+ */
+uint64_t serial_read_time(ntime_t time)
+{
+ return ((uint64_t)time.seconds) * NSEC_PER_SEC
+ + ((uint64_t)time.nanosec);
+}
+
+ntime_t serial_write_time(uint64_t time)
+{
+ ntime_t buf = {};
+ buf.seconds = time / NSEC_PER_SEC;
+ buf.nanosec = time % NSEC_PER_SEC;
+ return buf;
+}
+
+/**
+ * Output initialization message init message
+ */
+void serial_send_init(uint16_t device, uint64_t local)
+{
+}
/**
* Output time sync message
tdma_stop(serial_tdma_rcv);
- test_xmt_enab = 1;
- test_xmt_time0 = 0;
- test_xmt_time1 = 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
- //local = test_xmt_time1;
int valid = tdma_stamp(serial_tdma_xmt, &local);
- if (!valid) {
+ if (!valid)
sirq_printf("sync transmit time -- missed\r\n");
- } else {
+ else
//time_printf("sync transmit time ", local);
- //time_printf("sync transmit test0", test_xmt_time0);
- //time_printf("sync transmit test1", test_xmt_time1);
- }
tdma_start(serial_tdma_rcv);
* event: id of the received event
* time: compensated timestamp of the event
*/
-void serial_send_event(uint16_t event, uint64_t local)
+void serial_send_event(sirq_t *port, uint16_t event, uint64_t local)
{
+ time_printf("event received", local);
+
+ // Convert timestamp
uint64_t world = time_to_world(local);
+ ntime_t ltime = serial_write_time(local);
+ ntime_t wtime = serial_write_time(world);
- ntime_t time = {};
- time.seconds = (uint32_t)(world / NSEC_PER_SEC);
- time.nanosec = (uint32_t)(world % NSEC_PER_SEC);
+ // Message data
+ header_t head = {};
+ event_msg_t body = {};
+
+ // Transmit sync message
+ head.header = MSG_HEADER;
+ head.msgid = MSG_ID_EVENT;
+ head.length = sizeof(body);
+ head.cksum = 0; // todo
- sirq_printf("event received - %08x:%08x - %u.%09u\r\n",
- (uint32_t)(local >> 32), (uint32_t)local,
- time.seconds, time.nanosec);
- // todo
+ body.device = serial_device_id;
+ body.event = event;
+ body.world = ltime;
+ body.local = wtime;
+
+ // Transmit message to BBB
+ sirq_write(port, &head, sizeof(head));
+ sirq_write(port, &body, sizeof(body));
+}
+
+/**
+ * Handle init message
+ */
+void serial_handle_init(init_msg_t *msg)
+{
+ sirq_printf("initialize: %s %s %s %s %s\r\n",
+ msg->valid & MSG_VALID_DEVICE ? "DEV" : "dev",
+ msg->valid & MSG_VALID_START ? "START" : "start",
+ msg->valid & MSG_VALID_PERIOD ? "PERIOD" : "period",
+ msg->valid & MSG_VALID_WORLD ? "WORLD" : "world",
+ msg->valid & MSG_VALID_SYNC ? "SYNC" : "sync");
+ sirq_printf(" dev -- %d\r\n", msg->device);
+ time_printf(" start ", serial_read_time(msg->start));
+ time_printf(" period", serial_read_time(msg->period));
+ time_printf(" world ", serial_read_time(msg->world));
+
+ if (msg->valid & MSG_VALID_DEVICE)
+ serial_device_id = msg->device;
+
+ if (msg->valid & MSG_VALID_START ||
+ msg->valid & MSG_VALID_PERIOD) {
+ uint64_t start = serial_read_time(msg->start);
+ uint64_t period = serial_read_time(msg->period);
+ emit_enable(start, period);
+ }
+
+ if (msg->valid & MSG_VALID_WORLD) {
+ uint64_t world = serial_read_time(msg->world);
+ uint64_t local = tdma_time();
+ time_ext_init(local, world);
+ }
+
+ if (msg->valid & MSG_VALID_SYNC)
+ serial_sync_due = tdma_time() + serial_sync_delay;
}
/**
uint64_t world = ((uint64_t)msg->time.seconds) * NSEC_PER_SEC
+ ((uint64_t)msg->time.nanosec);
- // Initialize
- if (msg->seq == 0) {
- uint64_t local = tdma_time();
- time_ext_init(local, world);
- }
-
// Valid times timestamp
if (serial_prev_seq == (msg->seq-1)) {
uint64_t local = serial_prev_local;
void serial_deliver(int msgid, void *body)
{
switch (msgid) {
+ case MSG_ID_INIT:
+ //sirq_printf("received init msg\r\n");
+ serial_handle_init((init_msg_t*)body);
+ break;
case MSG_ID_SYNC:
//sirq_printf("received sync msg\r\n");
serial_handle_sync((sync_msg_t*)body);
case 1: // Header
if (parser->index == sizeof(header_t)) {
if (head->length <= max_length &&
- head->msgid <= MSG_MAXID) {
+ head->msgid <= MSG_MAX_ID) {
parser->state = 2;
} else {
parser->index = 0;
#endif
if (tdma_stamp(tdma_evt, &event))
- serial_send_event(0, event);
+ serial_send_event(sirq_bbb, 0, event);
tdma_stop(tdma_evt);
tdma_start(tdma_evt);
}
which ^= 1;
}
+void task_emit(uint64_t local, uint64_t world)
+{
+ emit_transmit(local, world);
+}
+
void task_debug(uint64_t local, uint64_t world)
{
//tdma_debug(tdma_rcv);
uint64_t due;
} tasks[] = {
{ task_serial, 0 }, // always
- { task_events, 1000000000 }, // always
+ { task_events, 0 }, // always -- testing
{ task_sync, 0 }, // always
+ { task_emit, 0 }, // always
{ task_leds, 100000000 }, // 10hz
{ task_debug, 1000000000 }, // 1hz
};
int main(int argc, char **argv)
{
tdma_init();
+ emit_init(3, PTE20, PullDown);
+
+ //pin = 1;
// Open serial ports
sirq_dbg = sirq_open(SIRQ_UART0, USBTX, USBRX, 115200); // to pc
sirq_mbed = sirq_open(SIRQ_UART2, PTD3, PTD2, 115200); // to mbed
// Setup timers
- tdma_evt = tdma_open(TDMA_CHAN0, 3, PTC9, PullUp); // async event
+ tdma_evt = tdma_open(TDMA_CHAN0, 3, PTC9, PullDown); // async event
// mbed time sync
tdma_rcv = tdma_open(TDMA_CHAN2, 3, PTD2, PullUp); // time sync rcv
//sirq_printf("MGC - C10 %02hx\r\n", MCG->C10); // 00
// Run background loop
- printf("hello");
while (true)
background();
+ // Performance testing
+ //uint64_t prev = 0, due = 0;
+ //uint64_t worst[10] = {};
+ //int count = 0;
+ //while (true) {
+ // uint64_t local = tdma_time();
+ // if (prev && (local-prev) > worst[count])
+ // worst[count] = (local-prev);
+ // prev = local;
+ // if (local > due) {
+ // if (count == 5) {
+ // static char str[] = "background background background\r\n";
+ // sirq_write(sirq_dbg, str, sizeof(str));
+ // }
+ // if (count == 9) {
+ // sirq_printf("background\r\n");
+ // for (int i = 0; i < 10; i++) {
+ // sirq_printf(" worst[%d] = 0.%09u\r\n",
+ // i, worst[i]);
+ // worst[i] = 0;
+ // }
+ // }
+ // due += NSEC_PER_SEC;
+ // count = (count + 1) % 10;
+ // }
+ //}
+
// Run tests
//test_main();