Subversion Repositories f9daq

Rev

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


#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>

#include <CAENVMElib.h>
#include <CAENVMEoslib.h>
#include <CAENVMEtypes.h>

#ifndef _CVI_
#define __stdcall
#define UCHAR unsigned char
#define  _VI_FUNC
#endif

#include "CAENV1718.h"



int32_t CAEN_udev;

int VMEerrors;

int CAEN_VME_start (char* serial) {

  CVErrorCodes result = CAENVME_Init( cvV1718 , 0 , 0 , &CAEN_udev);
  if (result != cvSuccess) {
    printf("CAEN1718 Init %s\n", CAENVME_DecodeError(result));
    printf(" Check usb cable, usb udev permissions and restart!  Exiting .....\n");
    return (-1);
  }
  if (serial == NULL) serial = (char *) malloc(100*sizeof(char));
  CAENVME_BoardFWRelease(CAEN_udev, serial);

  return result;
}


short CAEN_VME_read(long Handle, unsigned long Address, void *Data, CVAddressModifier AM, CVDataWidth DW) {

  CVErrorCodes result;
  int ncount=0;
  while (1) {
    result = CAENVME_ReadCycle(Handle, Address, Data, AM, DW );
    if (result !=cvSuccess ) {
      VMEerrors++;
      printf("%d CAENVME_ReadCycle at 0x%0lX failed! data=0x%0X  AM= 0x%0X DW= 0x%0X  err=%s\n", ncount,Address, *((uint32_t *) Data),AM,DW, CAENVME_DecodeError(result));
      ncount++;
      if (ncount==10) return (result);
    } else break;
  }
  return result;
}

int CAEN_VME_BltRead(long Handle, unsigned long Address, void *Data, int size, CVAddressModifier AM, CVDataWidth DW) {
  int count=0;
  CVErrorCodes result;
  int ncount=0;

  while (1) {

    result = CAENVME_BLTReadCycle(Handle, Address, Data, size, AM, DW , &count);
    if (result !=cvSuccess ) {
      VMEerrors++;
      printf("%d CAENVME_BLTReadCycle at 0x%0lX failed! data=0x%0X  AM= 0x%0X DW= 0x%0X  err=%s count=%d\n", ncount,Address, *((uint32_t *) Data),AM,DW, CAENVME_DecodeError(result),count);
      ncount++;
      if (ncount==10) return (result);
    } else break;
  }
  return count;
}

short CAEN_VME_write(long Handle, unsigned long Address, void *Data, CVAddressModifier AM, CVDataWidth DW) {

  CVErrorCodes result;
  int ncount=0;
  while (1) {

    result = CAENVME_WriteCycle(Handle, Address, Data, AM, DW );
    if (result !=cvSuccess ) {
      VMEerrors++;
      printf("CAENVME_WriteCycle at 0x%0lX failed! data=0x%0X  AM= 0x%0X DW= 0x%0X  err=%s\n", Address, *((uint32_t *) Data),AM,DW, CAENVME_DecodeError(result));
      if (ncount==10)  return (result);
    } else break;
  }
  return result;
}




uint32_t *WStackAddrs = NULL;
uint32_t *WStackBuffer;
unsigned int WStackNCycles;
unsigned int WStackMaxCycles=100000;
CVAddressModifier *WStackAMs;
CVDataWidth *WStackDWs;
CVErrorCodes *WStackECs;


uint32_t *RStackAddrs =NULL;
unsigned int RStackNCycles;
unsigned int RStackMaxCycles=100000;
CVAddressModifier *RStackAMs;
CVDataWidth *RStackDWs;
CVErrorCodes *RStackECs;



short CAEN_VME_appendread(unsigned long Address, void *Data, CVAddressModifier AM, CVDataWidth DW) {
  if (RStackNCycles<RStackMaxCycles) {
    RStackAddrs[RStackNCycles]  = Address;
    RStackAMs[RStackNCycles]    = AM;
    RStackDWs[RStackNCycles]    = DW;
    //int i=RStackNCycles;
    //printf("AppendRead %d ADDR= 0x%0X AM= 0x%0X DW= 0x%0X \n", i,RStackAddrs[i],RStackAMs[i], RStackDWs[i]);
    RStackNCycles++;
  } else {
    printf("Increase DataBuffer RStackMaxCycles =%d xx\n", RStackMaxCycles);
  }
  return RStackNCycles;
}

short CAEN_VME_appendwrite(unsigned long Address, void *Data, CVAddressModifier AM, CVDataWidth DW) {
  if (WStackNCycles<WStackMaxCycles) {
    WStackAddrs[WStackNCycles]  = Address;
    WStackBuffer[WStackNCycles] = *((uint32_t *) Data);
    WStackAMs[WStackNCycles]    = AM;
    WStackDWs[WStackNCycles]    = DW;
    WStackNCycles++;
  } else {
    printf("Increase DataBuffer WStackMaxCycles =%d xx\n", WStackMaxCycles);
  }
  return 0;
}

short CAEN_VME_MultiReadReset() {
  RStackNCycles=0;
  if (RStackAddrs!=NULL) return 0;
  RStackAddrs = (uint32_t *) malloc( RStackMaxCycles*sizeof(uint32_t));

  RStackAMs  = (CVAddressModifier *) malloc(RStackMaxCycles*sizeof(CVAddressModifier));
  RStackDWs = (CVDataWidth *) malloc(RStackMaxCycles*sizeof(CVDataWidth));
  RStackECs = (CVErrorCodes *) malloc(RStackMaxCycles*sizeof(CVErrorCodes));

  return 0;
}

short CAEN_VME_MultiWriteReset() {
  WStackNCycles=0;
  if (WStackAddrs!=NULL) return 0;
  WStackBuffer = (uint32_t *) malloc( WStackMaxCycles*sizeof(uint32_t));
  WStackAddrs = (uint32_t *) malloc( WStackMaxCycles*sizeof(uint32_t));
  WStackAMs  = (CVAddressModifier *) malloc(WStackMaxCycles*sizeof(CVAddressModifier));
  WStackDWs = (CVDataWidth *) malloc(WStackMaxCycles*sizeof(CVDataWidth));
  WStackECs = (CVErrorCodes *) malloc(WStackMaxCycles*sizeof(CVErrorCodes));

  return 0;
}

short CAEN_VME_MultiReadExecute(long Handle, uint32_t *  Data) {
  //printf("len=%d\n",RStackNCycles);
  int i=0;
  CVErrorCodes result =CAENVME_MultiRead(Handle, RStackAddrs,  Data,  RStackNCycles, RStackAMs, RStackDWs, RStackECs);
  if (result !=cvSuccess ) {
    VMEerrors++;
    printf("CAENVME_MultiRead at 0x%0X failed!  err=%s RStackNCycles=%d\n", RStackAddrs[0], CAENVME_DecodeError(result), RStackNCycles);
    for (i=0; i<RStackNCycles; i++) {
      if (RStackECs[i]!=cvSuccess ) printf("%d ADDR= 0x%0X AM= 0x%0X DW= 0x%0X  failed!  err=%s\n", i,RStackAddrs[i],RStackAMs[i], RStackDWs[i], CAENVME_DecodeError(RStackECs[i]) );
    }
    exit(result);
  }
  return  RStackNCycles;
}

short CAEN_VME_MultiWriteExecute(long Handle) {
  int i=0;
  CVErrorCodes result =CAENVME_MultiWrite(Handle, WStackAddrs, WStackBuffer,  WStackNCycles, WStackAMs, WStackDWs, WStackECs);
  if (result !=cvSuccess ) {
    printf("CAENVME_MultiWrite at 0x%0X failed!  err=%s WStackNCycles=%d\n", WStackAddrs[0], CAENVME_DecodeError(result), WStackNCycles);

    for (i=0; i<WStackNCycles; i++) {
      if (WStackECs[i]!=cvSuccess )
        printf("%d ADDR= 0x%0X AM= 0x%0X DW= 0x%0X  data= 0x%0X  failed!  err=%s\n", i,WStackAddrs[i],WStackAMs[i], WStackDWs[i],  WStackBuffer[i], CAENVME_DecodeError(WStackECs[i]) );
    }
    exit(result);
  }
  return  WStackNCycles;
}


short __stdcall CAEN_VME_R( uint16_t AddressModifier, uint16_t DataWidth,  uint32_t VME_Address, uint32_t *Data) {
  return CAEN_VME_read( CAEN_udev,   VME_Address, (void *)Data,  (CVAddressModifier) AddressModifier, (CVDataWidth) DataWidth);
}

short __stdcall CAEN_VME_W( uint16_t AddressModifier, uint16_t DataWidth,  uint32_t VME_Address, uint32_t Data) {
  return CAEN_VME_write( CAEN_udev,   VME_Address, &Data,  (CVAddressModifier) AddressModifier, (CVDataWidth) DataWidth);
}

short __stdcall CAEN_VME_MW( uint16_t AM, uint16_t DW, uint32_t VME_Address, uint32_t Data) {

  return CAEN_VME_appendwrite(  VME_Address, &Data ,(CVAddressModifier) AM , (CVDataWidth) DW) ;

}
short __stdcall CAEN_VME_MWRST( void ) {

  return CAEN_VME_MultiWriteReset();

}
short __stdcall CAEN_VME_MWEXEC( void ) {

  return CAEN_VME_MultiWriteExecute(CAEN_udev);

}

short __stdcall CAEN_VME_MR( uint16_t AM, uint16_t DW, uint32_t VME_Address, uint32_t *Data) {

  return   CAEN_VME_appendread(  VME_Address, Data ,(CVAddressModifier) AM , (CVDataWidth) DW) ;

}
short __stdcall CAEN_VME_MRRST( void ) {

  return CAEN_VME_MultiReadReset();

}
short __stdcall CAEN_VME_MREXEC(  uint32_t *Data  ) {
  return CAEN_VME_MultiReadExecute(CAEN_udev, Data);


}