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)
void emit_enable(uint64_t start, uint64_t period)
{
- const int slack_tick = 0x8000; // 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);
}
-#define CLOCKS(x) ((uint16_t)((x) * 24 / 1000))
-
void emit_schedule(uint64_t when)
{
uint64_t now = time_to_world(tdma_time());
- uint64_t start = when - now;
- uint64_t stop = start + 100000;
+ 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 = CLOCKS(start);
- TPM1->MOD = TPM_MOD_MOD(CLOCKS(stop));
+ 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
+ // Debug output
//sirq_printf("emitting event\r\n");
}
* 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;
-
+ // Convert timestamp
uint64_t world = time_to_world(local);
+ ntime_t ntime = 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.time = ntime;
- 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
}
/**
#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);
}