Subversion Repositories f9daq

Rev

Rev 122 | 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>
122 f9daq 4
#include <stdint.h>
77 f9daq 5
 
6
#include <CAENVMElib.h>
7
#include <CAENVMEoslib.h>
8
#include <CAENVMEtypes.h>
9
 
100 f9daq 10
#ifndef _CVI_
11
#define __stdcall
12
#define UCHAR unsigned char
13
#define  _VI_FUNC
14
#endif
77 f9daq 15
 
16
#include "CAENV1718.h"
17
 
100 f9daq 18
 
19
 
77 f9daq 20
int32_t CAEN_udev;
21
 
22
int VMEerrors;
23
 
122 f9daq 24
int CAEN_VME_start (char* serial) {
77 f9daq 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
  }
100 f9daq 32
  if (serial == NULL) serial = (char *) malloc(100*sizeof(char));
77 f9daq 33
  CAENVME_BoardFWRelease(CAEN_udev, serial);
122 f9daq 34
 
77 f9daq 35
  return result;
36
}
37
 
38
 
122 f9daq 39
short CAEN_VME_read(long Handle, unsigned long Address, void *Data, CVAddressModifier AM, CVDataWidth DW) {
77 f9daq 40
 
122 f9daq 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;
77 f9daq 53
}
54
 
122 f9daq 55
int CAEN_VME_BltRead(long Handle, unsigned long Address, void *Data, int size, CVAddressModifier AM, CVDataWidth DW) {
77 f9daq 56
  int count=0;
122 f9daq 57
  CVErrorCodes result;
77 f9daq 58
  int ncount=0;
122 f9daq 59
 
60
  while (1) {
61
 
77 f9daq 62
    result = CAENVME_BLTReadCycle(Handle, Address, Data, size, AM, DW , &count);
63
    if (result !=cvSuccess ) {
122 f9daq 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);
77 f9daq 68
    } else break;
122 f9daq 69
  }
77 f9daq 70
  return count;
71
}
72
 
122 f9daq 73
short CAEN_VME_write(long Handle, unsigned long Address, void *Data, CVAddressModifier AM, CVDataWidth DW) {
77 f9daq 74
 
122 f9daq 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;
77 f9daq 87
}
88
 
89
 
90
 
91
 
122 f9daq 92
uint32_t *WStackAddrs = NULL;
93
uint32_t *WStackBuffer;
132 f9daq 94
unsigned int WStackNCycles;
95
unsigned int WStackMaxCycles=100000;
122 f9daq 96
CVAddressModifier *WStackAMs;
97
CVDataWidth *WStackDWs;
77 f9daq 98
CVErrorCodes *WStackECs;
99
 
100
 
122 f9daq 101
uint32_t *RStackAddrs =NULL;
132 f9daq 102
unsigned int RStackNCycles;
103
unsigned int RStackMaxCycles=100000;
122 f9daq 104
CVAddressModifier *RStackAMs;
105
CVDataWidth *RStackDWs;
77 f9daq 106
CVErrorCodes *RStackECs;
107
 
108
 
109
 
122 f9daq 110
short CAEN_VME_appendread(unsigned long Address, void *Data, CVAddressModifier AM, CVDataWidth DW) {
111
  if (RStackNCycles<RStackMaxCycles) {
77 f9daq 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 {
132 f9daq 119
    printf("Increase DataBuffer RStackMaxCycles =%d xx\n", RStackMaxCycles);
77 f9daq 120
  }
121
  return RStackNCycles;
122
}
123
 
122 f9daq 124
short CAEN_VME_appendwrite(unsigned long Address, void *Data, CVAddressModifier AM, CVDataWidth DW) {
125
  if (WStackNCycles<WStackMaxCycles) {
77 f9daq 126
    WStackAddrs[WStackNCycles]  = Address;
127
    WStackBuffer[WStackNCycles] = *((uint32_t *) Data);
128
    WStackAMs[WStackNCycles]    = AM;
129
    WStackDWs[WStackNCycles]    = DW;
130
    WStackNCycles++;
131
  } else {
132 f9daq 132
    printf("Increase DataBuffer WStackMaxCycles =%d xx\n", WStackMaxCycles);
77 f9daq 133
  }
134
  return 0;
135
}
136
 
122 f9daq 137
short CAEN_VME_MultiReadReset() {
77 f9daq 138
  RStackNCycles=0;
139
  if (RStackAddrs!=NULL) return 0;
140
  RStackAddrs = (uint32_t *) malloc( RStackMaxCycles*sizeof(uint32_t));
122 f9daq 141
 
77 f9daq 142
  RStackAMs  = (CVAddressModifier *) malloc(RStackMaxCycles*sizeof(CVAddressModifier));
143
  RStackDWs = (CVDataWidth *) malloc(RStackMaxCycles*sizeof(CVDataWidth));
144
  RStackECs = (CVErrorCodes *) malloc(RStackMaxCycles*sizeof(CVErrorCodes));
122 f9daq 145
 
77 f9daq 146
  return 0;
147
}
148
 
122 f9daq 149
short CAEN_VME_MultiWriteReset() {
77 f9daq 150
  WStackNCycles=0;
151
  if (WStackAddrs!=NULL) return 0;
122 f9daq 152
  WStackBuffer = (uint32_t *) malloc( WStackMaxCycles*sizeof(uint32_t));
153
  WStackAddrs = (uint32_t *) malloc( WStackMaxCycles*sizeof(uint32_t));
77 f9daq 154
  WStackAMs  = (CVAddressModifier *) malloc(WStackMaxCycles*sizeof(CVAddressModifier));
155
  WStackDWs = (CVDataWidth *) malloc(WStackMaxCycles*sizeof(CVDataWidth));
156
  WStackECs = (CVErrorCodes *) malloc(WStackMaxCycles*sizeof(CVErrorCodes));
122 f9daq 157
 
77 f9daq 158
  return 0;
159
}
160
 
122 f9daq 161
short CAEN_VME_MultiReadExecute(long Handle, uint32_t *  Data) {
77 f9daq 162
  //printf("len=%d\n",RStackNCycles);
163
  int i=0;
164
  CVErrorCodes result =CAENVME_MultiRead(Handle, RStackAddrs,  Data,  RStackNCycles, RStackAMs, RStackDWs, RStackECs);
122 f9daq 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;
77 f9daq 174
}
175
 
122 f9daq 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;
77 f9daq 189
}
190
 
191
 
122 f9daq 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);
77 f9daq 194
}
195
 
122 f9daq 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);
77 f9daq 198
}
199
 
122 f9daq 200
short __stdcall CAEN_VME_MW( uint16_t AM, uint16_t DW, uint32_t VME_Address, uint32_t Data) {
77 f9daq 201
 
100 f9daq 202
  return CAEN_VME_appendwrite(  VME_Address, &Data ,(CVAddressModifier) AM , (CVDataWidth) DW) ;
77 f9daq 203
 
204
}
122 f9daq 205
short __stdcall CAEN_VME_MWRST( void ) {
77 f9daq 206
 
207
  return CAEN_VME_MultiWriteReset();
208
 
209
}
122 f9daq 210
short __stdcall CAEN_VME_MWEXEC( void ) {
77 f9daq 211
 
212
  return CAEN_VME_MultiWriteExecute(CAEN_udev);
213
 
214
}
215
 
122 f9daq 216
short __stdcall CAEN_VME_MR( uint16_t AM, uint16_t DW, uint32_t VME_Address, uint32_t *Data) {
77 f9daq 217
 
100 f9daq 218
  return   CAEN_VME_appendread(  VME_Address, Data ,(CVAddressModifier) AM , (CVDataWidth) DW) ;
77 f9daq 219
 
220
}
122 f9daq 221
short __stdcall CAEN_VME_MRRST( void ) {
77 f9daq 222
 
122 f9daq 223
  return CAEN_VME_MultiReadReset();
77 f9daq 224
 
225
}
122 f9daq 226
short __stdcall CAEN_VME_MREXEC(  uint32_t *Data  ) {
227
  return CAEN_VME_MultiReadExecute(CAEN_udev, Data);
77 f9daq 228
 
229
 
230
}
231
 
232
 
233