DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
WIB_DTS.cpp
Go to the documentation of this file.
1#include "ers/ers.hpp"
2#include "wibmod/Issues.hpp"
3#include "wibmod/WIB1/WIB.hh"
5
6#include <unistd.h>
7
8#include <chrono>
9#include <stdio.h>
10
11void
12WIB::InitializeDTS(uint8_t PDTSsource, uint8_t clockSource, uint32_t PDTSAlignment_timeout)
13{
14 // Disable the PDTS
15 WriteWithRetry("DTS.PDTS_ENABLE", 0x0);
16
17 // Disable SI5344 outputs (fixed by SI5344 config)
18 WriteWithRetry("DTS.SI5344.ENABLE", 0);
19 // Disable SI5344 (fixed by SI5344 config)
20 WriteWithRetry("DTS.SI5344.RESET", 1);
21
22 // Reset the I2C firmware
23 WriteWithRetry("DTS.CDS.I2C.RESET", 1);
24
25 if (0 == clockSource) {
26 printf("Using PDTS for DUNE timing.\n\nConfiguring clock and data separator\n");
27 // Bring up the CDS
28 float frequency = 0;
29 try {
30 frequency = ConfigureDTSCDS(PDTSsource);
31 } catch (BUException::exBase& e) {
32 frequency = 0;
33 e.Append("Failed to communicate with the DTS CDS via I2C\n");
34 throw;
35 }
36 uint32_t LOL = Read("DTS.CDS.LOL");
37 uint32_t LOS = Read("DTS.CDS.LOS");
38 printf("CDS frequency %f\n", frequency);
39 printf("CDS LOL=%d LOS=%d\n", LOL, LOS);
40
41 // Check for the correct frequency, not in LOS, and not in LOL
42 // if( (2.4136e+08 == frequency) &&
43 if (LOL || LOS) {
44 BUException::WIB_DTS_ERROR e;
45 e.Append("Failed to configure CDS chip\n");
46 throw e;
47 }
48 } else {
49 printf("Using local OSC for DUNE timing\n");
50 }
51 // CDS is up
52
53 printf("\nConfiguring SI5344.\n");
54
55 // Set the SI5344 source
56 WriteWithRetry("DTS.SI5344.INPUT_SELECT", clockSource);
57 // Configure Si5344 with default config file
58 // Do the I2C configuration
59 try {
61 } catch (BUException::exBase& e) {
62 // Disable SI5344 outputs
63 WriteWithRetry("DTS.SI5344.ENABLE", 0);
64 // Disable SI5344
65 WriteWithRetry("DTS.SI5344.RESET", 1);
66
67 e.Append("Error in LoadConfigDTS_SI5344\n");
68 throw;
69 }
70
71 usleep(100000);
72
73 // Check that SI5344 is locked on
74 if (ReadWithRetry("DTS.SI5344.LOS") || ReadWithRetry("DTS.SI5344.LOL")) {
75 // Disable SI5344 outputs
76 WriteWithRetry("DTS.SI5344.ENABLE", 0);
77 // Disable SI5344
78 WriteWithRetry("DTS.SI5344.RESET", 1);
79
80 // Throw
81 BUException::WIB_DTS_ERROR e;
82 e.Append("Failed to configure the SI5344 chip correctly\n");
83 throw e;
84 }
85
86 // Enable the clock for FPGA
87 WriteWithRetry("DTS.SI5344.ENABLE", 1);
88 usleep(100000);
89
90 char const* const PDTSStates[] = { "W_RST", "W_LINK", "W_FREQ", "W_ADJUST", "W_ALIGN", "W_LOCK", "W_PHASE", "W_RDY",
91 "RUN", "0x9", "0xA", "0xB", "ERR_R", "ERR_T", "ERR_P", "0xF" };
92
93 if (0 == clockSource) {
94 printf("\nSetup PDTS.\n");
95 bool timeout_exists = true;
96 if (PDTSAlignment_timeout == 0)
97 timeout_exists = false;
98
99 auto start_time = std::chrono::high_resolution_clock::now();
100 bool timed_out = false;
101
102 while ((timeout_exists == false) || timed_out == false) {
103 // Using PDTS, set that up.
104 usleep(500000);
105 WriteWithRetry("DTS.PDTS_ENABLE", 1);
106 usleep(500000); // needed in new PDTS system to get to a good state before giving up and trying a new phase.
107
108 // See if we've locked
109 uint32_t pdts_state = ReadWithRetry("DTS.PDTS_STATE");
110 printf("PDTS state: %s (0x%01X)\n", PDTSStates[pdts_state & 0xF], pdts_state);
111 if ((pdts_state < 0x6) || (pdts_state > 0x8)) {
112 WriteWithRetry("DTS.PDTS_ENABLE", 0);
113 // dynamic post-amble
114 Write("DTS.SI5344.I2C.RESET", 1);
116 Write("DTS.SI5344.I2C.RESET", 1);
117 WriteDTS_SI5344(0x1C, 0x1, 1);
118 } else if (0x6 == pdts_state) {
119 ers::info(dunedaq::wibmod::WaitingForAlignment(ERS_HERE));
120 } else if (0x7 == pdts_state) {
121 ers::info(dunedaq::wibmod::WaitingForTimestamp(ERS_HERE));
122 } else {
123 return; // 0x8 == pdts_state
124 }
125 auto now = std::chrono::high_resolution_clock::now();
126 auto duration = now - start_time;
127 if (duration.count() > PDTSAlignment_timeout)
128 timed_out = true;
129 }
130 // If we get here something went wrong
131 Write("DTS.SI5344.I2C.RESET", 1);
133 Write("DTS.SI5344.I2C.RESET", 1);
134 WriteDTS_SI5344(0x1C, 0x1, 1);
135
136 BUException::WIB_DTS_ERROR e;
137 e.Append("Failed to configure the PDTS correctly within timeout\n");
138 throw e;
139 }
140}
141
142void
144{
145 WriteWithRetry("DTS.CONVERT_CONTROL.HALT", 0);
146 WriteWithRetry("DTS.CONVERT_CONTROL.ENABLE", 1);
147 WriteWithRetry("DTS.CONVERT_CONTROL.START_SYNC", 1);
148}
149
150void
152{
153 if (Read("DTS.PDTS_STATE") != 0x8) {
154 BUException::WIB_DTS_ERROR e;
155 e.Append("WIB is not in PDTS state RUN(0x8)\n");
156 throw e;
157 }
158}
#define ERS_HERE
void Append(const char *buffer)
void WriteWithRetry(uint16_t address, uint32_t value)
Definition WIBBase.cpp:163
uint32_t ReadWithRetry(uint16_t address)
Definition WIBBase.cpp:142
uint32_t Read(uint16_t address)
Definition WIBBase.cpp:147
void Write(uint16_t address, uint32_t value)
Definition WIBBase.cpp:168
void LoadConfigDTS_SI5344(std::string const &fileName)
void InitializeDTS(uint8_t PDTSsource=0, uint8_t clockSource=0, uint32_t PDTSAlignment_timeout=0)
Definition WIB_DTS.cpp:12
void SetDTS_SI5344Page(uint8_t page)
void StartSyncDTS()
Definition WIB_DTS.cpp:143
void WriteDTS_SI5344(uint16_t address, uint32_t value, uint8_t byte_count=4)
Definition WIB_SI5344.cpp:9
float ConfigureDTSCDS(uint8_t source=0)
Definition WIB_CDS.cpp:48
void PDTSInRunningState()
Definition WIB_DTS.cpp:151
void info(const Issue &issue)
Definition ers.hpp:121