#include <stdlib.h>
#include <stdio.h>
#include <CAENVMElib.h>
#include <CAENVMElib.h>
#include <CAENVMEoslib.h>
#include <CAENVMEtypes.h>
int32_t udev;
int VMEerrors;
int VME_start (char* serial)
{
CVErrorCodes result = CAENVME_Init( cvV1718 , 0 , 0 , &udev);
if (result != cvSuccess) {
printf("CAEN1718 Init %s\n", CAENVME_DecodeError
(result
));
printf(" Check usb cable, usb udev permissions and restart! Exiting .....\n");
}
if (serial
== NULL
) serial
= malloc(100*sizeof(char));
CAENVME_BoardFWRelease(udev, serial);
return result;
}
int 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) exit(result
);
} else break;
}
return result;
}
int 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) exit(result
);
} else break;
}
return count;
}
int 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) exit(result
);
} else break;
}
return result;
}
uint32_t *WStackAddrs = NULL;
uint32_t *WStackBuffer;
int WStackNCycles;
int WStackMaxCycles=100000;
CVAddressModifier *WStackAMs;
CVDataWidth *WStackDWs;
CVErrorCodes *WStackECs;
uint32_t *RStackAddrs =NULL;
int RStackNCycles;
int RStackMaxCycles=100000;
CVAddressModifier *RStackAMs;
CVDataWidth *RStackDWs;
CVErrorCodes *RStackECs;
int 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\n", RStackMaxCycles
);
}
return RStackNCycles;
}
int 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\n", WStackMaxCycles
);
}
return 0;
}
int 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;
}
int 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;
}
int 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
]) );
}
}
return RStackNCycles;
}
int 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
]) );
}
}
return WStackNCycles;
}