Subversion Repositories f9daq

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1.  
  2. // libxxusb.cpp : Defines the entry point for the DLL application.
  3. //
  4.  
  5.  
  6.  
  7. #include <string.h>
  8. #include <malloc.h>
  9. #include "usb.h"
  10. #include "libxxusb.h"
  11. #include <time.h>
  12. #include "stdio.h"
  13.  
  14.  
  15.  
  16. // 03/09/06 Release 3.00 changes
  17. // 07/28/06     correction CAMAC write for F to be in range 16...23
  18. // 10/09/06     correction CAMAC read for F to be in range <16 OR >23
  19. // 10/16/06     CAMAC DGG corrected
  20. // 12/28/07     Open corrected for bug when calling register after opening
  21. /*
  22. ******** xxusb_longstack_execute ************************
  23.  
  24.   Executes stack array passed to the function and returns the data read from the VME bus
  25.  
  26.   Paramters:
  27.     hdev: USB device handle returned from an open function
  28.     DataBuffer: pointer to the dual use buffer
  29.                 when calling , DataBuffer contains (unsigned short) stack data, with first word serving
  30.                 as a placeholder
  31.                 upon successful return, DataBuffer contains (unsigned short) VME data
  32.     lDataLen: The number of bytes to be fetched from VME bus - not less than the actual number
  33.                 expected, or the function will return -5 code. For stack consisting only of write operations,
  34.                 lDataLen may be set to 1.
  35.     timeout: The time in ms that should be spent tryimg to write data.
  36.    
  37.   Returns:
  38.     When Successful, the number of bytes read from xxusb.
  39.     Upon failure, a negative number
  40.  
  41.   Note:
  42.         The function must pass a pointer to an array of unsigned integer stack data, in which the first word
  43.         is left empty to serve as a placeholder.
  44.         The function is intended for executing Uint32_t stacks, up to 4 MBytes Uint32_t, both "write" and "read"
  45.         oriented, such as using multi-block transfer operations.
  46.         Structure upon call:
  47.                 DataBuffer(0) = 0(don't care place holder)
  48.                 DataBuffer(1) = (unsigned short)StackLength bits 0-15
  49.                 DataBuffer(2) = (unsigned short)StackLength bits 16-20
  50.                 DataBuffer(3 - StackLength +2)  (unsigned short) stack data
  51.             StackLength represents the number of words following DataBuffer(1) word, thus the total number
  52.                 of words is StackLength+2
  53.         Structure upon return:
  54.                 DataBuffer(0 - (ReturnValue/2-1)) - (unsigned short)array of returned data when ReturnValue>0
  55. */
  56.  
  57. int xxusb_longstack_execute(usb_dev_handle *hDev, void *DataBuffer, int lDataLen, int timeout)
  58. {
  59.     int ret;
  60.     char *cbuf;
  61.         unsigned short *usbuf;
  62.     int bufsize;
  63.          
  64.         cbuf = (char *)DataBuffer;
  65.         usbuf = (unsigned short *)DataBuffer;
  66.     cbuf[0]=12;
  67.     cbuf[1]=0;
  68.         bufsize = 2*(usbuf[1]+0x10000*usbuf[2])+4;
  69.     ret=usb_bulk_write(hDev, XXUSB_ENDPOINT_OUT, cbuf, bufsize, timeout);
  70.     if (ret>0)
  71.         ret=usb_bulk_read(hDev, XXUSB_ENDPOINT_IN, cbuf, lDataLen, timeout);
  72.        
  73.     return ret;
  74. }
  75.  
  76. /*
  77. ******** xxusb_bulk_read ************************
  78.  
  79.   Reads the content of the usbfifo whenever "FIFO full" flag is set,
  80.     otherwise times out.
  81.    
  82.   Paramters:
  83.     hdev: USB device handle returned from an open function
  84.     DataBuffer: pointer to an array to store data that is read from the VME bus;
  85.                 the array may be declared as byte, unsigned short, or unsigned Uint32_t
  86.     lDatalen: The number of bytes to read from xxusb
  87.     timeout: The time in ms that should be spent waiting for data.
  88.    
  89.   Returns:
  90.     When Successful, the number of bytes read from xxusb.
  91.     Upon failure, a negative number
  92.  
  93.   Note:
  94.         Depending upon the actual need, the function may be used to return the data in a form
  95.         of an array of bytes, unsigned short integers (16 bits), or unsigned Uint32_t integers (32 bits).
  96.         The latter option of passing a pointer to an array of unsigned Uint32_t integers is meaningful when
  97.         xxusb data buffering option is used (bit 7=128 of the global register) that requires data
  98.         32-bit data alignment.
  99.  
  100. */
  101. int   xxusb_bulk_read(usb_dev_handle *hDev, void *DataBuffer, int lDataLen, int timeout)
  102. {
  103. int ret;
  104. char *cbuf;
  105. cbuf = (char *)DataBuffer;
  106.     ret = usb_bulk_read(hDev, XXUSB_ENDPOINT_IN, cbuf, lDataLen, timeout);
  107.     return ret;
  108. }
  109.  
  110. /*
  111. ******** xxusb_bulk_write ************************
  112.  
  113.   Writes the content of an array of bytes, unsigned short integers, or unsigned Uint32_t integers
  114.   to the USB port fifo; times out when the USB fifo is full (e.g., when xxusb is busy).
  115.    
  116.   Paramters:
  117.     hdev: USB device handle returned from an open function
  118.     DataBuffer: pointer to an array storing the data to be sent;
  119.                 the array may be declared as byte, unsigned short, or unsigned Uint32_t
  120.     lDatalen: The number of bytes to to send to xxusb
  121.     timeout: The time in ms that should be spent waiting for data.
  122.    
  123.   Returns:
  124.     When Successful, the number of bytes passed to xxusb.
  125.     Upon failure, a negative number
  126.  
  127.   Note:
  128.         Depending upon the actual need, the function may be used to pass to xxusb the data in a form
  129.         of an array of bytes, unsigned short integers (16 bits), or unsigned Uint32_t integers (32 bits).
  130. */
  131. int   xxusb_bulk_write(usb_dev_handle *hDev, void *DataBuffer, int lDataLen, int timeout)
  132. {
  133. int ret;
  134. char *cbuf;
  135. cbuf = (char *)DataBuffer;
  136.     ret = usb_bulk_write(hDev, XXUSB_ENDPOINT_OUT, cbuf, lDataLen, timeout);
  137.     return ret;
  138. }
  139.  
  140. /*
  141. ******** xxusb_usbfifo_read ************************
  142.  
  143.   Reads data stored in the xxusb fifo and packs them in an array of Uint32_t integers.
  144.  
  145.   Paramters:
  146.     hdev: USB device handle returned from an open function
  147.     DataBuffer: pointer to an array of Uint32_t to store data that is read
  148.                 the data occupy only the least significant 16 bits of the 32-bit data words
  149.     lDatalen: The number of bytes to read from the xxusb
  150.     timeout: The time in ms that should be spent waiting for data.
  151.    
  152.   Returns:
  153.     When Successful, the number of bytes read from xxusb.
  154.     Upon failure, a negative number
  155.        
  156.   Note:
  157.         The function is not economical as it wastes half of the space required for storing
  158.         the data received. Also, it is relatively slow, as it performs extensive data repacking.
  159.         It is recommended to use xxusb_bulk_read with a pointer to an array of unsigned short
  160.         integers.
  161. */
  162. int   xxusb_usbfifo_read(usb_dev_handle *hDev, int *DataBuffer, int lDataLen, int timeout)
  163. {
  164. int ret;
  165. char *cbuf;
  166. unsigned short *usbuf;
  167. int i;
  168.  
  169. cbuf = (char *)DataBuffer;
  170. usbuf = (unsigned short *)DataBuffer;
  171.  
  172.     ret = usb_bulk_read(hDev, XXUSB_ENDPOINT_IN, cbuf, lDataLen, timeout);
  173.     if (ret > 0)
  174.         for (i=ret/2-1; i >= 0; i=i-1)
  175.                 {
  176.                         usbuf[i*2]=usbuf[i];
  177.                         usbuf[i*2+1]=0;
  178.                 }
  179.     return ret;
  180. }
  181.  
  182.  
  183. //******************************************************//
  184. //******************* GENERAL XX_USB *******************//
  185. //******************************************************//
  186. // The following are functions used for both VM_USB & CC_USB
  187.  
  188.  
  189. /*
  190. ******** xxusb_register_write ************************
  191.  
  192.   Writes Data to the xxusb register selected by RedAddr.  For
  193.     acceptable values for RegData and RegAddr see the manual
  194.     the module you are using.
  195.    
  196.   Parameters:
  197.     hdev: usb device handle returned from open device
  198.     RegAddr: The internal address if the xxusb
  199.     RegData: The Data to be written to the register
  200.    
  201.   Returns:
  202.     Number of bytes sent to xxusb if successful
  203.     0 if the register is write only
  204.     Negative numbers if the call fails
  205. */
  206. short   xxusb_register_write(usb_dev_handle *hDev, short RegAddr, Uint32_t RegData)
  207. {
  208.     Uint32_t RegD;
  209.     char buf[8]={5,0,0,0,0,0,0,0};
  210.     int ret;
  211.     int lDataLen;
  212.     int timeout;
  213.     if ((RegAddr==0) || (RegAddr==12) || (RegAddr==15))
  214.         return 0;
  215.     buf[2]=(char)(RegAddr & 15);
  216.     buf[4]=(char)(RegData & 255);
  217.  
  218.     RegD = RegData >> 8;
  219.     buf[5]=(char)(RegD & 255);
  220.         RegD = RegD >>8;
  221.         if (RegAddr==8)
  222.         {
  223.                 buf[6]=(char)(RegD & 255);
  224.                 lDataLen=8;
  225.         }
  226.         else
  227.                 lDataLen=6;
  228.         timeout=10;
  229.         ret=xxusb_bulk_write(hDev, buf, lDataLen, timeout);
  230.         return ret;
  231. }
  232.  
  233. /*
  234. ******** xxusb_stack_write ************************
  235.  
  236.   Writes a stack of VME/CAMAC calls to the VM_USB/CC_USB
  237.     to be executed upon trigger.
  238.    
  239.   Parameters:
  240.     hdev: usb device handle returned from an open function
  241.     StackAddr: internal register to which the stack should be written
  242.     lpStackData: Pointer to an array holding the stack
  243.  
  244.   Returns:
  245.     The number of Bytes written to the xxusb when successful
  246.     A negative number upon failure
  247. */
  248. short   xxusb_stack_write(usb_dev_handle *hDev, short StackAddr, Uint32_t *intbuf)
  249. {
  250.   int timeout;
  251.     short ret;
  252.     short lDataLen;
  253.     char buf[2000];
  254.     short i;
  255.     int bufsize;
  256.      
  257.     buf[0]=(char)((StackAddr & 51) + 4);
  258.     buf[1]=0;
  259.     lDataLen=(short)(intbuf[0] & 0xFFF);
  260.     buf[2]=(char)(lDataLen & 255);
  261.     lDataLen = lDataLen >> 8;
  262.     buf[3] = (char)(lDataLen & 255);
  263.     bufsize=intbuf[0]*2+4;
  264.     if (intbuf[0]==0)
  265.       return 0;
  266.     for (i=1; i <= intbuf[0]; i++)
  267.       {
  268.         buf[2+2*i] = (char)(intbuf[i] & 255);
  269.         buf[3+2*i] = (char)((intbuf[i] >>8) & 255);
  270.       }
  271.     timeout=50;
  272.     ret=usb_bulk_write(hDev, XXUSB_ENDPOINT_OUT, buf, bufsize, timeout);
  273.     return ret;
  274. }
  275.  
  276. /*
  277. ******** xxusb_stack_execute **********************
  278.  
  279.   Writes, executes and returns the value of a DAQ stack.
  280.  
  281.   Parameters:
  282.     hdev: USB device handle returned from an open function
  283.     intbuf: Pointer to an array holding the values stack.  Upon return
  284.             Pointer value is the Data returned from the stack.
  285.  
  286.   Returns:
  287.     When successful, the number of Bytes read from xxusb
  288.     Upon Failure, a negative number.
  289. */
  290. short   xxusb_stack_execute(usb_dev_handle *hDev, Uint32_t *intbuf)
  291. {
  292.   int timeout;
  293.     short ret;
  294.     short lDataLen;
  295.     char buf[26700];
  296.     short i;
  297.     int bufsize;
  298.     int ii = 0;
  299.          
  300.     buf[0]=12;
  301.     buf[1]=0;
  302.     lDataLen=(short)(intbuf[0] & 0xFFF);
  303.     buf[2]=(char)(lDataLen & 255);
  304.     lDataLen = lDataLen >> 8;
  305.     buf[3] = (char)(lDataLen & 15);
  306.     bufsize=intbuf[0]*2+4;
  307.     if (intbuf[0]==0)
  308.       return 0;
  309.     for (i=1; i <= intbuf[0]; i++)
  310.       {
  311.         buf[2+2*i] = (char)(intbuf[i] & 255);
  312.         buf[3+2*i] = (char)((intbuf[i] >>8) & 255);
  313.       }
  314.     timeout=2000;
  315.     ret=usb_bulk_write(hDev, XXUSB_ENDPOINT_OUT, buf, bufsize, timeout);
  316.     if (ret>0)
  317.         {
  318.         lDataLen=26700;
  319.         timeout=6000;
  320.         ret=usb_bulk_read(hDev, XXUSB_ENDPOINT_IN, buf, lDataLen, timeout);
  321.         if (ret>0)
  322.         for (i=0; i < ret; i=i+2)
  323.           intbuf[ii++]=(UCHAR)(buf[i]) +(UCHAR)( buf[i+1])*256;
  324.         }
  325.     return ret;
  326. }
  327.  
  328. /*
  329. ******** xxusb_stack_read ************************
  330.  
  331.   Reads the current DAQ stack stored by xxusb
  332.  
  333.   Parameters:
  334.     hdev: USB device handle returned by an open function
  335.     StackAddr: Indicates which stack to read, primary or secondary
  336.     intbuf: Pointer to a array where the stack can be stored
  337.  
  338.   Returns:
  339.     Number of bytes read from xxusb when successful
  340.     Upon failure, a negative number
  341. */
  342. short   xxusb_stack_read(usb_dev_handle *hDev, short StackAddr, Uint32_t *intbuf)
  343. {
  344.   int timeout;
  345.     short ret;
  346.     short lDataLen;
  347.     short bufsize;
  348.     char buf[1600];
  349.     int i;
  350.  
  351.     buf[0]=(char)(StackAddr & 51);
  352.     buf[1]=0;
  353.     lDataLen = 2;
  354.     timeout=100;
  355.     ret=usb_bulk_write(hDev, XXUSB_ENDPOINT_OUT, buf, lDataLen, timeout);
  356.     if (ret < 0)
  357.         return ret;
  358.     else
  359.       bufsize=1600;
  360.     int ii=0;
  361.       {
  362.         ret=usb_bulk_read(hDev, XXUSB_ENDPOINT_IN, buf, bufsize, timeout);
  363.         if (ret>0)
  364.         for (i=0; i < ret; i=i+2)
  365.           intbuf[ii++]=(UCHAR)(buf[i]) + (UCHAR)(buf[i+1])*256;
  366.         return ret;
  367.          
  368.       }    
  369. }
  370.  
  371. /*
  372. ******** xxusb_register_read ************************
  373.  
  374.   Reads the current contents of an internal xxusb register
  375.  
  376.   Parameters:
  377.     hdev: USB device handle returned from an open function
  378.     RegAddr: The internal address of the register from which to read
  379.     RegData: Pointer to a Uint32_t to hold the data.
  380.    
  381.   Returns:
  382.     When Successful, the number of bytes read from xxusb.
  383.     Upon failure, a negative number
  384. */
  385. short   xxusb_register_read(usb_dev_handle *hDev, short RegAddr, Uint32_t *RegData)
  386. {
  387. //Uint32_t RegD;
  388. int timeout;
  389.         char buf[4]={1,0,0,0};
  390.         int ret;
  391.         int lDataLen;
  392.  
  393.         buf[2]=(char)(RegAddr & 15);   
  394.         timeout=10;
  395.         lDataLen=4;
  396.         ret=xxusb_bulk_write(hDev, buf, lDataLen, timeout);
  397.         if (ret < 0)
  398.                 return (short)ret;
  399.         else
  400.         {
  401.             lDataLen=8;
  402.             timeout=100;
  403.             ret=xxusb_bulk_read(hDev, buf, lDataLen, timeout);
  404.             if (ret<0)
  405.               return (short)ret;
  406.             else
  407.               {
  408.                 *RegData=(UCHAR)(buf[0])+256*(UCHAR)(buf[1]);
  409.                 if (ret==4)
  410.                  *RegData=*RegData+0x10000*(UCHAR)(buf[2]);
  411.                 return (short)ret;
  412.                 }
  413.         }
  414. }
  415.  
  416. /*
  417. ******** xxusb_reset_toggle ************************
  418.  
  419.   Toggles the reset state of the FPGA while the xxusb in programming mode
  420.  
  421.   Parameters
  422.     hdev: US B device handle returned from an open function
  423.    
  424.   Returns:
  425.     Upon failure, a negative number  
  426. */
  427. short   xxusb_reset_toggle(usb_dev_handle *hDev)
  428. {
  429.   short ret;
  430.   char buf[2] = {(char)255,(char)255};
  431.   int lDataLen=2;
  432.   int timeout=1000;
  433.   ret = usb_bulk_write(hDev, XXUSB_ENDPOINT_OUT, buf,lDataLen, timeout);
  434.   return (short)ret;
  435. }
  436.  
  437. /*
  438. ******** xxusb_devices_find ************************
  439.  
  440.   Determines the number and parameters of all xxusb devices attched to
  441.     the computer.
  442.    
  443.   Parameters:
  444.     xxdev: pointer to an array on which the device parameters are stored
  445.  
  446.   Returns:
  447.     Upon success, returns the number of devices found
  448.     Upon Failure returns a negative number
  449. */
  450. short   xxusb_devices_find(xxusb_device_type *xxdev)
  451. {
  452.   short DevFound = 0;
  453.   usb_dev_handle *udev;
  454.   struct usb_bus *bus;
  455.   struct usb_device *dev;  
  456. struct usb_bus *usb_busses;
  457.   char string[256];
  458.   short ret;
  459.    usb_init();
  460.    usb_find_busses();
  461.    usb_busses=usb_get_busses();
  462.   usb_find_devices();
  463.    for (bus=usb_busses; bus; bus = bus->next)
  464.    {
  465.        for (dev = bus->devices; dev; dev= dev->next)
  466.        {
  467.            if (dev->descriptor.idVendor==XXUSB_WIENER_VENDOR_ID)
  468.            {
  469.                udev = usb_open(dev);
  470.                if (udev)
  471.                {
  472.                    ret = usb_get_string_simple(udev, dev->descriptor.iSerialNumber, string, sizeof(string));
  473.                    if (ret >0 )  
  474.                      {
  475.                        xxdev[DevFound].usbdev=dev;
  476.                        strcpy(xxdev[DevFound].SerialString, string);
  477.                        DevFound++;
  478.                      }
  479.                    usb_close(udev);
  480.                 }
  481.                    else return -1;
  482.            }
  483.        }
  484.     }
  485.   return DevFound;
  486. }
  487.  
  488. /*
  489. ******** xxusb_device_close ************************
  490.  
  491.   Closes an xxusb device
  492.  
  493.   Parameters:
  494.     hdev: USB device handle returned from an open function
  495.  
  496.   Returns:  1
  497. */
  498. short   xxusb_device_close(usb_dev_handle *hDev)
  499. {
  500.     short ret;
  501.     ret=usb_release_interface(hDev,0);
  502.     usb_close(hDev);
  503.     return 1;
  504. }
  505.  
  506. /*
  507. ******** xxusb_device_open ************************
  508.  
  509.   Opens an xxusb device found by xxusb_device_find
  510.  
  511.   Parameters:
  512.     dev: a usb device
  513.    
  514.   Returns:
  515.     A USB device handle
  516. */
  517. usb_dev_handle*   xxusb_device_open(struct usb_device *dev)
  518. {
  519.     short ret;
  520.     Uint32_t val;
  521.     int count =0;
  522.     usb_dev_handle *udev;
  523.     udev = usb_open(dev);
  524.     ret = usb_set_configuration(udev,1);
  525.     ret = usb_claim_interface(udev,0);
  526. // RESET USB (added 10/16/06 Andreas Ruben)
  527.         ret=xxusb_register_write(udev, 10, 0x04);
  528. // Loop to find known state (added 12/28/07 TH / AR)
  529.         ret =-1;
  530.         while ((ret <0) && (count <10))
  531.         {
  532.             xxusb_register_read(udev, 0, &val);
  533.             count++;
  534.         }
  535.  
  536.     return udev;
  537. }
  538.  
  539. /*
  540. ******** xxusb_flash_program ************************
  541.  
  542.     --Untested and therefore uncommented--
  543. */
  544. short   xxusb_flash_program(usb_dev_handle *hDev, char *config, short nsect)
  545. {
  546.         int i=0;
  547.         int k=0;
  548.         short ret=0;
  549.         time_t t1,t2;
  550.  
  551.         char *pconfig;
  552.         char *pbuf;
  553.         pconfig=config;
  554.         char buf[518] ={(char)0xAA,(char)0xAA,(char)0x55,(char)0x55,(char)0xA0,(char)0xA0};
  555.         while (*pconfig++ != -1);
  556.         for (i=0; i<nsect; i++)
  557.         {
  558.                 pbuf=buf+6;
  559.                 for (k=0; k<256; k++)
  560.                 {
  561.                         *(pbuf++)=*(pconfig);
  562.                         *(pbuf++)=*(pconfig++);
  563.                 }
  564.                 ret = usb_bulk_write(hDev, XXUSB_ENDPOINT_OUT, buf, 518, 2000);
  565.                 if (ret<0)
  566.                         return ret;
  567.                 t1=clock()+(time_t)(0.03*CLOCKS_PER_SEC);
  568.                 while (t1>clock());
  569.                 t2=clock();
  570.         }
  571.         return ret;
  572. }
  573.  
  574. /*
  575. ******** xxusb_flashblock_program ************************
  576.  
  577.       --Untested and therefore uncommented--  
  578. */
  579. short   xxusb_flashblock_program(usb_dev_handle *hDev, UCHAR *config)
  580. {
  581.         int k=0;
  582.         short ret=0;
  583.  
  584.         UCHAR *pconfig;
  585.         char *pbuf;
  586.         pconfig=config;
  587.         char buf[518] ={(char)0xAA,(char)0xAA,(char)0x55,(char)0x55,(char)0xA0,(char)0xA0};
  588.         pbuf=buf+6;
  589.         for (k=0; k<256; k++)
  590.         {
  591.                 *(pbuf++)=(UCHAR)(*(pconfig));
  592.                 *(pbuf++)=(UCHAR)(*(pconfig++));
  593.         }
  594.         ret = usb_bulk_write(hDev, XXUSB_ENDPOINT_OUT, buf, 518, 2000);
  595.         return ret;
  596. }
  597.  
  598. /*
  599. ******** xxusb_serial_open ************************
  600.  
  601.   Opens a xxusb device whose serial number is given
  602.  
  603.   Parameters:
  604.     SerialString: a char string that gives the serial number of
  605.                   the device you wish to open.  It takes the form:
  606.                   VM0009 - for a vm_usb with serial number 9 or
  607.                   CC0009 - for a cc_usb with serial number 9
  608.  
  609.   Returns:
  610.     A USB device handle
  611. */
  612. usb_dev_handle*   xxusb_serial_open(char *SerialString)
  613. {
  614.   short DevFound = 0;
  615.   usb_dev_handle *udev = NULL;
  616.   struct usb_bus *bus;
  617.   struct usb_device *dev;  
  618. struct usb_bus *usb_busses;
  619. char string[20];
  620.   short ret;
  621.   //usb_set_debug(4);
  622.   usb_init();
  623.    usb_find_busses();
  624.   usb_busses=usb_get_busses();
  625.    usb_find_devices();
  626.    for (bus=usb_busses; bus; bus = bus->next)
  627.    {
  628.        for (dev = bus->devices; dev; dev= dev->next)
  629.        {
  630.            if (dev->descriptor.idVendor==XXUSB_WIENER_VENDOR_ID)
  631.            {
  632.                printf("id %04x\n",dev->descriptor.idVendor);
  633.                udev = xxusb_device_open(dev);
  634.                if (udev)
  635.                {
  636.                    ret = usb_get_string_simple(udev, dev->descriptor.iSerialNumber, string, sizeof(string));
  637.                                         printf("iSerial %04x string %s\n",dev->descriptor.iSerialNumber,string);
  638.                                         if (ret >0 )  
  639.                                                 {
  640.                                                         printf(" Device with %s\n",SerialString);
  641.  
  642.                                                         if (strcmp(string,SerialString)==0){
  643.                                                                 printf(" Device with %s found\n",SerialString);
  644.                                                                 return udev;
  645.                                                         }
  646.                                                 } else {
  647.                                                         printf(" Device returned  %d\n",ret);
  648.                                                 }
  649.                                                 usb_close(udev);
  650.                                 }
  651.            }
  652.        }
  653.     }
  654.    udev = NULL;
  655.    return udev;
  656. }
  657.  
  658.  
  659. //******************************************************//
  660. //****************** EZ_VME Functions ******************//
  661. //******************************************************//
  662. // The following are functions used to perform simple
  663. // VME Functions with the VM_USB
  664.  
  665. /*
  666. ******** VME_write_32 ************************
  667.  
  668.   Writes a 32 bit data word to the VME bus
  669.  
  670.   Parameters:
  671.     hdev: USB devcie handle returned from an open function
  672.     Address_Modifier: VME address modifier for the VME call
  673.     VME_Address: Address to write the data to
  674.     Data: 32 bit data word to be written to VME_Address
  675.  
  676.   Returns:
  677.     Number of bytes read from xxusb when successful
  678.     Upon failure, a negative number
  679. */
  680. short   VME_write_32(usb_dev_handle *hdev, short Address_Modifier, Uint32_t VME_Address, Uint32_t Data)
  681. {
  682.     Uint32_t intbuf[1000];
  683.     short ret;
  684.     intbuf[0]=7;
  685.     intbuf[1]=0;
  686.     intbuf[2]=Address_Modifier;
  687.     intbuf[3]=0;
  688.     intbuf[4]=(VME_Address & 0xffff);
  689.     intbuf[5]=((VME_Address >>16) & 0xffff);
  690.     intbuf[6]=(Data & 0xffff);
  691.     intbuf[7]=((Data >> 16) & 0xffff);
  692.     ret = xxusb_stack_execute(hdev, intbuf);
  693.     return ret;
  694. }
  695.  
  696. /*
  697. ******** VME_read_32 ************************
  698.  
  699.  
  700.   Reads a 32 bit data word from a VME address
  701.  
  702.   Parameters:
  703.     hdev: USB devcie handle returned from an open function
  704.     Address_Modifier: VME address modifier for the VME call
  705.     VME_Address: Address to read the data from
  706.     Data: 32 bit data word read from VME_Address
  707.  
  708.   Returns:
  709.     Number of bytes read from xxusb when successful
  710.     Upon failure, a negative number
  711. */
  712. short   VME_read_32(usb_dev_handle *hdev, short Address_Modifier, Uint32_t VME_Address, Uint32_t *Data)
  713. {
  714.     Uint32_t intbuf[1000];
  715.     short ret;
  716.     intbuf[0]=5;
  717.     intbuf[1]=0;
  718.     intbuf[2]=Address_Modifier +0x100;
  719.     intbuf[3]=0;
  720.     intbuf[4]=(VME_Address & 0xffff);
  721.     intbuf[5]=((VME_Address >>16) & 0xffff);
  722.     ret = xxusb_stack_execute(hdev, intbuf);
  723.     *Data=intbuf[0] + (intbuf[1] * 0x10000);
  724.     return ret;
  725. }
  726.  
  727. /*
  728. ******** VME_write_16 ************************
  729.  
  730.   Writes a 16 bit data word to the VME bus
  731.  
  732.   Parameters:
  733.     hdev: USB devcie handle returned from an open function
  734.     Address_Modifier: VME address modifier for the VME call
  735.     VME_Address: Address to write the data to
  736.     Data: word to be written to VME_Address
  737.  
  738.   Returns:
  739.     Number of bytes read from xxusb when successful
  740.     Upon failure, a negative number
  741. */
  742. short   VME_write_16(usb_dev_handle *hdev, short Address_Modifier, Uint32_t VME_Address, Uint32_t Data)
  743. {
  744.     Uint32_t intbuf[1000];
  745.     short ret;
  746.     intbuf[0]=7;
  747.     intbuf[1]=0;
  748.     intbuf[2]=Address_Modifier;
  749.     intbuf[3]=0;
  750.     intbuf[4]=(VME_Address & 0xffff)+ 0x01;
  751.     intbuf[5]=((VME_Address >>16) & 0xffff);
  752.     intbuf[6]=(Data & 0xffff);
  753.     intbuf[7]=0;
  754.     ret = xxusb_stack_execute(hdev, intbuf);
  755.     return ret;
  756. }
  757.  
  758. /*
  759. ******** VME_read_16 ************************
  760.  
  761.   Reads a 16 bit data word from a VME address
  762.  
  763.   Parameters:
  764.     hdev: USB devcie handle returned from an open function
  765.     Address_Modifier: VME address modifier for the VME call
  766.     VME_Address: Address to read the data from
  767.     Data: word read from VME_Address
  768.  
  769.   Returns:
  770.     Number of bytes read from xxusb when successful
  771.     Upon failure, a negative number
  772. */
  773. short   VME_read_16(usb_dev_handle *hdev,short Address_Modifier, Uint32_t VME_Address, Uint32_t *Data)
  774. {
  775.     Uint32_t intbuf[1000];
  776.     short ret;
  777.     intbuf[0]=5;
  778.     intbuf[1]=0;
  779.     intbuf[2]=Address_Modifier +0x100;
  780.     intbuf[3]=0;
  781.     intbuf[4]=(VME_Address & 0xffff)+ 0x01;
  782.     intbuf[5]=((VME_Address >>16) & 0xffff);
  783.     ret = xxusb_stack_execute(hdev, intbuf);
  784.     *Data=intbuf[0];
  785.     return ret;
  786. }
  787.  
  788. /*
  789. ******** VME_BLT_read_32 ************************
  790.  
  791.   Performs block transfer of 32 bit words from a VME address
  792.  
  793.   Parameters:
  794.     hdev: USB devcie handle returned from an open function
  795.     Address_Modifier: VME address modifier for the VME call
  796.     count: number of data words to read
  797.     VME_Address: Address to read the data from
  798.     Data: pointer to an array to hold the data words
  799.  
  800.   Returns:
  801.     Number of bytes read from xxusb when successful
  802.     Upon failure, a negative number
  803. */
  804. short   VME_BLT_read_32(usb_dev_handle *hdev, short Adress_Modifier, int count, Uint32_t VME_Address, Uint32_t Data[])
  805. {
  806.     Uint32_t intbuf[1000];
  807.     short ret;
  808.     int i=0;
  809.     if (count > 255) return -1;
  810.     intbuf[0]=5;
  811.     intbuf[1]=0;
  812.     intbuf[2]=Adress_Modifier +0x100;
  813.     intbuf[3]=(count << 8);
  814.     intbuf[4]=(VME_Address & 0xffff);
  815.     intbuf[5]=((VME_Address >>16) & 0xffff);
  816.     ret = xxusb_stack_execute(hdev, intbuf);
  817.     int j=0;
  818.     for (i=0;i<(2*count);i=i+2)
  819.     {
  820.         Data[j]=intbuf[i] + (intbuf[i+1] * 0x10000);
  821.         j++;
  822.     }
  823.     return ret;
  824. }
  825.  
  826. //******************************************************//
  827. //****************** VM_USB Registers ******************//
  828. //******************************************************//
  829. // The following are functions used to set the registers
  830. // in the VM_USB
  831.  
  832. /*
  833. ******** VME_register_write ************************
  834.  
  835.   Writes to the vmusb registers that are accessible through
  836.   VME style calls
  837.  
  838.   Parameters:
  839.     hdev: USB devcie handle returned from an open function
  840.     VME_Address: The VME Address of the internal register
  841.     Data: Data to be written to VME_Address
  842.  
  843.   Returns:
  844.     Number of bytes read from xxusb when successful
  845.     Upon failure, a negative number  
  846. */
  847. short   VME_register_write(usb_dev_handle *hdev, Uint32_t VME_Address, Uint32_t Data)
  848. {
  849.     Uint32_t intbuf[1000];
  850.     short ret;
  851.  
  852.     intbuf[0]=7;
  853.     intbuf[1]=0;
  854.     intbuf[2]=0x1000;
  855.     intbuf[3]=0;
  856.     intbuf[4]=(VME_Address & 0xffff);
  857.     intbuf[5]=((VME_Address >>16) & 0xffff);
  858.     intbuf[6]=(Data & 0xffff);
  859.     intbuf[7]=((Data >> 16) & 0xffff);
  860.     ret = xxusb_stack_execute(hdev, intbuf);
  861.     return ret;
  862. }
  863.  
  864. /*
  865. ******** VME_register_read ************************
  866.  
  867.   Reads from the vmusb registers that are accessible trough VME style calls
  868.  
  869.   Parameters:
  870.     hdev: USB devcie handle returned from an open function
  871.     VME_Address: The VME Address of the internal register
  872.     Data: Data read from VME_Address
  873.  
  874.   Returns:
  875.     Number of bytes read from xxusb when successful
  876.     Upon failure, a negative number
  877. */
  878. short   VME_register_read(usb_dev_handle *hdev, Uint32_t VME_Address, Uint32_t *Data)
  879. {
  880.     Uint32_t intbuf[1000];
  881.     short ret;
  882.  
  883.     intbuf[0]=5;
  884.     intbuf[1]=0;
  885.     intbuf[2]=0x1100;
  886.     intbuf[3]=0;
  887.     intbuf[4]=(VME_Address & 0xffff);
  888.     intbuf[5]=((VME_Address >>16) & 0xffff);
  889.     ret = xxusb_stack_execute(hdev, intbuf);
  890.     *Data=intbuf[0] + (intbuf[1] * 0x10000);
  891.     return ret;
  892. }
  893.  
  894. /*
  895. ******** VME_LED_settings ************************
  896.  
  897.   Sets the vmusb LED's
  898.  
  899.   Parameters:
  900.     hdev: USB devcie handle returned from an open function
  901.     LED: The number which corresponds to an LED values are:
  902.             0 - for Top YELLOW LED
  903.                         1 - for RED LED
  904.             2 - for GREEN LED
  905.             3 - for Bottom YELLOW LED
  906.     code: The LED aource selector code, valid values for each LED
  907.           are listed in the manual
  908.     invert: to invert the LED lighting
  909.     latch: sets LED latch bit
  910.  
  911.   Returns:
  912.     Number of bytes read from xxusb when successful
  913.     Upon failure, a negative number
  914. */
  915. short   VME_LED_settings(usb_dev_handle *hdev, int LED, int code, int invert, int latch)
  916. {
  917.     short ret;
  918. //    Uint32_t internal;
  919.     Uint32_t Data;
  920.    
  921.     if( (LED <0) ||(LED > 3) || (code < 0) || (code > 7))  return -1;
  922.      
  923.     VME_register_read(hdev,0xc,&Data);  
  924.     if(LED == 0)  
  925.     {
  926.         Data = Data & 0xFFFFFF00;
  927.         Data = Data | code;
  928.         if (invert == 1 && latch == 1)   Data = Data | 0x18;
  929.         if (invert == 1 && latch == 0)   Data = Data | 0x08;
  930.         if (invert == 0 && latch == 1)   Data = Data | 0x10;
  931.     }
  932.     if(LED == 1)  
  933.     {
  934.         Data = Data & 0xFFFF00FF;
  935.         Data = Data | (code * 0x0100);
  936.         if (invert == 1 && latch == 1)  Data = Data | 0x1800;
  937.         if (invert == 1 && latch == 0)  Data = Data | 0x0800;
  938.         if (invert == 0 && latch == 1)  Data = Data | 0x1000;
  939.     }  
  940.     if(LED == 2)
  941.     {
  942.         Data = Data & 0xFF00FFFF;
  943.         Data = Data | (code * 0x10000);
  944.         if (invert == 1 && latch == 1) Data = Data | 0x180000;
  945.         if (invert == 1 && latch == 0) Data = Data | 0x080000;
  946.         if (invert == 0 && latch == 1) Data = Data | 0x100000;
  947.     }
  948.     if(LED == 3)
  949.     {
  950.         Data = Data & 0x00FFFFFF;
  951.         Data = Data | (code * 0x10000);
  952.         if (invert == 1 && latch == 1)  Data = Data | 0x18000000;
  953.         if (invert == 1 && latch == 0)  Data = Data | 0x08000000;
  954.         if (invert == 0 && latch == 1)  Data = Data | 0x10000000;
  955.     }
  956.     ret = VME_register_write(hdev, 0xc, Data);
  957.     return ret;
  958. }
  959.  
  960. /*
  961. ******** VME_DGG ************************
  962.  
  963.   Sets the parameters for Gate & Delay channel A of vmusb
  964.  
  965.   Parameters:
  966.     hdev: USB devcie handle returned from an open function
  967.     channel: Which DGG channel to use Valid Values are:
  968.               0 - For DGG A
  969.                           1 - For DGG B
  970.     trigger:  Determines what triggers the start of the DGG Valid values are:
  971.               0 - Channel disabled
  972.               1 - NIM input 1
  973.               2 - NIM input 2
  974.               3 - Event Trigger
  975.               4 - End of Event
  976.               5 - USB Trigger
  977.               6 - Pulser
  978.     output: Determines which NIM output to use for the channel, Vaild values are:
  979.               0 - for NIM O1
  980.               1 - for NIM O2
  981.     delay: 32 bit word consisting of
  982.                         lower 16 bits: Delay_fine in steps of 12.5ns between trigger and start of gate
  983.                         upper 16 bits: Delay_coarse in steps of 81.7us between trigger and start of gate
  984.     gate: the time the gate should  stay open in steps of 12.5ns
  985.     invert: is 1 if you wish to invert the DGG channel output
  986.     latch: is 1 if you wish to run the DGG channel latched
  987.  
  988.   Returns:
  989.     Returns 1 when successful
  990.     Upon failure, a negative number
  991. */
  992. short   VME_DGG(usb_dev_handle *hdev, unsigned short channel, unsigned short trigger, unsigned short output,
  993.                                                 Uint32_t delay, unsigned short gate, unsigned short invert, unsigned short latch)
  994. {
  995.     Uint32_t Data, DGData, Delay_ext;
  996.     Uint32_t internal;
  997.     short ret;
  998.  
  999.  
  1000.     ret = VME_register_read(hdev, 0x10,  &Data);
  1001.     // check and correct values
  1002.     if(ret<=0) return -1;
  1003.  
  1004.     if(channel >1) channel =1;
  1005.     if(invert >1) invert =1;
  1006.     if(latch >1) latch =1;
  1007.         if(output >1) output =1;
  1008.         if(trigger >6) trigger =0;
  1009.  
  1010.     // define Delay and Gate data
  1011.     DGData = gate * 0x10000;
  1012.     DGData += (unsigned short) delay;
  1013.  
  1014.         // Set channel, output, invert, latch
  1015.         if (output == 0)
  1016.     {
  1017.                 Data = Data & 0xFFFFFF00;
  1018.                 Data += 0x04 + channel +0x08*invert + 0x10*latch;
  1019.     }
  1020.         if (output == 1)
  1021.     {
  1022.                 Data = Data & 0xFFFF00FF;
  1023.                 Data += (0x04 + channel +0x08*invert + 0x10*latch)*0x100;
  1024.     }
  1025.  
  1026.     // Set trigger, delay, gate
  1027.  
  1028.     if(channel ==0) // CHANNEL DGG_A
  1029.     {
  1030.             internal = (trigger * 0x1000000) ;
  1031.             Data= Data & 0xF0FFFFFF;
  1032.             Data += internal;
  1033.             ret = VME_register_write(hdev,0x10,Data);
  1034.                 if(ret<=0) return -1;
  1035.                 ret=VME_register_write(hdev,0x14,DGData);
  1036.                 if(ret<=0) return -1;
  1037.     // Set coarse delay in DGG_Extended register
  1038.             ret = VME_register_read(hdev,0x38,&Data);
  1039.                 Delay_ext= (Data & 0xffff0000);
  1040.                 Delay_ext+= ((delay/0x10000) & 0xffff);
  1041.             ret = VME_register_write(hdev,0x38,Delay_ext);
  1042.         }
  1043.         if( channel ==1)  // CHANNEL DGG_B
  1044.         {
  1045.                 internal = (trigger * 0x10000000) ;
  1046.                 Data= Data & 0x0FFFFFFF;
  1047.                 Data += internal;
  1048.             ret = VME_register_write(hdev,0x10,Data);
  1049.                 if(ret<=0) return -1;
  1050.                 ret=VME_register_write(hdev,0x18,DGData);
  1051.                 if(ret<=0) return -1;
  1052.     // Set coarse delay in DGG_Extended register
  1053.             ret = VME_register_read(hdev,0x38,&Data);
  1054.                 Delay_ext= (Data & 0x0000ffff);
  1055.                 Delay_ext+= (delay & 0xffff0000);
  1056.             ret = VME_register_write(hdev,0x38,Delay_ext);
  1057.     }
  1058.         return 1;
  1059.        
  1060. }
  1061.  
  1062. /*
  1063. ******** VME_Output_settings ************************
  1064.  
  1065.   Sets the vmusb NIM output register
  1066.  
  1067.   Parameters:
  1068.     hdev: USB devcie handle returned from an open function
  1069.     Channel: The number which corresponds to an output:
  1070.             1 - for Output 1
  1071.             2 - for Output 2
  1072.     code: The Output selector code, valid values
  1073.           are listed in the manual
  1074.     invert: to invert the output
  1075.     latch: sets latch bit
  1076.  
  1077.   Returns:
  1078.     Number of bytes read from xxusb when successful
  1079.     Upon failure, a negative number
  1080. */
  1081. short   VME_Output_settings(usb_dev_handle *hdev, int Channel, int code, int invert, int latch)
  1082. {
  1083.  
  1084.     short ret;
  1085. //    Uint32_t internal;
  1086.     Uint32_t Data;
  1087.    
  1088.     if( (Channel <1) ||(Channel > 2) || (code < 0) || (code > 7)) return -1;    
  1089.     VME_register_read(hdev,0x10,&Data);  
  1090.     if(Channel == 1)  
  1091.     {
  1092.         Data = Data & 0xFFFF00;
  1093.         Data = Data | code;
  1094.         if (invert == 1 && latch == 1)   Data = Data | 0x18;
  1095.         if (invert == 1 && latch == 0)   Data = Data | 0x08;
  1096.         if (invert == 0 && latch == 1)   Data = Data | 0x10;
  1097.     }
  1098.     if(Channel == 2)  
  1099.     {
  1100.         Data = Data & 0xFF00FF;
  1101.         Data = Data | (code * 0x0100);
  1102.         if (invert == 1 && latch == 1)   Data = Data | 0x1800;
  1103.         if (invert == 1 && latch == 0)   Data = Data | 0x0800;
  1104.         if (invert == 0 && latch == 1)   Data = Data | 0x1000;
  1105.     }  
  1106.     ret = VME_register_write(hdev, 0x10, Data);
  1107.     return ret;
  1108. }
  1109.  
  1110.  
  1111. //******************************************************//
  1112. //****************** CC_USB Registers ******************//
  1113. //******************************************************//
  1114. // The following are functions used to set the registers
  1115. // in the CAMAC_USB
  1116.  
  1117. /*
  1118. ******** CAMAC_register_write *****************
  1119.  
  1120.   Performs a CAMAC write to CC_USB register
  1121.  
  1122.   Parameters:
  1123.     hdev: USB device handle returned from an open function
  1124.     A: CAMAC Subaddress
  1125.     F: CAMAC Function
  1126.     Data: data to be written
  1127.    
  1128.   Returns:
  1129.     Number of bytes written to xxusb when successful
  1130.     Upon failure, a negative number
  1131. */
  1132. short   CAMAC_register_write(usb_dev_handle *hdev, int A, Uint32_t Data)
  1133. {
  1134.     int F = 16;
  1135.     int N = 25;
  1136.     Uint32_t intbuf[4];  
  1137.     int ret;
  1138.  
  1139.     intbuf[0]=1;
  1140.     intbuf[1]=(Uint32_t)(F+A*32+N*512 + 0x4000);       
  1141.         intbuf[0]=3;
  1142.         intbuf[2]=(Data & 0xffff);
  1143.         intbuf[3]=((Data >>16) & 0xffff);
  1144.         ret = xxusb_stack_execute(hdev, intbuf);
  1145.  
  1146.     return ret;
  1147. }
  1148.  
  1149. /*
  1150. ******** CAMAC_register_read ************************
  1151.  
  1152.   Performs a CAMAC read from CC_USB register
  1153.  
  1154.   Parameters:
  1155.     hdev: USB device handle returned from an open function
  1156.     N: CAMAC Station Number
  1157.     A: CAMAC Subaddress
  1158.     F: CAMAC Function
  1159.     Q: The Q response from the CAMAC dataway
  1160.     X: The comment accepted response from CAMAC dataway
  1161.    
  1162.   Returns:
  1163.     Number of bytes read from xxusb when successful
  1164.     Upon failure, a negative number
  1165. */
  1166. short   CAMAC_register_read(usb_dev_handle *hdev, int A, Uint32_t *Data)
  1167. {
  1168.     int F = 0;
  1169.     int N = 25;
  1170.     Uint32_t intbuf[4];  
  1171.     int ret;
  1172.  
  1173.     intbuf[0]=1;
  1174.     intbuf[1]=(Uint32_t)(F+A*32+N*512 + 0x4000);
  1175.     ret = xxusb_stack_execute(hdev, intbuf);
  1176.     *Data=intbuf[0] + (intbuf[1] * 0x10000);
  1177.  
  1178.     return ret;
  1179. }
  1180.  
  1181. /*
  1182. ******** CAMAC_DGG ************************
  1183.  
  1184.   Sets the parameters for Gate & Delay channel A of CC_USB
  1185.  
  1186.   Parameters:
  1187.     hdev: USB devcie handle returned from an open function
  1188.     channel: Which DGG channel to use Valid Values are:
  1189.               0 - For DGG A
  1190.                                                         1 - For DGG B
  1191.     trigger:  Determines what triggers the start of the DGG Valid values are:
  1192.               0 - Channel disabled
  1193.               1 - NIM input 1
  1194.               2 - NIM input 2
  1195.               3 - NIM input 2
  1196.               4 - Event Trigger
  1197.                                                   5 - End of Event
  1198.                                       6 - USB Trigger
  1199.                                                         7 - Pulser
  1200.     output: Determines which NIM output to use for the channel, Vaild values are:
  1201.               1 - for NIM O1
  1202.               2 - for NIM O2
  1203.               3 - for NIM O3
  1204.     delay: Delay in steps of 12.5ns between trigger and start of gate
  1205.     gate: the time the gate should  stay open in steps of 12.5ns
  1206.     invert: is 1 if you wish to invert the DGG channel output
  1207.     latch: is 1 if you wish to run the DGG channel latched
  1208.  
  1209.   Returns:
  1210.     Returns 1 when successful
  1211.     Upon failure, a negative number
  1212. */
  1213. short   CAMAC_DGG(usb_dev_handle *hdev, short channel, short trigger, short output,
  1214.                                     int delay, int gate, short invert, short latch)
  1215.  
  1216.  
  1217.  
  1218. {
  1219. //    short channel_ID;
  1220.     Uint32_t Data;
  1221.     Uint32_t internal;
  1222.     short ret;
  1223.                 Uint32_t Delay_ext;
  1224.  
  1225.     ret = CAMAC_register_read(hdev,5,&Data);
  1226.     //Set trigger
  1227.     if((output < 1 ) || (output >3) || (channel < 0 ) || (channel > 1))
  1228.         return -1;
  1229.     if(output ==1)
  1230.     {
  1231.       if(channel ==0)
  1232.       {
  1233.           internal = 0x03;      
  1234.       } else {
  1235.           internal = 0x04;
  1236.       }
  1237.     }
  1238.     if(output ==2)
  1239.     {
  1240.         if(channel ==0)
  1241.         {
  1242.           internal = 0x04;      
  1243.         } else {
  1244.           internal = 0x05;
  1245.         }
  1246.     }
  1247.     if(output ==3)
  1248.     {
  1249.         if(channel ==0)
  1250.         {
  1251.           internal = 0x05;      
  1252.         } else {
  1253.           internal = 0x06;
  1254.         }
  1255.     }
  1256.  
  1257.  
  1258. // Set invert bit
  1259.     if(invert ==1)
  1260.       internal = internal | 0x10;
  1261.     else
  1262.       internal = internal & 0x0F;
  1263.     // Set Latch Bit
  1264.     if(latch==1)
  1265.         internal = internal | 0x20;
  1266.     else
  1267.         internal = internal & 0x1F;
  1268. // Add new data to old
  1269.     if(output == 1)
  1270.     {
  1271.         Data = Data & 0xFFFF00;
  1272.         Data = Data | internal;
  1273.     }
  1274.     if(output == 2)
  1275.     {
  1276.         Data = Data & 0xFF00FF;
  1277.         Data = Data |(internal * 0x100);
  1278.     }
  1279.     if(output == 3)
  1280.     {
  1281.         Data = Data & 0x00FFFF;
  1282.         Data = Data | (internal * 0x10000) ;
  1283.     }
  1284.     CAMAC_register_write(hdev, 5, Data);
  1285.     ret = CAMAC_register_read(hdev,6,&Data);  
  1286. //Set Trigger
  1287.     if(trigger <0 || trigger > 7)
  1288.         return -1;
  1289.     if(channel ==0)
  1290.     {
  1291.         Data = Data & 0xFF00FFFF;
  1292.         internal = trigger * 0x10000;
  1293.         Data = Data | internal;
  1294.     } else {
  1295.         Data = Data & 0x00FFFFFF;
  1296.         internal = trigger * 0x1000000;
  1297.         Data = Data | internal;
  1298.     }
  1299.     ret = CAMAC_register_write(hdev, 6, Data);
  1300.     if(channel == 0)
  1301.     {
  1302. // Write Delay and Gate info
  1303.                 ret = CAMAC_register_read(hdev,13,&Data);  
  1304.                 Delay_ext= (Data & 0xffff0000);
  1305.                 Delay_ext+= ((delay/0x10000) & 0xffff);
  1306.                 internal = gate * 0x10000;
  1307.                 Data = internal + (delay & 0xffff);
  1308.                 ret=CAMAC_register_write(hdev,7,Data);
  1309. // Set coarse delay in DGG_Extended register
  1310.                 ret=CAMAC_register_write(hdev,13,Delay_ext);
  1311.     }
  1312.         else
  1313.         {
  1314.                 ret=CAMAC_register_write(hdev,8,Data);
  1315.                 ret = CAMAC_register_read(hdev,13,&Data);  
  1316.                 Delay_ext= (Data & 0x0000ffff);
  1317.                 Delay_ext+= (delay & 0xffff0000);
  1318.                 internal = gate * 0x10000;
  1319.                 Data = internal + (delay & 0xffff);
  1320. // Set coarse delay in DGG_Extended register
  1321.                 ret=CAMAC_register_write(hdev,13,Delay_ext);
  1322.     }
  1323.     return 1;
  1324. }
  1325.  
  1326. /*
  1327. ******** CAMAC_LED_settings ************************
  1328.  
  1329.   Writes a data word to the vmusb LED register
  1330.  
  1331.   Parameters:
  1332.     hdev: USB devcie handle returned from an open function
  1333.     LED: The number which corresponds to an LED values are:
  1334.             1 - for RED LED
  1335.             2 - for GREEN LED
  1336.             3 - for Yellow LED
  1337.     code: The LED aource selector code, valid values for each LED
  1338.           are listed in the manual
  1339.     invert: to invert the LED lighting
  1340.     latch: sets LED latch bit
  1341.  
  1342.   Returns:
  1343.     Number of bytes read from xxusb when successful
  1344.     Upon failure, a negative number
  1345. */
  1346. short   CAMAC_LED_settings(usb_dev_handle *hdev, int LED, int code, int invert, int latch)
  1347. {
  1348.  
  1349.     short ret;
  1350. //    Uint32_t internal;
  1351.     Uint32_t Data;
  1352.    
  1353.     if( (LED <1) ||(LED > 3) || (code < 0) || (code > 7))
  1354.         return -1;
  1355.        
  1356.     CAMAC_register_read(hdev,4,&Data);  
  1357.  
  1358.     if(LED == 1)  
  1359.     {
  1360.         Data = Data & 0xFFFF00;
  1361.         Data = Data | code;
  1362.         if (invert == 1 && latch == 1)  
  1363.             Data = Data | 0x30;
  1364.         if (invert == 1 && latch == 0)
  1365.             Data = Data | 0x10;
  1366.         if (invert == 0 && latch == 1)
  1367.             Data = Data | 0x20;
  1368.     }
  1369.     if(LED == 2)  
  1370.     {
  1371.         Data = Data & 0xFF00FF;
  1372.         Data = Data | (code * 0x0100);
  1373.         if (invert == 1 && latch == 1)  
  1374.             Data = Data | 0x3000;
  1375.         if (invert == 1 && latch == 0)
  1376.             Data = Data | 0x1000;
  1377.         if (invert == 0 && latch == 1)
  1378.             Data = Data | 0x2000;
  1379.     }      
  1380.     if(LED == 3)
  1381.     {
  1382.         Data = Data & 0x00FFFF;
  1383.         Data = Data | (code * 0x10000);
  1384.         if (invert == 1 && latch == 1)  
  1385.             Data = Data | 0x300000;
  1386.         if (invert == 1 && latch == 0)
  1387.             Data = Data | 0x100000;
  1388.         if (invert == 0 && latch == 1)
  1389.             Data = Data | 0x200000;
  1390.     }  
  1391.     ret = CAMAC_register_write(hdev, 4, Data);
  1392.     return ret;
  1393. }
  1394.  
  1395. /*
  1396. ******** CAMAC_Output_settings ************************
  1397.  
  1398.   Writes a data word to the vmusb LED register
  1399.  
  1400.   Parameters:
  1401.     hdev: USB devcie handle returned from an open function
  1402.     Channel: The number which corresponds to an output:
  1403.             1 - for Output 1
  1404.             2 - for Output 2
  1405.             3 - for Output 3
  1406.     code: The Output selector code, valid values
  1407.           are listed in the manual
  1408.     invert: to invert the output
  1409.     latch: sets latch bit
  1410.  
  1411.   Returns:
  1412.     Number of bytes read from xxusb when successful
  1413.     Upon failure, a negative number
  1414. */
  1415. short   CAMAC_Output_settings(usb_dev_handle *hdev, int Channel, int code, int invert, int latch)
  1416. {
  1417.     short ret;
  1418. //    Uint32_t internal;
  1419.     Uint32_t Data;
  1420.    
  1421.     if( (Channel <1) ||(Channel > 3) || (code < 0) || (code > 7))
  1422.         return -1;
  1423.        
  1424.     CAMAC_register_read(hdev,5,&Data);  
  1425.  
  1426.     if(Channel == 1)  
  1427.     {
  1428.         Data = Data & 0xFFFF00;
  1429.         Data = Data | code;
  1430.         if (invert == 1 && latch == 1)  
  1431.             Data = Data | 0x30;
  1432.         if (invert == 1 && latch == 0)
  1433.             Data = Data | 0x10;
  1434.         if (invert == 0 && latch == 1)
  1435.             Data = Data | 0x20;
  1436.     }    
  1437.     if(Channel == 2)  
  1438.     {
  1439.         Data = Data & 0xFF00FF;
  1440.         Data = Data | (code * 0x0100);
  1441.         if (invert == 1 && latch == 1)  
  1442.             Data = Data | 0x3000;
  1443.         if (invert == 1 && latch == 0)
  1444.             Data = Data | 0x1000;
  1445.         if (invert == 0 && latch == 1)
  1446.             Data = Data | 0x2000;
  1447.     }  
  1448.     if(Channel == 3)
  1449.     {
  1450.         Data = Data & 0x00FFFF;
  1451.         Data = Data | (code * 0x10000);
  1452.         if (invert == 1 && latch == 1)  
  1453.             Data = Data | 0x300000;
  1454.         if (invert == 1 && latch == 0)
  1455.             Data = Data | 0x100000;
  1456.         if (invert == 0 && latch == 1)
  1457.             Data = Data | 0x200000;
  1458.     }
  1459.     ret = CAMAC_register_write(hdev, 5, Data);
  1460.     return ret;
  1461. }
  1462.  
  1463. /*
  1464. ******** CAMAC_write_LAM_mask ************************
  1465.  
  1466.   Writes the data word to the LAM mask register
  1467.  
  1468.   Parameters:
  1469.     hdev: USB devcie handle returned from an open function
  1470.     Data: LAM mask to write
  1471.  
  1472.   Returns:
  1473.     Number of bytes read from xxusb when successful
  1474.     Upon failure, a negative number
  1475. */
  1476. short   CAMAC_write_LAM_mask(usb_dev_handle *hdev, Uint32_t Data)
  1477. {
  1478.     short ret;
  1479.     ret = CAMAC_register_write(hdev, 9, Data);
  1480.  
  1481.     return ret;
  1482. }
  1483.  
  1484. /*
  1485. ******** CAMAC_read_LAM_mask ************************
  1486.  
  1487.   Writes the data word to the LAM mask register
  1488.  
  1489.   Parameters:
  1490.     hdev: USB devcie handle returned from an open function
  1491.     Data: LAM mask to write
  1492.  
  1493.   Returns:
  1494.     Number of bytes read from xxusb when successful
  1495.     Upon failure, a negative number
  1496. */
  1497. short   CAMAC_read_LAM_mask(usb_dev_handle *hdev, Uint32_t *Data)
  1498. {
  1499.     Uint32_t intbuf[4];  
  1500.     int ret;
  1501.     int N = 25;
  1502.     int F = 0;
  1503.     int A = 9;
  1504.  
  1505.     // CAMAC direct read function
  1506.     intbuf[0]=1;
  1507.     intbuf[1]=(Uint32_t)(F+A*32+N*512 + 0x4000);
  1508.     ret = xxusb_stack_execute(hdev, intbuf);
  1509.     *Data=intbuf[0] + (intbuf[1] & 255) * 0x10000;    
  1510.         return ret;
  1511. }
  1512.  
  1513.  
  1514. //******************************************************//
  1515. //**************** EZ_CAMAC Functions ******************//
  1516. //******************************************************//
  1517. // The following are functions used to perform simple
  1518. // CAMAC Functions with the CC_USB
  1519.  
  1520.  
  1521. /*
  1522. ******** CAMAC_write ************************
  1523.  
  1524.   Performs a CAMAC write using NAF comments
  1525.  
  1526.   Parameters:
  1527.     hdev: USB device handle returned from an open function
  1528.     N: CAMAC Station Number
  1529.     A: CAMAC Subaddress
  1530.     F: CAMAC Function (16...23)
  1531.     Q: The Q response from the CAMAC dataway
  1532.     X: The comment accepted response from CAMAC dataway
  1533.    
  1534.   Returns:
  1535.     Number of bytes written to xxusb when successful
  1536.     Upon failure, a negative number    
  1537. */
  1538. short   CAMAC_write(usb_dev_handle *hdev, int N, int A, int F, Uint32_t Data, int *Q, int *X)
  1539. {
  1540.     Uint32_t intbuf[4];  
  1541.     int ret;
  1542. // CAMAC direct write function
  1543.     intbuf[0]=1;
  1544.     intbuf[1]=(Uint32_t)(F+A*32+N*512 + 0x4000);
  1545.     if ((F > 15) && (F < 24))
  1546.     {  
  1547.         intbuf[0]=3;
  1548.         intbuf[2]=(Data & 0xffff);
  1549.         intbuf[3]=((Data >>16) & 255);
  1550.         ret = xxusb_stack_execute(hdev, intbuf);
  1551.         *Q = (intbuf[0] & 1);
  1552.         *X = ((intbuf[0] >> 1) & 1);
  1553.     }  
  1554.     return ret;
  1555. }
  1556.  
  1557. /*
  1558. ******** CAMAC_read ************************
  1559.  
  1560.   Performs a CAMAC read using NAF comments
  1561.  
  1562.   Parameters:
  1563.     hdev: USB device handle returned from an open function
  1564.     N: CAMAC Station Number
  1565.     A: CAMAC Subaddress
  1566.     F: CAMAC Function (F<16 or F>23)
  1567.     Q: The Q response from the CAMAC dataway
  1568.     X: The comment accepted response from CAMAC dataway
  1569.    
  1570.   Returns:
  1571.     Number of bytes read from xxusb when successful
  1572.     Upon failure, a negative number  
  1573. */
  1574. short   CAMAC_read(usb_dev_handle *hdev, int N, int A, int F, Uint32_t *Data, int *Q, int *X)
  1575. {
  1576.     Uint32_t intbuf[4];  
  1577.     int ret;
  1578.     // CAMAC direct read function
  1579.     intbuf[0]=1;
  1580.     intbuf[1]=(Uint32_t)(F+A*32+N*512 + 0x4000);
  1581.     ret = xxusb_stack_execute(hdev, intbuf);
  1582.     if ((F < 16) || (F >23))
  1583.     {
  1584.         *Data=intbuf[0] + (intbuf[1] & 255) * 0x10000;   //24-bit word
  1585.         *Q = ((intbuf[1] >> 8) & 1);
  1586.         *X = ((intbuf[1] >> 9) & 1);
  1587.     }  
  1588.     return ret;
  1589. }
  1590.  
  1591. /*
  1592. ******** CAMAC_Z ************************
  1593.  
  1594.   Performs a CAMAC init
  1595.  
  1596.   Parameters:
  1597.     hdev: USB device handle returned from an open function
  1598.  
  1599.   Returns:
  1600.     Number of bytes written to xxusb when successful
  1601.     Upon failure, a negative number    
  1602. */
  1603. short   CAMAC_Z(usb_dev_handle *hdev)
  1604. {
  1605.     Uint32_t intbuf[4];  
  1606.     int  ret;
  1607. //  CAMAC Z = N(28) A(8) F(29)
  1608.     intbuf[0]=1;
  1609.     intbuf[1]=(Uint32_t)(29+8*32+28*512 + 0x4000);
  1610.     ret = xxusb_stack_execute(hdev, intbuf);
  1611.     return ret;
  1612. }
  1613.  
  1614. /*
  1615. ******** CAMAC_C ************************
  1616.  
  1617.   Performs a CAMAC clear
  1618.  
  1619.   Parameters:
  1620.     hdev: USB device handle returned from an open function
  1621.  
  1622.   Returns:
  1623.     Number of bytes written to xxusb when successful
  1624.     Upon failure, a negative number    
  1625. */
  1626. short   CAMAC_C(usb_dev_handle *hdev)
  1627. {
  1628.     Uint32_t intbuf[4];  
  1629.     int ret;
  1630.     intbuf[0]=1;
  1631.     intbuf[1]=(Uint32_t)(29+9*32+28*512 + 0x4000);
  1632.     ret = xxusb_stack_execute(hdev, intbuf);
  1633.     return ret;
  1634. }
  1635.  
  1636. /*
  1637. ******** CAMAC_I ************************
  1638.  
  1639.   Set CAMAC inhibit
  1640.  
  1641.   Parameters:
  1642.     hdev: USB device handle returned from an open function
  1643.  
  1644.   Returns:
  1645.     Number of bytes written to xxusb when successful
  1646.     Upon failure, a negative number    
  1647. */
  1648. short   CAMAC_I(usb_dev_handle *hdev, int inhibit)
  1649. {
  1650.     Uint32_t intbuf[4];  
  1651.     int  ret;
  1652.     intbuf[0]=1;
  1653.     if (inhibit) intbuf[1]=(Uint32_t)(24+9*32+29*512 + 0x4000);
  1654.     else intbuf[1]=(Uint32_t)(26+9*32+29*512 + 0x4000);
  1655.     ret = xxusb_stack_execute(hdev, intbuf);
  1656.     return ret;
  1657. }
  1658.  
  1659.  
  1660.