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