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,
51				(double)(pkt->timestamp * pi->packet_ts_period) / 1000.0,
52				packetTs / 1000., edgeTsPs / 1000.);
53
54		}
55
56		if (tdcChannel == 14) {
57			// dummy data, can be ignored
58		}
59
60		// rollover marker
61		if (tdcChannel == 15) {
62			rolloverEra += pi->tdc_rollover_period;
63		}
64	}
65}
66
67void Ndigo6GApp::ConfigureTDC(ndigo6g12_configuration* config) {
68	// enable TDC channels
69	for (int i = 0; i < NDIGO6G12_TDC_CHANNEL_COUNT; i++) {
70		// for NIM pulses: trigger at -350 mV
71		config->tdc_trigger_offsets[i] = NDIGO6G12_DC_OFFSET_N_NIM;
72
73		// enable TDC channel
74		config->tdc_configuration.channel[i].enable =
75		                                       (tdcChannelMask & (1 << i)) != 0;
76
77		// enable falling edge trigger as input to trigger matrix for selected
78		// TDC channel
79		// only required if used as trigger source for Gating, TiGer
80		// or ADC trigger blocks
81		config->trigger[NDIGO6G12_TRIGGER_TDC0 + i].edge = true;
82		config->trigger[NDIGO6G12_TRIGGER_TDC0 + i].rising = false;
83		// threshold not applicable for TDC inputs
84		// trigger threshold is set via tdc_trigger_offsets[i]
85		config->trigger[NDIGO6G12_TRIGGER_TDC0 + i].threshold = 0;
86	}
87}