- Timestamp:
- 07/13/11 15:00:42 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/fadctrl.cc
r11356 r11377 49 49 uint64_t fCounter; 50 50 51 FAD::EventHeader fBufEventHeader; 52 vector<uint16_t> fTargetRoi; 53 51 54 protected: 52 55 void PrintEventHeader() … … 279 282 void ConnectionEstablished() 280 283 { 284 fBufEventHeader.clear(); 285 fBufEventHeader.fEventCounter = 1; 286 fBufEventHeader.fStatus = 0xf000| 287 FAD::EventHeader::kDenable| 288 FAD::EventHeader::kDwrite| 289 FAD::EventHeader::kDcmLocked| 290 FAD::EventHeader::kDcmReady| 291 FAD::EventHeader::kSpiSclk; 292 281 293 fEventHeader.clear(); 282 294 for (unsigned int i=0; i<FAD::kNumChannels; i++) … … 348 360 Connection(ioservice, imp()), fSlot(slot), 349 361 fIsVerbose(false), fIsHexOutput(false), fIsDataOutput(false), 350 fBlockTransmission(false), fCounter(0) 362 fBlockTransmission(false), fCounter(0), 363 fTargetRoi(FAD::kNumChannels) 351 364 { 352 365 // Maximum possible needed space: … … 358 371 } 359 372 360 // void SetTcpEndpoint(const tcp::endpoint &ep) { fEndpoint = ep; }361 // const tcp::endpoint &GetTcpEndpoint() const { return fEndpoint; }362 363 373 void Cmd(FAD::Enable cmd, bool on=true) 364 374 { 375 switch (cmd) 376 { 377 case FAD::kCmdDrsEnable: fBufEventHeader.Enable(FAD::EventHeader::kDenable, on); break; 378 case FAD::kCmdDwrite: fBufEventHeader.Enable(FAD::EventHeader::kDwrite, on); break; 379 case FAD::kCmdTriggerLine: fBufEventHeader.Enable(FAD::EventHeader::kTriggerLine, on); break; 380 case FAD::kCmdBusy: fBufEventHeader.Enable(FAD::EventHeader::kBusy, on); break; 381 case FAD::kCmdContTrigger: fBufEventHeader.Enable(FAD::EventHeader::kContTrigger, on); break; 382 case FAD::kCmdSocket: fBufEventHeader.Enable(FAD::EventHeader::kSock17, !on); break; 383 default: 384 break; 385 } 386 365 387 PostCmd(cmd + (on ? 0 : 0x100)); 366 388 } … … 382 404 return false; 383 405 406 fBufEventHeader.fTriggerGeneratorPrescaler = val; 384 407 PostCmd(FAD::kCmdWriteRate, val);//uint8_t(1000./val/12.5)); 385 408 //PostCmd(FAD::kCmdWriteExecute); … … 390 413 void CmdSetRunNumber(uint32_t num) 391 414 { 415 fBufEventHeader.fRunNumber = num; 416 392 417 PostCmd(FAD::kCmdWriteRunNumberLSW, num&0xffff); 393 418 PostCmd(FAD::kCmdWriteRunNumberMSW, num>>16); … … 403 428 } 404 429 405 bool CmdSetDacValue(uint8_t addr, uint16_t val) 406 { 407 if (addr>FAD::kMaxDacAddr) // NDAC 430 bool CmdSetDacValue(int8_t addr, uint16_t val) 431 { 432 if (addr<0) 433 { 434 for (unsigned int i=0; i<=FAD::kMaxDacAddr; i++) 435 { 436 fBufEventHeader.fDac[i] = val; 437 PostCmd(FAD::kCmdWriteDac + i, val); 438 } 439 PostCmd(FAD::kCmdWriteExecute); 440 return true; 441 } 442 443 if (uint8_t(addr)>FAD::kMaxDacAddr) // NDAC 408 444 return false; 445 446 fBufEventHeader.fDac[addr] = val; 409 447 410 448 PostCmd(FAD::kCmdWriteDac + addr, val); … … 421 459 { 422 460 for (unsigned int i=0; i<=FAD::kMaxRoiAddr; i++) 461 { 462 fTargetRoi[i] = val; 423 463 PostCmd(FAD::kCmdWriteRoi + i, val); 464 } 424 465 PostCmd(FAD::kCmdWriteExecute); 425 466 return true; … … 429 470 return false; 430 471 472 fTargetRoi[addr] = val; 473 431 474 PostCmd(FAD::kCmdWriteRoi + addr, val); 432 475 PostCmd(FAD::kCmdWriteExecute); 433 476 return true; 434 477 } … … 536 579 } 537 580 581 bool IsConfigured() const 582 { 583 bool identical = true; 584 for (int i=0; i<FAD::kNumChannels; i++) 585 if (fTargetRoi[i]!=fChannelHeader[i].fRegionOfInterest) 586 { 587 identical = false; 588 break; 589 } 590 591 return fEventHeader==fBufEventHeader && identical; 592 } 538 593 }; 539 594 … … 687 742 return T::kSM_FatalError; 688 743 689 const uint32_t *dat = evt.Ptr<uint32_t>(); 690 691 if (!Check(dat, FAD::kMaxDacAddr, FAD::kMaxDacValue)) 692 return T::GetCurrentState(); 744 const int32_t *dat = evt.Ptr<int32_t>(); 693 745 694 746 for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++) 695 i->second->CmdSetDacValue(dat[0], dat[1]); 747 if (!i->second->CmdSetDacValue(dat[0], dat[1])) 748 { 749 ostringstream msg; 750 msg << hex << "Channel " << dat[0] << " or Value " << dat[1] << " out of range."; 751 T::Error(msg); 752 return false; 753 } 696 754 697 755 return T::GetCurrentState(); … … 1011 1069 } 1012 1070 1071 int StartConfigure(const EventImp &evt) 1072 { 1073 const string name = evt.GetText(); 1074 1075 fTargetConfig = fConfigs.find(name); 1076 if (fTargetConfig==fConfigs.end()) 1077 { 1078 T::Error("StartConfigure - Run-type '"+name+"' not found."); 1079 return T::GetCurrentState(); 1080 } 1081 1082 T::Message("Starting configuration for '"+name+"'"); 1083 1084 for (BoardList::iterator it=fBoards.begin(); it!=fBoards.end(); it++) 1085 { 1086 const FAD::Configuration &conf = fTargetConfig->second; 1087 1088 ConnectionFAD &fad = *it->second; 1089 1090 fad.Cmd(FAD::kCmdTriggerLine, false); 1091 fad.Cmd(FAD::kCmdContTrigger, false); 1092 fad.Cmd(FAD::kCmdSocket, true); 1093 fad.Cmd(FAD::kCmdBusy, false); 1094 1095 fad.Cmd(FAD::kCmdDwrite, conf.fDenable); 1096 fad.Cmd(FAD::kCmdDrsEnable, conf.fDwrite); 1097 fad.Cmd(FAD::kCmdContTrigger, conf.fContinousTrigger); 1098 1099 for (int i=0; i<FAD::kNumDac; i++) 1100 fad.CmdSetDacValue(i, conf.fDac[i]); 1101 1102 for (int i=0; i<FAD::kNumChips; i++) 1103 for (int j=0; j<FAD::kNumChannelsPerChip; j++) 1104 fad.CmdSetRoi(i*FAD::kNumChannelsPerChip+j, conf.fRoi[j]); 1105 1106 fad.CmdSetTriggerRate(conf.fTriggerRate); 1107 fad.CmdSetRunNumber(IncreaseRunNumber()); 1108 fad.Cmd(FAD::kCmdResetEventCounter); 1109 fad.Cmd(FAD::kCmdSingleTrigger); 1110 } 1111 1112 return FAD::kConfiguring; 1113 } 1114 1115 int ResetConfig() 1116 { 1117 return FAD::kConnected; 1118 } 1119 1013 1120 int AddAddress(const EventImp &evt) 1014 1121 { … … 1086 1193 str << " [data_ignored]"; 1087 1194 1195 if (fStatusC[idx]) 1196 str << " [configured]"; 1197 1088 1198 T::Out() << str.str() << endl; 1089 1199 } … … 1202 1312 vector<uint8_t> fStatus1; 1203 1313 vector<uint8_t> fStatus2; 1314 vector<uint8_t> fStatusC; 1204 1315 bool fStatusT; 1205 1316 … … 1220 1331 uint16_t nconnected1 = 0; 1221 1332 uint16_t nconnected2 = 0; 1333 uint16_t nconfigured = 0; 1222 1334 1223 1335 vector<uint8_t> stat1(40); 1224 1336 vector<uint8_t> stat2(40); 1337 vector<bool> statC(40); 1225 1338 1226 1339 int cnt = 0; // counter for enabled board … … 1251 1364 stat1[idx] = 2; 1252 1365 nconnected1++; 1366 1367 if (c->IsConfigured()) 1368 { 1369 statC[idx] = 1; 1370 nconfigured++; 1371 } 1253 1372 } 1254 1373 … … 1290 1409 // fadctrl: Always connecting if not disabled 1291 1410 // event builder: 1292 if (nconnecting1==0 && nconnected1>0 && 1293 nconnected2==nconnected1) 1294 return FAD::kConnected; 1411 if (nconnecting1==0 && nconnected1>0 && nconnected2==nconnected1) 1412 { 1413 if (nconfigured!=nconnected1) 1414 { 1415 if (T::GetCurrentState()==FAD::kConfiguring || 1416 T::GetCurrentState()==FAD::kConfigured) 1417 // Stay in Configured until at least one new 1418 // event has been received 1419 return T::GetCurrentState(); 1420 1421 return FAD::kConnected; 1422 } 1423 1424 if (T::GetCurrentState() != FAD::kConfiguring && 1425 T::GetCurrentState() != FAD::kConfigured) 1426 return FAD::kConnected; 1427 1428 if (T::GetCurrentState()== FAD::kConfiguring) 1429 { 1430 for (BoardList::iterator it=fBoards.begin(); it!=fBoards.end(); it++) 1431 { 1432 //const Configuration &conf = fTargetConfig->second; 1433 1434 ConnectionFAD &fad = *it->second; 1435 1436 fad.Cmd(FAD::kCmdResetEventCounter); 1437 fad.Cmd(FAD::kCmdSocket, false); 1438 fad.Cmd(FAD::kCmdTriggerLine, true); 1439 1440 // FIXME: How do we find out when the FADs 1441 // successfully enabled the trigger lines? 1442 } 1443 } 1444 return FAD::kConfigured; 1445 } 1295 1446 1296 1447 if (nconnecting1>0 || nconnecting2>0 || nconnected1!=nconnected2) … … 1347 1498 StateMachineFAD(ostream &out=cout) : 1348 1499 T(out, "FAD_CONTROL"), EventBuilderWrapper(*static_cast<MessageImp*>(this)), ba::io_service::work(static_cast<ba::io_service&>(*this)), 1349 fStatus1(40), fStatus2(40), fStatus T(false),1500 fStatus1(40), fStatus2(40), fStatusC(40), fStatusT(false), 1350 1501 fDimConnection("FAD_CONTROL/CONNECTIONS", "C:40;C:1", "") 1351 1502 { … … 1369 1520 T::AddStateName(FAD::kConnected, "Connected", 1370 1521 "All enabled FAD boards are connected.."); 1522 1523 T::AddStateName(FAD::kConfiguring, "Configuring", 1524 "."); 1525 1526 T::AddStateName(FAD::kConfigured, "Configured", 1527 "The last header received through the command socket fits the requested configureation and has EventCounter==0."); 1371 1528 1372 1529 // FAD Commands … … 1425 1582 ("Adjust ADC phase (in 'steps')"); 1426 1583 1427 T::AddEvent("RESET_ TRIGGER_ID")1428 (boost::bind(&StateMachineFAD::Cmd, this, FAD::kCmdReset TriggerId))1584 T::AddEvent("RESET_EVENT_COUNTER") 1585 (boost::bind(&StateMachineFAD::Cmd, this, FAD::kCmdResetEventCounter)) 1429 1586 (""); 1430 1587 … … 1455 1612 (boost::bind(&StateMachineFAD::SetDac, this, _1)) 1456 1613 ("Set DAC numbers in range to value" 1457 "|addr[short]:Address of register "1614 "|addr[short]:Address of register (-1 for all)" 1458 1615 "|val[short]:Value to be set"); 1616 1617 T::AddEvent("CONFIGURE", "C", FAD::kConnected) 1618 (boost::bind(&StateMachineFAD::StartConfigure, this, _1)) 1619 (""); 1620 1621 T::AddEvent("RESET_CONFIGURE", FAD::kConfiguring) 1622 (boost::bind(&StateMachineFAD::ResetConfig, this)) 1623 (""); 1459 1624 1460 1625 // Verbosity commands … … 1618 1783 } 1619 1784 1785 typedef map<string, FAD::Configuration> Configs; 1786 Configs fConfigs; 1787 Configs::const_iterator fTargetConfig; 1788 1789 1790 template<class V> 1791 bool CheckConfigVal(const Configuration &conf, V max, const string &name, const string &sub) 1792 { 1793 if (!conf.HasDef(name, sub)) 1794 { 1795 T::Error("Neither "+name+"default nor "+name+sub+" found."); 1796 return false; 1797 } 1798 1799 const V val = conf.GetDef<V>(name, sub); 1800 1801 if (val<=max) 1802 return true; 1803 1804 ostringstream str; 1805 str << name << sub << "=" << val << " exceeds allowed maximum of " << max << "!"; 1806 T::Error(str); 1807 1808 return false; 1809 } 1810 1620 1811 int EvalConfiguration(const Configuration &conf) 1621 1812 { 1813 // ---------- General setup --------- 1622 1814 fIsVerbose = !conf.Get<bool>("quiet"); 1623 1815 fIsHexOutput = conf.Get<bool>("hex-out"); … … 1625 1817 fDebugTx = conf.Get<bool>("debug-tx"); 1626 1818 1819 // ---------- Setup event builder --------- 1627 1820 SetMaxMemory(conf.Get<unsigned int>("max-mem")); 1628 1821 1629 // vvvvv for debugging vvvvv 1822 // ---------- Setup run types --------- 1823 const vector<string> types = conf.Vec<string>("run-type"); 1824 if (types.size()==0) 1825 T::Warn("No run-types defined."); 1826 else 1827 T::Message("Defining run-types"); 1828 for (vector<string>::const_iterator it=types.begin(); 1829 it!=types.end(); it++) 1830 { 1831 T::Message(" -> "+ *it); 1832 1833 if (fConfigs.count(*it)>0) 1834 { 1835 T::Error("Run-type "+*it+" defined twice."); 1836 return 1; 1837 } 1838 1839 FAD::Configuration target; 1840 1841 if (!CheckConfigVal<bool>(conf, true, "enable-drs.", *it) || 1842 !CheckConfigVal<bool>(conf, true, "enable-dwrite.", *it) || 1843 !CheckConfigVal<bool>(conf, true, "enable-continous-trigger.", *it)) 1844 return 2; 1845 1846 target.fDenable = conf.GetDef<bool>("enable-drs.", *it); 1847 target.fDwrite = conf.GetDef<bool>("enable-dwrite.", *it); 1848 target.fContinousTrigger = conf.GetDef<bool>("enable-continous-trigger.", *it); 1849 1850 target.fTriggerRate = 0; 1851 if (target.fContinousTrigger) 1852 { 1853 if (!CheckConfigVal<uint16_t>(conf, 0xffff, "trigger-rate.", *it)) 1854 return 3; 1855 1856 target.fTriggerRate = conf.GetDef<uint16_t>("trigger-rate.", *it); 1857 } 1858 1859 for (int i=0; i<FAD::kNumChannelsPerChip; i++) 1860 { 1861 ostringstream str; 1862 str << "roi-ch" << i << '.'; 1863 1864 if (!CheckConfigVal<uint16_t>(conf, FAD::kMaxRoiValue, "roi.", *it) && 1865 !CheckConfigVal<uint16_t>(conf, FAD::kMaxRoiValue, str.str(), *it)) 1866 return 4; 1867 1868 target.fRoi[i] = conf.HasDef(str.str(), *it) ? 1869 conf.GetDef<uint16_t>(str.str(), *it) : 1870 conf.GetDef<uint16_t>("roi.", *it); 1871 } 1872 1873 for (int i=0; i<FAD::kNumDac; i++) 1874 { 1875 ostringstream str; 1876 str << "dac-" << i << '.'; 1877 1878 if (!CheckConfigVal<uint16_t>(conf, FAD::kMaxDacValue, "dac.", *it) && 1879 !CheckConfigVal<uint16_t>(conf, FAD::kMaxDacValue, str.str(), *it)) 1880 return 5; 1881 1882 target.fDac[i] = conf.HasDef(str.str(), *it) ? 1883 conf.GetDef<uint16_t>(str.str(), *it) : 1884 conf.GetDef<uint16_t>("dac.", *it); 1885 } 1886 1887 fConfigs[*it] = target; 1888 } 1889 1890 // FIXME: Add a check about unsused configurations 1891 1892 // ---------- Setup board addresses for fake-fad --------- 1893 1630 1894 if (conf.Has("debug-addr")) 1631 1895 { … … 1643 1907 return -1; 1644 1908 } 1645 // ^^^^^ for debugging ^^^^^ 1909 1910 // ---------- Setup board addresses for the real camera --------- 1646 1911 1647 1912 if (conf.Has("base-addr")) … … 1654 1919 const tcp::endpoint endpoint = GetEndpoint(base); 1655 1920 if (endpoint==tcp::endpoint()) 1656 return 1 ;1921 return 10; 1657 1922 1658 1923 const ba::ip::address_v4::bytes_type ip = endpoint.address().to_v4().to_bytes(); … … 1661 1926 { 1662 1927 T::Out() << kRed << "EvalConfiguration - IP address given by --base-addr out-of-range." << endl; 1663 return 3;1928 return 11; 1664 1929 } 1665 1930 … … 1679 1944 } 1680 1945 1946 // ---------- Setup board addresses one by one --------- 1947 1681 1948 if (conf.Has("addr")) 1682 1949 { … … 1686 1953 const tcp::endpoint endpoint = GetEndpoint(*i); 1687 1954 if (endpoint==tcp::endpoint()) 1688 return 1 ;1955 return 12; 1689 1956 1690 1957 AddEndpoint(endpoint); … … 1812 2079 ; 1813 2080 1814 po::options_description builder("Event builder options");1815 builder.add_options()1816 ("max-mem,m", var<unsigned int>(100), "Maximum memory the event builder thread is allowed to consume for its event buffer")1817 ;1818 1819 2081 po::options_description connect("FAD connection options"); 1820 2082 connect.add_options() … … 1825 2087 ; 1826 2088 2089 po::options_description builder("Event builder options"); 2090 builder.add_options() 2091 ("max-mem,m", var<unsigned int>(100), "Maximum memory the event builder thread is allowed to consume for its event buffer") 2092 ; 2093 2094 2095 po::options_description runtype("Run type configuration"); 2096 runtype.add_options() 2097 ("run-type", vars<string>(), "") 2098 ("enable-dwrite.*", var<bool>(), "") 2099 ("enable-drs.*", var<bool>(), "") 2100 ("enable-continous-trigger.*", var<bool>(), "") 2101 ("trigger-rate.*", var<uint16_t>(), "") 2102 ("dac.*", var<uint16_t>(), "") 2103 ("dac-0.*", var<uint16_t>(), "") 2104 ("dac-1.*", var<uint16_t>(), "") 2105 ("dac-2.*", var<uint16_t>(), "") 2106 ("dac-3.*", var<uint16_t>(), "") 2107 ("dac-4.*", var<uint16_t>(), "") 2108 ("dac-5.*", var<uint16_t>(), "") 2109 ("dac-6.*", var<uint16_t>(), "") 2110 ("dac-7.*", var<uint16_t>(), "") 2111 ("roi.*", var<uint16_t>(), "") 2112 ("roi-ch0.*", var<uint16_t>(), "") 2113 ("roi-ch1.*", var<uint16_t>(), "") 2114 ("roi-ch2.*", var<uint16_t>(), "") 2115 ("roi-ch3.*", var<uint16_t>(), "") 2116 ("roi-ch4.*", var<uint16_t>(), "") 2117 ("roi-ch5.*", var<uint16_t>(), "") 2118 ("roi-ch6.*", var<uint16_t>(), "") 2119 ("roi-ch7.*", var<uint16_t>(), "") 2120 ("roi-ch8.*", var<uint16_t>(), "") 2121 ; 2122 1827 2123 conf.AddEnv("dns", "DIM_DNS_NODE"); 1828 2124 1829 2125 conf.AddOptions(config); 1830 2126 conf.AddOptions(control); 2127 conf.AddOptions(connect); 1831 2128 conf.AddOptions(builder); 1832 conf.AddOptions( connect);2129 conf.AddOptions(runtype); 1833 2130 } 1834 2131
Note:
See TracChangeset
for help on using the changeset viewer.