Line data Source code
1 : #include "wibmod/WIB1/WIB.hh"
2 : #include "wibmod/WIB1/WIBException.hh"
3 : #include <fstream>
4 : #include <unistd.h> //usleep
5 :
6 : #define SI5342_CONFIG_FILENAME "FELIX_SI5342.txt"
7 :
8 : void
9 0 : WIB::WriteDAQ_SI5342(uint16_t address, uint32_t value, uint8_t byte_count)
10 : {
11 0 : WriteI2C("DAQ.SI5342.I2C", address, value, byte_count);
12 0 : }
13 : uint32_t
14 0 : WIB::ReadDAQ_SI5342(uint16_t address, uint8_t byte_count)
15 : {
16 0 : return ReadI2C("DAQ.SI5342.I2C", address, byte_count);
17 : }
18 :
19 : void
20 0 : WIB::ResetSi5342()
21 : {
22 0 : Write("DAQ.SI5342.RESET", 0x1);
23 0 : Write("DAQ.SI5342.RESET", 0x0);
24 0 : usleep(100000);
25 0 : }
26 :
27 : void
28 0 : WIB::SetDAQ_SI5342Page(uint8_t page)
29 : {
30 0 : WriteDAQ_SI5342(0x1, page, 1);
31 0 : }
32 :
33 : uint8_t
34 0 : WIB::GetDAQ_SI5342Page()
35 : {
36 0 : return uint8_t(ReadDAQ_SI5342(0x1, 1) & 0xFF);
37 : }
38 :
39 : uint8_t
40 0 : WIB::GetDAQ_SI5342AddressPage(uint16_t address)
41 : {
42 0 : return uint8_t((address >> 8) & 0xFF);
43 : }
44 :
45 : void
46 0 : WIB::LoadConfigDAQ_SI5342(std::string const& fileName)
47 : {
48 0 : std::ifstream confFile(fileName.c_str());
49 0 : BUException::WIB_BAD_ARGS badFile;
50 :
51 0 : if (confFile.fail()) {
52 : // Failed to topen filename, add it to the exception
53 0 : badFile.Append("Bad SI5342 config file name:");
54 0 : badFile.Append(fileName.c_str());
55 :
56 : // Try the default
57 0 : if (getenv("WIBMOD_SHARE") != NULL) {
58 0 : std::string envBasedFileName = getenv("WIBMOD_SHARE");
59 0 : envBasedFileName += "/config/WIB1/config/";
60 0 : envBasedFileName += SI5342_CONFIG_FILENAME;
61 0 : confFile.open(envBasedFileName.c_str());
62 0 : if (confFile.fail()) {
63 0 : badFile.Append("Bad env based filename:");
64 0 : badFile.Append(envBasedFileName.c_str());
65 : }
66 0 : }
67 : }
68 :
69 0 : if (confFile.fail()) {
70 : // We are still failing to open our file
71 0 : throw badFile;
72 : }
73 :
74 : // Make sure the chip isn't in reset
75 0 : if (Read("DAQ.SI5342.RESET") != 0) {
76 0 : Write("DAQ.SI5342.RESET", 0x0);
77 0 : usleep(50000);
78 : }
79 :
80 : // Reset the I2C firmware
81 0 : Write("DAQ.SI5342.I2C.RESET", 1);
82 :
83 0 : std::vector<std::pair<uint16_t, uint8_t>> writes;
84 0 : while (!confFile.eof()) {
85 0 : std::string line;
86 0 : std::getline(confFile, line);
87 0 : if (line.size() == 0) {
88 0 : continue;
89 0 : } else if (line[0] == '#') {
90 0 : continue;
91 0 : } else if (line[0] == 'A') {
92 0 : continue;
93 : } else {
94 0 : if (line.find(',') == std::string::npos) {
95 0 : printf("Skipping bad line: \"%s\"\n", line.c_str());
96 0 : continue;
97 : }
98 0 : uint16_t address = strtoul(line.substr(0, line.find(',')).c_str(), NULL, 16);
99 0 : uint8_t data = strtoul(line.substr(line.find(',') + 1).c_str(), NULL, 16);
100 0 : writes.push_back(std::pair<uint16_t, uint8_t>(address, data));
101 : }
102 0 : }
103 :
104 : // Disable the SI5342 output
105 0 : Write("DAQ.SI5342.ENABLE", 0x0);
106 :
107 0 : uint8_t page = GetDAQ_SI5342Page();
108 0 : unsigned int percentDone = 0;
109 :
110 0 : printf("\n[==================================================]\n");
111 0 : fprintf(stderr, " ");
112 0 : for (size_t iWrite = 0; iWrite < writes.size(); iWrite++) {
113 :
114 0 : if (page != GetDAQ_SI5342AddressPage(writes[iWrite].first)) {
115 0 : page = GetDAQ_SI5342AddressPage(writes[iWrite].first);
116 0 : SetDAQ_SI5342Page(page);
117 0 : usleep(100000);
118 : }
119 :
120 0 : if (iWrite == 3) {
121 0 : usleep(300000);
122 : }
123 :
124 0 : uint8_t address = writes[iWrite].first & 0xFF;
125 0 : uint32_t data = (writes[iWrite].second) & 0xFF;
126 0 : uint8_t iData = 1;
127 :
128 0 : for (size_t iTries = 10; iTries > 0; iTries--) {
129 0 : try {
130 0 : WriteDAQ_SI5342(address, data, iData);
131 0 : } catch (BUException::WIB_ERROR& e) {
132 : // Reset the I2C firmware
133 0 : Write("DAQ.SI5342.I2C.RESET", 1);
134 0 : if (iTries == 1) {
135 0 : e.Append("\nTried 3 times\n");
136 0 : throw;
137 : }
138 0 : }
139 : }
140 0 : if ((100 * iWrite) / writes.size() > percentDone) {
141 0 : fprintf(stderr, "#");
142 0 : percentDone += 2;
143 : }
144 : }
145 0 : printf("\n");
146 0 : }
147 :
148 : void
149 0 : WIB::SelectSI5342(uint64_t input, bool enable)
150 : {
151 0 : Write("DAQ.SI5342.INPUT_SELECT", input);
152 0 : Write("DAQ.SI5342.ENABLE", uint64_t(enable));
153 0 : }
|