Subversion Repositories f9daq

Rev

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

  1.  
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <stdint.h>
  5.  
  6. #include <CAENVMElib.h>
  7. #include <CAENVMEoslib.h>
  8. #include <CAENVMEtypes.h>
  9.  
  10. #ifndef _CVI_
  11. #define __stdcall
  12. #define UCHAR unsigned char
  13. #define  _VI_FUNC
  14. #endif
  15.  
  16. #include "CAENV1718.h"
  17.  
  18.  
  19.  
  20. int32_t CAEN_udev;
  21.  
  22. int VMEerrors;
  23.  
  24. int CAEN_VME_start (char* serial) {
  25.  
  26.   CVErrorCodes result = CAENVME_Init( cvV1718 , 0 , 0 , &CAEN_udev);
  27.   if (result != cvSuccess) {
  28.     printf("CAEN1718 Init %s\n", CAENVME_DecodeError(result));
  29.     printf(" Check usb cable, usb udev permissions and restart!  Exiting .....\n");
  30.     return (-1);
  31.   }
  32.   if (serial == NULL) serial = (char *) malloc(100*sizeof(char));
  33.   CAENVME_BoardFWRelease(CAEN_udev, serial);
  34.  
  35.   return result;
  36. }
  37.  
  38.  
  39. short CAEN_VME_read(long Handle, unsigned long Address, void *Data, CVAddressModifier AM, CVDataWidth DW) {
  40.  
  41.   CVErrorCodes result;
  42.   int ncount=0;
  43.   while (1) {
  44.     result = CAENVME_ReadCycle(Handle, Address, Data, AM, DW );
  45.     if (result !=cvSuccess ) {
  46.       VMEerrors++;
  47.       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));
  48.       ncount++;
  49.       if (ncount==10) return (result);
  50.     } else break;
  51.   }
  52.   return result;
  53. }
  54.  
  55. int CAEN_VME_BltRead(long Handle, unsigned long Address, void *Data, int size, CVAddressModifier AM, CVDataWidth DW) {
  56.   int count=0;
  57.   CVErrorCodes result;
  58.   int ncount=0;
  59.  
  60.   while (1) {
  61.  
  62.     result = CAENVME_BLTReadCycle(Handle, Address, Data, size, AM, DW , &count);
  63.     if (result !=cvSuccess ) {
  64.       VMEerrors++;
  65.       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);
  66.       ncount++;
  67.       if (ncount==10) return (result);
  68.     } else break;
  69.   }
  70.   return count;
  71. }
  72.  
  73. short CAEN_VME_write(long Handle, unsigned long Address, void *Data, CVAddressModifier AM, CVDataWidth DW) {
  74.  
  75.   CVErrorCodes result;
  76.   int ncount=0;
  77.   while (1) {
  78.  
  79.     result = CAENVME_WriteCycle(Handle, Address, Data, AM, DW );
  80.     if (result !=cvSuccess ) {
  81.       VMEerrors++;
  82.       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));
  83.       if (ncount==10)  return (result);
  84.     } else break;
  85.   }
  86.   return result;
  87. }
  88.  
  89.  
  90.  
  91.  
  92. uint32_t *WStackAddrs = NULL;
  93. uint32_t *WStackBuffer;
  94. unsigned int WStackNCycles;
  95. unsigned int WStackMaxCycles=100000;
  96. CVAddressModifier *WStackAMs;
  97. CVDataWidth *WStackDWs;
  98. CVErrorCodes *WStackECs;
  99.  
  100.  
  101. uint32_t *RStackAddrs =NULL;
  102. unsigned int RStackNCycles;
  103. unsigned int RStackMaxCycles=100000;
  104. CVAddressModifier *RStackAMs;
  105. CVDataWidth *RStackDWs;
  106. CVErrorCodes *RStackECs;
  107.  
  108.  
  109.  
  110. short CAEN_VME_appendread(unsigned long Address, void *Data, CVAddressModifier AM, CVDataWidth DW) {
  111.   if (RStackNCycles<RStackMaxCycles) {
  112.     RStackAddrs[RStackNCycles]  = Address;
  113.     RStackAMs[RStackNCycles]    = AM;
  114.     RStackDWs[RStackNCycles]    = DW;
  115.     //int i=RStackNCycles;
  116.     //printf("AppendRead %d ADDR= 0x%0X AM= 0x%0X DW= 0x%0X \n", i,RStackAddrs[i],RStackAMs[i], RStackDWs[i]);
  117.     RStackNCycles++;
  118.   } else {
  119.     printf("Increase DataBuffer RStackMaxCycles =%d xx\n", RStackMaxCycles);
  120.   }
  121.   return RStackNCycles;
  122. }
  123.  
  124. short CAEN_VME_appendwrite(unsigned long Address, void *Data, CVAddressModifier AM, CVDataWidth DW) {
  125.   if (WStackNCycles<WStackMaxCycles) {
  126.     WStackAddrs[WStackNCycles]  = Address;
  127.     WStackBuffer[WStackNCycles] = *((uint32_t *) Data);
  128.     WStackAMs[WStackNCycles]    = AM;
  129.     WStackDWs[WStackNCycles]    = DW;
  130.     WStackNCycles++;
  131.   } else {
  132.     printf("Increase DataBuffer WStackMaxCycles =%d xx\n", WStackMaxCycles);
  133.   }
  134.   return 0;
  135. }
  136.  
  137. short CAEN_VME_MultiReadReset() {
  138.   RStackNCycles=0;
  139.   if (RStackAddrs!=NULL) return 0;
  140.   RStackAddrs = (uint32_t *) malloc( RStackMaxCycles*sizeof(uint32_t));
  141.  
  142.   RStackAMs  = (CVAddressModifier *) malloc(RStackMaxCycles*sizeof(CVAddressModifier));
  143.   RStackDWs = (CVDataWidth *) malloc(RStackMaxCycles*sizeof(CVDataWidth));
  144.   RStackECs = (CVErrorCodes *) malloc(RStackMaxCycles*sizeof(CVErrorCodes));
  145.  
  146.   return 0;
  147. }
  148.  
  149. short CAEN_VME_MultiWriteReset() {
  150.   WStackNCycles=0;
  151.   if (WStackAddrs!=NULL) return 0;
  152.   WStackBuffer = (uint32_t *) malloc( WStackMaxCycles*sizeof(uint32_t));
  153.   WStackAddrs = (uint32_t *) malloc( WStackMaxCycles*sizeof(uint32_t));
  154.   WStackAMs  = (CVAddressModifier *) malloc(WStackMaxCycles*sizeof(CVAddressModifier));
  155.   WStackDWs = (CVDataWidth *) malloc(WStackMaxCycles*sizeof(CVDataWidth));
  156.   WStackECs = (CVErrorCodes *) malloc(WStackMaxCycles*sizeof(CVErrorCodes));
  157.  
  158.   return 0;
  159. }
  160.  
  161. short CAEN_VME_MultiReadExecute(long Handle, uint32_t *  Data) {
  162.   //printf("len=%d\n",RStackNCycles);
  163.   int i=0;
  164.   CVErrorCodes result =CAENVME_MultiRead(Handle, RStackAddrs,  Data,  RStackNCycles, RStackAMs, RStackDWs, RStackECs);
  165.   if (result !=cvSuccess ) {
  166.     VMEerrors++;
  167.     printf("CAENVME_MultiRead at 0x%0X failed!  err=%s RStackNCycles=%d\n", RStackAddrs[0], CAENVME_DecodeError(result), RStackNCycles);
  168.     for (i=0; i<RStackNCycles; i++) {
  169.       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]) );
  170.     }
  171.     exit(result);
  172.   }
  173.   return  RStackNCycles;
  174. }
  175.  
  176. short CAEN_VME_MultiWriteExecute(long Handle) {
  177.   int i=0;
  178.   CVErrorCodes result =CAENVME_MultiWrite(Handle, WStackAddrs, WStackBuffer,  WStackNCycles, WStackAMs, WStackDWs, WStackECs);
  179.   if (result !=cvSuccess ) {
  180.     printf("CAENVME_MultiWrite at 0x%0X failed!  err=%s WStackNCycles=%d\n", WStackAddrs[0], CAENVME_DecodeError(result), WStackNCycles);
  181.  
  182.     for (i=0; i<WStackNCycles; i++) {
  183.       if (WStackECs[i]!=cvSuccess )
  184.         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]) );
  185.     }
  186.     exit(result);
  187.   }
  188.   return  WStackNCycles;
  189. }
  190.  
  191.  
  192. short __stdcall CAEN_VME_R( uint16_t AddressModifier, uint16_t DataWidth,  uint32_t VME_Address, uint32_t *Data) {
  193.   return CAEN_VME_read( CAEN_udev,   VME_Address, (void *)Data,  (CVAddressModifier) AddressModifier, (CVDataWidth) DataWidth);
  194. }
  195.  
  196. short __stdcall CAEN_VME_W( uint16_t AddressModifier, uint16_t DataWidth,  uint32_t VME_Address, uint32_t Data) {
  197.   return CAEN_VME_write( CAEN_udev,   VME_Address, &Data,  (CVAddressModifier) AddressModifier, (CVDataWidth) DataWidth);
  198. }
  199.  
  200. short __stdcall CAEN_VME_MW( uint16_t AM, uint16_t DW, uint32_t VME_Address, uint32_t Data) {
  201.  
  202.   return CAEN_VME_appendwrite(  VME_Address, &Data ,(CVAddressModifier) AM , (CVDataWidth) DW) ;
  203.  
  204. }
  205. short __stdcall CAEN_VME_MWRST( void ) {
  206.  
  207.   return CAEN_VME_MultiWriteReset();
  208.  
  209. }
  210. short __stdcall CAEN_VME_MWEXEC( void ) {
  211.  
  212.   return CAEN_VME_MultiWriteExecute(CAEN_udev);
  213.  
  214. }
  215.  
  216. short __stdcall CAEN_VME_MR( uint16_t AM, uint16_t DW, uint32_t VME_Address, uint32_t *Data) {
  217.  
  218.   return   CAEN_VME_appendread(  VME_Address, Data ,(CVAddressModifier) AM , (CVDataWidth) DW) ;
  219.  
  220. }
  221. short __stdcall CAEN_VME_MRRST( void ) {
  222.  
  223.   return CAEN_VME_MultiReadReset();
  224.  
  225. }
  226. short __stdcall CAEN_VME_MREXEC(  uint32_t *Data  ) {
  227.   return CAEN_VME_MultiReadExecute(CAEN_udev, Data);
  228.  
  229.  
  230. }
  231.  
  232.  
  233.  
  234.