Subversion Repositories f9daq

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
145 f9daq 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
 
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 long stacks, up to 4 MBytes long, 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 long
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 long integers (32 bits).
96
        The latter option of passing a pointer to an array of unsigned long 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 long 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 long
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 long 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 long integers.
144
 
145
  Paramters:
146
    hdev: USB device handle returned from an open function
147
    DataBuffer: pointer to an array of long 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
 
204
    Negative numbers if the call fails
205
*/
206
short   xxusb_register_write(usb_dev_handle *hDev, short RegAddr, long RegData)
207
{
208
    long 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, long *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, long *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, long *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 long 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, long *RegData)
386
{
387
//long 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
    long 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[7];
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
               udev = xxusb_device_open(dev);
633
               if (udev)
634
               {
635
                   ret = usb_get_string_simple(udev, dev->descriptor.iSerialNumber, string, sizeof(string));
636
                                        if (ret >0 )  
637
                                                {
638
                                                if (strcmp(string,SerialString)==0)
639
                                                                return udev;
640
                                                }
641
                                                usb_close(udev);
642
                                }
643
           }
644
       }
645
    }
646
   udev = NULL;
647
   return udev;
648
}
649
 
650
 
651
//******************************************************//
652
//****************** EZ_VME Functions ******************//
653
//******************************************************//
654
// The following are functions used to perform simple
655
// VME Functions with the VM_USB
656
 
657
/*
658
******** VME_write_32 ************************
659
 
660
  Writes a 32 bit data word to the VME bus
661
 
662
  Parameters:
663
    hdev: USB devcie handle returned from an open function
664
    Address_Modifier: VME address modifier for the VME call
665
    VME_Address: Address to write the data to
666
    Data: 32 bit data word to be written to VME_Address
667
 
668
  Returns:
669
    Number of bytes read from xxusb when successful
670
    Upon failure, a negative number
671
*/
672
short   VME_write_32(usb_dev_handle *hdev, short Address_Modifier, long VME_Address, long Data)
673
{
674
    long intbuf[1000];
675
    short ret;
676
    intbuf[0]=7;
677
    intbuf[1]=0;
678
    intbuf[2]=Address_Modifier;
679
    intbuf[3]=0;
680
    intbuf[4]=(VME_Address & 0xffff);
681
    intbuf[5]=((VME_Address >>16) & 0xffff);
682
    intbuf[6]=(Data & 0xffff);
683
    intbuf[7]=((Data >> 16) & 0xffff);
684
    ret = xxusb_stack_execute(hdev, intbuf);
685
    return ret;
686
}
687
 
688
/*
689
******** VME_read_32 ************************
690
 
691
 
692
  Reads a 32 bit data word from a VME address
693
 
694
  Parameters:
695
    hdev: USB devcie handle returned from an open function
696
    Address_Modifier: VME address modifier for the VME call
697
    VME_Address: Address to read the data from
698
    Data: 32 bit data word read from VME_Address
699
 
700
  Returns:
701
    Number of bytes read from xxusb when successful
702
    Upon failure, a negative number
703
*/
704
short   VME_read_32(usb_dev_handle *hdev, short Address_Modifier, long VME_Address, long *Data)
705
{
706
    long intbuf[1000];
707
    short ret;
708
    intbuf[0]=5;
709
    intbuf[1]=0;
710
    intbuf[2]=Address_Modifier +0x100;
711
    intbuf[3]=0;
712
    intbuf[4]=(VME_Address & 0xffff);
713
    intbuf[5]=((VME_Address >>16) & 0xffff);
714
    ret = xxusb_stack_execute(hdev, intbuf);
715
    *Data=intbuf[0] + (intbuf[1] * 0x10000);
716
    return ret;
717
}
718
 
719
/*
720
******** VME_write_16 ************************
721
 
722
  Writes a 16 bit data word to the VME bus
723
 
724
  Parameters:
725
    hdev: USB devcie handle returned from an open function
726
    Address_Modifier: VME address modifier for the VME call
727
    VME_Address: Address to write the data to
728
    Data: word to be written to VME_Address
729
 
730
  Returns:
731
    Number of bytes read from xxusb when successful
732
    Upon failure, a negative number
733
*/
734
short   VME_write_16(usb_dev_handle *hdev, short Address_Modifier, long VME_Address, long Data)
735
{
736
    long intbuf[1000];
737
    short ret;
738
    intbuf[0]=7;
739
    intbuf[1]=0;
740
    intbuf[2]=Address_Modifier;
741
    intbuf[3]=0;
742
    intbuf[4]=(VME_Address & 0xffff)+ 0x01;
743
    intbuf[5]=((VME_Address >>16) & 0xffff);
744
    intbuf[6]=(Data & 0xffff);
745
    intbuf[7]=0;
746
    ret = xxusb_stack_execute(hdev, intbuf);
747
    return ret;
748
}
749
 
750
/*
751
******** VME_read_16 ************************
752
 
753
  Reads a 16 bit data word from a VME address
754
 
755
  Parameters:
756
    hdev: USB devcie handle returned from an open function
757
    Address_Modifier: VME address modifier for the VME call
758
    VME_Address: Address to read the data from
759
    Data: word read from VME_Address
760
 
761
  Returns:
762
    Number of bytes read from xxusb when successful
763
    Upon failure, a negative number
764
*/
765
short   VME_read_16(usb_dev_handle *hdev,short Address_Modifier, long VME_Address, long *Data)
766
{
767
    long intbuf[1000];
768
    short ret;
769
    intbuf[0]=5;
770
    intbuf[1]=0;
771
    intbuf[2]=Address_Modifier +0x100;
772
    intbuf[3]=0;
773
    intbuf[4]=(VME_Address & 0xffff)+ 0x01;
774
    intbuf[5]=((VME_Address >>16) & 0xffff);
775
    ret = xxusb_stack_execute(hdev, intbuf);
776
    *Data=intbuf[0];
777
    return ret;
778
}
779
 
780
/*
781
******** VME_BLT_read_32 ************************
782
 
783
  Performs block transfer of 32 bit words from a VME address
784
 
785
  Parameters:
786
    hdev: USB devcie handle returned from an open function
787
    Address_Modifier: VME address modifier for the VME call
788
    count: number of data words to read
789
    VME_Address: Address to read the data from
790
    Data: pointer to an array to hold the data words
791
 
792
  Returns:
793
    Number of bytes read from xxusb when successful
794
    Upon failure, a negative number
795
*/
796
short   VME_BLT_read_32(usb_dev_handle *hdev, short Adress_Modifier, int count, long VME_Address, long Data[])
797
{
798
    long intbuf[1000];
799
    short ret;
800
    int i=0;
801
    if (count > 255) return -1;
802
    intbuf[0]=5;
803
    intbuf[1]=0;
804
    intbuf[2]=Adress_Modifier +0x100;
805
    intbuf[3]=(count << 8);
806
    intbuf[4]=(VME_Address & 0xffff);
807
    intbuf[5]=((VME_Address >>16) & 0xffff);
808
    ret = xxusb_stack_execute(hdev, intbuf);
809
    int j=0;
810
    for (i=0;i<(2*count);i=i+2)
811
    {
812
        Data[j]=intbuf[i] + (intbuf[i+1] * 0x10000);
813
        j++;
814
    }
815
    return ret;
816
}
817
 
818
//******************************************************//
819
//****************** VM_USB Registers ******************//
820
//******************************************************//
821
// The following are functions used to set the registers
822
// in the VM_USB
823
 
824
/*
825
******** VME_register_write ************************
826
 
827
  Writes to the vmusb registers that are accessible through
828
  VME style calls
829
 
830
  Parameters:
831
    hdev: USB devcie handle returned from an open function
832
    VME_Address: The VME Address of the internal register
833
    Data: Data to be written to VME_Address
834
 
835
  Returns:
836
    Number of bytes read from xxusb when successful
837
    Upon failure, a negative number  
838
*/
839
short   VME_register_write(usb_dev_handle *hdev, long VME_Address, long Data)
840
{
841
    long intbuf[1000];
842
    short ret;
843
 
844
    intbuf[0]=7;
845
    intbuf[1]=0;
846
    intbuf[2]=0x1000;
847
    intbuf[3]=0;
848
    intbuf[4]=(VME_Address & 0xffff);
849
    intbuf[5]=((VME_Address >>16) & 0xffff);
850
    intbuf[6]=(Data & 0xffff);
851
    intbuf[7]=((Data >> 16) & 0xffff);
852
    ret = xxusb_stack_execute(hdev, intbuf);
853
    return ret;
854
}
855
 
856
/*
857
******** VME_register_read ************************
858
 
859
  Reads from the vmusb registers that are accessible trough VME style calls
860
 
861
  Parameters:
862
    hdev: USB devcie handle returned from an open function
863
    VME_Address: The VME Address of the internal register
864
    Data: Data read from VME_Address
865
 
866
  Returns:
867
    Number of bytes read from xxusb when successful
868
    Upon failure, a negative number
869
*/
870
short   VME_register_read(usb_dev_handle *hdev, long VME_Address, long *Data)
871
{
872
    long intbuf[1000];
873
    short ret;
874
 
875
    intbuf[0]=5;
876
    intbuf[1]=0;
877
    intbuf[2]=0x1100;
878
    intbuf[3]=0;
879
    intbuf[4]=(VME_Address & 0xffff);
880
    intbuf[5]=((VME_Address >>16) & 0xffff);
881
    ret = xxusb_stack_execute(hdev, intbuf);
882
    *Data=intbuf[0] + (intbuf[1] * 0x10000);
883
    return ret;
884
}
885
 
886
/*
887
******** VME_LED_settings ************************
888
 
889
  Sets the vmusb LED's
890
 
891
  Parameters:
892
    hdev: USB devcie handle returned from an open function
893
    LED: The number which corresponds to an LED values are:
894
 
895
                        1 - for RED LED
896
            2 - for GREEN LED
897
            3 - for Bottom YELLOW LED
898
    code: The LED aource selector code, valid values for each LED
899
          are listed in the manual
900
    invert: to invert the LED lighting
901
    latch: sets LED latch bit
902
 
903
  Returns:
904
    Number of bytes read from xxusb when successful
905
    Upon failure, a negative number
906
*/
907
short   VME_LED_settings(usb_dev_handle *hdev, int LED, int code, int invert, int latch)
908
{
909
    short ret;
910
//    long internal;
911
    long Data;
912
 
913
    if( (LED <0) ||(LED > 3) || (code < 0) || (code > 7))  return -1;
914
 
915
    VME_register_read(hdev,0xc,&Data);  
916
    if(LED == 0)  
917
    {
918
        Data = Data & 0xFFFFFF00;
919
        Data = Data | code;
920
        if (invert == 1 && latch == 1)   Data = Data | 0x18;
921
        if (invert == 1 && latch == 0)   Data = Data | 0x08;
922
        if (invert == 0 && latch == 1)   Data = Data | 0x10;
923
    }
924
    if(LED == 1)  
925
    {
926
        Data = Data & 0xFFFF00FF;
927
        Data = Data | (code * 0x0100);
928
        if (invert == 1 && latch == 1)  Data = Data | 0x1800;
929
        if (invert == 1 && latch == 0)  Data = Data | 0x0800;
930
        if (invert == 0 && latch == 1)  Data = Data | 0x1000;
931
    }  
932
    if(LED == 2)
933
    {
934
        Data = Data & 0xFF00FFFF;
935
        Data = Data | (code * 0x10000);
936
        if (invert == 1 && latch == 1) Data = Data | 0x180000;
937
        if (invert == 1 && latch == 0) Data = Data | 0x080000;
938
        if (invert == 0 && latch == 1) Data = Data | 0x100000;
939
    }
940
    if(LED == 3)
941
    {
942
        Data = Data & 0x00FFFFFF;
943
        Data = Data | (code * 0x10000);
944
        if (invert == 1 && latch == 1)  Data = Data | 0x18000000;
945
        if (invert == 1 && latch == 0)  Data = Data | 0x08000000;
946
        if (invert == 0 && latch == 1)  Data = Data | 0x10000000;
947
    }
948
    ret = VME_register_write(hdev, 0xc, Data);
949
    return ret;
950
}
951
 
952
/*
953
******** VME_DGG ************************
954
 
955
  Sets the parameters for Gate & Delay channel A of vmusb
956
 
957
  Parameters:
958
    hdev: USB devcie handle returned from an open function
959
    channel: Which DGG channel to use Valid Values are:
960
 
961
                          1 - For DGG B
962
    trigger:  Determines what triggers the start of the DGG Valid values are:
963
 
964
              1 - NIM input 1
965
              2 - NIM input 2
966
              3 - Event Trigger
967
              4 - End of Event
968
              5 - USB Trigger
969
              6 - Pulser
970
    output: Determines which NIM output to use for the channel, Vaild values are:
971
 
972
              1 - for NIM O2
973
    delay: 32 bit word consisting of
974
                        lower 16 bits: Delay_fine in steps of 12.5ns between trigger and start of gate
975
                        upper 16 bits: Delay_coarse in steps of 81.7us between trigger and start of gate
976
    gate: the time the gate should  stay open in steps of 12.5ns
977
    invert: is 1 if you wish to invert the DGG channel output
978
    latch: is 1 if you wish to run the DGG channel latched
979
 
980
  Returns:
981
    Returns 1 when successful
982
    Upon failure, a negative number
983
*/
984
short   VME_DGG(usb_dev_handle *hdev, unsigned short channel, unsigned short trigger, unsigned short output,
985
                                                long delay, unsigned short gate, unsigned short invert, unsigned short latch)
986
{
987
    long Data, DGData, Delay_ext;
988
    long internal;
989
    short ret;
990
 
991
 
992
    ret = VME_register_read(hdev, 0x10,  &Data);
993
    // check and correct values
994
    if(ret<=0) return -1;
995
 
996
    if(channel >1) channel =1;
997
    if(invert >1) invert =1;
998
    if(latch >1) latch =1;
999
        if(output >1) output =1;
1000
        if(trigger >6) trigger =0;
1001
 
1002
    // define Delay and Gate data
1003
    DGData = gate * 0x10000;
1004
    DGData += (unsigned short) delay;
1005
 
1006
        // Set channel, output, invert, latch
1007
        if (output == 0)
1008
    {
1009
                Data = Data & 0xFFFFFF00;
1010
                Data += 0x04 + channel +0x08*invert + 0x10*latch;
1011
    }
1012
        if (output == 1)
1013
    {
1014
                Data = Data & 0xFFFF00FF;
1015
                Data += (0x04 + channel +0x08*invert + 0x10*latch)*0x100;
1016
    }
1017
 
1018
    // Set trigger, delay, gate
1019
 
1020
    if(channel ==0) // CHANNEL DGG_A
1021
    {
1022
            internal = (trigger * 0x1000000) ;
1023
            Data= Data & 0xF0FFFFFF;
1024
            Data += internal;
1025
            ret = VME_register_write(hdev,0x10,Data);
1026
                if(ret<=0) return -1;
1027
                ret=VME_register_write(hdev,0x14,DGData);
1028
                if(ret<=0) return -1;
1029
    // Set coarse delay in DGG_Extended register
1030
            ret = VME_register_read(hdev,0x38,&Data);
1031
                Delay_ext= (Data & 0xffff0000);
1032
                Delay_ext+= ((delay/0x10000) & 0xffff);
1033
            ret = VME_register_write(hdev,0x38,Delay_ext);
1034
        }
1035
        if( channel ==1)  // CHANNEL DGG_B
1036
        {
1037
                internal = (trigger * 0x10000000) ;
1038
                Data= Data & 0x0FFFFFFF;
1039
                Data += internal;
1040
            ret = VME_register_write(hdev,0x10,Data);
1041
                if(ret<=0) return -1;
1042
                ret=VME_register_write(hdev,0x18,DGData);
1043
                if(ret<=0) return -1;
1044
    // Set coarse delay in DGG_Extended register
1045
            ret = VME_register_read(hdev,0x38,&Data);
1046
                Delay_ext= (Data & 0x0000ffff);
1047
                Delay_ext+= (delay & 0xffff0000);
1048
            ret = VME_register_write(hdev,0x38,Delay_ext);
1049
    }
1050
        return 1;
1051
 
1052
}
1053
 
1054
/*
1055
******** VME_Output_settings ************************
1056
 
1057
  Sets the vmusb NIM output register
1058
 
1059
  Parameters:
1060
    hdev: USB devcie handle returned from an open function
1061
    Channel: The number which corresponds to an output:
1062
            1 - for Output 1
1063
            2 - for Output 2
1064
    code: The Output selector code, valid values
1065
          are listed in the manual
1066
    invert: to invert the output
1067
    latch: sets latch bit
1068
 
1069
  Returns:
1070
    Number of bytes read from xxusb when successful
1071
    Upon failure, a negative number
1072
*/
1073
short   VME_Output_settings(usb_dev_handle *hdev, int Channel, int code, int invert, int latch)
1074
{
1075
 
1076
    short ret;
1077
//    long internal;
1078
    long Data;
1079
 
1080
    if( (Channel <1) ||(Channel > 2) || (code < 0) || (code > 7)) return -1;    
1081
    VME_register_read(hdev,0x10,&Data);  
1082
    if(Channel == 1)  
1083
    {
1084
        Data = Data & 0xFFFF00;
1085
        Data = Data | code;
1086
        if (invert == 1 && latch == 1)   Data = Data | 0x18;
1087
        if (invert == 1 && latch == 0)   Data = Data | 0x08;
1088
        if (invert == 0 && latch == 1)   Data = Data | 0x10;
1089
    }
1090
    if(Channel == 2)  
1091
    {
1092
        Data = Data & 0xFF00FF;
1093
        Data = Data | (code * 0x0100);
1094
        if (invert == 1 && latch == 1)   Data = Data | 0x1800;
1095
        if (invert == 1 && latch == 0)   Data = Data | 0x0800;
1096
        if (invert == 0 && latch == 1)   Data = Data | 0x1000;
1097
    }  
1098
    ret = VME_register_write(hdev, 0x10, Data);
1099
    return ret;
1100
}
1101
 
1102
 
1103
//******************************************************//
1104
//****************** CC_USB Registers ******************//
1105
//******************************************************//
1106
// The following are functions used to set the registers
1107
// in the CAMAC_USB
1108
 
1109
/*
1110
******** CAMAC_register_write *****************
1111
 
1112
  Performs a CAMAC write to CC_USB register
1113
 
1114
  Parameters:
1115
    hdev: USB device handle returned from an open function
1116
    A: CAMAC Subaddress
1117
    F: CAMAC Function
1118
    Data: data to be written
1119
 
1120
  Returns:
1121
    Number of bytes written to xxusb when successful
1122
    Upon failure, a negative number
1123
*/
1124
short   CAMAC_register_write(usb_dev_handle *hdev, int A, long Data)
1125
{
1126
    int F = 16;
1127
    int N = 25;
1128
    long intbuf[4];  
1129
    int ret;
1130
 
1131
    intbuf[0]=1;
1132
    intbuf[1]=(long)(F+A*32+N*512 + 0x4000);   
1133
        intbuf[0]=3;
1134
        intbuf[2]=(Data & 0xffff);
1135
        intbuf[3]=((Data >>16) & 0xffff);
1136
        ret = xxusb_stack_execute(hdev, intbuf);
1137
 
1138
    return ret;
1139
}
1140
 
1141
/*
1142
******** CAMAC_register_read ************************
1143
 
1144
  Performs a CAMAC read from CC_USB register
1145
 
1146
  Parameters:
1147
    hdev: USB device handle returned from an open function
1148
    N: CAMAC Station Number
1149
    A: CAMAC Subaddress
1150
    F: CAMAC Function
1151
    Q: The Q response from the CAMAC dataway
1152
    X: The comment accepted response from CAMAC dataway
1153
 
1154
  Returns:
1155
    Number of bytes read from xxusb when successful
1156
    Upon failure, a negative number
1157
*/
1158
short   CAMAC_register_read(usb_dev_handle *hdev, int A, long *Data)
1159
{
1160
    int F = 0;
1161
    int N = 25;
1162
    long intbuf[4];  
1163
    int ret;
1164
 
1165
    intbuf[0]=1;
1166
    intbuf[1]=(long)(F+A*32+N*512 + 0x4000);
1167
    ret = xxusb_stack_execute(hdev, intbuf);
1168
    *Data=intbuf[0] + (intbuf[1] * 0x10000);
1169
 
1170
    return ret;
1171
}
1172
 
1173
/*
1174
******** CAMAC_DGG ************************
1175
 
1176
  Sets the parameters for Gate & Delay channel A of CC_USB
1177
 
1178
  Parameters:
1179
    hdev: USB devcie handle returned from an open function
1180
    channel: Which DGG channel to use Valid Values are:
1181
 
1182
                                                        1 - For DGG B
1183
    trigger:  Determines what triggers the start of the DGG Valid values are:
1184
 
1185
              1 - NIM input 1
1186
              2 - NIM input 2
1187
              3 - NIM input 2
1188
              4 - Event Trigger
1189
                                                  5 - End of Event
1190
                                      6 - USB Trigger
1191
                                                        7 - Pulser
1192
    output: Determines which NIM output to use for the channel, Vaild values are:
1193
              1 - for NIM O1
1194
              2 - for NIM O2
1195
              3 - for NIM O3
1196
    delay: Delay in steps of 12.5ns between trigger and start of gate
1197
    gate: the time the gate should  stay open in steps of 12.5ns
1198
    invert: is 1 if you wish to invert the DGG channel output
1199
    latch: is 1 if you wish to run the DGG channel latched
1200
 
1201
  Returns:
1202
    Returns 1 when successful
1203
    Upon failure, a negative number
1204
*/
1205
short   CAMAC_DGG(usb_dev_handle *hdev, short channel, short trigger, short output,
1206
                                    int delay, int gate, short invert, short latch)
1207
 
1208
 
1209
 
1210
{
1211
//    short channel_ID;
1212
    long Data;
1213
    long internal;
1214
    short ret;
1215
                long Delay_ext;
1216
 
1217
    ret = CAMAC_register_read(hdev,5,&Data);
1218
    //Set trigger
1219
    if((output < 1 ) || (output >3) || (channel < 0 ) || (channel > 1))
1220
        return -1;
1221
    if(output ==1)
1222
    {
1223
      if(channel ==0)
1224
      {
1225
          internal = 0x03;      
1226
      } else {
1227
          internal = 0x04;
1228
      }
1229
    }
1230
    if(output ==2)
1231
    {
1232
        if(channel ==0)
1233
        {
1234
          internal = 0x04;      
1235
        } else {
1236
          internal = 0x05;
1237
        }
1238
    }
1239
    if(output ==3)
1240
    {
1241
        if(channel ==0)
1242
        {
1243
          internal = 0x05;      
1244
        } else {
1245
          internal = 0x06;
1246
        }
1247
    }
1248
 
1249
 
1250
// Set invert bit
1251
    if(invert ==1)
1252
      internal = internal | 0x10;
1253
    else
1254
      internal = internal & 0x0F;
1255
    // Set Latch Bit
1256
    if(latch==1)
1257
        internal = internal | 0x20;
1258
    else
1259
        internal = internal & 0x1F;
1260
// Add new data to old
1261
    if(output == 1)
1262
    {
1263
        Data = Data & 0xFFFF00;
1264
        Data = Data | internal;
1265
    }
1266
    if(output == 2)
1267
    {
1268
        Data = Data & 0xFF00FF;
1269
        Data = Data |(internal * 0x100);
1270
    }
1271
    if(output == 3)
1272
    {
1273
        Data = Data & 0x00FFFF;
1274
        Data = Data | (internal * 0x10000) ;
1275
    }
1276
    CAMAC_register_write(hdev, 5, Data);
1277
    ret = CAMAC_register_read(hdev,6,&Data);  
1278
//Set Trigger
1279
    if(trigger <0 || trigger > 7)
1280
        return -1;
1281
    if(channel ==0)
1282
    {
1283
        Data = Data & 0xFF00FFFF;
1284
        internal = trigger * 0x10000;
1285
        Data = Data | internal;
1286
    } else {
1287
        Data = Data & 0x00FFFFFF;
1288
        internal = trigger * 0x1000000;
1289
        Data = Data | internal;
1290
    }
1291
    ret = CAMAC_register_write(hdev, 6, Data);
1292
    if(channel == 0)
1293
    {
1294
// Write Delay and Gate info
1295
                ret = CAMAC_register_read(hdev,13,&Data);  
1296
                Delay_ext= (Data & 0xffff0000);
1297
                Delay_ext+= ((delay/0x10000) & 0xffff);
1298
                internal = gate * 0x10000;
1299
                Data = internal + (delay & 0xffff);
1300
                ret=CAMAC_register_write(hdev,7,Data);
1301
// Set coarse delay in DGG_Extended register
1302
                ret=CAMAC_register_write(hdev,13,Delay_ext);
1303
    }
1304
        else
1305
        {
1306
                ret=CAMAC_register_write(hdev,8,Data);
1307
                ret = CAMAC_register_read(hdev,13,&Data);  
1308
                Delay_ext= (Data & 0x0000ffff);
1309
                Delay_ext+= (delay & 0xffff0000);
1310
                internal = gate * 0x10000;
1311
                Data = internal + (delay & 0xffff);
1312
// Set coarse delay in DGG_Extended register
1313
                ret=CAMAC_register_write(hdev,13,Delay_ext);
1314
    }
1315
    return 1;
1316
}
1317
 
1318
/*
1319
******** CAMAC_LED_settings ************************
1320
 
1321
  Writes a data word to the vmusb LED register
1322
 
1323
  Parameters:
1324
    hdev: USB devcie handle returned from an open function
1325
    LED: The number which corresponds to an LED values are:
1326
            1 - for RED LED
1327
            2 - for GREEN LED
1328
            3 - for Yellow LED
1329
    code: The LED aource selector code, valid values for each LED
1330
          are listed in the manual
1331
    invert: to invert the LED lighting
1332
    latch: sets LED latch bit
1333
 
1334
  Returns:
1335
    Number of bytes read from xxusb when successful
1336
    Upon failure, a negative number
1337
*/
1338
short   CAMAC_LED_settings(usb_dev_handle *hdev, int LED, int code, int invert, int latch)
1339
{
1340
 
1341
    short ret;
1342
//    long internal;
1343
    long Data;
1344
 
1345
    if( (LED <1) ||(LED > 3) || (code < 0) || (code > 7))
1346
        return -1;
1347
 
1348
    CAMAC_register_read(hdev,4,&Data);  
1349
 
1350
    if(LED == 1)  
1351
    {
1352
        Data = Data & 0xFFFF00;
1353
        Data = Data | code;
1354
        if (invert == 1 && latch == 1)  
1355
            Data = Data | 0x30;
1356
        if (invert == 1 && latch == 0)
1357
            Data = Data | 0x10;
1358
        if (invert == 0 && latch == 1)
1359
            Data = Data | 0x20;
1360
    }
1361
    if(LED == 2)  
1362
    {
1363
        Data = Data & 0xFF00FF;
1364
        Data = Data | (code * 0x0100);
1365
        if (invert == 1 && latch == 1)  
1366
            Data = Data | 0x3000;
1367
        if (invert == 1 && latch == 0)
1368
            Data = Data | 0x1000;
1369
        if (invert == 0 && latch == 1)
1370
            Data = Data | 0x2000;
1371
    }      
1372
    if(LED == 3)
1373
    {
1374
        Data = Data & 0x00FFFF;
1375
        Data = Data | (code * 0x10000);
1376
        if (invert == 1 && latch == 1)  
1377
            Data = Data | 0x300000;
1378
        if (invert == 1 && latch == 0)
1379
            Data = Data | 0x100000;
1380
        if (invert == 0 && latch == 1)
1381
            Data = Data | 0x200000;
1382
    }  
1383
    ret = CAMAC_register_write(hdev, 4, Data);
1384
    return ret;
1385
}
1386
 
1387
/*
1388
******** CAMAC_Output_settings ************************
1389
 
1390
  Writes a data word to the vmusb LED register
1391
 
1392
  Parameters:
1393
    hdev: USB devcie handle returned from an open function
1394
    Channel: The number which corresponds to an output:
1395
            1 - for Output 1
1396
            2 - for Output 2
1397
            3 - for Output 3
1398
    code: The Output selector code, valid values
1399
          are listed in the manual
1400
    invert: to invert the output
1401
    latch: sets latch bit
1402
 
1403
  Returns:
1404
    Number of bytes read from xxusb when successful
1405
    Upon failure, a negative number
1406
*/
1407
short   CAMAC_Output_settings(usb_dev_handle *hdev, int Channel, int code, int invert, int latch)
1408
{
1409
    short ret;
1410
//    long internal;
1411
    long Data;
1412
 
1413
    if( (Channel <1) ||(Channel > 3) || (code < 0) || (code > 7))
1414
        return -1;
1415
 
1416
    CAMAC_register_read(hdev,5,&Data);  
1417
 
1418
    if(Channel == 1)  
1419
    {
1420
        Data = Data & 0xFFFF00;
1421
        Data = Data | code;
1422
        if (invert == 1 && latch == 1)  
1423
            Data = Data | 0x30;
1424
        if (invert == 1 && latch == 0)
1425
            Data = Data | 0x10;
1426
        if (invert == 0 && latch == 1)
1427
            Data = Data | 0x20;
1428
    }    
1429
    if(Channel == 2)  
1430
    {
1431
        Data = Data & 0xFF00FF;
1432
        Data = Data | (code * 0x0100);
1433
        if (invert == 1 && latch == 1)  
1434
            Data = Data | 0x3000;
1435
        if (invert == 1 && latch == 0)
1436
            Data = Data | 0x1000;
1437
        if (invert == 0 && latch == 1)
1438
            Data = Data | 0x2000;
1439
    }  
1440
    if(Channel == 3)
1441
    {
1442
        Data = Data & 0x00FFFF;
1443
        Data = Data | (code * 0x10000);
1444
        if (invert == 1 && latch == 1)  
1445
            Data = Data | 0x300000;
1446
        if (invert == 1 && latch == 0)
1447
            Data = Data | 0x100000;
1448
        if (invert == 0 && latch == 1)
1449
            Data = Data | 0x200000;
1450
    }
1451
    ret = CAMAC_register_write(hdev, 5, Data);
1452
    return ret;
1453
}
1454
 
1455
/*
1456
******** CAMAC_write_LAM_mask ************************
1457
 
1458
  Writes the data word to the LAM mask register
1459
 
1460
  Parameters:
1461
    hdev: USB devcie handle returned from an open function
1462
    Data: LAM mask to write
1463
 
1464
  Returns:
1465
    Number of bytes read from xxusb when successful
1466
    Upon failure, a negative number
1467
*/
1468
short   CAMAC_write_LAM_mask(usb_dev_handle *hdev, long Data)
1469
{
1470
    short ret;
1471
    ret = CAMAC_register_write(hdev, 9, Data);
1472
 
1473
    return ret;
1474
}
1475
 
1476
/*
1477
******** CAMAC_read_LAM_mask ************************
1478
 
1479
  Writes the data word to the LAM mask register
1480
 
1481
  Parameters:
1482
    hdev: USB devcie handle returned from an open function
1483
    Data: LAM mask to write
1484
 
1485
  Returns:
1486
    Number of bytes read from xxusb when successful
1487
    Upon failure, a negative number
1488
*/
1489
short   CAMAC_read_LAM_mask(usb_dev_handle *hdev, long *Data)
1490
{
1491
    long intbuf[4];  
1492
    int ret;
1493
    int N = 25;
1494
    int F = 0;
1495
    int A = 9;
1496
 
1497
    // CAMAC direct read function
1498
    intbuf[0]=1;
1499
    intbuf[1]=(long)(F+A*32+N*512 + 0x4000);
1500
    ret = xxusb_stack_execute(hdev, intbuf);
1501
    *Data=intbuf[0] + (intbuf[1] & 255) * 0x10000;    
1502
        return ret;
1503
}
1504
 
1505
 
1506
//******************************************************//
1507
//**************** EZ_CAMAC Functions ******************//
1508
//******************************************************//
1509
// The following are functions used to perform simple
1510
// CAMAC Functions with the CC_USB
1511
 
1512
 
1513
/*
1514
******** CAMAC_write ************************
1515
 
1516
  Performs a CAMAC write using NAF comments
1517
 
1518
  Parameters:
1519
    hdev: USB device handle returned from an open function
1520
    N: CAMAC Station Number
1521
    A: CAMAC Subaddress
1522
    F: CAMAC Function (16...23)
1523
    Q: The Q response from the CAMAC dataway
1524
    X: The comment accepted response from CAMAC dataway
1525
 
1526
  Returns:
1527
    Number of bytes written to xxusb when successful
1528
    Upon failure, a negative number    
1529
*/
1530
short   CAMAC_write(usb_dev_handle *hdev, int N, int A, int F, long Data, int *Q, int *X)
1531
{
1532
    long intbuf[4];  
1533
    int ret;
1534
// CAMAC direct write function
1535
    intbuf[0]=1;
1536
    intbuf[1]=(long)(F+A*32+N*512 + 0x4000);
1537
    if ((F > 15) && (F < 24))
1538
    {  
1539
        intbuf[0]=3;
1540
        intbuf[2]=(Data & 0xffff);
1541
        intbuf[3]=((Data >>16) & 255);
1542
        ret = xxusb_stack_execute(hdev, intbuf);
1543
        *Q = (intbuf[0] & 1);
1544
        *X = ((intbuf[0] >> 1) & 1);
1545
    }  
1546
    return ret;
1547
}
1548
 
1549
/*
1550
******** CAMAC_read ************************
1551
 
1552
  Performs a CAMAC read using NAF comments
1553
 
1554
  Parameters:
1555
    hdev: USB device handle returned from an open function
1556
    N: CAMAC Station Number
1557
    A: CAMAC Subaddress
1558
    F: CAMAC Function (F<16 or F>23)
1559
    Q: The Q response from the CAMAC dataway
1560
    X: The comment accepted response from CAMAC dataway
1561
 
1562
  Returns:
1563
    Number of bytes read from xxusb when successful
1564
    Upon failure, a negative number  
1565
*/
1566
short   CAMAC_read(usb_dev_handle *hdev, int N, int A, int F, long *Data, int *Q, int *X)
1567
{
1568
    long intbuf[4];  
1569
    int ret;
1570
    // CAMAC direct read function
1571
    intbuf[0]=1;
1572
    intbuf[1]=(long)(F+A*32+N*512 + 0x4000);
1573
    ret = xxusb_stack_execute(hdev, intbuf);
1574
    if ((F < 16) || (F >23))
1575
    {
1576
        *Data=intbuf[0] + (intbuf[1] & 255) * 0x10000;   //24-bit word 
1577
        *Q = ((intbuf[1] >> 8) & 1);
1578
        *X = ((intbuf[1] >> 9) & 1);
1579
    }  
1580
    return ret;
1581
}
1582
 
1583
/*
1584
******** CAMAC_Z ************************
1585
 
1586
  Performs a CAMAC init
1587
 
1588
  Parameters:
1589
    hdev: USB device handle returned from an open function
1590
 
1591
  Returns:
1592
    Number of bytes written to xxusb when successful
1593
    Upon failure, a negative number    
1594
*/
1595
short   CAMAC_Z(usb_dev_handle *hdev)
1596
{
1597
    long intbuf[4];  
1598
    int  ret;
1599
//  CAMAC Z = N(28) A(8) F(29)
1600
    intbuf[0]=1;
1601
    intbuf[1]=(long)(29+8*32+28*512 + 0x4000);
1602
    ret = xxusb_stack_execute(hdev, intbuf);
1603
    return ret;
1604
}
1605
 
1606
/*
1607
******** CAMAC_C ************************
1608
 
1609
  Performs a CAMAC clear
1610
 
1611
  Parameters:
1612
    hdev: USB device handle returned from an open function
1613
 
1614
  Returns:
1615
    Number of bytes written to xxusb when successful
1616
    Upon failure, a negative number    
1617
*/
1618
short   CAMAC_C(usb_dev_handle *hdev)
1619
{
1620
    long intbuf[4];  
1621
    int ret;
1622
    intbuf[0]=1;
1623
    intbuf[1]=(long)(29+9*32+28*512 + 0x4000);
1624
    ret = xxusb_stack_execute(hdev, intbuf);
1625
    return ret;
1626
}
1627
 
1628
/*
1629
******** CAMAC_I ************************
1630
 
1631
  Set CAMAC inhibit
1632
 
1633
  Parameters:
1634
    hdev: USB device handle returned from an open function
1635
 
1636
  Returns:
1637
    Number of bytes written to xxusb when successful
1638
    Upon failure, a negative number    
1639
*/
1640
short   CAMAC_I(usb_dev_handle *hdev, int inhibit)
1641
{
1642
    long intbuf[4];  
1643
    int  ret;
1644
    intbuf[0]=1;
1645
    if (inhibit) intbuf[1]=(long)(24+9*32+29*512 + 0x4000);
1646
    else intbuf[1]=(long)(26+9*32+29*512 + 0x4000);
1647
    ret = xxusb_stack_execute(hdev, intbuf);
1648
    return ret;
1649
}
1650
 
1651