Subversion Repositories f9daq

Rev

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

Rev Author Line No. Line
61 f9daq 1
#include "wusbvme_dll.h"
2
 
82 f9daq 3
 
4
usb_dev_handle *WUSB_udev;
5
 
61 f9daq 6
static HINSTANCE DLLHandle;
7
 
82 f9daq 8
void _VI_FUNC WIENER_VMUSB_load (char* module_path)
61 f9daq 9
{
10
  if (module_path == NULL)
11
    DLLHandle = LoadLibrary("libxxusb.dll");
12
   else
13
    DLLHandle = LoadLibrary(module_path);
88 f9daq 14
  if (!DLLHandle) {
15
       printf ("\n\nFailed to Open libxxusb.dll \n");  
16
          return;
17
  }
61 f9daq 18
  if (!(xxusb_register_read_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_register_read"))) exit(1);
19
  if (!(xxusb_stack_read_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_stack_read"))) exit(1);
20
  if (!(xxusb_stack_write_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_stack_write"))) exit(1);
21
  if (!(xxusb_stack_execute_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_stack_execute"))) exit(1);
82 f9daq 22
  if (!(xxusb_longstack_execute_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_longstack_execute"))) exit(1);
61 f9daq 23
  if (!(xxusb_register_write_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_register_write"))) exit(1);
24
  if (!(xxusb_usbfifo_read_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_usbfifo_read"))) exit(1);
25
  if (!(xxusb_bulk_read_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_bulk_read"))) exit(1);
26
  if (!(xxusb_bulk_write_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_bulk_write"))) exit(1);
27
  if (!(xxusb_reset_toggle_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_reset_toggle"))) exit(1);
28
 
29
  if (!(xxusb_devices_find_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_devices_find"))) exit(1);
30
  if (!(xxusb_device_close_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_device_close"))) exit(1);
31
  if (!(xxusb_device_open_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_device_open"))) exit(1);
32
  if (!(xxusb_flash_program_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_flash_program"))) exit(1);
33
  if (!(xxusb_flashblock_program_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_flashblock_program"))) exit(1);
34
  if (!(xxusb_serial_open_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_serial_open"))) exit(1);
35
  if (!(VME_register_write_Ptr = (void*) GetProcAddress(DLLHandle,"VME_register_write"))) exit(1);
36
  if (!(VME_register_read_Ptr = (void*) GetProcAddress(DLLHandle,"VME_register_read"))) exit(1);
37
  if (!(VME_LED_settings_Ptr = (void*) GetProcAddress(DLLHandle,"VME_LED_settings"))) exit(1);
38
  if (!(VME_DGG_Ptr = (void*) GetProcAddress(DLLHandle,"VME_DGG"))) exit(1);
39
  if (!(VME_Output_settings_Ptr = (void*) GetProcAddress(DLLHandle,"VME_Output_settings"))) exit(1);
40
  if (!(VME_read_16_Ptr = (void*) GetProcAddress(DLLHandle,"VME_read_16"))) exit(1);
41
  if (!(VME_read_32_Ptr = (void*) GetProcAddress(DLLHandle,"VME_read_32"))) exit(1);
42
  if (!(VME_BLT_read_32_Ptr = (void*) GetProcAddress(DLLHandle,"VME_BLT_read_32"))) exit(1);
43
  if (!(VME_write_16_Ptr = (void*) GetProcAddress(DLLHandle,"VME_write_16"))) exit(1);
44
  if (!(VME_write_32_Ptr = (void*) GetProcAddress(DLLHandle,"VME_write_32"))) exit(1);
45
}
46
 
82 f9daq 47
void _VI_FUNC WIENER_VMUSB_open (char *serial)
61 f9daq 48
{
82 f9daq 49
 
50
  xxusb_device_type devices[100];
51
  struct usb_device *dev;
61 f9daq 52
  if (serial != NULL)
82 f9daq 53
        WUSB_udev = xxusb_serial_open(serial);
54
  else  {
55
  //Find XX_USB devices and open the first one found
56
    xxusb_devices_find(devices);
57
    dev = devices[0].usbdev;
58
    WUSB_udev = xxusb_device_open(dev);
59
  }  
60
  // Make sure VM_USB opened OK
61
  if(!WUSB_udev) {
62
    printf ("\n\nFailed to Open VM_USB \n\n");
63
    return ;
64
  } else {
65
    printf("VM-USB Device open.!\n");
66
  }  
67
 
68
 
61 f9daq 69
}
70
 
82 f9daq 71
void _VI_FUNC WIENER_VMUSB_close (void)
61 f9daq 72
{
82 f9daq 73
  if (WUSB_udev) xxusb_device_close(WUSB_udev);
61 f9daq 74
}
82 f9daq 75
 
76
 
77
uint16_t fStack[MAXSTACKSIZE];
78
 
79
 
80
int WIENER_VMUSB_StackInit(){
81
        fStack[0]=1;
82
        fStack[1]=0;
83
   return fStack[0];
84
}
85
 
86
void WIENER_VMUSB_StackClear(){
87
        WIENER_VMUSB_StackInit();
88
}
89
 
90
int WIENER_VMUSB_StackGetUInt16(int maxn, uint16_t *stack){
91
  int i;
92
  for (i=0;i< fStack[0]+1;i++){
93
                        if (i<maxn) stack[i]=fStack[i]; else return -1;
94
  }
95
  return fStack[0]+1;
96
}
97
 
98
int WIENER_VMUSB_StackGetUInt32(int maxn, uint32_t *stack){
99
  int i;
100
  for (i=0;i< fStack[0]+1;i++){
101
                        if (i<maxn) stack[i]=fStack[i]; else return -1;
102
  }
103
  return fStack[0]+1;
104
}
105
 
106
 
107
int WIENER_VMUSB_StackAppend(int cmd, uint32_t addr ){
108
  int i= fStack[0]+1;
109
  if (i+4 >= MAXSTACKSIZE) {
110
    fprintf(stderr,"WIENER_VMUSB_StackAppend Maximum stack size reached! Increase....%d\n", i+4);
111
    return -1;
112
  }
113
  fStack[i]   = (cmd & 0xFFFF);
114
  fStack[i+1] = (cmd >> 16);
115
  fStack[i+2] = addr&0xFFFF;
116
  fStack[i+3] = (addr >> 16)& 0xFFFF;
117
  fStack[0]+=4;
118
  return fStack[0];
119
}
120
 
121
int WIENER_VMUSB_StackAppendData(int cmd,uint32_t addr ,uint32_t data){
122
  int i=0;
123
  WIENER_VMUSB_StackAppend( cmd, addr );
124
  i = fStack[0]+1;
125
  if (i+2 >= MAXSTACKSIZE) {
126
    fprintf(stderr,"WIENER_VMUSB_StackAppendData Maximum stack size reached! Increase....%d\n", i+4);
127
    return -1;
128
  }
129
  fStack[i]  =  data & 0xFFFF;
130
  fStack[i+1] = (data >> 16)&0xFFFF;
131
  fStack[0]+=2;
132
  return fStack[0];
133
}
134
 
135
void WIENER_VMUSB_StackConditionalRead(int adr_mod,int d16d32, uint32_t addr,uint32_t bmask){
136
        WIENER_VMUSB_StackAppend(CMD_READ| CMD_HD | adr_mod ,addr);
137
        WIENER_VMUSB_StackAppendData(CMD_READ| CMD_HD | CMD_HM |adr_mod, d16d32 |addr,bmask);
138
}
139
 
140
void WIENER_VMUSB_StackMultiRead(int adr_mod,int d16d32,uint32_t baseaddr, int n, uint32_t increment){
141
        int i;
142
        for (i=0;i<n;i++) WIENER_VMUSB_StackAppend( adr_mod | CMD_READ, d16d32 +baseaddr + i*increment);
143
}
144
void WIENER_VMUSB_StackMultiWrite(int adr_mod,int d16d32,uint32_t baseaddr, int n, uint32_t increment, uint32_t data){
145
        int i;
146
        for (i=0;i<n;i++)  WIENER_VMUSB_StackAppendData( adr_mod | CMD_WRITE,d16d32 + baseaddr+i*increment, data);
147
}
148
 
149
void WIENER_VMUSB_StackPrint(){
150
   uint32_t i;
151
   for (i=0; i < fStack[0]+1;i++) printf("0x%04x\n",fStack[i]);
152
   printf("size of stack %d __________________________________\n",fStack[0]+1);
153
}
154
 
155
 
156
char wusbcc_stack[MAXSTACKSIZE*4];
157
 
158
 
159
int _VI_FUNC WUSBXX_stack_execute (usb_dev_handle * dev, char * stack){
160
  int i, nb=0;
161
  if (!dev) return -1;
162
  uint16_t * exstack = (uint16_t *) wusbcc_stack;
163
  exstack[0]=0;
164
  nb = WIENER_VMUSB_StackGetUInt16(MAXSTACKSIZE, &exstack[1] );
165
  //printf("WUSBXX_stack_execute stack size %d ", nb);
166
  //WIENER_VMUSB_StackPrint();
167
  nb = xxusb_longstack_execute (dev,  exstack, nb, 1000);
168
 
169
 
170
  if (nb<0 ) printf("%s line:%d err=%d %s\n",__FILE__,__LINE__,nb, strerror(-nb) );
171
  if (stack!=NULL) for ( i=0;i<nb;i++) stack[i]=wusbcc_stack[i];
172
  return nb/sizeof(uint32_t);
173
}
174
 
175
 
176
 
177
#ifndef VME_D32
178
 
179
#define VME_D8  0x1
180
#define VME_D16 0x2
181
#define VME_D32 0x4
182
#endif
183
 
184
 
185
short __stdcall WIENER_VMUSB_VME_R( uint16_t AM, uint16_t DW,  uint32_t VME_Address, uint32_t *Data){
186
        int nb=0;
187
        if (!WUSB_udev) return -222;
188
 
189
    switch (DW){
190
        case VME_D16: nb=  VME_read_16(WUSB_udev, AM,VME_Address,Data);  break;
191
   case VME_D32: nb=  VME_read_32(WUSB_udev, AM,VME_Address,Data); break;
192
   default: return 0;
193
}
194
//printf("R 0x%02x 0x%02x 0x%08x %x\n", AM , DW, VME_Address, *Data);
195
if (nb<0)  printf("%s %d %d %s\n",__FILE__,__LINE__,nb, strerror(-nb) );
196
 
197
return nb;
198
}
199
 
200
short __stdcall WIENER_VMUSB_VME_W( uint16_t AM, uint16_t DW, uint32_t VME_Address, uint32_t Data){
201
        int nb=0;
202
        if (!WUSB_udev) return -222;
203
//      printf("W 0x%02x 0x%02x 0x%08x %x\n", AM , DW, VME_Address, Data);
204
 switch (DW){
205
   case VME_D16: nb=    VME_write_16(WUSB_udev, AM,VME_Address,Data); break;
206
   case VME_D32: nb=    VME_write_32(WUSB_udev, AM,VME_Address,Data); break;
207
   default: return 0;
208
}
209
if (nb<0)  printf("%s %d %d %s\n",__FILE__,__LINE__,nb, strerror(-nb) );
210
 
211
return nb;
212
}
213
 
214
short __stdcall WIENER_VMUSB_VME_MW( uint16_t AM, uint16_t DW, uint32_t VME_Address, uint32_t Data){
215
switch (DW){
216
   case VME_D16: return  (short) WIENER_VMUSB_StackAppendData(CMD_WRITE | AM , VME_Address | CMD_D16 , Data);
217
   case VME_D32: return  (short) WIENER_VMUSB_StackAppendData(CMD_WRITE | AM , VME_Address | CMD_D32 , Data);
218
   default: return 0;
219
}
220
return 0;
221
}
222
short __stdcall WIENER_VMUSB_VME_MWRST( void ){
223
  WIENER_VMUSB_StackInit();
224
return 0;
225
}
226
short __stdcall WIENER_VMUSB_VME_MWEXEC( void ){
227
 WUSBXX_stack_execute (WUSB_udev, NULL);
228
return 0;
229
}
230
 
231
short __stdcall WIENER_VMUSB_VME_MR( uint16_t AM, uint16_t DW, uint32_t VME_Address, uint32_t *Data){
232
switch (DW){
233
   case VME_D16: return  (short) WIENER_VMUSB_StackAppendData(CMD_READ | AM , VME_Address | CMD_D16 , *Data);
234
   case VME_D32: return  (short) WIENER_VMUSB_StackAppendData(CMD_READ | AM , VME_Address | CMD_D32 , *Data);
235
   default: return 0;
236
}
237
return 0;      
238
}
239
short __stdcall WIENER_VMUSB_VME_MRRST( void ){
240
    WIENER_VMUSB_StackInit();
241
return 0;
242
}
243
short __stdcall WIENER_VMUSB_VME_MREXEC(  uint32_t *Data  ){
244
 WUSBXX_stack_execute (WUSB_udev,(char *) Data);
245
return 0;      
246
}