Subversion Repositories f9daq

Rev

Rev 77 | Rev 100 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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