Subversion Repositories f9daq

Rev

Rev 88 | Rev 100 | 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
 
92 f9daq 77
uint32_t fStack[MAXSTACKSIZE];
78
uint32_t fStackSize;
82 f9daq 79
 
80
int WIENER_VMUSB_StackInit(){
81
        fStack[0]=1;
92 f9daq 82
        fStackSize=1;
82 f9daq 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 f9daq 92
  uint16_t * u16Stack = (uint16_t *) fStack;
93
  for (i=0;i< fStackSize*2;i++){
94
                        if (i<maxn) stack[i]=u16Stack[i]; else return -1;
82 f9daq 95
  }
92 f9daq 96
  return fStackSize*2;
82 f9daq 97
}
98
 
99
int WIENER_VMUSB_StackGetUInt32(int maxn, uint32_t *stack){
100
  int i;
92 f9daq 101
  uint16_t * u16Stack = (uint16_t *) fStack;
102
  for (i=0;i< fStackSize*2;i++){
103
                        if (i<maxn) stack[i]=u16Stack[i]; else return -1;
82 f9daq 104
  }
92 f9daq 105
  return fStackSize*2;
82 f9daq 106
}
107
 
108
 
109
int WIENER_VMUSB_StackAppend(int cmd, uint32_t addr ){
92 f9daq 110
  if (fStackSize+2 >= MAXSTACKSIZE) {
111
    fprintf(stderr,"WIENER_VMUSB_StackAppend Maximum stack size reached! Increase....%d\n", fStackSize+2);
82 f9daq 112
    return -1;
113
  }
92 f9daq 114
  fStack[fStackSize ++]   = cmd;
115
  fStack[fStackSize ++]   = addr;
82 f9daq 116
  fStack[0]+=4;
117
  return fStack[0];
118
}
119
 
120
int WIENER_VMUSB_StackAppendData(int cmd,uint32_t addr ,uint32_t data){
92 f9daq 121
 
82 f9daq 122
  WIENER_VMUSB_StackAppend( cmd, addr );
92 f9daq 123
 
124
  if (fStackSize+1 >= MAXSTACKSIZE) {
125
    fprintf(stderr,"WIENER_VMUSB_StackAppendData Maximum stack size reached! Increase....%d\n", fStackSize+1);
82 f9daq 126
    return -1;
127
  }
92 f9daq 128
  fStack[fStackSize++]  =  data;
82 f9daq 129
  fStack[0]+=2;
130
  return fStack[0];
131
}
132
 
133
void WIENER_VMUSB_StackConditionalRead(int adr_mod,int d16d32, uint32_t addr,uint32_t bmask){
134
        WIENER_VMUSB_StackAppend(CMD_READ| CMD_HD | adr_mod ,addr);
135
        WIENER_VMUSB_StackAppendData(CMD_READ| CMD_HD | CMD_HM |adr_mod, d16d32 |addr,bmask);
136
}
137
 
138
void WIENER_VMUSB_StackMultiRead(int adr_mod,int d16d32,uint32_t baseaddr, int n, uint32_t increment){
139
        int i;
140
        for (i=0;i<n;i++) WIENER_VMUSB_StackAppend( adr_mod | CMD_READ, d16d32 +baseaddr + i*increment);
141
}
142
void WIENER_VMUSB_StackMultiWrite(int adr_mod,int d16d32,uint32_t baseaddr, int n, uint32_t increment, uint32_t data){
143
        int i;
144
        for (i=0;i<n;i++)  WIENER_VMUSB_StackAppendData( adr_mod | CMD_WRITE,d16d32 + baseaddr+i*increment, data);
145
}
146
 
147
void WIENER_VMUSB_StackPrint(){
92 f9daq 148
   uint16_t * u16Stack = (uint16_t *) fStack;  
82 f9daq 149
   uint32_t i;
92 f9daq 150
   for (i=0; i < fStackSize*2;i++) printf("0x%04x\n",u16Stack[i]);
151
   printf("size of stack %d __________________________________\n",fStackSize*2-1);
82 f9daq 152
}
153
 
154
 
155
char wusbcc_stack[MAXSTACKSIZE*4];
156
 
157
 
158
int _VI_FUNC WUSBXX_stack_execute (usb_dev_handle * dev, char * stack){
159
  int i, nb=0;
92 f9daq 160
  if (!dev) {
161
        printf("WUSBXX_stack_execute device not open\n");  
162
    return -1;
163
  }      
82 f9daq 164
  uint16_t * exstack = (uint16_t *) wusbcc_stack;
165
  exstack[0]=0;
166
  nb = WIENER_VMUSB_StackGetUInt16(MAXSTACKSIZE, &exstack[1] );
92 f9daq 167
  //printf("WUSBXX_stack_execute stack size %d\n", nb);
82 f9daq 168
  //WIENER_VMUSB_StackPrint();
169
  nb = xxusb_longstack_execute (dev,  exstack, nb, 1000);
92 f9daq 170
  //DataBuffer(0 - (ReturnValue/2-1)) - (unsigned short)array of returned data when ReturnValue>0
171
  //printf("WUSBXX_stack_execute return size %d\n", nb);         
172
  if (nb<0 ) printf("xxusb_longstack_execute %s line:%d err=%d %s\n",__FILE__,__LINE__,nb, strerror(-nb) );
82 f9daq 173
  if (stack!=NULL) for ( i=0;i<nb;i++) stack[i]=wusbcc_stack[i];
174
  return nb/sizeof(uint32_t);
175
}
176
 
177
 
178
 
179
#ifndef VME_D32
180
 
181
#define VME_D8  0x1
182
#define VME_D16 0x2
183
#define VME_D32 0x4
184
#endif
185
 
186
 
187
short __stdcall WIENER_VMUSB_VME_R( uint16_t AM, uint16_t DW,  uint32_t VME_Address, uint32_t *Data){
188
        int nb=0;
189
        if (!WUSB_udev) return -222;
190
 
191
    switch (DW){
192
        case VME_D16: nb=  VME_read_16(WUSB_udev, AM,VME_Address,Data);  break;
193
   case VME_D32: nb=  VME_read_32(WUSB_udev, AM,VME_Address,Data); break;
194
   default: return 0;
195
}
196
//printf("R 0x%02x 0x%02x 0x%08x %x\n", AM , DW, VME_Address, *Data);
197
if (nb<0)  printf("%s %d %d %s\n",__FILE__,__LINE__,nb, strerror(-nb) );
198
 
199
return nb;
200
}
201
 
202
short __stdcall WIENER_VMUSB_VME_W( uint16_t AM, uint16_t DW, uint32_t VME_Address, uint32_t Data){
203
        int nb=0;
204
        if (!WUSB_udev) return -222;
205
//      printf("W 0x%02x 0x%02x 0x%08x %x\n", AM , DW, VME_Address, Data);
206
 switch (DW){
207
   case VME_D16: nb=    VME_write_16(WUSB_udev, AM,VME_Address,Data); break;
208
   case VME_D32: nb=    VME_write_32(WUSB_udev, AM,VME_Address,Data); break;
209
   default: return 0;
210
}
211
if (nb<0)  printf("%s %d %d %s\n",__FILE__,__LINE__,nb, strerror(-nb) );
212
 
213
return nb;
214
}
215
 
216
short __stdcall WIENER_VMUSB_VME_MW( uint16_t AM, uint16_t DW, uint32_t VME_Address, uint32_t Data){
217
switch (DW){
218
   case VME_D16: return  (short) WIENER_VMUSB_StackAppendData(CMD_WRITE | AM , VME_Address | CMD_D16 , Data);
219
   case VME_D32: return  (short) WIENER_VMUSB_StackAppendData(CMD_WRITE | AM , VME_Address | CMD_D32 , Data);
220
   default: return 0;
221
}
222
return 0;
223
}
224
short __stdcall WIENER_VMUSB_VME_MWRST( void ){
225
  WIENER_VMUSB_StackInit();
226
return 0;
227
}
228
short __stdcall WIENER_VMUSB_VME_MWEXEC( void ){
92 f9daq 229
 return WUSBXX_stack_execute (WUSB_udev, NULL);
82 f9daq 230
}
231
 
232
short __stdcall WIENER_VMUSB_VME_MR( uint16_t AM, uint16_t DW, uint32_t VME_Address, uint32_t *Data){
233
switch (DW){
92 f9daq 234
   case VME_D16: return  (short) WIENER_VMUSB_StackAppend(CMD_READ | AM , VME_Address | CMD_D16 );
235
   case VME_D32: return  (short) WIENER_VMUSB_StackAppend(CMD_READ | AM , VME_Address | CMD_D32 );
82 f9daq 236
   default: return 0;
237
}
238
return 0;      
239
}
240
short __stdcall WIENER_VMUSB_VME_MRRST( void ){
241
    WIENER_VMUSB_StackInit();
242
return 0;
243
}
244
short __stdcall WIENER_VMUSB_VME_MREXEC(  uint32_t *Data  ){
92 f9daq 245
 return WUSBXX_stack_execute (WUSB_udev,(char *) Data);
82 f9daq 246
}