Subversion Repositories f9daq

Rev

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

  1. #include <windows.h>
  2. #include <userint.h>
  3. #include <utility.h>
  4. #include <formatio.h>
  5. #include <ansi_c.h>
  6. #include "usb.h"
  7. #include "xxusbdll.h"
  8.  
  9. #include <cvirte.h>    /* Needed if linking in external compiler; harmless otherwise */
  10. #include "uklon_ui.h"
  11.  
  12.  
  13. #define NDIS 21
  14. #define NSCA 22
  15. #define NTGG 23
  16.  
  17. int nc[]={P1_NC0, P1_NC1, P1_NC2, P1_NC3,
  18.           P1_NC4, P1_NC5, P1_NC6, P1_NC7,
  19.           P1_NC8, P1_NC9, P1_NC10,P1_NC11,
  20.           P1_NC12,P1_NC13,P1_NC14,P1_NC15 } ;
  21.  
  22.  
  23. #define C_I CAMAC_I
  24. #define C_Z CAMAC_Z
  25. #define C_C CAMAC_C
  26. //#define C_write        WUSBXX_CAMAC_write
  27. //#define C_read             WUSBXX_CAMAC_read
  28. #define C_write          CAMAC_write
  29. #define C_read       CAMAC_read
  30.  
  31. //********************************************************************
  32. usb_dev_handle *udev;
  33.  int Q;
  34.  int X;
  35. static HINSTANCE DLLHandle;
  36.  
  37. int  WUSBXX_load (char* module_path)
  38. {
  39.  
  40.  
  41.   if (module_path == NULL)
  42.     DLLHandle = LoadLibrary("libxxusb.dll");
  43.    else
  44.     DLLHandle = LoadLibrary(module_path);
  45.    
  46.   if (!(xxusb_register_read_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_register_read"))) return __LINE__;
  47.   if (!(xxusb_stack_read_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_stack_read"))) return __LINE__;
  48.   if (!(xxusb_stack_write_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_stack_write"))) return __LINE__;
  49.   if (!(xxusb_stack_execute_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_stack_execute"))) return __LINE__;
  50.   if (!(xxusb_register_write_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_register_write"))) return __LINE__;
  51.   if (!(xxusb_usbfifo_read_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_usbfifo_read"))) return __LINE__;
  52.   if (!(xxusb_bulk_read_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_bulk_read"))) return __LINE__;
  53.   if (!(xxusb_bulk_write_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_bulk_write"))) return __LINE__;
  54.   if (!(xxusb_reset_toggle_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_reset_toggle"))) return __LINE__;
  55.  
  56.   if (!(xxusb_devices_find_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_devices_find"))) return __LINE__;
  57.   if (!(xxusb_device_close_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_device_close"))) return __LINE__;
  58.   if (!(xxusb_device_open_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_device_open"))) return __LINE__;
  59.   if (!(xxusb_flash_program_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_flash_program"))) return __LINE__;
  60.   if (!(xxusb_flashblock_program_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_flashblock_program"))) return __LINE__;
  61.   if (!(xxusb_serial_open_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_serial_open"))) return __LINE__;
  62.  
  63.   if (!(VME_register_write_Ptr = (void*) GetProcAddress(DLLHandle,"VME_register_write"))) return __LINE__;
  64.   if (!(VME_register_read_Ptr = (void*) GetProcAddress(DLLHandle,"VME_register_read"))) return __LINE__;
  65.   if (!(VME_LED_settings_Ptr = (void*) GetProcAddress(DLLHandle,"VME_LED_settings"))) return __LINE__;
  66.   if (!(VME_DGG_Ptr = (void*) GetProcAddress(DLLHandle,"VME_DGG"))) return __LINE__;
  67.   if (!(VME_Output_settings_Ptr = (void*) GetProcAddress(DLLHandle,"VME_Output_settings"))) return __LINE__;
  68.   if (!(VME_read_16_Ptr = (void*) GetProcAddress(DLLHandle,"VME_read_16"))) return __LINE__;
  69.   if (!(VME_read_32_Ptr = (void*) GetProcAddress(DLLHandle,"VME_read_32"))) return __LINE__;
  70.   if (!(VME_BLT_read_32_Ptr = (void*) GetProcAddress(DLLHandle,"VME_BLT_read_32"))) return __LINE__;
  71.   if (!(VME_write_16_Ptr = (void*) GetProcAddress(DLLHandle,"VME_write_16"))) return __LINE__;
  72.   if (!(VME_write_32_Ptr = (void*) GetProcAddress(DLLHandle,"VME_write_32"))) return __LINE__;
  73.  
  74.   if (!(CAMAC_DGG_Ptr = (void*) GetProcAddress(DLLHandle,"CAMAC_DGG"))) return __LINE__;
  75.   if (!(CAMAC_register_read_Ptr = (void*) GetProcAddress(DLLHandle,"CAMAC_register_read"))) return __LINE__;
  76.   if (!(CAMAC_register_write_Ptr = (void*) GetProcAddress(DLLHandle,"CAMAC_register_write"))) return __LINE__;
  77.   if (!(CAMAC_LED_settings_Ptr = (void*) GetProcAddress(DLLHandle,"CAMAC_LED_settings"))) return __LINE__;
  78.   if (!(CAMAC_Output_settings_Ptr = (void*) GetProcAddress(DLLHandle,"CAMAC_Output_settings"))) return __LINE__;
  79.   if (!(CAMAC_read_LAM_mask_Ptr = (void*) GetProcAddress(DLLHandle,"CAMAC_read_LAM_mask"))) return __LINE__;
  80.   if (!(CAMAC_write_LAM_mask_Ptr = (void*) GetProcAddress(DLLHandle,"CAMAC_write_LAM_mask"))) return __LINE__;
  81.   if (!(CAMAC_write_Ptr = (void*) GetProcAddress(DLLHandle,"CAMAC_write"))) return __LINE__;
  82.   if (!(CAMAC_read_Ptr = (void*) GetProcAddress(DLLHandle,"CAMAC_read"))) return __LINE__;
  83.   if (!(CAMAC_Z_Ptr = (void*) GetProcAddress(DLLHandle,"CAMAC_Z"))) return __LINE__;
  84.   if (!(CAMAC_C_Ptr = (void*) GetProcAddress(DLLHandle,"CAMAC_C"))) return __LINE__;
  85.   if (!(CAMAC_I_Ptr = (void*) GetProcAddress(DLLHandle,"CAMAC_I"))) return __LINE__;
  86.   return 0;
  87. }
  88.  
  89.  
  90. void  WUSBXX_open (char *serial)
  91. {
  92.   if (serial != NULL)
  93.         udev = xxusb_serial_open(serial);
  94.   if (!udev)
  95.     MessagePopup ("Error", "Cannot connect to USB. ! Check the connection!");
  96. }
  97.  
  98. void  WUSBXX_close (void)
  99. {
  100.   if (udev) xxusb_device_close(udev);
  101. }
  102.  
  103. int WUSBXX_CAMAC_write( usb_dev_handle *udev, int N, int A, int F, long Data, int *Q, int *X);
  104. int WUSBXX_CAMAC_read( usb_dev_handle *udev,int N, int A, int F, long *Data, int *Q, int *X);
  105. int WUSBXX_CAMAC_Z( usb_dev_handle *udev );
  106. int WUSBXX_CAMAC_C( usb_dev_handle *udev );
  107. int WUSBXX_CAMAC_I(usb_dev_handle *udev, int inhibit);
  108.  
  109. int WUSBXX_CAMAC_write( usb_dev_handle *udev,int N, int A, int F, long Data, int *Q, int *X)
  110. {
  111.     long intbuf[4];  
  112.     int ret;
  113. // CAMAC direct write function
  114.     intbuf[0]=1;
  115.     intbuf[1]=(long)(F+A*32+N*512 + 0x4000);
  116.     if ((F > 15) && (F < 24))  // orig controla
  117.     //if ((F > 15) && (F < 28))  // orig controla
  118.     {  
  119.         intbuf[0]=3;
  120.         intbuf[2]=(Data & 0xffff);
  121.         intbuf[3]=((Data >>16) & 255);
  122.         ret = xxusb_stack_execute(udev, intbuf);
  123.         *Q = (intbuf[0] & 1);
  124.         *X = ((intbuf[0] >> 1) & 1);
  125.     } else {
  126.       printf("Error N%d A%d F%d",N,A,F);
  127.     }
  128.     return ret;
  129. }
  130.  
  131. int WUSBXX_CAMAC_read( usb_dev_handle *udev,int N, int A, int F, long *Data, int *Q, int *X)
  132. {
  133.     long intbuf[4];  
  134.     int ret;
  135.     // CAMAC direct read function
  136.     intbuf[0]=1;
  137.     intbuf[1]=(long)(F+A*32+N*512 + 0x4000);
  138. //    intbuf[1]=(long)(F+A*32+N*512);
  139.     ret = xxusb_stack_execute(udev, intbuf);
  140. //    printf ("ret = %d\n",ret);
  141.     if (F < 16)  // orig controla
  142.     {
  143. //      *Data = intbuf[0];   //16-bit word
  144.         *Data = intbuf[0] + (intbuf[1] & 0xFF) * 0x10000;   //24-bit word
  145.         *Q = ((intbuf[1] >> 8) & 1);
  146.         *X = ((intbuf[1] >> 9) & 1);
  147.     }
  148. //    else {
  149. //      printf("Error N%d A%d F%d",N,A,F);
  150. //    }
  151.     return ret;
  152. }
  153.  
  154. int WUSBXX_CAMAC_Z(usb_dev_handle *udev)
  155. {
  156.     long intbuf[4];  
  157.     int  ret;
  158. //  CAMAC Z = N(28) A(8) F(29)
  159.     intbuf[0]=1;
  160.     intbuf[1]=(long)(29+8*32+28*512 + 0x4000);
  161.     ret = xxusb_stack_execute(udev, intbuf);
  162.     return ret;
  163. }
  164.  
  165. int WUSBXX_CAMAC_C(usb_dev_handle *udev)
  166. {
  167.     long intbuf[4];  
  168.     int ret;
  169. //  CAMAC C = N(28) A(9) F(29)
  170.     intbuf[0]=1;
  171.     intbuf[1]=(long)(29+9*32+28*512 + 0x4000);
  172.     ret = xxusb_stack_execute( udev, intbuf);
  173.     return ret;
  174. }
  175.  
  176. int WUSBXX_CAMAC_I(usb_dev_handle *udev, int inhibit)
  177. {
  178.     long intbuf[4];  
  179.     int  ret;
  180. //      Set Inhibit             = N(29) A(9) F(24)
  181. //      Clear Inhibit   = N(29) A(9) F(26)
  182.     intbuf[0]=1;
  183.     if (inhibit) intbuf[1]=(long)(24+9*32+29*512 + 0x4000);
  184.     else intbuf[1]=(long)(26+9*32+29*512 + 0x4000);
  185.     ret = xxusb_stack_execute(udev, intbuf);
  186.     return ret;
  187. }
  188.  
  189. int init_CAMAC () {
  190.  
  191.   long dum;
  192.   C_Z(udev);
  193.   C_C(udev);
  194.   C_I(udev,1);
  195.  
  196.   C_read ( udev,NTGG, 0 , 9, &dum,  &Q,&X); /* init TGG */
  197.   C_write ( udev,NTGG, 0, 16, 1000,  &Q,&X); /* Set preset counting value */
  198.   C_write ( udev,NTGG, 0 ,17, 0x2,  &Q,&X); /* Set Load&Clock Modes */
  199.   C_write ( udev,NDIS, 0, 16, 0xffff,  &Q,&X); /* enable all ch. */
  200.   C_read ( udev,NDIS, 0, 26, &dum, &Q, &X);
  201.  
  202.   C_I(udev, 0);  
  203.  
  204.   return 0 ;
  205. }
  206.  
  207. //********************************************************************
  208.  
  209. int plot_graph ( int p1h, int logsc, int data[16] ) {
  210.   int max, max_I2 ;
  211.   double plot_data[16], plot_max ;
  212.   int ch ;
  213.  
  214.   max = 100 ;
  215.   for ( ch=0 ; ch<16 ; ch ++ ) {
  216.     if ( data[ch]>max ) max=data[ch] ;
  217.   }
  218.  
  219.   DeleteGraphPlot (p1h, P1_HISTO, -1, VAL_IMMEDIATE_DRAW);
  220.  
  221.   max = ((max*1.2)/1000)*1000 ;
  222.   GetCtrlVal (p1h, P1_I2, &max_I2 ) ;
  223.   if ( max_I2>0 ) max=max_I2 ;
  224.  
  225.   if ( logsc ) {
  226.                
  227.     for ( ch=0 ; ch<16 ; ch ++ ) {  
  228.           plot_data[ch] = log10((double)data[ch]) ;
  229.           plot_max = log10((double)max) ;
  230.         }
  231.          
  232.         SetAxisScalingMode (p1h, P1_HISTO, VAL_LEFT_YAXIS, VAL_MANUAL, 0.0, plot_max);
  233.         PlotY (p1h, P1_HISTO, plot_data, 16, VAL_DOUBLE, VAL_VERTICAL_BAR,
  234.                    VAL_NO_POINT, VAL_SOLID, 1, VAL_DK_BLUE);
  235.      
  236.  
  237.   } else {
  238.     SetAxisScalingMode (p1h, P1_HISTO, VAL_LEFT_YAXIS, VAL_MANUAL, 0.0, max);
  239.     PlotY (p1h, P1_HISTO, data, 16, VAL_INTEGER, VAL_VERTICAL_BAR,
  240.                    VAL_NO_POINT, VAL_SOLID, 1, VAL_DK_BLUE);
  241.      
  242.     for ( ch=0; ch<16; ch++ ) {
  243.       SetCtrlVal (p1h, nc[ch], data[ch] ) ;
  244.     }
  245.   }
  246.  
  247.  
  248.   return 0 ;
  249. }
  250.  
  251.  
  252. //********************************************************************
  253. //********************************************************************
  254.  
  255. int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  256.                                            LPSTR lpszCmdLine, int nCmdShow)
  257. {
  258.   int rph, rID, p1h, p2h ;
  259.   int data[16], sum, max, max_I2=0 ;
  260.   long val, dum;
  261.   double plot_data[16], splot_data[10][32], plot_max ;
  262.   int thr_set, time_set;
  263.   int ch, i, j,ii;
  264.  
  265.   FILE *fp ;
  266.   char filename[256] ;
  267.   int logscale ;
  268.   int os[10]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1} ;
  269.  
  270.   logscale = 0 ;
  271.  
  272.   if (InitCVIRTE (hInstance, 0, 0) == 0)    /* Needed if linking in external compiler; harmless otherwise */
  273.       return -1;    /* out of memory */
  274.  
  275.   for ( i=0; i<16; data[i++]=0 ) ;
  276.  
  277.  
  278.  
  279.   dum=WUSBXX_load (NULL);
  280.   if (dum) MessagePopup ("Error", "Cannot load dll!");
  281.   WUSBXX_open ("CC0130");                              
  282.  
  283.   //ii=80000000/i-40;
  284.   //if (ii<72) ii=72;
  285.   //CAMAC_DGG(udev,0,7,0,ii,40,0,0);
  286.   //CAMAC_DGG(udev,1,1,1,12,8,0,0);
  287.  
  288.  
  289.   init_CAMAC();  
  290.  
  291.   p1h = LoadPanel (0, "uklon_ui.uir", P1);
  292. //  p2h = LoadPanel (0, "uklon_ui.uir", P2);
  293.   DisplayPanel (p1h);
  294. //  DisplayPanel (p2h);
  295.  
  296.   GetUserEvent (1, &rph, &rID);
  297.  
  298.  
  299.   while ((rID!=P1_QUIT)||(rph!=p1h))
  300.   {
  301.     if ( rph == p1h ) {
  302.                 if ( rID==P1_START ) {
  303.               GetCtrlVal (p1h, P1_THR, &thr_set ) ;   /* read desired thr. */  
  304.           C_write ( udev, NDIS, 0, 17,  thr_set, &Q, &X);   /* set the threshold */
  305.                   C_write ( udev, NDIS, 1, 17, 0,  &Q,&X);         /*                                   */
  306.                
  307.               C_read ( udev, NSCA,0,9,&dum,  &Q,&X);
  308.  
  309.               GetCtrlVal (p1h, P1_TIME, &time_set ) ; /* read time (ms) */  
  310.              
  311.                   SetCtrlAttribute (p1h, P1_TIMER, ATTR_ENABLED, 0);
  312.           C_write ( udev, NTGG, 0, 16, time_set,  &Q,&X);
  313.           C_read ( udev, NTGG, 0, 15, &dum,  &Q,&X);
  314.                                                                                                                                          
  315.           SetCtrlVal (p1h, P1_LEDON,1);  
  316.                   SetCtrlAttribute (p1h, P1_TIMER, ATTR_INTERVAL,time_set*0.001 );
  317.           SetCtrlAttribute (p1h, P1_TIMER, ATTR_ENABLED, 1);
  318.           ResetTimer (p1h, P1_TIMER);
  319.                   Delay (0.1+(time_set/1000.));
  320.                 }
  321.    
  322.         if ( rID==P1_COUNT ) {
  323.                   sum = 0 ;
  324.                   max = 100 ;
  325.                   C_I(udev, 1);
  326.               for ( ch=0 ; ch<16 ; ch ++ ) {
  327.                 Delay(0.01);
  328.                 C_read (  udev, NSCA, ch, 0, &val,  &Q,&X);
  329.                 C_read (  udev, NSCA, ch, 0, &val,  &Q,&X);  
  330.                 // CC USB ne precita v redu CAEN C257 scalerja,
  331.                 // kanali so zamaknjeni za 1, ce je klic samo en
  332.                 // vrednost kanala 0 se pristeje h kanalom 8..16, zato ga ne uporabljamo.
  333.                 // vajo iyvedemo le s kanali 1..15.
  334.                 data[ch]=val;  
  335.                
  336.                 if (data[ch] > 0) data[ch]--;
  337.                //  if (data[ch] && 0x80000000) {
  338.                  //   data[ch] &= 0x00FFFFFF;
  339.                    
  340.             //}
  341.            
  342.             sum += data[ch] ;
  343.           }
  344.                   C_I(udev, 0);
  345.               SetCtrlVal (p1h, P1_I1, sum ) ;
  346.                   plot_graph ( p1h, logscale, data ) ;
  347.                 }
  348.         if ( rID==P1_INIT ) {
  349.                init_CAMAC ( ) ;  
  350.             }
  351.    
  352.             if ( rID==P1_SAVE ) {
  353.               if ( ( fp = fopen ( "scaler.txt", "w+" ) ) == NULL ) printf ( "ERROR" ) ;
  354.               for ( ch=0; ch<16 ; ch++ ) {  
  355.                 fprintf ( fp, "%d\t%d\n", ch, data[ch] ) ;
  356.               }
  357.               fclose ( fp ) ;
  358.             }
  359.  
  360.             if ( rID==P1_APPEND ) {
  361.                   GetCtrlVal (p1h, P1_APFILE, filename);
  362.  
  363.               if ( ( fp = fopen ( filename, "a" ) ) == NULL ) printf ( "ERROR" ) ;
  364.               for ( ch=0; ch<16 ; ch++ ) {  
  365.                 fprintf ( fp, "%d\t", data[ch] ) ;
  366.               }
  367.               fprintf ( fp, "\n" ) ;
  368.               fclose ( fp ) ;
  369.             }
  370.    
  371.                 if ( rID==P1_REPLOT ) {
  372.                   plot_graph ( p1h, logscale, data ) ;
  373.                 }
  374.    
  375.    
  376.             if ( rID == P1_PRINT ) {
  377.               SetCtrlVal (p1h, P1_L1,1);
  378.                   PrintPanel (p1h, "", 1, VAL_VISIBLE_AREA, 1);
  379.               SetCtrlVal (p1h, P1_L1,0);
  380.             }
  381.  
  382.  
  383.                 if ( rID == P1_LOG_SCALE ) {
  384.                   GetCtrlVal (p1h, P1_LOG_SCALE, &logscale) ;
  385.          
  386.           plot_graph ( p1h, logscale, data ) ;
  387.             }          
  388.     }
  389.    
  390.     if ( rph == p2h ) {
  391.             switch ( rID ) {
  392.               case P2_OFS1: GetCtrlVal (p2h, P2_OFS1, &os[0] ) ; i=0 ; break ;
  393.               case P2_OFS2: GetCtrlVal (p2h, P2_OFS2, &os[1] ) ; i=1 ; break ;
  394.               case P2_OFS3: GetCtrlVal (p2h, P2_OFS3, &os[2] ) ; i=2 ; break ;
  395.               case P2_OFS4: GetCtrlVal (p2h, P2_OFS4, &os[3] ) ; i=3 ; break ;
  396.               case P2_OFS5: GetCtrlVal (p2h, P2_OFS5, &os[4] ) ; i=4 ; break ;
  397.               case P2_OFS6: GetCtrlVal (p2h, P2_OFS6, &os[5] ) ; i=5 ; break ;
  398.               case P2_OFS7: GetCtrlVal (p2h, P2_OFS7, &os[6] ) ; i=6 ; break ;
  399.               case P2_OFS8: GetCtrlVal (p2h, P2_OFS8, &os[7] ) ; i=7 ; break ;
  400.               case P2_OFS9: GetCtrlVal (p2h, P2_OFS9, &os[8] ) ; i=8 ; break ;
  401.               case P2_OFS10: GetCtrlVal (p2h, P2_OFS10, &os[9] ) ; i=9 ; break ;
  402.             }
  403.    
  404.    
  405.     }
  406.    
  407.     GetUserEvent (1, &rph, &rID);
  408.   }
  409.      
  410.  
  411.    WUSBXX_close ();    
  412.   //lc8901a_close (hdev);
  413.   return 0;
  414. }
  415.  
  416. int CVICALLBACK TimerCB (int panel, int control, int event,
  417.                 void *callbackData, int eventData1, int eventData2)
  418. {
  419.         switch (event)
  420.                 {
  421.                 case EVENT_TIMER_TICK:
  422.                                                                  
  423.                             SetCtrlVal (panel, P1_LEDON,0);
  424.                             SetCtrlAttribute (panel, P1_TIMER, ATTR_ENABLED, 0);
  425.                         break;
  426.                 }
  427.         return 0;
  428. }
  429.