AlcapDAQ  1
A3818Upgrade.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  #include <sys/time.h>
9 #endif
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <math.h>
13 #include <time.h>
14 #include <sys/types.h>
15 #include <sys/timeb.h>
16 #include <string.h>
17 #include <stdarg.h>
18 #include "CAENVMElib.h"
19 #include <time.h>
20 #include <a3818.h>
21 #include "p30.h"
22 #include "A3818Upgrade.h"
23 
24 // Define FIRMWARE regions into flash by using their start address (116-bit word address)
25 #define FIRST_FIRMWARE_PAGE_BASE_WORD_ADDRESS 0x000000
26 #define SECOND_FIRMWARE_PAGE_BASE_WORD_ADDRESS 0x200000
27 #define THIRD_FIRMWARE_PAGE_BASE_WORD_ADDRESS 0x400000
28 #define FOURTH_FIRMWARE_PAGE_BASE_WORD_ADDRESS 0x600000
29 
30 
31 /* get time in milliseconds */
32 static long get_time()
33 {
34 long time_ms;
35 
36 #ifdef WIN32
37  struct _timeb timebuffer;
38 
39  _ftime( &timebuffer );
40  time_ms = (long)timebuffer.time * 1000 + (long)timebuffer.millitm;
41 #else
42  struct timeval t1;
43  struct timezone tz;
44 
45  gettimeofday(&t1, &tz);
46  time_ms = (t1.tv_sec) * 1000 + t1.tv_usec / 1000;
47 #endif
48 
49  return time_ms;
50 }
51 
52 int A3818UpgradeFromFile(int32_t A3818_handle, FILE * binfile, int fwcopy, A3818Upgrade_Mode mode) {
53 
54  uint32_t baseAddress;
55  long currentTime, elapsedTime;
56  int finish;
57  unsigned char c, c1;
58  uint32_t bp;
59  uint32_t bufferLength;
60 
61  uint32_t *buffer;
62  unsigned int verifyErrors = 0;
63  buffer = malloc(BITSTREAM_BYTES * sizeof(uint32_t));
64  switch(fwcopy) {
65  case 0 :
67  break;
68  case 1 :
70  break;
71  case 2 :
73  break;
74  case 3 :
76  break;
77  default :
79  break;
80 
81  }
82 
83  bp = 0;
84  finish = 0;
85 
86  // Carica il bitsream in un buffer di memoria
87  currentTime = get_time();
88  while( !finish ) {
89 
90  c = (unsigned char)fgetc(binfile); // read one byte from file
91  c1 = (unsigned char)fgetc(binfile); // read one byte from file (Strataflash a 16 bit)
92 
93 #ifdef __SWAP__
94  swap = 0;
95 
96  // Swap primo byte
97  for( i = 0; i < 8; i++ )
98  if( c & (1 << i) )
99  swap = swap | (0x80 >> i);
100 
101  swap1 = 0;
102  // Swap secondo byte
103  for( i = 0; i < 8; i++ )
104  if( c1 & (1 << i) )
105  swap1 = swap1 | (0x80 >> i);
106 
107  buffer[bp] = (uint32_t) ((swap1 <<8) | swap); // HACK : swap o non swap?
108 #else
109  buffer[bp] = (uint32_t) ((c1 <<8) | c);
110 #endif // __SWAP__
111 
112  bp++;
113  if( feof(binfile) )
114  finish = 1;
115  } // end of while loop
116 
117  bufferLength = (--bp);
118 
119  if ((bufferLength*2) != BITSTREAM_BYTES) {
120  printf("\nERROR: Input BIN file length (%d bytes) is different than expected bitstream size (%d bytes). Exiting.....\n", bufferLength*2, BITSTREAM_BYTES);
121  free(buffer);
122  return -1;
123  }
124 
125  elapsedTime = get_time() - currentTime;
126  printf("\nBitstream (%d bytes) loaded in %ld milliseconds\n", bufferLength*2, elapsedTime);
127 
128  A3818_EnableBPIAccess(A3818_handle);
129 
130  /* Cancellazione della zona di flash riservata al firmware */
131  if ( (mode == A3818_UPGRADE_FULL ) ||
132  (mode == A3818_UPGRADE_ERASE_ONLY) ) {
133  printf("Erasing flash ");
134  fflush(stdout);
135  currentTime = get_time();
136  eraseFirmware(A3818_handle, baseAddress, fwcopy);
137  elapsedTime = get_time() - currentTime;
138  printf("\nFlash erased in %ld milliseconds\n", elapsedTime);
139  }
140 
141  /* Programmazione immagine firmware in flash */
142  if ( (mode == A3818_UPGRADE_FULL ) ) {
143  currentTime = get_time();
144  writeFlash(A3818_handle, buffer, bufferLength, baseAddress);
145  elapsedTime = get_time() - currentTime;
146  printf("\n%d 16-bit words programmed in %ld milliseconds\n", bufferLength, elapsedTime);
147  }
148 
149  /* Verifica immagine firmware in flash */
150  if ( (mode == A3818_UPGRADE_FULL ) ||
151  (mode == A3818_UPGRADE_VERIFY_ONLY) ) {
152  currentTime = get_time();
153  verifyErrors = verifyFlash(A3818_handle, buffer, bufferLength, baseAddress);
154  elapsedTime = get_time() - currentTime;
155  printf("\n%d 16-bit words verified in %ld milliseconds\n", bufferLength, elapsedTime);
156  }
157 
158  A3818_EnableSPIAccess(A3818_handle);
159 
160 
161  if( verifyErrors > 0 ) {
162  printf("\n\n%d errors found during verify!\n",verifyErrors);
163  } else {
164  printf("\n\nFirmware updated without errors. Written %d words\n",bufferLength);
165  }
166  free(buffer);
167  return 0;
168 }
169 
170 
171 int A3818UpgradeFromMem(int32_t A3818_handle, char* fwdata, int fwsize, int fwcopy, A3818Upgrade_Mode mode) {
172  int fi = 0;
173  uint32_t baseAddress;
174  long currentTime, elapsedTime;
175  int finish;
176  unsigned char c, c1;
177  uint32_t bp;
178  uint32_t bufferLength;
179 
180  uint32_t *buffer;
181  unsigned int verifyErrors = 0;
182  buffer = malloc(BITSTREAM_BYTES * sizeof(uint32_t));
183  switch(fwcopy) {
184  case 0 :
186  break;
187  case 1 :
189  break;
190  case 2 :
192  break;
193  case 3 :
195  break;
196  default :
198  break;
199 
200  }
201 
202  bp = 0;
203  finish = 0;
204 
205  // Carica il bitsream in un buffer di memoria
206  currentTime = get_time();
207  while( !finish ) {
208 
209  c = (unsigned char)fwdata[fi++]; // read one byte from file
210  c1 = (unsigned char)fwdata[fi++]; // read one byte from file (Strataflash a 16 bit)
211 
212 #ifdef __SWAP__
213  swap = 0;
214 
215  // Swap primo byte
216  for( i = 0; i < 8; i++ )
217  if( c & (1 << i) )
218  swap = swap | (0x80 >> i);
219 
220  swap1 = 0;
221  // Swap secondo byte
222  for( i = 0; i < 8; i++ )
223  if( c1 & (1 << i) )
224  swap1 = swap1 | (0x80 >> i);
225 
226  buffer[bp] = (uint32_t) ((swap1 <<8) | swap); // HACK : swap o non swap?
227 #else
228  buffer[bp] = (uint32_t) ((c1 <<8) | c);
229 #endif // __SWAP__
230 
231  bp++;
232  if( fi >= fwsize )
233  finish = 1;
234  } // end of while loop
235 
236  bufferLength = (--bp);
237 
238  if ((bufferLength*2) != BITSTREAM_BYTES) {
239  printf("\nERROR: Input BIN file length (%d bytes) is different than expected bitstream size (%d bytes). Exiting.....\n", bufferLength*2, BITSTREAM_BYTES);
240  free(buffer);
241  return -1;
242  }
243 
244  elapsedTime = get_time() - currentTime;
245  printf("\nBitstream (%d bytes) loaded in %ld milliseconds\n", bufferLength*2, elapsedTime);
246 
247  A3818_EnableBPIAccess(A3818_handle);
248 
249  /* Cancellazione della zona di flash riservata al firmware */
250  if ( (mode == A3818_UPGRADE_FULL ) ||
251  (mode == A3818_UPGRADE_ERASE_ONLY) ) {
252  printf("Erasing flash ");
253  fflush(stdout);
254  currentTime = get_time();
255  eraseFirmware(A3818_handle, baseAddress, fwcopy);
256  elapsedTime = get_time() - currentTime;
257  printf("\nFlash erased in %ld milliseconds\n", elapsedTime);
258  }
259 
260  /* Programmazione immagine firmware in flash */
261  if ( (mode == A3818_UPGRADE_FULL ) ) {
262  currentTime = get_time();
263  writeFlash(A3818_handle, buffer, bufferLength, baseAddress);
264  elapsedTime = get_time() - currentTime;
265  printf("\n%d 16-bit words programmed in %ld milliseconds\n", bufferLength, elapsedTime);
266  }
267 
268  /* Verifica immagine firmware in flash */
269  if ( (mode == A3818_UPGRADE_FULL ) ||
270  (mode == A3818_UPGRADE_VERIFY_ONLY) ) {
271  currentTime = get_time();
272  verifyErrors = verifyFlash(A3818_handle, buffer, bufferLength, baseAddress);
273  elapsedTime = get_time() - currentTime;
274  printf("\n%d 16-bit words verified in %ld milliseconds\n", bufferLength, elapsedTime);
275  }
276 
277  A3818_EnableSPIAccess(A3818_handle);
278 
279 
280  if( verifyErrors > 0 ) {
281  printf("\n\n%d errors found during verify!\n",verifyErrors);
282  } else {
283  printf("\n\nFirmware updated without errors. Written %d words\n",bufferLength);
284  }
285  free(buffer);
286  return 0;
287 }
288