Subversion Repositories f9daq

Rev

Rev 264 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
100 f9daq 1
#ifdef LINUX
2
#include <dlfcn.h>
3
#define LoadLibrary(x) dlopen( (x), RTLD_NOW );
4
#define GetProcAddress(x,y) dlsym((x),(y))
5
#define __stdcall
6
#define UCHAR unsigned char
7
#define HINSTANCE void *
8
#define  _VI_FUNC
9
#include <stdlib.h>
10
#include <stdio.h>
11
#include <stdint.h>
12
#include <string.h>
13
#endif
14
 
61 f9daq 15
#include "wusbvme_dll.h"
264 f9daq 16
#include <userint.h>
17
#include <utility.h>
61 f9daq 18
 
82 f9daq 19
usb_dev_handle *WUSB_udev;
20
 
61 f9daq 21
static HINSTANCE DLLHandle;
22
 
100 f9daq 23
 
24
short __stdcall WIENER_VMUSB_VME_R( uint16_t AM, uint16_t DW,  uint32_t VME_Address, uint32_t *Data);
25
 
26
short __stdcall WIENER_VMUSB_VME_W( uint16_t AM, uint16_t DW, uint32_t VME_Address, uint32_t Data);
27
 
28
short __stdcall WIENER_VMUSB_VME_MW( uint16_t AM, uint16_t DW, uint32_t VME_Address, uint32_t Data);
29
short __stdcall WIENER_VMUSB_VME_MWRST( void );
30
short __stdcall WIENER_VMUSB_VME_MWEXEC( void );
31
 
32
short __stdcall WIENER_VMUSB_VME_MR( uint16_t AM, uint16_t DW, uint32_t VME_Address, uint32_t *Data);
33
short __stdcall WIENER_VMUSB_VME_MRRST( void );
34
short __stdcall WIENER_VMUSB_VME_MREXEC(  uint32_t *Data  );
35
 
36
 
138 f9daq 37
#ifdef XXDLL
139 f9daq 38
#warning Using DLL ...
138 f9daq 39
 
100 f9daq 40
short __stdcall xxusb_register_read(usb_dev_handle *hDev, short RegAddr, long *RegData);
41
short __stdcall xxusb_stack_read(usb_dev_handle *hDev, short StackAddr, long *StackData);
42
short __stdcall xxusb_stack_write(usb_dev_handle *hDev, short StackAddr, long *StackData);
43
short __stdcall xxusb_stack_execute(usb_dev_handle *hDev, long *StackData);
44
short __stdcall xxusb_longstack_execute(usb_dev_handle *hDev, void *DataBuffer, int lDataLen, int timeout);
45
short __stdcall xxusb_register_write(usb_dev_handle *hDev, short RegAddr, long RegData);
46
short __stdcall xxusb_usbfifo_read(usb_dev_handle *hDev, long *DataBuffer, short lDataLen, int timeout);
47
short __stdcall xxusb_bulk_read(usb_dev_handle *hDev, char *DataBuffer, short lDataLen, int timeout);
48
short __stdcall xxusb_bulk_write(usb_dev_handle *hDev, char *DataBuffer, short lDataLen, int timeout);
49
short __stdcall xxusb_reset_toggle(usb_dev_handle *hDev);
50
 
51
short __stdcall xxusb_devices_find(xxusb_device_type *xxusbDev);
52
short __stdcall xxusb_device_close(usb_dev_handle *hDev);
53
usb_dev_handle* __stdcall xxusb_device_open(struct usb_device *dev);
54
short __stdcall xxusb_flash_program(usb_dev_handle *hDev, char *config, short nsect);
55
short __stdcall xxusb_flashblock_program(usb_dev_handle *hDev, UCHAR *config);
56
usb_dev_handle* __stdcall xxusb_serial_open(char *SerialString);
57
 
58
short __stdcall VME_register_write(usb_dev_handle *hdev, long VME_Address, long Data);
59
short __stdcall VME_register_read(usb_dev_handle *hdev, long VME_Address, long *Data);
60
short __stdcall VME_LED_settings(usb_dev_handle *hdev, int LED, int code, int invert, int latch);
61
 
62
short __stdcall VME_DGG(usb_dev_handle *hdev, unsigned short channel, unsigned short trigger,unsigned short output, long delay, unsigned short gate, unsigned short invert, unsigned short latch);
63
 
64
short __stdcall VME_Output_settings(usb_dev_handle *hdev, int Channel, int code, int invert, int latch);
65
 
66
short __stdcall VME_read_16(usb_dev_handle *hdev,short Address_Modifier, long VME_Address, long *Data);
67
short __stdcall VME_read_32(usb_dev_handle *hdev, short Address_Modifier, long VME_Address, long *Data);
68
short __stdcall VME_BLT_read_32(usb_dev_handle *hdev, short Address_Modifier, int count, long VME_Address, long Data[]);
69
short __stdcall VME_write_16(usb_dev_handle *hdev, short Address_Modifier, long VME_Address, long Data);
70
short __stdcall VME_write_32(usb_dev_handle *hdev, short Address_Modifier, long VME_Address, long Data);
71
 
72
 
125 f9daq 73
void _VI_FUNC WIENER_VMUSB_load (const char* module_path) {
264 f9daq 74
 
100 f9daq 75
  if (module_path == NULL) {
138 f9daq 76
    DLLHandle = LoadLibrary("c:\\Windows\\system32\\libxxusb.dll");
100 f9daq 77
  } else {
61 f9daq 78
    DLLHandle = LoadLibrary(module_path);
264 f9daq 79
                printf("Module Path %s\n", module_path);
100 f9daq 80
  }
88 f9daq 81
  if (!DLLHandle) {
264 f9daq 82
    printf ("\n\nFailed to Open libxxusb.dll \n");
138 f9daq 83
    printf("LastError 0x%x\n",GetLastError() );
264 f9daq 84
                MessagePopup ("ERROR", "Failed to load libxxusb.dll ");
125 f9daq 85
    return;
264 f9daq 86
  } else {
87
                printf ("\n\nSuccesfully Opened libxxusb.dll  \n");    
88
        }
100 f9daq 89
  if (!(xxusb_register_read_Ptr = (void *) GetProcAddress(DLLHandle,"xxusb_register_read"))) exit(1);
61 f9daq 90
  if (!(xxusb_stack_read_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_stack_read"))) exit(1);
91
  if (!(xxusb_stack_write_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_stack_write"))) exit(1);
92
  if (!(xxusb_stack_execute_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_stack_execute"))) exit(1);
82 f9daq 93
  if (!(xxusb_longstack_execute_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_longstack_execute"))) exit(1);
61 f9daq 94
  if (!(xxusb_register_write_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_register_write"))) exit(1);
95
  if (!(xxusb_usbfifo_read_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_usbfifo_read"))) exit(1);
96
  if (!(xxusb_bulk_read_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_bulk_read"))) exit(1);
97
  if (!(xxusb_bulk_write_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_bulk_write"))) exit(1);
98
  if (!(xxusb_reset_toggle_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_reset_toggle"))) exit(1);
99
 
100
  if (!(xxusb_devices_find_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_devices_find"))) exit(1);
101
  if (!(xxusb_device_close_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_device_close"))) exit(1);
102
  if (!(xxusb_device_open_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_device_open"))) exit(1);
103
  if (!(xxusb_flash_program_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_flash_program"))) exit(1);
104
  if (!(xxusb_flashblock_program_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_flashblock_program"))) exit(1);
105
  if (!(xxusb_serial_open_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_serial_open"))) exit(1);
106
  if (!(VME_register_write_Ptr = (void*) GetProcAddress(DLLHandle,"VME_register_write"))) exit(1);
107
  if (!(VME_register_read_Ptr = (void*) GetProcAddress(DLLHandle,"VME_register_read"))) exit(1);
108
  if (!(VME_LED_settings_Ptr = (void*) GetProcAddress(DLLHandle,"VME_LED_settings"))) exit(1);
109
  if (!(VME_DGG_Ptr = (void*) GetProcAddress(DLLHandle,"VME_DGG"))) exit(1);
110
  if (!(VME_Output_settings_Ptr = (void*) GetProcAddress(DLLHandle,"VME_Output_settings"))) exit(1);
111
  if (!(VME_read_16_Ptr = (void*) GetProcAddress(DLLHandle,"VME_read_16"))) exit(1);
112
  if (!(VME_read_32_Ptr = (void*) GetProcAddress(DLLHandle,"VME_read_32"))) exit(1);
113
  if (!(VME_BLT_read_32_Ptr = (void*) GetProcAddress(DLLHandle,"VME_BLT_read_32"))) exit(1);
114
  if (!(VME_write_16_Ptr = (void*) GetProcAddress(DLLHandle,"VME_write_16"))) exit(1);
115
  if (!(VME_write_32_Ptr = (void*) GetProcAddress(DLLHandle,"VME_write_32"))) exit(1);
116
}
117
 
138 f9daq 118
#else
139 f9daq 119
  #warning: "Using libxxusb: add libxxusb.c to source list ..."
138 f9daq 120
 
121
void _VI_FUNC WIENER_VMUSB_load (const char* module_path) {
122
 
123
}
124
#endif
125
 
125 f9daq 126
void _VI_FUNC WIENER_VMUSB_open (const char *serial) {
127
 
82 f9daq 128
  xxusb_device_type devices[100];
129
  struct usb_device *dev;
264 f9daq 130
  if (serial != NULL) {
125 f9daq 131
    WUSB_udev = xxusb_serial_open((char *) serial);
264 f9daq 132
  } else  {
125 f9daq 133
    //Find XX_USB devices and open the first one found
82 f9daq 134
    xxusb_devices_find(devices);
135
    dev = devices[0].usbdev;
125 f9daq 136
    WUSB_udev = xxusb_device_open(dev);
137
  }
82 f9daq 138
  // Make sure VM_USB opened OK
139
  if(!WUSB_udev) {
140
    printf ("\n\nFailed to Open VM_USB \n\n");
141
    return ;
142
  } else {
143
    printf("VM-USB Device open.!\n");
125 f9daq 144
  }
145
 
146
 
61 f9daq 147
}
148
 
125 f9daq 149
void _VI_FUNC WIENER_VMUSB_close (void) {
82 f9daq 150
  if (WUSB_udev) xxusb_device_close(WUSB_udev);
61 f9daq 151
}
82 f9daq 152
 
153
 
92 f9daq 154
uint32_t fStack[MAXSTACKSIZE];
125 f9daq 155
uint32_t fStackSize;
82 f9daq 156
 
125 f9daq 157
int WIENER_VMUSB_StackInit() {
158
  fStack[0]=1;
159
  fStackSize=1;
160
  return fStack[0];
82 f9daq 161
}
162
 
125 f9daq 163
void WIENER_VMUSB_StackClear() {
164
  WIENER_VMUSB_StackInit();
82 f9daq 165
}
166
 
307 f9daq 167
int WIENER_VMUSB_StackGetUInt16(int maxn, uint16_t *stack) {
82 f9daq 168
  int i;
125 f9daq 169
  uint16_t * u16Stack = (uint16_t *) fStack;
170
  for (i=0; i< fStackSize*2; i++) {
171
    if (i<maxn) stack[i]=u16Stack[i];
172
    else return -1;
82 f9daq 173
  }
92 f9daq 174
  return fStackSize*2;
82 f9daq 175
}
176
 
307 f9daq 177
int WIENER_VMUSB_StackGetUInt32(int maxn, uint32_t *stack) {
82 f9daq 178
  int i;
92 f9daq 179
  uint16_t * u16Stack = (uint16_t *) fStack;
125 f9daq 180
  for (i=0; i< fStackSize*2; i++) {
181
    if (i<maxn) stack[i]=u16Stack[i];
182
    else return -1;
82 f9daq 183
  }
92 f9daq 184
  return fStackSize*2;
82 f9daq 185
}
186
 
187
 
125 f9daq 188
int WIENER_VMUSB_StackAppend(int cmd, uint32_t addr ) {
92 f9daq 189
  if (fStackSize+2 >= MAXSTACKSIZE) {
190
    fprintf(stderr,"WIENER_VMUSB_StackAppend Maximum stack size reached! Increase....%d\n", fStackSize+2);
82 f9daq 191
    return -1;
192
  }
92 f9daq 193
  fStack[fStackSize ++]   = cmd;
194
  fStack[fStackSize ++]   = addr;
82 f9daq 195
  fStack[0]+=4;
196
  return fStack[0];
197
}
198
 
125 f9daq 199
int WIENER_VMUSB_StackAppendData(int cmd,uint32_t addr ,uint32_t data) {
200
 
82 f9daq 201
  WIENER_VMUSB_StackAppend( cmd, addr );
125 f9daq 202
 
92 f9daq 203
  if (fStackSize+1 >= MAXSTACKSIZE) {
204
    fprintf(stderr,"WIENER_VMUSB_StackAppendData Maximum stack size reached! Increase....%d\n", fStackSize+1);
82 f9daq 205
    return -1;
206
  }
92 f9daq 207
  fStack[fStackSize++]  =  data;
82 f9daq 208
  fStack[0]+=2;
209
  return fStack[0];
210
}
211
 
125 f9daq 212
void WIENER_VMUSB_StackConditionalRead(int adr_mod,int d16d32, uint32_t addr,uint32_t bmask) {
213
  WIENER_VMUSB_StackAppend(CMD_READ| CMD_HD | adr_mod ,addr);
214
  WIENER_VMUSB_StackAppendData(CMD_READ| CMD_HD | CMD_HM |adr_mod, d16d32 |addr,bmask);
82 f9daq 215
}
216
 
125 f9daq 217
void WIENER_VMUSB_StackMultiRead(int adr_mod,int d16d32,uint32_t baseaddr, int n, uint32_t increment) {
218
  int i;
219
  for (i=0; i<n; i++) WIENER_VMUSB_StackAppend( adr_mod | CMD_READ, d16d32 +baseaddr + i*increment);
82 f9daq 220
}
125 f9daq 221
void WIENER_VMUSB_StackMultiWrite(int adr_mod,int d16d32,uint32_t baseaddr, int n, uint32_t increment, uint32_t data) {
222
  int i;
223
  for (i=0; i<n; i++)  WIENER_VMUSB_StackAppendData( adr_mod | CMD_WRITE,d16d32 + baseaddr+i*increment, data);
82 f9daq 224
}
225
 
125 f9daq 226
void WIENER_VMUSB_StackPrint() {
227
  uint16_t * u16Stack = (uint16_t *) fStack;
228
  uint32_t i;
229
  for (i=0; i < fStackSize*2; i++) printf("0x%04x\n",u16Stack[i]);
230
  printf("size of stack %d __________________________________\n",fStackSize*2-1);
82 f9daq 231
}
232
 
233
 
234
char wusbcc_stack[MAXSTACKSIZE*4];
235
 
236
 
125 f9daq 237
int _VI_FUNC WUSBXX_stack_execute (usb_dev_handle * dev, char * stack) {
82 f9daq 238
  int i, nb=0;
92 f9daq 239
  if (!dev) {
125 f9daq 240
    printf("WUSBXX_stack_execute device not open\n");
92 f9daq 241
    return -1;
125 f9daq 242
  }
82 f9daq 243
  uint16_t * exstack = (uint16_t *) wusbcc_stack;
244
  exstack[0]=0;
307 f9daq 245
  nb = WIENER_VMUSB_StackGetUInt16(MAXSTACKSIZE, &exstack[1] );
92 f9daq 246
  //printf("WUSBXX_stack_execute stack size %d\n", nb);
82 f9daq 247
  //WIENER_VMUSB_StackPrint();
248
  nb = xxusb_longstack_execute (dev,  exstack, nb, 1000);
92 f9daq 249
  //DataBuffer(0 - (ReturnValue/2-1)) - (unsigned short)array of returned data when ReturnValue>0
125 f9daq 250
  //printf("WUSBXX_stack_execute return size %d\n", nb);
92 f9daq 251
  if (nb<0 ) printf("xxusb_longstack_execute %s line:%d err=%d %s\n",__FILE__,__LINE__,nb, strerror(-nb) );
125 f9daq 252
  if (stack!=NULL) for ( i=0; i<nb; i++) stack[i]=wusbcc_stack[i];
82 f9daq 253
  return nb/sizeof(uint32_t);
254
}
255
 
256
 
257
 
264 f9daq 258
 
259
 
82 f9daq 260
#ifndef VME_D32
261
 
262
#define VME_D8  0x1
263
#define VME_D16 0x2
264
#define VME_D32 0x4
265
#endif
266
 
267
 
125 f9daq 268
short __stdcall WIENER_VMUSB_VME_R( uint16_t AM, uint16_t DW,  uint32_t VME_Address, uint32_t *Data) {
269
  int nb=0;
270
  if (!WUSB_udev) return -222;
271
 
272
  switch (DW) {
273
    case VME_D16:
274
      nb=  VME_read_16(WUSB_udev, AM,VME_Address,(long int *) Data);
275
      break;
276
    case VME_D32:
277
      nb=  VME_read_32(WUSB_udev, AM,VME_Address,(long int *)Data);
278
      break;
279
    default:
280
      return 0;
281
  }
82 f9daq 282
//printf("R 0x%02x 0x%02x 0x%08x %x\n", AM , DW, VME_Address, *Data);
125 f9daq 283
  if (nb<0)  printf("%s %d %d %s\n",__FILE__,__LINE__,nb, strerror(-nb) );
82 f9daq 284
 
136 f9daq 285
  return (short)nb;
82 f9daq 286
}
287
 
125 f9daq 288
short __stdcall WIENER_VMUSB_VME_W( uint16_t AM, uint16_t DW, uint32_t VME_Address, uint32_t Data) {
289
  int nb=0;
290
  if (!WUSB_udev) return -222;
291
//  printf("W 0x%02x 0x%02x 0x%08x %x\n", AM , DW, VME_Address, Data);
292
  switch (DW) {
293
    case VME_D16:
294
      nb=    VME_write_16(WUSB_udev, AM,VME_Address,Data);
295
      break;
296
    case VME_D32:
297
      nb=    VME_write_32(WUSB_udev, AM,VME_Address,Data);
298
      break;
299
    default:
300
      return 0;
301
  }
302
  if (nb<0)  printf("%s %d %d %s\n",__FILE__,__LINE__,nb, strerror(-nb) );
82 f9daq 303
 
136 f9daq 304
  return (short)nb;
82 f9daq 305
}
306
 
125 f9daq 307
short __stdcall WIENER_VMUSB_VME_MW( uint16_t AM, uint16_t DW, uint32_t VME_Address, uint32_t Data) {
308
  switch (DW) {
309
    case VME_D16:
310
      return  (short) WIENER_VMUSB_StackAppendData(CMD_WRITE | AM , VME_Address | CMD_D16 , Data);
311
    case VME_D32:
312
      return  (short) WIENER_VMUSB_StackAppendData(CMD_WRITE | AM , VME_Address | CMD_D32 , Data);
313
    default:
314
      return 0;
315
  }
316
  return 0;
82 f9daq 317
}
125 f9daq 318
short __stdcall WIENER_VMUSB_VME_MWRST( void ) {
319
  WIENER_VMUSB_StackInit();
320
  return 0;
82 f9daq 321
}
125 f9daq 322
short __stdcall WIENER_VMUSB_VME_MWEXEC( void ) {
136 f9daq 323
  return (short) WUSBXX_stack_execute (WUSB_udev, NULL);
82 f9daq 324
}
325
 
125 f9daq 326
short __stdcall WIENER_VMUSB_VME_MR( uint16_t AM, uint16_t DW, uint32_t VME_Address, uint32_t *Data) {
327
  switch (DW) {
328
    case VME_D16:
329
      return  (short) WIENER_VMUSB_StackAppend(CMD_READ | AM , VME_Address | CMD_D16 );
330
    case VME_D32:
331
      return  (short) WIENER_VMUSB_StackAppend(CMD_READ | AM , VME_Address | CMD_D32 );
332
    default:
333
      return 0;
334
  }
335
  return 0;
82 f9daq 336
}
125 f9daq 337
short __stdcall WIENER_VMUSB_VME_MRRST( void ) {
338
  WIENER_VMUSB_StackInit();
339
  return 0;
82 f9daq 340
}
125 f9daq 341
short __stdcall WIENER_VMUSB_VME_MREXEC(  uint32_t *Data  ) {
136 f9daq 342
  return (short) WUSBXX_stack_execute (WUSB_udev,(char *) Data);
82 f9daq 343
}