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