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