Subversion Repositories f9daq

Rev

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