Subversion Repositories f9daq

Rev

Rev 97 | Rev 122 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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