AlcapDAQ  1
CAENBridgeUpgrade.c
Go to the documentation of this file.
1 // CaenVMEUpgrade.c : Defines the entry point for the console application.
2 //
3 // 06/06/06 - Cambiato l'ordine dei parametri della Init. Link e BdNum erano
4 // invertiti quindi non funzionava l'upgrade con piu' di una A2818.
5 
6 #ifdef LINUX
7  #include <inttypes.h>
8 #endif
9 #include <stdlib.h>
10 #include <stdio.h>
11 
12 #include <math.h>
13 #include <time.h>
14 #include <sys/types.h>
15 #include <sys/timeb.h>
16 #include <string.h>
17 
18 #include "CFASegment.h"
19 
20 #ifdef LINUX
21 
22 typedef unsigned char byte;
23 typedef int VARIANT_BOOL;
24 #define FALSE (0)
25 #define TRUE (-1)
26 
27 #endif
28 
29 #include <stdarg.h>
30 #include <a3818.h>
31 #include "p30.h"
32 #include "A3818Upgrade.h"
33 #include "flash.h"
34 #include <CAENVMElib.h>
35 
36 // Define FIRMWARE regions into flash by using their start address (116-bit word address)
37 #define FIRST_FIRMWARE_PAGE_BASE_WORD_ADDRESS 0x000000
38 #define SECOND_FIRMWARE_PAGE_BASE_WORD_ADDRESS 0x200000
39 #define THIRD_FIRMWARE_PAGE_BASE_WORD_ADDRESS 0x400000
40 #define FOURTH_FIRMWARE_PAGE_BASE_WORD_ADDRESS 0x600000
41 
42 #define MAIN_MEM_PAGE_READ 0xD2
43 #define MAIN_MEM_PAGE_PROG_TH_BUF1 0x82
44 #define PAGE_ERASE 0x81
45 
46 #define FIRST_PAGE_BCK 1408
47 #define FIRST_PAGE_STD 768
48 #define CF_SIZE 166965
49 
50 /* get time in milliseconds */
51 /*
52 static long get_time()
53 {
54 long time_ms;
55 
56 #ifdef WIN32
57  struct _timeb timebuffer;
58 
59  _ftime( &timebuffer );
60  time_ms = (long)timebuffer.time * 1000 + (long)timebuffer.millitm;
61 #else
62  struct timeval t1;
63  struct timezone tz;
64 
65  gettimeofday(&t1, &tz);
66  time_ms = (t1.tv_sec) * 1000 + t1.tv_usec / 1000;
67 #endif
68 
69  return time_ms;
70 }
71 */
72 
73 void usage(void)
74 {
75  printf("\nCAENBridgeUpgrade V2.2 \n\n");
76  printf("Usage: CAENBridgeUpgrade V1718 <VMEDevIndex> [FlashFile] [standard|backup] [-rbf]\n");
77  printf(" CAENBridgeUpgrade V2718 <VMEDevIndex> <PCIDevIndex> [FlashFile] [standard|backup] [-rbf]\n");
78  printf(" CAENBridgeUpgrade A2719 <VMEDevIndex> <PCIDevIndex> [FlashFile] [standard|backup] [-rbf]\n");
79  printf(" CAENBridgeUpgrade A2818 <PCIDevIndex> [FlashFile] [standard|backup] [-rbf]\n");
80  printf(" CAENBridgeUpgrade A3818 <PCIDevIndex> [FlashFile] [TargetFlashImage (0,1,2,3)] [-rbf]\n");
81  printf("\n");
82  printf("Or: CAENBridgeUpgrade V1718 <VMEDevIndex> [FwRevFile] fwrev\n");
83  printf(" CAENBridgeUpgrade V2718 <VMEDevIndex> <PCIDevIndex> [FwRevFile] fwrev\n");
84  printf(" CAENBridgeUpgrade A2719 <VMEDevIndex> <PCIDevIndex> [FwRevFile] fwrev\n");
85  printf(" CAENBridgeUpgrade A2818 <PCIDevIndex> [FwRevFile] fwrev\n");
86  printf(" CAENBridgeUpgrade A3818 <PCIDevIndex> [FwRevFile] fwrev\n");
87 }
88 
90 {
91  if(( strcmp(arg, "V1718") == 0 )|| ( strcmp(arg, "v1718") == 0 )) return cvV1718;
92  if(( strcmp(arg, "V2718") == 0 )|| ( strcmp(arg, "v2718") == 0 )) return cvV2718;
93  if(( strcmp(arg, "A2719") == 0 )|| ( strcmp(arg, "a2719") == 0 )) return cvA2719;
94  if(( strcmp(arg, "A2818") == 0 )|| ( strcmp(arg, "a2818") == 0 )) return cvA2818;
95  if(( strcmp(arg, "A3818") == 0 )|| ( strcmp(arg, "a3818") == 0 )) return cvA3818;
96  return -1;
97 }
98 
99 int validateModel(FILE* fin, CVBoardTypes model, char** fwdata, int* fwsize) {
100  //FILE* fin = fopen(fname, "rb");
101  while (!feof(fin)) {
102  int last;
103  CFASegmentPtr segm = newSegment(fin, &last);
104  if (segm == NULL) {
105  fclose(fin);
106  return -2;
107  }
108  if (getSegmentType(segm) == CFA_SEGMENT_TYPE_2) {
109  CFASegmentType2* t2s = toType2(segm);
110  int i;
111  for (i = 0; i < t2s->numModels; i++) {
112  if (model == t2s->models[i]->modelId) {
113  *fwdata = malloc(t2s->fwSizeBytes);
114  memcpy(*fwdata, t2s->fwData, t2s->fwSizeBytes);
115  *fwsize = t2s->fwSizeBytes;
116  deleteSegment(segm);
117  return 0;
118  }
119  }
120  }
121  deleteSegment(segm);
122  if (last) break;
123  }
124  return -1;
125 }
126 
127 int main(int argc, char *argv[])
128 {
129  FILE *stream =NULL;
130  char* fwdata;
131  int fwsize;
132  CVBoardTypes Board;
133  short Link;
134  short Device;
135  int32_t BHandle;
136  int fwcopy;
137  int fwread;
138  int cfa = 1;
139  int fi;
140  int TargetFlashImage = 0;
141  int finish, i, verify_err,ret;
142  int bp, bcnt, pp;
143  unsigned char pdw[264], pdr[264], c;
144  cvFlashAccess flash;
145  // char FWRel[20];
146 
147  if( argc < 4 ) {
148  usage();
149  exit(1);
150  }
151 
152  fwcopy = 0;
153  fwread = 0;
154  cfa = 0;
155  fi = 0;
156 
157  switch( (Board = DecodeDev(argv[1])) ) {
158  case cvV1718:
159  case cvA2818:
160  if( argc >= 5 ) {
161  if( strcmp((char*)argv[4], "standard" ) == 0 ) {
162  fwcopy = 0;
163  } else {
164  if( strcmp((char*)argv[4], "backup") == 0 ) {
165  fwcopy = 1;
166  } else if ( strcmp((char*)argv[4], "fwrev") == 0 ) {
167  fwread = 1;
168  } else {
169  usage();
170  exit(1);
171  }
172  }
173  if (argc >= 6 && (strcmp(argv[5], "-rbf") == 0)) {
174  cfa = 0;
175  }
176  }
177  if( Board == cvV1718 ) {
178  flash.Sel_Flash = 0x04; // FLENA
179  flash.RW_Flash = 0x03;
180  Device = atoi((const char*)argv[2]);
181  Link = 0;
182  } else {
183  flash.Sel_Flash = 0x14;
184  flash.RW_Flash = 0x10;
185  Device = 0;
186  Link = atoi((const char*)argv[2]);
187  }
188  if(!fwread && (stream = fopen((const char*)argv[3], "rb" )) == NULL ) {
189  printf( "File %s not found\n",(char *)argv[3]);
190  exit(1);
191  }
192  if (!fwread)
193  printf("\n\nLoading file %s\n", argv[3]);
194  break;
195  case cvV2718:
196  case cvA2719:
197  if( argc < 5 ) {
198  usage();
199  exit(1);
200  }
201  if( argc >= 6 ) {
202  if( strcmp((char*)argv[5], "standard") == 0 ) {
203  fwcopy = 0;
204  } else {
205  if( strcmp((char*)argv[5], "backup") == 0 ) {
206  fwcopy = 1;
207  } else if ( strcmp((char*)argv[5], "fwrev") == 0 ) {
208  fwread = 1;
209  } else {
210  usage();
211  exit(1);
212  }
213  }
214  if (argc >= 7 && strcmp(argv[6], "-rbf") == 0) {
215  cfa = 0;
216  }
217  }
218  Device = atoi((const char*) argv[3]);
219  Link = atoi((const char*) argv[2]);
220  if(!fwread && (stream = fopen((const char*)argv[4], "rb" )) == NULL ) {
221  printf( "File %s not found\n",argv[4]);
222  exit(1);
223  }
224  if (Board == cvA2719) {
225  flash.Sel_Flash = 0x03;
226  flash.RW_Flash = 0x02;
227  }else {
228  flash.Sel_Flash = 0x04;
229  flash.RW_Flash = 0x03;
230  }
231  if (!fwread)
232  printf("\n\nLoading file %s\n", argv[4]);
233  break;
234  case cvA3818:
235  if( argc >= 5 ) {
236  if (strcmp((char*)argv[4], "fwrev")==0) fwread = 1;
237  else TargetFlashImage = atoi((const char*)argv[4]);
238  if (argc >= 6 && strcmp(argv[5], "-rbf") == 0) {
239  cfa = 0;
240  }
241  }
242  Device = 0;
243  Link = atoi((const char*)argv[2]);
244  if(!fwread && (stream = fopen((const char*)argv[3], "rb" )) == NULL ) {
245  printf( "File %s not found\n",argv[3]);
246  exit(1);
247  }
248  if (!fwread)
249  printf("\n\nLoading file %s\n", argv[3]);
250  break;
251  default:
252  usage();
253  exit(1);
254  }
255 
256  if (cfa) {
257  printf("CFA mode\n");
258  if (validateModel(stream, Board, &fwdata, &fwsize) < 0) {
259  fclose(stream);
260  printf("\n\nCannot find a compatible firmware in the CFA file\n");
261  exit(99);
262  }
263  }
264 
265  // initialize the Board
266  if( CAENVME_Init(Board, Link, Device, &BHandle) != cvSuccess ) {
267  printf("\n\nError opening the device\n");
268  exit(1);
269  }
270 
271  if (fwread) {
272  char fwrev[100];
273  int ret; // major, minor;
274  if ((Board == cvV2718) || (Board == cvA2719)) {
275  stream = fopen((const char*)argv[4], "w" );
276  }
277  else {
278  stream = fopen((const char*)argv[3], "w" );
279  }
280  memset(fwrev,0,100);
281  ret = CAENVME_BoardFWRelease(BHandle,fwrev);
282  if (ret) {
283  printf("\n\nError reading firmware revision\n");
284  CAENVME_End(BHandle);
285  exit(1);
286  }
287  //major = fwrev >> 8;
288  // minor = fwrev & 0xFF;
289  //fprintf(stream,"Firmware revision = %d.%02d",major,minor);
290  fprintf(stream,"Firmware revision = %s",fwrev);
291  fclose(stream);
292  printf("\n\nFirmware revision read successfully\n");
293  CAENVME_End(BHandle);
294  exit(0);
295  }
296 
297  if (Board == cvA3818) {
298  if (!cfa) ret = A3818UpgradeFromFile(BHandle,stream,TargetFlashImage,A3818_UPGRADE_FULL);
299  else ret = A3818UpgradeFromMem(BHandle,fwdata,fwsize,TargetFlashImage,A3818_UPGRADE_FULL);
300  CAENVME_End(BHandle);
301  exit(0);
302  }
303 
304  bcnt = 0; // byte counter
305 
306  if( fwcopy == 0 )
307  pp=FIRST_PAGE_STD; // page pointer
308  else
309  pp=FIRST_PAGE_BCK;
310 
311  bp = 0; // byte pointer in the page
312  finish = 0; // it goes high when eof is found
313  verify_err = 0; // number of errors during verify
314 
315  flash.Handle = BHandle;
316  flash.RegSize = 4;
317  flash.FlashEnable = 1;
318  flash.PageSize = 264;
319 
320  if (Board == cvA2719) {
321  uint32_t reg32;
322  int res = 0;
323 
324  reg32 = !flash.FlashEnable;
325  res |= CAENVME_WriteRegister(flash.Handle, flash.Sel_Flash, reg32);
326  // Enable flash
327  res |= CAENVME_WriteRegister(flash.Handle, flash.Sel_Flash, flash.FlashEnable);
328 
329  // Opcode
331 
332  // Disable flash
333  reg32 = !flash.FlashEnable;
334  res |= CAENVME_WriteRegister(flash.Handle, flash.Sel_Flash, reg32);
335  }
336 
337  while( !finish ) {
338  if (!cfa) c = (unsigned char)fgetc(stream); // read one byte from file
339  else c = fwdata[fi++];
340  // mirror byte (lsb becomes msb)
341  pdw[bp] = 0;
342  for( i = 0; i < 8; i++ )
343  if( c & (1 << i) )
344  pdw[bp] = pdw[bp] | (0x80 >> i);
345  bp++;
346  bcnt++;
347  if( !cfa && feof(stream) ) finish = 1;
348  else if (cfa && fi >= fwsize) finish = 1;
349  //write and verify a page
350  if( (bp == 264) || finish ) {
351  printf(".");
352  fflush(stdout);
353  if ((Board == cvV1718) || (Board == cvV2718) || (Board == cvA2818) || (Board == cvA2719)) {
354  CAENVME_WriteFlashPage(BHandle, pdw, pp); // write page
355  CAENVME_ReadFlashPage(BHandle, pdr, pp); // read page
356  }
357  else {
358  WriteFlashPage(&flash, pdw, pp); // write page
359  ReadFlashPage(&flash, pdr, pp);
360 
361  }
362  for( i = 0; i < bp; i++ ) { // verify page
363  if( pdr[i] != pdw[i] ) {
364  printf("%x = %x\n",pdr[i],pdw[i]);
365  printf("\n\n Writing or Verify error at page %d - %d\n", pp, i);
366  verify_err = 1;
367  finish = 1;
368  i = bp;
369  }
370  }
371  bp = 0;
372  pp++;
373  }
374  } // end of while loop
375 
376 
377  fclose(stream);
378 
379  CAENVME_End(BHandle);
380 
381  if( verify_err > 0 ) {
382  printf("\n\n%d errors found during verify!\n",verify_err);
383  printf("Firmware upgrade not done. Move jumper to BCK to restore\n\n");
384  return 1;
385  } else {
386  printf("\n\nFirmware updated without errors. Written %d bytes\n",bcnt);
387  printf("Power cycle to load the new firmware\n\n");
388  return 0;
389  }
390 }
391