Subversion Repositories f9daq

Rev

Blame | Last modification | View Log | RSS feed

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