Subversion Repositories f9daq

Rev

Rev 5 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. #include <stdio.h>
  2. #include <vector>
  3. #include "VmUsbStack.h"
  4.  
  5. VmUsbStack::VmUsbStack(){
  6.         fStack.push_back(0x1);
  7.         fStack.push_back(0x0);
  8. }
  9.  
  10. VmUsbStack::~VmUsbStack(){
  11.         fStack.clear();
  12. }
  13.  
  14. int VmUsbStack::Get(int maxn, int *stack){
  15.   for (int i=0;i<fStack.size();i++){
  16.                         if (i<maxn) stack[i]=fStack[i]; else return -1;
  17.   }
  18.   return fStack[0];
  19. }
  20.  
  21.  
  22. void VmUsbStack::Append(int cmd, int addr ){
  23.   fStack.push_back(cmd & 0xFFFF);
  24.   fStack.push_back(cmd >> 16);
  25.   fStack.push_back(addr&0xFFFF);
  26.   fStack.push_back((addr >> 16)& 0xFFFF);
  27.   fStack[0]=fStack.size()-1;
  28. }
  29.  
  30. void VmUsbStack::Append(int cmd, int addr , int data){
  31.   Append( cmd, addr );
  32.   fStack.push_back(data&0xFFFF);
  33.   fStack.push_back((data >> 16)&0xFFFF);
  34.   fStack[0] = fStack.size()-1;
  35. }
  36.  
  37. void VmUsbStack::ReadA24D16(int addr ){
  38.         Append(CMD_READ | CMD_A24 , CMD_D16 | addr);
  39. }
  40.  
  41. void VmUsbStack::WriteA24D16(int addr, int data){
  42.   return Append(CMD_WRITE | CMD_A24,CMD_D16 | addr, data);
  43. }
  44.  
  45. void VmUsbStack::ReadA24D32(int addr ){
  46.         Append(CMD_READ | CMD_A24 , CMD_D32 | addr);
  47. }
  48.  
  49. void VmUsbStack::WriteA24D32(int addr, int data){
  50.   return Append(CMD_WRITE | CMD_A24 , CMD_D32 | addr, data);
  51. }
  52.  
  53. void VmUsbStack::ReadA32D16(int addr ){
  54.         Append(CMD_READ | CMD_A32 , CMD_D16| addr);
  55. }
  56.  
  57. void VmUsbStack::WriteA32D16(int addr, int data){
  58.   return Append(CMD_WRITE | CMD_A32 , CMD_D16 | addr, data);
  59. }
  60.  
  61. void VmUsbStack::ReadA32D32(int addr ){
  62.         Append(CMD_READ | CMD_A32 , CMD_D32 | addr);
  63. }
  64.  
  65. void VmUsbStack::WriteA32D32(int addr, int data){
  66.   return Append(CMD_WRITE | CMD_A32 , CMD_D32| addr, data);
  67. }
  68.  
  69.  
  70. void VmUsbStack::Marker(int marker){
  71.   return Append(CMD_MRK, marker);
  72. }
  73.  
  74. void VmUsbStack::ConditionalRead(int adr_mod,int d16d32, int addr, int bmask){
  75.         Append(CMD_READ| CMD_HD | adr_mod ,addr);
  76.         Append(CMD_READ| CMD_HD | CMD_HM |adr_mod, d16d32 |addr,bmask);
  77. }
  78.  
  79. void VmUsbStack::RepeatRead(int adr_mod,int d16d32, int baseaddr, int n, int increment){
  80.         for (int i=0;i<n;i++) Append( adr_mod | CMD_READ, d16d32 +baseaddr + i*increment);
  81. }
  82. void VmUsbStack::RepeatWrite(int adr_mod,int d16d32,int baseaddr, int n, int increment, int data){
  83.         for (int i=0;i<n;i++)  Append( adr_mod | CMD_WRITE,d16d32 + baseaddr+i*increment, data);
  84. }
  85.  
  86. void VmUsbStack::Print(){
  87.         for (int i=0; i < fStack.size();i++) printf("0x%04x\n",fStack[i]);
  88.    printf("%d %d __________________________________\n",fStack.size(),fStack[0]);
  89. }
  90.  
  91. #ifdef MAIN
  92. #include "CAENV965_DEF.h"
  93. #define CAEN_V965    0x340000
  94.  
  95. int main(){
  96. // INIT stackdata
  97. int geo=1,fPedestal=255;
  98. VmUsbStack *init=new VmUsbStack();
  99. init->Write(CAEN_V965 + CAENV965_CRN, 0x0);
  100. init->Write(CAEN_V965 + CAENV965_GEO, geo);
  101. init->RepeatWrite( CAEN_V965 + CAENV965_THM, 32, 0x02,0x0); // threshold/kill for 32 channels, 2*i addr increment
  102. init->Write( CAEN_V965 + CAENV965_BS1, 0x80 ); // soft reset
  103. init->Write( CAEN_V965 + CAENV965_BC1, 0x80 ); // soft reset
  104. init->Write( CAEN_V965 + CAENV965_PED, fPedestal ); // pedestal
  105. init->Write(CAEN_V965 + CAENV965_BS2,0x5000);
  106. init->Write(CAEN_V965 + CAENV965_BS2,0x4);  // clear module
  107. init->Write(CAEN_V965 + CAENV965_BC2,0x4);
  108. init->Print();
  109.  
  110. // READOUT stackdata
  111. VmUsbStack *stack=new VmUsbStack();
  112. stack->Write(CAEN_V965 + CAENV965_BS2,0x4);  // clear module
  113. stack->Write(CAEN_V965 + CAENV965_BC2,0x4);  
  114. stack->ConditionalRead(CAEN_V965 + CAENV965_SR1,0x1);        // TRG wait : loop until bit 0 is on
  115. stack->ConditionalRead(CAEN_V965 + CAENV965_OB ,0x4000000) ; // loop until bit 26 is on, read data
  116. stack->Marker(0xFAFB);
  117. stack->Print();
  118.  
  119.  int st[1000];
  120.  int nb= stack->Get(1000,st);
  121.  for (int i=0;i<nb;i++) if (st[i]!=stack->fStack[i]) printf("error i=%d 0x%04x 0x%04x \n",i,st[i],stack->fStack[i] );
  122.  return 0;
  123. }
  124. #endif
  125.