Subversion Repositories f9daq

Rev

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

  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.  
  15. #include "wusbvme_dll.h"
  16. #include <userint.h>
  17. #include <utility.h>
  18.  
  19. usb_dev_handle *WUSB_udev;
  20.  
  21. static HINSTANCE DLLHandle;
  22.  
  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.  
  37. #ifdef XXDLL
  38. #warning Using DLL ...
  39.  
  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.  
  73. void _VI_FUNC WIENER_VMUSB_load (const char* module_path) {
  74.        
  75.   if (module_path == NULL) {
  76.     DLLHandle = LoadLibrary("c:\\Windows\\system32\\libxxusb.dll");
  77.   } else {
  78.     DLLHandle = LoadLibrary(module_path);
  79.                 printf("Module Path %s\n", module_path);
  80.   }
  81.   if (!DLLHandle) {
  82.     printf ("\n\nFailed to Open libxxusb.dll \n");
  83.     printf("LastError 0x%x\n",GetLastError() );
  84.                 MessagePopup ("ERROR", "Failed to load libxxusb.dll ");
  85.     return;
  86.   } else {
  87.                 printf ("\n\nSuccesfully Opened libxxusb.dll  \n");    
  88.         }
  89.   if (!(xxusb_register_read_Ptr = (void *) GetProcAddress(DLLHandle,"xxusb_register_read"))) exit(1);
  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);
  93.   if (!(xxusb_longstack_execute_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_longstack_execute"))) exit(1);
  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.  
  118. #else
  119.   #warning: "Using libxxusb: add libxxusb.c to source list ..."
  120.  
  121. void _VI_FUNC WIENER_VMUSB_load (const char* module_path) {
  122.  
  123. }
  124. #endif
  125.  
  126. void _VI_FUNC WIENER_VMUSB_open (const char *serial) {
  127.  
  128.   xxusb_device_type devices[100];
  129.   struct usb_device *dev;
  130.   if (serial != NULL) {
  131.     WUSB_udev = xxusb_serial_open((char *) serial);
  132.   } else  {
  133.     //Find XX_USB devices and open the first one found
  134.     xxusb_devices_find(devices);
  135.     dev = devices[0].usbdev;
  136.     WUSB_udev = xxusb_device_open(dev);
  137.   }
  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");
  144.   }
  145.  
  146.  
  147. }
  148.  
  149. void _VI_FUNC WIENER_VMUSB_close (void) {
  150.   if (WUSB_udev) xxusb_device_close(WUSB_udev);
  151. }
  152.  
  153.  
  154. uint32_t fStack[MAXSTACKSIZE];
  155. uint32_t fStackSize;
  156.  
  157. int WIENER_VMUSB_StackInit() {
  158.   fStack[0]=1;
  159.   fStackSize=1;
  160.   return fStack[0];
  161. }
  162.  
  163. void WIENER_VMUSB_StackClear() {
  164.   WIENER_VMUSB_StackInit();
  165. }
  166.  
  167. int WIENER_VMUSB_StackGetUInt16(int maxn, uint16_t *stack) {
  168.   int i;
  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;
  173.   }
  174.   return fStackSize*2;
  175. }
  176.  
  177. int WIENER_VMUSB_StackGetUInt32(int maxn, uint32_t *stack) {
  178.   int i;
  179.   uint16_t * u16Stack = (uint16_t *) fStack;
  180.   for (i=0; i< fStackSize*2; i++) {
  181.     if (i<maxn) stack[i]=u16Stack[i];
  182.     else return -1;
  183.   }
  184.   return fStackSize*2;
  185. }
  186.  
  187.  
  188. int WIENER_VMUSB_StackAppend(int cmd, uint32_t addr ) {
  189.   if (fStackSize+2 >= MAXSTACKSIZE) {
  190.     fprintf(stderr,"WIENER_VMUSB_StackAppend Maximum stack size reached! Increase....%d\n", fStackSize+2);
  191.     return -1;
  192.   }
  193.   fStack[fStackSize ++]   = cmd;
  194.   fStack[fStackSize ++]   = addr;
  195.   fStack[0]+=4;
  196.   return fStack[0];
  197. }
  198.  
  199. int WIENER_VMUSB_StackAppendData(int cmd,uint32_t addr ,uint32_t data) {
  200.  
  201.   WIENER_VMUSB_StackAppend( cmd, addr );
  202.  
  203.   if (fStackSize+1 >= MAXSTACKSIZE) {
  204.     fprintf(stderr,"WIENER_VMUSB_StackAppendData Maximum stack size reached! Increase....%d\n", fStackSize+1);
  205.     return -1;
  206.   }
  207.   fStack[fStackSize++]  =  data;
  208.   fStack[0]+=2;
  209.   return fStack[0];
  210. }
  211.  
  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);
  215. }
  216.  
  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);
  220. }
  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);
  224. }
  225.  
  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);
  231. }
  232.  
  233.  
  234. char wusbcc_stack[MAXSTACKSIZE*4];
  235.  
  236.  
  237. int _VI_FUNC WUSBXX_stack_execute (usb_dev_handle * dev, char * stack) {
  238.   int i, nb=0;
  239.   if (!dev) {
  240.     printf("WUSBXX_stack_execute device not open\n");
  241.     return -1;
  242.   }
  243.   uint16_t * exstack = (uint16_t *) wusbcc_stack;
  244.   exstack[0]=0;
  245.   nb = WIENER_VMUSB_StackGetUInt16(MAXSTACKSIZE, &exstack[1] );
  246.   //printf("WUSBXX_stack_execute stack size %d\n", nb);
  247.   //WIENER_VMUSB_StackPrint();
  248.   nb = xxusb_longstack_execute (dev,  exstack, nb, 1000);
  249.   //DataBuffer(0 - (ReturnValue/2-1)) - (unsigned short)array of returned data when ReturnValue>0
  250.   //printf("WUSBXX_stack_execute return size %d\n", nb);
  251.   if (nb<0 ) printf("xxusb_longstack_execute %s line:%d err=%d %s\n",__FILE__,__LINE__,nb, strerror(-nb) );
  252.   if (stack!=NULL) for ( i=0; i<nb; i++) stack[i]=wusbcc_stack[i];
  253.   return nb/sizeof(uint32_t);
  254. }
  255.  
  256.  
  257.  
  258.  
  259.  
  260. #ifndef VME_D32
  261.  
  262. #define VME_D8  0x1
  263. #define VME_D16 0x2
  264. #define VME_D32 0x4
  265. #endif
  266.  
  267.  
  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.   }
  282. //printf("R 0x%02x 0x%02x 0x%08x %x\n", AM , DW, VME_Address, *Data);
  283.   if (nb<0)  printf("%s %d %d %s\n",__FILE__,__LINE__,nb, strerror(-nb) );
  284.  
  285.   return (short)nb;
  286. }
  287.  
  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) );
  303.  
  304.   return (short)nb;
  305. }
  306.  
  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;
  317. }
  318. short __stdcall WIENER_VMUSB_VME_MWRST( void ) {
  319.   WIENER_VMUSB_StackInit();
  320.   return 0;
  321. }
  322. short __stdcall WIENER_VMUSB_VME_MWEXEC( void ) {
  323.   return (short) WUSBXX_stack_execute (WUSB_udev, NULL);
  324. }
  325.  
  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;
  336. }
  337. short __stdcall WIENER_VMUSB_VME_MRRST( void ) {
  338.   WIENER_VMUSB_StackInit();
  339.   return 0;
  340. }
  341. short __stdcall WIENER_VMUSB_VME_MREXEC(  uint32_t *Data  ) {
  342.   return (short) WUSBXX_stack_execute (WUSB_udev,(char *) Data);
  343. }
  344.