5.7 ndigo6g12_tdc.cpp

 1#include <stdio.h>
 2#include "ndigo6g12_app.h"
 3
 4void Ndigo6GApp::ProcessTDCPacket(crono_packet* pkt) {
 5	// TDC packet timestamp relates to end of packet
 6	// adjust for timespan covered
 7	double packetTs =
 8		(double)(pkt->timestamp - pi->tdc_packet_timestamp_offset);
 9
10	// calculate packet timestamp in picoseconds
11	packetTs *= pi->packet_ts_period;
12
13	// packet length is number of 64-bit words of data
14	// 2 TDC events are stored in each 64-bit chunk of packet data
15	uint32_t tdcEventCount = pkt->length * 2;
16
17
18	// event encoding:
19	// Bits 31 downto 8: event timestamp in TDC bins relative to packet
20	// 		             timestamp
21	// Bits 7 downto 4: event flags
22	// Bits 3 downto 0: channel number
23	uint32_t* tdcEventData = (uint32_t*)(pkt->data);
24
25	// each TDC packet covers up to 3 coarse TDC periods
26	// the end of one period is marked by an event on channel 15
27	uint32_t rolloverEra = 0;
28	// print all TDC timestamps of the packet
29	for (uint32_t i = 0; i < tdcEventCount; i++) {
30		// TDC channel number
31		// 0 - 3: LEMO inputs
32		// 15: internal marker: end of current TDC time frame
33		uint32_t tdcChannel = tdcEventData[i] & 0xf;
34		// event flags
35		uint32_t flags = (tdcEventData[i] >> 4) & 0xf;
36		// 24-bit timestamp
37		uint32_t event_ts = tdcEventData[i] >> 8;
38
39		// valid input channel?
40		if (tdcChannel < 4) {
41			// add accumulated rollovers since start of packet
42			event_ts += rolloverEra;
43			// calculate timestamp of TDC event in picoseconds
44			double edgeTsPs = event_ts * pi->tdc_period;
45			edgeTsPs += packetTs;
46			ProcessTDCTimestamp(tdcChannel, edgeTsPs);
47			printf("TDC event on channel %d timestamp: packet without "
48				"shift %.3f ns, "
49				"with shift %.3f ns, edge %.3f ns  \n",
50				tdcChannel, (double)(pkt->timestamp * pi->packet_ts_period) / 1000.0,
51				packetTs / 1000., edgeTsPs / 1000.);
52
53		}
54
55		if (tdcChannel == 14) {
56			// dummy data, can be ignored
57		}
58
59		// rollover marker
60		if (tdcChannel == 15) {
61			rolloverEra += pi->tdc_rollover_period;
62		}
63	}
64}
65
66void Ndigo6GApp::ConfigureTDC(ndigo6g12_configuration* config) {
67	// enable TDC channels
68	for (int i = 0; i < NDIGO6G12_TDC_CHANNEL_COUNT; i++) {
69		// for NIM pulses: trigger at -350 mV
70		config->tdc_trigger_offsets[i] = NDIGO6G12_DC_OFFSET_N_NIM;
71
72		// enable TDC channel
73		config->tdc_configuration.channel[i].enable = (tdcChannelMask & (1 << i)) != 0;
74
75		// enable falling edge trigger as input to trigger matrix for selected
76		// TDC channel 
77		// only required if used as trigger source for Gating, TiGer
78		// or ADC trigger blocks
79		config->trigger[NDIGO6G12_TRIGGER_TDC0 + i].edge = true;
80		config->trigger[NDIGO6G12_TRIGGER_TDC0 + i].rising = false;
81		// threshold not applicable for TDC inputs
82		// trigger threshold is set via tdc_trigger_offsets[i]
83		config->trigger[NDIGO6G12_TRIGGER_TDC0 + i].threshold = 0;
84	}
85}