Subversion Repositories f9daq

Rev

Rev 307 | Blame | Compare with Previous | Last modification | View Log | RSS feed

#ifdef LINUX
#include <dlfcn.h>
#define LoadLibrary(x) dlopen( (x), RTLD_NOW );
#define GetProcAddress(x,y) dlsym((x),(y))
#define __stdcall
#define UCHAR unsigned char
#define HINSTANCE void *
#define  _VI_FUNC
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#endif

#include "wusbvme_dll.h"
#include <userint.h>
#include <utility.h>

usb_dev_handle *WUSB_udev;

static HINSTANCE DLLHandle;


short __stdcall WIENER_VMUSB_VME_R( uint16_t AM, uint16_t DW,  uint32_t VME_Address, uint32_t *Data);

short __stdcall WIENER_VMUSB_VME_W( uint16_t AM, uint16_t DW, uint32_t VME_Address, uint32_t Data);

short __stdcall WIENER_VMUSB_VME_MW( uint16_t AM, uint16_t DW, uint32_t VME_Address, uint32_t Data);
short __stdcall WIENER_VMUSB_VME_MWRST( void );
short __stdcall WIENER_VMUSB_VME_MWEXEC( void );

short __stdcall WIENER_VMUSB_VME_MR( uint16_t AM, uint16_t DW, uint32_t VME_Address, uint32_t *Data);
short __stdcall WIENER_VMUSB_VME_MRRST( void );
short __stdcall WIENER_VMUSB_VME_MREXEC(  uint32_t *Data  );


#ifdef XXDLL
#warning Using DLL ...

short __stdcall xxusb_register_read(usb_dev_handle *hDev, short RegAddr, long *RegData);
short __stdcall xxusb_stack_read(usb_dev_handle *hDev, short StackAddr, long *StackData);
short __stdcall xxusb_stack_write(usb_dev_handle *hDev, short StackAddr, long *StackData);
short __stdcall xxusb_stack_execute(usb_dev_handle *hDev, long *StackData);
short __stdcall xxusb_longstack_execute(usb_dev_handle *hDev, void *DataBuffer, int lDataLen, int timeout);
short __stdcall xxusb_register_write(usb_dev_handle *hDev, short RegAddr, long RegData);
short __stdcall xxusb_usbfifo_read(usb_dev_handle *hDev, long *DataBuffer, short lDataLen, int timeout);
short __stdcall xxusb_bulk_read(usb_dev_handle *hDev, char *DataBuffer, short lDataLen, int timeout);
short __stdcall xxusb_bulk_write(usb_dev_handle *hDev, char *DataBuffer, short lDataLen, int timeout);
short __stdcall xxusb_reset_toggle(usb_dev_handle *hDev);

short __stdcall xxusb_devices_find(xxusb_device_type *xxusbDev);
short __stdcall xxusb_device_close(usb_dev_handle *hDev);
usb_dev_handle* __stdcall xxusb_device_open(struct usb_device *dev);
short __stdcall xxusb_flash_program(usb_dev_handle *hDev, char *config, short nsect);
short __stdcall xxusb_flashblock_program(usb_dev_handle *hDev, UCHAR *config);
usb_dev_handle* __stdcall xxusb_serial_open(char *SerialString);

short __stdcall VME_register_write(usb_dev_handle *hdev, long VME_Address, long Data);
short __stdcall VME_register_read(usb_dev_handle *hdev, long VME_Address, long *Data);
short __stdcall VME_LED_settings(usb_dev_handle *hdev, int LED, int code, int invert, int latch);

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);

short __stdcall VME_Output_settings(usb_dev_handle *hdev, int Channel, int code, int invert, int latch);

short __stdcall VME_read_16(usb_dev_handle *hdev,short Address_Modifier, long VME_Address, long *Data);
short __stdcall VME_read_32(usb_dev_handle *hdev, short Address_Modifier, long VME_Address, long *Data);
short __stdcall VME_BLT_read_32(usb_dev_handle *hdev, short Address_Modifier, int count, long VME_Address, long Data[]);
short __stdcall VME_write_16(usb_dev_handle *hdev, short Address_Modifier, long VME_Address, long Data);
short __stdcall VME_write_32(usb_dev_handle *hdev, short Address_Modifier, long VME_Address, long Data);


void _VI_FUNC WIENER_VMUSB_load (const char* module_path) {
       
  if (module_path == NULL) {
    DLLHandle = LoadLibrary("c:\\Windows\\system32\\libxxusb.dll");
  } else {
    DLLHandle = LoadLibrary(module_path);
                printf("Module Path %s\n", module_path);
  }
  if (!DLLHandle) {
    printf ("\n\nFailed to Open libxxusb.dll \n");
    printf("LastError 0x%x\n",GetLastError() );
                MessagePopup ("ERROR", "Failed to load libxxusb.dll ");
    return;
  } else {
                printf ("\n\nSuccesfully Opened libxxusb.dll  \n");    
        }
  if (!(xxusb_register_read_Ptr = (void *) GetProcAddress(DLLHandle,"xxusb_register_read"))) exit(1);
  if (!(xxusb_stack_read_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_stack_read"))) exit(1);
  if (!(xxusb_stack_write_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_stack_write"))) exit(1);
  if (!(xxusb_stack_execute_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_stack_execute"))) exit(1);
  if (!(xxusb_longstack_execute_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_longstack_execute"))) exit(1);
  if (!(xxusb_register_write_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_register_write"))) exit(1);
  if (!(xxusb_usbfifo_read_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_usbfifo_read"))) exit(1);
  if (!(xxusb_bulk_read_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_bulk_read"))) exit(1);
  if (!(xxusb_bulk_write_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_bulk_write"))) exit(1);
  if (!(xxusb_reset_toggle_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_reset_toggle"))) exit(1);

  if (!(xxusb_devices_find_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_devices_find"))) exit(1);
  if (!(xxusb_device_close_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_device_close"))) exit(1);
  if (!(xxusb_device_open_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_device_open"))) exit(1);
  if (!(xxusb_flash_program_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_flash_program"))) exit(1);
  if (!(xxusb_flashblock_program_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_flashblock_program"))) exit(1);
  if (!(xxusb_serial_open_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_serial_open"))) exit(1);
  if (!(VME_register_write_Ptr = (void*) GetProcAddress(DLLHandle,"VME_register_write"))) exit(1);
  if (!(VME_register_read_Ptr = (void*) GetProcAddress(DLLHandle,"VME_register_read"))) exit(1);
  if (!(VME_LED_settings_Ptr = (void*) GetProcAddress(DLLHandle,"VME_LED_settings"))) exit(1);
  if (!(VME_DGG_Ptr = (void*) GetProcAddress(DLLHandle,"VME_DGG"))) exit(1);
  if (!(VME_Output_settings_Ptr = (void*) GetProcAddress(DLLHandle,"VME_Output_settings"))) exit(1);
  if (!(VME_read_16_Ptr = (void*) GetProcAddress(DLLHandle,"VME_read_16"))) exit(1);
  if (!(VME_read_32_Ptr = (void*) GetProcAddress(DLLHandle,"VME_read_32"))) exit(1);
  if (!(VME_BLT_read_32_Ptr = (void*) GetProcAddress(DLLHandle,"VME_BLT_read_32"))) exit(1);
  if (!(VME_write_16_Ptr = (void*) GetProcAddress(DLLHandle,"VME_write_16"))) exit(1);
  if (!(VME_write_32_Ptr = (void*) GetProcAddress(DLLHandle,"VME_write_32"))) exit(1);
}

#else
  #warning: "Using libxxusb: add libxxusb.c to source list ..."

void _VI_FUNC WIENER_VMUSB_load (const char* module_path) {
 
}
#endif

void _VI_FUNC WIENER_VMUSB_open (const char *serial) {

  xxusb_device_type devices[100];
  struct usb_device *dev;
  if (serial != NULL) {
    WUSB_udev = xxusb_serial_open((char *) serial);
  } else  {
    //Find XX_USB devices and open the first one found
    xxusb_devices_find(devices);
    dev = devices[0].usbdev;
    WUSB_udev = xxusb_device_open(dev);
  }
  // Make sure VM_USB opened OK
  if(!WUSB_udev) {
    printf ("\n\nFailed to Open VM_USB \n\n");
    return ;
  } else {
    printf("VM-USB Device open.!\n");
  }


}

void _VI_FUNC WIENER_VMUSB_close (void) {
  if (WUSB_udev) xxusb_device_close(WUSB_udev);
}


uint32_t fStack[MAXSTACKSIZE];
uint32_t fStackSize;

int WIENER_VMUSB_StackInit() {
  fStack[0]=1;
  fStackSize=1;
  return fStack[0];
}

void WIENER_VMUSB_StackClear() {
  WIENER_VMUSB_StackInit();
}

int WIENER_VMUSB_StackGetUInt16(int maxn, uint16_t *stack) {
  int i;
  uint16_t * u16Stack = (uint16_t *) fStack;
  for (i=0; i< fStackSize*2; i++) {
    if (i<maxn) stack[i]=u16Stack[i];
    else return -1;
  }
  return fStackSize*2;
}

int WIENER_VMUSB_StackGetUInt32(int maxn, uint32_t *stack) {
  int i;
  uint16_t * u16Stack = (uint16_t *) fStack;
  for (i=0; i< fStackSize*2; i++) {
    if (i<maxn) stack[i]=u16Stack[i];
    else return -1;
  }
  return fStackSize*2;
}


int WIENER_VMUSB_StackAppend(int cmd, uint32_t addr ) {
  if (fStackSize+2 >= MAXSTACKSIZE) {
    fprintf(stderr,"WIENER_VMUSB_StackAppend Maximum stack size reached! Increase....%d\n", fStackSize+2);
    return -1;
  }
  fStack[fStackSize ++]   = cmd;
  fStack[fStackSize ++]   = addr;
  fStack[0]+=4;
  return fStack[0];
}

int WIENER_VMUSB_StackAppendData(int cmd,uint32_t addr ,uint32_t data) {

  WIENER_VMUSB_StackAppend( cmd, addr );

  if (fStackSize+1 >= MAXSTACKSIZE) {
    fprintf(stderr,"WIENER_VMUSB_StackAppendData Maximum stack size reached! Increase....%d\n", fStackSize+1);
    return -1;
  }
  fStack[fStackSize++]  =  data;
  fStack[0]+=2;
  return fStack[0];
}

void WIENER_VMUSB_StackConditionalRead(int adr_mod,int d16d32, uint32_t addr,uint32_t bmask) {
  WIENER_VMUSB_StackAppend(CMD_READ| CMD_HD | adr_mod ,addr);
  WIENER_VMUSB_StackAppendData(CMD_READ| CMD_HD | CMD_HM |adr_mod, d16d32 |addr,bmask);
}

void WIENER_VMUSB_StackMultiRead(int adr_mod,int d16d32,uint32_t baseaddr, int n, uint32_t increment) {
  int i;
  for (i=0; i<n; i++) WIENER_VMUSB_StackAppend( adr_mod | CMD_READ, d16d32 +baseaddr + i*increment);
}
void WIENER_VMUSB_StackMultiWrite(int adr_mod,int d16d32,uint32_t baseaddr, int n, uint32_t increment, uint32_t data) {
  int i;
  for (i=0; i<n; i++)  WIENER_VMUSB_StackAppendData( adr_mod | CMD_WRITE,d16d32 + baseaddr+i*increment, data);
}

void WIENER_VMUSB_StackPrint() {
  uint16_t * u16Stack = (uint16_t *) fStack;
  uint32_t i;
  for (i=0; i < fStackSize*2; i++) printf("0x%04x\n",u16Stack[i]);
  printf("size of stack %d __________________________________\n",fStackSize*2-1);
}


char wusbcc_stack[MAXSTACKSIZE*4];


int _VI_FUNC WUSBXX_stack_execute (usb_dev_handle * dev, char * stack) {
  int i, nb=0;
  if (!dev) {
    printf("WUSBXX_stack_execute device not open\n");
    return -1;
  }
  uint16_t * exstack = (uint16_t *) wusbcc_stack;
  exstack[0]=0;
  nb = WIENER_VMUSB_StackGetUInt16(MAXSTACKSIZE, &exstack[1] );
  //printf("WUSBXX_stack_execute stack size %d\n", nb);
  //WIENER_VMUSB_StackPrint();
  nb = xxusb_longstack_execute (dev,  exstack, nb, 1000);
  //DataBuffer(0 - (ReturnValue/2-1)) - (unsigned short)array of returned data when ReturnValue>0
  //printf("WUSBXX_stack_execute return size %d\n", nb);
  if (nb<0 ) printf("xxusb_longstack_execute %s line:%d err=%d %s\n",__FILE__,__LINE__,nb, strerror(-nb) );
  if (stack!=NULL) for ( i=0; i<nb; i++) stack[i]=wusbcc_stack[i];
  return nb/sizeof(uint32_t);
}





#ifndef VME_D32

#define VME_D8  0x1
#define VME_D16 0x2
#define VME_D32 0x4
#endif


short WIENER_VMUSB_VME_DGG(
 
  unsigned short channel,
  unsigned short trigger,
  unsigned short output,
  long delay,
  unsigned short gate,
  unsigned short invert,
  unsigned short latch
){

  if (!WUSB_udev) return -222;
  VME_DGG(WUSB_udev, channel, trigger, output, delay, gate, invert, latch);    
  return 0;
}

 

short WIENER_VMUSB_VME_Output_settings(
 
  unsigned short channel,
  unsigned short code,
  unsigned short invert,
  unsigned short latch
){

  if (!WUSB_udev) return -222;
  return VME_Output_settings(WUSB_udev, channel, code, invert, latch);    
}

short  WIENER_VMUSB_register_write( short RegAddr, long RegData){
   if (!WUSB_udev) return -222;  
   return xxusb_register_write( WUSB_udev, RegAddr, RegData);
}  


short __stdcall WIENER_VMUSB_VME_R( uint16_t AM, uint16_t DW,  uint32_t VME_Address, uint32_t *Data) {
  int nb=0;
  if (!WUSB_udev) return -222;

  switch (DW) {
    case VME_D16:
      nb=  VME_read_16(WUSB_udev, AM,VME_Address,(long int *) Data);
      break;
    case VME_D32:
      nb=  VME_read_32(WUSB_udev, AM,VME_Address,(long int *)Data);
      break;
    default:
      return 0;
  }
//printf("R 0x%02x 0x%02x 0x%08x %x\n", AM , DW, VME_Address, *Data);
  if (nb<0)  printf("%s %d %d %s\n",__FILE__,__LINE__,nb, strerror(-nb) );

  return (short)nb;
}

short __stdcall WIENER_VMUSB_VME_W( uint16_t AM, uint16_t DW, uint32_t VME_Address, uint32_t Data) {
  int nb=0;
  if (!WUSB_udev) return -222;
//  printf("W 0x%02x 0x%02x 0x%08x %x\n", AM , DW, VME_Address, Data);
  switch (DW) {
    case VME_D16:
      nb=    VME_write_16(WUSB_udev, AM,VME_Address,Data);
      break;
    case VME_D32:
      nb=    VME_write_32(WUSB_udev, AM,VME_Address,Data);
      break;
    default:
      return 0;
  }
  if (nb<0)  printf("%s %d %d %s\n",__FILE__,__LINE__,nb, strerror(-nb) );

  return (short)nb;
}

short __stdcall WIENER_VMUSB_VME_MW( uint16_t AM, uint16_t DW, uint32_t VME_Address, uint32_t Data) {
  switch (DW) {
    case VME_D16:
      return  (short) WIENER_VMUSB_StackAppendData(CMD_WRITE | AM , VME_Address | CMD_D16 , Data);
    case VME_D32:
      return  (short) WIENER_VMUSB_StackAppendData(CMD_WRITE | AM , VME_Address | CMD_D32 , Data);
    default:
      return 0;
  }
  return 0;
}
short __stdcall WIENER_VMUSB_VME_MWRST( void ) {
  WIENER_VMUSB_StackInit();
  return 0;
}
short __stdcall WIENER_VMUSB_VME_MWEXEC( void ) {
  return (short) WUSBXX_stack_execute (WUSB_udev, NULL);
}

short __stdcall WIENER_VMUSB_VME_MR( uint16_t AM, uint16_t DW, uint32_t VME_Address, uint32_t *Data) {
  switch (DW) {
    case VME_D16:
      return  (short) WIENER_VMUSB_StackAppend(CMD_READ | AM , VME_Address | CMD_D16 );
    case VME_D32:
      return  (short) WIENER_VMUSB_StackAppend(CMD_READ | AM , VME_Address | CMD_D32 );
    default:
      return 0;
  }
  return 0;
}
short __stdcall WIENER_VMUSB_VME_MRRST( void ) {
  WIENER_VMUSB_StackInit();
  return 0;
}
short __stdcall WIENER_VMUSB_VME_MREXEC(  uint32_t *Data  ) {
  return (short) WUSBXX_stack_execute (WUSB_udev,(char *) Data);
}