Subversion Repositories f9daq

Rev

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

#include "wusbvme_dll.h"


usb_dev_handle *WUSB_udev;

static HINSTANCE DLLHandle;

void _VI_FUNC WIENER_VMUSB_load (char* module_path)
{
  if (module_path == NULL)
    DLLHandle = LoadLibrary("libxxusb.dll");
   else
    DLLHandle = LoadLibrary(module_path);
  if (!DLLHandle) {
       printf ("\n\nFailed to Open libxxusb.dll \n");  
          return;
  }
  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);
}

void _VI_FUNC WIENER_VMUSB_open (char *serial)
{
       
  xxusb_device_type devices[100];
  struct usb_device *dev;
  if (serial != NULL)
        WUSB_udev = xxusb_serial_open(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 __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,Data);  break;
   case VME_D32: nb=  VME_read_32(WUSB_udev, AM,VME_Address,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 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 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 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 WUSBXX_stack_execute (WUSB_udev,(char *) Data);
}