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.
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;
* 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)
emit_pcr = (uint32_t*)(PORTA_BASE + pin);
// Enable clocks
- SIM->SCGC6 |= SIM_SCGC6_TPM0_MASK;
-
+ SIM->SCGC6 |= SIM_SCGC6_TPM1_MASK;
SIM->SOPT2 |= SIM_SOPT2_TPMSRC(1);
- SIM->SOPT4 |= SIM_SOPT4_TPM1CLKSEL_MASK;
// Set pin mode
emit_pcr[0] = PORT_PCR_ISF_MASK
| mode;
// Setup Timer/PWM Module
- TPM0->SC = TPM_SC_TOF_MASK
- | TPM_SC_PS(1); // 24 MHz clock ?
- TPM0->CNT = TPM_CNT_COUNT(0);
- TPM0->MOD = TPM_MOD_MOD(0xFFFF);
-
- TPM0->CONTROLS[0].CnSC = TPM_CnSC_CHF_MASK // clear flag
- | TPM_CnSC_MSB_MASK // pulse output on match
- | TPM_CnSC_MSA_MASK // ..
- | TPM_CnSC_ELSA_MASK; // pulse high
+ TPM1->SC = TPM_SC_TOF_MASK;
+ TPM1->CNT = TPM_CNT_COUNT(0);
+ TPM1->MOD = TPM_MOD_MOD(0xFFFF);
- TPM0->CONTROLS[0].CnV = 0xFFFF; // time delay
+ 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; // ..
- TPM0->STATUS = TPM_STATUS_CH0F_MASK
+ TPM1->STATUS = TPM_STATUS_CH0F_MASK
| TPM_STATUS_TOF_MASK;
- TPM0->CONF = TPM_CONF_CSOO_MASK;
+ TPM1->CONF = TPM_CONF_CSOO_MASK;
}
void emit_enable(uint64_t start, uint64_t period)
{
- const int slack_tick = 0xC000; // tune based on emit_worst
+ const int slack_clocks = 0x8000; // tune based on emit_worst
emit_start = start;
emit_period = period;
emit_due = start + period;
- emit_slack = slack_tick * 1000 / 24;
+ // 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());
- uint16_t delay = (uint16_t)(when-now);
-
- // Clear pending flags
- //emit_pcr[0] |= PORT_PCR_ISF_MASK
+ 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
- TPM0->SC = TPM_SC_TOF_MASK;
+ TPM1->SC = TPM_SC_TOF_MASK;
// Set transmit time
- TPM0->CNT = TPM_CNT_COUNT(0);
- TPM0->CONTROLS[0].CnV = delay;
+ TPM1->CONTROLS[0].CnV = EMIT_CLOCKS(start);
+ TPM1->MOD = TPM_MOD_MOD(EMIT_CLOCKS(stop));
// Start the timer
- TPM0->SC = TPM_SC_TOF_MASK
+ TPM1->SC = TPM_SC_TOF_MASK
+ | TPM_SC_PS(1)
| TPM_SC_CMOD(1);
- // Debug
- sirq_printf("emitting event\r\n");
+ // Debug output
+ //sirq_printf("emitting event\r\n");
}
void emit_transmit(uint64_t local, uint64_t world)
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;
}
/**
* 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);
-#if 0
- // Message data
- header_t head;
- event_msg_t body;
-
- uint64_t world = time_to_world(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_SYNC;
+ head.msgid = MSG_ID_EVENT;
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);
+ body.device = serial_device_id;
+ body.event = event;
+ body.world = wtime;
+ body.local = ltime;
- tdma_start(serial_tdma_xmt);
+ // Transmit message to BBB
sirq_write(port, &head, sizeof(head));
sirq_write(port, &body, sizeof(body));
- tdma_stop(serial_tdma_xmt);
-#endif
}
/**
*/
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;
}
/**
#endif
if (tdma_stamp(tdma_evt, &event))
- serial_send_event(0, event);
- tdma_stop(tdma_evt);
+ serial_send_event(sirq_bbb, 0, event);
+ 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),
int main(int argc, char **argv)
{
tdma_init();
- emit_init(4, PTC1, PullDown);
+ emit_init(3, PTE20, PullDown);
+
+ //pin = 1;
// Open serial ports
sirq_dbg = sirq_open(SIRQ_UART0, USBTX, USBRX, 115200); // to pc